Skip to content

Commit

Permalink
Merge pull request #703 from comdex-official/spam-proposal-check
Browse files Browse the repository at this point in the history
Add initial deposit for gov proposals to avoid spam
  • Loading branch information
dheerajkd30 authored Mar 6, 2023
2 parents b23b126 + b70bbaf commit 846447f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand All @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
88 changes: 88 additions & 0 deletions app/decorators/gov_filter.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 846447f

Please sign in to comment.