2019-06-07 11:59:19 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-06-07 11:59:19 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
servertypes "github.com/cosmos/cosmos-sdk/server/types"
|
2019-06-07 11:59:19 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2019-06-07 11:59:19 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2022-01-08 00:39:27 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2019-06-07 11:59:19 +00:00
|
|
|
)
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
// ExportAppStateAndValidators export the state of the app for a genesis file
|
|
|
|
func (app *App) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteList []string,
|
2022-01-08 00:39:27 +00:00
|
|
|
) (servertypes.ExportedApp, error) {
|
2019-06-07 11:59:19 +00:00
|
|
|
// as if they could withdraw from the start of the next block
|
2021-03-11 04:32:35 +00:00
|
|
|
// block time is not available and defaults to Jan 1st, 0001
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
|
2019-06-07 11:59:19 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
height := app.LastBlockHeight() + 1
|
2019-06-07 11:59:19 +00:00
|
|
|
if forZeroHeight {
|
2022-01-08 00:39:27 +00:00
|
|
|
height = 0
|
2019-06-07 11:59:19 +00:00
|
|
|
app.prepForZeroHeightGenesis(ctx, jailWhiteList)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
genState := app.mm.ExportGenesis(ctx, app.appCodec)
|
|
|
|
newAppState, err := json.MarshalIndent(genState, "", " ")
|
2019-06-07 11:59:19 +00:00
|
|
|
if err != nil {
|
2022-01-08 00:39:27 +00:00
|
|
|
return servertypes.ExportedApp{}, err
|
2019-06-07 11:59:19 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
validators, err := staking.WriteValidators(ctx, app.stakingKeeper)
|
|
|
|
return servertypes.ExportedApp{
|
|
|
|
AppState: newAppState,
|
|
|
|
Validators: validators,
|
|
|
|
Height: height,
|
|
|
|
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
|
|
|
|
}, err
|
2019-06-07 11:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare for fresh start at zero height
|
2020-04-23 16:35:58 +00:00
|
|
|
// NOTE zero height genesis is a temporary feature which will be deprecated
|
2023-11-21 19:08:07 +00:00
|
|
|
// in favour of export at a block height
|
2019-06-20 17:02:29 +00:00
|
|
|
func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) {
|
2019-06-07 11:59:19 +00:00
|
|
|
applyWhiteList := false
|
|
|
|
|
2022-05-09 18:37:36 +00:00
|
|
|
// Check if there is a whitelist
|
2019-06-07 11:59:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just to be safe, assert the invariants on current state. */
|
2019-07-18 17:36:31 +00:00
|
|
|
app.crisisKeeper.AssertInvariants(ctx)
|
2019-06-07 11:59:19 +00:00
|
|
|
|
|
|
|
/* Handle fee distribution state. */
|
|
|
|
|
|
|
|
// withdraw all validator commission
|
2022-01-08 00:39:27 +00:00
|
|
|
app.stakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
|
|
|
|
_, _ = app.distrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
|
2019-06-07 11:59:19 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// withdraw all delegator rewards
|
|
|
|
dels := app.stakingKeeper.GetAllDelegations(ctx)
|
|
|
|
for _, delegation := range dels {
|
2022-01-08 00:39:27 +00:00
|
|
|
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
|
2020-04-23 16:35:58 +00:00
|
|
|
if err != nil {
|
2022-01-08 00:39:27 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-04-23 16:35:58 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
_, _ = app.distrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
|
2019-06-07 11:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear validator slash events
|
|
|
|
app.distrKeeper.DeleteAllValidatorSlashEvents(ctx)
|
|
|
|
|
|
|
|
// clear validator historical rewards
|
|
|
|
app.distrKeeper.DeleteAllValidatorHistoricalRewards(ctx)
|
|
|
|
|
|
|
|
// set context height to zero
|
|
|
|
height := ctx.BlockHeight()
|
|
|
|
ctx = ctx.WithBlockHeight(0)
|
|
|
|
|
|
|
|
// reinitialize all validators
|
2022-01-08 00:39:27 +00:00
|
|
|
app.stakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
|
2019-06-07 11:59:19 +00:00
|
|
|
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
|
2022-01-08 00:39:27 +00:00
|
|
|
scraps := app.distrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
|
2019-06-07 11:59:19 +00:00
|
|
|
feePool := app.distrKeeper.GetFeePool(ctx)
|
2020-04-23 16:35:58 +00:00
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
|
2019-06-07 11:59:19 +00:00
|
|
|
app.distrKeeper.SetFeePool(ctx, feePool)
|
|
|
|
|
|
|
|
app.distrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// reinitialize all delegations
|
|
|
|
for _, del := range dels {
|
2022-01-08 00:39:27 +00:00
|
|
|
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
delAddr, err := sdk.AccAddressFromBech32(del.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
app.distrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr)
|
|
|
|
app.distrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr)
|
2019-06-07 11:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset context height
|
|
|
|
ctx = ctx.WithBlockHeight(height)
|
|
|
|
|
|
|
|
/* Handle staking state. */
|
|
|
|
|
|
|
|
// iterate through redelegations, reset creation height
|
2022-01-08 00:39:27 +00:00
|
|
|
app.stakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
|
2019-06-07 11:59:19 +00:00
|
|
|
for i := range red.Entries {
|
|
|
|
red.Entries[i].CreationHeight = 0
|
|
|
|
}
|
|
|
|
app.stakingKeeper.SetRedelegation(ctx, red)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// iterate through unbonding delegations, reset creation height
|
2022-01-08 00:39:27 +00:00
|
|
|
app.stakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
|
2019-06-07 11:59:19 +00:00
|
|
|
for i := range ubd.Entries {
|
|
|
|
ubd.Entries[i].CreationHeight = 0
|
|
|
|
}
|
|
|
|
app.stakingKeeper.SetUnbondingDelegation(ctx, ubd)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// Iterate through validators by power descending, reset bond heights, and
|
|
|
|
// update bond intra-tx counters.
|
2022-01-08 00:39:27 +00:00
|
|
|
store := ctx.KVStore(app.keys[stakingtypes.StoreKey])
|
|
|
|
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
|
2019-06-07 11:59:19 +00:00
|
|
|
counter := int16(0)
|
|
|
|
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
2022-01-08 00:39:27 +00:00
|
|
|
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
|
2019-06-07 11:59:19 +00:00
|
|
|
validator, found := app.stakingKeeper.GetValidator(ctx, addr)
|
|
|
|
if !found {
|
|
|
|
panic("expected validator, not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
validator.UnbondingHeight = 0
|
|
|
|
if applyWhiteList && !whiteListMap[addr.String()] {
|
|
|
|
validator.Jailed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
app.stakingKeeper.SetValidator(ctx, validator)
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
|
|
|
|
iter.Close()
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
_, err := app.stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-06-07 11:59:19 +00:00
|
|
|
|
|
|
|
/* Handle slashing state. */
|
|
|
|
|
|
|
|
// reset start height on signing infos
|
|
|
|
app.slashingKeeper.IterateValidatorSigningInfos(
|
|
|
|
ctx,
|
2022-01-08 00:39:27 +00:00
|
|
|
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
|
2019-06-07 11:59:19 +00:00
|
|
|
info.StartHeight = 0
|
|
|
|
app.slashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2019-07-19 14:06:33 +00:00
|
|
|
}
|