Skip to content

Commit

Permalink
Pass client to chain reader instead of the whole chain (#12967)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag authored Apr 25, 2024
1 parent ac7d340 commit 83d6f20
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
6 changes: 2 additions & 4 deletions core/services/relay/evm/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"

evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services"
Expand All @@ -41,11 +39,11 @@ type chainReader struct {

// NewChainReaderService is a constructor for ChainReader, returns nil if there is any error
// Note that the ChainReaderService returned does not support anonymous events.
func NewChainReaderService(ctx context.Context, lggr logger.Logger, lp logpoller.LogPoller, chain legacyevm.Chain, config types.ChainReaderConfig) (ChainReaderService, error) {
func NewChainReaderService(ctx context.Context, lggr logger.Logger, lp logpoller.LogPoller, client evmclient.Client, config types.ChainReaderConfig) (ChainReaderService, error) {
cr := &chainReader{
lggr: lggr.Named("ChainReader"),
lp: lp,
client: chain.Client(),
client: client,
contractBindings: contractBindings{},
parsed: &parsedTypes{encoderDefs: map[string]types.CodecEntry{}, decoderDefs: map[string]types.CodecEntry{}},
}
Expand Down
15 changes: 6 additions & 9 deletions core/services/relay/evm/chain_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm/mocks"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/chain_reader_example"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
Expand Down Expand Up @@ -120,7 +119,7 @@ func triggerFourTopics(t *testing.T, it *chainReaderInterfaceTester, i1, i2, i3
}

type chainReaderInterfaceTester struct {
chain *mocks.Chain
client client.Client
address string
address2 string
chainConfig types.ChainReaderConfig
Expand Down Expand Up @@ -157,12 +156,11 @@ func (it *chainReaderInterfaceTester) Setup(t *testing.T) {
})

// can re-use the same chain for tests, just make new contract for each test
if it.chain != nil {
if it.client != nil {
it.deployNewContracts(t)
return
}

it.chain = &mocks.Chain{}
it.setupChainNoClient(t)

testStruct := CreateTestStruct(0, it)
Expand Down Expand Up @@ -240,7 +238,7 @@ func (it *chainReaderInterfaceTester) Setup(t *testing.T) {
},
},
}
it.chain.On("Client").Return(client.NewSimulatedBackendClient(t, it.sim, big.NewInt(1337)))
it.client = client.NewSimulatedBackendClient(t, it.sim, big.NewInt(1337))
it.deployNewContracts(t)
}

Expand Down Expand Up @@ -270,10 +268,9 @@ func (it *chainReaderInterfaceTester) GetChainReader(t *testing.T) clcommontypes
RpcBatchSize: 1,
KeepFinalizedBlocksDepth: 10000,
}
lp := logpoller.NewLogPoller(logpoller.NewORM(testutils.SimulatedChainID, db, lggr), it.chain.Client(), lggr, lpOpts)
lp := logpoller.NewLogPoller(logpoller.NewORM(testutils.SimulatedChainID, db, lggr), it.client, lggr, lpOpts)
require.NoError(t, lp.Start(ctx))
it.chain.On("LogPoller").Return(lp)
cr, err := evm.NewChainReaderService(ctx, lggr, lp, it.chain, it.chainConfig)
cr, err := evm.NewChainReaderService(ctx, lggr, lp, it.client, it.chainConfig)
require.NoError(t, err)
require.NoError(t, cr.Start(ctx))
it.cr = cr
Expand All @@ -288,7 +285,7 @@ func (it *chainReaderInterfaceTester) TriggerEvent(t *testing.T, testStruct *Tes
it.sendTxWithTestStruct(t, testStruct, (*chain_reader_example.LatestValueHolderTransactor).TriggerEvent)
}

func (it *chainReaderInterfaceTester) GetBindings(t *testing.T) []clcommontypes.BoundContract {
func (it *chainReaderInterfaceTester) GetBindings(_ *testing.T) []clcommontypes.BoundContract {
return []clcommontypes.BoundContract{
{Name: AnyContractName, Address: it.address, Pending: true},
{Name: AnySecondContractName, Address: it.address2, Pending: true},
Expand Down
2 changes: 1 addition & 1 deletion core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ func (r *Relayer) NewMedianProvider(rargs commontypes.RelayArgs, pargs commontyp
// allow fallback until chain reader is default and median contract is removed, but still log just in case
var chainReaderService ChainReaderService
if relayConfig.ChainReader != nil {
if chainReaderService, err = NewChainReaderService(ctx, lggr, r.chain.LogPoller(), r.chain, *relayConfig.ChainReader); err != nil {
if chainReaderService, err = NewChainReaderService(ctx, lggr, r.chain.LogPoller(), r.chain.Client(), *relayConfig.ChainReader); err != nil {
return nil, err
}

Expand Down

0 comments on commit 83d6f20

Please sign in to comment.