Skip to content

Commit

Permalink
CCIP-4711: Fix incorrect terminology use of message hash (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-gopalswami authored Dec 20, 2024
1 parent 4e2de67 commit 609f7b0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
24 changes: 12 additions & 12 deletions execute/tokendata/usdc/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,37 @@ func NewSequentialAttestationClient(

func (s *sequentialAttestationClient) Attestations(
ctx context.Context,
msgs map[cciptypes.ChainSelector]map[reader.MessageTokenID]cciptypes.Bytes,
messagesByChain map[cciptypes.ChainSelector]map[reader.MessageTokenID]cciptypes.Bytes,
) (map[cciptypes.ChainSelector]map[reader.MessageTokenID]AttestationStatus, error) {
outcome := make(map[cciptypes.ChainSelector]map[reader.MessageTokenID]AttestationStatus)

for chainSelector, hashes := range msgs {
for chainSelector, messagesByTokenID := range messagesByChain {
outcome[chainSelector] = make(map[reader.MessageTokenID]AttestationStatus)

for tokenID, messageHash := range hashes {
for tokenID, message := range messagesByTokenID {
s.lggr.Debugw(
"Fetching attestation from the API",
"chainSelector", chainSelector,
"messageHash", messageHash,
"message", message,
"messageTokenID", tokenID,
)
// TODO sequential processing
outcome[chainSelector][tokenID] = s.fetchSingleMessage(ctx, messageHash)
outcome[chainSelector][tokenID] = s.fetchSingleMessage(ctx, message)
}
}
return outcome, nil
}

func (s *sequentialAttestationClient) fetchSingleMessage(
ctx context.Context,
messageHash cciptypes.Bytes,
message cciptypes.Bytes,
) AttestationStatus {
response, _, err := s.client.Get(ctx, s.hasher.Hash(messageHash))
response, _, err := s.client.Get(ctx, s.hasher.Hash(message))
if err != nil {
return ErrorAttestationStatus(err)
}

return SuccessAttestationStatus(messageHash, response)
return SuccessAttestationStatus(message, response)
}

type FakeAttestationClient struct {
Expand All @@ -127,15 +127,15 @@ type FakeAttestationClient struct {

func (f FakeAttestationClient) Attestations(
_ context.Context,
msgs map[cciptypes.ChainSelector]map[reader.MessageTokenID]cciptypes.Bytes,
messagesByChain map[cciptypes.ChainSelector]map[reader.MessageTokenID]cciptypes.Bytes,
) (map[cciptypes.ChainSelector]map[reader.MessageTokenID]AttestationStatus, error) {
outcome := make(map[cciptypes.ChainSelector]map[reader.MessageTokenID]AttestationStatus)

for chainSelector, hashes := range msgs {
for chainSelector, messagesByTokenID := range messagesByChain {
outcome[chainSelector] = make(map[reader.MessageTokenID]AttestationStatus)

for tokenID, messageHash := range hashes {
outcome[chainSelector][tokenID] = f.Data[string(messageHash)]
for tokenID, message := range messagesByTokenID {
outcome[chainSelector][tokenID] = f.Data[string(message)]
}
}
return outcome, nil
Expand Down
12 changes: 6 additions & 6 deletions execute/tokendata/usdc/usdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func (u *TokenDataObserver) Observe(
// 1. Pick only messages that contain USDC tokens
usdcMessages := u.pickOnlyUSDCMessages(messages)

// 2. Fetch USDC messages hashes based on the `MessageSent (bytes message)` event
usdcMessageHashes, err := u.fetchUSDCMessageHashes(ctx, usdcMessages)
// 2. Fetch USDC messages by token id based on the `MessageSent (bytes message)` event
usdcMessagesByTokenID, err := u.fetchUSDCEventMessages(ctx, usdcMessages)
if err != nil {
return nil, err
}

// 3. Fetch attestations for USDC messages
attestations, err := u.fetchAttestations(ctx, usdcMessageHashes)
attestations, err := u.fetchAttestations(ctx, usdcMessagesByTokenID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (u *TokenDataObserver) pickOnlyUSDCMessages(
return usdcMessages
}

func (u *TokenDataObserver) fetchUSDCMessageHashes(
func (u *TokenDataObserver) fetchUSDCEventMessages(
ctx context.Context,
usdcMessages map[cciptypes.ChainSelector]map[reader.MessageTokenID]cciptypes.RampTokenAmount,
) (map[cciptypes.ChainSelector]map[reader.MessageTokenID]cciptypes.Bytes, error) {
Expand All @@ -128,7 +128,7 @@ func (u *TokenDataObserver) fetchUSDCMessageHashes(
}

// TODO Sequential reading USDC messages from the source chain
usdcHashes, err := u.usdcMessageReader.MessageHashes(ctx, chainSelector, u.destChainSelector, messages)
msgByTokenID, err := u.usdcMessageReader.MessagesByTokenID(ctx, chainSelector, u.destChainSelector, messages)
if err != nil {
u.lggr.Errorw(
"Failed fetching USDC events from the source chain",
Expand All @@ -139,7 +139,7 @@ func (u *TokenDataObserver) fetchUSDCMessageHashes(
)
return nil, err
}
output[chainSelector] = usdcHashes
output[chainSelector] = msgByTokenID
}
return output, nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/reader/usdc_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type USDCMessageReader interface {
MessageHashes(ctx context.Context,
MessagesByTokenID(ctx context.Context,
source, dest cciptypes.ChainSelector,
tokens map[MessageTokenID]cciptypes.RampTokenAmount,
) (map[MessageTokenID]cciptypes.Bytes, error)
Expand Down Expand Up @@ -133,7 +133,7 @@ func AllAvailableDomains() map[uint64]uint32 {
return destDomains
}

func (u usdcMessageReader) MessageHashes(
func (u usdcMessageReader) MessagesByTokenID(
ctx context.Context,
source, dest cciptypes.ChainSelector,
tokens map[MessageTokenID]cciptypes.RampTokenAmount,
Expand Down Expand Up @@ -207,7 +207,7 @@ func (u usdcMessageReader) MessageHashes(
// 3. Remapping database events to the proper MessageTokenID
out := make(map[MessageTokenID]cciptypes.Bytes)
for tokenID, messageID := range eventIDs {
messageHash, ok1 := messageSentEvents[messageID]
message, ok1 := messageSentEvents[messageID]
if !ok1 {
// Token not available in the source chain, it should never happen at this stage
u.lggr.Warnw("Message not found in the source chain",
Expand All @@ -217,7 +217,7 @@ func (u usdcMessageReader) MessageHashes(
)
continue
}
out[tokenID] = messageHash
out[tokenID] = message
}

return out, nil
Expand Down Expand Up @@ -323,7 +323,7 @@ func NewFakeUSDCMessageReader(messages map[MessageTokenID]cciptypes.Bytes) FakeU
return FakeUSDCMessageReader{Messages: messages}
}

func (f FakeUSDCMessageReader) MessageHashes(
func (f FakeUSDCMessageReader) MessagesByTokenID(
_ context.Context,
_, _ cciptypes.ChainSelector,
tokens map[MessageTokenID]cciptypes.RampTokenAmount,
Expand Down
8 changes: 4 additions & 4 deletions pkg/reader/usdc_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func Test_USDCMessageReader_New(t *testing.T) {
}
}

func Test_USDCMessageReader_MessageHashes(t *testing.T) {
func Test_USDCMessageReader_MessagesByTokenID(t *testing.T) {
ctx := tests.Context(t)
emptyChain := cciptypes.ChainSelector(sel.ETHEREUM_MAINNET.Selector)
emptyReader := reader.NewMockContractReaderFacade(t)
Expand Down Expand Up @@ -227,7 +227,7 @@ func Test_USDCMessageReader_MessageHashes(t *testing.T) {

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
hashes, err1 := usdcReader.MessageHashes(
messages, err1 := usdcReader.MessagesByTokenID(
tests.Context(t),
tc.sourceSelector,
tc.destSelector,
Expand All @@ -239,8 +239,8 @@ func Test_USDCMessageReader_MessageHashes(t *testing.T) {
require.ErrorContains(t, err1, tc.errorMessage)
} else {
require.NoError(t, err)
require.NotNil(t, hashes)
require.Equal(t, tc.expectedMsgIDs, maps.Keys(hashes))
require.NotNil(t, messages)
require.Equal(t, tc.expectedMsgIDs, maps.Keys(messages))
}
})
}
Expand Down

0 comments on commit 609f7b0

Please sign in to comment.