<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description - Adds error handling for staking hooks <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
196 lines
5.8 KiB
Go
196 lines
5.8 KiB
Go
package simapp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
servertypes "github.com/cosmos/cosmos-sdk/server/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
|
|
"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 *SimApp) ExportAppStateAndValidators(
|
|
forZeroHeight bool, jailAllowedAddrs []string,
|
|
) (servertypes.ExportedApp, error) {
|
|
// as if they could withdraw from the start of the next block
|
|
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
|
|
|
|
// We export at last height + 1, because that's the height at which
|
|
// Tendermint will start InitChain.
|
|
height := app.LastBlockHeight() + 1
|
|
if forZeroHeight {
|
|
height = 0
|
|
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
|
|
}
|
|
|
|
genState := app.mm.ExportGenesis(ctx, app.appCodec)
|
|
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
|
|
// NOTE zero height genesis is a temporary feature which will be deprecated
|
|
// in favour of export at a block height
|
|
func (app *SimApp) 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
|
|
}
|
|
|
|
/* Just to be safe, assert the invariants on current state. */
|
|
app.CrisisKeeper.AssertInvariants(ctx)
|
|
|
|
/* Handle fee distribution state. */
|
|
|
|
// withdraw all validator commission
|
|
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
|
|
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
|
|
return false
|
|
})
|
|
|
|
// withdraw all delegator rewards
|
|
dels := app.StakingKeeper.GetAllDelegations(ctx)
|
|
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
|
|
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
|
|
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
|
|
scraps := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
|
|
feePool := app.DistrKeeper.GetFeePool(ctx)
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
|
|
app.DistrKeeper.SetFeePool(ctx, feePool)
|
|
|
|
if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()); 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)
|
|
}
|
|
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr)
|
|
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr)
|
|
}
|
|
|
|
// reset context height
|
|
ctx = ctx.WithBlockHeight(height)
|
|
|
|
/* Handle staking state. */
|
|
|
|
// iterate through redelegations, reset creation height
|
|
app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
|
|
for i := range red.Entries {
|
|
red.Entries[i].CreationHeight = 0
|
|
}
|
|
app.StakingKeeper.SetRedelegation(ctx, red)
|
|
return false
|
|
})
|
|
|
|
// iterate through unbonding delegations, reset creation height
|
|
app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
|
|
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.
|
|
store := ctx.KVStore(app.keys[stakingtypes.StoreKey])
|
|
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
|
|
counter := int16(0)
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
|
|
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
|
|
if !found {
|
|
panic("expected validator, not found")
|
|
}
|
|
|
|
validator.UnbondingHeight = 0
|
|
if applyAllowedAddrs && !allowedAddrsMap[addr.String()] {
|
|
validator.Jailed = true
|
|
}
|
|
|
|
app.StakingKeeper.SetValidator(ctx, validator)
|
|
counter++
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
_, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
/* Handle slashing state. */
|
|
|
|
// reset start height on signing infos
|
|
app.SlashingKeeper.IterateValidatorSigningInfos(
|
|
ctx,
|
|
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
|
|
info.StartHeight = 0
|
|
app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
|
|
return false
|
|
},
|
|
)
|
|
}
|