Skip to content

Commit

Permalink
simplify ChainID type
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann committed Sep 8, 2023
1 parent ae49fdd commit 6ebe26a
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 99 deletions.
2 changes: 1 addition & 1 deletion core/chains/cosmos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (cs *CosmosConfigs) SetFrom(fs *CosmosConfigs) (err error) {

func nodeStatus(n *coscfg.Node, id relay.ChainID) (relaytypes.NodeStatus, error) {
var s relaytypes.NodeStatus
s.ChainID = id.String()
s.ChainID = id
s.Name = *n.Name
b, err := toml.Marshal(n)
if err != nil {
Expand Down
18 changes: 13 additions & 5 deletions core/chains/evm/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package toml
import (
"fmt"
"net/url"
"strconv"

"github.com/ethereum/go-ethereum/core/txpool"
"github.com/pelletier/go-toml/v2"
Expand Down Expand Up @@ -115,7 +116,7 @@ func (cs EVMConfigs) Chains(ids ...relay.ChainID) (r []relaytypes.ChainStatus, t
if len(ids) > 0 {
var match bool
for _, id := range ids {
if id.String() == chainID {
if id == chainID {
match = true
break
}
Expand Down Expand Up @@ -179,7 +180,7 @@ func legacyNode(n *Node, chainID *utils.Big) (v2 types.Node) {

func nodeStatus(n *Node, chainID relay.ChainID) (relaytypes.NodeStatus, error) {
var s relaytypes.NodeStatus
s.ChainID = chainID.String()
s.ChainID = chainID
s.Name = *n.Name
b, err := toml.Marshal(n)
if err != nil {
Expand All @@ -191,16 +192,15 @@ func nodeStatus(n *Node, chainID relay.ChainID) (relaytypes.NodeStatus, error) {

func (cs EVMConfigs) nodes(id relay.ChainID) (ns EVMNodes) {
for _, c := range cs {
if c.ChainID.String() == id.String() {
if c.ChainID.String() == id {
return c.Nodes
}
}
return nil
}

func (cs EVMConfigs) Nodes(chainID relay.ChainID) (ns []types.Node, err error) {

evmID, err := chainID.Int64()
evmID, err := ChainIDInt64(chainID)
if err != nil {
return nil, fmt.Errorf("invalid evm chain id %q : %w", chainID, err)
}
Expand Down Expand Up @@ -802,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
}
6 changes: 3 additions & 3 deletions core/chains/solana/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (cs *SolanaConfigs) SetFrom(fs *SolanaConfigs) (err error) {

func nodeStatus(n *solcfg.Node, id relay.ChainID) (relaytypes.NodeStatus, error) {
var s relaytypes.NodeStatus
s.ChainID = id.String()
s.ChainID = id
s.Name = *n.Name
b, err := toml.Marshal(n)
if err != nil {
Expand Down Expand Up @@ -115,7 +115,7 @@ func setFromNode(n, f *solcfg.Node) {
func legacySolNode(n *solcfg.Node, id relay.ChainID) soldb.Node {
return soldb.Node{
Name: *n.Name,
SolanaChainID: id.String(),
SolanaChainID: id,
SolanaURL: (*url.URL)(n.URL).String(),
}
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func (c *SolanaConfig) FeeBumpPeriod() time.Duration {
func (c *SolanaConfig) ListNodes() ([]soldb.Node, error) {
var allNodes []soldb.Node
for _, n := range c.Nodes {
allNodes = append(allNodes, legacySolNode(n, relay.ChainID(*c.ChainID)))
allNodes = append(allNodes, legacySolNode(n, *c.ChainID))
}
return allNodes, nil
}
6 changes: 3 additions & 3 deletions core/chains/starknet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (cs *StarknetConfigs) SetFrom(fs *StarknetConfigs) (err error) {

func nodeStatus(n *stkcfg.Node, id relay.ChainID) (types.NodeStatus, error) {
var s types.NodeStatus
s.ChainID = id.String()
s.ChainID = id
s.Name = *n.Name
b, err := toml.Marshal(n)
if err != nil {
Expand Down Expand Up @@ -177,7 +177,7 @@ func setFromNode(n, f *stkcfg.Node) {
func legacyNode(n *stkcfg.Node, id relay.ChainID) db.Node {
return db.Node{
Name: *n.Name,
ChainID: id.String(),
ChainID: id,
URL: (*url.URL)(n.URL).String(),
}
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func (c *StarknetConfig) RequestTimeout() time.Duration {
func (c *StarknetConfig) ListNodes() ([]db.Node, error) {
var allNodes []db.Node
for _, n := range c.Nodes {
allNodes = append(allNodes, legacyNode(n, relay.ChainID(*c.ChainID)))
allNodes = append(allNodes, legacyNode(n, *c.ChainID))
}
return allNodes, nil
}
12 changes: 6 additions & 6 deletions core/internal/testutils/evmtest/evmtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,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
}
Expand Down Expand Up @@ -182,12 +182,12 @@ func (mo *TestConfigs) Chains(ids ...relay.ChainID) (cs []types.ChainStatus, cou
}
for i := range mo.EVMConfigs {
c := mo.EVMConfigs[i]
chainID := relay.ChainID(c.ChainID.String())
chainID := c.ChainID.String()
if !slices.Contains(ids, chainID) {
continue
}
c2 := types.ChainStatus{
ID: chainID.String(),
ID: chainID,
Enabled: c.IsEnabled(),
}
c2.Config, err = c.TOMLString()
Expand All @@ -207,13 +207,13 @@ func (mo *TestConfigs) Nodes(id relay.ChainID) (nodes []evmtypes.Node, err error

for i := range mo.EVMConfigs {
c := mo.EVMConfigs[i]
if id.String() == c.ChainID.String() {
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", id.String(), chains.ErrNotFound)
err = fmt.Errorf("no nodes: chain %s: %w", id, chains.ErrNotFound)
return
}

Expand Down
4 changes: 2 additions & 2 deletions core/services/chainlink/relayer_chain_interoperators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions core/services/chainlink/relayer_chain_interoperators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/services/chainlink/relayer_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -225,7 +225,7 @@ 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,
Expand Down
19 changes: 18 additions & 1 deletion core/services/job/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"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"
Expand Down Expand Up @@ -358,12 +359,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) {
Expand Down
16 changes: 8 additions & 8 deletions core/services/ocr2/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -618,7 +618,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)
}
Expand Down Expand Up @@ -686,7 +686,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)
}
Expand Down Expand Up @@ -912,7 +912,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)
}
Expand Down Expand Up @@ -1025,7 +1025,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)
}
Expand Down Expand Up @@ -1160,7 +1160,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)
}
Expand Down
Loading

0 comments on commit 6ebe26a

Please sign in to comment.