From 65148234579fd464cade651c36f21e53aa240f32 Mon Sep 17 00:00:00 2001 From: abel Date: Fri, 26 Apr 2024 13:27:58 -0300 Subject: [PATCH] (feat) Added a new config option to disable the auto fix of sequence mismatch. Added configuration for local network. --- client/chain/chain.go | 55 +++++++--------------------------------- client/common/network.go | 16 ++++++++++++ client/common/options.go | 11 +++++--- 3 files changed, 32 insertions(+), 50 deletions(-) diff --git a/client/chain/chain.go b/client/chain/chain.go index 3e33f06a..1a35c5a0 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -675,7 +675,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.ShouldFixSequenceMismatch && strings.Contains(err.Error(), "account sequence mismatch") { c.syncNonce() sequence := c.getAccSeq() c.txFactory = c.txFactory.WithSequence(sequence) @@ -741,7 +741,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.ShouldFixSequenceMismatch && strings.Contains(err.Error(), "account sequence mismatch") { c.syncNonce() sequence := c.getAccSeq() c.txFactory = c.txFactory.WithSequence(sequence) @@ -761,7 +761,10 @@ func (c *chainClient) AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRe func (c *chainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, initialGas uint64, msgs ...sdk.Msg) ([]byte, error) { txf := NewTxFactory(clientCtx).WithSequence(accSeq).WithAccountNumber(accNum).WithGas(initialGas) + return c.buildSignedTx(clientCtx, txf, msgs...) +} +func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) ([]byte, error) { if clientCtx.Simulate { simTxBytes, err := txf.BuildSimTx(msgs...) if err != nil { @@ -873,49 +876,9 @@ func (c *chainClient) broadcastTx( await bool, msgs ...sdk.Msg, ) (*txtypes.BroadcastTxResponse, error) { - txf, err := c.prepareFactory(clientCtx, txf) - if err != nil { - err = errors.Wrap(err, "failed to prepareFactory") - return nil, err - } - ctx := context.Background() - if clientCtx.Simulate { - simTxBytes, err := txf.BuildSimTx(msgs...) - if err != nil { - err = errors.Wrap(err, "failed to build sim tx bytes") - return nil, err - } - - req := &txtypes.SimulateRequest{TxBytes: simTxBytes} - simRes, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.txClient.Simulate, req) - if err != nil { - err = errors.Wrap(err, "failed to CalculateGas") - return nil, err - } - - adjustedGas := uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)) - txf = txf.WithGas(adjustedGas) - - c.gasWanted = adjustedGas - } - - txn, err := txf.BuildUnsignedTx(msgs...) - + txBytes, err := c.buildSignedTx(clientCtx, txf, msgs...) if err != nil { - err = errors.Wrap(err, "failed to BuildUnsignedTx") - return nil, err - } - - txn.SetFeeGranter(clientCtx.GetFeeGranterAddress()) - err = tx.Sign(txf, clientCtx.GetFromName(), txn, true) - if err != nil { - err = errors.Wrap(err, "failed to Sign Tx") - return nil, err - } - - txBytes, err := clientCtx.TxConfig.TxEncoder()(txn.GetTx()) - if err != nil { - err = errors.Wrap(err, "failed TxEncoder to encode Tx") + err = errors.Wrap(err, "failed to build signed Tx") return nil, err } @@ -924,7 +887,7 @@ func (c *chainClient) broadcastTx( Mode: txtypes.BroadcastMode_BROADCAST_MODE_SYNC, } - res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.txClient.BroadcastTx, &req) + res, err := common.ExecuteCall(context.Background(), c.network.ChainCookieAssistant, c.txClient.BroadcastTx, &req) if !await || err != nil { return res, err } @@ -998,7 +961,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.ShouldFixSequenceMismatch && strings.Contains(err.Error(), "account sequence mismatch") { c.syncNonce() sequence := c.getAccSeq() c.txFactory = c.txFactory.WithSequence(sequence) diff --git a/client/common/network.go b/client/common/network.go index 0fbbd499..d2a5184a 100644 --- a/client/common/network.go +++ b/client/common/network.go @@ -204,6 +204,22 @@ type Network struct { func LoadNetwork(name string, node string) Network { switch name { + case "local": + return Network{ + LcdEndpoint: "http://localhost:10337", + TmEndpoint: "http://localhost:26657", + ChainGrpcEndpoint: "tcp://localhost:9900", + ChainStreamGrpcEndpoint: "tcp://localhost:9999", + ExchangeGrpcEndpoint: "tcp://localhost:9910", + ExplorerGrpcEndpoint: "tcp://localhost:9911", + ChainId: "injective-1", + FeeDenom: "inj", + Name: "local", + ChainCookieAssistant: &DisabledCookieAssistant{}, + ExchangeCookieAssistant: &DisabledCookieAssistant{}, + ExplorerCookieAssistant: &DisabledCookieAssistant{}, + } + case "devnet-1": return Network{ LcdEndpoint: "https://devnet-1.lcd.injective.dev", diff --git a/client/common/options.go b/client/common/options.go index 59706cd1..a4050947 100644 --- a/client/common/options.go +++ b/client/common/options.go @@ -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 + ShouldFixSequenceMismatch bool } type ClientOption func(opts *ClientOptions) error func DefaultClientOptions() *ClientOptions { - return &ClientOptions{} + return &ClientOptions{ + ShouldFixSequenceMismatch: true, + } } func OptionGasPrices(gasPrices string) ClientOption {