Skip to content

Commit

Permalink
Merge PR #4456: Using QueryWithData to query unbonding delegations
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Yang authored and alexanderbez committed Jun 4, 2019
1 parent 9c04932 commit 3055025
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 7 deletions.
1 change: 1 addition & 0 deletions .pending/bugfixes/sdk/4455-Print-null-whil
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4455 Use `QueryWithData()` to query unbonding delegations.
25 changes: 18 additions & 7 deletions x/staking/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ $ %s query staking delegations-to cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ld

// GetCmdQueryUnbondingDelegation implements the command to query a single
// unbonding-delegation record.
func GetCmdQueryUnbondingDelegation(storeName string, cdc *codec.Codec) *cobra.Command {
func GetCmdQueryUnbondingDelegation(storeKey string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "unbonding-delegation [delegator-addr] [validator-addr]",
Short: "Query an unbonding-delegation record based on delegator and validator address",
Expand All @@ -338,20 +338,25 @@ $ %s query staking unbonding-delegation cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld7
return err
}

res, err := cliCtx.QueryStore(types.GetUBDKey(delAddr, valAddr), storeName)
bz, err := cdc.MarshalJSON(querier.NewQueryBondsParams(delAddr, valAddr))
if err != nil {
return err
}

return cliCtx.PrintOutput(types.MustUnmarshalUBD(cdc, res))
route := fmt.Sprintf("custom/%s/%s", storeKey, querier.QueryUnbondingDelegation)
res, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return err
}

return cliCtx.PrintOutput(types.MustUnmarshalUBD(cdc, res))
},
}
}

// GetCmdQueryUnbondingDelegations implements the command to query all the
// unbonding-delegation records for a delegator.
func GetCmdQueryUnbondingDelegations(storeName string, cdc *codec.Codec) *cobra.Command {
func GetCmdQueryUnbondingDelegations(storeKey string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "unbonding-delegations [delegator-addr]",
Short: "Query all unbonding-delegations records for one delegator",
Expand All @@ -373,14 +378,20 @@ $ %s query staking unbonding-delegation cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld7
return err
}

resKVs, err := cliCtx.QuerySubspace(types.GetUBDsKey(delegatorAddr), storeName)
bz, err := cdc.MarshalJSON(querier.NewQueryDelegatorParams(delegatorAddr))
if err != nil {
return err
}

route := fmt.Sprintf("custom/%s/%s", storeKey, querier.QueryDelegatorUnbondingDelegations)
res, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return err
}

var ubds types.UnbondingDelegations
for _, kv := range resKVs {
ubds = append(ubds, types.MustUnmarshalUBD(cdc, kv.Value))
if err = cdc.UnmarshalJSON(res, &ubds); err != nil {
return err
}

return cliCtx.PrintOutput(ubds)
Expand Down
91 changes: 91 additions & 0 deletions x/staking/querier/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,94 @@ func TestQueryRedelegations(t *testing.T) {
require.Equal(t, redel.ValidatorDstAddress, redelRes[0].ValidatorDstAddress)
require.Len(t, redel.Entries, len(redelRes[0].Entries))
}

func TestQueryUnbondingDelegation(t *testing.T) {
cdc := codec.New()
ctx, _, keeper := keep.CreateTestInput(t, false, 10000)

// Create Validators and Delegation
val1 := types.NewValidator(addrVal1, pk1, types.Description{})
keeper.SetValidator(ctx, val1)

// delegate
delAmount := sdk.TokensFromTendermintPower(100)
_, err := keeper.Delegate(ctx, addrAcc1, delAmount, val1, true)
require.NoError(t, err)
_ = keeper.ApplyAndReturnValidatorSetUpdates(ctx)

// undelegate
undelAmount := sdk.TokensFromTendermintPower(20)
_, err = keeper.Undelegate(ctx, addrAcc1, val1.GetOperator(), undelAmount.ToDec())
require.NoError(t, err)
keeper.ApplyAndReturnValidatorSetUpdates(ctx)

_, found := keeper.GetUnbondingDelegation(ctx, addrAcc1, val1.OperatorAddress)
require.True(t, found)

//
// found: query unbonding delegation by delegator and validator
//
queryValidatorParams := NewQueryBondsParams(addrAcc1, val1.GetOperator())
bz, errRes := cdc.MarshalJSON(queryValidatorParams)
require.Nil(t, errRes)
query := abci.RequestQuery{
Path: "/custom/staking/unbondingDelegation",
Data: bz,
}
res, err := queryUnbondingDelegation(ctx, query, keeper)
require.Nil(t, err)
require.NotNil(t, res)
var ubDel types.UnbondingDelegation
require.NoError(t, cdc.UnmarshalJSON(res, &ubDel))
require.Equal(t, addrAcc1, ubDel.DelegatorAddress)
require.Equal(t, val1.OperatorAddress, ubDel.ValidatorAddress)
require.Equal(t, 1, len(ubDel.Entries))

//
// not found: query unbonding delegation by delegator and validator
//
queryValidatorParams = NewQueryBondsParams(addrAcc2, val1.GetOperator())
bz, errRes = cdc.MarshalJSON(queryValidatorParams)
require.Nil(t, errRes)
query = abci.RequestQuery{
Path: "/custom/staking/unbondingDelegation",
Data: bz,
}
res, err = queryUnbondingDelegation(ctx, query, keeper)
require.NotNil(t, err)

//
// found: query unbonding delegation by delegator and validator
//
queryDelegatorParams := NewQueryDelegatorParams(addrAcc1)
bz, errRes = cdc.MarshalJSON(queryDelegatorParams)
require.Nil(t, errRes)
query = abci.RequestQuery{
Path: "/custom/staking/delegatorUnbondingDelegations",
Data: bz,
}
res, err = queryDelegatorUnbondingDelegations(ctx, query, keeper)
require.Nil(t, err)
require.NotNil(t, res)
var ubDels []types.UnbondingDelegation
require.NoError(t, cdc.UnmarshalJSON(res, &ubDels))
require.Equal(t, 1, len(ubDels))
require.Equal(t, addrAcc1, ubDels[0].DelegatorAddress)
require.Equal(t, val1.OperatorAddress, ubDels[0].ValidatorAddress)

//
// not found: query unbonding delegation by delegator and validator
//
queryDelegatorParams = NewQueryDelegatorParams(addrAcc2)
bz, errRes = cdc.MarshalJSON(queryDelegatorParams)
require.Nil(t, errRes)
query = abci.RequestQuery{
Path: "/custom/staking/delegatorUnbondingDelegations",
Data: bz,
}
res, err = queryDelegatorUnbondingDelegations(ctx, query, keeper)
require.Nil(t, err)
require.NotNil(t, res)
require.NoError(t, cdc.UnmarshalJSON(res, &ubDels))
require.Equal(t, 0, len(ubDels))
}

0 comments on commit 3055025

Please sign in to comment.