2019-01-11 12:08:01 -08:00
|
|
|
package staking
|
2018-07-09 19:51:13 -07:00
|
|
|
|
|
|
|
import (
|
2018-07-18 23:39:40 -07:00
|
|
|
"fmt"
|
2018-07-09 19:51:13 -07:00
|
|
|
"testing"
|
|
|
|
|
2018-10-07 21:12:37 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
2018-07-09 19:51:13 -07:00
|
|
|
|
2018-10-07 21:12:37 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-07-18 23:39:40 -07:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2018-07-09 19:51:13 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
keep "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-07-09 19:51:13 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInitGenesis(t *testing.T) {
|
2019-06-28 13:11:27 -07:00
|
|
|
ctx, accKeeper, keeper, supplyKeeper := keep.CreateTestInput(t, false, 1000)
|
2018-07-09 19:51:13 -07:00
|
|
|
|
2019-06-12 08:57:47 -07:00
|
|
|
valTokens := sdk.TokensFromConsensusPower(1)
|
2018-07-09 19:51:13 -07:00
|
|
|
|
|
|
|
params := keeper.GetParams(ctx)
|
2018-11-08 16:28:28 -08:00
|
|
|
validators := make([]Validator, 2)
|
2018-07-09 19:51:13 -07:00
|
|
|
var delegations []Delegation
|
|
|
|
|
2018-07-18 13:09:40 -07:00
|
|
|
// initialize the validators
|
2019-02-25 07:16:52 -08:00
|
|
|
validators[0].OperatorAddress = sdk.ValAddress(keep.Addrs[0])
|
2018-11-08 16:28:28 -08:00
|
|
|
validators[0].ConsPubKey = keep.PKs[0]
|
2019-02-05 21:30:48 -08:00
|
|
|
validators[0].Description = NewDescription("hoop", "", "", "")
|
2018-07-18 23:39:40 -07:00
|
|
|
validators[0].Status = sdk.Bonded
|
2019-02-05 21:30:48 -08:00
|
|
|
validators[0].Tokens = valTokens
|
2019-02-21 00:05:31 -08:00
|
|
|
validators[0].DelegatorShares = valTokens.ToDec()
|
2019-02-25 07:16:52 -08:00
|
|
|
validators[1].OperatorAddress = sdk.ValAddress(keep.Addrs[1])
|
2018-11-08 16:28:28 -08:00
|
|
|
validators[1].ConsPubKey = keep.PKs[1]
|
2019-02-05 21:30:48 -08:00
|
|
|
validators[1].Description = NewDescription("bloop", "", "", "")
|
2018-07-18 23:39:40 -07:00
|
|
|
validators[1].Status = sdk.Bonded
|
2019-02-05 21:30:48 -08:00
|
|
|
validators[1].Tokens = valTokens
|
2019-02-21 00:05:31 -08:00
|
|
|
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 := InitGenesis(ctx, keeper, accKeeper, supplyKeeper, genesisState)
|
2018-07-18 13:09:40 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
actualGenesis := ExportGenesis(ctx, keeper)
|
2018-09-13 14:23:44 -07:00
|
|
|
require.Equal(t, genesisState.Params, actualGenesis.Params)
|
2019-02-13 15:01:50 -08:00
|
|
|
require.Equal(t, genesisState.Delegations, actualGenesis.Delegations)
|
2018-09-13 14:23:44 -07:00
|
|
|
require.EqualValues(t, keeper.GetAllValidators(ctx), actualGenesis.Validators)
|
|
|
|
|
2018-07-27 11:01:06 -07:00
|
|
|
// now make sure the validators are bonded and intra-tx counters are correct
|
2018-08-30 21:06:44 -07:00
|
|
|
resVal, found := keeper.GetValidator(ctx, sdk.ValAddress(keep.Addrs[0]))
|
2018-07-18 13:09:40 -07:00
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, sdk.Bonded, resVal.Status)
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
resVal, found = keeper.GetValidator(ctx, sdk.ValAddress(keep.Addrs[1]))
|
2018-07-18 13:09:40 -07:00
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, sdk.Bonded, resVal.Status)
|
2018-07-18 23:39:40 -07:00
|
|
|
|
2018-10-03 08:48:23 -07:00
|
|
|
abcivals := make([]abci.ValidatorUpdate, len(vals))
|
2018-07-18 23:39:40 -07:00
|
|
|
for i, val := range validators {
|
2018-10-03 08:48:23 -07:00
|
|
|
abcivals[i] = val.ABCIValidatorUpdate()
|
2018-07-18 23:39:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, abcivals, vals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitGenesisLargeValidatorSet(t *testing.T) {
|
|
|
|
size := 200
|
|
|
|
require.True(t, size > 100)
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
ctx, accKeeper, keeper, supplyKeeper := keep.CreateTestInput(t, false, 1000)
|
2018-07-18 23:39:40 -07:00
|
|
|
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
delegations := []Delegation{}
|
|
|
|
validators := make([]Validator, size)
|
|
|
|
|
|
|
|
for i := range validators {
|
2019-02-05 21:30:48 -08:00
|
|
|
validators[i] = NewValidator(sdk.ValAddress(keep.Addrs[i]),
|
|
|
|
keep.PKs[i], NewDescription(fmt.Sprintf("#%d", i), "", "", ""))
|
2018-07-18 23:39:40 -07:00
|
|
|
|
|
|
|
validators[i].Status = sdk.Bonded
|
2019-02-05 21:30:48 -08:00
|
|
|
|
2019-06-12 08:57:47 -07:00
|
|
|
tokens := sdk.TokensFromConsensusPower(1)
|
2018-07-18 23:39:40 -07:00
|
|
|
if i < 100 {
|
2019-06-12 08:57:47 -07:00
|
|
|
tokens = sdk.TokensFromConsensusPower(2)
|
2018-07-18 23:39:40 -07:00
|
|
|
}
|
2019-02-05 21:30:48 -08:00
|
|
|
validators[i].Tokens = tokens
|
2019-02-21 00:05:31 -08:00
|
|
|
validators[i].DelegatorShares = tokens.ToDec()
|
2018-07-18 23:39:40 -07:00
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
genesisState := types.NewGenesisState(params, validators, delegations)
|
|
|
|
vals := InitGenesis(ctx, keeper, accKeeper, supplyKeeper, genesisState)
|
2018-07-18 23:39:40 -07:00
|
|
|
|
2018-10-03 08:48:23 -07:00
|
|
|
abcivals := make([]abci.ValidatorUpdate, 100)
|
2018-07-18 23:39:40 -07:00
|
|
|
for i, val := range validators[:100] {
|
2018-10-03 08:48:23 -07:00
|
|
|
abcivals[i] = val.ABCIValidatorUpdate()
|
2018-07-18 23:39:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, abcivals, vals)
|
2018-07-09 19:51:13 -07:00
|
|
|
}
|
2018-10-07 21:12:37 -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("", "", "", ""))
|
2019-01-02 12:29:47 -08:00
|
|
|
genValidators1[0].Tokens = sdk.OneInt()
|
2018-10-07 21:12:37 -07:00
|
|
|
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) {
|
|
|
|
(*data).Validators = genValidators1
|
|
|
|
(*data).Validators = append((*data).Validators, genValidators1[0])
|
|
|
|
}, true},
|
|
|
|
{"no delegator shares", func(data *types.GenesisState) {
|
|
|
|
(*data).Validators = genValidators1
|
|
|
|
(*data).Validators[0].DelegatorShares = sdk.ZeroDec()
|
|
|
|
}, true},
|
|
|
|
{"jailed and bonded validator", func(data *types.GenesisState) {
|
|
|
|
(*data).Validators = genValidators1
|
|
|
|
(*data).Validators[0].Jailed = true
|
|
|
|
(*data).Validators[0].Status = sdk.Bonded
|
|
|
|
}, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
genesisState := types.DefaultGenesisState()
|
|
|
|
tt.mutate(&genesisState)
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, ValidateGenesis(genesisState))
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, ValidateGenesis(genesisState))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|