Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara committed Sep 20, 2024
1 parent 9564c52 commit d44065c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
7 changes: 6 additions & 1 deletion execute/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ func (p PluginFactory) NewReportingPlugin(
return nil, ocr3types.ReportingPluginInfo{}, fmt.Errorf("failed to validate exec offchain config: %w", err)
}

tokenDataObserver, err := tokendata.NewConfigBasedCompositeObservers(p.lggr, offchainConfig.TokenDataObservers)
tokenDataObserver, err := tokendata.NewConfigBasedCompositeObservers(
p.lggr,
offchainConfig.TokenDataObservers,
// FIXME contract readers should be `exteded` to support the new interface
nil, //p.contractReaders,
)
if err != nil {
return nil, ocr3types.ReportingPluginInfo{}, fmt.Errorf("failed to create token data observer: %w", err)
}
Expand Down
29 changes: 28 additions & 1 deletion execute/tokendata/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/smartcontractkit/chainlink-ccip/execute/exectypes"
"github.com/smartcontractkit/chainlink-ccip/execute/tokendata/usdc"
"github.com/smartcontractkit/chainlink-ccip/internal/reader"
"github.com/smartcontractkit/chainlink-ccip/pkg/contractreader"
"github.com/smartcontractkit/chainlink-ccip/pluginconfig"
)

