From 244599f062772464487672725e73b3d84748780f Mon Sep 17 00:00:00 2001 From: Joel Smith Date: Thu, 15 Aug 2024 10:32:14 -0500 Subject: [PATCH] fix: tx out of gas (#1216) * fix: rounding of gas fees * Add Gas to ChainConfig * Sync TxCommand with main * fix merge chain spec config * test tx encoding & decoding * nit: path.Join --------- Co-authored-by: Reece Williams --- chain/cosmos/chain_node.go | 10 ++++-- chain/cosmos/cosmos_chain.go | 2 +- examples/cosmos/chain_miscellaneous_test.go | 38 +++++++++++++++++++++ ibc/types.go | 6 ++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/chain/cosmos/chain_node.go b/chain/cosmos/chain_node.go index 71a74d85b..54085596a 100644 --- a/chain/cosmos/chain_node.go +++ b/chain/cosmos/chain_node.go @@ -506,7 +506,7 @@ func (tn *ChainNode) FindTxs(ctx context.Context, height int64) ([]blockdb.Tx, e // with the chain node binary. func (tn *ChainNode) TxCommand(keyName string, command ...string) []string { command = append([]string{"tx"}, command...) - var gasPriceFound, gasAdjustmentFound, feesFound = false, false, false + var gasPriceFound, gasAdjustmentFound, gasFound, feesFound = false, false, false, false for i := 0; i < len(command); i++ { if command[i] == "--gas-prices" { gasPriceFound = true @@ -517,12 +517,18 @@ func (tn *ChainNode) TxCommand(keyName string, command ...string) []string { if command[i] == "--fees" { feesFound = true } + if command[i] == "--gas" { + gasFound = true + } } if !gasPriceFound && !feesFound { command = append(command, "--gas-prices", tn.Chain.Config().GasPrices) } if !gasAdjustmentFound { - command = append(command, "--gas-adjustment", fmt.Sprint(tn.Chain.Config().GasAdjustment)) + command = append(command, "--gas-adjustment", strconv.FormatFloat(tn.Chain.Config().GasAdjustment, 'f', -1, 64)) + } + if !gasFound && !feesFound && tn.Chain.Config().Gas != "" { + command = append(command, "--gas", tn.Chain.Config().Gas) } return tn.NodeCommand(append(command, "--from", keyName, diff --git a/chain/cosmos/cosmos_chain.go b/chain/cosmos/cosmos_chain.go index 23a0bc102..5f1e1799b 100644 --- a/chain/cosmos/cosmos_chain.go +++ b/chain/cosmos/cosmos_chain.go @@ -622,7 +622,7 @@ func (c *CosmosChain) GetTransaction(txhash string) (*types.TxResponse, error) { func (c *CosmosChain) GetGasFeesInNativeDenom(gasPaid int64) int64 { gasPrice, _ := strconv.ParseFloat(strings.Replace(c.cfg.GasPrices, c.cfg.Denom, "", 1), 64) fees := float64(gasPaid) * gasPrice - return int64(fees) + return int64(math.Ceil(fees)) } func (c *CosmosChain) UpgradeVersion(ctx context.Context, cli *client.Client, containerRepo, version string) { diff --git a/examples/cosmos/chain_miscellaneous_test.go b/examples/cosmos/chain_miscellaneous_test.go index 065f72ce4..7bd65dfde 100644 --- a/examples/cosmos/chain_miscellaneous_test.go +++ b/examples/cosmos/chain_miscellaneous_test.go @@ -3,6 +3,7 @@ package cosmos_test import ( "context" "fmt" + "path" "testing" "cosmossdk.io/math" @@ -51,6 +52,8 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) { CoinType: "118", ModifyGenesis: cosmos.ModifyGenesis(sdk47Genesis), EncodingConfig: wasmEncoding(), + GasAdjustment: 1.5, + Gas: "auto", }, NumValidators: &numVals, NumFullNodes: &numFullNodes, @@ -94,6 +97,7 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) { testAddingNode(ctx, t, chain) testGetGovernanceAddress(ctx, t, chain) testTXFailsOnBlockInclusion(ctx, t, chain, users) + testTXEncodeDecode(ctx, t, chain, users) } func wasmEncoding() *testutil.TestEncodingConfig { @@ -440,6 +444,40 @@ func testTXFailsOnBlockInclusion(ctx context.Context, t *testing.T, chain *cosmo require.Error(t, err) } +func testTXEncodeDecode(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) { + generate := chain.GetNode().TxCommand(users[0].KeyName(), "bank", "send", users[0].FormattedAddress(), users[1].FormattedAddress(), "1"+chain.Config().Denom, "--generate-only") + + // check if generate contains "--gas" + require.Contains(t, generate, "--gas") + + txJson, _, err := chain.GetNode().Exec(ctx, generate, nil) + require.NoError(t, err) + + // encode + err = chain.GetNode().WriteFile(ctx, txJson, "tx.json") + require.NoError(t, err) + encode := chain.GetNode().TxCommand(users[0].KeyName(), "encode", path.Join(chain.GetNode().HomeDir(), "tx.json")) + encoded, _, err := chain.GetNode().Exec(ctx, encode, nil) + require.NoError(t, err) + + // decode + decode := chain.GetNode().TxCommand(users[0].KeyName(), "decode", string(encoded)) + decoded, _, err := chain.GetNode().Exec(ctx, decode, nil) + require.NoError(t, err) + + err = chain.GetNode().WriteFile(ctx, decoded, "decoded.json") + require.NoError(t, err) + + sign := chain.GetNode().TxCommand(users[0].KeyName(), "sign", path.Join(chain.GetNode().HomeDir(), "decoded.json")) + signed, _, err := chain.GetNode().Exec(ctx, sign, nil) + require.NoError(t, err) + + err = chain.GetNode().WriteFile(ctx, signed, "signed.json") + require.NoError(t, err) + _, err = chain.GetNode().ExecTx(ctx, users[0].KeyName(), "broadcast", path.Join(chain.GetNode().HomeDir(), "signed.json")) + require.NoError(t, err) +} + // helpers func sendTokens(ctx context.Context, chain *cosmos.CosmosChain, from, to ibc.Wallet, token string, amount int64) (ibc.WalletAmount, error) { if token == "" { diff --git a/ibc/types.go b/ibc/types.go index b11aa1559..71a90aea6 100644 --- a/ibc/types.go +++ b/ibc/types.go @@ -33,6 +33,8 @@ type ChainConfig struct { GasPrices string `yaml:"gas-prices"` // Adjustment multiplier for gas fees. GasAdjustment float64 `yaml:"gas-adjustment"` + // Default gas limit for transactions. May be empty, "auto", or a number. + Gas string `yaml:"gas" default:"auto"` // Trusting period of the chain. TrustingPeriod string `yaml:"trusting-period"` // Do not use docker host mount. @@ -156,6 +158,10 @@ func (c ChainConfig) MergeChainSpecConfig(other ChainConfig) ChainConfig { c.GasAdjustment = other.GasAdjustment } + if other.Gas != "" { + c.Gas = other.Gas + } + if other.TrustingPeriod != "" { c.TrustingPeriod = other.TrustingPeriod }