Skip to content

Commit

Permalink
remove db config
Browse files Browse the repository at this point in the history
  • Loading branch information
aalu1418 committed May 17, 2024
1 parent 1549341 commit eacd0fe
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 464 deletions.
7 changes: 3 additions & 4 deletions pkg/solana/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/db"
)

var mockTransmission = []byte{
Expand Down Expand Up @@ -94,7 +93,7 @@ func testTransmissionsResponse(t *testing.T, body []byte, sub uint64) []byte {

func testSetupReader(t *testing.T, endpoint string) client.Reader {
lggr := logger.Test(t)
cfg := config.NewConfig(db.ChainCfg{}, lggr)
cfg := config.NewDefault()
client, err := client.NewClient(endpoint, cfg, 1*time.Second, lggr)
require.NoError(t, err)
return client
Expand Down Expand Up @@ -170,7 +169,7 @@ func TestCache(t *testing.T) {
lggr := logger.Test(t)
stateCache := StateCache{
StateID: solana.MustPublicKeyFromBase58("11111111111111111111111111111111"),
cfg: config.NewConfig(db.ChainCfg{}, lggr),
cfg: config.NewDefault(),
reader: testSetupReader(t, mockServer.URL),
lggr: lggr,
}
Expand All @@ -182,7 +181,7 @@ func TestCache(t *testing.T) {

transmissionsCache := TransmissionsCache{
TransmissionsID: solana.MustPublicKeyFromBase58("11111111111111111111111111111112"),
cfg: config.NewConfig(db.ChainCfg{}, lggr),
cfg: config.NewDefault(),
reader: testSetupReader(t, mockServer.URL),
lggr: lggr,
}
Expand Down
33 changes: 19 additions & 14 deletions pkg/solana/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/db"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/monitor"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/txm"
)
Expand Down Expand Up @@ -65,7 +64,7 @@ func (o *ChainOpts) GetLogger() logger.Logger {
return o.Logger
}

func NewChain(cfg *TOMLConfig, opts ChainOpts) (Chain, error) {
func NewChain(cfg *config.TOMLConfig, opts ChainOpts) (Chain, error) {
if !cfg.IsEnabled() {
return nil, fmt.Errorf("cannot create new chain with ID %s: chain is disabled", *cfg.ChainID)
}
Expand All @@ -81,7 +80,7 @@ var _ Chain = (*chain)(nil)
type chain struct {
services.StateMachine
id string
cfg *TOMLConfig
cfg *config.TOMLConfig
txm *txm.Txm
balanceMonitor services.Service
lggr logger.Logger
Expand Down Expand Up @@ -214,7 +213,7 @@ func (v *verifiedCachedClient) GetAccountInfoWithOpts(ctx context.Context, addr
return v.ReaderWriter.GetAccountInfoWithOpts(ctx, addr, opts)
}

func newChain(id string, cfg *TOMLConfig, ks loop.Keystore, lggr logger.Logger) (*chain, error) {
func newChain(id string, cfg *config.TOMLConfig, ks loop.Keystore, lggr logger.Logger) (*chain, error) {
lggr = logger.With(lggr, "chainID", id, "chain", "solana")
var ch = chain{
id: id,
Expand Down Expand Up @@ -297,12 +296,9 @@ func (c *chain) ChainID() string {

// getClient returns a client, randomly selecting one from available and valid nodes
func (c *chain) getClient() (client.ReaderWriter, error) {
var node db.Node
var node *config.Node
var client client.ReaderWriter
nodes, err := c.cfg.ListNodes()
if err != nil {
return nil, fmt.Errorf("failed to get nodes: %w", err)
}
nodes := c.cfg.ListNodes()
if len(nodes) == 0 {
return nil, errors.New("no nodes available")
}
Expand All @@ -311,10 +307,11 @@ func (c *chain) getClient() (client.ReaderWriter, error) {
for _, i := range index {
node = nodes[i]
// create client and check
var err error
client, err = c.verifiedClient(node)
// if error, try another node
if err != nil {
c.lggr.Warnw("failed to create node", "name", node.Name, "solana-url", node.SolanaURL, "err", err.Error())
c.lggr.Warnw("failed to create node", "name", node.Name, "solana-url", node.URL, "err", err.Error())
continue
}
// if all checks passed, mark found and break loop
Expand All @@ -324,15 +321,23 @@ func (c *chain) getClient() (client.ReaderWriter, error) {
if client == nil {
return nil, errors.New("no node valid nodes available")
}
c.lggr.Debugw("Created client", "name", node.Name, "solana-url", node.SolanaURL)
c.lggr.Debugw("Created client", "name", node.Name, "solana-url", node.URL)
return client, nil
}

// verifiedClient returns a client for node or an error if fails to create the client.
// The client will still be returned if the nodes are not valid, or the chain id doesn't match.
// Further client calls will try and verify the client, and fail if the client is still not valid.
func (c *chain) verifiedClient(node db.Node) (client.ReaderWriter, error) {
url := node.SolanaURL
func (c *chain) verifiedClient(node *config.Node) (client.ReaderWriter, error) {
if node == nil {
return nil, fmt.Errorf("nil node")
}

if node.Name == nil || node.URL == nil {
return nil, fmt.Errorf("node config contains nil: %+v", node)
}

url := node.URL.String()
var err error

// check if cached client exists
Expand All @@ -346,7 +351,7 @@ func (c *chain) verifiedClient(node db.Node) (client.ReaderWriter, error) {
expectedChainID: c.id,
}
// create client
cl.ReaderWriter, err = client.NewClient(url, c.cfg, DefaultRequestTimeout, logger.Named(c.lggr, "Client."+node.Name))
cl.ReaderWriter, err = client.NewClient(url, c.cfg, DefaultRequestTimeout, logger.Named(c.lggr, "Client."+*node.Name))
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
}
Expand Down
28 changes: 18 additions & 10 deletions pkg/solana/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -18,7 +19,6 @@ import (

"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"
)

const TestSolanaGenesisHashTemplate = `{"jsonrpc":"2.0","result":"%s","id":1}`
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestSolanaChain_GetClient(t *testing.T) {

ch := solcfg.Chain{}
ch.SetDefaults()
cfg := &TOMLConfig{
cfg := &solcfg.TOMLConfig{
ChainID: ptr("devnet"),
Chain: ch,
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestSolanaChain_VerifiedClient(t *testing.T) {

ch := solcfg.Chain{}
ch.SetDefaults()
cfg := &TOMLConfig{
cfg := &solcfg.TOMLConfig{
ChainID: ptr("devnet"),
Chain: ch,
}
Expand All @@ -152,28 +152,32 @@ func TestSolanaChain_VerifiedClient(t *testing.T) {
lggr: logger.Test(t),
clientCache: map[string]*verifiedCachedClient{},
}
node := db.Node{SolanaURL: mockServer.URL}
nName := t.Name() + "-" + uuid.NewString()
node := &solcfg.Node{
Name: &nName,
URL: config.MustParseURL(mockServer.URL),
}

// happy path
testChain.id = "devnet"
_, err := testChain.verifiedClient(node)
assert.NoError(t, err)
require.NoError(t, err)

// retrieve cached client and retrieve slot height
c, err := testChain.verifiedClient(node)
assert.NoError(t, err)
require.NoError(t, err)
slot, err := c.SlotHeight()
assert.NoError(t, err)
assert.Equal(t, uint64(1234), slot)

node.SolanaURL = mockServer.URL + "/mismatch"
node.URL = config.MustParseURL(mockServer.URL + "/mismatch")
testChain.id = "incorrect"
c, err = testChain.verifiedClient(node)
assert.NoError(t, err)
_, err = c.ChainID()
// expect error from id mismatch (even if using a cached client) when performing RPC calls
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("client returned mismatched chain id (expected: %s, got: %s): %s", "incorrect", "devnet", node.SolanaURL), err.Error())
assert.Equal(t, fmt.Sprintf("client returned mismatched chain id (expected: %s, got: %s): %s", "incorrect", "devnet", node.URL), err.Error())
}

func TestSolanaChain_VerifiedClient_ParallelClients(t *testing.T) {
Expand All @@ -186,7 +190,7 @@ func TestSolanaChain_VerifiedClient_ParallelClients(t *testing.T) {

ch := solcfg.Chain{}
ch.SetDefaults()
cfg := &TOMLConfig{
cfg := &solcfg.TOMLConfig{
ChainID: ptr("devnet"),
Enabled: ptr(true),
Chain: ch,
Expand All @@ -197,7 +201,11 @@ func TestSolanaChain_VerifiedClient_ParallelClients(t *testing.T) {
lggr: logger.Test(t),
clientCache: map[string]*verifiedCachedClient{},
}
node := db.Node{SolanaURL: mockServer.URL}
nName := t.Name() + "-" + uuid.NewString()
node := &solcfg.Node{
Name: &nName,
URL: config.MustParseURL(mockServer.URL),
}

var wg sync.WaitGroup
wg.Add(2)
Expand Down
9 changes: 4 additions & 5 deletions pkg/solana/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/logger"

"github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/db"
)

func TestClient_Reader_Integration(t *testing.T) {
Expand All @@ -31,7 +30,7 @@ func TestClient_Reader_Integration(t *testing.T) {

requestTimeout := 5 * time.Second
lggr := logger.Test(t)
cfg := config.NewConfig(db.ChainCfg{}, lggr)
cfg := config.NewDefault()

c, err := NewClient(url, cfg, requestTimeout, lggr)
require.NoError(t, err)
Expand Down Expand Up @@ -105,7 +104,7 @@ func TestClient_Reader_ChainID(t *testing.T) {

requestTimeout := 5 * time.Second
lggr := logger.Test(t)
cfg := config.NewConfig(db.ChainCfg{}, lggr)
cfg := config.NewDefault()
c, err := NewClient(mockServer.URL, cfg, requestTimeout, lggr)
require.NoError(t, err)

Expand All @@ -126,7 +125,7 @@ func TestClient_Writer_Integration(t *testing.T) {

requestTimeout := 5 * time.Second
lggr := logger.Test(t)
cfg := config.NewConfig(db.ChainCfg{}, lggr)
cfg := config.NewDefault()

ctx := context.Background()
c, err := NewClient(url, cfg, requestTimeout, lggr)
Expand Down Expand Up @@ -212,7 +211,7 @@ func TestClient_SendTxDuplicates_Integration(t *testing.T) {
// create client
requestTimeout := 5 * time.Second
lggr := logger.Test(t)
cfg := config.NewConfig(db.ChainCfg{}, lggr)
cfg := config.NewDefault()
c, err := NewClient(url, cfg, requestTimeout, lggr)
require.NoError(t, err)

Expand Down
3 changes: 1 addition & 2 deletions pkg/solana/client/test_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/logger"

"github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/db"
)

func TestSetupLocalSolNode_SimultaneousNetworks(t *testing.T) {
Expand All @@ -25,7 +24,7 @@ func TestSetupLocalSolNode_SimultaneousNetworks(t *testing.T) {
// client configs
requestTimeout := 5 * time.Second
lggr := logger.Test(t)
cfg := config.NewConfig(db.ChainCfg{}, lggr)
cfg := config.NewDefault()

// check & fund address
checkFunded := func(t *testing.T, url string) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/solana/cmd/chainlink-solana/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/loop"

"github.com/smartcontractkit/chainlink-solana/pkg/solana"
solcfg "github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
)

const (
Expand Down Expand Up @@ -53,7 +54,7 @@ func (c *pluginRelayer) NewRelayer(ctx context.Context, config string, keystore
d := toml.NewDecoder(strings.NewReader(config))
d.DisallowUnknownFields()
var cfg struct {
Solana solana.TOMLConfig
Solana solcfg.TOMLConfig
}

if err := d.Decode(&cfg); err != nil {
Expand Down
Loading

0 comments on commit eacd0fe

Please sign in to comment.