Skip to content

Commit

Permalink
fix: tx out of gas (#1216)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
joelsmith-2019 and Reecepbcups authored Aug 15, 2024
1 parent 8d552ae commit 244599f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
10 changes: 8 additions & 2 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
38 changes: 38 additions & 0 deletions examples/cosmos/chain_miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cosmos_test
import (
"context"
"fmt"
"path"
"testing"

"cosmossdk.io/math"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 == "" {
Expand Down
6 changes: 6 additions & 0 deletions ibc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 244599f

Please sign in to comment.