Skip to content

Commit

Permalink
fix(transactor): add nonce too low fix. (#54)
Browse files Browse the repository at this point in the history
* noncer fix

* fix

* fix

* run linter
  • Loading branch information
Devon Bear authored Jan 23, 2024
1 parent 9119429 commit f011c6b
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 5 deletions.
39 changes: 36 additions & 3 deletions core/transactor/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@ package sender

import (
"context"
"errors"
"time"

"github.com/berachain/offchain-sdk/core/transactor/tracker"
"github.com/berachain/offchain-sdk/core/transactor/types"
sdk "github.com/berachain/offchain-sdk/types"

"github.com/ethereum/go-ethereum/core"
coretypes "github.com/ethereum/go-ethereum/core/types"
)

type Noncer interface {
RemoveInFlight(tx *tracker.InFlightTx)
}

// Factory interface for signing transactions.
type Factory interface {
BuildTransaction(context.Context, *types.TxRequest) (*coretypes.Transaction, error)
SignTransaction(*coretypes.Transaction) (*coretypes.Transaction, error)
}

// Sender struct holds the transaction replacement and retry policies.
type Sender struct {
noncer Noncer // noncer to acquire nonces
factory Factory
txReplacementPolicy TxReplacementPolicy // policy to replace transactions
retryPolicy RetryPolicy // policy to retry transactions
}

// New creates a new Sender with default replacement and retry policies.
func New(factory Factory) *Sender {
func New(factory Factory, noncer Noncer) *Sender {
return &Sender{
noncer: noncer, // noncer to acquire nonces
factory: factory, // factory to sign transactions
txReplacementPolicy: DefaultTxReplacementPolicy, // default transaction replacement policy
retryPolicy: DefaultRetryPolicy, // default retry policy
Expand Down Expand Up @@ -94,12 +104,35 @@ func (s *Sender) OnStale(ctx context.Context, tx *tracker.InFlightTx) error {
// transaction is replaced with a new transaction with a higher gas price as defined by
// the txReplacementPolicy.
// TODO: make this more robust probably.
func (s *Sender) OnError(ctx context.Context, tx *tracker.InFlightTx, _ error) {
func (s *Sender) OnError(ctx context.Context, tx *tracker.InFlightTx, err error) {
if errors.Is(err, core.ErrNonceTooLow) {
ethTx, buildErr := s.factory.BuildTransaction(ctx, &types.TxRequest{
To: tx.To(),
Value: tx.Value(),
Data: tx.Data(),
})
if buildErr != nil {
sdk.UnwrapContext(ctx).Logger().Error(
"failed to build replacement transaction", "err", err)
return
}
// The original tx was never sent so we remove from the in-flight list.
s.noncer.RemoveInFlight(tx)

// Assign the new transaction to the in-flight transaction.
tx.Transaction = ethTx
tx.Receipt = nil
}

replacementTx, err := s.factory.SignTransaction(s.txReplacementPolicy(ctx, tx.Transaction))
if err != nil {
sdk.UnwrapContext(ctx).Logger().Error(
"failed to sign replacement transaction", "err", err)
return
}
_ = s.SendTransaction(ctx, replacementTx)
if err = s.SendTransaction(ctx, replacementTx); err != nil {
sdk.UnwrapContext(ctx).Logger().Error(
"failed to send replacement transaction", "err", err)
return
}
}
2 changes: 1 addition & 1 deletion core/transactor/transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewTransactor(
dispatcher: dispatcher,
cfg: cfg,
factory: factory,
sender: sender.New(factory),
sender: sender.New(factory, noncer),
noncer: noncer,
tracker: tracker.New(noncer, dispatcher, cfg.TxReceiptTimeout),
requests: queue,
Expand Down
19 changes: 18 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
)

require (
Expand All @@ -28,12 +29,14 @@ require (
github.com/Antonboom/errname v0.1.10 // indirect
github.com/Antonboom/nilnil v0.1.5 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/DataDog/zstd v1.4.5 // indirect
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 // indirect
github.com/GaijinEntertainment/go-exhaustruct/v3 v3.1.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/OpenPeeDeeP/depguard/v2 v2.1.0 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/alexkohler/nakedret/v2 v2.0.2 // indirect
github.com/alexkohler/prealloc v1.0.0 // indirect
github.com/alingse/asasalint v0.0.11 // indirect
Expand Down Expand Up @@ -62,6 +65,12 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charithe/durationcheck v0.0.10 // indirect
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 // indirect
github.com/cockroachdb/errors v1.8.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593 // indirect
github.com/cockroachdb/redact v1.0.8 // indirect
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
Expand Down Expand Up @@ -93,7 +102,9 @@ require (
github.com/go-xmlfmt/xmlfmt v1.1.2 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe // indirect
Expand All @@ -115,6 +126,7 @@ require (
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jgautheron/goconst v1.5.1 // indirect
Expand All @@ -124,6 +136,9 @@ require (
github.com/kisielk/errcheck v1.6.3 // indirect
github.com/kisielk/gotool v1.0.0 // indirect
github.com/kkHAIKE/contextcheck v1.1.4 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kulti/thelper v0.6.3 // indirect
github.com/kunwardeep/paralleltest v1.0.8 // indirect
github.com/kyoh86/exportloopref v0.1.11 // indirect
Expand Down Expand Up @@ -152,6 +167,7 @@ require (
github.com/nunnatsa/ginkgolinter v0.13.3 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/polyfloyd/go-errorlint v1.4.3 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
Expand All @@ -162,6 +178,7 @@ require (
github.com/quasilyte/regex/syntax v0.0.0-20210819130434-b3f0c404a727 // indirect
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/ryancurrah/gomodguard v1.3.0 // indirect
github.com/ryanrolds/sqlclosecheck v0.4.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
Expand All @@ -185,9 +202,9 @@ require (
github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect
github.com/stbenjam/no-sprintf-host-port v0.1.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/t-yuki/gocover-cobertura v0.0.0-20180217150009-aaee18c8195c // indirect
github.com/tdakkota/asciicheck v0.2.0 // indirect
github.com/tetafro/godot v1.4.11 // indirect
Expand Down
Loading

0 comments on commit f011c6b

Please sign in to comment.