2019-05-18 01:42:24 -07:00
|
|
|
package simapp
|
2018-11-26 04:21:23 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-02-04 08:42:48 -08:00
|
|
|
"log"
|
2018-11-26 04:21:23 -08:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
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
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-06-12 07:25:12 -07:00
|
|
|
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2019-01-16 13:38:05 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2019-06-05 10:42:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/exported"
|
2020-06-12 07:54:37 -07:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-11-26 04:21:23 -08:00
|
|
|
)
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
// ExportAppStateAndValidators exports the state of the application for a genesis
|
|
|
|
// file.
|
|
|
|
func (app *SimApp) ExportAppStateAndValidators(
|
|
|
|
forZeroHeight bool, jailWhiteList []string,
|
2020-04-16 08:10:39 -07:00
|
|
|
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, cp *abci.ConsensusParams, err error) {
|
2018-11-26 04:21:23 -08:00
|
|
|
|
|
|
|
// as if they could withdraw from the start of the next block
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
|
2018-11-26 04:21:23 -08:00
|
|
|
|
|
|
|
if forZeroHeight {
|
2019-02-04 08:42:48 -08:00
|
|
|
app.prepForZeroHeightGenesis(ctx, jailWhiteList)
|
2018-11-26 04:21:23 -08:00
|
|
|
}
|
|
|
|
|
2020-08-11 07:22:30 -07:00
|
|
|
genState := app.mm.ExportGenesis(ctx, app.appCodec)
|
|
|
|
appState, err = json.MarshalIndent(genState, "", " ")
|
2018-11-26 04:21:23 -08:00
|
|
|
if err != nil {
|
2020-04-16 08:10:39 -07:00
|
|
|
return nil, nil, nil, err
|
2018-11-26 04:21:23 -08:00
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2019-08-13 13:59:01 -07:00
|
|
|
validators = staking.WriteValidators(ctx, app.StakingKeeper)
|
2020-04-16 08:10:39 -07:00
|
|
|
return appState, validators, app.BaseApp.GetConsensusParams(ctx), nil
|
2018-11-26 04:21:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare for fresh start at zero height
|
2019-05-16 08:25:32 -07:00
|
|
|
// NOTE zero height genesis is a temporary feature which will be deprecated
|
|
|
|
// in favour of export at a block height
|
2019-05-18 01:42:24 -07:00
|
|
|
func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) {
|
2019-02-04 08:42:48 -08:00
|
|
|
applyWhiteList := false
|
|
|
|
|
|
|
|
//Check if there is a whitelist
|
|
|
|
if len(jailWhiteList) > 0 {
|
|
|
|
applyWhiteList = true
|
|
|
|
}
|
|
|
|
|
|
|
|
whiteListMap := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, addr := range jailWhiteList {
|
|
|
|
_, err := sdk.ValAddressFromBech32(addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
whiteListMap[addr] = true
|
|
|
|
}
|
2018-11-26 04:21:23 -08:00
|
|
|
|
2018-12-04 10:17:02 -08:00
|
|
|
/* Just to be safe, assert the invariants on current state. */
|
2019-08-13 13:59:01 -07:00
|
|
|
app.CrisisKeeper.AssertInvariants(ctx)
|
2018-11-26 04:21:23 -08:00
|
|
|
|
|
|
|
/* Handle fee distribution state. */
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// withdraw all validator commission
|
2019-08-13 13:59:01 -07:00
|
|
|
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) {
|
|
|
|
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
|
2018-11-26 04:21:23 -08:00
|
|
|
return false
|
2019-01-16 13:38:05 -08:00
|
|
|
})
|
2018-11-26 04:21:23 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// withdraw all delegator rewards
|
2019-08-13 13:59:01 -07:00
|
|
|
dels := app.StakingKeeper.GetAllDelegations(ctx)
|
2019-01-16 13:38:05 -08:00
|
|
|
for _, delegation := range dels {
|
2019-08-13 13:59:01 -07:00
|
|
|
_, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress)
|
2018-11-26 04:21:23 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// clear validator slash events
|
2019-08-13 13:59:01 -07:00
|
|
|
app.DistrKeeper.DeleteAllValidatorSlashEvents(ctx)
|
2018-12-04 10:17:02 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// clear validator historical rewards
|
2019-08-13 13:59:01 -07:00
|
|
|
app.DistrKeeper.DeleteAllValidatorHistoricalRewards(ctx)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
// set context height to zero
|
|
|
|
height := ctx.BlockHeight()
|
|
|
|
ctx = ctx.WithBlockHeight(0)
|
|
|
|
|
|
|
|
// reinitialize all validators
|
2019-08-13 13:59:01 -07:00
|
|
|
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) {
|
2019-03-06 10:54:12 -08:00
|
|
|
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
|
2020-02-11 06:58:37 -08:00
|
|
|
scraps := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
|
2019-08-13 13:59:01 -07:00
|
|
|
feePool := app.DistrKeeper.GetFeePool(ctx)
|
2020-01-03 12:44:53 -08:00
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
|
2019-08-13 13:59:01 -07:00
|
|
|
app.DistrKeeper.SetFeePool(ctx, feePool)
|
2019-03-06 10:54:12 -08:00
|
|
|
|
2019-08-13 13:59:01 -07:00
|
|
|
app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
|
2018-12-04 10:17:02 -08:00
|
|
|
return false
|
|
|
|
})
|
2018-11-26 04:21:23 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// reinitialize all delegations
|
|
|
|
for _, del := range dels {
|
2019-08-13 13:59:01 -07:00
|
|
|
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.DelegatorAddress, del.ValidatorAddress)
|
|
|
|
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.DelegatorAddress, del.ValidatorAddress)
|
2018-11-26 04:21:23 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// reset context height
|
|
|
|
ctx = ctx.WithBlockHeight(height)
|
2018-11-26 04:21:23 -08:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
/* Handle staking state. */
|
2018-11-26 04:21:23 -08:00
|
|
|
|
2018-12-20 12:40:27 -08:00
|
|
|
// iterate through redelegations, reset creation height
|
2020-06-12 07:54:37 -07:00
|
|
|
app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
|
2019-01-16 02:35:18 -08:00
|
|
|
for i := range red.Entries {
|
|
|
|
red.Entries[i].CreationHeight = 0
|
|
|
|
}
|
2019-08-13 13:59:01 -07:00
|
|
|
app.StakingKeeper.SetRedelegation(ctx, red)
|
2018-12-20 12:40:27 -08:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// iterate through unbonding delegations, reset creation height
|
2020-06-12 07:54:37 -07:00
|
|
|
app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
|
2019-01-16 02:35:18 -08:00
|
|
|
for i := range ubd.Entries {
|
|
|
|
ubd.Entries[i].CreationHeight = 0
|
|
|
|
}
|
2019-08-13 13:59:01 -07:00
|
|
|
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
|
2018-12-20 12:40:27 -08:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2019-01-02 15:57:13 -08:00
|
|
|
// Iterate through validators by power descending, reset bond heights, and
|
|
|
|
// update bond intra-tx counters.
|
2020-06-12 07:54:37 -07:00
|
|
|
store := ctx.KVStore(app.keys[stakingtypes.StoreKey])
|
|
|
|
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
|
2018-11-26 04:21:23 -08:00
|
|
|
counter := int16(0)
|
2019-01-02 15:57:13 -08:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
for ; iter.Valid(); iter.Next() {
|
2018-12-20 12:40:27 -08:00
|
|
|
addr := sdk.ValAddress(iter.Key()[1:])
|
2019-08-13 13:59:01 -07:00
|
|
|
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
|
2018-11-26 04:21:23 -08:00
|
|
|
if !found {
|
|
|
|
panic("expected validator, not found")
|
|
|
|
}
|
2019-01-02 15:57:13 -08:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
validator.UnbondingHeight = 0
|
2019-02-04 08:42:48 -08:00
|
|
|
if applyWhiteList && !whiteListMap[addr.String()] {
|
|
|
|
validator.Jailed = true
|
|
|
|
}
|
2019-01-02 15:57:13 -08:00
|
|
|
|
2019-08-13 13:59:01 -07:00
|
|
|
app.StakingKeeper.SetValidator(ctx, validator)
|
2018-11-26 04:21:23 -08:00
|
|
|
counter++
|
|
|
|
}
|
2019-01-02 15:57:13 -08:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
iter.Close()
|
|
|
|
|
2019-08-13 13:59:01 -07:00
|
|
|
_ = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
2019-02-04 08:42:48 -08:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
/* Handle slashing state. */
|
|
|
|
|
|
|
|
// reset start height on signing infos
|
2019-08-13 13:59:01 -07:00
|
|
|
app.SlashingKeeper.IterateValidatorSigningInfos(
|
2019-01-02 15:57:13 -08:00
|
|
|
ctx,
|
2020-06-12 07:25:12 -07:00
|
|
|
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
|
2019-01-02 15:57:13 -08:00
|
|
|
info.StartHeight = 0
|
2019-08-13 13:59:01 -07:00
|
|
|
app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
|
2019-01-02 15:57:13 -08:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2018-11-26 04:21:23 -08:00
|
|
|
}
|