Skip to content

Commit

Permalink
Merge pull request #8001 from smartcontractkit/sc-55510/remove_header…
Browse files Browse the repository at this point in the history
…_usage

Replace header type calls with head
  • Loading branch information
dimriou authored Dec 8, 2022
2 parents 8ed0da1 + 3e3206f commit 190196d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 48 deletions.
12 changes: 11 additions & 1 deletion core/chains/evm/client/simulated_backend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
10 changes: 5 additions & 5 deletions core/chains/evm/txmgr/transmitchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "":
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions core/chains/evm/txmgr/transmitchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -335,17 +335,17 @@ 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))
require.NoError(t, checker.Check(ctx, log, tx, attempt))
})

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
Expand Down
30 changes: 17 additions & 13 deletions core/services/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions core/services/keeper/registry1_1_synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions core/services/keeper/registry1_2_synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions core/services/keeper/registry1_3_synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
23 changes: 12 additions & 11 deletions core/services/keeper/registry_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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
}

Expand Down Expand Up @@ -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),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/services/keeper/upkeep_executer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions core/services/keeper/upkeep_executer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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})
Expand Down

0 comments on commit 190196d

Please sign in to comment.