2024-02-01 04:48:40 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
storetypes "cosmossdk.io/store/types"
|
|
|
|
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
|
|
|
servertypes "github.com/cosmos/cosmos-sdk/server/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2024-08-16 06:23:56 +00:00
|
|
|
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2024-02-01 04:48:40 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExportAppStateAndValidators exports the state of the application for a genesis file.
|
|
|
|
func (app *LaconicApp) ExportAppStateAndValidators(
|
|
|
|
forZeroHeight bool,
|
|
|
|
jailAllowedAddrs []string,
|
|
|
|
modulesToExport []string,
|
|
|
|
) (servertypes.ExportedApp, error) {
|
|
|
|
// as if they could withdraw from the start of the next block
|
|
|
|
ctx := app.NewContextLegacy(true, tmproto.Header{Height: app.LastBlockHeight()})
|
|
|
|
|
|
|
|
// We export at last height + 1, because that's the height at which
|
|
|
|
// CometBFT will start InitChain.
|
|
|
|
height := app.LastBlockHeight() + 1
|
|
|
|
if forZeroHeight {
|
|
|
|
height = 0
|
|
|
|
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
genState, err := app.ModuleManager.ExportGenesis(ctx, app.appCodec)
|
|
|
|
if err != nil {
|
|
|
|
return servertypes.ExportedApp{}, fmt.Errorf("failed to export genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
appState, err := json.MarshalIndent(genState, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return servertypes.ExportedApp{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
|
|
|
|
return servertypes.ExportedApp{
|
|
|
|
AppState: appState,
|
|
|
|
Validators: validators,
|
|
|
|
Height: height,
|
|
|
|
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare for fresh start at zero height
|
2024-07-11 05:16:31 +00:00
|
|
|
// NOTE zero height genesis is a temporary feature, which will be deprecated in favor of export at a block height
|
2024-02-01 04:48:40 +00:00
|
|
|
func (app *LaconicApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
|
|
|
|
applyAllowedAddrs := false
|
|
|
|
|
|
|
|
// check if there is a allowed address list
|
|
|
|
if len(jailAllowedAddrs) > 0 {
|
|
|
|
applyAllowedAddrs = true
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedAddrsMap := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, addr := range jailAllowedAddrs {
|
|
|
|
_, err := sdk.ValAddressFromBech32(addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
allowedAddrsMap[addr] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle fee distribution state. */
|
|
|
|
|
|
|
|
// withdraw all validator commission
|
2024-03-07 09:11:53 +00:00
|
|
|
_ = app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
|
2024-02-01 04:48:40 +00:00
|
|
|
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, valBz)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// withdraw all delegator rewards
|
|
|
|
dels, err := app.StakingKeeper.GetAllDelegations(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, delegation := range dels {
|
|
|
|
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2024-03-07 09:11:53 +00:00
|
|
|
_ = app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
|
2024-02-01 04:48:40 +00:00
|
|
|
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
|
|
|
|
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
feePool, err := app.DistrKeeper.FeePool.Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
|
|
|
|
if err := app.DistrKeeper.FeePool.Set(ctx, feePool); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valBz); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// reinitialize all delegations
|
|
|
|
for _, del := range dels {
|
|
|
|
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
delAddr, err := sdk.AccAddressFromBech32(del.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil {
|
|
|
|
// never called as BeforeDelegationCreated always returns nil
|
|
|
|
panic(fmt.Errorf("error while incrementing period: %w", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil {
|
|
|
|
// never called as AfterDelegationModified always returns nil
|
|
|
|
panic(fmt.Errorf("error while creating a new delegation period record: %w", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset context height
|
|
|
|
ctx = ctx.WithBlockHeight(height)
|
|
|
|
|
|
|
|
/* Handle staking state. */
|
|
|
|
|
|
|
|
// iterate through redelegations, reset creation height
|
2024-03-07 09:11:53 +00:00
|
|
|
_ = app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
|
2024-02-01 04:48:40 +00:00
|
|
|
for i := range red.Entries {
|
|
|
|
red.Entries[i].CreationHeight = 0
|
|
|
|
}
|
2024-03-07 09:11:53 +00:00
|
|
|
_ = app.StakingKeeper.SetRedelegation(ctx, red)
|
2024-02-01 04:48:40 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// iterate through unbonding delegations, reset creation height
|
2024-03-07 09:11:53 +00:00
|
|
|
_ = app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
|
2024-02-01 04:48:40 +00:00
|
|
|
for i := range ubd.Entries {
|
|
|
|
ubd.Entries[i].CreationHeight = 0
|
|
|
|
}
|
2024-03-07 09:11:53 +00:00
|
|
|
_ = app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
|
2024-02-01 04:48:40 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// Iterate through validators by power descending, reset bond heights, and
|
|
|
|
// update bond intra-tx counters.
|
|
|
|
store := ctx.KVStore(app.GetKey(stakingtypes.StoreKey))
|
|
|
|
iter := storetypes.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
|
|
|
|
counter := int16(0)
|
|
|
|
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
|
|
|
|
validator, err := app.StakingKeeper.GetValidator(ctx, addr)
|
|
|
|
if errors.Is(err, stakingtypes.ErrNoValidatorFound) {
|
|
|
|
panic("expected validator, not found")
|
|
|
|
} else if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
validator.UnbondingHeight = 0
|
|
|
|
if applyAllowedAddrs && !allowedAddrsMap[addr.String()] {
|
|
|
|
validator.Jailed = true
|
|
|
|
}
|
|
|
|
|
2024-03-07 09:11:53 +00:00
|
|
|
_ = app.StakingKeeper.SetValidator(ctx, validator)
|
2024-02-01 04:48:40 +00:00
|
|
|
counter++
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := iter.Close(); err != nil {
|
|
|
|
app.Logger().Error("error while closing the key-value store reverse prefix iterator: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-08-16 06:23:56 +00:00
|
|
|
|
|
|
|
/* Handle slashing state. */
|
|
|
|
|
|
|
|
// reset start height on signing infos
|
2024-09-02 06:49:47 +00:00
|
|
|
err = app.SlashingKeeper.IterateValidatorSigningInfos(
|
2024-08-16 06:23:56 +00:00
|
|
|
ctx,
|
|
|
|
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
|
|
|
|
info.StartHeight = 0
|
2024-09-02 06:49:47 +00:00
|
|
|
err = app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-08-16 06:23:56 +00:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2024-09-02 06:49:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-02-01 04:48:40 +00:00
|
|
|
}
|