Skip to content

Commit

Permalink
Merge pull request #220 from InjectiveLabs/feat/configurable_seq_mism…
Browse files Browse the repository at this point in the history
…atch_fixing

(feat) Added a new config option to disable the auto fix of sequence …
  • Loading branch information
aarmoa committed Apr 30, 2024
2 parents 73f266a + 6514823 commit 68f05f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 50 deletions.
55 changes: 9 additions & 46 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions client/common/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 7 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
ShouldFixSequenceMismatch bool
}

type ClientOption func(opts *ClientOptions) error

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

func OptionGasPrices(gasPrices string) ClientOption {
Expand Down

0 comments on commit 68f05f6

Please sign in to comment.