Skip to content

Commit

Permalink
ccipocr3: add getocrconfigs method and test (#1102)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
makramkd authored Jun 26, 2024
1 parent 2869d26 commit df2d484
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
36 changes: 36 additions & 0 deletions core/services/ocr3/plugins/ccip/internal/reader/home_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
44 changes: 44 additions & 0 deletions core/services/ocr3/plugins/ccip/internal/reader/home_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -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)
}

0 comments on commit df2d484

Please sign in to comment.