Skip to content

Commit d404619

Browse files
mergify[bot]likhita-809amaury1093
authored
fix: Charge gas even when there are no entries in gaskv (backport #10218) (#10696)
* fix: Charge gas even when there are no entries in gaskv (#10218) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #10127 Charge gas for seeks with empty ranges --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 4c3aa4d) # Conflicts: # CHANGELOG.md * Fix conflicts Co-authored-by: likhita-809 <[email protected]> Co-authored-by: Amaury M <[email protected]>
1 parent 7ef483e commit d404619

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
3737

3838
## [Unreleased]
3939

40+
## [v0.45.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.0) - 2021-12-07
41+
42+
### State Machine Breaking
43+
44+
* (store) [#10218](https://github.com/cosmos/cosmos-sdk/pull/10218) Charge gas even when there are no entries while seeking.
45+
4046
## [v0.44.5](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.44.5) - 2021-12-02
4147

4248
### Improvements

store/gaskv/store.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ func (gs *Store) iterator(start, end []byte, ascending bool) types.Iterator {
108108
}
109109

110110
gi := newGasIterator(gs.gasMeter, gs.gasConfig, parent)
111-
if gi.Valid() {
112-
gi.(*gasIterator).consumeSeekGas()
113-
}
111+
gi.(*gasIterator).consumeSeekGas()
114112

115113
return gi
116114
}
@@ -143,10 +141,7 @@ func (gi *gasIterator) Valid() bool {
143141
// in the iterator. It incurs a flat gas cost for seeking and a variable gas
144142
// cost based on the current value's length if the iterator is valid.
145143
func (gi *gasIterator) Next() {
146-
if gi.Valid() {
147-
gi.consumeSeekGas()
148-
}
149-
144+
gi.consumeSeekGas()
150145
gi.parent.Next()
151146
}
152147

@@ -177,8 +172,10 @@ func (gi *gasIterator) Error() error {
177172
// consumeSeekGas consumes on each iteration step a flat gas cost and a variable gas cost
178173
// based on the current value's length.
179174
func (gi *gasIterator) consumeSeekGas() {
180-
value := gi.Value()
175+
if gi.Valid() {
176+
value := gi.Value()
181177

182-
gi.gasMeter.ConsumeGas(gi.gasConfig.ReadCostPerByte*types.Gas(len(value)), types.GasValuePerByteDesc)
178+
gi.gasMeter.ConsumeGas(gi.gasConfig.ReadCostPerByte*types.Gas(len(value)), types.GasValuePerByteDesc)
179+
}
183180
gi.gasMeter.ConsumeGas(gi.gasConfig.IterNextCostFlat, types.GasIterNextCostFlatDesc)
184181
}

store/gaskv/store_test.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/stretchr/testify/require"
78
dbm "github.com/tendermint/tm-db"
89

910
"github.com/cosmos/cosmos-sdk/store/dbadapter"
1011
"github.com/cosmos/cosmos-sdk/store/gaskv"
1112
"github.com/cosmos/cosmos-sdk/store/types"
12-
13-
"github.com/stretchr/testify/require"
1413
)
1514

1615
func bz(s string) []byte { return []byte(s) }
@@ -41,14 +40,18 @@ func TestGasKVStoreBasic(t *testing.T) {
4140

4241
func TestGasKVStoreIterator(t *testing.T) {
4342
mem := dbadapter.Store{DB: dbm.NewMemDB()}
44-
meter := types.NewGasMeter(10000)
43+
meter := types.NewGasMeter(100000)
4544
st := gaskv.NewStore(mem, meter, types.KVGasConfig())
4645
require.False(t, st.Has(keyFmt(1)))
4746
require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty")
4847
require.Empty(t, st.Get(keyFmt(2)), "Expected `key2` to be empty")
48+
require.Empty(t, st.Get(keyFmt(3)), "Expected `key3` to be empty")
49+
4950
st.Set(keyFmt(1), valFmt(1))
5051
require.True(t, st.Has(keyFmt(1)))
5152
st.Set(keyFmt(2), valFmt(2))
53+
require.True(t, st.Has(keyFmt(2)))
54+
st.Set(keyFmt(3), valFmt(0))
5255

5356
iterator := st.Iterator(nil, nil)
5457
start, end := iterator.Domain()
@@ -71,8 +74,16 @@ func TestGasKVStoreIterator(t *testing.T) {
7174
vb := iterator.Value()
7275
require.Equal(t, vb, valFmt(2))
7376
iterator.Next()
77+
require.Equal(t, types.Gas(13377), meter.GasConsumed())
78+
kc := iterator.Key()
79+
require.Equal(t, kc, keyFmt(3))
80+
vc := iterator.Value()
81+
require.Equal(t, vc, valFmt(0))
82+
iterator.Next()
83+
require.Equal(t, types.Gas(13446), meter.GasConsumed())
7484
require.False(t, iterator.Valid())
7585
require.Panics(t, iterator.Next)
86+
require.Equal(t, types.Gas(13476), meter.GasConsumed())
7687
require.NoError(t, iterator.Error())
7788

7889
reverseIterator := st.ReverseIterator(nil, nil)
@@ -81,14 +92,16 @@ func TestGasKVStoreIterator(t *testing.T) {
8192
t.Fatal(err)
8293
}
8394
})
95+
require.Equal(t, reverseIterator.Key(), keyFmt(3))
96+
reverseIterator.Next()
8497
require.Equal(t, reverseIterator.Key(), keyFmt(2))
8598
reverseIterator.Next()
8699
require.Equal(t, reverseIterator.Key(), keyFmt(1))
87100
reverseIterator.Next()
88101
require.False(t, reverseIterator.Valid())
89102
require.Panics(t, reverseIterator.Next)
90103

91-
require.Equal(t, types.Gas(9194), meter.GasConsumed())
104+
require.Equal(t, types.Gas(13782), meter.GasConsumed())
92105
}
93106

94107
func TestGasKVStoreOutOfGasSet(t *testing.T) {

0 commit comments

Comments
 (0)