2019-08-15 16:26:40 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-04-17 10:00:07 +00:00
|
|
|
"fmt"
|
2019-08-15 16:26:40 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-08-15 16:26:40 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
servertypes "github.com/cosmos/cosmos-sdk/server/types"
|
2020-04-17 22:32:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-08-15 16:26:40 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2019-08-15 16:26:40 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2021-04-17 10:00:07 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2021-06-04 13:14:45 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/encoding"
|
2019-08-15 16:26:40 +00:00
|
|
|
)
|
|
|
|
|
2020-04-17 22:32:01 +00:00
|
|
|
// NewDefaultGenesisState generates the default state for the application.
|
|
|
|
func NewDefaultGenesisState() simapp.GenesisState {
|
2021-06-04 13:14:45 +00:00
|
|
|
encCfg := encoding.MakeConfig(ModuleBasics)
|
2022-07-28 13:43:49 +00:00
|
|
|
return ModuleBasics.DefaultGenesis(encCfg.Codec)
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 16:26:40 +00:00
|
|
|
// ExportAppStateAndValidators exports the state of the application for a genesis
|
|
|
|
// file.
|
|
|
|
func (app *EthermintApp) ExportAppStateAndValidators(
|
2021-04-17 10:00:07 +00:00
|
|
|
forZeroHeight bool, jailAllowedAddrs []string,
|
|
|
|
) (servertypes.ExportedApp, error) {
|
2019-08-15 16:26:40 +00:00
|
|
|
// Creates context with current height and checks txs for ctx to be usable by start of next block
|
2021-04-17 10:00:07 +00:00
|
|
|
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
|
2019-08-15 16:26:40 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// We export at last height + 1, because that's the height at which
|
|
|
|
// Tendermint will start InitChain.
|
|
|
|
height := app.LastBlockHeight() + 1
|
2019-08-15 16:26:40 +00:00
|
|
|
if forZeroHeight {
|
2021-04-17 10:00:07 +00:00
|
|
|
height = 0
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
if err := app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs); err != nil {
|
|
|
|
return servertypes.ExportedApp{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
genState := app.mm.ExportGenesis(ctx, app.appCodec)
|
|
|
|
appState, err := json.MarshalIndent(genState, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return servertypes.ExportedApp{}, err
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
|
2019-08-15 16:26:40 +00:00
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return servertypes.ExportedApp{}, err
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
return servertypes.ExportedApp{
|
|
|
|
AppState: appState,
|
|
|
|
Validators: validators,
|
|
|
|
Height: height,
|
|
|
|
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
|
|
|
|
}, nil
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare for fresh start at zero height
|
|
|
|
// NOTE zero height genesis is a temporary feature which will be deprecated
|
2021-04-18 16:39:15 +00:00
|
|
|
// in favor of export at a block height
|
2021-04-17 10:00:07 +00:00
|
|
|
func (app *EthermintApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) error {
|
|
|
|
applyAllowedAddrs := false
|
2019-08-15 16:26:40 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// check if there is a allowed address list
|
|
|
|
if len(jailAllowedAddrs) > 0 {
|
|
|
|
applyAllowedAddrs = true
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
allowedAddrsMap := make(map[string]bool)
|
2019-08-15 16:26:40 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
for _, addr := range jailAllowedAddrs {
|
2019-08-15 16:26:40 +00:00
|
|
|
_, err := sdk.ValAddressFromBech32(addr)
|
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return err
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
allowedAddrsMap[addr] = true
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Just to be safe, assert the invariants on current state. */
|
2020-04-01 18:49:21 +00:00
|
|
|
app.CrisisKeeper.AssertInvariants(ctx)
|
2019-08-15 16:26:40 +00:00
|
|
|
|
|
|
|
/* Handle fee distribution state. */
|
|
|
|
|
|
|
|
// withdraw all validator commission
|
2021-04-17 10:00:07 +00:00
|
|
|
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
|
2020-04-01 18:49:21 +00:00
|
|
|
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
|
2019-08-15 16:26:40 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// withdraw all delegator rewards
|
2020-04-01 18:49:21 +00:00
|
|
|
dels := app.StakingKeeper.GetAllDelegations(ctx)
|
2019-08-15 16:26:40 +00:00
|
|
|
for _, delegation := range dels {
|
2021-04-17 10:00:07 +00:00
|
|
|
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear validator slash events
|
2020-04-01 18:49:21 +00:00
|
|
|
app.DistrKeeper.DeleteAllValidatorSlashEvents(ctx)
|
2019-08-15 16:26:40 +00:00
|
|
|
|
|
|
|
// clear validator historical rewards
|
2020-04-01 18:49:21 +00:00
|
|
|
app.DistrKeeper.DeleteAllValidatorHistoricalRewards(ctx)
|
2019-08-15 16:26:40 +00:00
|
|
|
|
|
|
|
// set context height to zero
|
|
|
|
height := ctx.BlockHeight()
|
|
|
|
ctx = ctx.WithBlockHeight(0)
|
|
|
|
|
|
|
|
// reinitialize all validators
|
2021-04-17 10:00:07 +00:00
|
|
|
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
|
2019-08-15 16:26:40 +00:00
|
|
|
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
|
2020-04-22 19:26:01 +00:00
|
|
|
scraps := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
|
2020-04-01 18:49:21 +00:00
|
|
|
feePool := app.DistrKeeper.GetFeePool(ctx)
|
2020-04-22 19:26:01 +00:00
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
|
2020-04-01 18:49:21 +00:00
|
|
|
app.DistrKeeper.SetFeePool(ctx, feePool)
|
2019-08-15 16:26:40 +00:00
|
|
|
|
2022-07-28 13:43:49 +00:00
|
|
|
if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()); err != nil {
|
|
|
|
return true
|
|
|
|
}
|
2019-08-15 16:26:40 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// reinitialize all delegations
|
|
|
|
for _, del := range dels {
|
2021-04-17 10:00:07 +00:00
|
|
|
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delAddr, err := sdk.AccAddressFromBech32(del.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-28 13:43:49 +00:00
|
|
|
if err := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset context height
|
|
|
|
ctx = ctx.WithBlockHeight(height)
|
|
|
|
|
|
|
|
/* Handle staking state. */
|
|
|
|
|
|
|
|
// iterate through redelegations, reset creation height
|
2021-04-17 10:00:07 +00:00
|
|
|
app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
|
2019-08-15 16:26:40 +00:00
|
|
|
for i := range red.Entries {
|
|
|
|
red.Entries[i].CreationHeight = 0
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
app.StakingKeeper.SetRedelegation(ctx, red)
|
2019-08-15 16:26:40 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// iterate through unbonding delegations, reset creation height
|
2021-04-17 10:00:07 +00:00
|
|
|
app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
|
2019-08-15 16:26:40 +00:00
|
|
|
for i := range ubd.Entries {
|
|
|
|
ubd.Entries[i].CreationHeight = 0
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
|
2019-08-15 16:26:40 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// Iterate through validators by power descending, reset bond heights, and
|
|
|
|
// update bond intra-tx counters.
|
2021-04-17 10:00:07 +00:00
|
|
|
store := ctx.KVStore(app.keys[stakingtypes.StoreKey])
|
|
|
|
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
|
2019-08-15 16:26:40 +00:00
|
|
|
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
2021-08-17 12:06:08 +00:00
|
|
|
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
|
2020-04-01 18:49:21 +00:00
|
|
|
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
|
2019-08-15 16:26:40 +00:00
|
|
|
if !found {
|
2021-04-17 10:00:07 +00:00
|
|
|
return fmt.Errorf("expected validator %s not found", addr)
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validator.UnbondingHeight = 0
|
2021-04-17 10:00:07 +00:00
|
|
|
if applyAllowedAddrs && !allowedAddrsMap[addr.String()] {
|
2019-08-15 16:26:40 +00:00
|
|
|
validator.Jailed = true
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
app.StakingKeeper.SetValidator(ctx, validator)
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 15:12:57 +00:00
|
|
|
if err := iter.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-15 16:26:40 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
if _, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-15 16:26:40 +00:00
|
|
|
|
|
|
|
/* Handle slashing state. */
|
|
|
|
|
|
|
|
// reset start height on signing infos
|
2020-04-01 18:49:21 +00:00
|
|
|
app.SlashingKeeper.IterateValidatorSigningInfos(
|
2019-08-15 16:26:40 +00:00
|
|
|
ctx,
|
2021-04-17 10:00:07 +00:00
|
|
|
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
|
2019-08-15 16:26:40 +00:00
|
|
|
info.StartHeight = 0
|
2020-04-01 18:49:21 +00:00
|
|
|
app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
|
2019-08-15 16:26:40 +00:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2021-04-17 10:00:07 +00:00
|
|
|
return nil
|
2019-08-15 16:26:40 +00:00
|
|
|
}
|