Skip to content

Commit

Permalink
Updated simulation to set max CU limit and enabled sig verification
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-momin committed Nov 8, 2024
1 parent f985d16 commit 82ceaee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
20 changes: 16 additions & 4 deletions pkg/solana/txm/txm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import (

const (
MaxQueueLen = 1000
MaxRetryTimeMs = 250 // max tx retry time (exponential retry will taper to retry every 0.25s)
MaxSigsToConfirm = 256 // max number of signatures in GetSignatureStatus call
EstimateComputeUnitLimitBuffer = 10 // percent buffer added on top of estimated compute unit limits to account for any variance
MaxRetryTimeMs = 250 // max tx retry time (exponential retry will taper to retry every 0.25s)
MaxSigsToConfirm = 256 // max number of signatures in GetSignatureStatus call
EstimateComputeUnitLimitBuffer = 10 // percent buffer added on top of estimated compute unit limits to account for any variance
MaxComputeUnitLimit = 1_400_000 // max compute unit limit a transaction can have
)

var _ services.Service = (*Txm)(nil)
Expand Down Expand Up @@ -613,7 +614,18 @@ func (txm *Txm) simulateTx(ctx context.Context, tx *solanaGo.Transaction) (res *
return
}

res, err = client.SimulateTx(ctx, tx, nil) // use default options (does not verify signatures)
// Copy tx to avoid changing the original tx
txCopy := *tx

// Set max compute unit limit when simulating a transaction to avoid getting an error for exceeding the default 200k compute unit limit
if computeUnitLimitErr := fees.SetComputeUnitLimit(&txCopy, fees.ComputeUnitLimit(MaxComputeUnitLimit)); computeUnitLimitErr != nil {
// This error can occur if endpoint goes down or if invalid signature
txm.lggr.Errorw("failed to set compute unit limit when simulating tx", "error", computeUnitLimitErr)
return nil, computeUnitLimitErr
}

// Simulate with signature verification since it can have a significant impact on compute units
res, err = client.SimulateTx(ctx, &txCopy, &rpc.SimulateTransactionOpts{SigVerify: true})
if err != nil {
// This error can occur if endpoint goes down or if invalid signature
txm.lggr.Errorw("failed to simulate tx", "error", err)
Expand Down
14 changes: 12 additions & 2 deletions pkg/solana/txm/txm_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
solanaClient "github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
clientmocks "github.com/smartcontractkit/chainlink-solana/pkg/solana/client/mocks"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/fees"
solanatxm "github.com/smartcontractkit/chainlink-solana/pkg/solana/txm"
keyMocks "github.com/smartcontractkit/chainlink-solana/pkg/solana/txm/mocks"

Expand All @@ -25,7 +26,6 @@ import (

func TestTxm_EstimateComputeUnitLimit(t *testing.T) {
t.Parallel()

ctx := tests.Context(t)

// setup mock keystore
Expand Down Expand Up @@ -57,7 +57,17 @@ func TestTxm_EstimateComputeUnitLimit(t *testing.T) {
Blockhash: solana.Hash{},
},
}, nil).Once()
client.On("SimulateTx", mock.Anything, mock.Anything, mock.Anything).Return(&rpc.SimulateTransactionResult{
client.On("SimulateTx", mock.Anything, mock.IsType(&solana.Transaction{}), mock.IsType(&rpc.SimulateTransactionOpts{})).Run(func(args mock.Arguments) {
// Validate max compute unit limit is set in transaction
tx := args.Get(1).(*solana.Transaction)
limit, err := fees.ParseComputeUnitLimit(tx.Message.Instructions[1].Data)
require.NoError(t, err)
require.Equal(t, fees.ComputeUnitLimit(solanatxm.MaxComputeUnitLimit), limit)

// Validate signature verification is enabled
opts := args.Get(2).(*rpc.SimulateTransactionOpts)
require.True(t, opts.SigVerify)
}).Return(&rpc.SimulateTransactionResult{
Err: nil,
UnitsConsumed: &usedCompute,
}, nil).Once()
Expand Down

0 comments on commit 82ceaee

Please sign in to comment.