2020-02-18 07:44:23 -08:00
|
|
|
package keeper_test
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
import (
|
2019-04-24 08:48:39 -07:00
|
|
|
"fmt"
|
2018-09-13 14:23:44 -07:00
|
|
|
"testing"
|
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-02-18 07:44:23 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2018-09-13 14:23:44 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-06-12 07:54:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
2020-10-23 05:07:52 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-09-13 14:23:44 -07:00
|
|
|
)
|
|
|
|
|
2018-10-19 15:49:10 -07:00
|
|
|
func TestNewQuerier(t *testing.T) {
|
2020-02-26 06:57:41 -08:00
|
|
|
cdc, app, ctx := createTestInput()
|
2020-02-21 09:49:54 -08:00
|
|
|
|
2020-02-18 10:10:53 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 500, sdk.NewInt(10000))
|
|
|
|
_, addrAcc2 := addrs[0], addrs[1]
|
|
|
|
addrVal1, _ := sdk.ValAddress(addrs[0]), sdk.ValAddress(addrs[1])
|
2020-02-18 07:44:23 -08:00
|
|
|
|
2018-10-19 15:49:10 -07:00
|
|
|
// Create Validators
|
|
|
|
amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8)}
|
|
|
|
var validators [2]types.Validator
|
|
|
|
for i, amt := range amts {
|
2020-10-23 05:07:52 -07:00
|
|
|
validators[i] = teststaking.NewValidator(t, sdk.ValAddress(addrs[i]), PKs[i])
|
2019-06-28 13:11:27 -07:00
|
|
|
validators[i], _ = validators[i].AddTokensFromDel(amt)
|
2020-02-18 07:44:23 -08:00
|
|
|
app.StakingKeeper.SetValidator(ctx, validators[i])
|
|
|
|
app.StakingKeeper.SetValidatorByPowerIndex(ctx, validators[i])
|
2018-10-19 15:49:10 -07:00
|
|
|
}
|
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header := tmproto.Header{
|
2019-12-18 05:20:02 -08:00
|
|
|
ChainID: "HelloChain",
|
|
|
|
Height: 5,
|
|
|
|
}
|
2021-01-28 05:54:39 -08:00
|
|
|
hi := types.NewHistoricalInfo(header, validators[:], app.StakingKeeper.PowerReduction(ctx))
|
2020-10-23 05:07:52 -07:00
|
|
|
app.StakingKeeper.SetHistoricalInfo(ctx, 5, &hi)
|
2019-12-18 05:20:02 -08:00
|
|
|
|
2018-10-19 15:49:10 -07:00
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: "",
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.StakingKeeper, legacyQuerierCdc.LegacyAmino)
|
2018-10-19 15:49:10 -07:00
|
|
|
|
|
|
|
bz, err := querier(ctx, []string{"other"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.Error(t, err)
|
2018-10-19 15:49:10 -07:00
|
|
|
require.Nil(t, bz)
|
|
|
|
|
|
|
|
_, err = querier(ctx, []string{"pool"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-10-19 15:49:10 -07:00
|
|
|
|
|
|
|
_, err = querier(ctx, []string{"parameters"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-10-19 15:49:10 -07:00
|
|
|
|
2020-06-06 10:32:15 -07:00
|
|
|
queryValParams := types.NewQueryValidatorParams(addrVal1, 0, 0)
|
2018-10-22 03:03:10 -07:00
|
|
|
bz, errRes := cdc.MarshalJSON(queryValParams)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, errRes)
|
2018-10-19 15:49:10 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
query.Path = "/custom/staking/validator"
|
2018-10-19 15:49:10 -07:00
|
|
|
query.Data = bz
|
|
|
|
|
|
|
|
_, err = querier(ctx, []string{"validator"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-10-20 11:38:07 -07:00
|
|
|
|
2018-10-23 00:42:06 -07:00
|
|
|
_, err = querier(ctx, []string{"validatorDelegations"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-10-23 00:42:06 -07:00
|
|
|
|
2018-10-20 12:30:07 -07:00
|
|
|
_, err = querier(ctx, []string{"validatorUnbondingDelegations"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-10-20 12:30:07 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
queryDelParams := types.NewQueryDelegatorParams(addrAcc2)
|
2018-10-22 03:03:10 -07:00
|
|
|
bz, errRes = cdc.MarshalJSON(queryDelParams)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, errRes)
|
2018-10-22 03:03:10 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
query.Path = "/custom/staking/validator"
|
2018-10-22 03:03:10 -07:00
|
|
|
query.Data = bz
|
|
|
|
|
|
|
|
_, err = querier(ctx, []string{"delegatorDelegations"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-10-22 03:03:10 -07:00
|
|
|
|
|
|
|
_, err = querier(ctx, []string{"delegatorUnbondingDelegations"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-10-22 03:03:10 -07:00
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
_, err = querier(ctx, []string{"delegatorValidators"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-10-22 03:03:10 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
bz, errRes = cdc.MarshalJSON(types.NewQueryRedelegationParams(nil, nil, nil))
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, errRes)
|
2018-12-18 13:43:41 -08:00
|
|
|
query.Data = bz
|
|
|
|
|
|
|
|
_, err = querier(ctx, []string{"redelegations"}, query)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
queryHisParams := types.QueryHistoricalInfoRequest{Height: 5}
|
2019-12-18 05:20:02 -08:00
|
|
|
bz, errRes = cdc.MarshalJSON(queryHisParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query.Path = "/custom/staking/historicalInfo"
|
|
|
|
query.Data = bz
|
|
|
|
|
|
|
|
_, err = querier(ctx, []string{"historicalInfo"}, query)
|
|
|
|
require.NoError(t, err)
|
2018-10-19 15:49:10 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
func TestQueryParametersPool(t *testing.T) {
|
2020-02-26 06:57:41 -08:00
|
|
|
cdc, app, ctx := createTestInput()
|
2020-08-10 12:41:21 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.StakingKeeper, legacyQuerierCdc.LegacyAmino)
|
2020-02-18 07:44:23 -08:00
|
|
|
|
2019-08-02 09:37:35 -07:00
|
|
|
bondDenom := sdk.DefaultBondDenom
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
res, err := querier(ctx, []string{types.QueryParameters}, abci.RequestQuery{})
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
var params types.Params
|
|
|
|
errRes := cdc.UnmarshalJSON(res, ¶ms)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, errRes)
|
2020-02-18 07:44:23 -08:00
|
|
|
require.Equal(t, app.StakingKeeper.GetParams(ctx), params)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
res, err = querier(ctx, []string{types.QueryPool}, abci.RequestQuery{})
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
var pool types.Pool
|
2020-02-18 07:44:23 -08:00
|
|
|
bondedPool := app.StakingKeeper.GetBondedPool(ctx)
|
|
|
|
notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx)
|
2020-01-30 13:31:16 -08:00
|
|
|
require.NoError(t, cdc.UnmarshalJSON(res, &pool))
|
2020-02-18 07:44:23 -08:00
|
|
|
require.Equal(t, app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount, pool.NotBondedTokens)
|
|
|
|
require.Equal(t, app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount, pool.BondedTokens)
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryValidators(t *testing.T) {
|
2020-02-26 06:57:41 -08:00
|
|
|
cdc, app, ctx := createTestInput()
|
2020-02-18 07:44:23 -08:00
|
|
|
params := app.StakingKeeper.GetParams(ctx)
|
2020-08-10 12:41:21 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.StakingKeeper, legacyQuerierCdc.LegacyAmino)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 500, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
|
2020-02-18 10:10:53 -08:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
// Create Validators
|
2019-04-24 08:48:39 -07:00
|
|
|
amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8), sdk.NewInt(7)}
|
2020-10-12 06:56:02 -07:00
|
|
|
status := []types.BondStatus{types.Bonded, types.Unbonded, types.Unbonding}
|
2019-04-24 08:48:39 -07:00
|
|
|
var validators [3]types.Validator
|
2018-09-13 14:23:44 -07:00
|
|
|
for i, amt := range amts {
|
2020-10-23 05:07:52 -07:00
|
|
|
validators[i] = teststaking.NewValidator(t, sdk.ValAddress(addrs[i]), PKs[i])
|
2019-06-28 13:11:27 -07:00
|
|
|
validators[i], _ = validators[i].AddTokensFromDel(amt)
|
|
|
|
validators[i] = validators[i].UpdateStatus(status[i])
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-04-24 08:48:39 -07:00
|
|
|
|
2020-02-18 07:44:23 -08:00
|
|
|
app.StakingKeeper.SetValidator(ctx, validators[0])
|
|
|
|
app.StakingKeeper.SetValidator(ctx, validators[1])
|
|
|
|
app.StakingKeeper.SetValidator(ctx, validators[2])
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
// Query Validators
|
2020-02-18 07:44:23 -08:00
|
|
|
queriedValidators := app.StakingKeeper.GetValidators(ctx, params.MaxValidators)
|
2020-02-20 07:09:25 -08:00
|
|
|
require.Len(t, queriedValidators, 3)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2019-04-24 08:48:39 -07:00
|
|
|
for i, s := range status {
|
2019-06-05 16:26:17 -07:00
|
|
|
queryValsParams := types.NewQueryValidatorsParams(1, int(params.MaxValidators), s.String())
|
2019-05-16 08:25:32 -07:00
|
|
|
bz, err := cdc.MarshalJSON(queryValsParams)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2019-04-24 08:48:39 -07:00
|
|
|
req := abci.RequestQuery{
|
2019-06-05 16:26:17 -07:00
|
|
|
Path: fmt.Sprintf("/custom/%s/%s", types.QuerierRoute, types.QueryValidators),
|
2019-04-24 08:48:39 -07:00
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
2020-02-18 07:44:23 -08:00
|
|
|
res, err := querier(ctx, []string{types.QueryValidators}, req)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2019-04-24 08:48:39 -07:00
|
|
|
|
|
|
|
var validatorsResp []types.Validator
|
2019-05-16 08:25:32 -07:00
|
|
|
err = cdc.UnmarshalJSON(res, &validatorsResp)
|
2019-12-18 05:20:02 -08:00
|
|
|
require.NoError(t, err)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2019-04-24 08:48:39 -07:00
|
|
|
require.Equal(t, 1, len(validatorsResp))
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, validators[i].OperatorAddress, validatorsResp[0].OperatorAddress)
|
2019-04-24 08:48:39 -07:00
|
|
|
}
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
// Query each validator
|
2020-02-20 07:09:25 -08:00
|
|
|
for _, validator := range validators {
|
2020-09-25 03:25:37 -07:00
|
|
|
queryParams := types.NewQueryValidatorParams(validator.GetOperator(), 0, 0)
|
2020-02-20 07:09:25 -08:00
|
|
|
bz, err := cdc.MarshalJSON(queryParams)
|
|
|
|
require.NoError(t, err)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-02-20 07:09:25 -08:00
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/validator",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
res, err := querier(ctx, []string{types.QueryValidator}, query)
|
|
|
|
require.NoError(t, err)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-02-20 07:09:25 -08:00
|
|
|
var queriedValidator types.Validator
|
|
|
|
err = cdc.UnmarshalJSON(res, &queriedValidator)
|
|
|
|
require.NoError(t, err)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-10-23 05:07:52 -07:00
|
|
|
require.True(t, validator.Equal(&queriedValidator))
|
2020-02-20 07:09:25 -08:00
|
|
|
}
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
2020-02-20 08:30:45 -08:00
|
|
|
func TestQueryDelegation(t *testing.T) {
|
2020-02-26 06:57:41 -08:00
|
|
|
cdc, app, ctx := createTestInput()
|
2020-02-19 01:53:43 -08:00
|
|
|
params := app.StakingKeeper.GetParams(ctx)
|
2020-08-10 12:41:21 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.StakingKeeper, legacyQuerierCdc.LegacyAmino)
|
2020-02-19 01:53:43 -08:00
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 2, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
|
2020-02-20 07:09:25 -08:00
|
|
|
addrAcc1, addrAcc2 := addrs[0], addrs[1]
|
|
|
|
addrVal1, addrVal2 := sdk.ValAddress(addrAcc1), sdk.ValAddress(addrAcc2)
|
2020-02-19 01:53:43 -08:00
|
|
|
|
2020-02-20 07:09:25 -08:00
|
|
|
pubKeys := simapp.CreateTestPubKeys(2)
|
|
|
|
pk1, pk2 := pubKeys[0], pubKeys[1]
|
2020-02-19 01:53:43 -08:00
|
|
|
|
|
|
|
// Create Validators and Delegation
|
2020-10-23 05:07:52 -07:00
|
|
|
val1 := teststaking.NewValidator(t, addrVal1, pk1)
|
2020-02-19 01:53:43 -08:00
|
|
|
app.StakingKeeper.SetValidator(ctx, val1)
|
|
|
|
app.StakingKeeper.SetValidatorByPowerIndex(ctx, val1)
|
|
|
|
|
2020-10-23 05:07:52 -07:00
|
|
|
val2 := teststaking.NewValidator(t, addrVal2, pk2)
|
2020-02-19 01:53:43 -08:00
|
|
|
app.StakingKeeper.SetValidator(ctx, val2)
|
|
|
|
app.StakingKeeper.SetValidatorByPowerIndex(ctx, val2)
|
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
delTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 20)
|
2020-10-12 06:56:02 -07:00
|
|
|
_, err := app.StakingKeeper.Delegate(ctx, addrAcc2, delTokens, types.Unbonded, val1, true)
|
2020-02-20 07:09:25 -08:00
|
|
|
require.NoError(t, err)
|
2020-02-19 01:53:43 -08:00
|
|
|
|
|
|
|
// apply TM updates
|
2020-10-23 05:07:52 -07:00
|
|
|
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)
|
2020-02-19 01:53:43 -08:00
|
|
|
|
|
|
|
// Query Delegator bonded validators
|
|
|
|
queryParams := types.NewQueryDelegatorParams(addrAcc2)
|
|
|
|
bz, errRes := cdc.MarshalJSON(queryParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/delegatorValidators",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
delValidators := app.StakingKeeper.GetDelegatorValidators(ctx, addrAcc2, params.MaxValidators)
|
|
|
|
|
|
|
|
res, err := querier(ctx, []string{types.QueryDelegatorValidators}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-23 05:07:52 -07:00
|
|
|
var validatorsResp types.Validators
|
2020-02-19 01:53:43 -08:00
|
|
|
errRes = cdc.UnmarshalJSON(res, &validatorsResp)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
require.Equal(t, len(delValidators), len(validatorsResp))
|
|
|
|
require.ElementsMatch(t, delValidators, validatorsResp)
|
|
|
|
|
|
|
|
// error unknown request
|
|
|
|
query.Data = bz[:len(bz)-1]
|
|
|
|
|
2020-02-26 07:29:48 -08:00
|
|
|
_, err = querier(ctx, []string{types.QueryDelegatorValidators}, query)
|
2020-02-19 01:53:43 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Query bonded validator
|
2020-09-25 03:25:37 -07:00
|
|
|
queryBondParams := types.QueryDelegatorValidatorRequest{DelegatorAddr: addrAcc2.String(), ValidatorAddr: addrVal1.String()}
|
2020-02-19 01:53:43 -08:00
|
|
|
bz, errRes = cdc.MarshalJSON(queryBondParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/delegatorValidator",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = querier(ctx, []string{types.QueryDelegatorValidator}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var validator types.Validator
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &validator)
|
|
|
|
require.NoError(t, errRes)
|
2020-10-23 05:07:52 -07:00
|
|
|
require.True(t, validator.Equal(&delValidators[0]))
|
2020-02-19 01:53:43 -08:00
|
|
|
|
|
|
|
// error unknown request
|
|
|
|
query.Data = bz[:len(bz)-1]
|
|
|
|
|
2020-02-26 07:29:48 -08:00
|
|
|
_, err = querier(ctx, []string{types.QueryDelegatorValidator}, query)
|
2020-02-19 01:53:43 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Query delegation
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/delegation",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
delegation, found := app.StakingKeeper.GetDelegation(ctx, addrAcc2, addrVal1)
|
|
|
|
require.True(t, found)
|
|
|
|
|
|
|
|
res, err = querier(ctx, []string{types.QueryDelegation}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var delegationRes types.DelegationResponse
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &delegationRes)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
require.Equal(t, delegation.ValidatorAddress, delegationRes.Delegation.ValidatorAddress)
|
|
|
|
require.Equal(t, delegation.DelegatorAddress, delegationRes.Delegation.DelegatorAddress)
|
2020-02-19 01:53:43 -08:00
|
|
|
require.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, delegation.Shares.TruncateInt()), delegationRes.Balance)
|
|
|
|
|
|
|
|
// Query Delegator Delegations
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/delegatorDelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = querier(ctx, []string{types.QueryDelegatorDelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var delegatorDelegations types.DelegationResponses
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &delegatorDelegations)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
require.Len(t, delegatorDelegations, 1)
|
2020-07-14 10:41:30 -07:00
|
|
|
require.Equal(t, delegation.ValidatorAddress, delegatorDelegations[0].Delegation.ValidatorAddress)
|
|
|
|
require.Equal(t, delegation.DelegatorAddress, delegatorDelegations[0].Delegation.DelegatorAddress)
|
2020-02-19 01:53:43 -08:00
|
|
|
require.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, delegation.Shares.TruncateInt()), delegatorDelegations[0].Balance)
|
|
|
|
|
|
|
|
// error unknown request
|
|
|
|
query.Data = bz[:len(bz)-1]
|
|
|
|
|
2020-02-26 07:29:48 -08:00
|
|
|
_, err = querier(ctx, []string{types.QueryDelegation}, query)
|
2020-02-19 01:53:43 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Query validator delegations
|
2020-06-06 10:32:15 -07:00
|
|
|
bz, errRes = cdc.MarshalJSON(types.NewQueryValidatorParams(addrVal1, 1, 100))
|
2020-02-19 01:53:43 -08:00
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "custom/staking/validatorDelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = querier(ctx, []string{types.QueryValidatorDelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var delegationsRes types.DelegationResponses
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &delegationsRes)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
require.Len(t, delegatorDelegations, 1)
|
2020-07-14 10:41:30 -07:00
|
|
|
require.Equal(t, delegation.ValidatorAddress, delegationsRes[0].Delegation.ValidatorAddress)
|
|
|
|
require.Equal(t, delegation.DelegatorAddress, delegationsRes[0].Delegation.DelegatorAddress)
|
2020-02-19 01:53:43 -08:00
|
|
|
require.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, delegation.Shares.TruncateInt()), delegationsRes[0].Balance)
|
|
|
|
|
2020-02-20 08:30:13 -08:00
|
|
|
// Query unbonding delegation
|
2021-01-28 05:54:39 -08:00
|
|
|
unbondingTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 10)
|
2020-09-25 03:25:37 -07:00
|
|
|
_, err = app.StakingKeeper.Undelegate(ctx, addrAcc2, val1.GetOperator(), unbondingTokens.ToDec())
|
2020-02-19 01:53:43 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
queryBondParams = types.QueryDelegatorValidatorRequest{DelegatorAddr: addrAcc2.String(), ValidatorAddr: addrVal1.String()}
|
2020-02-19 01:53:43 -08:00
|
|
|
bz, errRes = cdc.MarshalJSON(queryBondParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/unbondingDelegation",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
unbond, found := app.StakingKeeper.GetUnbondingDelegation(ctx, addrAcc2, addrVal1)
|
|
|
|
require.True(t, found)
|
|
|
|
|
|
|
|
res, err = querier(ctx, []string{types.QueryUnbondingDelegation}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var unbondRes types.UnbondingDelegation
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &unbondRes)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
require.Equal(t, unbond, unbondRes)
|
|
|
|
|
|
|
|
// error unknown request
|
|
|
|
query.Data = bz[:len(bz)-1]
|
|
|
|
|
|
|
|
_, err = querier(ctx, []string{types.QueryUnbondingDelegation}, query)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
// Query Delegator Unbonding Delegations
|
2020-02-19 01:53:43 -08:00
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/delegatorUnbondingDelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = querier(ctx, []string{types.QueryDelegatorUnbondingDelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var delegatorUbds []types.UnbondingDelegation
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &delegatorUbds)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
require.Equal(t, unbond, delegatorUbds[0])
|
|
|
|
|
|
|
|
// error unknown request
|
|
|
|
query.Data = bz[:len(bz)-1]
|
|
|
|
|
|
|
|
_, err = querier(ctx, []string{types.QueryDelegatorUnbondingDelegations}, query)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Query redelegation
|
2021-01-28 05:54:39 -08:00
|
|
|
redelegationTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 10)
|
2020-09-25 03:25:37 -07:00
|
|
|
_, err = app.StakingKeeper.BeginRedelegation(ctx, addrAcc2, val1.GetOperator(),
|
|
|
|
val2.GetOperator(), redelegationTokens.ToDec())
|
2020-02-19 01:53:43 -08:00
|
|
|
require.NoError(t, err)
|
2020-09-25 03:25:37 -07:00
|
|
|
redel, found := app.StakingKeeper.GetRedelegation(ctx, addrAcc2, val1.GetOperator(), val2.GetOperator())
|
2020-02-19 01:53:43 -08:00
|
|
|
require.True(t, found)
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
bz, errRes = cdc.MarshalJSON(types.NewQueryRedelegationParams(addrAcc2, val1.GetOperator(), val2.GetOperator()))
|
2020-02-19 01:53:43 -08:00
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/redelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
2020-02-20 07:09:25 -08:00
|
|
|
res, err = querier(ctx, []string{types.QueryRedelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var redelRes types.RedelegationResponses
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &redelRes)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
require.Len(t, redelRes, 1)
|
2020-07-14 10:41:30 -07:00
|
|
|
require.Equal(t, redel.DelegatorAddress, redelRes[0].Redelegation.DelegatorAddress)
|
|
|
|
require.Equal(t, redel.ValidatorSrcAddress, redelRes[0].Redelegation.ValidatorSrcAddress)
|
|
|
|
require.Equal(t, redel.ValidatorDstAddress, redelRes[0].Redelegation.ValidatorDstAddress)
|
2020-02-20 07:09:25 -08:00
|
|
|
require.Len(t, redel.Entries, len(redelRes[0].Entries))
|
|
|
|
}
|
|
|
|
|
2020-06-06 10:32:15 -07:00
|
|
|
func TestQueryValidatorDelegations_Pagination(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
page int
|
|
|
|
limit int
|
|
|
|
expectedResults int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
page: 1,
|
|
|
|
limit: 75,
|
|
|
|
expectedResults: 75,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
page: 2,
|
|
|
|
limit: 75,
|
|
|
|
expectedResults: 25,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
page: 1,
|
|
|
|
limit: 100,
|
|
|
|
expectedResults: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cdc, app, ctx := createTestInput()
|
2020-08-10 12:41:21 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.StakingKeeper, legacyQuerierCdc.LegacyAmino)
|
2020-06-06 10:32:15 -07:00
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 100, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
|
2020-06-06 10:32:15 -07:00
|
|
|
pubKeys := simapp.CreateTestPubKeys(1)
|
|
|
|
|
|
|
|
valAddress := sdk.ValAddress(addrs[0])
|
|
|
|
|
2020-10-23 05:07:52 -07:00
|
|
|
val1 := teststaking.NewValidator(t, valAddress, pubKeys[0])
|
2020-06-06 10:32:15 -07:00
|
|
|
app.StakingKeeper.SetValidator(ctx, val1)
|
|
|
|
app.StakingKeeper.SetValidatorByPowerIndex(ctx, val1)
|
|
|
|
|
|
|
|
// Create Validators and Delegation
|
|
|
|
for _, addr := range addrs {
|
|
|
|
validator, found := app.StakingKeeper.GetValidator(ctx, valAddress)
|
|
|
|
if !found {
|
|
|
|
t.Error("expected validator not found")
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
delTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 20)
|
2020-10-12 06:56:02 -07:00
|
|
|
_, err := app.StakingKeeper.Delegate(ctx, addr, delTokens, types.Unbonded, validator, true)
|
2020-06-06 10:32:15 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply TM updates
|
2020-10-23 05:07:52 -07:00
|
|
|
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)
|
2020-06-06 10:32:15 -07:00
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
// Query Delegator bonded validators
|
|
|
|
queryParams := types.NewQueryDelegatorParams(addrs[0])
|
|
|
|
bz, errRes := cdc.MarshalJSON(queryParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
// Query valAddress delegations
|
|
|
|
bz, errRes = cdc.MarshalJSON(types.NewQueryValidatorParams(valAddress, c.page, c.limit))
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: "custom/staking/validatorDelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := querier(ctx, []string{types.QueryValidatorDelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var delegationsRes types.DelegationResponses
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &delegationsRes)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
require.Len(t, delegationsRes, c.expectedResults)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undelegate
|
|
|
|
for _, addr := range addrs {
|
2021-01-28 05:54:39 -08:00
|
|
|
delTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 20)
|
2020-06-06 10:32:15 -07:00
|
|
|
_, err := app.StakingKeeper.Undelegate(ctx, addr, val1.GetOperator(), delTokens.ToDec())
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply TM updates
|
2020-10-23 05:07:52 -07:00
|
|
|
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)
|
2020-06-06 10:32:15 -07:00
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
// Query Unbonding delegations with pagination.
|
|
|
|
queryParams := types.NewQueryDelegatorParams(addrs[0])
|
|
|
|
bz, errRes := cdc.MarshalJSON(queryParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
bz, errRes = cdc.MarshalJSON(types.NewQueryValidatorParams(valAddress, c.page, c.limit))
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
unbondingDelegations := types.UnbondingDelegations{}
|
|
|
|
res, err := querier(ctx, []string{types.QueryValidatorUnbondingDelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &unbondingDelegations)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
require.Len(t, unbondingDelegations, c.expectedResults)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 08:58:17 -08:00
|
|
|
func TestQueryRedelegations(t *testing.T) {
|
2020-02-26 06:57:41 -08:00
|
|
|
cdc, app, ctx := createTestInput()
|
2020-08-10 12:41:21 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.StakingKeeper, legacyQuerierCdc.LegacyAmino)
|
2020-02-20 08:58:17 -08:00
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 2, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
|
2020-02-20 08:58:17 -08:00
|
|
|
addrAcc1, addrAcc2 := addrs[0], addrs[1]
|
|
|
|
addrVal1, addrVal2 := sdk.ValAddress(addrAcc1), sdk.ValAddress(addrAcc2)
|
|
|
|
|
|
|
|
// Create Validators and Delegation
|
2020-10-23 05:07:52 -07:00
|
|
|
val1 := teststaking.NewValidator(t, addrVal1, PKs[0])
|
|
|
|
val2 := teststaking.NewValidator(t, addrVal2, PKs[1])
|
2020-02-20 08:58:17 -08:00
|
|
|
app.StakingKeeper.SetValidator(ctx, val1)
|
|
|
|
app.StakingKeeper.SetValidator(ctx, val2)
|
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
delAmount := app.StakingKeeper.TokensFromConsensusPower(ctx, 100)
|
2020-10-12 06:56:02 -07:00
|
|
|
_, err := app.StakingKeeper.Delegate(ctx, addrAcc2, delAmount, types.Unbonded, val1, true)
|
2020-03-20 12:24:18 -07:00
|
|
|
require.NoError(t, err)
|
2020-10-23 05:07:52 -07:00
|
|
|
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)
|
2020-02-20 08:58:17 -08:00
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
rdAmount := app.StakingKeeper.TokensFromConsensusPower(ctx, 20)
|
2020-03-20 12:24:18 -07:00
|
|
|
_, err = app.StakingKeeper.BeginRedelegation(ctx, addrAcc2, val1.GetOperator(), val2.GetOperator(), rdAmount.ToDec())
|
|
|
|
require.NoError(t, err)
|
2020-10-23 05:07:52 -07:00
|
|
|
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)
|
2020-02-20 08:58:17 -08:00
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
redel, found := app.StakingKeeper.GetRedelegation(ctx, addrAcc2, val1.GetOperator(), val2.GetOperator())
|
2020-02-20 08:58:17 -08:00
|
|
|
require.True(t, found)
|
|
|
|
|
|
|
|
// delegator redelegations
|
|
|
|
queryDelegatorParams := types.NewQueryDelegatorParams(addrAcc2)
|
|
|
|
bz, errRes := cdc.MarshalJSON(queryDelegatorParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/redelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := querier(ctx, []string{types.QueryRedelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var redelRes types.RedelegationResponses
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &redelRes)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
require.Len(t, redelRes, 1)
|
2020-07-14 10:41:30 -07:00
|
|
|
require.Equal(t, redel.DelegatorAddress, redelRes[0].Redelegation.DelegatorAddress)
|
|
|
|
require.Equal(t, redel.ValidatorSrcAddress, redelRes[0].Redelegation.ValidatorSrcAddress)
|
|
|
|
require.Equal(t, redel.ValidatorDstAddress, redelRes[0].Redelegation.ValidatorDstAddress)
|
2020-02-20 08:58:17 -08:00
|
|
|
require.Len(t, redel.Entries, len(redelRes[0].Entries))
|
|
|
|
|
|
|
|
// validator redelegations
|
2020-06-06 10:32:15 -07:00
|
|
|
queryValidatorParams := types.NewQueryValidatorParams(val1.GetOperator(), 0, 0)
|
2020-02-20 08:58:17 -08:00
|
|
|
bz, errRes = cdc.MarshalJSON(queryValidatorParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/redelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = querier(ctx, []string{types.QueryRedelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
errRes = cdc.UnmarshalJSON(res, &redelRes)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
require.Len(t, redelRes, 1)
|
2020-07-14 10:41:30 -07:00
|
|
|
require.Equal(t, redel.DelegatorAddress, redelRes[0].Redelegation.DelegatorAddress)
|
|
|
|
require.Equal(t, redel.ValidatorSrcAddress, redelRes[0].Redelegation.ValidatorSrcAddress)
|
|
|
|
require.Equal(t, redel.ValidatorDstAddress, redelRes[0].Redelegation.ValidatorDstAddress)
|
2020-02-20 08:58:17 -08:00
|
|
|
require.Len(t, redel.Entries, len(redelRes[0].Entries))
|
|
|
|
}
|
|
|
|
|
2020-02-20 09:10:37 -08:00
|
|
|
func TestQueryUnbondingDelegation(t *testing.T) {
|
2020-02-26 06:57:41 -08:00
|
|
|
cdc, app, ctx := createTestInput()
|
2020-08-10 12:41:21 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.StakingKeeper, legacyQuerierCdc.LegacyAmino)
|
2020-02-20 09:10:37 -08:00
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 2, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
|
2020-02-20 09:10:37 -08:00
|
|
|
addrAcc1, addrAcc2 := addrs[0], addrs[1]
|
|
|
|
addrVal1 := sdk.ValAddress(addrAcc1)
|
|
|
|
|
|
|
|
// Create Validators and Delegation
|
2020-10-23 05:07:52 -07:00
|
|
|
val1 := teststaking.NewValidator(t, addrVal1, PKs[0])
|
2020-02-20 09:10:37 -08:00
|
|
|
app.StakingKeeper.SetValidator(ctx, val1)
|
|
|
|
|
|
|
|
// delegate
|
2021-01-28 05:54:39 -08:00
|
|
|
delAmount := app.StakingKeeper.TokensFromConsensusPower(ctx, 100)
|
2020-10-12 06:56:02 -07:00
|
|
|
_, err := app.StakingKeeper.Delegate(ctx, addrAcc1, delAmount, types.Unbonded, val1, true)
|
2020-02-20 09:10:37 -08:00
|
|
|
require.NoError(t, err)
|
2020-10-23 05:07:52 -07:00
|
|
|
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)
|
2020-02-20 09:10:37 -08:00
|
|
|
|
|
|
|
// undelegate
|
2021-01-28 05:54:39 -08:00
|
|
|
undelAmount := app.StakingKeeper.TokensFromConsensusPower(ctx, 20)
|
2020-02-20 09:10:37 -08:00
|
|
|
_, err = app.StakingKeeper.Undelegate(ctx, addrAcc1, val1.GetOperator(), undelAmount.ToDec())
|
|
|
|
require.NoError(t, err)
|
2020-10-23 05:07:52 -07:00
|
|
|
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)
|
2020-02-20 09:10:37 -08:00
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
_, found := app.StakingKeeper.GetUnbondingDelegation(ctx, addrAcc1, val1.GetOperator())
|
2020-02-20 09:10:37 -08:00
|
|
|
require.True(t, found)
|
|
|
|
|
|
|
|
//
|
|
|
|
// found: query unbonding delegation by delegator and validator
|
|
|
|
//
|
2020-09-25 03:25:37 -07:00
|
|
|
queryValidatorParams := types.QueryDelegatorValidatorRequest{DelegatorAddr: addrAcc1.String(), ValidatorAddr: val1.GetOperator().String()}
|
2020-02-20 09:10:37 -08:00
|
|
|
bz, errRes := cdc.MarshalJSON(queryValidatorParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/unbondingDelegation",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
res, err := querier(ctx, []string{types.QueryUnbondingDelegation}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
var ubDel types.UnbondingDelegation
|
|
|
|
require.NoError(t, cdc.UnmarshalJSON(res, &ubDel))
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, addrAcc1.String(), ubDel.DelegatorAddress)
|
2020-02-20 09:10:37 -08:00
|
|
|
require.Equal(t, val1.OperatorAddress, ubDel.ValidatorAddress)
|
|
|
|
require.Equal(t, 1, len(ubDel.Entries))
|
|
|
|
|
|
|
|
//
|
|
|
|
// not found: query unbonding delegation by delegator and validator
|
|
|
|
//
|
2020-09-25 03:25:37 -07:00
|
|
|
queryValidatorParams = types.QueryDelegatorValidatorRequest{DelegatorAddr: addrAcc2.String(), ValidatorAddr: val1.GetOperator().String()}
|
2020-02-20 09:10:37 -08:00
|
|
|
bz, errRes = cdc.MarshalJSON(queryValidatorParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/unbondingDelegation",
|
|
|
|
Data: bz,
|
|
|
|
}
|
2020-02-26 07:35:04 -08:00
|
|
|
_, err = querier(ctx, []string{types.QueryUnbondingDelegation}, query)
|
2020-02-20 09:10:37 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
//
|
|
|
|
// found: query unbonding delegation by delegator and validator
|
|
|
|
//
|
|
|
|
queryDelegatorParams := types.NewQueryDelegatorParams(addrAcc1)
|
|
|
|
bz, errRes = cdc.MarshalJSON(queryDelegatorParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/delegatorUnbondingDelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
res, err = querier(ctx, []string{types.QueryDelegatorUnbondingDelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
var ubDels []types.UnbondingDelegation
|
|
|
|
require.NoError(t, cdc.UnmarshalJSON(res, &ubDels))
|
|
|
|
require.Equal(t, 1, len(ubDels))
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, addrAcc1.String(), ubDels[0].DelegatorAddress)
|
2020-02-20 09:10:37 -08:00
|
|
|
require.Equal(t, val1.OperatorAddress, ubDels[0].ValidatorAddress)
|
|
|
|
|
|
|
|
//
|
|
|
|
// not found: query unbonding delegation by delegator and validator
|
|
|
|
//
|
|
|
|
queryDelegatorParams = types.NewQueryDelegatorParams(addrAcc2)
|
|
|
|
bz, errRes = cdc.MarshalJSON(queryDelegatorParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/delegatorUnbondingDelegations",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
res, err = querier(ctx, []string{types.QueryDelegatorUnbondingDelegations}, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
require.NoError(t, cdc.UnmarshalJSON(res, &ubDels))
|
|
|
|
require.Equal(t, 0, len(ubDels))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryHistoricalInfo(t *testing.T) {
|
2020-02-26 06:57:41 -08:00
|
|
|
cdc, app, ctx := createTestInput()
|
2020-10-23 05:07:52 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(cdc)
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.StakingKeeper, legacyQuerierCdc.LegacyAmino)
|
2020-02-20 09:10:37 -08:00
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 2, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
|
2020-02-20 09:10:37 -08:00
|
|
|
addrAcc1, addrAcc2 := addrs[0], addrs[1]
|
|
|
|
addrVal1, addrVal2 := sdk.ValAddress(addrAcc1), sdk.ValAddress(addrAcc2)
|
|
|
|
|
|
|
|
// Create Validators and Delegation
|
2020-10-23 05:07:52 -07:00
|
|
|
val1 := teststaking.NewValidator(t, addrVal1, PKs[0])
|
|
|
|
val2 := teststaking.NewValidator(t, addrVal2, PKs[1])
|
2020-02-20 09:10:37 -08:00
|
|
|
vals := []types.Validator{val1, val2}
|
|
|
|
app.StakingKeeper.SetValidator(ctx, val1)
|
|
|
|
app.StakingKeeper.SetValidator(ctx, val2)
|
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header := tmproto.Header{
|
2020-02-20 09:10:37 -08:00
|
|
|
ChainID: "HelloChain",
|
|
|
|
Height: 5,
|
|
|
|
}
|
2021-01-28 05:54:39 -08:00
|
|
|
hi := types.NewHistoricalInfo(header, vals, app.StakingKeeper.PowerReduction(ctx))
|
2020-10-23 05:07:52 -07:00
|
|
|
app.StakingKeeper.SetHistoricalInfo(ctx, 5, &hi)
|
2020-02-20 09:10:37 -08:00
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
queryHistoricalParams := types.QueryHistoricalInfoRequest{Height: 4}
|
2020-02-20 09:10:37 -08:00
|
|
|
bz, errRes := cdc.MarshalJSON(queryHistoricalParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: "/custom/staking/historicalInfo",
|
|
|
|
Data: bz,
|
|
|
|
}
|
|
|
|
res, err := querier(ctx, []string{types.QueryHistoricalInfo}, query)
|
|
|
|
require.Error(t, err, "Invalid query passed")
|
|
|
|
require.Nil(t, res, "Invalid query returned non-nil result")
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
queryHistoricalParams = types.QueryHistoricalInfoRequest{Height: 5}
|
2020-02-20 09:10:37 -08:00
|
|
|
bz, errRes = cdc.MarshalJSON(queryHistoricalParams)
|
|
|
|
require.NoError(t, errRes)
|
|
|
|
query.Data = bz
|
|
|
|
res, err = querier(ctx, []string{types.QueryHistoricalInfo}, query)
|
|
|
|
require.NoError(t, err, "Valid query passed")
|
|
|
|
require.NotNil(t, res, "Valid query returned nil result")
|
|
|
|
|
|
|
|
var recv types.HistoricalInfo
|
|
|
|
require.NoError(t, cdc.UnmarshalJSON(res, &recv))
|
|
|
|
require.Equal(t, hi, recv, "HistoricalInfo query returned wrong result")
|
|
|
|
}
|