Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BCF-2605 clean up Configs #10551

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading