@@ -3,9 +3,12 @@ package keeper_test
33import (
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+ }
0 commit comments