From 6c2c10d8cce42a67d45babe3046d1fe6392b7eeb Mon Sep 17 00:00:00 2001 From: krehermann Date: Mon, 11 Sep 2023 12:38:19 -0600 Subject: [PATCH] BCF-2605 clean up Configs (#10551) * unify chain type * rm Configs from single chain; delete generic Configs struct and correpsonding iface * delete unneeded configs code * more dead code cleanup * add tests * rm dead code * simplify ChainID type --- core/chains/config.go | 24 +--- core/chains/config_v2.go | 86 ----------- core/chains/cosmos/chain.go | 42 +++--- core/chains/cosmos/config.go | 133 +++--------------- core/chains/cosmos/config_test.go | 71 ++++++++++ core/chains/cosmos/types/types.go | 12 -- core/chains/evm/chain.go | 32 +---- core/chains/evm/chain_test.go | 28 ---- core/chains/evm/config/toml/config.go | 47 +++++-- core/chains/evm/types/types.go | 9 +- core/chains/solana/chain.go | 23 ++- core/chains/solana/chain_test.go | 96 +++++-------- core/chains/solana/config.go | 133 ++---------------- core/chains/starknet/chain.go | 24 ++-- core/chains/starknet/config.go | 125 ++-------------- core/chains/starknet/orm.go | 12 -- core/chains/starknet/types/types.go | 12 -- core/internal/testutils/evmtest/evmtest.go | 13 +- .../chainlink/relayer_chain_interoperators.go | 4 +- .../relayer_chain_interoperators_test.go | 10 +- core/services/chainlink/relayer_factory.go | 17 +-- core/services/job/models.go | 19 ++- core/services/ocr2/delegate.go | 16 +-- core/services/relay/relay.go | 38 +---- core/services/relay/relay_test.go | 33 +---- core/web/loader/chain.go | 7 +- core/web/resolver/query.go | 15 +- plugins/cmd/chainlink-solana/main.go | 4 - plugins/cmd/chainlink-starknet/main.go | 4 - 29 files changed, 299 insertions(+), 790 deletions(-) delete mode 100644 core/chains/config_v2.go delete mode 100644 core/chains/starknet/orm.go delete mode 100644 core/chains/starknet/types/types.go diff --git a/core/chains/config.go b/core/chains/config.go index 0111521b304..3556c33a785 100644 --- a/core/chains/config.go +++ b/core/chains/config.go @@ -2,9 +2,6 @@ package chains import ( "errors" - - "github.com/smartcontractkit/chainlink-relay/pkg/logger" - "github.com/smartcontractkit/chainlink-relay/pkg/types" ) var ( @@ -13,26 +10,7 @@ var ( ErrNotFound = errors.New("not found") ) -type ChainConfigs interface { - Chains(offset, limit int, ids ...string) ([]types.ChainStatus, int, error) -} - -type NodeConfigs[I ID, N Node] interface { - Node(name string) (N, error) - Nodes(chainID I) (nodes []N, err error) - - NodeStatus(name string) (types.NodeStatus, error) -} - -// Configs holds chain and node configurations. -// TODO: BCF-2605 audit the usage of this interface and potentially remove it -type Configs[I ID, N Node] interface { - ChainConfigs - NodeConfigs[I, N] -} - // ChainOpts holds options for configuring a Chain -type ChainOpts[I ID, N Node] interface { +type ChainOpts interface { Validate() error - ConfigsAndLogger() (Configs[I, N], logger.Logger) } diff --git a/core/chains/config_v2.go b/core/chains/config_v2.go deleted file mode 100644 index 414179d8495..00000000000 --- a/core/chains/config_v2.go +++ /dev/null @@ -1,86 +0,0 @@ -package chains - -import "github.com/smartcontractkit/chainlink-relay/pkg/types" - -type configsV2AsV1[I ID, N Node] struct { - *configChains - *configNodes[I, N] -} - -type ConfigsV2[I ID, N Node] interface { - chainConfigsV2 - nodeConfigsV2[I, N] -} - -// NewConfigs returns a [Configs] backed by [ConfigsV2]. -func NewConfigs[I ID, N Node](cfgs ConfigsV2[I, N]) Configs[I, N] { - return configsV2AsV1[I, N]{ - newConfigChains[I](cfgs), - newConfigNodes[I, N](cfgs), - } -} - -// configChains is a generic, immutable Configs for chains. -type configChains struct { - v2 chainConfigsV2 -} - -type chainConfigsV2 interface { - Chains(ids ...string) ([]types.ChainStatus, error) -} - -// newConfigChains returns a chains backed by chains. -func newConfigChains[I ID](d chainConfigsV2) *configChains { - return &configChains{v2: d} -} - -func (o *configChains) Chains(offset, limit int, ids ...string) (chains []types.ChainStatus, count int, err error) { - chains, err = o.v2.Chains(ids...) - if err != nil { - return - } - count = len(chains) - if offset < len(chains) { - chains = chains[offset:] - } else { - chains = nil - } - if limit > 0 && len(chains) > limit { - chains = chains[:limit] - } - return -} - -type nodeConfigsV2[I ID, N Node] interface { - Node(name string) (N, error) - Nodes(chainID I) ([]N, error) - - NodeStatus(name string) (types.NodeStatus, error) - NodeStatuses(chainIDs ...string) (nodes []types.NodeStatus, err error) -} - -// configNodes is a generic Configs for nodes. -type configNodes[I ID, N Node] struct { - nodeConfigsV2[I, N] -} - -func newConfigNodes[I ID, N Node](d nodeConfigsV2[I, N]) *configNodes[I, N] { - return &configNodes[I, N]{d} -} - -func (o *configNodes[I, N]) NodeStatusesPaged(offset, limit int, chainIDs ...string) (nodes []types.NodeStatus, count int, err error) { - nodes, err = o.nodeConfigsV2.NodeStatuses(chainIDs...) - if err != nil { - return - } - count = len(nodes) - if offset < len(nodes) { - nodes = nodes[offset:] - } else { - nodes = nil - } - if limit > 0 && len(nodes) > limit { - nodes = nodes[:limit] - } - return -} diff --git a/core/chains/cosmos/chain.go b/core/chains/cosmos/chain.go index 48f4c2f8854..c2c7f25f843 100644 --- a/core/chains/cosmos/chain.go +++ b/core/chains/cosmos/chain.go @@ -25,9 +25,9 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains" "github.com/smartcontractkit/chainlink/v2/core/chains/cosmos/cosmostxm" - "github.com/smartcontractkit/chainlink/v2/core/chains/cosmos/types" "github.com/smartcontractkit/chainlink/v2/core/chains/internal" "github.com/smartcontractkit/chainlink/v2/core/services/pg" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/utils" ) @@ -56,7 +56,6 @@ type ChainOpts struct { DB *sqlx.DB KeyStore loop.Keystore EventBroadcaster pg.EventBroadcaster - Configs types.Configs } func (o *ChainOpts) Validate() (err error) { @@ -78,21 +77,14 @@ func (o *ChainOpts) Validate() (err error) { if o.EventBroadcaster == nil { err = multierr.Append(err, required("EventBroadcaster")) } - if o.Configs == nil { - err = multierr.Append(err, required("Configs")) - } return } -func (o *ChainOpts) ConfigsAndLogger() (chains.Configs[string, db.Node], logger.Logger) { - return o.Configs, o.Logger -} - func NewChain(cfg *CosmosConfig, opts ChainOpts) (adapters.Chain, error) { if !cfg.IsEnabled() { return nil, fmt.Errorf("cannot create new chain with ID %s, the chain is disabled", *cfg.ChainID) } - c, err := newChain(*cfg.ChainID, cfg, opts.DB, opts.KeyStore, opts.QueryConfig, opts.EventBroadcaster, opts.Configs, opts.Logger) + c, err := newChain(*cfg.ChainID, cfg, opts.DB, opts.KeyStore, opts.QueryConfig, opts.EventBroadcaster, opts.Logger) if err != nil { return nil, err } @@ -103,21 +95,17 @@ var _ adapters.Chain = (*chain)(nil) type chain struct { utils.StartStopOnce - id string - cfg *CosmosConfig - txm *cosmostxm.Txm - // TODO remove this dep after BCF-2441 - // cfs implements the loop.Relayer interface that will be removed - cfgs types.Configs + id string + cfg *CosmosConfig + txm *cosmostxm.Txm lggr logger.Logger } -func newChain(id string, cfg *CosmosConfig, db *sqlx.DB, ks loop.Keystore, logCfg pg.QConfig, eb pg.EventBroadcaster, cfgs types.Configs, lggr logger.Logger) (*chain, error) { +func newChain(id string, cfg *CosmosConfig, db *sqlx.DB, ks loop.Keystore, logCfg pg.QConfig, eb pg.EventBroadcaster, lggr logger.Logger) (*chain, error) { lggr = logger.With(lggr, "cosmosChainID", id) var ch = chain{ id: id, cfg: cfg, - cfgs: cfgs, lggr: logger.Named(lggr, "Chain"), } tc := func() (cosmosclient.ReaderWriter, error) { @@ -143,6 +131,10 @@ func (c *chain) ID() string { return c.id } +func (c *chain) ChainID() relay.ChainID { + return relay.ChainID(c.id) +} + func (c *chain) Config() coscfg.Config { return c.cfg } @@ -159,23 +151,23 @@ func (c *chain) Reader(name string) (cosmosclient.Reader, error) { func (c *chain) getClient(name string) (cosmosclient.ReaderWriter, error) { var node db.Node if name == "" { // Any node - nodes, err := c.cfgs.Nodes(c.id) + nodes, err := c.cfg.ListNodes() if err != nil { - return nil, errors.Wrap(err, "failed to get nodes") + return nil, fmt.Errorf("failed to list nodes: %w", err) } if len(nodes) == 0 { return nil, errors.New("no nodes available") } nodeIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(nodes)))) if err != nil { - return nil, errors.Wrap(err, "could not generate a random node index") + return nil, fmt.Errorf("could not generate a random node index: %w", err) } node = nodes[nodeIndex.Int64()] } else { // Named node var err error - node, err = c.cfgs.Node(name) + node, err = c.cfg.GetNode(name) if err != nil { - return nil, errors.Wrapf(err, "failed to get node named %s", name) + return nil, fmt.Errorf("failed to get node named %s: %w", name, err) } if node.CosmosChainID != c.id { return nil, fmt.Errorf("failed to create client for chain %s with node %s: wrong chain id %s", c.id, name, node.CosmosChainID) @@ -183,7 +175,7 @@ func (c *chain) getClient(name string) (cosmosclient.ReaderWriter, error) { } client, err := cosmosclient.NewClient(c.id, node.TendermintURL, DefaultRequestTimeout, logger.Named(c.lggr, "Client."+name)) if err != nil { - return nil, errors.Wrap(err, "failed to create client") + return nil, fmt.Errorf("failed to create client: %w", err) } c.lggr.Debugw("Created client", "name", node.Name, "tendermint-url", node.TendermintURL) return client, nil @@ -251,7 +243,7 @@ func (c *chain) listNodeStatuses(start, end int) ([]relaytypes.NodeStatus, int, } nodes := c.cfg.Nodes[start:end] for _, node := range nodes { - stat, err := nodeStatus(node, c.id) + stat, err := nodeStatus(node, c.ChainID()) if err != nil { return stats, total, err } diff --git a/core/chains/cosmos/config.go b/core/chains/cosmos/config.go index 878de2130b6..21a46505255 100644 --- a/core/chains/cosmos/config.go +++ b/core/chains/cosmos/config.go @@ -16,7 +16,7 @@ import ( relaytypes "github.com/smartcontractkit/chainlink-relay/pkg/types" "github.com/smartcontractkit/chainlink/v2/core/chains" - "github.com/smartcontractkit/chainlink/v2/core/chains/cosmos/types" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/utils/config" ) @@ -77,119 +77,9 @@ func (cs *CosmosConfigs) SetFrom(fs *CosmosConfigs) (err error) { return } -func (cs CosmosConfigs) Chains(ids ...string) (r []relaytypes.ChainStatus, err error) { - for _, ch := range cs { - if ch == nil { - continue - } - if len(ids) > 0 { - var match bool - for _, id := range ids { - if id == *ch.ChainID { - match = true - break - } - } - if !match { - continue - } - } - ch2 := relaytypes.ChainStatus{ - ID: *ch.ChainID, - Enabled: ch.IsEnabled(), - } - ch2.Config, err = ch.TOMLString() - if err != nil { - return - } - r = append(r, ch2) - } - return -} - -func (cs CosmosConfigs) Node(name string) (n db.Node, err error) { - for i := range cs { - for _, n := range cs[i].Nodes { - if n.Name != nil && *n.Name == name { - return legacyNode(n, *cs[i].ChainID), nil - } - } - } - err = fmt.Errorf("node %s: %w", name, chains.ErrNotFound) - return -} - -func (cs CosmosConfigs) nodes(chainID string) (ns CosmosNodes) { - for _, c := range cs { - if *c.ChainID == chainID { - return c.Nodes - } - } - return nil -} - -func (cs CosmosConfigs) Nodes(chainID string) (ns []db.Node, err error) { - nodes := cs.nodes(chainID) - if nodes == nil { - err = fmt.Errorf("no nodes: chain %s: %w", chainID, chains.ErrNotFound) - return - } - for _, n := range nodes { - if n == nil { - continue - } - ns = append(ns, legacyNode(n, chainID)) - } - return - -} - -func (cs CosmosConfigs) NodeStatus(name string) (n relaytypes.NodeStatus, err error) { - for i := range cs { - for _, n := range cs[i].Nodes { - if n.Name != nil && *n.Name == name { - return nodeStatus(n, *cs[i].ChainID) - } - } - } - err = fmt.Errorf("node %s: %w", name, chains.ErrNotFound) - return -} - -func (cs CosmosConfigs) NodeStatuses(chainIDs ...string) (ns []relaytypes.NodeStatus, err error) { - if len(chainIDs) == 0 { - for i := range cs { - for _, n := range cs[i].Nodes { - if n == nil { - continue - } - n2, err := nodeStatus(n, *cs[i].ChainID) - if err != nil { - return nil, err - } - ns = append(ns, n2) - } - } - return - } - for _, id := range chainIDs { - for _, n := range cs.nodes(id) { - if n == nil { - continue - } - n2, err := nodeStatus(n, id) - if err != nil { - return nil, err - } - ns = append(ns, n2) - } - } - return -} - -func nodeStatus(n *coscfg.Node, chainID string) (relaytypes.NodeStatus, error) { +func nodeStatus(n *coscfg.Node, id relay.ChainID) (relaytypes.NodeStatus, error) { var s relaytypes.NodeStatus - s.ChainID = chainID + s.ChainID = id s.Name = *n.Name b, err := toml.Marshal(n) if err != nil { @@ -364,6 +254,19 @@ func sdkDecFromDecimal(d *decimal.Decimal) sdk.Dec { return sdk.NewDecFromBigIntWithPrec(i.BigInt(), sdk.Precision) } -func NewConfigs(cfgs chains.ConfigsV2[string, db.Node]) types.Configs { - return chains.NewConfigs(cfgs) +func (c *CosmosConfig) GetNode(name string) (db.Node, error) { + for _, n := range c.Nodes { + if *n.Name == name { + return legacyNode(n, *c.ChainID), nil + } + } + return db.Node{}, fmt.Errorf("%w: node %q", chains.ErrNotFound, name) +} + +func (c *CosmosConfig) ListNodes() ([]db.Node, error) { + var allNodes []db.Node + for _, n := range c.Nodes { + allNodes = append(allNodes, legacyNode(n, *c.ChainID)) + } + return allNodes, nil } diff --git a/core/chains/cosmos/config_test.go b/core/chains/cosmos/config_test.go index 3446e7bcb01..54f91a13620 100644 --- a/core/chains/cosmos/config_test.go +++ b/core/chains/cosmos/config_test.go @@ -1,11 +1,16 @@ package cosmos import ( + "reflect" "testing" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/shopspring/decimal" "github.com/stretchr/testify/assert" + + coscfg "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/config" + "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/db" + "github.com/smartcontractkit/chainlink-relay/pkg/utils" ) func Test_sdkDecFromDecimal(t *testing.T) { @@ -23,3 +28,69 @@ func Test_sdkDecFromDecimal(t *testing.T) { }) } } + +func TestCosmosConfig_GetNode(t *testing.T) { + type fields struct { + ChainID *string + Nodes CosmosNodes + } + type args struct { + name string + } + tests := []struct { + name string + fields fields + args args + want db.Node + wantErr bool + }{ + { + name: "not found", + args: args{ + name: "not a node", + }, + fields: fields{Nodes: CosmosNodes{}}, + want: db.Node{}, + wantErr: true, + }, + { + name: "success", + args: args{ + name: "node", + }, + fields: fields{ + ChainID: ptr("chainID"), + Nodes: []*coscfg.Node{ + &coscfg.Node{ + Name: ptr("node"), + TendermintURL: &utils.URL{}, + }, + }}, + want: db.Node{ + CosmosChainID: "chainID", + Name: "node", + TendermintURL: "", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &CosmosConfig{ + Nodes: tt.fields.Nodes, + ChainID: tt.fields.ChainID, + } + got, err := c.GetNode(tt.args.name) + if (err != nil) != tt.wantErr { + t.Errorf("CosmosConfig.GetNode() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("CosmosConfig.GetNode() = %v, want %v", got, tt.want) + } + }) + } +} + +func ptr[T any](t T) *T { + return &t +} diff --git a/core/chains/cosmos/types/types.go b/core/chains/cosmos/types/types.go index 082ffe1b4cc..69b086a9706 100644 --- a/core/chains/cosmos/types/types.go +++ b/core/chains/cosmos/types/types.go @@ -1,17 +1,5 @@ package types -import ( - "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/db" - - "github.com/smartcontractkit/chainlink/v2/core/chains" -) - -// Configs manages cosmos chains and nodes. -type Configs interface { - chains.ChainConfigs - chains.NodeConfigs[string, db.Node] -} - // NewNode defines a new node to create. type NewNode struct { Name string `json:"name"` diff --git a/core/chains/evm/chain.go b/core/chains/evm/chain.go index 524e84fd51b..39c92252c7f 100644 --- a/core/chains/evm/chain.go +++ b/core/chains/evm/chain.go @@ -6,7 +6,6 @@ import ( "fmt" "math/big" "net/url" - "sync" "time" "go.uber.org/multierr" @@ -18,8 +17,6 @@ import ( "github.com/smartcontractkit/chainlink-relay/pkg/types" - relaytypes "github.com/smartcontractkit/chainlink-relay/pkg/types" - "github.com/smartcontractkit/chainlink/v2/core/chains" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" @@ -57,13 +54,6 @@ type Chain interface { BalanceMonitor() monitor.BalanceMonitor LogPoller() logpoller.LogPoller GasEstimator() gas.EvmFeeEstimator - - // TODO remove after BCF-2441 - // This funcs are implemented now in preparation the interface change, which is expected - // to absorb these definitions into [types.ChainService] - GetChainStatus(ctx context.Context) (relaytypes.ChainStatus, error) - ListNodeStatuses(ctx context.Context, pageSize int32, pageToken string) (stats []relaytypes.NodeStatus, nextPageToken string, total int, err error) - Transact(ctx context.Context, from, to string, amount *big.Int, balanceCheck bool) error } var ( @@ -77,7 +67,7 @@ type LegacyChains struct { *chains.ChainsKV[Chain] dflt Chain - cfgs evmtypes.Configs + cfgs toml.EVMConfigs } // LegacyChainContainer is container for EVM chains. @@ -91,6 +81,9 @@ type LegacyChainContainer interface { List(ids ...string) ([]Chain, error) Slice() []Chain + // BCF-2516: this is only used for EVMORM. When we delete that + // we can promote/move the needed funcs from it to LegacyChainContainer + // so instead of EVMORM().XYZ() we'd have something like legacyChains.XYZ() ChainNodeConfigs() evmtypes.Configs } @@ -99,7 +92,7 @@ var _ LegacyChainContainer = &LegacyChains{} func NewLegacyChains(m map[string]Chain, evmCfgs toml.EVMConfigs) *LegacyChains { return &LegacyChains{ ChainsKV: chains.NewChainsKV[Chain](m), - cfgs: chains.NewConfigs[utils.Big, evmtypes.Node](evmCfgs), + cfgs: evmCfgs, } } @@ -180,9 +173,6 @@ type RelayerConfig struct { MailMon *utils.MailboxMonitor GasEstimator gas.EvmFeeEstimator - init sync.Once - operationalConfigs evmtypes.Configs - // TODO BCF-2513 remove test code from the API // Gen-functions are useful for dependency injection by tests GenEthClient func(*big.Int) client.Client @@ -193,14 +183,6 @@ type RelayerConfig struct { GenGasEstimator func(*big.Int) gas.EvmFeeEstimator } -func (r *RelayerConfig) EVMConfigs() evmtypes.Configs { - if r.operationalConfigs == nil { - r.init.Do(func() { - r.operationalConfigs = chains.NewConfigs[utils.Big, evmtypes.Node](r.AppConfig.EVMConfigs()) - }) - } - return r.operationalConfigs -} func NewTOMLChain(ctx context.Context, chain *toml.EVMConfig, opts ChainRelayExtenderConfig) (Chain, error) { chainID := chain.ChainID l := opts.Logger.With("evmChainID", chainID.String()) @@ -500,9 +482,5 @@ func (opts *ChainRelayExtenderConfig) Check() error { return errors.New("config must be non-nil") } - opts.init.Do(func() { - opts.operationalConfigs = chains.NewConfigs[utils.Big, evmtypes.Node](opts.AppConfig.EVMConfigs()) - }) - return nil } diff --git a/core/chains/evm/chain_test.go b/core/chains/evm/chain_test.go index 3ae8a74bfe2..41f498b3e76 100644 --- a/core/chains/evm/chain_test.go +++ b/core/chains/evm/chain_test.go @@ -5,13 +5,10 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "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/services/chainlink" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) func TestLegacyChains(t *testing.T) { @@ -27,29 +24,4 @@ func TestLegacyChains(t *testing.T) { assert.NoError(t, err) assert.Equal(t, c, got) - require.NotPanics(t, func() { - l = evm.NewLegacyChains(m, nil) - assert.NotNil(t, l.ChainNodeConfigs()) - }) -} - -func TestRelayConfigInit(t *testing.T) { - appCfg := configtest.NewGeneralConfig(t, nil) - rCfg := evm.RelayerConfig{ - AppConfig: appCfg, - } - - evmCfg := rCfg.EVMConfigs() - assert.NotNil(t, evmCfg) - - // test lazy init is done only once - // note this kind of swapping should never happen in prod - appCfg2 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { - c.EVM[0].ChainID = utils.NewBig(big.NewInt(27)) - }) - rCfg.AppConfig = appCfg2 - - newEvmCfg := rCfg.EVMConfigs() - assert.NotNil(t, newEvmCfg) - assert.Equal(t, evmCfg, newEvmCfg) } diff --git a/core/chains/evm/config/toml/config.go b/core/chains/evm/config/toml/config.go index fdfce23a877..07183945194 100644 --- a/core/chains/evm/config/toml/config.go +++ b/core/chains/evm/config/toml/config.go @@ -3,6 +3,7 @@ package toml import ( "fmt" "net/url" + "strconv" "github.com/ethereum/go-ethereum/core/txpool" "github.com/pelletier/go-toml/v2" @@ -18,6 +19,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" "github.com/smartcontractkit/chainlink/v2/core/config" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/store/models" "github.com/smartcontractkit/chainlink/v2/core/utils" configutils "github.com/smartcontractkit/chainlink/v2/core/utils/config" @@ -94,7 +96,18 @@ func (cs *EVMConfigs) SetFrom(fs *EVMConfigs) (err error) { return } -func (cs EVMConfigs) Chains(ids ...string) (r []relaytypes.ChainStatus, err error) { +func (cs EVMConfigs) totalChains() int { + total := 0 + for _, ch := range cs { + if ch == nil { + continue + } + total++ + } + return total +} +func (cs EVMConfigs) Chains(ids ...relay.ChainID) (r []relaytypes.ChainStatus, total int, err error) { + total = cs.totalChains() for _, ch := range cs { if ch == nil { continue @@ -140,7 +153,7 @@ func (cs EVMConfigs) NodeStatus(name string) (relaytypes.NodeStatus, error) { for i := range cs { for _, n := range cs[i].Nodes { if n.Name != nil && *n.Name == name { - return nodeStatus(n, cs[i].ChainID.String()) + return nodeStatus(n, relay.ChainID(cs[i].ChainID.String())) } } } @@ -165,7 +178,7 @@ func legacyNode(n *Node, chainID *utils.Big) (v2 types.Node) { return } -func nodeStatus(n *Node, chainID string) (relaytypes.NodeStatus, error) { +func nodeStatus(n *Node, chainID relay.ChainID) (relaytypes.NodeStatus, error) { var s relaytypes.NodeStatus s.ChainID = chainID s.Name = *n.Name @@ -177,18 +190,21 @@ func nodeStatus(n *Node, chainID string) (relaytypes.NodeStatus, error) { return s, nil } -func (cs EVMConfigs) nodes(chainID string) (ns EVMNodes) { +func (cs EVMConfigs) nodes(id relay.ChainID) (ns EVMNodes) { for _, c := range cs { - if c.ChainID.String() == chainID { + if c.ChainID.String() == id { return c.Nodes } } return nil } -func (cs EVMConfigs) Nodes(chainID utils.Big) (ns []types.Node, err error) { - id := chainID.String() - nodes := cs.nodes(id) +func (cs EVMConfigs) Nodes(chainID relay.ChainID) (ns []types.Node, err error) { + evmID, err := ChainIDInt64(chainID) + if err != nil { + return nil, fmt.Errorf("invalid evm chain id %q : %w", chainID, err) + } + nodes := cs.nodes(chainID) if nodes == nil { err = fmt.Errorf("no nodes: chain %s: %w", &chainID, chains.ErrNotFound) return @@ -197,19 +213,20 @@ func (cs EVMConfigs) Nodes(chainID utils.Big) (ns []types.Node, err error) { if n == nil { continue } - ns = append(ns, legacyNode(n, &chainID)) + + ns = append(ns, legacyNode(n, utils.NewBigI(evmID))) } return } -func (cs EVMConfigs) NodeStatuses(chainIDs ...string) (ns []relaytypes.NodeStatus, err error) { +func (cs EVMConfigs) NodeStatuses(chainIDs ...relay.ChainID) (ns []relaytypes.NodeStatus, err error) { if len(chainIDs) == 0 { for i := range cs { for _, n := range cs[i].Nodes { if n == nil { continue } - n2, err := nodeStatus(n, cs[i].ChainID.String()) + n2, err := nodeStatus(n, relay.ChainID(cs[i].ChainID.String())) if err != nil { return nil, err } @@ -785,3 +802,11 @@ func (n *Node) SetFrom(f *Node) { n.Order = f.Order } } + +func ChainIDInt64(cid relay.ChainID) (int64, error) { + i, err := strconv.Atoi(cid) + if err != nil { + return int64(0), err + } + return int64(i), nil +} diff --git a/core/chains/evm/types/types.go b/core/chains/evm/types/types.go index dea4f9771fd..7d756485d00 100644 --- a/core/chains/evm/types/types.go +++ b/core/chains/evm/types/types.go @@ -12,13 +12,16 @@ import ( "github.com/pkg/errors" "gopkg.in/guregu/null.v4" - "github.com/smartcontractkit/chainlink/v2/core/chains" + "github.com/smartcontractkit/chainlink-relay/pkg/types" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/utils" ) type Configs interface { - chains.ChainConfigs - chains.NodeConfigs[utils.Big, Node] + Chains(ids ...relay.ChainID) ([]types.ChainStatus, int, error) + Node(name string) (Node, error) + Nodes(chainID relay.ChainID) (nodes []Node, err error) + NodeStatus(name string) (types.NodeStatus, error) } type Node struct { diff --git a/core/chains/solana/chain.go b/core/chains/solana/chain.go index b39435368ad..682fb23f9f2 100644 --- a/core/chains/solana/chain.go +++ b/core/chains/solana/chain.go @@ -30,6 +30,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/internal" "github.com/smartcontractkit/chainlink/v2/core/chains/solana/monitor" "github.com/smartcontractkit/chainlink/v2/core/services" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/utils" ) @@ -40,7 +41,6 @@ const DefaultRequestTimeout = 30 * time.Second type ChainOpts struct { Logger logger.Logger KeyStore loop.Keystore - Configs Configs } func (o *ChainOpts) Validate() (err error) { @@ -53,21 +53,18 @@ func (o *ChainOpts) Validate() (err error) { if o.KeyStore == nil { err = multierr.Append(err, required("KeyStore")) } - if o.Configs == nil { - err = multierr.Append(err, required("Configs")) - } return } -func (o *ChainOpts) ConfigsAndLogger() (chains.Configs[string, db.Node], logger.Logger) { - return o.Configs, o.Logger +func (o *ChainOpts) GetLogger() logger.Logger { + return o.Logger } func NewChain(cfg *SolanaConfig, opts ChainOpts) (solana.Chain, error) { if !cfg.IsEnabled() { return nil, fmt.Errorf("cannot create new chain with ID %s: %w", *cfg.ChainID, chains.ErrChainDisabled) } - c, err := newChain(*cfg.ChainID, cfg, opts.KeyStore, opts.Configs, opts.Logger) + c, err := newChain(*cfg.ChainID, cfg, opts.KeyStore, opts.Logger) if err != nil { return nil, err } @@ -82,7 +79,6 @@ type chain struct { cfg *SolanaConfig txm *txm.Txm balanceMonitor services.ServiceCtx - nodes func(chainID string) (nodes []db.Node, err error) lggr logger.Logger // tracking node chain id for verification @@ -213,12 +209,11 @@ func (v *verifiedCachedClient) GetAccountInfoWithOpts(ctx context.Context, addr return v.ReaderWriter.GetAccountInfoWithOpts(ctx, addr, opts) } -func newChain(id string, cfg *SolanaConfig, ks loop.Keystore, cfgs Configs, lggr logger.Logger) (*chain, error) { +func newChain(id string, cfg *SolanaConfig, ks loop.Keystore, lggr logger.Logger) (*chain, error) { lggr = logger.With(lggr, "chainID", id, "chain", "solana") var ch = chain{ id: id, cfg: cfg, - nodes: cfgs.Nodes, lggr: logger.Named(lggr, "Chain"), clientCache: map[string]*verifiedCachedClient{}, } @@ -262,7 +257,7 @@ func (c *chain) listNodeStatuses(start, end int) ([]relaytypes.NodeStatus, int, } nodes := c.cfg.Nodes[start:end] for _, node := range nodes { - stat, err := nodeStatus(node, c.id) + stat, err := nodeStatus(node, c.ChainID()) if err != nil { return stats, total, err } @@ -291,11 +286,15 @@ func (c *chain) Reader() (client.Reader, error) { return c.getClient() } +func (c *chain) ChainID() relay.ChainID { + return relay.ChainID(c.id) +} + // getClient returns a client, randomly selecting one from available and valid nodes func (c *chain) getClient() (client.ReaderWriter, error) { var node db.Node var client client.ReaderWriter - nodes, err := c.nodes(c.id) // opt: pass static nodes set to constructor + nodes, err := c.cfg.ListNodes() if err != nil { return nil, errors.Wrap(err, "failed to get nodes") } diff --git a/core/chains/solana/chain_test.go b/core/chains/solana/chain_test.go index 898b70213df..c8dfcf8501e 100644 --- a/core/chains/solana/chain_test.go +++ b/core/chains/solana/chain_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/smartcontractkit/chainlink-relay/pkg/types" + "github.com/smartcontractkit/chainlink-relay/pkg/utils" "github.com/smartcontractkit/chainlink-solana/pkg/solana/client" solcfg "github.com/smartcontractkit/chainlink-solana/pkg/solana/config" "github.com/smartcontractkit/chainlink-solana/pkg/solana/db" @@ -44,8 +44,6 @@ func TestSolanaChain_GetClient(t *testing.T) { })) defer mockServer.Close() - solORM := &mockConfigs{} - ch := solcfg.Chain{} ch.SetDefaults() cfg := &SolanaConfig{ @@ -54,68 +52,66 @@ func TestSolanaChain_GetClient(t *testing.T) { } testChain := chain{ id: "devnet", - nodes: solORM.Nodes, cfg: cfg, lggr: logger.TestLogger(t), clientCache: map[string]*verifiedCachedClient{}, } - // random nodes (happy path, all valid) - solORM.nodesForChain = []db.Node{ - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/1", + cfg.Nodes = SolanaNodes([]*solcfg.Node{ + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/1"), }, - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/2", + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/2"), }, - } + }) _, err := testChain.getClient() assert.NoError(t, err) // random nodes (happy path, 1 valid + multiple invalid) - solORM.nodesForChain = []db.Node{ - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/1", + cfg.Nodes = SolanaNodes([]*solcfg.Node{ + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/1"), }, - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/mismatch/1", + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/mismatch/1"), }, - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/mismatch/2", + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/mismatch/2"), }, - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/mismatch/3", + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/mismatch/3"), }, - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/mismatch/4", + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/mismatch/4"), }, - } + }) _, err = testChain.getClient() assert.NoError(t, err) // empty nodes response - solORM.nodesForChain = nil + cfg.Nodes = nil _, err = testChain.getClient() assert.Error(t, err) // no valid nodes to select from - solORM.nodesForChain = []db.Node{ - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/mismatch/1", + cfg.Nodes = SolanaNodes([]*solcfg.Node{ + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/mismatch/1"), }, - { - SolanaChainID: "devnet", - SolanaURL: mockServer.URL + "/mismatch/2", + &solcfg.Node{ + Name: ptr("devnet"), + URL: utils.MustParseURL(mockServer.URL + "/mismatch/2"), }, - } + }) _, err = testChain.getClient() assert.NoError(t, err) } @@ -230,28 +226,6 @@ func TestSolanaChain_VerifiedClient_ParallelClients(t *testing.T) { assert.Equal(t, testChain.clientCache[mockServer.URL], client1) } -var _ Configs = &mockConfigs{} - -type mockConfigs struct { - nodesForChain []db.Node -} - -func (m *mockConfigs) Nodes(chainID string) (nodes []db.Node, err error) { - return m.nodesForChain, nil -} - -func (m *mockConfigs) Chains(offset, limit int, ids ...string) ([]types.ChainStatus, int, error) { - panic("unimplemented") -} - -func (m *mockConfigs) Node(s string) (db.Node, error) { panic("unimplemented") } - -func (m *mockConfigs) NodeStatus(s string) (types.NodeStatus, error) { panic("unimplemented") } - -func (m *mockConfigs) NodeStatusesPaged(offset, limit int, chainIDs ...string) (nodes []types.NodeStatus, count int, err error) { - panic("unimplemented") -} - func ptr[T any](t T) *T { return &t } diff --git a/core/chains/solana/config.go b/core/chains/solana/config.go index c9d2f19d61c..b6e4a077f9e 100644 --- a/core/chains/solana/config.go +++ b/core/chains/solana/config.go @@ -15,7 +15,7 @@ import ( solcfg "github.com/smartcontractkit/chainlink-solana/pkg/solana/config" soldb "github.com/smartcontractkit/chainlink-solana/pkg/solana/db" - "github.com/smartcontractkit/chainlink/v2/core/chains" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/utils/config" ) @@ -75,116 +75,9 @@ func (cs *SolanaConfigs) SetFrom(fs *SolanaConfigs) (err error) { return } -func (cs SolanaConfigs) Chains(ids ...string) (r []relaytypes.ChainStatus, err error) { - for _, ch := range cs { - if ch == nil { - continue - } - if len(ids) > 0 { - var match bool - for _, id := range ids { - if id == *ch.ChainID { - match = true - break - } - } - if !match { - continue - } - } - ch2 := relaytypes.ChainStatus{ - ID: *ch.ChainID, - Enabled: ch.IsEnabled(), - } - ch2.Config, err = ch.TOMLString() - if err != nil { - return - } - r = append(r, ch2) - } - return -} - -func (cs SolanaConfigs) Node(name string) (soldb.Node, error) { - for i := range cs { - for _, n := range cs[i].Nodes { - if n.Name != nil && *n.Name == name { - return legacySolNode(n, *cs[i].ChainID), nil - } - } - } - return soldb.Node{}, fmt.Errorf("node %s: %w", name, chains.ErrNotFound) -} - -func (cs SolanaConfigs) nodes(chainID string) (ns SolanaNodes) { - for _, c := range cs { - if *c.ChainID == chainID { - return c.Nodes - } - } - return nil -} - -func (cs SolanaConfigs) Nodes(chainID string) (ns []soldb.Node, err error) { - nodes := cs.nodes(chainID) - if nodes == nil { - err = fmt.Errorf("no nodes: chain %s: %w", chainID, chains.ErrNotFound) - return - } - for _, n := range nodes { - if n == nil { - continue - } - ns = append(ns, legacySolNode(n, chainID)) - } - return -} - -func (cs SolanaConfigs) NodeStatus(name string) (relaytypes.NodeStatus, error) { - for i := range cs { - for _, n := range cs[i].Nodes { - if n.Name != nil && *n.Name == name { - return nodeStatus(n, *cs[i].ChainID) - } - } - } - return relaytypes.NodeStatus{}, fmt.Errorf("node %s: %w", name, chains.ErrNotFound) -} - -func (cs SolanaConfigs) NodeStatuses(chainIDs ...string) (ns []relaytypes.NodeStatus, err error) { - if len(chainIDs) == 0 { - for i := range cs { - for _, n := range cs[i].Nodes { - if n == nil { - continue - } - n2, err := nodeStatus(n, *cs[i].ChainID) - if err != nil { - return nil, err - } - ns = append(ns, n2) - } - } - return - } - for _, id := range chainIDs { - for _, n := range cs.nodes(id) { - if n == nil { - continue - } - n2, err := nodeStatus(n, id) - if err != nil { - return nil, err - } - ns = append(ns, n2) - } - } - return -} - -func nodeStatus(n *solcfg.Node, chainID string) (relaytypes.NodeStatus, error) { +func nodeStatus(n *solcfg.Node, id relay.ChainID) (relaytypes.NodeStatus, error) { var s relaytypes.NodeStatus - s.ChainID = chainID + s.ChainID = id s.Name = *n.Name b, err := toml.Marshal(n) if err != nil { @@ -219,10 +112,10 @@ func setFromNode(n, f *solcfg.Node) { } } -func legacySolNode(n *solcfg.Node, chainID string) soldb.Node { +func legacySolNode(n *solcfg.Node, id relay.ChainID) soldb.Node { return soldb.Node{ Name: *n.Name, - SolanaChainID: chainID, + SolanaChainID: id, SolanaURL: (*url.URL)(n.URL).String(), } } @@ -370,14 +263,10 @@ func (c *SolanaConfig) FeeBumpPeriod() time.Duration { return c.Chain.FeeBumpPeriod.Duration() } -// Configs manages solana chains and nodes. -type Configs interface { - chains.ChainConfigs - chains.NodeConfigs[string, soldb.Node] -} - -var _ chains.Configs[string, soldb.Node] = (Configs)(nil) - -func NewConfigs(cfgs chains.ConfigsV2[string, soldb.Node]) Configs { - return chains.NewConfigs(cfgs) +func (c *SolanaConfig) ListNodes() ([]soldb.Node, error) { + var allNodes []soldb.Node + for _, n := range c.Nodes { + allNodes = append(allNodes, legacySolNode(n, *c.ChainID)) + } + return allNodes, nil } diff --git a/core/chains/starknet/chain.go b/core/chains/starknet/chain.go index c6718c68065..fead94cda60 100644 --- a/core/chains/starknet/chain.go +++ b/core/chains/starknet/chain.go @@ -23,7 +23,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains" "github.com/smartcontractkit/chainlink/v2/core/chains/internal" - "github.com/smartcontractkit/chainlink/v2/core/chains/starknet/types" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/utils" ) @@ -31,7 +31,6 @@ type ChainOpts struct { Logger logger.Logger // the implementation used here needs to be co-ordinated with the starknet transaction manager keystore adapter KeyStore loop.Keystore - Configs types.Configs } func (o *ChainOpts) Name() string { @@ -48,23 +47,15 @@ func (o *ChainOpts) Validate() (err error) { if o.KeyStore == nil { err = multierr.Append(err, required("KeyStore")) } - if o.Configs == nil { - err = multierr.Append(err, required("Configs")) - } return } -func (o *ChainOpts) ConfigsAndLogger() (chains.Configs[string, db.Node], logger.Logger) { - return o.Configs, o.Logger -} - var _ starkChain.Chain = (*chain)(nil) type chain struct { utils.StartStopOnce id string cfg *StarknetConfig - cfgs types.Configs lggr logger.Logger txm txm.StarkTXM } @@ -73,19 +64,18 @@ func NewChain(cfg *StarknetConfig, opts ChainOpts) (starkchain.Chain, error) { if !cfg.IsEnabled() { return nil, fmt.Errorf("cannot create new chain with ID %s: %w", *cfg.ChainID, chains.ErrChainDisabled) } - c, err := newChain(*cfg.ChainID, cfg, opts.KeyStore, opts.Configs, opts.Logger) + c, err := newChain(*cfg.ChainID, cfg, opts.KeyStore, opts.Logger) if err != nil { return nil, err } return c, nil } -func newChain(id string, cfg *StarknetConfig, loopKs loop.Keystore, cfgs types.Configs, lggr logger.Logger) (*chain, error) { +func newChain(id string, cfg *StarknetConfig, loopKs loop.Keystore, lggr logger.Logger) (*chain, error) { lggr = logger.With(lggr, "starknetChainID", id) ch := &chain{ id: id, cfg: cfg, - cfgs: cfgs, lggr: logger.Named(lggr, "Chain"), } @@ -118,11 +108,15 @@ func (c *chain) Reader() (starknet.Reader, error) { return c.getClient() } +func (c *chain) ChainID() relay.ChainID { + return relay.ChainID(c.id) +} + // getClient returns a client, randomly selecting one from available and valid nodes func (c *chain) getClient() (*starknet.Client, error) { var node db.Node var client *starknet.Client - nodes, err := c.cfgs.Nodes(c.id) + nodes, err := c.cfg.ListNodes() if err != nil { return nil, errors.Wrap(err, "failed to get nodes") } @@ -215,7 +209,7 @@ func (c *chain) listNodeStatuses(start, end int) ([]relaytypes.NodeStatus, int, } nodes := c.cfg.Nodes[start:end] for _, node := range nodes { - stat, err := nodeStatus(node, c.id) + stat, err := nodeStatus(node, c.ChainID()) if err != nil { return stats, total, err } diff --git a/core/chains/starknet/config.go b/core/chains/starknet/config.go index b28d8e6a487..33b2a8d257a 100644 --- a/core/chains/starknet/config.go +++ b/core/chains/starknet/config.go @@ -14,7 +14,7 @@ import ( stkcfg "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/config" "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/db" - "github.com/smartcontractkit/chainlink/v2/core/chains" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/utils/config" ) @@ -74,118 +74,9 @@ func (cs *StarknetConfigs) SetFrom(fs *StarknetConfigs) (err error) { return } -func (cs StarknetConfigs) Chains(ids ...string) (r []types.ChainStatus, err error) { - for _, ch := range cs { - if ch == nil { - continue - } - if len(ids) > 0 { - var match bool - for _, id := range ids { - if id == *ch.ChainID { - match = true - break - } - } - if !match { - continue - } - } - ch2 := types.ChainStatus{ - ID: *ch.ChainID, - Enabled: ch.IsEnabled(), - } - ch2.Config, err = ch.TOMLString() - if err != nil { - return - } - r = append(r, ch2) - } - return -} - -func (cs StarknetConfigs) Node(name string) (n db.Node, err error) { - for i := range cs { - for _, n := range cs[i].Nodes { - if n.Name != nil && *n.Name == name { - return legacyNode(n, *cs[i].ChainID), nil - } - } - } - err = fmt.Errorf("node %s: %w", name, chains.ErrNotFound) - return -} - -func (cs StarknetConfigs) nodes(chainID string) (ns StarknetNodes) { - for _, c := range cs { - if *c.ChainID == chainID { - return c.Nodes - } - } - return nil -} - -func (cs StarknetConfigs) Nodes(chainID string) (ns []db.Node, err error) { - nodes := cs.nodes(chainID) - if nodes == nil { - err = fmt.Errorf("no nodes: chain %s: %w", chainID, chains.ErrNotFound) - return - } - for _, n := range nodes { - if n == nil { - continue - } - ns = append(ns, legacyNode(n, chainID)) - } - return -} - -func (cs StarknetConfigs) NodeStatus(name string) (n types.NodeStatus, err error) { - for i := range cs { - for _, n := range cs[i].Nodes { - if n.Name != nil && *n.Name == name { - return nodeStatus(n, *cs[i].ChainID) - } - } - } - err = fmt.Errorf("node %s: %w", name, chains.ErrNotFound) - return -} - -func (cs StarknetConfigs) NodeStatuses(chainIDs ...string) (ns []types.NodeStatus, err error) { - if len(chainIDs) == 0 { - for i := range cs { - for _, n := range cs[i].Nodes { - if n == nil { - continue - } - n2, err := nodeStatus(n, *cs[i].ChainID) - if err != nil { - return nil, err - } - ns = append(ns, n2) - } - } - return - } - for _, id := range chainIDs { - for _, n := range cs.nodes(id) { - if n == nil { - continue - } - n2, err := nodeStatus(n, id) - if err != nil { - return nil, err - } - ns = append(ns, n2) - } - } - return -} - -func nodeStatus(n *stkcfg.Node, chainID string) (types.NodeStatus, error) { +func nodeStatus(n *stkcfg.Node, id relay.ChainID) (types.NodeStatus, error) { var s types.NodeStatus - s.ChainID = chainID + s.ChainID = id s.Name = *n.Name b, err := toml.Marshal(n) if err != nil { @@ -283,7 +174,7 @@ func setFromNode(n, f *stkcfg.Node) { } } -func legacyNode(n *stkcfg.Node, id string) db.Node { +func legacyNode(n *stkcfg.Node, id relay.ChainID) db.Node { return db.Node{ Name: *n.Name, ChainID: id, @@ -312,3 +203,11 @@ func (c *StarknetConfig) OCR2CacheTTL() time.Duration { func (c *StarknetConfig) RequestTimeout() time.Duration { return c.Chain.RequestTimeout.Duration() } + +func (c *StarknetConfig) ListNodes() ([]db.Node, error) { + var allNodes []db.Node + for _, n := range c.Nodes { + allNodes = append(allNodes, legacyNode(n, *c.ChainID)) + } + return allNodes, nil +} diff --git a/core/chains/starknet/orm.go b/core/chains/starknet/orm.go deleted file mode 100644 index 9d7109d150e..00000000000 --- a/core/chains/starknet/orm.go +++ /dev/null @@ -1,12 +0,0 @@ -package starknet - -import ( - starknetdb "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/db" - - "github.com/smartcontractkit/chainlink/v2/core/chains" - "github.com/smartcontractkit/chainlink/v2/core/chains/starknet/types" -) - -func NewConfigs(cfgs chains.ConfigsV2[string, starknetdb.Node]) types.Configs { - return chains.NewConfigs(cfgs) -} diff --git a/core/chains/starknet/types/types.go b/core/chains/starknet/types/types.go deleted file mode 100644 index 2158d80fbb9..00000000000 --- a/core/chains/starknet/types/types.go +++ /dev/null @@ -1,12 +0,0 @@ -package types - -import ( - "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/db" - - "github.com/smartcontractkit/chainlink/v2/core/chains" -) - -type Configs interface { - chains.ChainConfigs - chains.NodeConfigs[string, db.Node] -} diff --git a/core/internal/testutils/evmtest/evmtest.go b/core/internal/testutils/evmtest/evmtest.go index 43502ea35ec..4532e79ac07 100644 --- a/core/internal/testutils/evmtest/evmtest.go +++ b/core/internal/testutils/evmtest/evmtest.go @@ -34,6 +34,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/keystore" "github.com/smartcontractkit/chainlink/v2/core/services/pg" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" evmrelay "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" "github.com/smartcontractkit/chainlink/v2/core/services/srvctest" "github.com/smartcontractkit/chainlink/v2/core/utils" @@ -150,9 +151,9 @@ func (mo *TestConfigs) PutChains(cs ...evmtoml.EVMConfig) { defer mo.mu.Unlock() chains: for i := range cs { - id := cs[i].ChainID.String() + id := cs[i].ChainID for j, c2 := range mo.EVMConfigs { - if c2.ChainID.String() == id { + if c2.ChainID == id { mo.EVMConfigs[j] = &cs[i] // replace continue chains } @@ -161,7 +162,7 @@ chains: } } -func (mo *TestConfigs) Chains(offset int, limit int, ids ...string) (cs []types.ChainStatus, count int, err error) { +func (mo *TestConfigs) Chains(ids ...relay.ChainID) (cs []types.ChainStatus, count int, err error) { mo.mu.RLock() defer mo.mu.RUnlock() if len(ids) == 0 { @@ -200,19 +201,19 @@ func (mo *TestConfigs) Chains(offset int, limit int, ids ...string) (cs []types. } // Nodes implements evmtypes.Configs -func (mo *TestConfigs) Nodes(chainID utils.Big) (nodes []evmtypes.Node, err error) { +func (mo *TestConfigs) Nodes(id relay.ChainID) (nodes []evmtypes.Node, err error) { mo.mu.RLock() defer mo.mu.RUnlock() for i := range mo.EVMConfigs { c := mo.EVMConfigs[i] - if chainID.Cmp(c.ChainID) == 0 { + if id == c.ChainID.String() { for _, n := range c.Nodes { nodes = append(nodes, legacyNode(n, c.ChainID)) } } } - err = fmt.Errorf("no nodes: chain %s: %w", chainID.String(), chains.ErrNotFound) + err = fmt.Errorf("no nodes: chain %s: %w", id, chains.ErrNotFound) return } diff --git a/core/services/chainlink/relayer_chain_interoperators.go b/core/services/chainlink/relayer_chain_interoperators.go index f342176f4cf..8a0b2ad001f 100644 --- a/core/services/chainlink/relayer_chain_interoperators.go +++ b/core/services/chainlink/relayer_chain_interoperators.go @@ -116,7 +116,7 @@ func InitEVM(ctx context.Context, factory RelayerFactory, config EVMFactoryConfi // adapter is a service op.srvs = append(op.srvs, a) op.loopRelayers[id] = a - legacyMap[id.ChainID.String()] = a.Chain() + legacyMap[id.ChainID] = a.Chain() if a.Default() { defaultChain = a.Chain() } @@ -143,7 +143,7 @@ func InitCosmos(ctx context.Context, factory RelayerFactory, config CosmosFactor for id, a := range adapters { op.srvs = append(op.srvs, a) op.loopRelayers[id] = a - legacyMap[id.ChainID.String()] = a.Chain() + legacyMap[id.ChainID] = a.Chain() } op.legacyChains.CosmosChains = cosmos.NewLegacyChains(legacyMap) diff --git a/core/services/chainlink/relayer_chain_interoperators_test.go b/core/services/chainlink/relayer_chain_interoperators_test.go index 29dffd1df19..5a6e3cbcfb3 100644 --- a/core/services/chainlink/relayer_chain_interoperators_test.go +++ b/core/services/chainlink/relayer_chain_interoperators_test.go @@ -412,19 +412,19 @@ func TestCoreRelayerChainInteroperators(t *testing.T) { assert.NoError(t, err) stat, err := cr.ChainStatus(testctx, wantId) assert.NoError(t, err) - assert.Equal(t, wantId.ChainID.String(), stat.ID) + assert.Equal(t, wantId.ChainID, stat.ID) // check legacy chains for evm and cosmos if wantId.Network == relay.EVM { - c, err := cr.LegacyEVMChains().Get(wantId.ChainID.String()) + c, err := cr.LegacyEVMChains().Get(wantId.ChainID) assert.NoError(t, err) assert.NotNil(t, c) - assert.Equal(t, wantId.ChainID.String(), c.ID().String()) + assert.Equal(t, wantId.ChainID, c.ID().String()) } if wantId.Network == relay.Cosmos { - c, err := cr.LegacyCosmosChains().Get(wantId.ChainID.String()) + c, err := cr.LegacyCosmosChains().Get(wantId.ChainID) assert.NoError(t, err) assert.NotNil(t, c) - assert.Equal(t, wantId.ChainID.String(), c.ID()) + assert.Equal(t, wantId.ChainID, c.ID()) } } } diff --git a/core/services/chainlink/relayer_factory.go b/core/services/chainlink/relayer_factory.go index df7de835052..0a0d653f5fc 100644 --- a/core/services/chainlink/relayer_factory.go +++ b/core/services/chainlink/relayer_factory.go @@ -58,7 +58,7 @@ func (r *RelayerFactory) NewEVM(ctx context.Context, config EVMFactoryConfig) (m legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(evmRelayExtenders) for _, ext := range evmRelayExtenders.Slice() { relayID := relay.ID{Network: relay.EVM, ChainID: relay.ChainID(ext.Chain().ID().String())} - chain, err := legacyChains.Get(relayID.ChainID.String()) + chain, err := legacyChains.Get(relayID.ChainID) if err != nil { return nil, err } @@ -98,10 +98,6 @@ func (r *RelayerFactory) NewSolana(ks keystore.Solana, chainCfgs solana.SolanaCo continue } - // all the lower level APIs expect a config slice. create a single valued set per id - // TODO BCF-2605: clean this up - singleChainCfg := solana.SolanaConfigs{chainCfg} - if cmdName := env.SolanaPluginCmd.Get(); cmdName != "" { // setup the solana relayer to be a LOOP @@ -128,7 +124,6 @@ func (r *RelayerFactory) NewSolana(ks keystore.Solana, chainCfgs solana.SolanaCo opts := solana.ChainOpts{ Logger: solLggr, KeyStore: signer, - Configs: solana.NewConfigs(singleChainCfg), } chain, err := solana.NewChain(chainCfg, opts) @@ -172,10 +167,6 @@ func (r *RelayerFactory) NewStarkNet(ks keystore.StarkNet, chainCfgs starknet.St continue } - // all the lower level APIs expect a config slice. create a single valued set per id - // TODO BCF-2605: clean this up - singleChainCfg := starknet.StarknetConfigs{chainCfg} - if cmdName := env.StarknetPluginCmd.Get(); cmdName != "" { // setup the starknet relayer to be a LOOP cfgTOML, err := toml.Marshal(struct { @@ -200,7 +191,6 @@ func (r *RelayerFactory) NewStarkNet(ks keystore.StarkNet, chainCfgs starknet.St opts := starknet.ChainOpts{ Logger: starkLggr, KeyStore: loopKs, - Configs: starknet.NewConfigs(singleChainCfg), } chain, err := starknet.NewChain(chainCfg, opts) @@ -235,14 +225,13 @@ func (r *RelayerFactory) NewCosmos(ctx context.Context, config CosmosFactoryConf opts := cosmos.ChainOpts{ QueryConfig: r.QConfig, - Logger: lggr.Named(relayId.ChainID.String()), + Logger: lggr.Named(relayId.ChainID), DB: r.DB, KeyStore: loopKs, EventBroadcaster: config.EventBroadcaster, } - opts.Configs = cosmos.NewConfigs(cosmos.CosmosConfigs{chainCfg}) - chain, err := cosmos.NewChain(chainCfg, opts) + chain, err := cosmos.NewChain(chainCfg, opts) if err != nil { return nil, fmt.Errorf("failed to load Cosmos chain %q: %w", relayId, err) } diff --git a/core/services/job/models.go b/core/services/job/models.go index 8f9a2f529d3..03015aa1a7b 100644 --- a/core/services/job/models.go +++ b/core/services/job/models.go @@ -17,6 +17,7 @@ import ( "github.com/smartcontractkit/chainlink-relay/pkg/types" "github.com/smartcontractkit/chainlink/v2/core/assets" "github.com/smartcontractkit/chainlink/v2/core/bridges" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml" clnull "github.com/smartcontractkit/chainlink/v2/core/null" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey" "github.com/smartcontractkit/chainlink/v2/core/services/pipeline" @@ -338,12 +339,28 @@ type OCR2OracleSpec struct { CaptureAutomationCustomTelemetry bool `toml:"captureAutomationCustomTelemetry"` } +func validateRelayID(id relay.ID) error { + // only the EVM has specific requirements + if id.Network == relay.EVM { + _, err := toml.ChainIDInt64(id.ChainID) + if err != nil { + return fmt.Errorf("invalid EVM chain id %s: %w", id.ChainID, err) + } + } + return nil +} + func (s *OCR2OracleSpec) RelayID() (relay.ID, error) { cid, err := s.getChainID() if err != nil { return relay.ID{}, err } - return relay.NewID(s.Relay, cid) + rid := relay.NewID(s.Relay, cid) + err = validateRelayID(rid) + if err != nil { + return relay.ID{}, err + } + return rid, nil } func (s *OCR2OracleSpec) getChainID() (relay.ChainID, error) { diff --git a/core/services/ocr2/delegate.go b/core/services/ocr2/delegate.go index 6c7924e2fef..2d1ff41ac12 100644 --- a/core/services/ocr2/delegate.go +++ b/core/services/ocr2/delegate.go @@ -265,7 +265,7 @@ func (d *Delegate) cleanupEVM(jb job.Job, q pg.Queryer, relayID relay.ID) error // an inconsistent state. This assumes UnregisterFilter will return nil if the filter wasn't found // at all (no rows deleted). spec := jb.OCR2OracleSpec - chain, err := d.legacyChains.Get(relayID.ChainID.String()) + chain, err := d.legacyChains.Get(relayID.ChainID) if err != nil { d.lggr.Error("cleanupEVM: failed to chain get chain %s", "err", relayID.ChainID, err) return nil @@ -345,7 +345,7 @@ func (d *Delegate) ServicesForSpec(jb job.Job, qopts ...pg.QOpt) ([]job.ServiceC if rid.Network == relay.EVM { lggr = logger.Sugared(lggr.With("evmChainID", rid.ChainID)) - chain, err2 := d.legacyChains.Get(rid.ChainID.String()) + chain, err2 := d.legacyChains.Get(rid.ChainID) if err2 != nil { return nil, fmt.Errorf("ServicesForSpec: could not get EVM chain %s: %w", rid.ChainID, err2) } @@ -509,7 +509,7 @@ func (d *Delegate) newServicesMercury( if err != nil { return nil, fmt.Errorf("failed to get relay %s is it enabled?: %w", spec.Relay, err) } - chain, err := d.legacyChains.Get(rid.ChainID.String()) + chain, err := d.legacyChains.Get(rid.ChainID) if err != nil { return nil, fmt.Errorf("mercury services: failed to get chain %s: %w", rid.ChainID, err) } @@ -624,7 +624,7 @@ func (d *Delegate) newServicesDKG( return nil, fmt.Errorf("DKG services: expected EVM relayer got %s", rid.Network) } - chain, err2 := d.legacyChains.Get(rid.ChainID.String()) + chain, err2 := d.legacyChains.Get(rid.ChainID) if err2 != nil { return nil, fmt.Errorf("DKG services: failed to get chain %s: %w", rid.ChainID, err2) } @@ -692,7 +692,7 @@ func (d *Delegate) newServicesOCR2VRF( if rid.Network != relay.EVM { return nil, fmt.Errorf("VRF services: expected EVM relayer got %s", rid.Network) } - chain, err2 := d.legacyChains.Get(rid.ChainID.String()) + chain, err2 := d.legacyChains.Get(rid.ChainID) if err2 != nil { return nil, fmt.Errorf("VRF services: failed to get chain (%s): %w", rid.ChainID, err2) } @@ -918,7 +918,7 @@ func (d *Delegate) newServicesOCR2Keepers21( return nil, fmt.Errorf("keeper2 services: expected EVM relayer got %s", rid.Network) } - chain, err2 := d.legacyChains.Get(rid.ChainID.String()) + chain, err2 := d.legacyChains.Get(rid.ChainID) if err2 != nil { return nil, fmt.Errorf("keeper2 services: failed to get chain %s: %w", rid.ChainID, err2) } @@ -1031,7 +1031,7 @@ func (d *Delegate) newServicesOCR2Keepers20( if rid.Network != relay.EVM { return nil, fmt.Errorf("keepers2.0 services: expected EVM relayer got %s", rid.Network) } - chain, err2 := d.legacyChains.Get(rid.ChainID.String()) + chain, err2 := d.legacyChains.Get(rid.ChainID) if err2 != nil { return nil, fmt.Errorf("keepers2.0 services: failed to get chain (%s): %w", rid.ChainID, err2) } @@ -1166,7 +1166,7 @@ func (d *Delegate) newServicesOCR2Functions( if rid.Network != relay.EVM { return nil, fmt.Errorf("functions services: expected EVM relayer got %s", rid.Network) } - chain, err := d.legacyChains.Get(rid.ChainID.String()) + chain, err := d.legacyChains.Get(rid.ChainID) if err != nil { return nil, fmt.Errorf("functions services: failed to get chain %s: %w", rid.ChainID, err) } diff --git a/core/services/relay/relay.go b/core/services/relay/relay.go index 5e8f065f289..c96abb4b8c8 100644 --- a/core/services/relay/relay.go +++ b/core/services/relay/relay.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "regexp" - "strconv" "golang.org/x/exp/maps" @@ -14,7 +13,8 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services" ) -type Network string +type Network = string +type ChainID = string var ( EVM Network = "evm" @@ -36,29 +36,14 @@ type ID struct { } func (i *ID) Name() string { - return fmt.Sprintf("%s.%s", i.Network, i.ChainID.String()) + return fmt.Sprintf("%s.%s", i.Network, i.ChainID) } func (i *ID) String() string { return i.Name() } -func NewID(n Network, c ChainID) (ID, error) { - id := ID{Network: n, ChainID: c} - err := id.validate() - if err != nil { - return ID{}, err - } - return id, nil -} -func (i *ID) validate() error { - // the only validation is to ensure that EVM chain ids are compatible with int64 - if i.Network == EVM { - _, err := i.ChainID.Int64() - if err != nil { - return fmt.Errorf("RelayIdentifier invalid: EVM relayer must have integer-compatible chain ID: %w", err) - } - } - return nil +func NewID(n Network, c ChainID) ID { + return ID{Network: n, ChainID: c} } var idRegex = regexp.MustCompile( @@ -88,19 +73,6 @@ func (i *ID) UnmarshalString(s string) error { return nil } -type ChainID string - -func (c ChainID) String() string { - return string(c) -} -func (c ChainID) Int64() (int64, error) { - i, err := strconv.Atoi(c.String()) - if err != nil { - return int64(0), err - } - return int64(i), nil -} - // RelayerExt is a subset of [loop.Relayer] for adapting [types.Relayer], typically with a Chain. See [relayerAdapter]. type RelayerExt interface { types.ChainService diff --git a/core/services/relay/relay_test.go b/core/services/relay/relay_test.go index 1180379677e..28ee0172c20 100644 --- a/core/services/relay/relay_test.go +++ b/core/services/relay/relay_test.go @@ -51,36 +51,9 @@ func TestIdentifier_UnmarshalString(t *testing.T) { } func TestNewID(t *testing.T) { - type args struct { - n Network - c ChainID - } - tests := []struct { - name string - args args - want ID - wantErr bool - }{ - {name: "good evm", - args: args{n: EVM, c: "1"}, - want: ID{Network: EVM, ChainID: "1"}, - }, - {name: "bad evm", - args: args{n: EVM, c: "not a number"}, - want: ID{}, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := NewID(tt.args.n, tt.args.c) - if (err != nil) != tt.wantErr { - t.Errorf("NewID() error = %v, wantErr %v", err, tt.wantErr) - return - } - assert.Equal(t, tt.want, got, "got id %v", got) - }) - } + rid := NewID(EVM, "chain id") + assert.Equal(t, EVM, rid.Network) + assert.Equal(t, "chain id", rid.ChainID) } type staticMedianProvider struct { diff --git a/core/web/loader/chain.go b/core/web/loader/chain.go index 77e231ace13..c91c2f02a3b 100644 --- a/core/web/loader/chain.go +++ b/core/web/loader/chain.go @@ -7,6 +7,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" ) type chainBatcher struct { @@ -17,14 +18,14 @@ func (b *chainBatcher) loadByIDs(_ context.Context, keys dataloader.Keys) []*dat // Create a map for remembering the order of keys passed in keyOrder := make(map[string]int, len(keys)) // Collect the keys to search for - var chainIDs []string + var chainIDs []relay.ChainID for ix, key := range keys { - chainIDs = append(chainIDs, key.String()) + chainIDs = append(chainIDs, relay.ChainID(key.String())) keyOrder[key.String()] = ix } // Fetch the chains - cs, _, err := b.app.EVMORM().Chains(0, -1, chainIDs...) + cs, _, err := b.app.EVMORM().Chains(chainIDs...) if err != nil { return []*dataloader.Result{{Data: nil, Error: err}} } diff --git a/core/web/resolver/query.go b/core/web/resolver/query.go index 22b95a2d2ef..e9fd18cf19a 100644 --- a/core/web/resolver/query.go +++ b/core/web/resolver/query.go @@ -15,6 +15,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains" "github.com/smartcontractkit/chainlink/v2/core/services/keystore" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/vrfkey" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" evmrelay "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" "github.com/smartcontractkit/chainlink/v2/core/utils/stringutils" ) @@ -68,7 +69,7 @@ func (r *Resolver) Chain(ctx context.Context, args struct{ ID graphql.ID }) (*Ch return nil, err } - cs, _, err := r.App.EVMORM().Chains(0, -1, string(args.ID)) + cs, _, err := r.App.EVMORM().Chains(relay.ChainID(args.ID)) if err != nil { return nil, err } @@ -94,12 +95,20 @@ func (r *Resolver) Chains(ctx context.Context, args struct { offset := pageOffset(args.Offset) limit := pageLimit(args.Limit) - page, count, err := r.App.EVMORM().Chains(offset, limit) + chains, count, err := r.App.EVMORM().Chains() if err != nil { return nil, err } + // bound the chain results + if offset >= len(chains) { + return nil, fmt.Errorf("offset %d out of range", offset) + } + end := len(chains) + if limit > 0 && offset+limit < end { + end = offset + limit + } - return NewChainsPayload(page, int32(count)), nil + return NewChainsPayload(chains[offset:end], int32(count)), nil } // FeedsManager retrieves a feeds manager by id. diff --git a/plugins/cmd/chainlink-solana/main.go b/plugins/cmd/chainlink-solana/main.go index 9f470355297..df2824fb338 100644 --- a/plugins/cmd/chainlink-solana/main.go +++ b/plugins/cmd/chainlink-solana/main.go @@ -63,13 +63,9 @@ func (c *pluginRelayer) NewRelayer(ctx context.Context, config string, keystore return nil, fmt.Errorf("failed to decode config toml: %w:\n\t%s", err, config) } - // TODO BCF-2605 clean this up when the internal details of Solana Chain construction - // doesn't need `Configs` - cfgAdapter := solana.SolanaConfigs{&cfg.Solana} opts := solana.ChainOpts{ Logger: c.Logger, KeyStore: keystore, - Configs: solana.NewConfigs(cfgAdapter), } chain, err := solana.NewChain(&cfg.Solana, opts) if err != nil { diff --git a/plugins/cmd/chainlink-starknet/main.go b/plugins/cmd/chainlink-starknet/main.go index 433d4408e31..5015f70be2e 100644 --- a/plugins/cmd/chainlink-starknet/main.go +++ b/plugins/cmd/chainlink-starknet/main.go @@ -66,13 +66,9 @@ func (c *pluginRelayer) NewRelayer(ctx context.Context, config string, loopKs lo return nil, fmt.Errorf("failed to decode config toml: %w:\n\t%s", err, config) } - // TODO BCF-2605 clean this up when the internal details of Chain construction - // doesn't need `Configs` - cfgAdapter := starknet.StarknetConfigs{&cfg.Starknet} opts := starknet.ChainOpts{ Logger: c.Logger, KeyStore: loopKs, - Configs: starknet.NewConfigs(cfgAdapter), } chain, err := starknet.NewChain(&cfg.Starknet, opts)