Skip to content

Commit

Permalink
move config.go -> config/toml.go
Browse files Browse the repository at this point in the history
  • Loading branch information
aalu1418 committed May 17, 2024
1 parent 3258424 commit ecb23e9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 84 deletions.
2 changes: 1 addition & 1 deletion pkg/solana/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,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.ChainID())
stat, err := config.NodeStatus(node, c.ChainID())
if err != nil {
return stats, total, err
}
Expand Down
83 changes: 0 additions & 83 deletions pkg/solana/config.go

This file was deleted.

71 changes: 71 additions & 0 deletions pkg/solana/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,86 @@ package config

import (
"errors"
"fmt"
"net/url"
"time"

"github.com/gagliardetto/solana-go/rpc"
"github.com/pelletier/go-toml/v2"
"golang.org/x/exp/slices"

"github.com/smartcontractkit/chainlink-common/pkg/config"
relaytypes "github.com/smartcontractkit/chainlink-common/pkg/types"
)

type TOMLConfigs []*TOMLConfig

func (cs TOMLConfigs) ValidateConfig() (err error) {
return cs.validateKeys()
}

func (cs TOMLConfigs) validateKeys() (err error) {
// Unique chain IDs
chainIDs := config.UniqueStrings{}
for i, c := range cs {
if chainIDs.IsDupe(c.ChainID) {
err = errors.Join(err, config.NewErrDuplicate(fmt.Sprintf("%d.ChainID", i), *c.ChainID))
}
}

// Unique node names
names := config.UniqueStrings{}
for i, c := range cs {
for j, n := range c.Nodes {
if names.IsDupe(n.Name) {
err = errors.Join(err, config.NewErrDuplicate(fmt.Sprintf("%d.Nodes.%d.Name", i, j), *n.Name))
}
}
}

// Unique URLs
urls := config.UniqueStrings{}
for i, c := range cs {
for j, n := range c.Nodes {
u := (*url.URL)(n.URL)
if urls.IsDupeFmt(u) {
err = errors.Join(err, config.NewErrDuplicate(fmt.Sprintf("%d.Nodes.%d.URL", i, j), u.String()))
}
}
}
return
}

func (cs *TOMLConfigs) SetFrom(fs *TOMLConfigs) (err error) {
if err1 := fs.validateKeys(); err1 != nil {
return err1
}
for _, f := range *fs {
if f.ChainID == nil {
*cs = append(*cs, f)
} else if i := slices.IndexFunc(*cs, func(c *TOMLConfig) bool {
return c.ChainID != nil && *c.ChainID == *f.ChainID
}); i == -1 {
*cs = append(*cs, f)
} else {
(*cs)[i].SetFrom(f)
}
}
return
}

func NodeStatus(n *Node, id string) (relaytypes.NodeStatus, error) {
var s relaytypes.NodeStatus
s.ChainID = id
s.Name = *n.Name
b, err := toml.Marshal(n)
if err != nil {
return relaytypes.NodeStatus{}, err
}
s.Config = string(b)
return s, nil
}

type SolanaNodes []*Node

func (ns *SolanaNodes) SetFrom(fs *SolanaNodes) {
Expand Down

0 comments on commit ecb23e9

Please sign in to comment.