Skip to content

Commit

Permalink
Remove all utils dependencies from evm (#11622)
Browse files Browse the repository at this point in the history
* Move core eth utils to evm

* Fixes

* Fix import mismatch

* Move error handling utils

* Remove all utils dependencies from evm

* Fix Sleepertask

* Remove unused utils

* Fix errors

* Undo test removal

* Upgrade chainlink-common

---------

Co-authored-by: Prashant Yadav <[email protected]>
  • Loading branch information
dimriou and prashantkumar1982 authored Dec 21, 2023
1 parent c5aa49b commit 09067a3
Show file tree
Hide file tree
Showing 88 changed files with 198 additions and 628 deletions.
80 changes: 40 additions & 40 deletions core/chains/evm/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"gopkg.in/guregu/null.v4"

commonassets "github.com/smartcontractkit/chainlink-common/pkg/assets"
commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"

"github.com/smartcontractkit/chainlink/v2/common/config"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
configutils "github.com/smartcontractkit/chainlink/v2/core/utils/config"
)

type HasEVMConfigs interface {
Expand All @@ -37,41 +37,41 @@ func (cs EVMConfigs) ValidateConfig() (err error) {

func (cs EVMConfigs) validateKeys() (err error) {
// Unique chain IDs
chainIDs := configutils.UniqueStrings{}
chainIDs := commonconfig.UniqueStrings{}
for i, c := range cs {
if chainIDs.IsDupeFmt(c.ChainID) {
err = multierr.Append(err, configutils.NewErrDuplicate(fmt.Sprintf("%d.ChainID", i), c.ChainID.String()))
err = multierr.Append(err, commonconfig.NewErrDuplicate(fmt.Sprintf("%d.ChainID", i), c.ChainID.String()))
}
}

// Unique node names
names := configutils.UniqueStrings{}
names := commonconfig.UniqueStrings{}
for i, c := range cs {
for j, n := range c.Nodes {
if names.IsDupe(n.Name) {
err = multierr.Append(err, configutils.NewErrDuplicate(fmt.Sprintf("%d.Nodes.%d.Name", i, j), *n.Name))
err = multierr.Append(err, commonconfig.NewErrDuplicate(fmt.Sprintf("%d.Nodes.%d.Name", i, j), *n.Name))
}
}
}

// Unique node WSURLs
wsURLs := configutils.UniqueStrings{}
wsURLs := commonconfig.UniqueStrings{}
for i, c := range cs {
for j, n := range c.Nodes {
u := (*url.URL)(n.WSURL)
if wsURLs.IsDupeFmt(u) {
err = multierr.Append(err, configutils.NewErrDuplicate(fmt.Sprintf("%d.Nodes.%d.WSURL", i, j), u.String()))
err = multierr.Append(err, commonconfig.NewErrDuplicate(fmt.Sprintf("%d.Nodes.%d.WSURL", i, j), u.String()))
}
}
}

// Unique node HTTPURLs
httpURLs := configutils.UniqueStrings{}
httpURLs := commonconfig.UniqueStrings{}
for i, c := range cs {
for j, n := range c.Nodes {
u := (*url.URL)(n.HTTPURL)
if httpURLs.IsDupeFmt(u) {
err = multierr.Append(err, configutils.NewErrDuplicate(fmt.Sprintf("%d.Nodes.%d.HTTPURL", i, j), u.String()))
err = multierr.Append(err, commonconfig.NewErrDuplicate(fmt.Sprintf("%d.Nodes.%d.HTTPURL", i, j), u.String()))
}
}
}
Expand Down Expand Up @@ -290,29 +290,29 @@ func (c *EVMConfig) SetFrom(f *EVMConfig) {

func (c *EVMConfig) ValidateConfig() (err error) {
if c.ChainID == nil {
err = multierr.Append(err, configutils.ErrMissing{Name: "ChainID", Msg: "required for all chains"})
err = multierr.Append(err, commonconfig.ErrMissing{Name: "ChainID", Msg: "required for all chains"})
} else if c.ChainID.String() == "" {
err = multierr.Append(err, configutils.ErrEmpty{Name: "ChainID", Msg: "required for all chains"})
err = multierr.Append(err, commonconfig.ErrEmpty{Name: "ChainID", Msg: "required for all chains"})
} else if must, ok := ChainTypeForID(c.ChainID); ok { // known chain id
if c.ChainType == nil && must != "" {
err = multierr.Append(err, configutils.ErrMissing{Name: "ChainType",
err = multierr.Append(err, commonconfig.ErrMissing{Name: "ChainType",
Msg: fmt.Sprintf("only %q can be used with this chain id", must)})
} else if c.ChainType != nil && *c.ChainType != string(must) {
if *c.ChainType == "" {
err = multierr.Append(err, configutils.ErrEmpty{Name: "ChainType",
err = multierr.Append(err, commonconfig.ErrEmpty{Name: "ChainType",
Msg: fmt.Sprintf("only %q can be used with this chain id", must)})
} else if must == "" {
err = multierr.Append(err, configutils.ErrInvalid{Name: "ChainType", Value: *c.ChainType,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "ChainType", Value: *c.ChainType,
Msg: "must not be set with this chain id"})
} else {
err = multierr.Append(err, configutils.ErrInvalid{Name: "ChainType", Value: *c.ChainType,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "ChainType", Value: *c.ChainType,
Msg: fmt.Sprintf("only %q can be used with this chain id", must)})
}
}
}

if len(c.Nodes) == 0 {
err = multierr.Append(err, configutils.ErrMissing{Name: "Nodes", Msg: "must have at least one node"})
err = multierr.Append(err, commonconfig.ErrMissing{Name: "Nodes", Msg: "must have at least one node"})
} else {
var hasPrimary bool
for _, n := range c.Nodes {
Expand All @@ -323,7 +323,7 @@ func (c *EVMConfig) ValidateConfig() (err error) {
break
}
if !hasPrimary {
err = multierr.Append(err, configutils.ErrMissing{Name: "Nodes",
err = multierr.Append(err, commonconfig.ErrMissing{Name: "Nodes",
Msg: "must have at least one primary node with WSURL"})
}
}
Expand Down Expand Up @@ -377,24 +377,24 @@ func (c *Chain) ValidateConfig() (err error) {
chainType = config.ChainType(*c.ChainType)
}
if !chainType.IsValid() {
err = multierr.Append(err, configutils.ErrInvalid{Name: "ChainType", Value: *c.ChainType,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "ChainType", Value: *c.ChainType,
Msg: config.ErrInvalidChainType.Error()})
}

if c.GasEstimator.BumpTxDepth != nil && uint32(*c.GasEstimator.BumpTxDepth) > *c.Transactions.MaxInFlight {
err = multierr.Append(err, configutils.ErrInvalid{Name: "GasEstimator.BumpTxDepth", Value: *c.GasEstimator.BumpTxDepth,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "GasEstimator.BumpTxDepth", Value: *c.GasEstimator.BumpTxDepth,
Msg: "must be less than or equal to Transactions.MaxInFlight"})
}
if *c.HeadTracker.HistoryDepth < *c.FinalityDepth {
err = multierr.Append(err, configutils.ErrInvalid{Name: "HeadTracker.HistoryDepth", Value: *c.HeadTracker.HistoryDepth,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "HeadTracker.HistoryDepth", Value: *c.HeadTracker.HistoryDepth,
Msg: "must be equal to or greater than FinalityDepth"})
}
if *c.FinalityDepth < 1 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "FinalityDepth", Value: *c.FinalityDepth,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "FinalityDepth", Value: *c.FinalityDepth,
Msg: "must be greater than or equal to 1"})
}
if *c.MinIncomingConfirmations < 1 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "MinIncomingConfirmations", Value: *c.MinIncomingConfirmations,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "MinIncomingConfirmations", Value: *c.MinIncomingConfirmations,
Msg: "must be greater than or equal to 1"})
}
return
Expand Down Expand Up @@ -487,36 +487,36 @@ type GasEstimator struct {

func (e *GasEstimator) ValidateConfig() (err error) {
if uint64(*e.BumpPercent) < txpool.DefaultConfig.PriceBump {
err = multierr.Append(err, configutils.ErrInvalid{Name: "BumpPercent", Value: *e.BumpPercent,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "BumpPercent", Value: *e.BumpPercent,
Msg: fmt.Sprintf("may not be less than Geth's default of %d", txpool.DefaultConfig.PriceBump)})
}
if e.TipCapDefault.Cmp(e.TipCapMin) < 0 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "TipCapDefault", Value: e.TipCapDefault,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "TipCapDefault", Value: e.TipCapDefault,
Msg: "must be greater than or equal to TipCapMinimum"})
}
if e.FeeCapDefault.Cmp(e.TipCapDefault) < 0 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "FeeCapDefault", Value: e.TipCapDefault,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "FeeCapDefault", Value: e.TipCapDefault,
Msg: "must be greater than or equal to TipCapDefault"})
}
if *e.Mode == "FixedPrice" && *e.BumpThreshold == 0 && *e.EIP1559DynamicFees && e.FeeCapDefault.Cmp(e.PriceMax) != 0 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "FeeCapDefault", Value: e.FeeCapDefault,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "FeeCapDefault", Value: e.FeeCapDefault,
Msg: fmt.Sprintf("must be equal to PriceMax (%s) since you are using FixedPrice estimation with gas bumping disabled in "+
"EIP1559 mode - PriceMax will be used as the FeeCap for transactions instead of FeeCapDefault", e.PriceMax)})
} else if e.FeeCapDefault.Cmp(e.PriceMax) > 0 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "FeeCapDefault", Value: e.FeeCapDefault,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "FeeCapDefault", Value: e.FeeCapDefault,
Msg: fmt.Sprintf("must be less than or equal to PriceMax (%s)", e.PriceMax)})
}

