Skip to content

Commit

Permalink
Pass contractID, relayConfig instead of relayOpts
Browse files Browse the repository at this point in the history
Also: move test for invalid contract ID to evm_test.go, & add mismatched ChainID
test while we're here since it's pretty similar
  • Loading branch information
reductionista committed Nov 23, 2023
1 parent 879e286 commit 109e0d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 35 deletions.
14 changes: 2 additions & 12 deletions core/services/relay/evm/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,12 @@ type chainReader struct {
}

// NewChainReaderService constructor for ChainReader
func NewChainReaderService(lggr logger.Logger, lp logpoller.LogPoller, ropts *types.RelayOpts) (*chainReader, error) {
relayConfig, err := ropts.RelayConfig()
if err != nil {
return nil, fmt.Errorf("failed parsing RelayConfig: %w", err)
}

if !common.IsHexAddress(ropts.ContractID) {
return nil, fmt.Errorf("%w: invalid contractID, expected hex address", commontypes.ErrInvalidConfig)
}
contractID := common.HexToAddress(ropts.ContractID)

func NewChainReaderService(lggr logger.Logger, lp logpoller.LogPoller, contractID common.Address, relayConfig *types.RelayConfig) (*chainReader, error) {
if relayConfig.ChainReader == nil {
return nil, fmt.Errorf("%w: ChainReader missing from RelayConfig", errors.ErrUnsupported)
}

if err = validateChainReaderConfig(*relayConfig.ChainReader); err != nil {
if err := validateChainReaderConfig(*relayConfig.ChainReader); err != nil {
return nil, err
}

Expand Down
25 changes: 3 additions & 22 deletions core/services/relay/evm/chain_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,21 @@ func TestNewChainReader(t *testing.T) {

t.Run("happy path", func(t *testing.T) {
chain.On("LogPoller").Return(lp)
_, err2 := NewChainReaderService(lggr, chain.LogPoller(), ropts)
_, err2 := NewChainReaderService(lggr, chain.LogPoller(), contractID, &relayConfig)
assert.NoError(t, err2)
})

t.Run("invalid contractID", func(t *testing.T) {
rargs = commontypes.RelayArgs{ContractID: "invalid hex string", RelayConfig: r}
ropts = evmtypes.NewRelayOpts(rargs)
require.NotNil(t, ropts)
_, err2 := NewChainReaderService(lggr, chain.LogPoller(), ropts)
assert.ErrorIs(t, err2, commontypes.ErrInvalidConfig)
assert.ErrorContains(t, err2, "invalid contractID")
})

t.Run("invalid config", func(t *testing.T) {
chainReaderConfig = makeRelayConfig(contractABI, []string{"result", "extraResult"}) // 2 results required but abi includes only one
invalidConfig := evmtypes.RelayConfig{ChainReader: &chainReaderConfig}
r2, err2 := json.Marshal(&invalidConfig)
require.NoError(t, err2)
rargs = commontypes.RelayArgs{ContractID: contractID.String(), RelayConfig: r2}
ropts = evmtypes.NewRelayOpts(rargs)
require.NotNil(t, ropts)
_, err2 = NewChainReaderService(lggr, chain.LogPoller(), ropts)
_, err2 := NewChainReaderService(lggr, chain.LogPoller(), contractID, &invalidConfig)
assert.ErrorIs(t, err2, commontypes.ErrInvalidConfig)
assert.ErrorContains(t, err2, "return values: [result,extraResult] don't match abi method outputs: [result]")
})

t.Run("ChainReader missing from RelayConfig", func(t *testing.T) {
preChainReaderConfig := evmtypes.RelayConfig{}
r2, err2 := json.Marshal(&preChainReaderConfig)
require.NoError(t, err2)
rargs = commontypes.RelayArgs{ContractID: contractID.String(), RelayConfig: r2}
ropts = evmtypes.NewRelayOpts(rargs)
require.NotNil(t, ropts)
_, err2 = NewChainReaderService(lggr, chain.LogPoller(), ropts)
_, err2 := NewChainReaderService(lggr, chain.LogPoller(), contractID, &preChainReaderConfig)
assert.ErrorIs(t, err2, errors.ErrUnsupported)
assert.ErrorContains(t, err2, "ChainReader missing from RelayConfig")
})
Expand Down
6 changes: 5 additions & 1 deletion core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ func (r *Relayer) NewMedianProvider(rargs commontypes.RelayArgs, pargs commontyp
if expectedChainID != r.chain.ID().String() {
return nil, fmt.Errorf("internal error: chain id in spec does not match this relayer's chain: have %s expected %s", relayConfig.ChainID.String(), r.chain.ID().String())
}
if !common.IsHexAddress(relayOpts.ContractID) {
return nil, fmt.Errorf("invalid contractID %s, expected hex address", relayOpts.ContractID)
}
contractID := common.HexToAddress(relayOpts.ContractID)

configWatcher, err := newConfigProvider(lggr, r.chain, relayOpts, r.eventBroadcaster)
if err != nil {
Expand All @@ -526,7 +530,7 @@ func (r *Relayer) NewMedianProvider(rargs commontypes.RelayArgs, pargs commontyp
medianContract: medianContract,
}

chainReader, err := NewChainReaderService(lggr, r.chain.LogPoller(), relayOpts)
chainReader, err := NewChainReaderService(lggr, r.chain.LogPoller(), contractID, &relayConfig)
if err != nil {
if errors.Is(err, errors.ErrUnsupported) {
// ignore for now, until we can remove old MedianContract code from MedianProvider
Expand Down

0 comments on commit 109e0d9

Please sign in to comment.