Skip to content

Commit 9a2e881

Browse files
kocubinskitac0turtle
authored andcommitted
fix(store): handle nil end in cachekv/iterator (#12945)
Closes: #12661 Adds support for nil end semantics to iterators in cachekv store, addressing [this workaround](https://github.com/osmosis-labs/osmosis/blob/4176b287d48338870bfda3029bfa20a6e45ac126/osmoutils/store_helper.go#L37-L41). Note that this has the effect of sorting the dirty cache into the BTree cache store in the bounds `[startIndex, end-of-cache-space]` --- *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... - [x] 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 - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed *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)
1 parent 516b36b commit 9a2e881

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ replace github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8
230230

231231
* (x/group) [#12888](https://github.com/cosmos/cosmos-sdk/pull/12888) Fix event propagation to the current context of `x/group` message execution `[]sdk.Result`.
232232
* (x/upgrade) [#12906](https://github.com/cosmos/cosmos-sdk/pull/12906) Fix upgrade failure by moving downgrade verification logic after store migration.
233+
* (store) [#12945](https://github.com/cosmos/cosmos-sdk/pull/12945) Fix nil end semantics in store/cachekv/iterator when iterating a dirty cache.
233234

234235
## [v0.46.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0) - 2022-07-26
235236

store/cachekv/store.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/cosmos/cosmos-sdk/store/tracekv"
1414
"github.com/cosmos/cosmos-sdk/store/types"
1515
"github.com/cosmos/cosmos-sdk/types/kv"
16-
"github.com/tendermint/tendermint/libs/math"
1716
)
1817

1918
// cValue represents a cached value.
@@ -279,7 +278,7 @@ const minSortSize = 1024
279278
// Constructs a slice of dirty items, to use w/ memIterator.
280279
func (store *Store) dirtyItems(start, end []byte) {
281280
startStr, endStr := conv.UnsafeBytesToStr(start), conv.UnsafeBytesToStr(end)
282-
if startStr > endStr {
281+
if end != nil && startStr > endStr {
283282
// Nothing to do here.
284283
return
285284
}
@@ -294,6 +293,7 @@ func (store *Store) dirtyItems(start, end []byte) {
294293
// than just not having the cache.
295294
if n < minSortSize {
296295
for key := range store.unsortedCache {
296+
// dbm.IsKeyInDomain is nil safe and returns true iff key is greater than start
297297
if dbm.IsKeyInDomain(conv.UnsafeStrToBytes(key), start, end) {
298298
cacheValue := store.cache[key]
299299
unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value})
@@ -314,24 +314,18 @@ func (store *Store) dirtyItems(start, end []byte) {
314314
// Now find the values within the domain
315315
// [start, end)
316316
startIndex := findStartIndex(strL, startStr)
317-
endIndex := findEndIndex(strL, endStr)
318-
319-
if endIndex < 0 {
320-
endIndex = len(strL) - 1
321-
}
322317
if startIndex < 0 {
323318
startIndex = 0
324319
}
325320

326-
// Since we spent cycles to sort the values, we should process and remove a reasonable amount
327-
// ensure start to end is at least minSortSize in size
328-
// if below minSortSize, expand it to cover additional values
329-
// this amortizes the cost of processing elements across multiple calls
330-
if endIndex-startIndex < minSortSize {
331-
endIndex = math.MinInt(startIndex+minSortSize, len(strL)-1)
332-
if endIndex-startIndex < minSortSize {
333-
startIndex = math.MaxInt(endIndex-minSortSize, 0)
334-
}
321+
var endIndex int
322+
if end == nil {
323+
endIndex = len(strL) - 1
324+
} else {
325+
endIndex = findEndIndex(strL, endStr)
326+
}
327+
if endIndex < 0 {
328+
endIndex = len(strL) - 1
335329
}
336330

337331
kvL := make([]*kv.Pair, 0)

0 commit comments

Comments
 (0)