Skip to content

Commit 2949d8d

Browse files
authored
Resume scan-era cursors on the delegation-by-validator index (#4688)
Follow-up to #4518, part of PLT-980. Once the delegation-by-validator index is ready, `ValidatorDelegations` handed a `next_key` issued by the scan path (`len|delegator|len|validator`) straight to the index paginator, which pages by `len|delegator`. The delegator that should start the next page sorts before such a key and was silently dropped, and a reverse page with such a key was served instead of refused. The index path also paginated with `query.Paginate`, so the staking precompile's results inside DeliverTx depended on the shared driver rather than the release/v6.6 paginator the scan path pins, and an index entry without its delegation decoded to an empty `Delegation` and panicked the query. `validatorDelegationsFromIndex` now pages through `query.FilteredPaginateForContext`, as `validatorDelegationsFromScan` does. `withIndexCursor` translates a key longer than one length-prefixed delegator: a forward page resumes at that delegator when its delegation to the requested validator sorts at or after the key, and otherwise keeps the key, which the index already resumes after that delegator. A reverse page with such a key returns `InvalidArgument`. The cursor's validator is deliberately not matched against the request, because the scan's `next_key` names whichever delegation row comes next, usually for another validator. A missing primary row now returns `Internal`. This changes consensus-visible precompile output only once the index is ready, and v6.8 is not yet applied or scheduled on pacific-1 or atlantic-2. ABCI queries on the index path now get the same limit and offset caps as the scan path. The new keeper tests take cursors from the scan, mark the index ready and check the continued pages match the scan in both consensus and ABCI contexts, check the reverse refusal and the orphan entry, and fail without the fix.
1 parent b936db3 commit 2949d8d

2 files changed

Lines changed: 123 additions & 6 deletions

File tree

‎sei-cosmos/x/staking/keeper/delegation_index_test.go‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package keeper_test
33
import (
44
"bytes"
55
"encoding/binary"
6+
"fmt"
67
"testing"
78

89
"github.com/stretchr/testify/require"
10+
"google.golang.org/grpc/codes"
11+
"google.golang.org/grpc/status"
912

1013
seiapp "github.com/sei-protocol/sei-chain/app"
1114
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
@@ -386,3 +389,78 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidatorDelegationsIndexMatchesScan(
386389

387390
suite.ElementsMatch(scanned.DelegationResponses, indexed.DelegationResponses)
388391
}
392+
393+
// TestGRPCQueryValidatorDelegationsScanCursorAfterIndexReady pins that a forward page
394+
// resumed from a key the scan issued returns, once the index is ready, what the scan
395+
// would have returned, and that a reverse one is refused.
396+
func (suite *KeeperTestSuite) TestGRPCQueryValidatorDelegationsScanCursorAfterIndexReady() {
397+
for _, abciQuery := range []bool{false, true} {
398+
suite.Run(fmt.Sprintf("abciQuery=%v", abciQuery), func() {
399+
suite.SetupTest()
400+
app, ctx := suite.app, suite.ctx.WithIsABCIQuery(abciQuery)
401+
querier := keeper.Querier{Keeper: app.StakingKeeper}
402+
unreadyDelegationByValIndex(suite.T(), app, ctx)
403+
404+
valAddr := suite.vals[1].GetOperator()
405+
for b := byte(1); b <= 6; b++ {
406+
app.StakingKeeper.SetDelegation(ctx, types.NewDelegation(delegatorAt(0x00, b), valAddr, sdk.NewDec(1)))
407+
}
408+
shared := delegatorAt(0x00, 3)
409+
for _, val := range suite.vals {
410+
app.StakingKeeper.SetDelegation(ctx, types.NewDelegation(shared, val.GetOperator(), sdk.NewDec(1)))
411+
}
412+
413+
page := func(pageReq *query.PageRequest) ([]string, []byte, error) {
414+
res, err := querier.ValidatorDelegations(sdk.WrapSDKContext(ctx), &types.QueryValidatorDelegationsRequest{
415+
ValidatorAddr: valAddr.String(), Pagination: pageReq,
416+
})
417+
if err != nil {
418+
return nil, nil, err
419+
}
420+
delegators := make([]string, 0, len(res.DelegationResponses))
421+
for _, dr := range res.DelegationResponses {
422+
delegators = append(delegators, dr.Delegation.DelegatorAddress)
423+
}
424+
return delegators, res.Pagination.NextKey, nil
425+
}
426+
427+
_, firstPageCursor, err := page(&query.PageRequest{Limit: 2})
428+
suite.Require().NoError(err)
429+
cursors := [][]byte{firstPageCursor}
430+
for _, val := range suite.vals {
431+
cursors = append(cursors, types.GetDelegationKey(shared, val.GetOperator())[len(types.DelegationKey):])
432+
}
433+
scanned := make([][]string, len(cursors))
434+
for i, cursor := range cursors {
435+
scanned[i], _, err = page(&query.PageRequest{Key: cursor})
436+
suite.Require().NoError(err)
437+
}
438+
_, reverseCursor, err := page(&query.PageRequest{Limit: 2, Reverse: true})
439+
suite.Require().NoError(err)
440+
441+
_, err = app.StakingKeeper.MigrateDelegationByValIndex(ctx)
442+
suite.Require().NoError(err)
443+
444+
for i, cursor := range cursors {
445+
indexed, _, err := page(&query.PageRequest{Key: cursor})
446+
suite.Require().NoError(err)
447+
suite.Require().Equal(scanned[i], indexed, "cursor %X", cursor)
448+
}
449+
_, _, err = page(&query.PageRequest{Key: reverseCursor, Reverse: true})
450+
suite.Require().Equal(codes.InvalidArgument, status.Code(err))
451+
})
452+
}
453+
}
454+
455+
// TestGRPCQueryValidatorDelegationsOrphanIndexEntry pins that an index entry without
456+
// its delegation fails the query rather than panicking it.
457+
func (suite *KeeperTestSuite) TestGRPCQueryValidatorDelegationsOrphanIndexEntry() {
458+
app, ctx := suite.app, suite.ctx
459+
suite.Require().True(app.StakingKeeper.DelegationByValIndexReady(ctx))
460+
valAddr := suite.vals[0].GetOperator()
461+
ctx.KVStore(app.StakingKeeper.GetStoreKey()).Set(types.GetDelegationByValIndexKey(delegatorAt(0x00, 1), valAddr), []byte{})
462+
463+
querier := keeper.Querier{Keeper: app.StakingKeeper}
464+
_, err := querier.ValidatorDelegations(sdk.WrapSDKContext(ctx), &types.QueryValidatorDelegationsRequest{ValidatorAddr: valAddr.String()})
465+
suite.Require().Equal(codes.Internal, status.Code(err))
466+
}

‎sei-cosmos/x/staking/keeper/query_server.go‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keeper
22

33
import (
4+
"bytes"
45
"context"
56
"strings"
67

@@ -9,6 +10,7 @@ import (
910

1011
"github.com/sei-protocol/sei-chain/sei-cosmos/store/prefix"
1112
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
13+
"github.com/sei-protocol/sei-chain/sei-cosmos/types/address"
1214
"github.com/sei-protocol/sei-chain/sei-cosmos/types/query"
1315
"github.com/sei-protocol/sei-chain/sei-cosmos/x/staking/types"
1416
)
@@ -132,22 +134,59 @@ func (k Querier) validatorDelegationsFromIndex(ctx sdk.Context, req *types.Query
132134
valPrefix := types.GetDelegationsByValIndexKey(valAddr)
133135
indexStore := prefix.NewStore(store, valPrefix)
134136

135-
pageRes, err := query.Paginate(ctx, indexStore, req.Pagination, func(key []byte, _ []byte) error {
136-
indexKey := append(append([]byte{}, valPrefix...), key...)
137-
storeKey := types.GetDelegationKeyFromValIndexKey(indexKey)
138-
delegation, err := types.UnmarshalDelegation(k.cdc, store.Get(storeKey))
137+
pageReq, err := withIndexCursor(req.Pagination, valAddr)
138+
if err != nil {
139+
return nil, nil, err
140+
}
141+
// Consensus execution defaults to release/v6.6 behavior, as on the scan path.
142+
pageRes, err := query.FilteredPaginateForContext(ctx, indexStore, pageReq, func(key []byte, _ []byte, accumulate bool) (bool, error) {
143+
if !accumulate {
144+
return true, nil
145+
}
146+
storeKey := types.GetDelegationKeyFromValIndexKey(append(append([]byte{}, valPrefix...), key...))
147+
value := store.Get(storeKey)
148+
if value == nil {
149+
return false, status.Errorf(codes.Internal, "delegation-by-validator index entry %X has no delegation", storeKey)
150+
}
151+
delegation, err := types.UnmarshalDelegation(k.cdc, value)
139152
if err != nil {
140-
return err
153+
return false, err
141154
}
142155
delegations = append(delegations, delegation)
143-
return nil
156+
return true, nil
144157
})
145158
if err != nil {
146159
return nil, nil, query.WrapQueryError(err)
147160
}
148161
return delegations, pageRes, nil
149162
}
150163

164+
// withIndexCursor returns pageReq with a key issued by validatorDelegationsFromScan,
165+
// which orders delegations by delegator then validator, translated into the index's
166+
// per-delegator keyspace. Such a key is refused on a reverse page.
167+
func withIndexCursor(pageReq *query.PageRequest, valAddr sdk.ValAddress) (*query.PageRequest, error) {
168+
if pageReq == nil || len(pageReq.Key) == 0 {
169+
return pageReq, nil
170+
}
171+
delegatorEnd := 1 + int(pageReq.Key[0])
172+
if len(pageReq.Key) <= delegatorEnd {
173+
return pageReq, nil
174+
}
175+
if pageReq.Reverse {
176+
return nil, status.Error(codes.InvalidArgument, "reverse pagination cannot resume from a key issued before the delegation-by-validator index was ready")
177+
}
178+
delegator := pageReq.Key[:delegatorEnd]
179+
delegationKey := append(append([]byte{}, delegator...), address.MustLengthPrefix(valAddr)...)
180+
if bytes.Compare(delegationKey, pageReq.Key) < 0 {
181+
// The scan has passed this delegator's delegation to valAddr, and the index
182+
// already resumes after the delegator for a key longer than its own.
183+
return pageReq, nil
184+
}
185+
indexReq := *pageReq
186+
indexReq.Key = delegator
187+
return &indexReq, nil
188+
}
189+
151190
// validatorDelegationsFromScan filters the whole delegation store for the requested
152191
// validator. It is the only source at heights before the delegation-by-validator index
153192
// is ready, and reproduces the results and failures those heights had.

0 commit comments

Comments
 (0)