Skip to content
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

move feegrant ante to auth ante #8682

Merged
merged 39 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d7b83ba
move feegrant ante to auth ante
atheeshp Feb 22, 2021
967dd28
Merge branch 'master' into atheesh/move_feegrant_ante
atheeshp Feb 24, 2021
d692f10
Merge branch 'master' into atheesh/move_feegrant_ante
atheeshp Feb 25, 2021
b229812
update ante builder
atheeshp Feb 26, 2021
7eb8897
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Feb 26, 2021
f3874a7
Merge branch 'atheesh/move_feegrant_ante' of github.com:cosmos/cosmos…
atheeshp Feb 26, 2021
c8b38fa
remove commented code
atheeshp Feb 26, 2021
d1d74b8
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 3, 2021
8bd1cfe
review changes
atheeshp Mar 5, 2021
b282167
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 5, 2021
5e2c190
fix lint
atheeshp Mar 5, 2021
3c6c8a1
review changes
atheeshp Mar 5, 2021
3d633df
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 5, 2021
f29391f
review changes
atheeshp Mar 8, 2021
81bfce6
review changes
atheeshp Mar 8, 2021
a652ace
review changes
atheeshp Mar 8, 2021
6fa68e3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 8, 2021
8fdf57b
fix ante builder
atheeshp Mar 8, 2021
a1e986c
Merge branch 'master' into atheesh/move_feegrant_ante
atheeshp Mar 9, 2021
87f2ecd
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 9, 2021
88b38e5
review changes
atheeshp Mar 9, 2021
6fd08a2
Merge branch 'master' into atheesh/move_feegrant_ante
atheeshp Mar 12, 2021
2355779
Merge branch 'master' into atheesh/move_feegrant_ante
atheeshp Mar 12, 2021
3d72ad3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 18, 2021
b5404aa
update ante builder with options struct
atheeshp Mar 18, 2021
587b681
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 18, 2021
4343835
Merge branch 'atheesh/move_feegrant_ante' of github.com:cosmos/cosmos…
atheeshp Mar 18, 2021
576a93a
review changes
atheeshp Mar 18, 2021
0e47dcf
Merge branch 'master' into atheesh/move_feegrant_ante
atheeshp Mar 18, 2021
6ac8011
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 19, 2021
943190c
review changes
atheeshp Mar 19, 2021
3e4649c
Merge branch 'atheesh/move_feegrant_ante' of github.com:cosmos/cosmos…
atheeshp Mar 19, 2021
bb91e5b
Merge branch 'master' into atheesh/move_feegrant_ante
atheeshp Mar 20, 2021
7c08eba
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mo…
atheeshp Mar 22, 2021
e852d05
add changelog
atheeshp Mar 22, 2021
7ca23a6
Merge branch 'atheesh/move_feegrant_ante' of github.com:cosmos/cosmos…
atheeshp Mar 22, 2021
dcfdf08
Merge branch 'master' into atheesh/move_feegrant_ante
amaury1093 Mar 22, 2021
4a6199f
Merge branch 'master' into atheesh/move_feegrant_ante
aaronc Mar 22, 2021
148294a
Merge branch 'master' into atheesh/move_feegrant_ante
atheeshp Mar 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import (
evidencekeeper "github.com/cosmos/cosmos-sdk/x/evidence/keeper"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
feegrant "github.com/cosmos/cosmos-sdk/x/feegrant"
feegrantante "github.com/cosmos/cosmos-sdk/x/feegrant/ante"
feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
Expand Down Expand Up @@ -381,9 +380,13 @@ func NewSimApp(
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetAnteHandler(
feegrantante.NewAnteHandler(
app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer,
encodingConfig.TxConfig.SignModeHandler(),
ante.NewAnteHandler(
app.AccountKeeper, app.BankKeeper,
ante.HandlerOptions{
FeegrantKeeper: &app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
},
),
)
app.SetEndBlocker(app.EndBlocker)
Expand Down
40 changes: 30 additions & 10 deletions x/auth/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,52 @@ package ante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/auth/types"
feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
)

type DefaultSigVerificationGasConsumerHandler func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

// HandlerOptions are the options for ante handler build
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
type HandlerOptions struct {
FeegrantKeeper *feegrantkeeper.Keeper
SigGasConsumer DefaultSigVerificationGasConsumerHandler
SignModeHandler authsigning.SignModeHandler
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, and deducts fees from the first
// signer.
func NewAnteHandler(
ak AccountKeeper, bankKeeper types.BankKeeper,
sigGasConsumer SignatureVerificationGasConsumer,
signModeHandler signing.SignModeHandler,
ak AccountKeeper, bk types.BankKeeper,
anteHandlerOptions HandlerOptions,
) sdk.AnteHandler {
return sdk.ChainAnteDecorators(

var feeGrantAnteHandler sdk.AnteDecorator
feeGrantAnteHandler = NewRejectFeeGranterDecorator()

if anteHandlerOptions.FeegrantKeeper != nil {
feeGrantAnteHandler = NewDeductGrantedFeeDecorator(ak, bk, anteHandlerOptions.FeegrantKeeper)
}

anteDecorators := []sdk.AnteDecorator{
NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
NewRejectExtensionOptionsDecorator(),
NewMempoolFeeDecorator(),
NewValidateBasicDecorator(),
TxTimeoutHeightDecorator{},
NewValidateMemoDecorator(ak),
NewConsumeGasForTxSizeDecorator(ak),
NewRejectFeeGranterDecorator(),
feeGrantAnteHandler,
NewDeductFeeDecorator(ak, bk),
NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators
NewValidateSigCountDecorator(ak),
NewDeductFeeDecorator(ak, bankKeeper),
NewSigGasConsumeDecorator(ak, sigGasConsumer),
NewSigVerificationDecorator(ak, signModeHandler),
NewSigGasConsumeDecorator(ak, anteHandlerOptions.SigGasConsumer),
NewSigVerificationDecorator(ak, anteHandlerOptions.SignModeHandler),
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
NewIncrementSequenceDecorator(ak),
)
}

return sdk.ChainAnteDecorators(anteDecorators...)
}
24 changes: 15 additions & 9 deletions x/auth/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,15 +1011,21 @@ func (suite *AnteTestSuite) TestCustomSignatureVerificationGasConsumer() {
suite.SetupTest(false) // setup

// setup an ante handler that only accepts PubKeyEd25519
suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error {
switch pubkey := sig.PubKey.(type) {
case *ed25519.PubKey:
meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519")
return nil
default:
return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey)
}
}, suite.clientCtx.TxConfig.SignModeHandler())
suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper,
ante.HandlerOptions{
FeegrantKeeper: &suite.app.FeeGrantKeeper,
SigGasConsumer: func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error {
switch pubkey := sig.PubKey.(type) {
case *ed25519.PubKey:
meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519")
return nil
default:
return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey)
}
},
SignModeHandler: suite.clientCtx.TxConfig.SignModeHandler(),
},
)

