Skip to content

Commit

Permalink
Merge pull request #49 from bze-alphateam/feature/tx-ante
Browse files Browse the repository at this point in the history
added ante handler to reject txes with fees in unknown denoms
  • Loading branch information
faneaatiku authored Aug 19, 2024
2 parents f6ea5b9 + 8c2e38e commit b3f1a38
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
87 changes: 87 additions & 0 deletions app/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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"
)

const (
denomMainnet = "ubze"
denomTestnet = "utbz"
)

func NewAnteHandler(options ante.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")
}

sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
}

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewRejectExtensionOptionsDecorator(),
ante.NewMempoolFeeDecorator(),
ante.NewValidateBasicDecorator(),
NewValidateTxFeeDenomsDecorator(), //use our own validate basic to enforce denoms that should be used for fees
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
}

// ValidateTxFeeDenomsDecorator will check if denominations used for tx fees are allowed and returns an error otherwise
type ValidateTxFeeDenomsDecorator struct{}

func NewValidateTxFeeDenomsDecorator() ValidateTxFeeDenomsDecorator {
return ValidateTxFeeDenomsDecorator{}
}

func (vbd ValidateTxFeeDenomsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
// no need to validate basic on recheck tx, call next antehandler
if ctx.IsReCheckTx() {
return next(ctx, tx, simulate)
}

feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "ValidateTxFeeDenomsDecorator requires tx to be a FeeTx")
}

for _, c := range feeTx.GetFee() {
if !vbd.isAllowedDenom(c.Denom) {
return ctx, sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"invalid fee supplied. cannot pay fee in %s denomination. Allowed denomination is %s for mainnet and %s for testnet",
c.Denom,
denomMainnet,
denomTestnet,
)
}
}

return next(ctx, tx, simulate)
}

func (vbd ValidateTxFeeDenomsDecorator) isAllowedDenom(denom string) bool {
return denom == denomMainnet || denom == denomTestnet
}
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func New(
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)

anteHandler, err := ante.NewAnteHandler(
anteHandler, err := NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/stretchr/testify v1.8.1
github.com/tendermint/tendermint v0.34.27
github.com/tendermint/tm-db v0.6.7
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a
google.golang.org/grpc v1.64.1
google.golang.org/protobuf v1.34.2
Expand Down

0 comments on commit b3f1a38

Please sign in to comment.