-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] add tx-gas-hard-limit flag to prevent spamming attack (#460)
* add tx-gas-hard-limit flag to prevent spamming attack * convert spamming protection to mempool operation
- Loading branch information
yys
authored
Mar 17, 2021
1 parent
eb93425
commit 6d19120
Showing
4 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ante | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// FlagTxGasHardLimit defines the hard cap to prevent tx spamming attack | ||
const FlagTxGasHardLimit = "tx-gas-hard-limit" | ||
|
||
// SpammingPreventionDecorator will check if the transaction's gas is smaller than | ||
// configured hard cap | ||
type SpammingPreventionDecorator struct { | ||
} | ||
|
||
// NewSpammingPreventionDecorator returns new spamming prevention decorator instance | ||
func NewSpammingPreventionDecorator() SpammingPreventionDecorator { | ||
return SpammingPreventionDecorator{} | ||
} | ||
|
||
// AnteHandle handles msg tax fee checking | ||
func (spd SpammingPreventionDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
if ctx.IsCheckTx() { | ||
feeTx, ok := tx.(FeeTx) | ||
if !ok { | ||
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") | ||
} | ||
|
||
gas := feeTx.GetGas() | ||
gasHardLimit := viper.GetUint64(FlagTxGasHardLimit) | ||
if gas > gasHardLimit { | ||
return ctx, sdkerrors.Wrapf(sdkerrors.ErrOutOfGas, "Tx cannot spend more than %d gas", gasHardLimit) | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters