Skip to content

Commit

Permalink
Add DataSource to ChainOpts and pass down to tx mgr from NewChain
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Nov 6, 2024
1 parent b49b2be commit 7bcc5ae
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
12 changes: 8 additions & 4 deletions pkg/solana/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/chains"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/loop"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/sqlutil"
"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink-common/pkg/types/core"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
Expand Down Expand Up @@ -49,6 +49,7 @@ const DefaultRequestTimeout = 30 * time.Second
type ChainOpts struct {
Logger logger.Logger
KeyStore core.Keystore
DS sqlutil.DataSource
}

func (o *ChainOpts) Validate() (err error) {
Expand All @@ -61,6 +62,9 @@ func (o *ChainOpts) Validate() (err error) {
if o.KeyStore == nil {
err = errors.Join(err, required("KeyStore"))
}
if o.DS == nil {
err = errors.Join(err, required("DataSource"))
}
return
}

Expand All @@ -72,7 +76,7 @@ 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)
}
c, err := newChain(*cfg.ChainID, cfg, opts.KeyStore, opts.Logger)
c, err := newChain(*cfg.ChainID, cfg, opts.KeyStore, opts.Logger, opts.DS)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -222,7 +226,7 @@ func (v *verifiedCachedClient) GetAccountInfoWithOpts(ctx context.Context, addr
return v.ReaderWriter.GetAccountInfoWithOpts(ctx, addr, opts)
}

