Skip to content

Commit

Permalink
BCF-2605 clean up Configs (#10551)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
krehermann authored Sep 11, 2023
1 parent f70283a commit 6c2c10d
Show file tree
Hide file tree
Showing 29 changed files with 299 additions and 790 deletions.
24 changes: 1 addition & 23 deletions core/chains/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package chains

import (
"errors"

"github.com/smartcontractkit/chainlink-relay/pkg/logger"
"github.com/smartcontractkit/chainlink-relay/pkg/types"
)

var (
Expand All @@ -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)
}
86 changes: 0 additions & 86 deletions core/chains/config_v2.go

This file was deleted.

42 changes: 17 additions & 25 deletions core/chains/cosmos/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -159,31 +151,31 @@ 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)
}
}
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
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 6c2c10d

Please sign in to comment.