Skip to content

Commit 2279a7c

Browse files
kocubinskiHuangYi
authored andcommitted
fix(store): handle nil end in cachekv/iterator (cosmos#12945)
Closes: cosmos#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 52ed70c commit 2279a7c

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5656
### Bug Fixes
5757

5858
* (store) [#13516](https://github.com/cosmos/cosmos-sdk/pull/13516) Fix state listener that was observing writes at wrong time.
59+
* (store) [#12945](https://github.com/cosmos/cosmos-sdk/pull/12945) Fix nil end semantics in store/cachekv/iterator when iterating a dirty cache.
5960

6061
## v0.45.11 - 2022-11-09
6162

store/cachekv/store.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ const minSortSize = 1024
281281
// Constructs a slice of dirty items, to use w/ memIterator.
282282
func (store *Store) dirtyItems(start, end []byte) {
283283
startStr, endStr := conv.UnsafeBytesToStr(start), conv.UnsafeBytesToStr(end)
284-
if startStr > endStr {
284+
if end != nil && startStr > endStr {
285285
// Nothing to do here.
286286
return
287287
}
@@ -296,6 +296,7 @@ func (store *Store) dirtyItems(start, end []byte) {
296296
// than just not having the cache.
297297
if n < minSortSize {
298298
for key := range store.unsortedCache {
299+
// dbm.IsKeyInDomain is nil safe and returns true iff key is greater than start
299300
if dbm.IsKeyInDomain(conv.UnsafeStrToBytes(key), start, end) {
300301
cacheValue := store.cache[key]
301302
unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value})
@@ -313,7 +314,7 @@ func (store *Store) dirtyItems(start, end []byte) {
313314
}
314315
sort.Strings(strL)
315316

316-
startIndex, endIndex := findStartEndIndex(strL, startStr, endStr)
317+
startIndex, endIndex := findStartEndIndex(strL, startStr, endStr, end)
317318

318319
// Since we spent cycles to sort the values, we should process and remove a reasonable amount
319320
// ensure start to end is at least minSortSize in size
@@ -337,18 +338,24 @@ func (store *Store) dirtyItems(start, end []byte) {
337338
store.clearUnsortedCacheSubset(kvL, stateAlreadySorted)
338339
}
339340

340-
func findStartEndIndex(strL []string, startStr, endStr string) (int, int) {
341+
func findStartEndIndex(strL []string, startStr, endStr string, end []byte) (int, int) {
341342
// Now find the values within the domain
342343
// [start, end)
343344
startIndex := findStartIndex(strL, startStr)
344-
endIndex := findEndIndex(strL, endStr)
345+
if startIndex < 0 {
346+
startIndex = 0
347+
}
345348

346-
if endIndex < 0 {
349+
var endIndex int
350+
if end == nil {
347351
endIndex = len(strL) - 1
352+
} else {
353+
endIndex = findEndIndex(strL, endStr)
348354
}
349-
if startIndex < 0 {
350-
startIndex = 0
355+
if endIndex < 0 {
356+
endIndex = len(strL) - 1
351357
}
358+
352359
return startIndex, endIndex
353360
}
354361

0 commit comments

Comments
 (0)