diff --git a/CHANGELOG.md b/CHANGELOG.md index 93f9f9358..1e456c471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ * [\#1221](https://github.com/cosmos/relayer/pull/1221) Update cometbft to v0.37.2 and ibc-go to v7.2.0. * [\#1226](https://github.com/cosmos/relayer/pull/1226) Avoid invalid Bech32 prefix error in parallel tests when sdk Config get overwritten by each other in single process. * [\#1231](https://github.com/cosmos/relayer/pull/1231) Reduce get bech32 prefix when get signer. +* [\#1302](https://github.com/cosmos/relayer/pull/1302) Avoid packet get relayed when estimated gas is higher than max gas. +* [\#1303](https://github.com/cosmos/relayer/pull/1303) Add missing max gas amount on txf to avoid estimate less gas when simualte runTx. ## v0.9.3 diff --git a/relayer/chains/cosmos/tx.go b/relayer/chains/cosmos/tx.go index 44e002889..163af0515 100644 --- a/relayer/chains/cosmos/tx.go +++ b/relayer/chains/cosmos/tx.go @@ -267,8 +267,6 @@ func (cc *CosmosProvider) SendMsgsWith(ctx context.Context, msgs []sdk.Msg, memo if err != nil { return nil, err } - - adjusted = uint64(float64(adjusted) * cc.PCfg.GasAdjustment) } //Cannot feegrant your own TX @@ -1668,6 +1666,9 @@ func (cc *CosmosProvider) PrepareFactory(txf tx.Factory, signingKey string) (tx. txf = txf.WithGas(cc.PCfg.MinGasAmount) } + if cc.PCfg.MaxGasAmount != 0 { + txf = txf.WithGas(cc.PCfg.MaxGasAmount) + } txf, err = cc.SetWithExtensionOptions(txf) if err != nil { return tx.Factory{}, err @@ -1676,21 +1677,19 @@ func (cc *CosmosProvider) PrepareFactory(txf tx.Factory, signingKey string) (tx. } // AdjustEstimatedGas adjusts the estimated gas usage by multiplying it by the gas adjustment factor -// and bounding the result by the maximum gas amount option. If the gas usage is zero, the adjusted gas -// is also zero. If the gas adjustment factor produces an infinite result, an error is returned. -// max-gas-amount is enforced. +// and return estimated gas is higher than max gas error. If the gas usage is zero, the adjusted gas +// is also zero. func (cc *CosmosProvider) AdjustEstimatedGas(gasUsed uint64) (uint64, error) { if gasUsed == 0 { return gasUsed, nil } + if cc.PCfg.MaxGasAmount > 0 && gasUsed > cc.PCfg.MaxGasAmount { + return 0, fmt.Errorf("estimated gas %d is higher than max gas %d", gasUsed, cc.PCfg.MaxGasAmount) + } gas := cc.PCfg.GasAdjustment * float64(gasUsed) if math.IsInf(gas, 1) { return 0, fmt.Errorf("infinite gas used") } - // Bound the gas estimate by the max_gas option - if cc.PCfg.MaxGasAmount > 0 { - gas = math.Min(gas, float64(cc.PCfg.MaxGasAmount)) - } return uint64(gas), nil } diff --git a/relayer/chains/cosmos/tx_test.go b/relayer/chains/cosmos/tx_test.go index 25bb02dd2..cf1b086e4 100644 --- a/relayer/chains/cosmos/tx_test.go +++ b/relayer/chains/cosmos/tx_test.go @@ -68,6 +68,14 @@ func TestCosmosProvider_AdjustEstimatedGas(t *testing.T) { expectedGas: 75000, expectedErr: nil, }, + { + name: "estimated gas is higher than max gas", + gasUsed: 50000, + gasAdjustment: 1.5, + maxGasAmount: 70000, + expectedGas: 75000, + expectedErr: fmt.Errorf("estimated gas 75000 is higher than max gas 70000"), + }, } for _, tc := range testCases {