From f036da91a76a033a296518917c311d18cde2d7ba Mon Sep 17 00:00:00 2001 From: kakysha Date: Mon, 18 Mar 2024 19:05:24 +0200 Subject: [PATCH 1/5] feat: add "local" network type support --- client/common/network.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/client/common/network.go b/client/common/network.go index ec50a01b..9e5dccf0 100644 --- a/client/common/network.go +++ b/client/common/network.go @@ -176,7 +176,21 @@ func (network *Network) ExplorerMetadata(provider MetadataProvider) (string, err func LoadNetwork(name string, node string) Network { switch name { - + case "local": + return Network{ + LcdEndpoint: "", + TmEndpoint: "tcp://localhost:26657", + ChainGrpcEndpoint: "tcp://localhost:9900", + ChainStreamGrpcEndpoint: "", + ExchangeGrpcEndpoint: "", + ExplorerGrpcEndpoint: "", + ChainId: "injective-1", + Fee_denom: "inj", + Name: "local-1", + chainCookieAssistant: &DisabledCookieAssistant{}, + exchangeCookieAssistant: &DisabledCookieAssistant{}, + explorerCookieAssistant: &DisabledCookieAssistant{}, + } case "devnet-1": return Network{ LcdEndpoint: "https://devnet-1.lcd.injective.dev", From 0fd35792c9235ca75c6a93be3f837917ee059771 Mon Sep 17 00:00:00 2001 From: kakysha Date: Mon, 18 Mar 2024 19:19:11 +0200 Subject: [PATCH 2/5] feat: option to fix seq mismatch error --- client/chain/chain.go | 7 +++---- client/common/options.go | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/client/chain/chain.go b/client/chain/chain.go index e5340eba..e32e70f1 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -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") { c.syncNonce() sequence := c.getAccSeq() c.txFactory = c.txFactory.WithSequence(sequence) @@ -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") { c.syncNonce() sequence := c.getAccSeq() c.txFactory = c.txFactory.WithSequence(sequence) @@ -631,7 +631,6 @@ func (c *chainClient) AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRe return nil, err } } - return res, nil } @@ -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) diff --git a/client/common/options.go b/client/common/options.go index 59706cd1..c9bbb0a6 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 + FixSeqMismatch bool } type ClientOption func(opts *ClientOptions) error func DefaultClientOptions() *ClientOptions { - return &ClientOptions{} + return &ClientOptions{ + FixSeqMismatch: true, + } } func OptionGasPrices(gasPrices string) ClientOption { @@ -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 + } +} From b4bd99bcc9e3dcc56329606488023607c9097a5e Mon Sep 17 00:00:00 2001 From: kakysha Date: Tue, 19 Mar 2024 18:20:37 +0200 Subject: [PATCH 3/5] chore: refactor repetitive code by extracting common functionality --- client/chain/chain.go | 55 +++++++------------------------------------ 1 file changed, 9 insertions(+), 46 deletions(-) diff --git a/client/chain/chain.go b/client/chain/chain.go index 2ca5e486..00566a26 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -423,7 +423,7 @@ func (c *chainClient) syncTimeoutHeight() { // if the account number and/or the account sequence number are zero (not set), // they will be queried for and set on the provided Factory. A new Factory with // the updated fields will be returned. -func (c *chainClient) prepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) { +func prepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) { from := clientCtx.GetFromAddress() if err := txf.AccountRetriever().EnsureExists(clientCtx, from); err != nil { @@ -633,7 +633,7 @@ func (c *chainClient) GetFeeDiscountInfo(ctx context.Context, account string) (* func (c *chainClient) SimulateMsg(clientCtx client.Context, msgs ...sdk.Msg) (*txtypes.SimulateResponse, error) { c.txFactory = c.txFactory.WithSequence(c.accSeq) c.txFactory = c.txFactory.WithAccountNumber(c.accNum) - txf, err := c.prepareFactory(clientCtx, c.txFactory) + txf, err := prepareFactory(clientCtx, c.txFactory) if err != nil { err = errors.Wrap(err, "failed to prepareFactory") return nil, err @@ -684,10 +684,12 @@ func (c *chainClient) AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRe } return res, nil } - 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 { @@ -708,7 +710,7 @@ func (c *chainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, in c.gasWanted = adjustedGas } - txf, err := c.prepareFactory(clientCtx, txf) + txf, err := prepareFactory(clientCtx, txf) if err != nil { return nil, errors.Wrap(err, "failed to prepareFactory") } @@ -799,48 +801,9 @@ func (c *chainClient) broadcastTx( await bool, msgs ...sdk.Msg, ) (*txtypes.BroadcastTxResponse, error) { - txf, err := c.prepareFactory(clientCtx, txf) + txBytes, err := c.buildSignedTx(clientCtx, txf, msgs...) 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 - } - ctx := c.getCookie(ctx) - simRes, err := c.txClient.Simulate(ctx, &txtypes.SimulateRequest{TxBytes: simTxBytes}) - 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...) - - 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 } @@ -849,7 +812,7 @@ func (c *chainClient) broadcastTx( Mode: txtypes.BroadcastMode_BROADCAST_MODE_SYNC, } // use our own client to broadcast tx - ctx = c.getCookie(ctx) + ctx := c.getCookie(context.Background()) res, err := c.txClient.BroadcastTx(ctx, &req) if !await || err != nil { return res, err From a9ea38cfc1e53865d9deec58c8e26f4de46a43fe Mon Sep 17 00:00:00 2001 From: kakysha Date: Wed, 20 Mar 2024 01:48:07 +0200 Subject: [PATCH 4/5] refactorL make PrepareFactory client method public needed to sdk clients that use own tx factory and want to initialize it --- client/chain/chain.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/chain/chain.go b/client/chain/chain.go index 00566a26..04fabbb8 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -419,11 +419,11 @@ func (c *chainClient) syncTimeoutHeight() { } } -// prepareFactory ensures the account defined by ctx.GetFromAddress() exists and +// PrepareFactory ensures the account defined by ctx.GetFromAddress() exists and // if the account number and/or the account sequence number are zero (not set), // they will be queried for and set on the provided Factory. A new Factory with // the updated fields will be returned. -func prepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) { +func PrepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) { from := clientCtx.GetFromAddress() if err := txf.AccountRetriever().EnsureExists(clientCtx, from); err != nil { @@ -633,7 +633,7 @@ func (c *chainClient) GetFeeDiscountInfo(ctx context.Context, account string) (* func (c *chainClient) SimulateMsg(clientCtx client.Context, msgs ...sdk.Msg) (*txtypes.SimulateResponse, error) { c.txFactory = c.txFactory.WithSequence(c.accSeq) c.txFactory = c.txFactory.WithAccountNumber(c.accNum) - txf, err := prepareFactory(clientCtx, c.txFactory) + txf, err := PrepareFactory(clientCtx, c.txFactory) if err != nil { err = errors.Wrap(err, "failed to prepareFactory") return nil, err @@ -710,7 +710,7 @@ func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, ms c.gasWanted = adjustedGas } - txf, err := prepareFactory(clientCtx, txf) + txf, err := PrepareFactory(clientCtx, txf) if err != nil { return nil, errors.Wrap(err, "failed to prepareFactory") } From 8e00488c3396b4823d25211abe84eb7571590a6d Mon Sep 17 00:00:00 2001 From: Kishan Dhakan Date: Mon, 25 Mar 2024 20:35:47 +0530 Subject: [PATCH 5/5] try reducing testing suggestions --- .coderabbit.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.coderabbit.yaml b/.coderabbit.yaml index 930db111..e62d849f 100644 --- a/.coderabbit.yaml +++ b/.coderabbit.yaml @@ -1,8 +1,12 @@ reviews: + path_instructions: + - path: "**/*.go" + instructions: "Ignore making test suggestions for Go files. Review best practices, security, and performance." auto_review: base_branches: - "master" - "dev" - "feat/.*" + chat: auto_reply: true