From 3e3206f3ec3b06940d899452dfeb94e93fcb6cab Mon Sep 17 00:00:00 2001 From: Dimitris Date: Fri, 4 Nov 2022 18:10:07 +0200 Subject: [PATCH] Replace header type calls with head --- .../evm/client/simulated_backend_client.go | 12 +++++++- core/chains/evm/txmgr/transmitchecker.go | 10 +++---- core/chains/evm/txmgr/transmitchecker_test.go | 14 ++++----- core/services/keeper/integration_test.go | 30 +++++++++++-------- .../keeper/registry1_1_synchronizer_test.go | 5 ++-- .../keeper/registry1_2_synchronizer_test.go | 5 ++-- .../keeper/registry1_3_synchronizer_test.go | 5 ++-- core/services/keeper/registry_interface.go | 23 +++++++------- core/services/keeper/upkeep_executer.go | 4 +-- core/services/keeper/upkeep_executer_test.go | 4 +-- 10 files changed, 64 insertions(+), 48 deletions(-) diff --git a/core/chains/evm/client/simulated_backend_client.go b/core/chains/evm/client/simulated_backend_client.go index eb874013cd5..73fa4c9c061 100644 --- a/core/chains/evm/client/simulated_backend_client.go +++ b/core/chains/evm/client/simulated_backend_client.go @@ -515,8 +515,18 @@ func (c *SimulatedBackendClient) BatchCallContextAll(ctx context.Context, b []rp // SuggestGasTipCap suggests a gas tip cap. func (c *SimulatedBackendClient) SuggestGasTipCap(ctx context.Context) (tipCap *big.Int, err error) { - return nil, nil + return c.b.SuggestGasTipCap(ctx) +} + +func (c *SimulatedBackendClient) Backend() *backends.SimulatedBackend { + return c.b } // NodeStates implements evmclient.Client func (c *SimulatedBackendClient) NodeStates() map[string]string { return nil } + +// Commit imports all the pending transactions as a single block and starts a +// fresh new state. +func (c *SimulatedBackendClient) Commit() common.Hash { + return c.b.Commit() +} diff --git a/core/chains/evm/txmgr/transmitchecker.go b/core/chains/evm/txmgr/transmitchecker.go index f29d23a3ad1..8185d381353 100644 --- a/core/chains/evm/txmgr/transmitchecker.go +++ b/core/chains/evm/txmgr/transmitchecker.go @@ -66,7 +66,7 @@ func (c *CheckerFactory) BuildChecker(spec TransmitCheckerSpec) (TransmitChecker } return &VRFV2Checker{ GetCommitment: coord.GetCommitment, - HeaderByNumber: c.Client.HeaderByNumber, + HeadByNumber: c.Client.HeadByNumber, RequestBlockNumber: spec.VRFRequestBlockNumber, }, nil case "": @@ -245,9 +245,9 @@ type VRFV2Checker struct { // Solidity contract. GetCommitment func(opts *bind.CallOpts, requestID *big.Int) ([32]byte, error) - // HeaderByNumber fetches the header given the number. If nil is provided, + // HeadByNumber fetches the head given the number. If nil is provided, // the latest header is fetched. - HeaderByNumber func(ctx context.Context, n *big.Int) (*gethtypes.Header, error) + HeadByNumber func(ctx context.Context, n *big.Int) (*types.Head, error) // RequestBlockNumber is the block number of the VRFV2 request. RequestBlockNumber *big.Int @@ -277,7 +277,7 @@ func (v *VRFV2Checker) Check( return nil } - h, err := v.HeaderByNumber(ctx, nil) + h, err := v.HeadByNumber(ctx, nil) if err != nil { l.Errorw("Failed to fetch latest header. Attempting to transmit anyway.", "err", err, @@ -301,7 +301,7 @@ func (v *VRFV2Checker) Check( // Subtract 5 since the newest block likely isn't indexed yet and will cause "header not found" // errors. - latest := new(big.Int).Sub(h.Number, big.NewInt(5)) + latest := new(big.Int).Sub(big.NewInt(h.Number), big.NewInt(5)) blockNumber := bigmath.Max(latest, v.RequestBlockNumber) callback, err := v.GetCommitment(&bind.CallOpts{ Context: ctx, diff --git a/core/chains/evm/txmgr/transmitchecker_test.go b/core/chains/evm/txmgr/transmitchecker_test.go index b504f41b425..6761772e036 100644 --- a/core/chains/evm/txmgr/transmitchecker_test.go +++ b/core/chains/evm/txmgr/transmitchecker_test.go @@ -310,9 +310,9 @@ func TestTransmitCheckers(t *testing.T) { return [32]byte{1}, nil } }, - HeaderByNumber: func(ctx context.Context, n *big.Int) (*types.Header, error) { - return &types.Header{ - Number: big.NewInt(1), + HeadByNumber: func(ctx context.Context, n *big.Int) (*evmtypes.Head, error) { + return &evmtypes.Head{ + Number: 1, }, nil }, RequestBlockNumber: big.NewInt(1), @@ -335,7 +335,7 @@ func TestTransmitCheckers(t *testing.T) { }) t.Run("can't get header", func(t *testing.T) { - checker.HeaderByNumber = func(ctx context.Context, n *big.Int) (*types.Header, error) { + checker.HeadByNumber = func(ctx context.Context, n *big.Int) (*evmtypes.Head, error) { return nil, errors.New("can't get head") } tx, attempt := newTx(t, big.NewInt(3)) @@ -343,9 +343,9 @@ func TestTransmitCheckers(t *testing.T) { }) t.Run("nil request block number", func(t *testing.T) { - checker.HeaderByNumber = func(ctx context.Context, n *big.Int) (*types.Header, error) { - return &types.Header{ - Number: big.NewInt(1), + checker.HeadByNumber = func(ctx context.Context, n *big.Int) (*evmtypes.Head, error) { + return &evmtypes.Head{ + Number: 1, }, nil } checker.RequestBlockNumber = nil diff --git a/core/services/keeper/integration_test.go b/core/services/keeper/integration_test.go index 9f7078939cd..0cd8cb3b9a2 100644 --- a/core/services/keeper/integration_test.go +++ b/core/services/keeper/integration_test.go @@ -7,7 +7,6 @@ import ( "time" "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" @@ -18,6 +17,7 @@ import ( "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink/core/assets" + "github.com/smartcontractkit/chainlink/core/chains/evm/client" "github.com/smartcontractkit/chainlink/core/chains/evm/forwarders" "github.com/smartcontractkit/chainlink/core/gethwrappers/generated/authorized_forwarder" "github.com/smartcontractkit/chainlink/core/gethwrappers/generated/basic_upkeep_contract" @@ -54,7 +54,7 @@ func deployKeeperRegistry( t *testing.T, version keeper.RegistryVersion, auth *bind.TransactOpts, - backend *backends.SimulatedBackend, + backend *client.SimulatedBackendClient, linkAddr, linkFeedAddr, gasFeedAddr common.Address, ) (common.Address, *keeper.RegistryWrapper) { switch version { @@ -76,6 +76,7 @@ func deployKeeperRegistry( ) require.NoError(t, err) backend.Commit() + wrapper, err := keeper.NewRegistryWrapper(ethkey.EIP55AddressFromAddress(regAddr), backend) require.NoError(t, err) return regAddr, wrapper @@ -147,7 +148,7 @@ func deployKeeperRegistry( } } -func getUpkeepIdFromTx(t *testing.T, registryWrapper *keeper.RegistryWrapper, registrationTx *types.Transaction, backend *backends.SimulatedBackend) *big.Int { +func getUpkeepIdFromTx(t *testing.T, registryWrapper *keeper.RegistryWrapper, registrationTx *types.Transaction, backend *client.SimulatedBackendClient) *big.Int { receipt, err := backend.TransactionReceipt(testutils.Context(t), registrationTx.Hash()) require.NoError(t, err) upkeepId, err := registryWrapper.GetUpkeepIdFromRawRegistrationLog(*receipt.Logs[0]) @@ -198,9 +199,10 @@ func TestKeeperEthIntegration(t *testing.T) { } gasLimit := uint32(ethconfig.Defaults.Miner.GasCeil * 2) - backend := cltest.NewSimulatedBackend(t, genesisData, gasLimit) + b := cltest.NewSimulatedBackend(t, genesisData, gasLimit) + backend := client.NewSimulatedBackendClient(t, b, testutils.SimulatedChainID) - stopMining := cltest.Mine(backend, 1*time.Second) // >> 2 seconds and the test gets slow, << 1 second and the app may miss heads + stopMining := cltest.Mine(backend.Backend(), 1*time.Second) // >> 2 seconds and the test gets slow, << 1 second and the app may miss heads defer stopMining() linkAddr, _, linkToken, err := link_token_interface.DeployLinkToken(sergey, backend) @@ -248,7 +250,7 @@ func TestKeeperEthIntegration(t *testing.T) { scopedConfig := evmtest.NewChainScopedConfig(t, config) korm := keeper.NewORM(db, logger.TestLogger(t), scopedConfig, nil) - app := cltest.NewApplicationWithConfigV2AndKeyOnSimulatedBlockchain(t, config, backend, nodeKey) + app := cltest.NewApplicationWithConfigV2AndKeyOnSimulatedBlockchain(t, config, backend.Backend(), nodeKey) require.NoError(t, app.Start(testutils.Context(t))) // create job @@ -349,9 +351,10 @@ func TestKeeperForwarderEthIntegration(t *testing.T) { } gasLimit := uint32(ethconfig.Defaults.Miner.GasCeil * 2) - backend := cltest.NewSimulatedBackend(t, genesisData, gasLimit) + b := cltest.NewSimulatedBackend(t, genesisData, gasLimit) + backend := client.NewSimulatedBackendClient(t, b, testutils.SimulatedChainID) - stopMining := cltest.Mine(backend, 1*time.Second) // >> 2 seconds and the test gets slow, << 1 second and the app may miss heads + stopMining := cltest.Mine(backend.Backend(), 1*time.Second) // >> 2 seconds and the test gets slow, << 1 second and the app may miss heads defer stopMining() linkAddr, _, linkToken, err := link_token_interface.DeployLinkToken(sergey, backend) @@ -406,11 +409,11 @@ func TestKeeperForwarderEthIntegration(t *testing.T) { scopedConfig := evmtest.NewChainScopedConfig(t, config) korm := keeper.NewORM(db, logger.TestLogger(t), scopedConfig, nil) - app := cltest.NewApplicationWithConfigV2AndKeyOnSimulatedBlockchain(t, config, backend, nodeKey) + app := cltest.NewApplicationWithConfigV2AndKeyOnSimulatedBlockchain(t, config, backend.Backend(), nodeKey) require.NoError(t, app.Start(testutils.Context(t))) forwarderORM := forwarders.NewORM(db, logger.TestLogger(t), config) - chainID := utils.Big(*backend.Blockchain().Config().ChainID) + chainID := utils.Big(*backend.ChainID()) _, err = forwarderORM.CreateForwarder(fwdrAddress, chainID) require.NoError(t, err) @@ -502,9 +505,10 @@ func TestMaxPerformDataSize(t *testing.T) { } gasLimit := uint32(ethconfig.Defaults.Miner.GasCeil * 2) - backend := cltest.NewSimulatedBackend(t, genesisData, gasLimit) + b := cltest.NewSimulatedBackend(t, genesisData, gasLimit) + backend := client.NewSimulatedBackendClient(t, b, testutils.SimulatedChainID) - stopMining := cltest.Mine(backend, 1*time.Second) // >> 2 seconds and the test gets slow, << 1 second and the app may miss heads + stopMining := cltest.Mine(backend.Backend(), 1*time.Second) // >> 2 seconds and the test gets slow, << 1 second and the app may miss heads defer stopMining() linkAddr, _, linkToken, err := link_token_interface.DeployLinkToken(sergey, backend) @@ -548,7 +552,7 @@ func TestMaxPerformDataSize(t *testing.T) { scopedConfig := evmtest.NewChainScopedConfig(t, config) korm := keeper.NewORM(db, logger.TestLogger(t), scopedConfig, nil) - app := cltest.NewApplicationWithConfigV2AndKeyOnSimulatedBlockchain(t, config, backend, nodeKey) + app := cltest.NewApplicationWithConfigV2AndKeyOnSimulatedBlockchain(t, config, backend.Backend(), nodeKey) require.NoError(t, app.Start(testutils.Context(t))) // create job diff --git a/core/services/keeper/registry1_1_synchronizer_test.go b/core/services/keeper/registry1_1_synchronizer_test.go index d8d98bdacc8..049debe274a 100644 --- a/core/services/keeper/registry1_1_synchronizer_test.go +++ b/core/services/keeper/registry1_1_synchronizer_test.go @@ -13,6 +13,7 @@ import ( logmocks "github.com/smartcontractkit/chainlink/core/chains/evm/log/mocks" evmmocks "github.com/smartcontractkit/chainlink/core/chains/evm/mocks" + evmtypes "github.com/smartcontractkit/chainlink/core/chains/evm/types" registry1_1 "github.com/smartcontractkit/chainlink/core/gethwrappers/generated/keeper_registry_wrapper1_1" "github.com/smartcontractkit/chainlink/core/internal/cltest" "github.com/smartcontractkit/chainlink/core/internal/testutils" @@ -56,8 +57,8 @@ func mockRegistry1_1( ) { registryMock := cltest.NewContractMockReceiver(t, ethMock, keeper.Registry1_1ABI, contractAddress) - ethMock.On("HeaderByNumber", mock.Anything, mock.Anything). - Return(&types.Header{Number: big.NewInt(10)}, nil) + ethMock.On("HeadByNumber", mock.Anything, (*big.Int)(nil)). + Return(&evmtypes.Head{Number: 10}, nil) registryMock.MockResponse("getConfig", config).Once() registryMock.MockResponse("getKeeperList", keeperList).Once() registryMock.MockResponse("getCanceledUpkeepList", cancelledUpkeeps).Once() diff --git a/core/services/keeper/registry1_2_synchronizer_test.go b/core/services/keeper/registry1_2_synchronizer_test.go index 6c823e59425..7d15031c311 100644 --- a/core/services/keeper/registry1_2_synchronizer_test.go +++ b/core/services/keeper/registry1_2_synchronizer_test.go @@ -13,6 +13,7 @@ import ( logmocks "github.com/smartcontractkit/chainlink/core/chains/evm/log/mocks" evmmocks "github.com/smartcontractkit/chainlink/core/chains/evm/mocks" + evmtypes "github.com/smartcontractkit/chainlink/core/chains/evm/types" registry1_2 "github.com/smartcontractkit/chainlink/core/gethwrappers/generated/keeper_registry_wrapper1_2" "github.com/smartcontractkit/chainlink/core/internal/cltest" "github.com/smartcontractkit/chainlink/core/internal/testutils" @@ -77,8 +78,8 @@ func mockRegistry1_2( Config: config, Keepers: keeperList, } - ethMock.On("HeaderByNumber", mock.Anything, mock.Anything). - Return(&types.Header{Number: big.NewInt(10)}, nil) + ethMock.On("HeadByNumber", mock.Anything, (*big.Int)(nil)). + Return(&evmtypes.Head{Number: 10}, nil) if getStateTime > 0 { registryMock.MockResponse("getState", getState).Times(getStateTime) } diff --git a/core/services/keeper/registry1_3_synchronizer_test.go b/core/services/keeper/registry1_3_synchronizer_test.go index b4411fb086c..53f0b765e6a 100644 --- a/core/services/keeper/registry1_3_synchronizer_test.go +++ b/core/services/keeper/registry1_3_synchronizer_test.go @@ -17,6 +17,7 @@ import ( logmocks "github.com/smartcontractkit/chainlink/core/chains/evm/log/mocks" evmmocks "github.com/smartcontractkit/chainlink/core/chains/evm/mocks" + evmtypes "github.com/smartcontractkit/chainlink/core/chains/evm/types" "github.com/smartcontractkit/chainlink/core/internal/cltest" "github.com/smartcontractkit/chainlink/core/internal/testutils" "github.com/smartcontractkit/chainlink/core/internal/testutils/evmtest" @@ -79,8 +80,8 @@ func mockRegistry1_3( Config: config, Keepers: keeperList, } - ethMock.On("HeaderByNumber", mock.Anything, mock.Anything). - Return(&types.Header{Number: big.NewInt(10)}, nil) + ethMock.On("HeadByNumber", mock.Anything, (*big.Int)(nil)). + Return(&evmtypes.Head{Number: 10}, nil) if getStateTime > 0 { registryMock.MockResponse("getState", getState).Times(getStateTime) } diff --git a/core/services/keeper/registry_interface.go b/core/services/keeper/registry_interface.go index 262fd8df969..c9964cba170 100644 --- a/core/services/keeper/registry_interface.go +++ b/core/services/keeper/registry_interface.go @@ -12,6 +12,7 @@ import ( "github.com/pkg/errors" evmclient "github.com/smartcontractkit/chainlink/core/chains/evm/client" + evmtypes "github.com/smartcontractkit/chainlink/core/chains/evm/types" registry1_1 "github.com/smartcontractkit/chainlink/core/gethwrappers/generated/keeper_registry_wrapper1_1" registry1_2 "github.com/smartcontractkit/chainlink/core/gethwrappers/generated/keeper_registry_wrapper1_2" registry1_3 "github.com/smartcontractkit/chainlink/core/gethwrappers/generated/keeper_registry_wrapper1_3" @@ -56,13 +57,13 @@ type RegistryWrapper struct { contract1_1 *registry1_1.KeeperRegistry contract1_2 *registry1_2.KeeperRegistry contract1_3 *registry1_3.KeeperRegistry - evmClient bind.ContractBackend + evmClient evmclient.Client } -func NewRegistryWrapper(address ethkey.EIP55Address, backend bind.ContractBackend) (*RegistryWrapper, error) { +func NewRegistryWrapper(address ethkey.EIP55Address, evmClient evmclient.Client) (*RegistryWrapper, error) { interface_wrapper, err := type_and_version.NewTypeAndVersionInterface( address.Address(), - backend, + evmClient, ) if err != nil { return nil, errors.Wrap(err, "unable to create type and interface wrapper") @@ -74,21 +75,21 @@ func NewRegistryWrapper(address ethkey.EIP55Address, backend bind.ContractBacken contract1_1, err := registry1_1.NewKeeperRegistry( address.Address(), - backend, + evmClient, ) if err != nil { return nil, errors.Wrap(err, "unable to create keeper registry 1_1 contract wrapper") } contract1_2, err := registry1_2.NewKeeperRegistry( address.Address(), - backend, + evmClient, ) if err != nil { return nil, errors.Wrap(err, "unable to create keeper registry 1_2 contract wrapper") } contract1_3, err := registry1_3.NewKeeperRegistry( address.Address(), - backend, + evmClient, ) if err != nil { return nil, errors.Wrap(err, "unable to create keeper registry 1_3 contract wrapper") @@ -100,7 +101,7 @@ func NewRegistryWrapper(address ethkey.EIP55Address, backend bind.ContractBacken contract1_1: contract1_1, contract1_2: contract1_2, contract1_3: contract1_3, - evmClient: backend, + evmClient: evmClient, }, nil } @@ -162,17 +163,17 @@ func (rw *RegistryWrapper) getUpkeepCount(opts *bind.CallOpts) (*big.Int, error) func (rw *RegistryWrapper) GetActiveUpkeepIDs(opts *bind.CallOpts) ([]*big.Int, error) { if opts == nil || opts.BlockNumber.Int64() == 0 { - var header *types.Header + var head *evmtypes.Head // fetch the current block number so batched GetActiveUpkeepIDs calls can be performed on the same block - header, err := rw.evmClient.HeaderByNumber(context.Background(), nil) + head, err := rw.evmClient.HeadByNumber(context.Background(), nil) if err != nil { return nil, errors.Wrap(err, "failed to fetch EVM block header") } if opts != nil { - opts.BlockNumber = header.Number + opts.BlockNumber = big.NewInt(head.Number) } else { opts = &bind.CallOpts{ - BlockNumber: header.Number, + BlockNumber: big.NewInt(head.Number), } } } diff --git a/core/services/keeper/upkeep_executer.go b/core/services/keeper/upkeep_executer.go index 68132df315e..2c57148f988 100644 --- a/core/services/keeper/upkeep_executer.go +++ b/core/services/keeper/upkeep_executer.go @@ -237,11 +237,11 @@ func (ex *UpkeepExecuter) execute(upkeep UpkeepRegistration, head *evmtypes.Head func (ex *UpkeepExecuter) turnBlockHashBinary(registry Registry, head *evmtypes.Head, lookback int64) (string, error) { turnBlock := head.Number - (head.Number % int64(registry.BlockCountPerTurn)) - lookback - block, err := ex.ethClient.HeaderByNumber(context.Background(), big.NewInt(turnBlock)) + block, err := ex.ethClient.HeadByNumber(context.Background(), big.NewInt(turnBlock)) if err != nil { return "", err } - hashAtHeight := block.Hash() + hashAtHeight := block.Hash binaryString := fmt.Sprintf("%b", hashAtHeight.Big()) return binaryString, nil } diff --git a/core/services/keeper/upkeep_executer_test.go b/core/services/keeper/upkeep_executer_test.go index f044b301837..abf75afe35d 100644 --- a/core/services/keeper/upkeep_executer_test.go +++ b/core/services/keeper/upkeep_executer_test.go @@ -7,7 +7,6 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/onsi/gomega" "github.com/smartcontractkit/sqlx" "github.com/stretchr/testify/assert" @@ -74,8 +73,7 @@ func setup(t *testing.T, estimator *gasmocks.Estimator, overrideFn func(c *chain db := pgtest.NewSqlxDB(t) keyStore := cltest.NewKeyStore(t, db, cfg) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) - block := &types.Header{Number: big.NewInt(1)} - ethClient.On("HeaderByNumber", mock.Anything, mock.Anything).Maybe().Return(block, nil) + ethClient.On("HeadByNumber", mock.Anything, mock.Anything).Maybe().Return(&evmtypes.Head{Number: 1, Hash: utils.NewHash()}, nil) txm := txmmocks.NewTxManager(t) txm.On("GetGasEstimator").Return(estimator) cc := evmtest.NewChainSet(t, evmtest.TestChainOpts{TxManager: txm, DB: db, Client: ethClient, KeyStore: keyStore.Eth(), GeneralConfig: cfg})