Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 138 additions & 68 deletions app/app.go

Large diffs are not rendered by default.

86 changes: 69 additions & 17 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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"
Expand All @@ -16,11 +16,11 @@ import (
// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *App) 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()})
ctx := app.NewContext(true).WithBlockHeader(tmproto.Header{Height: app.LastBlockHeight()})

// We export at last height + 1, because that's the height at which
// Tendermint will start InitChain.
Expand All @@ -30,7 +30,10 @@ func (app *App) ExportAppStateAndValidators(
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState := app.mm.ExportGenesis(ctx, app.appCodec)
genState, err := app.mm.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
if err != nil {
return servertypes.ExportedApp{}, err
}
appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
Expand Down Expand Up @@ -77,17 +80,36 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str

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

// withdraw all delegator rewards
dels := app.StakingKeeper.GetAllDelegations(ctx)
dels, err := app.StakingKeeper.GetAllDelegations(ctx)
if err != nil {
log.Fatal(err)
}
accAddrCodec := app.AccountKeeper.AddressCodec()
valAddrCodec := app.StakingKeeper.ValidatorAddressCodec()
for _, delegation := range dels {
_, err := app.DistrKeeper.WithdrawDelegationRewards(ctx, delegation.GetDelegatorAddr(), delegation.GetValidatorAddr())
delAddress, err := accAddrCodec.StringToBytes(delegation.GetDelegatorAddr())
if err != nil {
panic(err)
}

valAddress, err := valAddrCodec.StringToBytes(delegation.GetValidatorAddr())
if err != nil {
panic(err)
}

_, err = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddress, valAddress)
if err != nil {
panic(err)
}
Expand All @@ -104,21 +126,51 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str
ctx = ctx.WithBlockHeight(0)

// reinitialize all validators
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
err = 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)
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
return true
}
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz)
if err != nil {
return true
}
feePool, err := app.DistrKeeper.FeePool.Get(ctx)
if err != nil {
return true
}
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
app.DistrKeeper.SetFeePool(ctx, feePool)
err = app.DistrKeeper.FeePool.Set(ctx, feePool)
if err != nil {
return true
}

app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
//nolint:errcheck // AfterValidator always returns nil, line authored by Tendermint
app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valBz)
return false
})
if err != nil {
log.Fatal(err)
}

// 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())

delAddress, err := accAddrCodec.StringToBytes(del.GetDelegatorAddr())
if err != nil {
panic(err)
}

valAddress, err := valAddrCodec.StringToBytes(del.GetValidatorAddr())
if err != nil {
panic(err)
}

//nolint:errcheck // BeforeDelegationCreated always returns nil, line authored by Tendermint
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddress, valAddress)
//nolint:errcheck // AfterDelegationModified always returns nil, line authored by Tendermint
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddress, valAddress)
}

// reset context height
Expand Down Expand Up @@ -147,13 +199,13 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str
// 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)
iter := storetypes.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
counter := int16(0)

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

Expand Down
4 changes: 2 additions & 2 deletions app/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type CosmosApp interface {
LegacyAmino() *codec.LegacyAmino

// Application updates every begin block.
BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock
BeginBlocker(ctx sdk.Context) sdk.BeginBlock

// Application updates every end block.
EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock
EndBlocker(ctx sdk.Context) sdk.EndBlock

// Application update at chain (i.e app) initialization.
InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain
Expand Down
5 changes: 4 additions & 1 deletion cmd/polynetwork-cosmosd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
baseAccount := authtypes.NewBaseAccount(addr, nil, 0, 0)

if !vestingAmt.IsZero() {
baseVestingAccount := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd)
baseVestingAccount, err := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd)
if err != nil {
return fmt.Errorf("failed to create base vesting account: %w", err)
}

if (balances.Coins.IsZero() && !baseVestingAccount.OriginalVesting.IsZero()) ||
baseVestingAccount.OriginalVesting.IsAnyGT(balances.Coins) {
Expand Down
20 changes: 6 additions & 14 deletions cmd/polynetwork-cosmosd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ import (

"github.com/spf13/viper"

"cosmossdk.io/log"
"cosmossdk.io/simapp"
dbm "github.com/cometbft/cometbft-db"
tmcfg "github.com/cometbft/cometbft/config"
tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cometbft/cometbft/libs/log"
dbm "github.com/cosmos/cosmos-db"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/Switcheo/polynetwork-cosmos/app"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/debug"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/auth/types"
vestingcli "github.com/cosmos/cosmos-sdk/x/auth/vesting/client/cli"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
Expand Down Expand Up @@ -82,9 +80,8 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
rootCmd.AddCommand(
genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome, genutiltypes.DefaultMessageValidator),
genutilcli.MigrateGenesisCmd(),
genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome, genutiltypes.DefaultMessageValidator, encodingConfig.TxConfig.SigningContext().ValidatorAddressCodec()),
genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome, encodingConfig.TxConfig.SigningContext().ValidatorAddressCodec()),
genutilcli.ValidateGenesisCmd(app.ModuleBasics),
AddGenesisAccountCmd(app.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
Expand All @@ -96,10 +93,8 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {

// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
rpc.StatusCommand(),
queryCommand(),
txCommand(),
keys.Commands(app.DefaultNodeHome),
)
}

Expand All @@ -119,9 +114,7 @@ func queryCommand() *cobra.Command {
}

cmd.AddCommand(
authcmd.GetAccountCmd(),
rpc.ValidatorCommand(),
rpc.BlockCommand(),
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
)
Expand Down Expand Up @@ -151,7 +144,6 @@ func txCommand() *cobra.Command {
authcmd.GetEncodeCommand(),
authcmd.GetDecodeCommand(),
flags.LineBreak,
vestingcli.GetTxCmd(),
)

app.ModuleBasics.AddTxCommands(cmd)
Expand Down Expand Up @@ -184,7 +176,7 @@ func newApp(
// appExport creates a new simapp (optionally at a given height)
func (a appCreator) appExport(
logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailAllowedAddrs []string,
appOpts servertypes.AppOptions) (servertypes.ExportedApp, error) {
appOpts servertypes.AppOptions, modulesToExport []string) (servertypes.ExportedApp, error) {

var anApp *app.App

Expand Down Expand Up @@ -225,7 +217,7 @@ func (a appCreator) appExport(
)
}

return anApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs)
return anApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
}

func overwriteFlagDefaults(c *cobra.Command, defaults map[string]string) {
Expand Down
Loading