Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Using QueryWithData to query unbonding delegations. #4456

Merged
merged 3 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}