Skip to content

Commit

Permalink
feat: option to fix seq mismatch error
Browse files Browse the repository at this point in the history
  • Loading branch information
kakysha committed Mar 18, 2024
1 parent f036da9 commit 0fd3579
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
7 changes: 3 additions & 4 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func (c *chainClient) SyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRes
res, err := c.broadcastTx(c.ctx, c.txFactory, true, msgs...)

if err != nil {
if strings.Contains(err.Error(), "account sequence mismatch") {
if c.opts.FixSeqMismatch && strings.Contains(err.Error(), "account sequence mismatch") {

Check warning on line 557 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L557

Added line #L557 was not covered by tests
c.syncNonce()
sequence := c.getAccSeq()
c.txFactory = c.txFactory.WithSequence(sequence)
Expand Down Expand Up @@ -617,7 +617,7 @@ func (c *chainClient) AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRe
c.txFactory = c.txFactory.WithAccountNumber(c.accNum)
res, err := c.broadcastTx(c.ctx, c.txFactory, false, msgs...)
if err != nil {
if strings.Contains(err.Error(), "account sequence mismatch") {
if c.opts.FixSeqMismatch && strings.Contains(err.Error(), "account sequence mismatch") {

Check warning on line 620 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L620

Added line #L620 was not covered by tests
c.syncNonce()
sequence := c.getAccSeq()
c.txFactory = c.txFactory.WithSequence(sequence)
Expand All @@ -631,7 +631,6 @@ func (c *chainClient) AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRe
return nil, err
}
}

return res, nil
}

Expand Down Expand Up @@ -874,7 +873,7 @@ func (c *chainClient) runBatchBroadcast() {
log.Debugln("broadcastTx with nonce", sequence)
res, err := c.broadcastTx(c.ctx, c.txFactory, true, toSubmit...)
if err != nil {
if strings.Contains(err.Error(), "account sequence mismatch") {
if c.opts.FixSeqMismatch && strings.Contains(err.Error(), "account sequence mismatch") {
c.syncNonce()
sequence := c.getAccSeq()
c.txFactory = c.txFactory.WithSequence(sequence)
Expand Down
18 changes: 14 additions & 4 deletions client/common/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ func init() {
}

type ClientOptions struct {
GasPrices string
TLSCert credentials.TransportCredentials
TxFactory *tx.Factory
GasPrices string
TLSCert credentials.TransportCredentials
TxFactory *tx.Factory
FixSeqMismatch bool
}

type ClientOption func(opts *ClientOptions) error

func DefaultClientOptions() *ClientOptions {
return &ClientOptions{}
return &ClientOptions{
FixSeqMismatch: true,
}

Check warning on line 33 in client/common/options.go

View check run for this annotation

Codecov / codecov/patch

client/common/options.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}

func OptionGasPrices(gasPrices string) ClientOption {
Expand Down Expand Up @@ -61,3 +64,10 @@ func OptionTxFactory(txFactory *tx.Factory) ClientOption {
return nil
}
}

func OptionFixSeqMismatch(fixSeqMismatch bool) ClientOption {
return func(opts *ClientOptions) error {
opts.FixSeqMismatch = fixSeqMismatch
return nil
}

Check warning on line 72 in client/common/options.go

View check run for this annotation

Codecov / codecov/patch

client/common/options.go#L68-L72

Added lines #L68 - L72 were not covered by tests
}

0 comments on commit 0fd3579

Please sign in to comment.