Merge PR #4456: Using `QueryWithData` to query unbonding delegations

This commit is contained in:
Frank Yang 2019-06-04 20:05:44 +08:00 committed by Alexander Bezobchuk
parent 9c049321a1
commit 30550257a7
3 changed files with 110 additions and 7 deletions

View File

@ -0,0 +1 @@
#4455 Use `QueryWithData()` to query unbonding delegations.

View File

@ -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",
@ -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
}
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",
@ -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)

View File

@ -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))
}