Expand Down Expand Up @@ -49,21 +51,46 @@ type compositeTokenDataObserver struct {
func NewConfigBasedCompositeObservers(
lggr logger.Logger,
config []pluginconfig.TokenDataObserverConfig,
readers map[cciptypes.ChainSelector]contractreader.Extended,
) (*compositeTokenDataObserver, error) {
observers := make([]TokenDataObserver, len(config))
for i, c := range config {
// TODO consider if we can get rid of this switch stmt by moving the logic to the config
// e.g. observers[i] := config.CreateTokenDataObserver()
switch {
case c.USDCCCTPObserverConfig != nil:
observers[i] = usdc.NewTokenDataObserver(lggr, *c.USDCCCTPObserverConfig, nil, nil)
observer, err1 := createUSDCTokenObserver(lggr, c.USDCCCTPObserverConfig.Tokens, readers)
if err1 != nil {
return nil, err1
}
observers[i] = observer
default:
return nil, errors.New("unsupported token data observer")
}
}
return NewCompositeObservers(lggr, observers...), nil
}

func createUSDCTokenObserver(lggr logger.Logger,
tokensConfig map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig,
readers map[cciptypes.ChainSelector]contractreader.Extended,
) (TokenDataObserver, error) {
usdcReader, err := reader.NewUSDCMessageReader(
tokensConfig,
readers,
)
if err != nil {
return nil, err
}

return usdc.NewTokenDataObserver(
lggr,
tokensConfig,
usdcReader,
nil,
), nil
}

// NewCompositeObservers creates a compositeTokenDataObserver based on the provided observers.
// Created mostly for tests purposes, it allows the user to specify custom observers and skip the part
// in which we match the configuration to the proper TokenDataObserver.
Expand Down
2 changes: 1 addition & 1 deletion execute/tokendata/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func Test_CompositeTokenDataObserver_EmptyObservers(t *testing.T) {
obs, err := tokendata.NewConfigBasedCompositeObservers(logger.Test(t), []pluginconfig.TokenDataObserverConfig{})
obs, err := tokendata.NewConfigBasedCompositeObservers(logger.Test(t), []pluginconfig.TokenDataObserverConfig{}, nil)
require.NoError(t, err)

tests := []struct {
Expand Down
6 changes: 2 additions & 4 deletions execute/tokendata/usdc/usdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

type tokenDataObserver struct {
lggr logger.Logger
config pluginconfig.USDCCCTPObserverConfig
supportedTokens map[string]struct{}
usdcMessageReader reader.USDCMessageReader
attestationClient AttestationClient
Expand All @@ -24,19 +23,18 @@ type tokenDataObserver struct {
//nolint:revive
func NewTokenDataObserver(
lggr logger.Logger,
config pluginconfig.USDCCCTPObserverConfig,
tokens map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig,
usdcMessageReader reader.USDCMessageReader,
attestationClient AttestationClient,
) *tokenDataObserver {
supportedTokens := make(map[string]struct{})
for chainSelector, tokenConfig := range config.Tokens {
for chainSelector, tokenConfig := range tokens {
key := sourceTokenIdentifier(chainSelector, tokenConfig.SourcePoolAddress)
supportedTokens[key] = struct{}{}
}

return &tokenDataObserver{
lggr: lggr,
config: config,
supportedTokens: supportedTokens,
usdcMessageReader: usdcMessageReader,
attestationClient: attestationClient,
Expand Down
22 changes: 8 additions & 14 deletions execute/tokendata/usdc/usdc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
"github.com/stretchr/testify/require"
Expand All @@ -19,19 +18,14 @@ import (
func TestTokenDataObserver_Observe_USDCAndRegularTokens(t *testing.T) {
ethereumUSDCPool := internal.RandBytes().String()
avalancheUSDCPool := internal.RandBytes().String()
config := pluginconfig.USDCCCTPObserverConfig{
AttestationAPI: "https://attestation.api",
AttestationAPITimeout: commonconfig.MustNewDuration(1),
AttestationAPIInterval: commonconfig.MustNewDuration(1),
Tokens: map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig{
1: {
SourcePoolAddress: ethereumUSDCPool,
SourceMessageTransmitterAddr: internal.RandBytes().String(),
},
2: {
SourcePoolAddress: avalancheUSDCPool,
SourceMessageTransmitterAddr: internal.RandBytes().String(),
},
config := map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig{
1: {
SourcePoolAddress: ethereumUSDCPool,
SourceMessageTransmitterAddr: internal.RandBytes().String(),
},
2: {
SourcePoolAddress: avalancheUSDCPool,
SourceMessageTransmitterAddr: internal.RandBytes().String(),
},
}

Expand Down
14 changes: 6 additions & 8 deletions internal/reader/usdc_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ var CCTPDestDomains = map[uint64]uint32{
}

type usdcMessageReader struct {
config pluginconfig.USDCCCTPObserverConfig
contractReaders map[cciptypes.ChainSelector]contractreader.Extended
cctpDestDomain map[uint64]uint32
eventIndex int
Expand All @@ -71,18 +70,17 @@ func (m messageSentEvent) unpackID() (eventID, error) {
}

func NewUSDCMessageReader(
config pluginconfig.USDCCCTPObserverConfig,
tokensConfig map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig,
contractReaders map[cciptypes.ChainSelector]contractreader.Extended,
) (USDCMessageReader, error) {
for chainSelector, token := range config.Tokens {
for chainSelector, token := range tokensConfig {
err := bindMessageTransmitters(context.Background(), contractReaders, chainSelector, token.SourcePoolAddress)
if err != nil {
return nil, fmt.Errorf("unable to bind message transmitter for chain %d: %w", chainSelector, err)
}
}

return usdcMessageReader{
config: config,
contractReaders: contractReaders,
cctpDestDomain: CCTPDestDomains,
eventIndex: MessageSentWordIndex,
Expand Down Expand Up @@ -150,7 +148,7 @@ func (u usdcMessageReader) recreateMessageTransmitterEvents(
messageTransmitterEvents := make(map[exectypes.MessageTokenID]eventID)

for id, token := range tokens {
sourceTokenPayload, err := parseUSDCExtraData(token)
sourceTokenPayload, err := parseTokenExtraData(token)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -226,8 +224,8 @@ type sourceTokenDataPayload struct {
SourceDomain uint32
}

// extractDetailsFromUSDCMessage extracts the nonce and source domain from the USDC message.
// Please see the Solidity code for USDCTokenPool to understand more details
// parseTokenExtraData extracts the nonce and source domain from the USDC message.
// Please see the Solidity code in USDCTokenPool to understand more details
//
// struct SourceTokenDataPayload {
// uint64 nonce;
Expand All @@ -237,7 +235,7 @@ type sourceTokenDataPayload struct {
// destTokenAddress: getRemoteToken(lockOrBurnIn.remoteChainSelector),
// destPoolData: abi.encode(SourceTokenDataPayload({nonce: nonce, sourceDomain: i_localDomainIdentifier}))
// });
func parseUSDCExtraData(token cciptypes.RampTokenAmount) (*sourceTokenDataPayload, error) {
func parseTokenExtraData(token cciptypes.RampTokenAmount) (*sourceTokenDataPayload, error) {
if len(token.ExtraData) < 12 {
return nil, fmt.Errorf("extraData is too short, expected at least 12 bytes")
}
Expand Down

0 comments on commit d44065c

Please sign in to comment.