Skip to content

Commit

Permalink
Merge branch 'robert/cosmos-sdk-047' into lint-fixes-047
Browse files Browse the repository at this point in the history
  • Loading branch information
JeancarloBarrios authored Dec 17, 2024
2 parents f3fe5d7 + bfc3cb6 commit 67bc4b9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
77 changes: 56 additions & 21 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,52 @@ package app
import (
"encoding/json"
"fmt"
"log"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"log"

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 *RegenApp) ExportAppStateAndValidators(
forZeroHeight bool,
jailAllowedAddrs []string,
modulesToExport []string,
) (servertypes.ExportedApp, error) {
//func (app *RegenApp) ExportAppStateAndValidators(
// forZeroHeight bool,
// jailAllowedAddrs []string,
// modulesToExport []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.ModuleManager.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
// 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
//}

// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *RegenApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs []string, modulesToExport []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()})

Expand Down Expand Up @@ -50,7 +78,8 @@ func (app *RegenApp) ExportAppStateAndValidators(

// prepare for fresh start at zero height
// NOTE zero height genesis is a temporary feature which will be deprecated
// in favor of export at a block height
//
// in favour of export at a block height
func (app *RegenApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
applyAllowedAddrs := false

Expand All @@ -76,21 +105,21 @@ func (app *RegenApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs

// withdraw all validator commission
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, err := app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
if err != nil {
log.Fatalf("failed to withdraw validator commission: %s", err)
}
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
return false
})

// withdraw all delegator rewards
dels := app.StakingKeeper.GetAllDelegations(ctx)
for _, delegation := range dels {
_, err := app.DistrKeeper.WithdrawDelegationRewards(ctx,
delegation.GetDelegatorAddr(), delegation.GetValidatorAddr())
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(fmt.Sprintf("failed to withdraw delegation rewards: %s", err))
panic(err)
}

delAddr := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

_, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
}

// clear validator slash events
Expand All @@ -110,6 +139,7 @@ func (app *RegenApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
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)
}
Expand All @@ -118,12 +148,17 @@ func (app *RegenApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs

// reinitialize all delegations
for _, del := range dels {
delAddr := del.GetDelegatorAddr()
valAddr := del.GetValidatorAddr()
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
panic(err)
}
delAddr := sdk.MustAccAddressFromBech32(del.DelegatorAddress)

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))
Expand Down Expand Up @@ -155,15 +190,15 @@ func (app *RegenApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs

// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
store := ctx.KVStore(app.keys[stakingtypes.StoreKey])
store := ctx.KVStore(app.GetKey(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(fmt.Sprintf("expected validator (%s) not found", addr))
panic("expected validator, not found")
}

validator.UnbondingHeight = 0
Expand Down
2 changes: 1 addition & 1 deletion app/simulation/app_after_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestAppAfterImport(t *testing.T) {
}()

newApp := regen.NewRegenApp(log.NewNopLogger(), newDB, nil, true, simcli.FlagPeriodValue, appOptions, fauxMerkleModeOpt, baseapp.SetChainID(SimAppChainID))
require.Equal(t, "SimApp", newApp.Name())
require.Equal(t, "regen", newApp.Name())

newApp.InitChain(abci.RequestInitChain{
ChainId: SimAppChainID,
Expand Down

0 comments on commit 67bc4b9

Please sign in to comment.