Skip to content

Commit

Permalink
BCF-2508: cleanup evm config for chain, relayer, and relayer factory (#…
Browse files Browse the repository at this point in the history
…10767)

* BCF-2508: cleanup evm config for chain, relayer, and relayer factory

* address comments - simplify validation and error creation
  • Loading branch information
krehermann authored Sep 25, 2023
1 parent 6dc2b6d commit 13f4986
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 93 deletions.
61 changes: 43 additions & 18 deletions core/chains/evm/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,34 @@ type AppConfig interface {

type ChainRelayExtenderConfig struct {
Logger logger.Logger
DB *sqlx.DB
KeyStore keystore.Eth
*RelayerConfig
ChainOpts
}

// options for the relayer factory.
// TODO BCF-2508 clean up configuration of chain and relayer after BCF-2440
// the factory wants to own the logger and db
// the factory creates extenders, which need the same and more opts
type RelayerConfig struct {
func (c ChainRelayExtenderConfig) Validate() error {
err := c.ChainOpts.Validate()
if c.Logger == nil {
err = errors.Join(err, errors.New("nil Logger"))
}
if c.KeyStore == nil {
err = errors.Join(err, errors.New("nil Keystore"))
}

if err != nil {
err = fmt.Errorf("invalid ChainRelayerExtenderConfig: %w", err)
}
return err
}

type ChainOpts struct {
AppConfig AppConfig

EventBroadcaster pg.EventBroadcaster
MailMon *utils.MailboxMonitor
GasEstimator gas.EvmFeeEstimator

*sqlx.DB

// TODO BCF-2513 remove test code from the API
// Gen-functions are useful for dependency injection by tests
GenEthClient func(*big.Int) client.Client
Expand All @@ -168,7 +180,31 @@ type RelayerConfig struct {
GenGasEstimator func(*big.Int) gas.EvmFeeEstimator
}

func (o ChainOpts) Validate() error {
var err error
if o.AppConfig == nil {
err = errors.Join(err, errors.New("nil AppConfig"))
}
if o.EventBroadcaster == nil {
err = errors.Join(err, errors.New("nil EventBroadcaster"))
}
if o.MailMon == nil {
err = errors.Join(err, errors.New("nil MailMon"))
}
if o.DB == nil {
err = errors.Join(err, errors.New("nil DB"))
}
if err != nil {
err = fmt.Errorf("invalid ChainOpts: %w", err)
}
return err
}

func NewTOMLChain(ctx context.Context, chain *toml.EVMConfig, opts ChainRelayExtenderConfig) (Chain, error) {
err := opts.Validate()
if err != nil {
return nil, err
}
chainID := chain.ChainID
l := opts.Logger.With("evmChainID", chainID.String())
if !chain.IsEnabled() {
Expand Down Expand Up @@ -458,14 +494,3 @@ func newPrimary(cfg evmconfig.NodePool, noNewHeadsThreshold time.Duration, lggr

return evmclient.NewNode(cfg, noNewHeadsThreshold, lggr, (url.URL)(*n.WSURL), (*url.URL)(n.HTTPURL), *n.Name, id, chainID, *n.Order), nil
}

func (opts *ChainRelayExtenderConfig) Check() error {
if opts.Logger == nil {
return errors.New("logger must be non-nil")
}
if opts.AppConfig == nil {
return errors.New("config must be non-nil")
}

return nil
}
51 changes: 51 additions & 0 deletions core/chains/evm/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"math/big"
"testing"

"github.com/smartcontractkit/sqlx"
"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/mocks"
configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

func TestLegacyChains(t *testing.T) {
Expand All @@ -25,3 +29,50 @@ func TestLegacyChains(t *testing.T) {
assert.Equal(t, c, got)

}

func TestChainOpts_Validate(t *testing.T) {
type fields struct {
AppConfig evm.AppConfig
EventBroadcaster pg.EventBroadcaster
MailMon *utils.MailboxMonitor
DB *sqlx.DB
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "valid",
fields: fields{
AppConfig: configtest.NewTestGeneralConfig(t),
EventBroadcaster: pg.NewNullEventBroadcaster(),
MailMon: &utils.MailboxMonitor{},
DB: pgtest.NewSqlxDB(t),
},
},
{
name: "invalid",
fields: fields{
AppConfig: nil,
EventBroadcaster: nil,
MailMon: nil,
DB: nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := evm.ChainOpts{
AppConfig: tt.fields.AppConfig,
EventBroadcaster: tt.fields.EventBroadcaster,
MailMon: tt.fields.MailMon,
DB: tt.fields.DB,
}
if err := o.Validate(); (err != nil) != tt.wantErr {
t.Errorf("ChainOpts.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
4 changes: 1 addition & 3 deletions core/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,13 @@ func (n ChainlinkAppFactory) NewApplication(ctx context.Context, cfg chainlink.G
// create the relayer-chain interoperators from application configuration
relayerFactory := chainlink.RelayerFactory{
Logger: appLggr,
DB: db,
QConfig: cfg.Database(),
LoopRegistry: loopRegistry,
GRPCOpts: grpcOpts,
}

evmFactoryCfg := chainlink.EVMFactoryConfig{
CSAETHKeystore: keyStore,
RelayerConfig: &evm.RelayerConfig{AppConfig: cfg, EventBroadcaster: eventBroadcaster, MailMon: mailMon},
ChainOpts: evm.ChainOpts{AppConfig: cfg, EventBroadcaster: eventBroadcaster, MailMon: mailMon, DB: db},
}
// evm always enabled for backward compatibility
// TODO BCF-2510 this needs to change in order to clear the path for EVM extraction
Expand Down
15 changes: 6 additions & 9 deletions core/cmd/shell_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ import (
func genTestEVMRelayers(t *testing.T, opts evm.ChainRelayExtenderConfig, ks evmrelayer.CSAETHKeystore) *chainlink.CoreRelayerChainInteroperators {
f := chainlink.RelayerFactory{
Logger: opts.Logger,
DB: opts.DB,
QConfig: opts.AppConfig.Database(),
LoopRegistry: plugins.NewLoopRegistry(opts.Logger),
}

relayers, err := chainlink.NewCoreRelayerChainInteroperators(chainlink.InitEVM(testutils.Context(t), f, chainlink.EVMFactoryConfig{
RelayerConfig: opts.RelayerConfig,
ChainOpts: opts.ChainOpts,
CSAETHKeystore: ks,
}))
if err != nil {
Expand Down Expand Up @@ -87,12 +85,12 @@ func TestShell_RunNodeWithPasswords(t *testing.T) {

opts := evm.ChainRelayExtenderConfig{
Logger: lggr,
DB: db,
KeyStore: keyStore.Eth(),
RelayerConfig: &evm.RelayerConfig{
ChainOpts: evm.ChainOpts{
AppConfig: cfg,
EventBroadcaster: pg.NewNullEventBroadcaster(),
MailMon: &utils.MailboxMonitor{},
DB: db,
},
}
testRelayers := genTestEVMRelayers(t, opts, keyStore)
Expand Down Expand Up @@ -191,13 +189,12 @@ func TestShell_RunNodeWithAPICredentialsFile(t *testing.T) {
lggr := logger.TestLogger(t)
opts := evm.ChainRelayExtenderConfig{
Logger: lggr,
DB: db,
KeyStore: keyStore.Eth(),
RelayerConfig: &evm.RelayerConfig{
ChainOpts: evm.ChainOpts{
AppConfig: cfg,
EventBroadcaster: pg.NewNullEventBroadcaster(),

MailMon: &utils.MailboxMonitor{},
MailMon: &utils.MailboxMonitor{},
DB: db,
},
}
testRelayers := genTestEVMRelayers(t, opts, keyStore)
Expand Down
4 changes: 0 additions & 4 deletions core/cmd/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,6 @@ func TestSetupSolanaRelayer(t *testing.T) {

rf := chainlink.RelayerFactory{
Logger: lggr,
DB: pgtest.NewSqlxDB(t),
QConfig: tConfig.Database(),
LoopRegistry: reg,
}

Expand Down Expand Up @@ -457,8 +455,6 @@ func TestSetupStarkNetRelayer(t *testing.T) {
})
rf := chainlink.RelayerFactory{
Logger: lggr,
DB: pgtest.NewSqlxDB(t),
QConfig: tConfig.Database(),
LoopRegistry: reg,
}

Expand Down
7 changes: 4 additions & 3 deletions core/internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,16 @@ func NewApplicationWithConfig(t testing.TB, cfg chainlink.GeneralConfig, flagsAn

relayerFactory := chainlink.RelayerFactory{
Logger: lggr,
DB: db,
QConfig: cfg.Database(),
LoopRegistry: loopRegistry,
GRPCOpts: loop.GRPCOpts{},
}

evmOpts := chainlink.EVMFactoryConfig{
RelayerConfig: &evm.RelayerConfig{
ChainOpts: evm.ChainOpts{
AppConfig: cfg,
EventBroadcaster: eventBroadcaster,
MailMon: mailMon,
DB: db,
},
CSAETHKeystore: keyStore,
}
Expand All @@ -423,6 +422,8 @@ func NewApplicationWithConfig(t testing.TB, cfg chainlink.GeneralConfig, flagsAn
Keystore: keyStore.Cosmos(),
CosmosConfigs: cfg.CosmosConfigs(),
EventBroadcaster: eventBroadcaster,
DB: db,
QConfig: cfg.Database(),
}
initOps = append(initOps, chainlink.InitCosmos(testCtx, relayerFactory, cosmosCfg))
}
Expand Down
4 changes: 2 additions & 2 deletions core/internal/testutils/evmtest/evmtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ func NewChainRelayExtOpts(t testing.TB, testopts TestChainOpts) evm.ChainRelayEx
require.NotNil(t, testopts.KeyStore)
opts := evm.ChainRelayExtenderConfig{
Logger: logger.TestLogger(t),
DB: testopts.DB,
KeyStore: testopts.KeyStore,
RelayerConfig: &evm.RelayerConfig{
ChainOpts: evm.ChainOpts{
AppConfig: testopts.GeneralConfig,
EventBroadcaster: pg.NewNullEventBroadcaster(),
MailMon: testopts.MailMon,
GasEstimator: testopts.GasEstimator,
DB: testopts.DB,
},
}
opts.GenEthClient = func(*big.Int) evmclient.Client {
Expand Down
6 changes: 3 additions & 3 deletions core/services/chainlink/relayer_chain_interoperators.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func NewCoreRelayerChainInteroperators(initFuncs ...CoreRelayerChainInitFunc) (*
srvs: make([]services.ServiceCtx, 0),
}
for _, initFn := range initFuncs {
err2 := initFn(cr)
if err2 != nil {
return nil, err2
err := initFn(cr)
if err != nil {
return nil, err
}
}
return cr, nil
Expand Down
14 changes: 9 additions & 5 deletions core/services/chainlink/relayer_chain_interoperators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ func TestCoreRelayerChainInteroperators(t *testing.T) {

factory := chainlink.RelayerFactory{
Logger: lggr,
DB: db,
QConfig: cfg.Database(),
LoopRegistry: plugins.NewLoopRegistry(lggr),
GRPCOpts: loop.GRPCOpts{},
}
Expand Down Expand Up @@ -207,10 +205,11 @@ func TestCoreRelayerChainInteroperators(t *testing.T) {
{name: "2 evm chains with 3 nodes",
initFuncs: []chainlink.CoreRelayerChainInitFunc{
chainlink.InitEVM(testctx, factory, chainlink.EVMFactoryConfig{
RelayerConfig: &evm.RelayerConfig{
ChainOpts: evm.ChainOpts{
AppConfig: cfg,
EventBroadcaster: pg.NewNullEventBroadcaster(),
MailMon: &utils.MailboxMonitor{},
DB: db,
},
CSAETHKeystore: keyStore,
}),
Expand Down Expand Up @@ -262,7 +261,9 @@ func TestCoreRelayerChainInteroperators(t *testing.T) {
chainlink.InitCosmos(testctx, factory, chainlink.CosmosFactoryConfig{
Keystore: keyStore.Cosmos(),
CosmosConfigs: cfg.CosmosConfigs(),
EventBroadcaster: pg.NewNullEventBroadcaster()}),
EventBroadcaster: pg.NewNullEventBroadcaster(),
DB: db,
QConfig: cfg.Database()}),
},
expectedCosmosChainCnt: 2,
expectedCosmosNodeCnt: 2,
Expand All @@ -279,10 +280,11 @@ func TestCoreRelayerChainInteroperators(t *testing.T) {
Keystore: keyStore.Solana(),
SolanaConfigs: cfg.SolanaConfigs()}),
chainlink.InitEVM(testctx, factory, chainlink.EVMFactoryConfig{
RelayerConfig: &evm.RelayerConfig{
ChainOpts: evm.ChainOpts{
AppConfig: cfg,
EventBroadcaster: pg.NewNullEventBroadcaster(),
MailMon: &utils.MailboxMonitor{},
DB: db,
},
CSAETHKeystore: keyStore,
}),
Expand All @@ -293,6 +295,8 @@ func TestCoreRelayerChainInteroperators(t *testing.T) {
Keystore: keyStore.Cosmos(),
CosmosConfigs: cfg.CosmosConfigs(),
EventBroadcaster: pg.NewNullEventBroadcaster(),
DB: db,
QConfig: cfg.Database(),
}),
},
expectedEVMChainCnt: 2,
Expand Down
Loading

0 comments on commit 13f4986

Please sign in to comment.