2018-07-09 19:51:13 -07:00
|
|
|
package stake
|
|
|
|
|
|
|
|
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"
|
|
|
|
keep "github.com/cosmos/cosmos-sdk/x/stake/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInitGenesis(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
|
|
|
|
pool := keeper.GetPool(ctx)
|
2019-01-02 12:29:47 -08:00
|
|
|
pool.BondedTokens = sdk.NewInt(2)
|
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
|
2018-11-08 16:28:28 -08:00
|
|
|
validators[0].OperatorAddr = sdk.ValAddress(keep.Addrs[0])
|
|
|
|
validators[0].ConsPubKey = keep.PKs[0]
|
|
|
|
validators[0].Description = Description{Moniker: "hoop"}
|
2018-07-18 23:39:40 -07:00
|
|
|
validators[0].Status = sdk.Bonded
|
2019-01-02 12:29:47 -08:00
|
|
|
validators[0].Tokens = sdk.OneInt()
|
2018-08-14 17:15:02 -07:00
|
|
|
validators[0].DelegatorShares = sdk.OneDec()
|
2018-11-08 16:28:28 -08:00
|
|
|
validators[1].OperatorAddr = sdk.ValAddress(keep.Addrs[1])
|
|
|
|
validators[1].ConsPubKey = keep.PKs[1]
|
|
|
|
validators[1].Description = Description{Moniker: "bloop"}
|
2018-07-18 23:39:40 -07:00
|
|
|
validators[1].Status = sdk.Bonded
|
2019-01-02 12:29:47 -08:00
|
|
|
validators[1].Tokens = sdk.OneInt()
|
2018-08-14 17:15:02 -07:00
|
|
|
validators[1].DelegatorShares = sdk.OneDec()
|
2018-07-09 19:51:13 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
genesisState := types.NewGenesisState(pool, params, validators, delegations)
|
2018-07-18 23:39:40 -07:00
|
|
|
vals, err := InitGenesis(ctx, keeper, genesisState)
|
2018-07-09 19:51:13 -07:00
|
|
|
require.NoError(t, err)
|
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.Pool, actualGenesis.Pool)
|
|
|
|
require.Equal(t, genesisState.Params, actualGenesis.Params)
|
|
|
|
require.Equal(t, genesisState.Bonds, actualGenesis.Bonds)
|
|
|
|
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)
|
|
|
|
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
|
|
|
|
// Assigning 2 to the first 100 vals, 1 to the rest
|
|
|
|
pool := keeper.GetPool(ctx)
|
2019-01-02 12:29:47 -08:00
|
|
|
pool.BondedTokens = sdk.NewInt(int64(200 + (size - 100)))
|
2018-07-18 23:39:40 -07:00
|
|
|
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
delegations := []Delegation{}
|
|
|
|
validators := make([]Validator, size)
|
|
|
|
|
|
|
|
for i := range validators {
|
2018-08-30 21:06:44 -07:00
|
|
|
validators[i] = NewValidator(sdk.ValAddress(keep.Addrs[i]), keep.PKs[i], Description{Moniker: fmt.Sprintf("#%d", i)})
|
2018-07-18 23:39:40 -07:00
|
|
|
|
|
|
|
validators[i].Status = sdk.Bonded
|
|
|
|
if i < 100 {
|
2019-01-02 12:29:47 -08:00
|
|
|
validators[i].Tokens = sdk.NewInt(2)
|
2018-08-14 17:15:02 -07:00
|
|
|
validators[i].DelegatorShares = sdk.NewDec(2)
|
2018-07-18 23:39:40 -07:00
|
|
|
} else {
|
2019-01-02 12:29:47 -08:00
|
|
|
validators[i].Tokens = sdk.OneInt()
|
2018-08-14 17:15:02 -07:00
|
|
|
validators[i].DelegatorShares = sdk.OneDec()
|
2018-07-18 23:39:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
genesisState := types.NewGenesisState(pool, params, validators, delegations)
|
|
|
|
vals, err := InitGenesis(ctx, keeper, genesisState)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|