Skip to content

Commit

Permalink
Remove x/distribution from testapp (#272)
Browse files Browse the repository at this point in the history
Remove the x/distribution module from the monogen generated testapp.
This is necessary since we expect all funds to be stored in the
FeeCollector module account instead of in the distribution module
account.
  • Loading branch information
natebeauregard authored Oct 28, 2024
1 parent 63bbf2c commit 0f167ef
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 143 deletions.
Binary file modified cmd/monogen/testapp.zip
Binary file not shown.
5 changes: 1 addition & 4 deletions cmd/monogen/testapp/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
_ "github.com/cosmos/cosmos-sdk/x/bank" // import for side-effects
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
_ "github.com/cosmos/cosmos-sdk/x/distribution" // import for side-effects
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
_ "github.com/cosmos/cosmos-sdk/x/mint" // import for side-effects
Expand All @@ -36,10 +35,10 @@ import (

// this line is used by starport scaffolding # stargate/app/moduleImport

"github.com/polymerdao/monomer/cmd/monogen/testapp/docs"
_ "github.com/polymerdao/monomer/x/rollup"
rollupkeeper "github.com/polymerdao/monomer/x/rollup/keeper"
"github.com/polymerdao/monomer/x/rollup/tx/helpers"
"github.com/polymerdao/monomer/cmd/monogen/testapp/docs"
)

const (
Expand Down Expand Up @@ -71,7 +70,6 @@ type App struct {
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
DistrKeeper distrkeeper.Keeper

RollupKeeper *rollupkeeper.Keeper
// this line is used by starport scaffolding # stargate/app/keeperDeclaration
Expand Down Expand Up @@ -143,7 +141,6 @@ func New(
&app.AccountKeeper,
&app.BankKeeper,
&app.StakingKeeper,
&app.DistrKeeper,
&app.RollupKeeper,
// this line is used by starport scaffolding # stargate/app/keeperDefinition
); err != nil {
Expand Down
15 changes: 0 additions & 15 deletions cmd/monogen/testapp/app/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
bankmodulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
distrmodulev1 "cosmossdk.io/api/cosmos/distribution/module/v1"
genutilmodulev1 "cosmossdk.io/api/cosmos/genutil/module/v1"
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
Expand All @@ -14,9 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/runtime"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
rolluptypes "github.com/polymerdao/monomer/x/rollup/types"

Expand All @@ -36,20 +33,15 @@ var (
// cosmos-sdk modules
authtypes.ModuleName,
banktypes.ModuleName,
distrtypes.ModuleName,
stakingtypes.ModuleName,
genutiltypes.ModuleName,
// chain modules
// this line is used by starport scaffolding # stargate/app/initGenesis,
}

// During begin block slashing happens after distr.BeginBlocker so that
// there is nothing left over in the validator fee pool, so as to keep the
// CanWithdrawInvariant invariant.
// NOTE: staking module is required if HistoricalEntries param > 0
beginBlockers = []string{
// cosmos sdk modules
distrtypes.ModuleName,
stakingtypes.ModuleName,
// chain modules
// this line is used by starport scaffolding # stargate/app/beginBlockers
Expand All @@ -70,8 +62,6 @@ var (
// module account permissions
moduleAccPerms = []*authmodulev1.ModuleAccountPermission{
{Account: authtypes.FeeCollectorName},
{Account: distrtypes.ModuleName},
{Account: minttypes.ModuleName, Permissions: []string{authtypes.Minter}},
{Account: stakingtypes.BondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
{Account: stakingtypes.NotBondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
{Account: rolluptypes.ModuleName, Permissions: []string{authtypes.Minter, authtypes.Burner}},
Expand All @@ -81,7 +71,6 @@ var (
// blocked account addresses
blockAccAddrs = []string{
authtypes.FeeCollectorName,
distrtypes.ModuleName,
stakingtypes.BondedPoolName,
stakingtypes.NotBondedPoolName,
}
Expand Down Expand Up @@ -130,10 +119,6 @@ var (
BlockedModuleAccountsOverride: blockAccAddrs,
}),
},
{
Name: distrtypes.ModuleName,
Config: appconfig.WrapAny(&distrmodulev1.Module{}),
},
{
Name: stakingtypes.ModuleName,
Config: appconfig.WrapAny(&stakingmodulev1.Module{
Expand Down
125 changes: 1 addition & 124 deletions cmd/monogen/testapp/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"encoding/json"
"fmt"
"log"

storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -68,128 +67,6 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str
allowedAddrsMap[addr] = true
}

/* Handle fee distribution state. */

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

// 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 := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

_, _ = 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
err = app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
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 := 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))
}
}

// reset context height
ctx = ctx.WithBlockHeight(height)

/* Handle staking state. */

// 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
}
err = app.StakingKeeper.SetRedelegation(ctx, red)
if err != nil {
panic(err)
}
return false
})
if err != nil {
panic(err)
}

// 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
}
err = app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
if 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.GetKey(stakingtypes.StoreKey))
Expand Down Expand Up @@ -219,7 +96,7 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str
return
}

_, err = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
_, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 0f167ef

Please sign in to comment.