Skip to content

Commit

Permalink
CCIP-1261 Don't cache DB values on the USDC Reader level (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara authored Nov 6, 2023
1 parent 147e0ab commit af2deee
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 25 deletions.
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (o *ObservedORM) SelectLogs(start, end int64, address common.Address, event
})
}

func (o *ObservedORM) IndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash, qopts ...pg.QOpt) ([]Log, error) {
func (o *ObservedORM) SelectIndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash, qopts ...pg.QOpt) ([]Log, error) {
return withObservedQueryAndResults(o, "IndexedLogsByTxHash", func() ([]Log, error) {
return o.ORM.SelectIndexedLogsByTxHash(eventSig, txHash, qopts...)
})
Expand Down
21 changes: 3 additions & 18 deletions core/services/ocr2/plugins/ccip/tokendata/usdc/usdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"net/url"
"strings"
"sync"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
Expand Down Expand Up @@ -67,9 +66,6 @@ type TokenDataReader struct {
lggr logger.Logger
usdcReader ccipdata.USDCReader
attestationApi *url.URL
// Cache of sequence number -> usdc message body
usdcMessageHashCache map[uint64][]byte
usdcMessageHashCacheMutex sync.Mutex
}

type attestationResponse struct {
Expand All @@ -81,10 +77,9 @@ var _ tokendata.Reader = &TokenDataReader{}

func NewUSDCTokenDataReader(lggr logger.Logger, usdcReader ccipdata.USDCReader, usdcAttestationApi *url.URL) *TokenDataReader {
return &TokenDataReader{
lggr: lggr,
usdcReader: usdcReader,
attestationApi: usdcAttestationApi,
usdcMessageHashCache: make(map[uint64][]byte),
lggr: lggr,
usdcReader: usdcReader,
attestationApi: usdcAttestationApi,
}
}

Expand Down Expand Up @@ -130,21 +125,11 @@ func encodeMessageAndAttestation(messageBody []byte, attestation string) ([]byte
}

func (s *TokenDataReader) getUSDCMessageBody(ctx context.Context, msg internal.EVM2EVMOnRampCCIPSendRequestedWithMeta) ([]byte, error) {
s.usdcMessageHashCacheMutex.Lock()
defer s.usdcMessageHashCacheMutex.Unlock()

if body, ok := s.usdcMessageHashCache[msg.SequenceNumber]; ok {
return body, nil
}

parsedMsgBody, err := s.usdcReader.GetLastUSDCMessagePriorToLogIndexInTx(ctx, int64(msg.LogIndex), msg.TxHash)
if err != nil {
return []byte{}, err
}
s.lggr.Infow("Got USDC message body", "messageBody", hexutil.Encode(parsedMsgBody), "messageID", hexutil.Encode(msg.MessageId[:]))

// Save the attempt in the cache in case the external call fails
s.usdcMessageHashCache[msg.SequenceNumber] = parsedMsgBody
return parsedMsgBody, nil
}

Expand Down
6 changes: 0 additions & 6 deletions core/services/ocr2/plugins/ccip/tokendata/usdc/usdc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,4 @@ func TestGetUSDCMessageBody(t *testing.T) {
require.Equal(t, body, expectedBody)

usdcReader.AssertNumberOfCalls(t, "GetLastUSDCMessagePriorToLogIndexInTx", 1)

// Make another call and assert that the cache is used
body, err = usdcService.getUSDCMessageBody(context.Background(), internal.EVM2EVMOnRampCCIPSendRequestedWithMeta{})
require.NoError(t, err)
require.Equal(t, body, expectedBody)
usdcReader.AssertNumberOfCalls(t, "GetLastUSDCMessagePriorToLogIndexInTx", 1)
}

0 comments on commit af2deee

Please sign in to comment.