Skip to content

Commit 711d86a

Browse files
mergify[bot]beer-1facundomedicajulienrbrt
authored
fix: include pagination.key at reverse mode (backport #20939) (#20954)
Co-authored-by: beer-1 <147697694+beer-1@users.noreply.github.com> Co-authored-by: Facundo <facundomedica@gmail.com> Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent d975e01 commit 711d86a

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3838

3939
## [Unreleased]
4040

41+
## Bug Fixes
42+
43+
* [#20939](https://github.com/cosmos/cosmos-sdk/pull/20939) Fix collection reverse iterator to include `pagination.key` in the result.
44+
4145
## [v0.50.8](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.8) - 2024-07-15
4246

4347
## Features

types/query/collections_pagination.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ func encodeCollKey[K, V any, C Collection[K, V]](coll C, key K) ([]byte, error)
321321
func getCollIter[K, V any, C Collection[K, V]](ctx context.Context, coll C, prefix, start []byte, reverse bool) (collections.Iterator[K, V], error) {
322322
// TODO: maybe can be simplified
323323
if reverse {
324-
var end []byte
325-
if prefix != nil {
326-
start = storetypes.PrefixEndBytes(append(prefix, start...))
327-
end = prefix
328-
}
324+
// if we are in reverse mode, we need to increase the start key
325+
// to include the start key in the iteration.
326+
start = storetypes.PrefixEndBytes(append(prefix, start...))
327+
end := prefix
328+
329329
return coll.IterateRaw(ctx, end, start, collections.OrderDescending)
330330
}
331331
var end []byte

types/query/collections_pagination_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ func TestCollectionPagination(t *testing.T) {
8383
},
8484
expResults: createResults(299, 200),
8585
},
86+
"with key and reverse": {
87+
req: &PageRequest{
88+
Key: encodeKey(199),
89+
Reverse: true,
90+
},
91+
expResp: &PageResponse{
92+
NextKey: encodeKey(99),
93+
},
94+
expResults: createResults(199, 100),
95+
},
8696
"with offset and count total": {
8797
req: &PageRequest{
8898
Offset: 50,

0 commit comments

Comments
 (0)