2018-05-12 15:22:59 -07:00
|
|
|
package stake
|
|
|
|
|
2018-06-06 09:38:13 -07:00
|
|
|
import (
|
2018-07-18 23:39:40 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2018-06-06 09:38:13 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-06-26 19:00:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
2018-07-09 19:51:13 -07:00
|
|
|
"github.com/pkg/errors"
|
2018-06-06 09:38:13 -07:00
|
|
|
)
|
2018-05-12 15:22:59 -07:00
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// InitGenesis sets the pool and parameters for the provided keeper and
|
|
|
|
// initializes the IntraTxCounter. For each validator in data, it sets that
|
|
|
|
// validator in the keeper along with manually setting the indexes. In
|
|
|
|
// addition, it also sets any delegations found in data. Finally, it updates
|
|
|
|
// the bonded validators.
|
2018-07-18 23:39:40 -07:00
|
|
|
// Returns final validator set after applying all declaration and delegations
|
2018-10-03 08:48:23 -07:00
|
|
|
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res []abci.ValidatorUpdate, err error) {
|
2018-10-05 05:11:36 -07:00
|
|
|
|
|
|
|
// We need to pretend to be "one block before genesis" so that e.g. slashing periods
|
|
|
|
// are correctly initialized for the validator set one-block offset - the first TM block
|
|
|
|
// is at height 0, so state updates applied from genesis.json are in block -1.
|
|
|
|
ctx = ctx.WithBlockHeight(-1)
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper.SetPool(ctx, data.Pool)
|
2018-10-03 09:37:06 -07:00
|
|
|
keeper.SetParams(ctx, data.Params)
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper.InitIntraTxCounter(ctx)
|
2018-05-30 18:28:02 -07:00
|
|
|
|
2018-07-18 13:09:40 -07:00
|
|
|
for i, validator := range data.Validators {
|
2018-07-27 11:01:06 -07:00
|
|
|
validator.BondIntraTxCounter = int16(i) // set the intra-tx counter to the order the validators are presented
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper.SetValidator(ctx, validator)
|
2018-05-30 18:28:02 -07:00
|
|
|
|
2018-07-13 13:46:14 -07:00
|
|
|
if validator.Tokens.IsZero() {
|
2018-07-18 23:39:40 -07:00
|
|
|
return res, errors.Errorf("genesis validator cannot have zero pool shares, validator: %v", validator)
|
2018-07-09 19:51:13 -07:00
|
|
|
}
|
|
|
|
if validator.DelegatorShares.IsZero() {
|
2018-07-18 23:39:40 -07:00
|
|
|
return res, errors.Errorf("genesis validator cannot have zero delegator shares, validator: %v", validator)
|
2018-07-09 19:51:13 -07:00
|
|
|
}
|
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// Manually set indices for the first time
|
2018-09-24 21:09:31 -07:00
|
|
|
keeper.SetValidatorByConsAddr(ctx, validator)
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper.SetValidatorByPowerIndex(ctx, validator, data.Pool)
|
2018-05-12 15:22:59 -07:00
|
|
|
}
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-05-12 15:22:59 -07:00
|
|
|
for _, bond := range data.Bonds {
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper.SetDelegation(ctx, bond)
|
2018-05-12 15:22:59 -07:00
|
|
|
}
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
res = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
2018-07-18 23:39:40 -07:00
|
|
|
return
|
2018-05-12 15:22:59 -07:00
|
|
|
}
|
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// WriteGenesis returns a GenesisState for a given context and keeper. The
|
|
|
|
// GenesisState will contain the pool, params, validators, and bonds found in
|
|
|
|
// the keeper.
|
2018-06-26 19:00:12 -07:00
|
|
|
func WriteGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
|
|
|
|
pool := keeper.GetPool(ctx)
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
validators := keeper.GetAllValidators(ctx)
|
|
|
|
bonds := keeper.GetAllDelegations(ctx)
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
return types.GenesisState{
|
2018-07-03 21:21:36 -07:00
|
|
|
Pool: pool,
|
|
|
|
Params: params,
|
|
|
|
Validators: validators,
|
|
|
|
Bonds: bonds,
|
2018-05-12 15:22:59 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-06 09:38:13 -07:00
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// WriteValidators returns a slice of bonded genesis validators.
|
2018-06-26 19:00:12 -07:00
|
|
|
func WriteValidators(ctx sdk.Context, keeper Keeper) (vals []tmtypes.GenesisValidator) {
|
|
|
|
keeper.IterateValidatorsBonded(ctx, func(_ int64, validator sdk.Validator) (stop bool) {
|
2018-06-06 09:38:13 -07:00
|
|
|
vals = append(vals, tmtypes.GenesisValidator{
|
2018-09-24 21:09:31 -07:00
|
|
|
PubKey: validator.GetConsPubKey(),
|
2018-07-02 08:57:33 -07:00
|
|
|
Power: validator.GetPower().RoundInt64(),
|
2018-06-06 09:38:13 -07:00
|
|
|
Name: validator.GetMoniker(),
|
|
|
|
})
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-06-06 09:38:13 -07:00
|
|
|
return false
|
|
|
|
})
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-06-06 09:38:13 -07:00
|
|
|
return
|
|
|
|
}
|