cosmos-sdk/x/staking/genesis_test.go

173 lines
5.6 KiB
Go
Raw Normal View History

package staking_test
2018-07-09 19:51:13 -07:00
import (
"fmt"
2018-07-09 19:51:13 -07:00
"testing"
"github.com/stretchr/testify/assert"
2018-12-10 06:27:25 -08:00
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
2020-02-26 07:05:12 -08:00
"github.com/tendermint/tendermint/crypto/ed25519"
2018-12-10 06:27:25 -08:00
"github.com/cosmos/cosmos-sdk/simapp"
2018-07-09 19:51:13 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/staking"
2019-01-11 12:08:01 -08:00
"github.com/cosmos/cosmos-sdk/x/staking/types"
2018-07-09 19:51:13 -07:00
)
func bootstrapGenesisTest(t *testing.T, power int64, numAddrs int) (*simapp.SimApp, sdk.Context, []sdk.AccAddress) {
_, app, ctx := getBaseSimappWithCustomKeeper()
addrDels, _ := generateAddresses(app, ctx, numAddrs, 10000)
amt := sdk.TokensFromConsensusPower(power)
totalSupply := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), amt.MulRaw(int64(len(addrDels)))))
notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx)
err := app.BankKeeper.SetBalances(ctx, notBondedPool.GetAddress(), totalSupply)
require.NoError(t, err)
app.AccountKeeper.SetModuleAccount(ctx, notBondedPool)
app.BankKeeper.SetSupply(ctx, banktypes.NewSupply(totalSupply))
return app, ctx, addrDels
}
2018-07-09 19:51:13 -07:00
func TestInitGenesis(t *testing.T) {
app, ctx, addrs := bootstrapGenesisTest(t, 1000, 10)
2018-07-09 19:51:13 -07:00
valTokens := sdk.TokensFromConsensusPower(1)
2018-07-09 19:51:13 -07:00
params := app.StakingKeeper.GetParams(ctx)
validators := make([]types.Validator, 2)
var delegations []types.Delegation
2018-07-09 19:51:13 -07:00
2020-02-25 06:27:51 -08:00
pk0, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, PKs[0])
require.NoError(t, err)
2020-02-25 06:27:51 -08:00
pk1, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, PKs[1])
require.NoError(t, err)
// initialize the validators
validators[0].OperatorAddress = sdk.ValAddress(addrs[0])
validators[0].ConsensusPubkey = pk0
validators[0].Description = types.NewDescription("hoop", "", "", "", "")
validators[0].Status = sdk.Bonded
validators[0].Tokens = valTokens
validators[0].DelegatorShares = valTokens.ToDec()
validators[1].OperatorAddress = sdk.ValAddress(addrs[1])
validators[1].ConsensusPubkey = pk1
validators[1].Description = types.NewDescription("bloop", "", "", "", "")
validators[1].Status = sdk.Bonded
validators[1].Tokens = valTokens
validators[1].DelegatorShares = valTokens.ToDec()
2018-07-09 19:51:13 -07:00
2019-06-28 13:11:27 -07:00
genesisState := types.NewGenesisState(params, validators, delegations)
vals := staking.InitGenesis(ctx, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, genesisState)
actualGenesis := staking.ExportGenesis(ctx, app.StakingKeeper)
Merge PR #2249: Staking Querier pt1 * Cherry picked commits from prev branch * Added new keepers for querier functionalities * Renaming * Fixed gov errors and messages * Added Querier to stake and app * Update delegation keepers * REST Queriers not working * Fix marshalling error * Querier tests working * Pool and params working * sdk.NewCoin for test handler * Refactor and renaming * Update LCD queries and added more tests for queriers * use sdk.NewCoin * Delegator summary query and tests * Added more tests for keeper * Update PENDING.md * Update stake rest query * Format and replaced panics for sdk.Error * Refactor and addressed comments from Sunny and Aleks * Fixed some of the errors produced by addr type change * Fixed remaining errors * Updated and fixed lite tests * JSON Header and consistency on errors * Increased cov for genesis * Added comment for maxRetrieve param in keepers * Comment on DelegationWithoutDec * Bech32Validator Keepers * Changed Bech validator * Updated remaining tests and bech32 validator * Addressed most of Rigel's comments * Updated tests and types * Make codec to be unexported from keeper * Moved logic to query_utils and updated tests * Fix linter err and PENDING * Fix err * Fix err * Fixed tests * Update PENDING description * Update UpdateBondedValidatorsFull * Update iterator * defer iterator.Close() * delete comment * Address some of Aleks comments, need to fix tests * export querier * Fixed tests * Address Rigel's comments * More tests * return error for GetDelegatorValidator * Fixed conflicts * Fix linter warnings * Address @rigelrozanski comments * Delete comments * wire ––> codec
2018-09-13 14:23:44 -07:00
require.Equal(t, genesisState.Params, actualGenesis.Params)
require.Equal(t, genesisState.Delegations, actualGenesis.Delegations)
require.EqualValues(t, app.StakingKeeper.GetAllValidators(ctx), actualGenesis.Validators)
Merge PR #2249: Staking Querier pt1 * Cherry picked commits from prev branch * Added new keepers for querier functionalities * Renaming * Fixed gov errors and messages * Added Querier to stake and app * Update delegation keepers * REST Queriers not working * Fix marshalling error * Querier tests working * Pool and params working * sdk.NewCoin for test handler * Refactor and renaming * Update LCD queries and added more tests for queriers * use sdk.NewCoin * Delegator summary query and tests * Added more tests for keeper * Update PENDING.md * Update stake rest query * Format and replaced panics for sdk.Error * Refactor and addressed comments from Sunny and Aleks * Fixed some of the errors produced by addr type change * Fixed remaining errors * Updated and fixed lite tests * JSON Header and consistency on errors * Increased cov for genesis * Added comment for maxRetrieve param in keepers * Comment on DelegationWithoutDec * Bech32Validator Keepers * Changed Bech validator * Updated remaining tests and bech32 validator * Addressed most of Rigel's comments * Updated tests and types * Make codec to be unexported from keeper * Moved logic to query_utils and updated tests * Fix linter err and PENDING * Fix err * Fix err * Fixed tests * Update PENDING description * Update UpdateBondedValidatorsFull * Update iterator * defer iterator.Close() * delete comment * Address some of Aleks comments, need to fix tests * export querier * Fixed tests * Address Rigel's comments * More tests * return error for GetDelegatorValidator * Fixed conflicts * Fix linter warnings * Address @rigelrozanski comments * Delete comments * wire ––> codec
2018-09-13 14:23:44 -07:00
// Ensure validators have addresses.
for _, val := range staking.WriteValidators(ctx, app.StakingKeeper) {
require.NotEmpty(t, val.Address)
}
2018-07-27 11:01:06 -07:00
// now make sure the validators are bonded and intra-tx counters are correct
resVal, found := app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addrs[0]))
require.True(t, found)
require.Equal(t, sdk.Bonded, resVal.Status)
resVal, found = app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addrs[1]))
require.True(t, found)
require.Equal(t, sdk.Bonded, resVal.Status)
abcivals := make([]abci.ValidatorUpdate, len(vals))
for i, val := range validators {
abcivals[i] = val.ABCIValidatorUpdate()
}
require.Equal(t, abcivals, vals)
}
func TestInitGenesisLargeValidatorSet(t *testing.T) {
size := 200
require.True(t, size > 100)
app, ctx, addrs := bootstrapGenesisTest(t, 1000, 200)
params := app.StakingKeeper.GetParams(ctx)
delegations := []types.Delegation{}
validators := make([]types.Validator, size)
for i := range validators {
validators[i] = types.NewValidator(sdk.ValAddress(addrs[i]),
PKs[i], types.NewDescription(fmt.Sprintf("#%d", i), "", "", "", ""))
validators[i].Status = sdk.Bonded
tokens := sdk.TokensFromConsensusPower(1)
if i < 100 {
tokens = sdk.TokensFromConsensusPower(2)
}
validators[i].Tokens = tokens
validators[i].DelegatorShares = tokens.ToDec()
}
2019-06-28 13:11:27 -07:00
genesisState := types.NewGenesisState(params, validators, delegations)
vals := staking.InitGenesis(ctx, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, genesisState)
abcivals := make([]abci.ValidatorUpdate, 100)
for i, val := range validators[:100] {
abcivals[i] = val.ABCIValidatorUpdate()
}
require.Equal(t, abcivals, vals)
2018-07-09 19:51:13 -07:00
}
func TestValidateGenesis(t *testing.T) {
genValidators1 := make([]types.Validator, 1, 5)
pk := ed25519.GenPrivKey().PubKey()
genValidators1[0] = types.NewValidator(sdk.ValAddress(pk.Address()), pk, types.NewDescription("", "", "", "", ""))
genValidators1[0].Tokens = sdk.OneInt()
genValidators1[0].DelegatorShares = sdk.OneDec()
tests := []struct {
name string
mutate func(*types.GenesisState)
wantErr bool
}{
{"default", func(*types.GenesisState) {}, false},
// validate genesis validators
{"duplicate validator", func(data *types.GenesisState) {
2019-08-19 09:06:27 -07:00
data.Validators = genValidators1
data.Validators = append(data.Validators, genValidators1[0])
}, true},
{"no delegator shares", func(data *types.GenesisState) {
2019-08-19 09:06:27 -07:00
data.Validators = genValidators1
data.Validators[0].DelegatorShares = sdk.ZeroDec()
}, true},
{"jailed and bonded validator", func(data *types.GenesisState) {
2019-08-19 09:06:27 -07:00
data.Validators = genValidators1
data.Validators[0].Jailed = true
data.Validators[0].Status = sdk.Bonded
}, true},
}
for _, tt := range tests {
2019-10-17 06:47:35 -07:00
tt := tt
t.Run(tt.name, func(t *testing.T) {
genesisState := types.DefaultGenesisState()
tt.mutate(&genesisState)
if tt.wantErr {
assert.Error(t, staking.ValidateGenesis(genesisState))
} else {
assert.NoError(t, staking.ValidateGenesis(genesisState))
}
})
}
}