From 525b2d5bbc2f12761c5b608957cae7d5e624fce9 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 8 Jul 2021 14:54:42 +0530 Subject: [PATCH 1/7] return MaxUint64 for Limit on InfiniteGasMeter and add a func to return GasLeft on GasMeter --- store/types/gas.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/store/types/gas.go b/store/types/gas.go index bf44e717d095..53121bb06f1c 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -41,6 +41,7 @@ type ErrorGasOverflow struct { type GasMeter interface { GasConsumed() Gas GasConsumedToLimit() Gas + GasLeft() Gas Limit() Gas ConsumeGas(amount Gas, descriptor string) RefundGas(amount Gas, descriptor string) @@ -66,6 +67,10 @@ func (g *basicGasMeter) GasConsumed() Gas { return g.consumed } +func (g *basicGasMeter) GasLeft() Gas { + return g.limit - g.consumed +} + func (g *basicGasMeter) Limit() Gas { return g.limit } @@ -145,8 +150,12 @@ func (g *infiniteGasMeter) GasConsumedToLimit() Gas { return g.consumed } +func (g *infiniteGasMeter) GasLeft() Gas { + return math.MaxUint64 +} + func (g *infiniteGasMeter) Limit() Gas { - return 0 + return math.MaxUint64 } func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) { From c300b48fa8d7d62adaf2a690b20b591b8c60773e Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 8 Jul 2021 16:05:22 +0530 Subject: [PATCH 2/7] fix failing tests --- store/types/gas_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/types/gas_test.go b/store/types/gas_test.go index 8a02df4cfa18..93c02d253aa8 100644 --- a/store/types/gas_test.go +++ b/store/types/gas_test.go @@ -10,7 +10,7 @@ import ( func TestInfiniteGasMeter(t *testing.T) { t.Parallel() meter := NewInfiniteGasMeter() - require.Equal(t, uint64(0), meter.Limit()) + require.Equal(t, uint64(math.MaxUint64), meter.Limit()) require.Equal(t, uint64(0), meter.GasConsumed()) require.Equal(t, uint64(0), meter.GasConsumedToLimit()) meter.ConsumeGas(10, "consume 10") From ab739c8fe577e99e400c2bb7ac3b221e0f0b1af1 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 9 Jul 2021 10:15:56 +0530 Subject: [PATCH 3/7] change limit to MaxUint64 from 0 in AnteTestSuite tests --- x/auth/ante/setup_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x/auth/ante/setup_test.go b/x/auth/ante/setup_test.go index 4942665cac04..86c633d899fa 100644 --- a/x/auth/ante/setup_test.go +++ b/x/auth/ante/setup_test.go @@ -1,6 +1,8 @@ package ante_test import ( + "math" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" @@ -33,8 +35,8 @@ func (suite *AnteTestSuite) TestSetup() { // Set height to non-zero value for GasMeter to be set suite.ctx = suite.ctx.WithBlockHeight(1) - // Context GasMeter Limit not set - suite.Require().Equal(uint64(0), suite.ctx.GasMeter().Limit(), "GasMeter set with limit before setup") + // Context GasMeter Limit set to MaxUint64 + suite.Require().Equal(uint64(math.MaxUint64), suite.ctx.GasMeter().Limit(), "GasMeter set with limit other than MaxUint64 before setup") newCtx, err := antehandler(suite.ctx, tx, false) suite.Require().Nil(err, "SetUpContextDecorator returned error") From 963f589969709a448e26328685eab4ece3e6d277 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 9 Jul 2021 10:22:02 +0530 Subject: [PATCH 4/7] small fix to GasLeft func --- store/types/gas.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/store/types/gas.go b/store/types/gas.go index 53121bb06f1c..7a9497b99fee 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -68,6 +68,9 @@ func (g *basicGasMeter) GasConsumed() Gas { } func (g *basicGasMeter) GasLeft() Gas { + if g.IsPastLimit() { + return 0 + } return g.limit - g.consumed } From e669dc1885ae9412ebadb97f2bb77ac362730112 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 9 Jul 2021 11:24:27 +0530 Subject: [PATCH 5/7] add tests for GasLeft func --- store/types/gas_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/store/types/gas_test.go b/store/types/gas_test.go index 93c02d253aa8..fb341721e6b2 100644 --- a/store/types/gas_test.go +++ b/store/types/gas_test.go @@ -11,12 +11,15 @@ func TestInfiniteGasMeter(t *testing.T) { t.Parallel() meter := NewInfiniteGasMeter() require.Equal(t, uint64(math.MaxUint64), meter.Limit()) + require.Equal(t, uint64(math.MaxUint64), meter.GasLeft()) require.Equal(t, uint64(0), meter.GasConsumed()) require.Equal(t, uint64(0), meter.GasConsumedToLimit()) meter.ConsumeGas(10, "consume 10") + require.Equal(t, uint64(math.MaxUint64), meter.GasLeft()) require.Equal(t, uint64(10), meter.GasConsumed()) require.Equal(t, uint64(10), meter.GasConsumedToLimit()) meter.RefundGas(1, "refund 1") + require.Equal(t, uint64(math.MaxUint64), meter.GasLeft()) require.Equal(t, uint64(9), meter.GasConsumed()) require.False(t, meter.IsPastLimit()) require.False(t, meter.IsOutOfGas()) @@ -48,6 +51,7 @@ func TestGasMeter(t *testing.T) { used += usage require.NotPanics(t, func() { meter.ConsumeGas(usage, "") }, "Not exceeded limit but panicked. tc #%d, usage #%d", tcnum, unum) require.Equal(t, used, meter.GasConsumed(), "Gas consumption not match. tc #%d, usage #%d", tcnum, unum) + require.Equal(t, tc.limit-used, meter.GasLeft(), "Gas left not match. tc #%d, usage #%d", tcnum, unum) require.Equal(t, used, meter.GasConsumedToLimit(), "Gas consumption (to limit) not match. tc #%d, usage #%d", tcnum, unum) require.False(t, meter.IsPastLimit(), "Not exceeded limit but got IsPastLimit() true") if unum < len(tc.usage)-1 { @@ -60,13 +64,20 @@ func TestGasMeter(t *testing.T) { require.Panics(t, func() { meter.ConsumeGas(1, "") }, "Exceeded but not panicked. tc #%d", tcnum) require.Equal(t, meter.GasConsumedToLimit(), meter.Limit(), "Gas consumption (to limit) not match limit") require.Equal(t, meter.GasConsumed(), meter.Limit()+1, "Gas consumption not match limit+1") + require.Equal(t, uint64(0), meter.GasLeft()) require.NotPanics(t, func() { meter.RefundGas(1, "refund 1") }) - require.Equal(t, meter.GasConsumed(), meter.Limit(), "Gas consumption not match limit+1") + require.Equal(t, meter.GasConsumed(), meter.Limit(), "Gas consumption not match with limit") + require.Equal(t, uint64(0), meter.GasLeft()) require.Panics(t, func() { meter.RefundGas(meter.GasConsumed()+1, "refund greater than consumed") }) + require.NotPanics(t, func() { meter.RefundGas(meter.GasConsumed(), "refund consumed gas") }) + require.Equal(t, meter.Limit(), meter.GasLeft()) + meter2 := NewGasMeter(math.MaxUint64) + require.Equal(t, uint64(math.MaxUint64), meter2.GasLeft()) meter2.ConsumeGas(Gas(math.MaxUint64/2), "consume half max uint64") + require.Equal(t, Gas(math.MaxUint64-(math.MaxUint64/2)), meter2.GasLeft()) require.Panics(t, func() { meter2.ConsumeGas(Gas(math.MaxUint64/2)+2, "panic") }) } } From b1bc58065d4f93fc3956bc5acdd61dd6e57c603c Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 9 Jul 2021 11:28:10 +0530 Subject: [PATCH 6/7] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccbc6d66f08e..06e1c5c2eb7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* [\#9651](https://github.com/cosmos/cosmos-sdk/pull/9651) Change inconsistent limit of `0` to `MaxUint64` on InfiniteGasMeter and add GasLeft func to GasMeter. * [\#9639](https://github.com/cosmos/cosmos-sdk/pull/9639) Check store keys length before accessing them by making sure that `key` is of length `m+1` (for `key[n:m]`) * (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt` * (x/genutil) [\#9574](https://github.com/cosmos/cosmos-sdk/pull/9575) Actually use the `gentx` client tx flags (like `--keyring-dir`) From 1d1bf27b1cfe87207083b89631e997e353c01913 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 9 Jul 2021 18:01:42 +0530 Subject: [PATCH 7/7] rename GasLeft to GasRemaining --- CHANGELOG.md | 2 +- store/types/gas.go | 6 +++--- store/types/gas_test.go | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06e1c5c2eb7c..da47d74fbfd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,7 +75,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes -* [\#9651](https://github.com/cosmos/cosmos-sdk/pull/9651) Change inconsistent limit of `0` to `MaxUint64` on InfiniteGasMeter and add GasLeft func to GasMeter. +* [\#9651](https://github.com/cosmos/cosmos-sdk/pull/9651) Change inconsistent limit of `0` to `MaxUint64` on InfiniteGasMeter and add GasRemaining func to GasMeter. * [\#9639](https://github.com/cosmos/cosmos-sdk/pull/9639) Check store keys length before accessing them by making sure that `key` is of length `m+1` (for `key[n:m]`) * (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt` * (x/genutil) [\#9574](https://github.com/cosmos/cosmos-sdk/pull/9575) Actually use the `gentx` client tx flags (like `--keyring-dir`) diff --git a/store/types/gas.go b/store/types/gas.go index 7a9497b99fee..dc7e44b3c2e4 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -41,7 +41,7 @@ type ErrorGasOverflow struct { type GasMeter interface { GasConsumed() Gas GasConsumedToLimit() Gas - GasLeft() Gas + GasRemaining() Gas Limit() Gas ConsumeGas(amount Gas, descriptor string) RefundGas(amount Gas, descriptor string) @@ -67,7 +67,7 @@ func (g *basicGasMeter) GasConsumed() Gas { return g.consumed } -func (g *basicGasMeter) GasLeft() Gas { +func (g *basicGasMeter) GasRemaining() Gas { if g.IsPastLimit() { return 0 } @@ -153,7 +153,7 @@ func (g *infiniteGasMeter) GasConsumedToLimit() Gas { return g.consumed } -func (g *infiniteGasMeter) GasLeft() Gas { +func (g *infiniteGasMeter) GasRemaining() Gas { return math.MaxUint64 } diff --git a/store/types/gas_test.go b/store/types/gas_test.go index fb341721e6b2..f4b5a6abe5ba 100644 --- a/store/types/gas_test.go +++ b/store/types/gas_test.go @@ -11,15 +11,15 @@ func TestInfiniteGasMeter(t *testing.T) { t.Parallel() meter := NewInfiniteGasMeter() require.Equal(t, uint64(math.MaxUint64), meter.Limit()) - require.Equal(t, uint64(math.MaxUint64), meter.GasLeft()) + require.Equal(t, uint64(math.MaxUint64), meter.GasRemaining()) require.Equal(t, uint64(0), meter.GasConsumed()) require.Equal(t, uint64(0), meter.GasConsumedToLimit()) meter.ConsumeGas(10, "consume 10") - require.Equal(t, uint64(math.MaxUint64), meter.GasLeft()) + require.Equal(t, uint64(math.MaxUint64), meter.GasRemaining()) require.Equal(t, uint64(10), meter.GasConsumed()) require.Equal(t, uint64(10), meter.GasConsumedToLimit()) meter.RefundGas(1, "refund 1") - require.Equal(t, uint64(math.MaxUint64), meter.GasLeft()) + require.Equal(t, uint64(math.MaxUint64), meter.GasRemaining()) require.Equal(t, uint64(9), meter.GasConsumed()) require.False(t, meter.IsPastLimit()) require.False(t, meter.IsOutOfGas()) @@ -51,7 +51,7 @@ func TestGasMeter(t *testing.T) { used += usage require.NotPanics(t, func() { meter.ConsumeGas(usage, "") }, "Not exceeded limit but panicked. tc #%d, usage #%d", tcnum, unum) require.Equal(t, used, meter.GasConsumed(), "Gas consumption not match. tc #%d, usage #%d", tcnum, unum) - require.Equal(t, tc.limit-used, meter.GasLeft(), "Gas left not match. tc #%d, usage #%d", tcnum, unum) + require.Equal(t, tc.limit-used, meter.GasRemaining(), "Gas left not match. tc #%d, usage #%d", tcnum, unum) require.Equal(t, used, meter.GasConsumedToLimit(), "Gas consumption (to limit) not match. tc #%d, usage #%d", tcnum, unum) require.False(t, meter.IsPastLimit(), "Not exceeded limit but got IsPastLimit() true") if unum < len(tc.usage)-1 { @@ -64,20 +64,20 @@ func TestGasMeter(t *testing.T) { require.Panics(t, func() { meter.ConsumeGas(1, "") }, "Exceeded but not panicked. tc #%d", tcnum) require.Equal(t, meter.GasConsumedToLimit(), meter.Limit(), "Gas consumption (to limit) not match limit") require.Equal(t, meter.GasConsumed(), meter.Limit()+1, "Gas consumption not match limit+1") - require.Equal(t, uint64(0), meter.GasLeft()) + require.Equal(t, uint64(0), meter.GasRemaining()) require.NotPanics(t, func() { meter.RefundGas(1, "refund 1") }) require.Equal(t, meter.GasConsumed(), meter.Limit(), "Gas consumption not match with limit") - require.Equal(t, uint64(0), meter.GasLeft()) + require.Equal(t, uint64(0), meter.GasRemaining()) require.Panics(t, func() { meter.RefundGas(meter.GasConsumed()+1, "refund greater than consumed") }) require.NotPanics(t, func() { meter.RefundGas(meter.GasConsumed(), "refund consumed gas") }) - require.Equal(t, meter.Limit(), meter.GasLeft()) + require.Equal(t, meter.Limit(), meter.GasRemaining()) meter2 := NewGasMeter(math.MaxUint64) - require.Equal(t, uint64(math.MaxUint64), meter2.GasLeft()) + require.Equal(t, uint64(math.MaxUint64), meter2.GasRemaining()) meter2.ConsumeGas(Gas(math.MaxUint64/2), "consume half max uint64") - require.Equal(t, Gas(math.MaxUint64-(math.MaxUint64/2)), meter2.GasLeft()) + require.Equal(t, Gas(math.MaxUint64-(math.MaxUint64/2)), meter2.GasRemaining()) require.Panics(t, func() { meter2.ConsumeGas(Gas(math.MaxUint64/2)+2, "panic") }) } }