Skip to content

Commit

Permalink
Merge pull request #175 from DongLieu/dong/upgrades-sdk-v0.50
Browse files Browse the repository at this point in the history
feat: Upgrades to sdk v0.50
  • Loading branch information
vuong177 authored Oct 4, 2024
2 parents 8207a95 + 2bf0a3b commit 9f1f0e6
Show file tree
Hide file tree
Showing 84 changed files with 4,872 additions and 5,633 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ release/
/.idea/
*.iml
*.tar.gz
onomyd
# onomyd
/tmp-swagger-gen/
/target
Cargo.lock
787 changes: 275 additions & 512 deletions app/app.go

Large diffs are not rendered by default.

202 changes: 137 additions & 65 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package app

import (
"encoding/json"
"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"
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"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *OnomyApp) ExportAppStateAndValidators(
forZeroHeight bool, jailAllowedAddrs []string,
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()})
// 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
// Tendermint will start InitChain.
Expand All @@ -29,31 +31,32 @@ func (app *OnomyApp) ExportAppStateAndValidators(
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState := app.mm.ExportGenesis(ctx, app.appCodec)
appState, err := json.MarshalIndent(genState, "", " ")
genState, err := app.mm.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
if err != nil {
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
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),
}, nil
}, err
}

// 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
func (app *OnomyApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) { // nolint:cyclop,gocyclo,gocognit // generated by cosmos
// in favor of export at a block height.
func (app *OnomyApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
applyAllowedAddrs := false

// check if there is a allowed address list
// check if there is a allowed address list.
if len(jailAllowedAddrs) > 0 {
applyAllowedAddrs = true
}
Expand All @@ -63,7 +66,7 @@ func (app *OnomyApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
for _, addr := range jailAllowedAddrs {
_, err := sdk.ValAddressFromBech32(addr)
if err != nil {
log.Fatal(err)
panic(err)
}
allowedAddrsMap[addr] = true
}
Expand All @@ -73,114 +76,183 @@ func (app *OnomyApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs

/* Handle fee distribution state. */

// withdraw all validator commission
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, err := app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
// withdraw all validator commission.
err := app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valAddr, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
app.Logger().Error(err.Error(), "ValOperatorAddress", val.GetOperator())
}
_, err = app.DistrKeeper.WithdrawValidatorCommission(ctx, valAddr)
if err != nil {
app.Logger().Error(err.Error(), "ValOperatorAddress", val.GetOperator())
}
return false
})
if err != nil {
panic(err)
}

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

delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
if err != nil {
panic(err)
}

_, err = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
if err != nil {
panic(err)
}
}

// clear validator slash events
// clear validator slash events.
app.DistrKeeper.DeleteAllValidatorSlashEvents(ctx)

// clear validator historical rewards
// clear validator historical rewards.
app.DistrKeeper.DeleteAllValidatorHistoricalRewards(ctx)

// set context height to zero
// 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)
// reinitialize all validators.
err = app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
// donate any unwithdrawn outstanding reward fraction tokens to the community pool.
valAddr, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
}
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valAddr)
if err != nil {
panic(err)
}
feePool, err := app.DistrKeeper.FeePool.Get(ctx)
if err != nil {
panic(err)
}
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
app.DistrKeeper.SetFeePool(ctx, feePool)

app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
err = app.DistrKeeper.FeePool.Set(ctx, feePool)
if err != nil {
panic(err)
}
if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valAddr); err != nil {
panic(err)
}
return false
})
if err != nil {
panic(err)
}

// reinitialize all delegations
// reinitialize all delegations.
for _, del := range dels {
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
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 {
panic(err)
}
if err := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil {
panic(err)
}
}

// reset context height
// 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) {
// iterate through redelegations, reset creation height.
err = 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)
if err := app.StakingKeeper.SetRedelegation(ctx, red); err != nil {
panic(err)
}
return false
})
if err != nil {
panic(err)
}

// iterate through unbonding delegations, reset creation height
app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
// iterate through unbonding delegations, reset creation height.
err = 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)
if err := app.StakingKeeper.SetUnbondingDelegation(ctx, ubd); err != nil {
panic(err)
}
return false
})
if err != nil {
panic(err)
}

// 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)
store := ctx.KVStore(app.GetKey(stakingtypes.StoreKey))
iter := storetypes.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[1:])
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
}
counter := int16(0)

validator.UnbondingHeight = 0
if applyAllowedAddrs && !allowedAddrsMap[addr.String()] {
validator.Jailed = true
// Closure to ensure iterator doesn't leak.
func() {
defer iter.Close()
for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, err := app.StakingKeeper.GetValidator(ctx, addr)
if err != nil {
panic("expected validator, not found")
}

validator.UnbondingHeight = 0
if applyAllowedAddrs && !allowedAddrsMap[addr.String()] {
validator.Jailed = true
}

if err = app.StakingKeeper.SetValidator(ctx, validator); err != nil {
panic(err)
}

counter++
}
}()

app.StakingKeeper.SetValidator(ctx, validator)
counter++
}

if err := iter.Close(); err != nil {
panic(err)
}

if _, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx); err != nil {
_, err = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
panic(err)
}

/* Handle slashing state. */

// reset start height on signing infos
app.SlashingKeeper.IterateValidatorSigningInfos(
// reset start height on signing infos.
err = app.SlashingKeeper.IterateValidatorSigningInfos(
ctx,
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
info.StartHeight = 0
app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
if err = app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info); err != nil {
panic(err)
}
return false
},
)
if err != nil {
panic(err)
}
}
9 changes: 4 additions & 5 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package app

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
// "github.com/cosmos/cosmos-sdk/codec".
)

// GenesisState of the blockchain is represented here as a map of raw json
Expand All @@ -16,6 +15,6 @@ import (
type GenesisState map[string]json.RawMessage

// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
return ModuleBasics.DefaultGenesis(cdc)
}
// func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
// return ModuleBasics.DefaultGenesis(cdc)
// }.
Loading

0 comments on commit 9f1f0e6

Please sign in to comment.