if e.PriceMin.Cmp(e.PriceDefault) > 0 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "PriceMin", Value: e.PriceMin,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "PriceMin", Value: e.PriceMin,
Msg: "must be less than or equal to PriceDefault"})
}
if e.PriceMax.Cmp(e.PriceDefault) < 0 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "PriceMax", Value: e.PriceMin,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "PriceMax", Value: e.PriceMin,
Msg: "must be greater than or equal to PriceDefault"})
}
if *e.Mode == "BlockHistory" && *e.BlockHistory.BlockHistorySize <= 0 {
err = multierr.Append(err, configutils.ErrInvalid{Name: "BlockHistory.BlockHistorySize", Value: *e.BlockHistory.BlockHistorySize,
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "BlockHistory.BlockHistorySize", Value: *e.BlockHistory.BlockHistorySize,
Msg: "must be greater than or equal to 1 with BlockHistory Mode"})
}

Expand Down Expand Up @@ -643,7 +643,7 @@ func (ks KeySpecificConfig) ValidateConfig() (err error) {
for _, k := range ks {
addr := k.Key.String()
if _, ok := addrs[addr]; ok {
err = multierr.Append(err, configutils.NewErrDuplicate("Key", addr))
err = multierr.Append(err, commonconfig.NewErrDuplicate("Key", addr))
} else {
addrs[addr] = struct{}{}
}
Expand Down Expand Up @@ -750,9 +750,9 @@ type Node struct {

func (n *Node) ValidateConfig() (err error) {
if n.Name == nil {
err = multierr.Append(err, configutils.ErrMissing{Name: "Name", Msg: "required for all nodes"})
err = multierr.Append(err, commonconfig.ErrMissing{Name: "Name", Msg: "required for all nodes"})
} else if *n.Name == "" {
err = multierr.Append(err, configutils.ErrEmpty{Name: "Name", Msg: "required for all nodes"})
err = multierr.Append(err, commonconfig.ErrEmpty{Name: "Name", Msg: "required for all nodes"})
}

var sendOnly bool
Expand All @@ -761,34 +761,34 @@ func (n *Node) ValidateConfig() (err error) {
}
if n.WSURL == nil {
if !sendOnly {
err = multierr.Append(err, configutils.ErrMissing{Name: "WSURL", Msg: "required for primary nodes"})
err = multierr.Append(err, commonconfig.ErrMissing{Name: "WSURL", Msg: "required for primary nodes"})
}
} else if n.WSURL.IsZero() {
if !sendOnly {
err = multierr.Append(err, configutils.ErrEmpty{Name: "WSURL", Msg: "required for primary nodes"})
err = multierr.Append(err, commonconfig.ErrEmpty{Name: "WSURL", Msg: "required for primary nodes"})
}
} else {
switch n.WSURL.Scheme {
case "ws", "wss":
default:
err = multierr.Append(err, configutils.ErrInvalid{Name: "WSURL", Value: n.WSURL.Scheme, Msg: "must be ws or wss"})
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "WSURL", Value: n.WSURL.Scheme, Msg: "must be ws or wss"})
}
}

if n.HTTPURL == nil {
err = multierr.Append(err, configutils.ErrMissing{Name: "HTTPURL", Msg: "required for all nodes"})
err = multierr.Append(err, commonconfig.ErrMissing{Name: "HTTPURL", Msg: "required for all nodes"})
} else if n.HTTPURL.IsZero() {
err = multierr.Append(err, configutils.ErrEmpty{Name: "HTTPURL", Msg: "required for all nodes"})
err = multierr.Append(err, commonconfig.ErrEmpty{Name: "HTTPURL", Msg: "required for all nodes"})
} else {
switch n.HTTPURL.Scheme {
case "http", "https":
default:
err = multierr.Append(err, configutils.ErrInvalid{Name: "HTTPURL", Value: n.HTTPURL.Scheme, Msg: "must be http or https"})
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "HTTPURL", Value: n.HTTPURL.Scheme, Msg: "must be http or https"})
}
}

