From c1390faed045c635d6de606c8a146f66df06787c Mon Sep 17 00:00:00 2001 From: Alex Lynham Date: Mon, 8 Nov 2021 12:58:54 +0000 Subject: [PATCH 1/3] Added upgrade handler for minimum commission. --- app/app.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/app.go b/app/app.go index f09f828c7..73dc03c9f 100644 --- a/app/app.go +++ b/app/app.go @@ -105,7 +105,6 @@ import ( const ( AccountAddressPrefix = "juno" Name = "juno" - upgradeName = "moneta-alpha" ) // this line is used by starport scaffolding # stargate/wasm/app/enabledProposals @@ -548,7 +547,7 @@ func New( panic(err) } - if upgradeInfo.Name == upgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { + if upgradeInfo.Name == "moneta-alpha" && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { storeUpgrades := store.StoreUpgrades{ Added: []string{authz.ModuleName, feegrant.ModuleName, wasm.ModuleName}, } @@ -702,7 +701,32 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) { // RegisterUpgradeHandlers returns upgrade handlers func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) { - app.UpgradeKeeper.SetUpgradeHandler(upgradeName, func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + app.UpgradeKeeper.SetUpgradeHandler("moneta-alpha", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + return app.mm.RunMigrations(ctx, cfg, vm) + }) + + app.UpgradeKeeper.SetUpgradeHandler("moneta-beta", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + // force an update of validator min commission + validators := app.StakingKeeper.GetAllValidators(ctx) + // hard code this because we don't want + // a) a fork or + // b) immediate reaction with additional gov props + minCommissionRate := sdk.NewDecWithPrec(5, 2) + for _, v := range validators { + if v.Commission.Rate.LT(minCommissionRate) { + comm, err := app.StakingKeeper.UpdateValidatorCommission( + ctx, v, minCommissionRate) + if err != nil { + panic(err) + } + v.Commission = comm + + // call the before-modification hook since we're about to update the commission + app.StakingKeeper.BeforeValidatorModified(ctx, v.GetOperator()) + + app.StakingKeeper.SetValidator(ctx, v) + } + } return app.mm.RunMigrations(ctx, cfg, vm) }) } From 7e7dde1929076e95e71ea86f2f2221072e469562 Mon Sep 17 00:00:00 2001 From: Alex Lynham Date: Mon, 8 Nov 2021 13:49:27 +0000 Subject: [PATCH 2/3] Copypasta of antehandler solution from jhernandezb #17 --- app/ante.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ app/app.go | 17 ++++++---- 2 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 app/ante.go diff --git a/app/ante.go b/app/ante.go new file mode 100644 index 000000000..eca8d31c0 --- /dev/null +++ b/app/ante.go @@ -0,0 +1,97 @@ +package app + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/x/auth/ante" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + channelkeeper "github.com/cosmos/ibc-go/modules/core/04-channel/keeper" + ibcante "github.com/cosmos/ibc-go/modules/core/ante" +) + +// HandlerOptions extends the SDK's AnteHandler options by requiring the IBC +// channel keeper. +type HandlerOptions struct { + ante.HandlerOptions + + IBCChannelkeeper channelkeeper.Keeper +} + +type MinCommissionDecorator struct{} + +func NewMinCommissionDecorator() MinCommissionDecorator { + return MinCommissionDecorator{} +} + +func (MinCommissionDecorator) AnteHandle( + ctx sdk.Context, tx sdk.Tx, + simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { + msgs := tx.GetMsgs() + minCommissionRate := sdk.NewDecWithPrec(5, 2) + for _, m := range msgs { + switch msg := m.(type) { + case *stakingtypes.MsgCreateValidator: + // prevent new validators joining the set with + // commission set below 5% + c := msg.Commission + if c.Rate.LT(minCommissionRate) { + return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "commission can't be lower than 5%") + } + case *stakingtypes.MsgEditValidator: + // if commission rate is nil, it means only + // other fields are affected - skip + if msg.CommissionRate == nil { + continue + } + if msg.CommissionRate.LT(minCommissionRate) { + return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "commission can't be lower than 5%") + } + default: + continue + } + } + return next(ctx, tx, simulate) +} + +// NewAnteHandler returns an AnteHandler that checks and increments sequence +// numbers, checks signatures & account numbers, and deducts fees from the first +// signer. +func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { + if options.AccountKeeper == nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder") + } + + if options.BankKeeper == nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for ante builder") + } + + if options.SignModeHandler == nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder") + } + + var sigGasConsumer = options.SigGasConsumer + if sigGasConsumer == nil { + sigGasConsumer = ante.DefaultSigVerificationGasConsumer + } + + anteDecorators := []sdk.AnteDecorator{ + ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first + NewMinCommissionDecorator(), + ante.NewRejectExtensionOptionsDecorator(), + ante.NewMempoolFeeDecorator(), + ante.NewValidateBasicDecorator(), + ante.NewTxTimeoutHeightDecorator(), + ante.NewValidateMemoDecorator(options.AccountKeeper), + ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), + ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper), + // SetPubKeyDecorator must be called before all signature verification decorators + ante.NewSetPubKeyDecorator(options.AccountKeeper), + ante.NewValidateSigCountDecorator(options.AccountKeeper), + ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer), + ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler), + ante.NewIncrementSequenceDecorator(options.AccountKeeper), + ibcante.NewAnteDecorator(options.IBCChannelkeeper), + } + + return sdk.ChainAnteDecorators(anteDecorators...), nil +} diff --git a/app/app.go b/app/app.go index 73dc03c9f..521c31160 100644 --- a/app/app.go +++ b/app/app.go @@ -523,13 +523,16 @@ func New( app.MountTransientStores(tkeys) app.MountMemoryStores(memKeys) - anteHandler, err := ante.NewAnteHandler( - ante.HandlerOptions{ - AccountKeeper: app.AccountKeeper, - BankKeeper: app.BankKeeper, - FeegrantKeeper: app.FeeGrantKeeper, - SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + anteHandler, err := NewAnteHandler( + HandlerOptions{ + HandlerOptions: ante.HandlerOptions{ + AccountKeeper: app.AccountKeeper, + BankKeeper: app.BankKeeper, + FeegrantKeeper: app.FeeGrantKeeper, + SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + }, + IBCChannelkeeper: app.IBCKeeper.ChannelKeeper, }, ) if err != nil { From 86ab6e3762e8d8ce0d3b4c2e54002f605f1a58f3 Mon Sep 17 00:00:00 2001 From: Alex Lynham Date: Mon, 8 Nov 2021 14:05:05 +0000 Subject: [PATCH 3/3] Ensure undo-tree files don't get included --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 53e3e375a..3d14697c5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,8 @@ secret.yml cosmoshub3.json exchanges.json juno_out.json -bin \ No newline at end of file +bin + +# emacs editor config +\#*\# +.\#*