// Same data for every test cases
accounts := suite.CreateTestAccounts(1)
Expand Down
2 changes: 2 additions & 0 deletions x/auth/ante/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ type AccountKeeper interface {
GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
SetAccount(ctx sdk.Context, acc types.AccountI)
GetModuleAddress(moduleName string) sdk.AccAddress
GetModuleAccount(ctx sdk.Context, moduleName string) types.ModuleAccountI
NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 8 additions & 0 deletions x/auth/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
}

feePayer := feeTx.FeePayer()
feeGranter := feeTx.FeeGranter()

// if feegranter set deduct fee from feegranter account.
// this works with only when feegrant enabled.
if feeGranter != nil {
feePayer = feeGranter
}

feePayerAcc := dfd.ak.GetAccount(ctx, feePayer)

atheeshp marked this conversation as resolved.
Show resolved Hide resolved
if feePayerAcc == nil {
Expand Down
35 changes: 3 additions & 32 deletions x/feegrant/ante/fee.go → x/auth/ante/feegrant_ante.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package ante

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
"github.com/cosmos/cosmos-sdk/x/feegrant/types"
)

Expand All @@ -17,11 +13,11 @@ import (
// CONTRACT: Tx must implement GrantedFeeTx interface to use DeductGrantedFeeDecorator
type DeductGrantedFeeDecorator struct {
ak types.AccountKeeper
k keeper.Keeper
k *feegrantkeeper.Keeper
bk types.BankKeeper
}

func NewDeductGrantedFeeDecorator(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) DeductGrantedFeeDecorator {
func NewDeductGrantedFeeDecorator(ak types.AccountKeeper, bk types.BankKeeper, k *feegrantkeeper.Keeper) DeductGrantedFeeDecorator {
return DeductGrantedFeeDecorator{
ak: ak,
k: k,
Expand All @@ -40,42 +36,17 @@ func (d DeductGrantedFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a GrantedFeeTx")
}

// sanity check from DeductFeeDecorator
if addr := d.ak.GetModuleAddress(authtypes.FeeCollectorName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", authtypes.FeeCollectorName))
}

fee := feeTx.GetFee()
feePayer := feeTx.FeePayer()
feeGranter := feeTx.FeeGranter()

deductFeesFrom := feePayer

// ensure the grant is allowed, if we request a different fee payer
if feeGranter != nil && !feeGranter.Equals(feePayer) {
err := d.k.UseGrantedFees(ctx, feeGranter, feePayer, fee)
if err != nil {
return ctx, sdkerrors.Wrapf(err, "%s not allowed to pay fees from %s", feeGranter, feePayer)
}

deductFeesFrom = feeGranter
}

// now, either way, we know that we are authorized to deduct the fees from the deductFeesFrom account
deductFeesFromAcc := d.ak.GetAccount(ctx, deductFeesFrom)
if deductFeesFromAcc == nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "fee payer address: %s does not exist", deductFeesFrom)
}

// move on if there is no fee to deduct
if fee.IsZero() {
return next(ctx, tx, simulate)
}

// deduct fee if non-zero
err = authante.DeductFees(d.bk, ctx, deductFeesFromAcc, fee)
if err != nil {
return ctx, err
}

return next(ctx, tx, simulate)
Expand Down
Loading