if n.Order != nil && (*n.Order < 1 || *n.Order > 100) {
err = multierr.Append(err, configutils.ErrInvalid{Name: "Order", Value: *n.Order, Msg: "must be between 1 and 100"})
err = multierr.Append(err, commonconfig.ErrInvalid{Name: "Order", Value: *n.Order, Msg: "must be between 1 and 100"})
} else if n.Order == nil {
z := int32(100)
n.Order = &z
Expand Down
4 changes: 2 additions & 2 deletions core/chains/evm/config/toml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"slices"
"strings"

cconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink/v2/common/config"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
configutils "github.com/smartcontractkit/chainlink/v2/core/utils/config"
)

var (
Expand Down Expand Up @@ -40,7 +40,7 @@ func init() {
Chain
}{}

if err := configutils.DecodeTOML(bytes.NewReader(b), &config); err != nil {
if err := cconfig.DecodeTOML(bytes.NewReader(b), &config); err != nil {
log.Fatalf("failed to decode %q: %v", path, err)
}
if fe.Name() == "fallback.toml" {
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/gas/arbitrum_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils"

feetypes "github.com/smartcontractkit/chainlink/v2/common/fee/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

type ArbConfig interface {
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/gas/rollups/l1_gas_price_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils"

"github.com/smartcontractkit/chainlink/v2/common/config"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

//go:generate mockery --quiet --name ethClient --output ./mocks/ --case=underscore --structname ETHClient
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/gas/suggested_price_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils"

feetypes "github.com/smartcontractkit/chainlink/v2/common/fee/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/log/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
"github.com/smartcontractkit/chainlink-common/pkg/utils/mailbox"

evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated"
"github.com/smartcontractkit/chainlink/v2/core/null"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

//go:generate mockery --quiet --name Broadcaster --output ./mocks/ --case=underscore --structname Broadcaster --filename broadcaster.go
Expand Down
3 changes: 2 additions & 1 deletion core/chains/evm/log/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
"github.com/jmoiron/sqlx"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/utils"

ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

// ORM is the interface for log broadcasts.
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
"github.com/smartcontractkit/chainlink-common/pkg/utils/mathutil"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

//go:generate mockery --quiet --name LogPoller --output ./mocks/ --case=underscore --structname LogPoller --filename log_poller.go
Expand Down
4 changes: 3 additions & 1 deletion core/chains/evm/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"go.uber.org/zap/zapcore"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
commonutils "github.com/smartcontractkit/chainlink-common/pkg/utils"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
Expand Down Expand Up @@ -1294,7 +1296,7 @@ func TestLogPoller_DBErrorHandling(t *testing.T) {
require.ErrorContains(t, err, "Invalid replay block number")

// Force a db error while loading the filters (tx aborted, already rolled back)
require.Error(t, utils.JustError(db.Exec(`invalid query`)))
require.Error(t, commonutils.JustError(db.Exec(`invalid query`)))
go func() {
err = lp.Replay(ctx, 2)
assert.ErrorContains(t, err, "current transaction is aborted")
Expand Down
4 changes: 2 additions & 2 deletions core/chains/evm/monitor/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

//go:generate mockery --quiet --name BalanceMonitor --output ../mocks/ --case=underscore
Expand All @@ -42,7 +42,7 @@ type (
ethKeyStore keystore.Eth
ethBalances map[gethCommon.Address]*assets.Eth
ethBalancesMtx *sync.RWMutex
sleeperTask utils.SleeperTask
sleeperTask *utils.SleeperTask
}

NullBalanceMonitor struct{}
Expand Down
Loading

0 comments on commit 09067a3

Please sign in to comment.