Skip to content

Commit

Permalink
Remove remaining small core packages from evm (#12381)
Browse files Browse the repository at this point in the history
* Remove chains from evm config

* Change services in rollups

* Migrate nullInt64 to common

* Use sql NullInt64

* Fix lint

* Add NullInt64 Unmarshal test case
  • Loading branch information
dimriou authored Mar 15, 2024
1 parent 4cb56fd commit 346354b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 deletions.
10 changes: 6 additions & 4 deletions core/chains/evm/config/toml/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package toml

import (
"errors"
"fmt"
"net/url"
"slices"
Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions core/chains/evm/gas/rollups/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ 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.
// For example, on Optimistic Rollups, this oracle can return rollup-specific l1BaseFee
//
//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)
Expand Down
23 changes: 13 additions & 10 deletions core/chains/evm/log/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"context"
"database/sql"
"fmt"
"math/big"
"sync"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/chains/evm/log/eth_subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"context"
"database/sql"
"fmt"
"math/big"
"time"
Expand All @@ -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 (
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions core/chains/evm/types/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
Expand All @@ -24,15 +25,14 @@ 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.
type Head struct {
ID uint64
Hash common.Hash
Number int64
L1BlockNumber null.Int64
L1BlockNumber sql.NullInt64
ParentHash common.Hash
Parent *Head
EVMChainID *ubig.Big
Expand Down Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions core/chains/evm/types/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types_test

import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"math"
Expand All @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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"),
Expand Down
4 changes: 2 additions & 2 deletions core/services/ocrcommon/arbitrum_block_translator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ocrcommon_test

import (
"database/sql"
"math/big"
mrand "math/rand"
"testing"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 346354b

Please sign in to comment.