From c8a6f66d9330c39fb5c74310b79fedacf8a2dedb Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 6 Oct 2023 21:58:40 +0800 Subject: [PATCH 1/4] Problem: packet get relayed even estimated gas is higher than max gas --- CHANGELOG.md | 1 + relayer/chains/cosmos/tx.go | 13 +++---------- relayer/chains/cosmos/tx_test.go | 17 ++++++++--------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93f9f9358..0abcd0f57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ * [\#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. +* [\#](https://github.com/cosmos/relayer/pull/) Avoid packet get relayed when estimated gas is higher than max gas. ## v0.9.3 diff --git a/relayer/chains/cosmos/tx.go b/relayer/chains/cosmos/tx.go index 44e002889..ce2e6af25 100644 --- a/relayer/chains/cosmos/tx.go +++ b/relayer/chains/cosmos/tx.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "math" "math/big" "math/rand" "regexp" @@ -267,8 +266,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 @@ -1683,14 +1680,10 @@ func (cc *CosmosProvider) AdjustEstimatedGas(gasUsed uint64) (uint64, error) { if gasUsed == 0 { return gasUsed, nil } - 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)) + 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) return uint64(gas), nil } diff --git a/relayer/chains/cosmos/tx_test.go b/relayer/chains/cosmos/tx_test.go index 25bb02dd2..db20a7304 100644 --- a/relayer/chains/cosmos/tx_test.go +++ b/relayer/chains/cosmos/tx_test.go @@ -2,7 +2,6 @@ package cosmos import ( "fmt" - "math" "testing" "github.com/cosmos/cosmos-sdk/client" @@ -52,14 +51,6 @@ func TestCosmosProvider_AdjustEstimatedGas(t *testing.T) { expectedGas: 75000, expectedErr: nil, }, - { - name: "gas used is infinite", - gasUsed: 10000, - gasAdjustment: math.Inf(1), - maxGasAmount: 0, - expectedGas: 0, - expectedErr: fmt.Errorf("infinite gas used"), - }, { name: "gas used is non-zero with zero max gas amount as default", gasUsed: 50000, @@ -68,6 +59,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 { From 0060c8dea054515ca2a3aa19dedca4d5388bd0d4 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 6 Oct 2023 22:05:51 +0800 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0abcd0f57..482a59564 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ * [\#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. -* [\#](https://github.com/cosmos/relayer/pull/) Avoid packet get relayed when estimated gas is higher than max gas. +* [\#1302](https://github.com/cosmos/relayer/pull/1302) Avoid packet get relayed when estimated gas is higher than max gas. ## v0.9.3 From 0ebf21e550cbb7f94e0e868562437292b92cbfc4 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 9 Oct 2023 09:39:32 +0800 Subject: [PATCH 3/4] update doc --- relayer/chains/cosmos/tx.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/relayer/chains/cosmos/tx.go b/relayer/chains/cosmos/tx.go index ce2e6af25..f243ec1ef 100644 --- a/relayer/chains/cosmos/tx.go +++ b/relayer/chains/cosmos/tx.go @@ -1673,9 +1673,8 @@ 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 From 0945c569667e1bbc87f5ab7d2e23872165b63238 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Wed, 11 Oct 2023 14:07:11 +0800 Subject: [PATCH 4/4] add back infinite check --- relayer/chains/cosmos/tx.go | 4 ++++ relayer/chains/cosmos/tx_test.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/relayer/chains/cosmos/tx.go b/relayer/chains/cosmos/tx.go index f243ec1ef..706bbba4d 100644 --- a/relayer/chains/cosmos/tx.go +++ b/relayer/chains/cosmos/tx.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "math" "math/big" "math/rand" "regexp" @@ -1683,6 +1684,9 @@ func (cc *CosmosProvider) AdjustEstimatedGas(gasUsed uint64) (uint64, error) { 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") + } return uint64(gas), nil } diff --git a/relayer/chains/cosmos/tx_test.go b/relayer/chains/cosmos/tx_test.go index db20a7304..cf1b086e4 100644 --- a/relayer/chains/cosmos/tx_test.go +++ b/relayer/chains/cosmos/tx_test.go @@ -2,6 +2,7 @@ package cosmos import ( "fmt" + "math" "testing" "github.com/cosmos/cosmos-sdk/client" @@ -51,6 +52,14 @@ func TestCosmosProvider_AdjustEstimatedGas(t *testing.T) { expectedGas: 75000, expectedErr: nil, }, + { + name: "gas used is infinite", + gasUsed: 10000, + gasAdjustment: math.Inf(1), + maxGasAmount: 0, + expectedGas: 0, + expectedErr: fmt.Errorf("infinite gas used"), + }, { name: "gas used is non-zero with zero max gas amount as default", gasUsed: 50000,