From b70bbaf5906d381b046c3ad89afb0045b73b3cb5 Mon Sep 17 00:00:00 2001 From: Dheeraj Dubey Date: Wed, 1 Mar 2023 06:53:10 +0530 Subject: [PATCH] Add initial(25%) deposit for gov proposals to prevent spams --- app/ante.go | 6 +++ app/app.go | 2 + app/decorators/gov_filter.go | 88 ++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 app/decorators/gov_filter.go diff --git a/app/ante.go b/app/ante.go index 72c29ae9c..5516326b1 100644 --- a/app/ante.go +++ b/app/ante.go @@ -3,9 +3,12 @@ package app import ( "github.com/CosmWasm/wasmd/x/wasm" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" + "github.com/comdex-official/comdex/app/decorators" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/auth/ante" + govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" ibcante "github.com/cosmos/ibc-go/v4/modules/core/ante" ibckeeper "github.com/cosmos/ibc-go/v4/modules/core/keeper" ) @@ -17,6 +20,8 @@ type HandlerOptions struct { wasmConfig wasm.Config txCounterStoreKey sdk.StoreKey IBCChannelKeeper *ibckeeper.Keeper + GovKeeper govkeeper.Keeper + Cdc codec.BinaryCodec } func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { @@ -40,6 +45,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { wasmkeeper.NewLimitSimulationGasDecorator(options.wasmConfig.SimulationGasLimit), wasmkeeper.NewCountTXDecorator(options.txCounterStoreKey), ante.NewRejectExtensionOptionsDecorator(), + decorators.NewGovPreventSpamDecorator(options.Cdc, options.GovKeeper), ante.NewMempoolFeeDecorator(), ante.NewValidateBasicDecorator(), ante.NewTxTimeoutHeightDecorator(), diff --git a/app/app.go b/app/app.go index 7b3b02d6b..b394dfc1a 100644 --- a/app/app.go +++ b/app/app.go @@ -996,9 +996,11 @@ func New( SignModeHandler: encoding.TxConfig.SignModeHandler(), SigGasConsumer: ante.DefaultSigVerificationGasConsumer, }, + GovKeeper: app.GovKeeper, wasmConfig: wasmConfig, txCounterStoreKey: app.GetKey(wasm.StoreKey), IBCChannelKeeper: app.IbcKeeper, + Cdc: appCodec, }, ) if err != nil { diff --git a/app/decorators/gov_filter.go b/app/decorators/gov_filter.go new file mode 100644 index 000000000..966be7653 --- /dev/null +++ b/app/decorators/gov_filter.go @@ -0,0 +1,88 @@ +package decorators + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/x/authz" + govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" +) + +var MiniumInitialDepositRate = sdk.NewDecWithPrec(25, 2) + +type GovPreventSpamDecorator struct { + govKeeper govkeeper.Keeper + cdc codec.BinaryCodec +} + +func NewGovPreventSpamDecorator(cdc codec.BinaryCodec, govKeeper govkeeper.Keeper) GovPreventSpamDecorator { + return GovPreventSpamDecorator{ + govKeeper: govKeeper, + cdc: cdc, + } +} + +func (gpsd GovPreventSpamDecorator) AnteHandle( + ctx sdk.Context, tx sdk.Tx, + simulate bool, next sdk.AnteHandler, +) (newCtx sdk.Context, err error) { + msgs := tx.GetMsgs() + + err = gpsd.checkSpamSubmitProposalMsg(ctx, msgs) + + if err != nil { + return ctx, err + } + + return next(ctx, tx, simulate) +} + +func (gpsd GovPreventSpamDecorator) checkSpamSubmitProposalMsg(ctx sdk.Context, msgs []sdk.Msg) error { + validMsg := func(m sdk.Msg) error { + if msg, ok := m.(*govtypes.MsgSubmitProposal); ok { + // prevent spam gov msg + depositParams := gpsd.govKeeper.GetDepositParams(ctx) + miniumInitialDeposit := gpsd.calcMiniumInitialDeposit(depositParams.MinDeposit) + if msg.InitialDeposit.IsAllLT(miniumInitialDeposit) { + return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "not enough initial deposit. required: %v", miniumInitialDeposit) + } + } + return nil + } + + // Check every msg in the tx, if it's a MsgExec, check the inner msgs. + // If it's a MsgSubmitProposal, check the initial deposit is enough. + for _, m := range msgs { + var innerMsg sdk.Msg + if msg, ok := m.(*authz.MsgExec); ok { + for _, v := range msg.Msgs { + err := gpsd.cdc.UnpackAny(v, &innerMsg) + if err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs") + } + + err = validMsg(innerMsg) + if err != nil { + return err + } + } + } else { + err := validMsg(m) + if err != nil { + return err + } + } + } + + return nil +} + +func (gpsd GovPreventSpamDecorator) calcMiniumInitialDeposit(minDeposit sdk.Coins) (miniumInitialDeposit sdk.Coins) { + for _, coin := range minDeposit { + miniumInitialCoin := MiniumInitialDepositRate.MulInt(coin.Amount).RoundInt() + miniumInitialDeposit = miniumInitialDeposit.Add(sdk.NewCoin(coin.Denom, miniumInitialCoin)) + } + + return +}