From df2d4847910c357a5702c9faa1af40a7fd5af8e3 Mon Sep 17 00:00:00 2001 From: Makram Date: Wed, 26 Jun 2024 19:31:02 +0300 Subject: [PATCH] ccipocr3: add getocrconfigs method and test (#1102) ## Motivation We need to be able to fetch OCR configs from the home chain reader in the capability launcher. ## Solution Add a method to fetch OCR configs for a (don ID, plugin type) pair in the home chain reader. --- .../ccip/internal/reader/home_chain.go | 36 +++++++++++++++ .../ccip/internal/reader/home_chain_test.go | 44 +++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/core/services/ocr3/plugins/ccip/internal/reader/home_chain.go b/core/services/ocr3/plugins/ccip/internal/reader/home_chain.go index aeb9d071a0..9a90558762 100644 --- a/core/services/ocr3/plugins/ccip/internal/reader/home_chain.go +++ b/core/services/ocr3/plugins/ccip/internal/reader/home_chain.go @@ -23,6 +23,8 @@ type HomeChain interface { GetKnownCCIPChains() (mapset.Set[cciptypes.ChainSelector], error) // GetFChain Gets the FChain value for each chain GetFChain() (map[cciptypes.ChainSelector]int, error) + // GetOCRConfigs Gets the OCR3Configs for a given donID and pluginType + GetOCRConfigs(ctx context.Context, donID uint32, pluginType uint8) ([]OCR3ConfigWithMeta, error) services.Service } @@ -179,6 +181,19 @@ func (r *homeChainPoller) GetFChain() (map[cciptypes.ChainSelector]int, error) { return r.state.fChain, nil } +func (r *homeChainPoller) GetOCRConfigs(ctx context.Context, donID uint32, pluginType uint8) ([]OCR3ConfigWithMeta, error) { + var ocrConfigs []OCR3ConfigWithMeta + err := r.homeChainReader.GetLatestValue(ctx, "CCIPCapabilityConfiguration", "getOCRConfig", map[string]any{ + "donId": donID, + "pluginType": pluginType, + }, &ocrConfigs) + if err != nil { + return nil, fmt.Errorf("error fetching OCR configs: %w", err) + } + + return ocrConfigs, nil +} + func (r *homeChainPoller) Close() error { return r.sync.StopOnce(r.Name(), func() error { close(r.stopCh) @@ -273,4 +288,25 @@ type ChainConfig struct { Config []byte `json:"config"` } +// OCR3Config mirrors CCIPCapabilityConfiguration.sol's OCR3Config struct +type OCR3Config struct { + PluginType uint8 `json:"pluginType"` + ChainSelector cciptypes.ChainSelector `json:"chainSelector"` + F uint8 `json:"F"` + OffchainConfigVersion uint64 `json:"offchainConfigVersion"` + OfframpAddress []byte `json:"offrampAddress"` + BootstrapP2PIds [][32]byte `json:"bootstrapP2PIds"` + P2PIds [][32]byte `json:"p2pIds"` + Signers [][]byte `json:"signers"` + Transmitters [][]byte `json:"transmitters"` + OffchainConfig []byte `json:"offchainConfig"` +} + +// OCR3ConfigWithmeta mirrors CCIPCapabilityConfiguration.sol's OCR3ConfigWithMeta struct +type OCR3ConfigWithMeta struct { + Config OCR3Config `json:"config"` + ConfigCount uint64 `json:"configCount"` + ConfigDigest [32]byte `json:"configDigest"` +} + var _ HomeChain = (*homeChainPoller)(nil) diff --git a/core/services/ocr3/plugins/ccip/internal/reader/home_chain_test.go b/core/services/ocr3/plugins/ccip/internal/reader/home_chain_test.go index 2ae0a3afc1..8965273b55 100644 --- a/core/services/ocr3/plugins/ccip/internal/reader/home_chain_test.go +++ b/core/services/ocr3/plugins/ccip/internal/reader/home_chain_test.go @@ -17,6 +17,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) var ( @@ -154,3 +155,46 @@ func Test_PollingWorking(t *testing.T) { assert.NoError(t, err) assert.Equal(t, homeChainConfig, configs) } + +func Test_HomeChainPoller_GetOCRConfig(t *testing.T) { + donID := uint32(1) + pluginType := uint8(1) // execution + homeChainReader := mocks.NewContractReaderMock() + homeChainReader.On( + "GetLatestValue", + mock.Anything, + "CCIPCapabilityConfiguration", + "getOCRConfig", + map[string]any{ + "donId": donID, + "pluginType": pluginType, + }, + mock.AnythingOfType("*[]reader.OCR3ConfigWithMeta"), + ).Return(nil).Run(func(args mock.Arguments) { + arg := args.Get(4).(*[]OCR3ConfigWithMeta) + *arg = append(*arg, OCR3ConfigWithMeta{ + ConfigCount: 1, + Config: OCR3Config{ + PluginType: pluginType, + ChainSelector: 1, + F: 1, + OfframpAddress: []byte("offramp"), + }, + }) + }) + defer homeChainReader.AssertExpectations(t) + + configPoller := NewHomeChainConfigPoller( + homeChainReader, + logger.Test(t), + 10*time.Millisecond, + ) + + configs, err := configPoller.GetOCRConfigs(context.Background(), donID, pluginType) + require.NoError(t, err) + require.Len(t, configs, 1) + require.Equal(t, uint8(1), configs[0].Config.PluginType) + require.Equal(t, cciptypes.ChainSelector(1), configs[0].Config.ChainSelector) + require.Equal(t, uint8(1), configs[0].Config.F) + require.Equal(t, []byte("offramp"), configs[0].Config.OfframpAddress) +}