func newChain(id string, cfg *config.TOMLConfig, ks loop.Keystore, lggr logger.Logger) (*chain, error) {
func newChain(id string, cfg *config.TOMLConfig, ks core.Keystore, lggr logger.Logger, ds sqlutil.DataSource) (*chain, error) {
lggr = logger.With(lggr, "chainID", id, "chain", "solana")
var ch = chain{
id: id,
Expand Down Expand Up @@ -306,7 +310,7 @@ func newChain(id string, cfg *config.TOMLConfig, ks loop.Keystore, lggr logger.L
bc = internal.NewLoader[monitor.BalanceClient](func() (monitor.BalanceClient, error) { return ch.multiNode.SelectRPC() })
}

ch.txm = txm.NewTxm(ch.id, tc, sendTx, cfg, ks, lggr)
ch.txm = txm.NewTxm(ch.id, tc, sendTx, cfg, ks, lggr, ds)
ch.balanceMonitor = monitor.NewBalanceMonitor(ch.id, cfg, lggr, ks, bc)
return &ch, nil
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/solana/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import (
"github.com/gagliardetto/solana-go/programs/system"
"github.com/gagliardetto/solana-go/rpc"
"github.com/google/uuid"
"github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
sqlutilmocks "github.com/smartcontractkit/chainlink-common/pkg/sqlutil/mocks"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
mn "github.com/smartcontractkit/chainlink-solana/pkg/solana/client/multinode"
solcfg "github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
Expand Down Expand Up @@ -282,7 +284,7 @@ func TestChain_Transact(t *testing.T) {
return sig[:]
}, nil)

c, err := newChain("localnet", cfg, mkey, lgr)
c, err := newChain("localnet", cfg, mkey, lgr, sqlutilmocks.NewDataSource(t))
require.NoError(t, err)
require.NoError(t, c.txm.Start(ctx))

Expand Down Expand Up @@ -356,7 +358,7 @@ func TestSolanaChain_MultiNode_GetClient(t *testing.T) {
},
}

testChain, err := newChain("devnet", cfg, nil, logger.Test(t))
testChain, err := newChain("devnet", cfg, nil, logger.Test(t), sqlutilmocks.NewDataSource(t))
require.NoError(t, err)

err = testChain.Start(tests.Context(t))
Expand Down Expand Up @@ -398,7 +400,7 @@ func TestChain_MultiNode_TransactionSender(t *testing.T) {

// mocked keystore
mkey := mocks.NewSimpleKeystore(t)
c, err := newChain("localnet", cfg, mkey, lgr)
c, err := newChain("localnet", cfg, mkey, lgr, sqlutilmocks.NewDataSource(t))
require.NoError(t, err)
require.NoError(t, c.Start(ctx))
defer func() {
Expand Down Expand Up @@ -516,7 +518,7 @@ func TestSolanaChain_MultiNode_Txm(t *testing.T) {
}, nil)
mkey.On("Sign", mock.Anything, pubKeyReceiver.String(), mock.Anything).Return([]byte{}, config.KeyNotFoundError{ID: pubKeyReceiver.String(), KeyType: "Solana"})

testChain, err := newChain("localnet", cfg, mkey, logger.Test(t))
testChain, err := newChain("localnet", cfg, mkey, logger.Test(t), sqlutilmocks.NewDataSource(t))
require.NoError(t, err)

err = testChain.Start(tests.Context(t))
Expand Down
5 changes: 4 additions & 1 deletion pkg/solana/txm/txm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
solanaGo "github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/google/uuid"
"github.com/smartcontractkit/chainlink-common/pkg/sqlutil"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/loop"
Expand Down Expand Up @@ -55,6 +56,7 @@ type Txm struct {
cfg config.Config
txs PendingTxContext
ks SimpleKeystore
ds sqlutil.DataSource
client internal.Loader[client.ReaderWriter]
fee fees.Estimator
// sendTx is an override for sending transactions rather than using a single client
Expand Down Expand Up @@ -85,7 +87,7 @@ type pendingTx struct {
// NewTxm creates a txm. Uses simulation so should only be used to send txes to trusted contracts i.e. OCR.
func NewTxm(chainID string, client internal.Loader[client.ReaderWriter],
sendTx func(ctx context.Context, tx *solanaGo.Transaction) (solanaGo.Signature, error),
cfg config.Config, ks SimpleKeystore, lggr logger.Logger) *Txm {
cfg config.Config, ks SimpleKeystore, lggr logger.Logger, ds sqlutil.DataSource) *Txm {
if sendTx == nil {
// default sendTx using a single RPC
sendTx = func(ctx context.Context, tx *solanaGo.Transaction) (solanaGo.Signature, error) {
Expand All @@ -105,6 +107,7 @@ func NewTxm(chainID string, client internal.Loader[client.ReaderWriter],
cfg: cfg,
txs: newPendingTxContextWithProm(chainID),
ks: ks,
ds: ds,
client: client,
sendTx: sendTx,
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/solana/txm/txm_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.uber.org/zap/zapcore"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
sqlutilmocks "github.com/smartcontractkit/chainlink-common/pkg/sqlutil/mocks"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

Expand Down Expand Up @@ -66,7 +67,7 @@ func TestTxm_SendWithRetry_Race(t *testing.T) {
loader := utils.NewLazyLoad(func() (solanaClient.ReaderWriter, error) {
return client, nil
})
txm := NewTxm("retry_race", loader, nil, cfg, ks, lggr)
txm := NewTxm("retry_race", loader, nil, cfg, ks, lggr, sqlutilmocks.NewDataSource(t))
txm.fee = fee

_, _, _, err := txm.sendWithRetry(
Expand Down
3 changes: 2 additions & 1 deletion pkg/solana/txm/txm_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
keyMocks "github.com/smartcontractkit/chainlink-solana/pkg/solana/txm/mocks"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
sqlutilmocks "github.com/smartcontractkit/chainlink-common/pkg/sqlutil/mocks"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
bigmath "github.com/smartcontractkit/chainlink-common/pkg/utils/big_math"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestTxm_EstimateComputeUnitLimit(t *testing.T) {
client := clientmocks.NewReaderWriter(t)
require.NoError(t, err)
loader := utils.NewLazyLoad(func() (solanaClient.ReaderWriter, error) { return client, nil })
txm := solanatxm.NewTxm("localnet", loader, nil, cfg, mkey, lggr)
txm := solanatxm.NewTxm("localnet", loader, nil, cfg, mkey, lggr, sqlutilmocks.NewDataSource(t))

t.Run("successfully sets estimated compute unit limit", func(t *testing.T) {
usedCompute := uint64(100)
Expand Down

0 comments on commit 7bcc5ae

Please sign in to comment.