From 346354ba011cfe56c6b3ab90308eac3d524a7b3b Mon Sep 17 00:00:00 2001 From: Dimitris Grigoriou Date: Fri, 15 Mar 2024 19:16:48 +0200 Subject: [PATCH] Remove remaining small core packages from evm (#12381) * Remove chains from evm config * Change services in rollups * Migrate nullInt64 to common * Use sql NullInt64 * Fix lint * Add NullInt64 Unmarshal test case --- core/chains/evm/config/toml/config.go | 10 ++++---- core/chains/evm/gas/rollups/models.go | 4 ++-- core/chains/evm/log/broadcaster.go | 23 +++++++++++-------- core/chains/evm/log/eth_subscriber.go | 4 ++-- core/chains/evm/types/models.go | 6 ++--- core/chains/evm/types/models_test.go | 18 +++++++++++++-- .../arbitrum_block_translator_test.go | 4 ++-- 7 files changed, 44 insertions(+), 25 deletions(-) diff --git a/core/chains/evm/config/toml/config.go b/core/chains/evm/config/toml/config.go index cb95ed54d56..a81ef4d94ff 100644 --- a/core/chains/evm/config/toml/config.go +++ b/core/chains/evm/config/toml/config.go @@ -1,6 +1,7 @@ package toml import ( + "errors" "fmt" "net/url" "slices" @@ -17,12 +18,13 @@ import ( commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" "github.com/smartcontractkit/chainlink/v2/common/config" - "github.com/smartcontractkit/chainlink/v2/core/chains" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" ) +var ErrNotFound = errors.New("not found") + type HasEVMConfigs interface { EVMConfigs() EVMConfigs } @@ -144,7 +146,7 @@ func (cs EVMConfigs) Node(name string) (types.Node, error) { } } } - return types.Node{}, fmt.Errorf("node %s: %w", name, chains.ErrNotFound) + return types.Node{}, fmt.Errorf("node %s: %w", name, ErrNotFound) } func (cs EVMConfigs) NodeStatus(name string) (commontypes.NodeStatus, error) { @@ -155,7 +157,7 @@ func (cs EVMConfigs) NodeStatus(name string) (commontypes.NodeStatus, error) { } } } - return commontypes.NodeStatus{}, fmt.Errorf("node %s: %w", name, chains.ErrNotFound) + return commontypes.NodeStatus{}, fmt.Errorf("node %s: %w", name, ErrNotFound) } func legacyNode(n *Node, chainID *big.Big) (v2 types.Node) { @@ -204,7 +206,7 @@ func (cs EVMConfigs) Nodes(chainID string) (ns []types.Node, err error) { } nodes := cs.nodes(chainID) if nodes == nil { - err = fmt.Errorf("no nodes: chain %q: %w", chainID, chains.ErrNotFound) + err = fmt.Errorf("no nodes: chain %q: %w", chainID, ErrNotFound) return } for _, n := range nodes { diff --git a/core/chains/evm/gas/rollups/models.go b/core/chains/evm/gas/rollups/models.go index 8158ba2b906..7aa3d4059dd 100644 --- a/core/chains/evm/gas/rollups/models.go +++ b/core/chains/evm/gas/rollups/models.go @@ -6,8 +6,8 @@ import ( "github.com/ethereum/go-ethereum/core/types" + "github.com/smartcontractkit/chainlink-common/pkg/services" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets" - "github.com/smartcontractkit/chainlink/v2/core/services" ) // L1Oracle provides interface for fetching L1-specific fee components if the chain is an L2. @@ -15,7 +15,7 @@ import ( // //go:generate mockery --quiet --name L1Oracle --output ./mocks/ --case=underscore type L1Oracle interface { - services.ServiceCtx + services.Service GasPrice(ctx context.Context) (*assets.Wei, error) GetGasCost(ctx context.Context, tx *types.Transaction, blockNum *big.Int) (*assets.Wei, error) diff --git a/core/chains/evm/log/broadcaster.go b/core/chains/evm/log/broadcaster.go index c4db2a4826c..6a47d823ea6 100644 --- a/core/chains/evm/log/broadcaster.go +++ b/core/chains/evm/log/broadcaster.go @@ -2,6 +2,7 @@ package log import ( "context" + "database/sql" "fmt" "math/big" "sync" @@ -22,7 +23,6 @@ import ( evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" evmutils "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated" - "github.com/smartcontractkit/chainlink/v2/core/null" "github.com/smartcontractkit/chainlink/v2/core/services/pg" ) @@ -69,7 +69,7 @@ type ( BroadcasterInTest interface { Broadcaster - BackfillBlockNumber() null.Int64 + BackfillBlockNumber() sql.NullInt64 TrackedAddressesCount() uint32 // Pause pauses the eventLoop until Resume is called. Pause() @@ -98,7 +98,7 @@ type ( evmChainID big.Int // a block number to start backfill from - backfillBlockNumber null.Int64 + backfillBlockNumber sql.NullInt64 ethSubscriber *ethSubscriber registrations *registrations @@ -327,7 +327,7 @@ func (b *broadcaster) startResubscribeLoop() { if from < 0 { from = 0 } - b.backfillBlockNumber = null.NewInt64(from, true) + b.backfillBlockNumber = sql.NullInt64{Int64: from, Valid: true} } // Remove leftover unconsumed logs, maybe update pending broadcasts, and backfill sooner if necessary. @@ -337,7 +337,8 @@ func (b *broadcaster) startResubscribeLoop() { // No need to worry about r.highestNumConfirmations here because it's // already at minimum this deep due to the latest seen head check above if !b.backfillBlockNumber.Valid || *backfillStart < b.backfillBlockNumber.Int64 { - b.backfillBlockNumber.SetValid(*backfillStart) + b.backfillBlockNumber.Int64 = *backfillStart + b.backfillBlockNumber.Valid = true } } @@ -490,7 +491,8 @@ func (b *broadcaster) onReplayRequest(replayReq replayRequest) { // NOTE: This ignores r.highestNumConfirmations, but it is // generally assumed that this will only be performed rarely and // manually by someone who knows what he is doing - b.backfillBlockNumber.SetValid(replayReq.fromBlock) + b.backfillBlockNumber.Int64 = replayReq.fromBlock + b.backfillBlockNumber.Valid = true if replayReq.forceBroadcast { ctx, cancel := b.chStop.NewCtx() defer cancel() @@ -515,7 +517,8 @@ func (b *broadcaster) invalidatePool() int64 { b.logPool = newLogPool(b.logger) // Note: even if we crash right now, PendingMinBlock is preserved in the database and we will backfill the same. blockNum := int64(min.(Uint64)) - b.backfillBlockNumber.SetValid(blockNum) + b.backfillBlockNumber.Int64 = blockNum + b.backfillBlockNumber.Valid = true return blockNum } return -1 @@ -717,7 +720,7 @@ func (b *broadcaster) TrackedAddressesCount() uint32 { } // test only -func (b *broadcaster) BackfillBlockNumber() null.Int64 { +func (b *broadcaster) BackfillBlockNumber() sql.NullInt64 { return b.backfillBlockNumber } @@ -766,8 +769,8 @@ func (n *NullBroadcaster) Register(listener Listener, opts ListenerOpts) (unsubs // ReplayFromBlock implements the Broadcaster interface. func (n *NullBroadcaster) ReplayFromBlock(number int64, forceBroadcast bool) {} -func (n *NullBroadcaster) BackfillBlockNumber() null.Int64 { - return null.NewInt64(0, false) +func (n *NullBroadcaster) BackfillBlockNumber() sql.NullInt64 { + return sql.NullInt64{Int64: 0, Valid: false} } func (n *NullBroadcaster) TrackedAddressesCount() uint32 { return 0 diff --git a/core/chains/evm/log/eth_subscriber.go b/core/chains/evm/log/eth_subscriber.go index ff20a6e74e8..e5ba202dbf2 100644 --- a/core/chains/evm/log/eth_subscriber.go +++ b/core/chains/evm/log/eth_subscriber.go @@ -2,6 +2,7 @@ package log import ( "context" + "database/sql" "fmt" "math/big" "time" @@ -15,7 +16,6 @@ import ( evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils" - "github.com/smartcontractkit/chainlink/v2/core/null" ) type ( @@ -39,7 +39,7 @@ func newEthSubscriber(ethClient evmclient.Client, config Config, lggr logger.Log // backfillLogs - fetches earlier logs either from a relatively recent block (latest minus BlockBackfillDepth) or from the given fromBlockOverride // note that the whole operation has no timeout - it relies on BlockBackfillSkip (set outside) to optionally prevent very deep, long backfills // Max runtime is: (10 sec + 1 min * numBlocks/batchSize) * 3 retries -func (sub *ethSubscriber) backfillLogs(fromBlockOverride null.Int64, addresses []common.Address, topics []common.Hash) (chBackfilledLogs chan types.Log, abort bool) { +func (sub *ethSubscriber) backfillLogs(fromBlockOverride sql.NullInt64, addresses []common.Address, topics []common.Hash) (chBackfilledLogs chan types.Log, abort bool) { sub.logger.Infow("backfilling logs", "from", fromBlockOverride, "addresses", addresses) if len(addresses) == 0 { sub.logger.Debug("LogBroadcaster: No addresses to backfill for, returning") diff --git a/core/chains/evm/types/models.go b/core/chains/evm/types/models.go index 7f312401f7d..4aeaec511d1 100644 --- a/core/chains/evm/types/models.go +++ b/core/chains/evm/types/models.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "database/sql" "database/sql/driver" "encoding/json" "fmt" @@ -24,7 +25,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types/internal/blocks" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils" ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" - "github.com/smartcontractkit/chainlink/v2/core/null" ) // Head represents a BlockNumber, BlockHash. @@ -32,7 +32,7 @@ type Head struct { ID uint64 Hash common.Hash Number int64 - L1BlockNumber null.Int64 + L1BlockNumber sql.NullInt64 ParentHash common.Hash Parent *Head EVMChainID *ubig.Big @@ -285,7 +285,7 @@ func (h *Head) UnmarshalJSON(bs []byte) error { h.Timestamp = time.Unix(int64(jsonHead.Timestamp), 0).UTC() h.BaseFeePerGas = assets.NewWei((*big.Int)(jsonHead.BaseFeePerGas)) if jsonHead.L1BlockNumber != nil { - h.L1BlockNumber = null.Int64From((*big.Int)(jsonHead.L1BlockNumber).Int64()) + h.L1BlockNumber = sql.NullInt64{Int64: (*big.Int)(jsonHead.L1BlockNumber).Int64(), Valid: true} } h.ReceiptsRoot = jsonHead.ReceiptsRoot h.TransactionsRoot = jsonHead.TransactionsRoot diff --git a/core/chains/evm/types/models_test.go b/core/chains/evm/types/models_test.go index 4fc986ae9d3..38dbfa76a0a 100644 --- a/core/chains/evm/types/models_test.go +++ b/core/chains/evm/types/models_test.go @@ -2,6 +2,7 @@ package types_test import ( "bytes" + "database/sql" "encoding/json" "fmt" "math" @@ -17,6 +18,7 @@ import ( "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink-common/pkg/utils/hex" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" @@ -25,7 +27,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" - "github.com/smartcontractkit/chainlink/v2/core/null" ) func TestHead_NewHead(t *testing.T) { @@ -329,7 +330,20 @@ func TestHead_UnmarshalJSON(t *testing.T) { Number: 0x15156, ParentHash: common.HexToHash("0x923ad1e27c1d43cb2d2fb09e26d2502ca4b4914a2e0599161d279c6c06117d34"), Timestamp: time.Unix(0x60d0952d, 0).UTC(), - L1BlockNumber: null.Int64From(0x8652f9), + L1BlockNumber: sql.NullInt64{Int64: 0x8652f9, Valid: true}, + ReceiptsRoot: common.HexToHash("0x2c292672b8fc9d223647a2569e19721f0757c96a1421753a93e141f8e56cf504"), + TransactionsRoot: common.HexToHash("0x71448077f5ce420a8e24db62d4d58e8d8e6ad2c7e76318868e089d41f7e0faf3"), + StateRoot: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, + }, + {"arbitrum_empty_l1BlockNumber", + `{"number":"0x15156","hash":"0x752dab43f7a2482db39227d46cd307623b26167841e2207e93e7566ab7ab7871","parentHash":"0x923ad1e27c1d43cb2d2fb09e26d2502ca4b4914a2e0599161d279c6c06117d34","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x71448077f5ce420a8e24db62d4d58e8d8e6ad2c7e76318868e089d41f7e0faf3","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x2c292672b8fc9d223647a2569e19721f0757c96a1421753a93e141f8e56cf504","miner":"0x0000000000000000000000000000000000000000","difficulty":"0x0","totalDifficulty":"0x0","extraData":"0x","size":"0x0","gasLimit":"0x11278208","gasUsed":"0x3d1fe9","timestamp":"0x60d0952d","transactions":["0xa1ea93556b93ed3b45cb24f21c8deb584e6a9049c35209242651bf3533c23b98","0xfc6593c45ba92351d17173aa1381e84734d252ab0169887783039212c4a41024","0x85ee9d04fd0ebb5f62191eeb53cb45d9c0945d43eba444c3548de2ac8421682f","0x50d120936473e5b75f6e04829ad4eeca7a1df7d3c5026ebb5d34af936a39b29c"],"uncles":[]}`, + evmtypes.Head{ + Hash: common.HexToHash("0x752dab43f7a2482db39227d46cd307623b26167841e2207e93e7566ab7ab7871"), + Number: 0x15156, + ParentHash: common.HexToHash("0x923ad1e27c1d43cb2d2fb09e26d2502ca4b4914a2e0599161d279c6c06117d34"), + Timestamp: time.Unix(0x60d0952d, 0).UTC(), + L1BlockNumber: sql.NullInt64{Int64: 0, Valid: false}, ReceiptsRoot: common.HexToHash("0x2c292672b8fc9d223647a2569e19721f0757c96a1421753a93e141f8e56cf504"), TransactionsRoot: common.HexToHash("0x71448077f5ce420a8e24db62d4d58e8d8e6ad2c7e76318868e089d41f7e0faf3"), StateRoot: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), diff --git a/core/services/ocrcommon/arbitrum_block_translator_test.go b/core/services/ocrcommon/arbitrum_block_translator_test.go index 1ad3a6c5950..fa6875fb798 100644 --- a/core/services/ocrcommon/arbitrum_block_translator_test.go +++ b/core/services/ocrcommon/arbitrum_block_translator_test.go @@ -1,6 +1,7 @@ package ocrcommon_test import ( + "database/sql" "math/big" mrand "math/rand" "testing" @@ -10,7 +11,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" "github.com/smartcontractkit/chainlink/v2/core/logger" - "github.com/smartcontractkit/chainlink/v2/core/null" "github.com/smartcontractkit/chainlink/v2/core/services/ocrcommon" "github.com/ethereum/go-ethereum/common" @@ -239,7 +239,7 @@ func generateDeterministicL2Blocks() (heads []evmtypes.Head) { for i := 0; i <= l2max; i++ { head := evmtypes.Head{ Number: int64(i), - L1BlockNumber: null.Int64From(l1BlockNumber), + L1BlockNumber: sql.NullInt64{Int64: l1BlockNumber, Valid: true}, Hash: utils.NewHash(), ParentHash: parentHash, }