-
Notifications
You must be signed in to change notification settings - Fork 697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add gov spam prevention antehandler #2251
Changes from 8 commits
2830694
70f8552
4026469
35a50d4
0bf9505
24a90af
d1510b8
6145d83
848551f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,7 @@ var ( | |
stakingAmountCoin = sdk.NewCoin(uatomDenom, stakingAmount) | ||
tokenAmount = sdk.NewCoin(uatomDenom, sdk.NewInt(3300000000)) // 3,300uatom | ||
standardFees = sdk.NewCoin(uatomDenom, sdk.NewInt(330000)) // 0.33uatom | ||
depositAmount = sdk.NewCoin(uatomDenom, sdk.NewInt(10000000)) // 10uatom | ||
depositAmount = sdk.NewCoin(uatomDenom, sdk.NewInt(3300000000)) // 3,300uatom | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you give more context on this change and how it tests the new ante handler? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unit tests and e2e tests need to be added:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
distModuleAddress = authtypes.NewModuleAddress(distrtypes.ModuleName).String() | ||
proposalCounter = 0 | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package ante | ||
|
||
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(10, 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() | ||
if err = gpsd.checkSpamSubmitProposalMsg(ctx, msgs); 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.ErrInsufficientFunds, "not enough initial deposit. required: %v", miniumInitialDeposit) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
validAuthz := func(execMsg *authz.MsgExec) error { | ||
for _, v := range execMsg.Msgs { | ||
var innerMsg sdk.Msg | ||
if err := gpsd.cdc.UnpackAny(v, &innerMsg); err != nil { | ||
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs") | ||
} | ||
|
||
if err := validMsg(innerMsg); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
for _, m := range msgs { | ||
if msg, ok := m.(*authz.MsgExec); ok { | ||
if err := validAuthz(msg); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
|
||
// validate normal msgs | ||
if err := validMsg(m); 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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be under
x/
as it's not a module.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where should it live sir?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can move it to
gaia/ante/