Skip to content

Commit 908acb2

Browse files
refactor plugin to use init factory registration pattern (#17706)
* refactor plugin to use init factory registration pattern * register chain families * fix make * fix make * fix * combine plugin config * rm redundant return * revert * change * refactor * register extra data codec * add comment back * rm extradatacodec map * fix make errors * gofmt * fix * Jade comments * fix typo * step 1 Will comments * fix make * rm unused * fix bug * rename * rename
1 parent e81644e commit 908acb2

25 files changed

+212
-183
lines changed

core/capabilities/ccip/ccipevm/executecodec_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
1212
"github.com/ethereum/go-ethereum/common"
1313
"github.com/ethereum/go-ethereum/core"
14+
chainsel "github.com/smartcontractkit/chain-selectors"
1415
"github.com/smartcontractkit/chainlink-ccip/chains/evm/gobindings/generated/v1_6_0/message_hasher"
1516
"github.com/smartcontractkit/chainlink-ccip/chains/evm/gobindings/generated/v1_6_0/offramp"
1617
"github.com/smartcontractkit/chainlink-ccip/chains/evm/gobindings/generated/v1_6_0/report_codec"
@@ -179,10 +180,15 @@ func TestExecutePluginCodecV1(t *testing.T) {
179180
simulatedBackend.Commit()
180181
contract, err := report_codec.NewReportCodec(address, simulatedBackend)
181182
require.NoError(t, err)
183+
registeredMockExtraDataCodecMap := map[string]ccipcommon.SourceChainExtraDataCodec{
184+
chainsel.FamilyEVM: mockExtraDataCodec,
185+
chainsel.FamilySolana: mockExtraDataCodec,
186+
}
182187

183188
for _, tc := range testCases {
184189
t.Run(tc.name, func(t *testing.T) {
185-
codec := NewExecutePluginCodecV1(ccipcommon.NewExtraDataCodec(mockExtraDataCodec, mockExtraDataCodec))
190+
edc := ccipcommon.ExtraDataCodec(registeredMockExtraDataCodecMap)
191+
codec := NewExecutePluginCodecV1(edc)
186192
report := tc.report(randomExecuteReport(t, d, tc.chainSelector, tc.gasLimit, tc.destGasAmount))
187193
bytes, err := codec.Encode(ctx, report)
188194
if tc.expErr {

core/capabilities/ccip/ccipevm/extradatacodec.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ const (
1414
evmDestExecDataKey = "destGasAmount"
1515
)
1616

17-
// ExtraDataCodec is a concrete implementation of SourceChainExtraDataCodec
18-
type ExtraDataCodec struct{}
17+
// ExtraDataDecoder is a concrete implementation of SourceChainExtraDataCodec
18+
type ExtraDataDecoder struct{}
1919

2020
// DecodeDestExecDataToMap reformats bytes into a chain agnostic map[string]interface{} representation for dest exec data
21-
func (d ExtraDataCodec) DecodeDestExecDataToMap(destExecData cciptypes.Bytes) (map[string]interface{}, error) {
21+
func (d ExtraDataDecoder) DecodeDestExecDataToMap(destExecData cciptypes.Bytes) (map[string]interface{}, error) {
2222
destGasAmount, err := abiDecodeUint32(destExecData)
2323
if err != nil {
2424
return nil, fmt.Errorf("decode dest gas amount: %w", err)
@@ -30,7 +30,7 @@ func (d ExtraDataCodec) DecodeDestExecDataToMap(destExecData cciptypes.Bytes) (m
3030
}
3131

3232
// DecodeExtraArgsToMap reformats bytes into a chain agnostic map[string]any representation for extra args
33-
func (d ExtraDataCodec) DecodeExtraArgsToMap(extraArgs cciptypes.Bytes) (map[string]any, error) {
33+
func (d ExtraDataDecoder) DecodeExtraArgsToMap(extraArgs cciptypes.Bytes) (map[string]any, error) {
3434
if len(extraArgs) < 4 {
3535
return nil, fmt.Errorf("extra args too short: %d, should be at least 4 (i.e the extraArgs tag)", len(extraArgs))
3636
}
@@ -85,5 +85,5 @@ func (d ExtraDataCodec) DecodeExtraArgsToMap(extraArgs cciptypes.Bytes) (map[str
8585
return output, nil
8686
}
8787

88-
// Ensure ExtraDataCodec implements the SourceChainExtraDataCodec interface
89-
var _ ccipcommon.SourceChainExtraDataCodec = &ExtraDataCodec{}
88+
// Ensure ExtraDataDecoder implements the SourceChainExtraDataCodec interface
89+
var _ ccipcommon.SourceChainExtraDataCodec = &ExtraDataDecoder{}

core/capabilities/ccip/ccipevm/extradatacodec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func Test_decodeExtraData(t *testing.T) {
1616
d := testSetup(t)
1717
gasLimit := big.NewInt(rand.Int63())
18-
extraDataDecoder := &ExtraDataCodec{}
18+
extraDataDecoder := &ExtraDataDecoder{}
1919

2020
t.Run("decode extra args into map evm v1", func(t *testing.T) {
2121
encoded, err := d.contract.EncodeEVMExtraArgsV1(nil, message_hasher.ClientEVMExtraArgsV1{
@@ -68,7 +68,7 @@ func Test_decodeExtraData(t *testing.T) {
6868

6969
func TestExtraDataDecoder_DecodeExtraArgsToMap_SVMDestination(t *testing.T) {
7070
d := testSetup(t)
71-
extraDataDecoder := &ExtraDataCodec{}
71+
extraDataDecoder := &ExtraDataDecoder{}
7272

7373
t.Run("decode extra args into map svm", func(t *testing.T) {
7474
key, err := solana.NewRandomPrivateKey()

core/capabilities/ccip/ccipevm/gas_helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func Test_calculateMessageMaxGas(t *testing.T) {
8080
}
8181
// Set the source chain selector to be EVM for now
8282
msg.Header.SourceChainSelector = ccipocr3.ChainSelector(chainsel.ETHEREUM_TESTNET_SEPOLIA.Selector)
83-
ep := EstimateProvider{extraDataCodec: extraDataCodec}
83+
ep := EstimateProvider{extraDataCodec}
8484
got := ep.CalculateMessageMaxGas(msg)
8585
t.Log(got)
8686
assert.Equalf(t, tt.want, got, "calculateMessageMaxGas(%v, %v)", tt.args.dataLen, tt.args.numTokens)

core/capabilities/ccip/ccipevm/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func Test_decodeExtraArgs(t *testing.T) {
1414
d := testSetup(t)
1515
gasLimit := big.NewInt(rand.Int63())
16-
extraDataDecoder := &ExtraDataCodec{}
16+
extraDataDecoder := &ExtraDataDecoder{}
1717

1818
t.Run("decode extra args into map evm v1", func(t *testing.T) {
1919
encoded, err := d.contract.EncodeEVMExtraArgsV1(nil, message_hasher.ClientEVMExtraArgsV1{

core/capabilities/ccip/ccipevm/msghasher_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/ethereum/go-ethereum/core/types"
2222
agbinary "github.com/gagliardetto/binary"
2323
solanago "github.com/gagliardetto/solana-go"
24+
chainsel "github.com/smartcontractkit/chain-selectors"
2425
"github.com/smartcontractkit/chainlink-ccip/chains/evm/gobindings/generated/v1_6_0/message_hasher"
2526
"github.com/smartcontractkit/chainlink-ccip/chains/solana/gobindings/fee_quoter"
2627
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
@@ -34,7 +35,10 @@ import (
3435
"github.com/stretchr/testify/require"
3536
)
3637

37-
var extraDataCodec = ccipcommon.NewExtraDataCodec(ExtraDataCodec{}, ccipsolana.ExtraDataCodec{})
38+
var extraDataCodec = ccipcommon.ExtraDataCodec(map[string]ccipcommon.SourceChainExtraDataCodec{
39+
chainsel.FamilyEVM: ExtraDataDecoder{},
40+
chainsel.FamilySolana: ccipsolana.ExtraDataDecoder{},
41+
})
3842

3943
// NOTE: these test cases are only EVM <-> EVM.
4044
// Update these cases once we have non-EVM examples.

core/capabilities/ccip/ccipevm/pluginconfig.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,13 @@ func InitializePluginConfig(lggr logger.Logger, extraDataCodec ccipcommon.ExtraD
1919
GasEstimateProvider: NewGasEstimateProvider(extraDataCodec),
2020
RMNCrypto: NewEVMRMNCrypto(lggr.Named(chainsel.FamilyEVM).Named("RMNCrypto")),
2121
ContractTransmitterFactory: ocrimpls.NewEVMContractTransmitterFactory(extraDataCodec),
22+
ChainRW: ChainCWProvider{},
23+
ExtraDataCodec: ExtraDataDecoder{},
24+
AddressCodec: AddressCodec{},
2225
}
2326
}
27+
28+
func init() {
29+
// Register the EVM plugin config factory
30+
ccipcommon.RegisterPluginConfig(chainsel.FamilyEVM, InitializePluginConfig)
31+
}

core/capabilities/ccip/ccipsolana/executecodec_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
agbinary "github.com/gagliardetto/binary"
1111
solanago "github.com/gagliardetto/solana-go"
12+
chainsel "github.com/smartcontractkit/chain-selectors"
1213
"github.com/stretchr/testify/mock"
1314

1415
ccipcommon "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/common"
@@ -179,7 +180,13 @@ func TestExecutePluginCodecV1(t *testing.T) {
179180
"accountIsWritableBitmap": uint64(2),
180181
"TokenReceiver": [32]byte(solanago.MustPublicKeyFromBase58("42Gia5bGsh8R2S44e37t9fsucap1qsgjr6GjBmWotgdF").Bytes()),
181182
}, nil).Maybe()
182-
cd := NewExecutePluginCodecV1(ccipcommon.NewExtraDataCodec(mockExtraDataCodec, mockExtraDataCodec))
183+
registeredMockExtraDataCodecMap := map[string]ccipcommon.SourceChainExtraDataCodec{
184+
chainsel.FamilyEVM: mockExtraDataCodec,
185+
chainsel.FamilySolana: mockExtraDataCodec,
186+
}
187+
188+
edc := ccipcommon.ExtraDataCodec(registeredMockExtraDataCodecMap)
189+
cd := NewExecutePluginCodecV1(edc)
183190

184191
for _, tc := range testCases {
185192
t.Run(tc.name, func(t *testing.T) {
@@ -219,6 +226,11 @@ func Test_DecodingExecuteReport(t *testing.T) {
219226
"ComputeUnits": uint32(1000),
220227
"accountIsWritableBitmap": uint64(2),
221228
}, nil)
229+
registeredMockExtraDataCodecMap := map[string]ccipcommon.SourceChainExtraDataCodec{
230+
chainsel.FamilyEVM: mockExtraDataCodec,
231+
chainsel.FamilySolana: mockExtraDataCodec,
232+
}
233+
222234
t.Run("decode on-chain execute report", func(t *testing.T) {
223235
chainSel := cciptypes.ChainSelector(rand.Uint64())
224236

@@ -257,7 +269,8 @@ func Test_DecodingExecuteReport(t *testing.T) {
257269
err = onChainReport.MarshalWithEncoder(encoder)
258270
require.NoError(t, err)
259271

260-
executeCodec := NewExecutePluginCodecV1(ccipcommon.NewExtraDataCodec(mockExtraDataCodec, mockExtraDataCodec))
272+
edc := ccipcommon.ExtraDataCodec(registeredMockExtraDataCodecMap)
273+
executeCodec := NewExecutePluginCodecV1(edc)
261274
decode, err := executeCodec.Decode(testutils.Context(t), buf.Bytes())
262275
require.NoError(t, err)
263276

@@ -273,7 +286,8 @@ func Test_DecodingExecuteReport(t *testing.T) {
273286

274287
t.Run("decode Borsh encoded execute report", func(t *testing.T) {
275288
ocrReport := randomExecuteReport(t, 124615329519749607)
276-
cd := NewExecutePluginCodecV1(ccipcommon.NewExtraDataCodec(mockExtraDataCodec, mockExtraDataCodec))
289+
edc := ccipcommon.ExtraDataCodec(registeredMockExtraDataCodecMap)
290+
cd := NewExecutePluginCodecV1(edc)
277291
encodedReport, err := cd.Encode(testutils.Context(t), ocrReport)
278292
require.NoError(t, err)
279293

core/capabilities/ccip/ccipsolana/extradatacodec.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ var (
2929
evmExtraArgsV2Tag = hexutil.MustDecode("0x181dcf10")
3030
)
3131

32-
// ExtraDataCodec is a helper struct for decoding extra data
33-
type ExtraDataCodec struct{}
32+
// ExtraDataDecoder is a helper struct for decoding extra data
33+
type ExtraDataDecoder struct{}
3434

3535
// DecodeExtraArgsToMap is a helper function for converting Borsh encoded extra args bytes into map[string]any
36-
func (d ExtraDataCodec) DecodeExtraArgsToMap(extraArgs cciptypes.Bytes) (map[string]any, error) {
36+
func (d ExtraDataDecoder) DecodeExtraArgsToMap(extraArgs cciptypes.Bytes) (map[string]any, error) {
3737
if len(extraArgs) < 4 {
3838
return nil, fmt.Errorf("extra args too short: %d, should be at least 4 (i.e the extraArgs tag)", len(extraArgs))
3939
}
@@ -74,11 +74,11 @@ func (d ExtraDataCodec) DecodeExtraArgsToMap(extraArgs cciptypes.Bytes) (map[str
7474
}
7575

7676
// DecodeDestExecDataToMap is a helper function for converting dest exec data bytes into map[string]any
77-
func (d ExtraDataCodec) DecodeDestExecDataToMap(destExecData cciptypes.Bytes) (map[string]any, error) {
77+
func (d ExtraDataDecoder) DecodeDestExecDataToMap(destExecData cciptypes.Bytes) (map[string]any, error) {
7878
return map[string]any{
7979
svmDestExecDataKey: binary.BigEndian.Uint32(destExecData),
8080
}, nil
8181
}
8282

83-
// Ensure ExtraDataCodec implements the SourceChainExtraDataCodec interface
84-
var _ ccipcommon.SourceChainExtraDataCodec = &ExtraDataCodec{}
83+
// Ensure ExtraDataDecoder implements the SourceChainExtraDataCodec interface
84+
var _ ccipcommon.SourceChainExtraDataCodec = &ExtraDataDecoder{}

core/capabilities/ccip/ccipsolana/extradatacodec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func Test_decodeExtraArgs(t *testing.T) {
18-
extraDataDecoder := &ExtraDataCodec{}
18+
extraDataDecoder := &ExtraDataDecoder{}
1919
t.Run("decode dest exec data into map svm", func(t *testing.T) {
2020
destGasAmount := uint32(10000)
2121
encoded := make([]byte, 4)

core/capabilities/ccip/ccipsolana/gas_helpers_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ func Test_calculateMessageMaxGas(t *testing.T) {
5656
}
5757
// Set the source chain selector to be EVM for now
5858
msg.Header.SourceChainSelector = ccipocr3.ChainSelector(chainsel.SOLANA_TESTNET.Selector)
59-
ep := EstimateProvider{extraDataCodec: ccipcommon.NewExtraDataCodec(ccipevm.ExtraDataCodec{}, ExtraDataCodec{})}
59+
edc := ccipcommon.ExtraDataCodec(map[string]ccipcommon.SourceChainExtraDataCodec{
60+
chainsel.FamilyEVM: ccipevm.ExtraDataDecoder{},
61+
chainsel.FamilySolana: ExtraDataDecoder{},
62+
})
63+
ep := EstimateProvider{extraDataCodec: edc}
6064
got := ep.CalculateMessageMaxGas(msg)
6165
t.Log(got)
6266
assert.Equalf(t, tt.want, got, "calculateMessageMaxGas(%v, %v)", tt.args.dataLen, tt.args.numTokens)
@@ -96,7 +100,11 @@ func TestCalculateMaxGas(t *testing.T) {
96100
}
97101

98102
msg.Header.SourceChainSelector = ccipocr3.ChainSelector(chainsel.SOLANA_TESTNET.Selector)
99-
ep := EstimateProvider{extraDataCodec: ccipcommon.NewExtraDataCodec(ccipevm.ExtraDataCodec{}, ExtraDataCodec{})}
103+
edc := ccipcommon.ExtraDataCodec(map[string]ccipcommon.SourceChainExtraDataCodec{
104+
chainsel.FamilyEVM: ccipevm.ExtraDataDecoder{},
105+
chainsel.FamilySolana: ExtraDataDecoder{},
106+
})
107+
ep := EstimateProvider{extraDataCodec: edc}
100108
gotTree := ep.CalculateMerkleTreeGas(tt.numRequests)
101109
gotMsg := ep.CalculateMessageMaxGas(msg)
102110
t.Log("want", tt.want, "got", gotTree+gotMsg)

core/capabilities/ccip/ccipsolana/msghasher_test.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,28 @@ import (
77
"testing"
88

99
"github.com/gagliardetto/solana-go"
10+
chainsel "github.com/smartcontractkit/chain-selectors"
1011
"github.com/stretchr/testify/mock"
1112
"github.com/stretchr/testify/require"
1213

13-
"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/ccipevm"
14-
15-
ccipcommon "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/common"
16-
17-
"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/common/mocks"
18-
1914
"github.com/smartcontractkit/chainlink-ccip/chains/solana/contracts/tests/config"
2015
"github.com/smartcontractkit/chainlink-ccip/chains/solana/gobindings/ccip_offramp"
2116
"github.com/smartcontractkit/chainlink-ccip/chains/solana/utils/ccip"
22-
17+
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
2318
"github.com/smartcontractkit/chainlink-common/pkg/logger"
24-
2519
"github.com/smartcontractkit/chainlink-evm/pkg/utils"
26-
20+
"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/ccipevm"
21+
ccipcommon "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/common"
22+
"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/common/mocks"
2723
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
28-
29-
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
3024
)
3125

3226
func TestMessageHasher_EVM2SVM(t *testing.T) {
33-
var extraDataCodec = ccipcommon.NewExtraDataCodec(ccipevm.ExtraDataCodec{}, ExtraDataCodec{})
27+
registeredExtraDataCodecMap := map[string]ccipcommon.SourceChainExtraDataCodec{
28+
chainsel.FamilyEVM: ccipevm.ExtraDataDecoder{},
29+
chainsel.FamilySolana: ExtraDataDecoder{},
30+
}
31+
var extraDataCodec = ccipcommon.ExtraDataCodec(registeredExtraDataCodecMap)
3432
any2AnyMsg, any2SolanaMsg, msgAccounts := createEVM2SolanaMessages(t)
3533
msgHasher := NewMessageHasherV1(logger.Test(t), extraDataCodec)
3634
actualHash, err := msgHasher.Hash(testutils.Context(t), any2AnyMsg)
@@ -58,7 +56,14 @@ func TestMessageHasher_InvalidReceiver(t *testing.T) {
5856
[32]byte(solana.SystemProgramID.Bytes()),
5957
},
6058
}, nil).Maybe()
61-
msgHasher := NewMessageHasherV1(logger.Test(t), ccipcommon.NewExtraDataCodec(mockExtraDataCodec, mockExtraDataCodec))
59+
60+
registeredMockExtraDataCodecMap := map[string]ccipcommon.SourceChainExtraDataCodec{
61+
chainsel.FamilyEVM: mockExtraDataCodec,
62+
chainsel.FamilySolana: mockExtraDataCodec,
63+
}
64+
65+
edc := ccipcommon.ExtraDataCodec(registeredMockExtraDataCodecMap)
66+
msgHasher := NewMessageHasherV1(logger.Test(t), edc)
6267
_, err := msgHasher.Hash(testutils.Context(t), any2AnyMsg)
6368
require.Error(t, err)
6469
}
@@ -81,7 +86,13 @@ func TestMessageHasher_InvalidDestinationTokenAddress(t *testing.T) {
8186
[32]byte(solana.SystemProgramID.Bytes()),
8287
},
8388
}, nil).Maybe()
84-
msgHasher := NewMessageHasherV1(logger.Test(t), ccipcommon.NewExtraDataCodec(mockExtraDataCodec, mockExtraDataCodec))
89+
90+
registeredMockExtraDataCodecMap := map[string]ccipcommon.SourceChainExtraDataCodec{
91+
chainsel.FamilyEVM: mockExtraDataCodec,
92+
chainsel.FamilySolana: mockExtraDataCodec,
93+
}
94+
edc := ccipcommon.ExtraDataCodec(registeredMockExtraDataCodecMap)
95+
msgHasher := NewMessageHasherV1(logger.Test(t), edc)
8596
_, err := msgHasher.Hash(testutils.Context(t), any2AnyMsg)
8697
require.Error(t, err)
8798
}

core/capabilities/ccip/ccipsolana/pluginconfig.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,13 @@ func InitializePluginConfig(lggr logger.Logger, extraDataCodec ccipcommon.ExtraD
1919
GasEstimateProvider: NewGasEstimateProvider(extraDataCodec),
2020
RMNCrypto: nil,
2121
ContractTransmitterFactory: ocrimpls.NewSVMContractTransmitterFactory(extraDataCodec),
22+
AddressCodec: AddressCodec{},
23+
ChainRW: ChainRWProvider{},
24+
ExtraDataCodec: ExtraDataDecoder{},
2225
}
2326
}
27+
28+
func init() {
29+
// Register the Solana plugin config factory
30+
ccipcommon.RegisterPluginConfig(chainsel.FamilySolana, InitializePluginConfig)
31+
}

core/capabilities/ccip/common/addresscodec.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import (
99

1010
// AddressCodec is a struct that holds the chain specific address codecs
1111
type AddressCodec struct {
12-
EVMAddressCodec ChainSpecificAddressCodec
13-
SolanaAddressCodec ChainSpecificAddressCodec
12+
registeredAddressCodecMap map[string]ChainSpecificAddressCodec
1413
}
1514

1615
// NewAddressCodec is a constructor for NewAddressCodec
17-
func NewAddressCodec(evmAddrCodec, solanaAddrCodec ChainSpecificAddressCodec) AddressCodec {
16+
func NewAddressCodec(registeredMap map[string]ChainSpecificAddressCodec) AddressCodec {
1817
return AddressCodec{
19-
EVMAddressCodec: evmAddrCodec,
20-
SolanaAddressCodec: solanaAddrCodec,
18+
registeredAddressCodecMap: registeredMap,
2119
}
2220
}
2321

@@ -28,16 +26,12 @@ func (ac AddressCodec) AddressBytesToString(addr cciptypes.UnknownAddress, chain
2826
return "", fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err)
2927
}
3028

31-
switch family {
32-
case chainsel.FamilyEVM:
33-
return ac.EVMAddressCodec.AddressBytesToString(addr)
34-
35-
case chainsel.FamilySolana:
36-
return ac.SolanaAddressCodec.AddressBytesToString(addr)
37-
38-
default:
39-
return "", fmt.Errorf("unsupported family for address encode type %s", family)
29+
codec, exist := ac.registeredAddressCodecMap[family]
30+
if !exist {
31+
return "", fmt.Errorf("unsupported family for address decode type %s", family)
4032
}
33+
34+
return codec.AddressBytesToString(addr)
4135
}
4236

4337
// AddressStringToBytes converts an address from string to bytes
@@ -46,15 +40,10 @@ func (ac AddressCodec) AddressStringToBytes(addr string, chainSelector cciptypes
4640
if err != nil {
4741
return nil, fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err)
4842
}
49-
50-
switch family {
51-
case chainsel.FamilyEVM:
52-
return ac.EVMAddressCodec.AddressStringToBytes(addr)
53-
54-
case chainsel.FamilySolana:
55-
return ac.SolanaAddressCodec.AddressStringToBytes(addr)
56-
57-
default:
43+
codec, exist := ac.registeredAddressCodecMap[family]
44+
if !exist {
5845
return nil, fmt.Errorf("unsupported family for address decode type %s", family)
5946
}
47+
48+
return codec.AddressStringToBytes(addr)
6049
}

0 commit comments

Comments
 (0)