diff --git a/orm/model/ormtable/paginate.go b/orm/model/ormtable/paginate.go index c14040796d81..56f30cf8fb04 100644 --- a/orm/model/ormtable/paginate.go +++ b/orm/model/ormtable/paginate.go @@ -55,18 +55,28 @@ func (it *paginationIterator) Next() bool { if it.i >= it.done { it.pageRes = &queryv1beta1.PageResponse{} cursor := it.Cursor() - if it.Iterator.Next() { + next := it.Iterator.Next() + if next { it.pageRes.NextKey = cursor it.i++ } if it.countTotal { - for { - if !it.Iterator.Next() { - it.pageRes.Total = uint64(it.i) - return false + // once it.Iterator.Next() returns false, another call to it will panic. + // we check next here to ensure we do not call it again. + if next { + for { + if !it.Iterator.Next() { + it.pageRes.Total = uint64(it.i) + return false + } + it.i++ } - it.i++ + } else { + // when next is false, the iterator can no longer move forward, + // so the index == total entries. + it.pageRes.Total = uint64(it.i) } + } return false } diff --git a/orm/model/ormtable/table_test.go b/orm/model/ormtable/table_test.go index 4b1a88ff0753..06df3f3cb48b 100644 --- a/orm/model/ormtable/table_test.go +++ b/orm/model/ormtable/table_test.go @@ -4,15 +4,14 @@ import ( "bytes" "context" "fmt" - "google.golang.org/protobuf/types/known/timestamppb" "sort" "strings" "testing" "time" - dbm "github.com/tendermint/tm-db" + "google.golang.org/protobuf/types/known/timestamppb" - "github.com/cosmos/cosmos-sdk/orm/types/kv" + dbm "github.com/tendermint/tm-db" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" @@ -21,6 +20,8 @@ import ( "gotest.tools/v3/golden" "pgregory.net/rapid" + "github.com/cosmos/cosmos-sdk/orm/types/kv" + queryv1beta1 "github.com/cosmos/cosmos-sdk/api/cosmos/base/query/v1beta1" sdkerrors "github.com/cosmos/cosmos-sdk/errors" "github.com/cosmos/cosmos-sdk/orm/encoding/ormkv" @@ -67,6 +68,38 @@ func TestScenario(t *testing.T) { checkEncodeDecodeEntries(t, table, store.IndexStoreReader()) } +// isolated test for bug - https://github.com/cosmos/cosmos-sdk/issues/11431 +func TestPaginationLimitCountTotal(t *testing.T) { + table, err := ormtable.Build(ormtable.Options{ + MessageType: (&testpb.ExampleTable{}).ProtoReflect().Type(), + }) + backend := testkv.NewSplitMemBackend() + ctx := ormtable.WrapContextDefault(backend) + store, err := testpb.NewExampleTableTable(table) + assert.NilError(t, err) + + assert.NilError(t, store.Insert(ctx, &testpb.ExampleTable{U32: 4, I64: 2, Str: "co"})) + assert.NilError(t, store.Insert(ctx, &testpb.ExampleTable{U32: 5, I64: 2, Str: "sm"})) + assert.NilError(t, store.Insert(ctx, &testpb.ExampleTable{U32: 6, I64: 2, Str: "os"})) + + it, err := store.List(ctx, &testpb.ExampleTablePrimaryKey{}, ormlist.Paginate(&queryv1beta1.PageRequest{Limit: 3, CountTotal: true})) + assert.NilError(t, err) + assert.Check(t, it.Next()) + + it, err = store.List(ctx, &testpb.ExampleTablePrimaryKey{}, ormlist.Paginate(&queryv1beta1.PageRequest{Limit: 4, CountTotal: true})) + assert.NilError(t, err) + assert.Check(t, it.Next()) + + it, err = store.List(ctx, &testpb.ExampleTablePrimaryKey{}, ormlist.Paginate(&queryv1beta1.PageRequest{Limit: 1, CountTotal: true})) + assert.NilError(t, err) + for it.Next() { + } + pr := it.PageResponse() + assert.Check(t, pr != nil) + assert.Equal(t, uint64(3), pr.Total) + +} + func TestImportedMessageIterator(t *testing.T) { table, err := ormtable.Build(ormtable.Options{ MessageType: (&testpb.ExampleTimestamp{}).ProtoReflect().Type(),