Skip to content

Commit

Permalink
fix(client/v1beta2): handle sequence mismatch during calculate gas (#183
Browse files Browse the repository at this point in the history
)

due to cosmos-sdk not returning ABCI errors for /simulate call
exact error match does not work and we have to rely and strings compare

fixes akash-network/support#13

Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Jan 26, 2025
1 parent 31c0606 commit 1de2a35
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 7 additions & 7 deletions go/node/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var (
)

const (
// DefaultClientApiVersion indicates the default ApiVersion of the client.
DefaultClientApiVersion = "v1beta2"
// DefaultClientAPIVersion indicates the default ApiVersion of the client.
DefaultClientAPIVersion = "v1beta2"
)

// SetupFn defines a function that takes a parameter, ideally a Client or QueryClient.
Expand All @@ -27,7 +27,7 @@ type SetupFn func(interface{}) error
// DiscoverClient queries an RPC node to get the version of the client and executes a SetupFn function
// passing a new versioned Client instance as parameter.
// If any error occurs when calling the RPC node or the Cosmos SDK client Context is set to offline the default value of
// DefaultClientApiVersion will be used.
// DefaultClientAPIVersion will be used.
// An error is returned if client discovery is not successful.
func DiscoverClient(ctx context.Context, cctx sdkclient.Context, setup SetupFn, opts ...cltypes.ClientOption) error {
rpc, err := tmjclient.New(cctx.NodeURI)
Expand All @@ -43,9 +43,9 @@ func DiscoverClient(ctx context.Context, cctx sdkclient.Context, setup SetupFn,
}

// if client info is nil, mostly likely "akash" endpoint is not yet supported on the node
// fallback to manually set version to DefaultClientApiVersion
// fallback to manually set version to DefaultClientAPIVersion
if result.ClientInfo == nil || cctx.Offline {
result.ClientInfo = &ClientInfo{ApiVersion: DefaultClientApiVersion}
result.ClientInfo = &ClientInfo{ApiVersion: DefaultClientAPIVersion}
}

var cl interface{}
Expand All @@ -71,7 +71,7 @@ func DiscoverClient(ctx context.Context, cctx sdkclient.Context, setup SetupFn,
// DiscoverQueryClient queries an RPC node to get the version of the client and executes a SetupFn function
// passing a new versioned QueryClient instance as parameter.
// If any error occurs when calling the RPC node or the Cosmos SDK client Context is set to offline the default value of
// DefaultClientApiVersion will be used.
// DefaultClientAPIVersion will be used.
// An error is returned if client discovery is not successful.
func DiscoverQueryClient(ctx context.Context, cctx sdkclient.Context, setup SetupFn) error {
rpc, err := tmjclient.New(cctx.NodeURI)
Expand All @@ -87,7 +87,7 @@ func DiscoverQueryClient(ctx context.Context, cctx sdkclient.Context, setup Setu
}

if result.ClientInfo == nil {
result.ClientInfo = &ClientInfo{ApiVersion: DefaultClientApiVersion}
result.ClientInfo = &ClientInfo{ApiVersion: DefaultClientAPIVersion}
}

var cl interface{}
Expand Down
10 changes: 9 additions & 1 deletion go/node/client/v1beta2/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"os"
"regexp"
"strings"
"time"
"unsafe"
Expand Down Expand Up @@ -58,6 +59,10 @@ const (

var _ TxClient = (*serialBroadcaster)(nil)

var (
sequenceMismatchRegexp = regexp.MustCompile(`(account sequence mismatch, expected \d+, got \d+)`)
)

type ConfirmFn func(string) (bool, error)

// BroadcastOptions defines the options allowed to configure a transaction broadcast.
Expand Down Expand Up @@ -376,7 +381,10 @@ func deriveTxfFromOptions(txf tx.Factory, opts *BroadcastOptions) tx.Factory {
func (c *serialBroadcaster) broadcaster(ptxf tx.Factory) {
syncSequence := func(f tx.Factory, rErr error) (uint64, bool) {
if rErr != nil {
if sdkerrors.ErrWrongSequence.Is(rErr) {
// due to cosmos-sdk not returning ABCI errors for /simulate call
// exact error match does not work and we have to improvise
// use sdkerrors.ErrWrongSequence.Is(rErr) when /simulate call is fixed
if sequenceMismatchRegexp.MatchString(rErr.Error()) {
// attempt to sync account sequence
if rSeq, err := c.syncAccountSequence(f.Sequence()); err == nil {
return rSeq, true
Expand Down

0 comments on commit 1de2a35

Please sign in to comment.