From 81aa9b735eac3df9d4b7064d8495b616719c2260 Mon Sep 17 00:00:00 2001 From: Domino Valdano <2644901+reductionista@users.noreply.github.com> Date: Sun, 12 May 2024 18:41:09 -0700 Subject: [PATCH] Fix chainreader & config poller tests --- .../evm/client/simulated_backend_client.go | 2 +- core/chains/evm/logpoller/helper_test.go | 2 -- core/chains/evm/logpoller/log_poller_test.go | 7 +++-- core/services/relay/evm/chain_reader_test.go | 25 ++++++++-------- core/services/relay/evm/config_poller_test.go | 29 ++++++++++--------- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/core/chains/evm/client/simulated_backend_client.go b/core/chains/evm/client/simulated_backend_client.go index b736a6e649c..7ff4f0ec6ac 100644 --- a/core/chains/evm/client/simulated_backend_client.go +++ b/core/chains/evm/client/simulated_backend_client.go @@ -481,7 +481,7 @@ func (c *SimulatedBackendClient) EstimateGas(ctx context.Context, call ethereum. // SuggestGasPrice recommends a gas price. func (c *SimulatedBackendClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) { - panic("unimplemented") + return c.client.SuggestGasPrice(ctx) } // BatchCallContext makes a batch rpc call. diff --git a/core/chains/evm/logpoller/helper_test.go b/core/chains/evm/logpoller/helper_test.go index e5fc1404836..6da7d20dcdb 100644 --- a/core/chains/evm/logpoller/helper_test.go +++ b/core/chains/evm/logpoller/helper_test.go @@ -49,8 +49,6 @@ func SetupTH(t testing.TB, opts logpoller.Opts) TestHarness { chainID := testutils.NewRandomEVMChainID() chainID2 := testutils.NewRandomEVMChainID() db := pgtest.NewSqlxDB(t) - //dataDir, err := os.MkdirTemp("", "simgethdata") - //require.NoError(t, err) o := logpoller.NewORM(chainID, db, lggr) o2 := logpoller.NewORM(chainID2, db, lggr) diff --git a/core/chains/evm/logpoller/log_poller_test.go b/core/chains/evm/logpoller/log_poller_test.go index 91bcc4110d3..395ecfc646b 100644 --- a/core/chains/evm/logpoller/log_poller_test.go +++ b/core/chains/evm/logpoller/log_poller_test.go @@ -344,7 +344,7 @@ func Test_BackupLogPoller(t *testing.T) { require.ErrorIs(t, err, ethereum.NotFound) currentBlockNumber := th.PollAndSaveLogs(ctx, 1) - assert.Equal(t, int64(35), currentBlockNumber) + assert.Equal(t, int64(1), currentBlockNumber) // flush out cached block 34 by reading logs from first 32 blocks //query := ethereum.FilterQuery{ @@ -380,7 +380,7 @@ func Test_BackupLogPoller(t *testing.T) { require.NoError(t, err) assert.Equal(t, 0, len(logs)) th.Backend.Commit() - //markBlockAsFinalized(t, th, 35) + finalizeThroughBlock(t, th, 35) // Run ordinary poller + backup poller at least once more th.LogPoller.PollAndSaveLogs(ctx, currentBlockNumber+1) @@ -1760,7 +1760,7 @@ func Test_PollAndSavePersistsFinalityInBlocks(t *testing.T) { } if tt.useFinalityTag { - finalizeThroughBlock(t, th, int64(tt.expectedFinalizedBlock)) + finalizeThroughBlock(t, th, tt.expectedFinalizedBlock) } th.PollAndSaveLogs(ctx, 1) @@ -1944,6 +1944,7 @@ func finalizeThroughBlock(t *testing.T, th TestHarness, blockNumber int64) { require.Len(t, currentBlock, 32) } h, err = th.Client.HeaderByNumber(ctx, nil) + require.NoError(t, err) require.GreaterOrEqual(t, h.Number.Int64(), targetBlockNumber) } diff --git a/core/services/relay/evm/chain_reader_test.go b/core/services/relay/evm/chain_reader_test.go index c3cb36b93e3..c81cafebf3c 100644 --- a/core/services/relay/evm/chain_reader_test.go +++ b/core/services/relay/evm/chain_reader_test.go @@ -13,12 +13,11 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/core" evmtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/smartcontractkit/libocr/commontypes" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -63,7 +62,7 @@ func TestChainReaderGetLatestValue(t *testing.T) { anyString := "foo" tx, err := it.evmTest.TriggerEventWithDynamicTopic(it.auth, anyString) require.NoError(t, err) - it.sim.Commit() + it.backend.Commit() it.incNonce() it.awaitTx(t, tx) @@ -113,7 +112,7 @@ func triggerFourTopics(t *testing.T, it *chainReaderInterfaceTester, i1, i2, i3 tx, err := it.evmTest.ChainReaderTesterTransactor.TriggerWithFourTopics(it.auth, i1, i2, i3) require.NoError(t, err) require.NoError(t, err) - it.sim.Commit() + it.backend.Commit() it.incNonce() it.awaitTx(t, tx) } @@ -124,7 +123,7 @@ type chainReaderInterfaceTester struct { address2 string chainConfig types.ChainReaderConfig auth *bind.TransactOpts - sim *backends.SimulatedBackend + backend *simulated.Backend pk *ecdsa.PrivateKey evmTest *chain_reader_tester.ChainReaderTester cr evm.ChainReaderService @@ -238,7 +237,7 @@ func (it *chainReaderInterfaceTester) Setup(t *testing.T) { }, }, } - it.client = client.NewSimulatedBackendClient(t, it.sim, big.NewInt(1337)) + it.client = client.NewSimulatedBackendClient(t, it.backend, big.NewInt(1337)) it.deployNewContracts(t) } @@ -308,7 +307,7 @@ func (it *chainReaderInterfaceTester) sendTxWithTestStruct(t *testing.T, testStr midToInternalType(testStruct.NestedStruct), ) require.NoError(t, err) - it.sim.Commit() + it.backend.Commit() it.incNonce() it.awaitTx(t, tx) } @@ -337,8 +336,8 @@ func (it *chainReaderInterfaceTester) setupChainNoClient(t require.TestingT) { it.auth, err = bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337)) require.NoError(t, err) - it.sim = backends.NewSimulatedBackend(core.GenesisAlloc{it.auth.From: {Balance: big.NewInt(math.MaxInt64)}}, commonGasLimitOnEvms*5000) - it.sim.Commit() + it.backend = simulated.NewBackend(evmtypes.GenesisAlloc{it.auth.From: {Balance: big.NewInt(math.MaxInt64)}}, simulated.WithBlockGasLimit(commonGasLimitOnEvms*5000)) + it.backend.Commit() } func (it *chainReaderInterfaceTester) deployNewContracts(t *testing.T) { @@ -348,7 +347,7 @@ func (it *chainReaderInterfaceTester) deployNewContracts(t *testing.T) { func (it *chainReaderInterfaceTester) deployNewContract(t *testing.T) string { ctx := testutils.Context(t) - gasPrice, err := it.sim.SuggestGasPrice(ctx) + gasPrice, err := it.client.SuggestGasPrice(ctx) require.NoError(t, err) it.auth.GasPrice = gasPrice @@ -356,10 +355,10 @@ func (it *chainReaderInterfaceTester) deployNewContract(t *testing.T) string { // Not sure if there's a better way to get it. it.auth.GasLimit = 10552800 - address, tx, ts, err := chain_reader_tester.DeployChainReaderTester(it.auth, it.sim) + address, tx, ts, err := chain_reader_tester.DeployChainReaderTester(it.auth, it.client) require.NoError(t, err) - it.sim.Commit() + it.backend.Commit() if it.evmTest == nil { it.evmTest = ts } @@ -370,7 +369,7 @@ func (it *chainReaderInterfaceTester) deployNewContract(t *testing.T) string { func (it *chainReaderInterfaceTester) awaitTx(t *testing.T, tx *evmtypes.Transaction) { ctx := testutils.Context(t) - receipt, err := it.sim.TransactionReceipt(ctx, tx.Hash()) + receipt, err := it.client.TransactionReceipt(ctx, tx.Hash()) require.NoError(t, err) require.Equal(t, evmtypes.ReceiptStatusSuccessful, receipt.Status) } diff --git a/core/services/relay/evm/config_poller_test.go b/core/services/relay/evm/config_poller_test.go index 8c02c4e2e7e..7931bfaa5d9 100644 --- a/core/services/relay/evm/config_poller_test.go +++ b/core/services/relay/evm/config_poller_test.go @@ -8,11 +8,11 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth/ethconfig" + "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/onsi/gomega" "github.com/pkg/errors" "github.com/stretchr/testify/assert" @@ -52,7 +52,8 @@ func TestConfigPoller(t *testing.T) { var configStoreContractAddr common.Address var configStoreContract *ocrconfigurationstoreevmsimple.OCRConfigurationStoreEVMSimple var user *bind.TransactOpts - var b *backends.SimulatedBackend + var b *simulated.Backend + var ec simulated.Client var linkTokenAddress common.Address var accessAddress common.Address ctx := testutils.Context(t) @@ -64,16 +65,18 @@ func TestConfigPoller(t *testing.T) { require.NoError(t, err) user, err = bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337)) require.NoError(t, err) - b = backends.NewSimulatedBackend(core.GenesisAlloc{ + b = simulated.NewBackend(types.GenesisAlloc{ user.From: {Balance: big.NewInt(1000000000000000000)}}, - 5*ethconfig.Defaults.Miner.GasCeil) - linkTokenAddress, _, _, err = link_token_interface.DeployLinkToken(user, b) + simulated.WithBlockGasLimit(5*ethconfig.Defaults.Miner.GasCeil)) + require.NotNil(t, b) + ec = b.Client() + linkTokenAddress, _, _, err = link_token_interface.DeployLinkToken(user, ec) require.NoError(t, err) - accessAddress, _, _, err = testoffchainaggregator2.DeploySimpleWriteAccessController(user, b) + accessAddress, _, _, err = testoffchainaggregator2.DeploySimpleWriteAccessController(user, ec) require.NoError(t, err, "failed to deploy test access controller contract") ocrAddress, _, ocrContract, err = ocr2aggregator.DeployOCR2Aggregator( user, - b, + ec, linkTokenAddress, big.NewInt(0), big.NewInt(10), @@ -83,7 +86,7 @@ func TestConfigPoller(t *testing.T) { "TEST", ) require.NoError(t, err) - configStoreContractAddr, _, configStoreContract, err = ocrconfigurationstoreevmsimple.DeployOCRConfigurationStoreEVMSimple(user, b) + configStoreContractAddr, _, configStoreContract, err = ocrconfigurationstoreevmsimple.DeployOCRConfigurationStoreEVMSimple(user, ec) require.NoError(t, err) b.Commit() @@ -133,7 +136,7 @@ func TestConfigPoller(t *testing.T) { DeltaC: 10, }, ocrContract, user) b.Commit() - latest, err := b.BlockByNumber(testutils.Context(t), nil) + latest, err := ec.BlockByNumber(testutils.Context(t), nil) require.NoError(t, err) // Ensure we capture this config set log. require.NoError(t, lp.Replay(testutils.Context(t), latest.Number().Int64()-1)) @@ -164,7 +167,7 @@ func TestConfigPoller(t *testing.T) { var err error ocrAddress, _, ocrContract, err = ocr2aggregator.DeployOCR2Aggregator( user, - b, + ec, linkTokenAddress, big.NewInt(0), big.NewInt(10), @@ -207,7 +210,7 @@ func TestConfigPoller(t *testing.T) { changedInBlock, configDigest, err := cp.LatestConfigDetails(testutils.Context(t)) require.NoError(t, err) - latest, err := b.BlockByNumber(testutils.Context(t), nil) + latest, err := ec.BlockByNumber(testutils.Context(t), nil) require.NoError(t, err) onchainDetails, err := ocrContract.LatestConfigDetails(nil) @@ -239,7 +242,7 @@ func TestConfigPoller(t *testing.T) { // deploy it again to reset to empty config ocrAddress, _, ocrContract, err = ocr2aggregator.DeployOCR2Aggregator( user, - b, + ec, linkTokenAddress, big.NewInt(0), big.NewInt(10),