From d7b83bae5fee747b2fd06c477581090f331b91ef Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 22 Feb 2021 14:49:46 +0530 Subject: [PATCH 01/15] move feegrant ante to auth ante --- simapp/app.go | 3 +- x/auth/ante/ante.go | 8 +- x/auth/ante/ante_test.go | 2 +- x/auth/ante/expected_keepers.go | 2 + x/auth/ante/feegrant_ante.go | 81 +++++ x/auth/ante/feegrant_ante_test.go | 242 +++++++++++++ x/auth/ante/sigverify_test.go | 2 +- x/auth/ante/testutil_test.go | 2 +- x/auth/types/expected_keepers.go | 1 + x/feegrant/ante/ante.go | 62 ++-- x/feegrant/ante/fee.go | 134 +++---- x/feegrant/ante/fee_test.go | 568 +++++++++++++++--------------- 12 files changed, 717 insertions(+), 390 deletions(-) create mode 100644 x/auth/ante/feegrant_ante.go create mode 100644 x/auth/ante/feegrant_ante_test.go diff --git a/simapp/app.go b/simapp/app.go index 388c8c50eb8..40a6d274df4 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -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" @@ -435,7 +434,7 @@ func NewSimApp( app.SetInitChainer(app.InitChainer) app.SetBeginBlocker(app.BeginBlocker) app.SetAnteHandler( - feegrantante.NewAnteHandler( + ante.NewAnteHandler( app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler(), ), diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 8cc025bad2d..76151c7adca 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -4,13 +4,14 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "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" ) // 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, + ak AccountKeeper, bankKeeper types.BankKeeper, feeGrantKeeper feegrantkeeper.Keeper, sigGasConsumer SignatureVerificationGasConsumer, signModeHandler signing.SignModeHandler, ) sdk.AnteHandler { @@ -22,10 +23,11 @@ func NewAnteHandler( TxTimeoutHeightDecorator{}, NewValidateMemoDecorator(ak), NewConsumeGasForTxSizeDecorator(ak), - NewRejectFeeGranterDecorator(), + NewDeductGrantedFeeDecorator(ak, bankKeeper, feeGrantKeeper), + // NewRejectFeeGranterDecorator(), NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators NewValidateSigCountDecorator(ak), - NewDeductFeeDecorator(ak, bankKeeper), + // NewDeductFeeDecorator(ak, bankKeeper), NewSigGasConsumeDecorator(ak, sigGasConsumer), NewSigVerificationDecorator(ak, signModeHandler), NewIncrementSequenceDecorator(ak), diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 9b268b1ec60..cdc10f78296 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1009,7 +1009,7 @@ 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 { + suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, 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") diff --git a/x/auth/ante/expected_keepers.go b/x/auth/ante/expected_keepers.go index d8be4312e5c..1231092bfa9 100644 --- a/x/auth/ante/expected_keepers.go +++ b/x/auth/ante/expected_keepers.go @@ -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 } diff --git a/x/auth/ante/feegrant_ante.go b/x/auth/ante/feegrant_ante.go new file mode 100644 index 00000000000..37a3f28270c --- /dev/null +++ b/x/auth/ante/feegrant_ante.go @@ -0,0 +1,81 @@ +package ante + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" + "github.com/cosmos/cosmos-sdk/x/feegrant/types" +) + +// DeductGrantedFeeDecorator deducts fees from fee_payer or fee_granter (if exists a valid fee allowance) of the tx +// If the fee_payer or fee_granter does not have the funds to pay for the fees, return with InsufficientFunds error +// Call next AnteHandler if fees successfully deducted +// CONTRACT: Tx must implement GrantedFeeTx interface to use DeductGrantedFeeDecorator +type DeductGrantedFeeDecorator struct { + ak types.AccountKeeper + k keeper.Keeper + bk types.BankKeeper +} + +func NewDeductGrantedFeeDecorator(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) DeductGrantedFeeDecorator { + return DeductGrantedFeeDecorator{ + ak: ak, + k: k, + bk: bk, + } +} + +// AnteHandle performs a decorated ante-handler responsible for deducting transaction +// fees. Fees will be deducted from the account designated by the FeePayer on a +// transaction by default. However, if the fee payer differs from the transaction +// signer, the handler will check if a fee grant has been authorized. If the +// transaction's signer does not exist, it will be created. +func (d DeductGrantedFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { + feeTx, ok := tx.(sdk.FeeTx) + if !ok { + 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 = DeductFees(d.bk, ctx, deductFeesFromAcc, fee) + if err != nil { + return ctx, err + } + + return next(ctx, tx, simulate) +} diff --git a/x/auth/ante/feegrant_ante_test.go b/x/auth/ante/feegrant_ante_test.go new file mode 100644 index 00000000000..9f9224e58c5 --- /dev/null +++ b/x/auth/ante/feegrant_ante_test.go @@ -0,0 +1,242 @@ +package ante_test + +import ( + "math/rand" + "testing" + "time" + + "github.com/tendermint/tendermint/crypto" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + + "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/simapp/helpers" + "github.com/cosmos/cosmos-sdk/testutil/testdata" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/types/tx/signing" + "github.com/cosmos/cosmos-sdk/x/auth/ante" + authsign "github.com/cosmos/cosmos-sdk/x/auth/signing" + "github.com/cosmos/cosmos-sdk/x/auth/tx" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/feegrant/types" +) + +func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { + suite.SetupTest(false) + // setup + app, ctx := suite.app, suite.ctx + + protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(app.InterfaceRegistry()), tx.DefaultSignModes) + + // this just tests our handler + dfd := ante.NewDeductGrantedFeeDecorator(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) + ourAnteHandler := sdk.ChainAnteDecorators(dfd) + + // this tests the whole stack + anteHandlerStack := suite.anteHandler + + // keys and addresses + priv1, _, addr1 := testdata.KeyTestPubAddr() + priv2, _, addr2 := testdata.KeyTestPubAddr() + priv3, _, addr3 := testdata.KeyTestPubAddr() + priv4, _, addr4 := testdata.KeyTestPubAddr() + priv5, _, addr5 := testdata.KeyTestPubAddr() + + // Set addr1 with insufficient funds + err := simapp.FundAccount(suite.app, suite.ctx, addr1, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))}) + suite.Require().NoError(err) + + // Set addr2 with more funds + err = simapp.FundAccount(suite.app, suite.ctx, addr2, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(99999))}) + suite.Require().NoError(err) + + // grant fee allowance from `addr2` to `addr3` (plenty to pay) + err = app.FeeGrantKeeper.GrantFeeAllowance(ctx, addr2, addr3, &types.BasicFeeAllowance{ + SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 500)), + }) + suite.Require().NoError(err) + + // grant low fee allowance (20atom), to check the tx requesting more than allowed. + err = app.FeeGrantKeeper.GrantFeeAllowance(ctx, addr2, addr4, &types.BasicFeeAllowance{ + SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 20)), + }) + suite.Require().NoError(err) + + cases := map[string]struct { + signerKey cryptotypes.PrivKey + signer sdk.AccAddress + feeAccount sdk.AccAddress + feeAccountKey cryptotypes.PrivKey + handler sdk.AnteHandler + fee int64 + valid bool + }{ + "paying with low funds (only ours)": { + signerKey: priv1, + signer: addr1, + fee: 50, + handler: ourAnteHandler, + valid: false, + }, + "paying with good funds (only ours)": { + signerKey: priv2, + signer: addr2, + fee: 50, + handler: ourAnteHandler, + valid: true, + }, + "paying with no account (only ours)": { + signerKey: priv3, + signer: addr3, + fee: 1, + handler: ourAnteHandler, + valid: false, + }, + "no fee with real account (only ours)": { + signerKey: priv1, + signer: addr1, + fee: 0, + handler: ourAnteHandler, + valid: true, + }, + "no fee with no account (only ours)": { + signerKey: priv5, + signer: addr5, + fee: 0, + handler: ourAnteHandler, + valid: false, + }, + "valid fee grant without account (only ours)": { + signerKey: priv3, + signer: addr3, + feeAccount: addr2, + fee: 50, + handler: ourAnteHandler, + valid: true, + }, + "no fee grant (only ours)": { + signerKey: priv3, + signer: addr3, + feeAccount: addr1, + fee: 2, + handler: ourAnteHandler, + valid: false, + }, + "allowance smaller than requested fee (only ours)": { + signerKey: priv4, + signer: addr4, + feeAccount: addr2, + fee: 50, + handler: ourAnteHandler, + valid: false, + }, + "granter cannot cover allowed fee grant (only ours)": { + signerKey: priv4, + signer: addr4, + feeAccount: addr1, + fee: 50, + handler: ourAnteHandler, + valid: false, + }, + } + + for name, stc := range cases { + tc := stc // to make scopelint happy + suite.T().Run(name, func(t *testing.T) { + fee := sdk.NewCoins(sdk.NewInt64Coin("atom", tc.fee)) + msgs := []sdk.Msg{testdata.NewTestMsg(tc.signer)} + + acc := app.AccountKeeper.GetAccount(ctx, tc.signer) + privs, accNums, seqs := []cryptotypes.PrivKey{tc.signerKey}, []uint64{0}, []uint64{0} + if acc != nil { + accNums, seqs = []uint64{acc.GetAccountNumber()}, []uint64{acc.GetSequence()} + } + + tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, helpers.DefaultGenTxGas, ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...) + suite.Require().NoError(err) + _, err = ourAnteHandler(ctx, tx, false) + if tc.valid { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + _, err = anteHandlerStack(ctx, tx, false) + if tc.valid { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +// don't consume any gas +func SigGasNoConsumer(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params authtypes.Params) error { + return nil +} + +func genTxWithFeeGranter(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, + accSeqs []uint64, feeGranter sdk.AccAddress, priv ...cryptotypes.PrivKey) (sdk.Tx, error) { + sigs := make([]signing.SignatureV2, len(priv)) + + // create a random length memo + r := rand.New(rand.NewSource(time.Now().UnixNano())) + + memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100)) + + signMode := gen.SignModeHandler().DefaultMode() + + // 1st round: set SignatureV2 with empty signatures, to set correct + // signer infos. + for i, p := range priv { + sigs[i] = signing.SignatureV2{ + PubKey: p.PubKey(), + Data: &signing.SingleSignatureData{ + SignMode: signMode, + }, + Sequence: accSeqs[i], + } + } + + tx := gen.NewTxBuilder() + err := tx.SetMsgs(msgs...) + if err != nil { + return nil, err + } + err = tx.SetSignatures(sigs...) + if err != nil { + return nil, err + } + tx.SetMemo(memo) + tx.SetFeeAmount(feeAmt) + tx.SetGasLimit(gas) + tx.SetFeeGranter(feeGranter) + + // 2nd round: once all signer infos are set, every signer can sign. + for i, p := range priv { + signerData := authsign.SignerData{ + ChainID: chainID, + AccountNumber: accNums[i], + Sequence: accSeqs[i], + } + signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) + if err != nil { + panic(err) + } + sig, err := p.Sign(signBytes) + if err != nil { + panic(err) + } + sigs[i].Data.(*signing.SingleSignatureData).Signature = sig + err = tx.SetSignatures(sigs...) + if err != nil { + panic(err) + } + } + + return tx.GetTx(), nil +} diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index fa15e1f179f..28e1c70f2ba 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -200,7 +200,7 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { suite.clientCtx = client.Context{}. WithTxConfig(txConfig) - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, ante.DefaultSigVerificationGasConsumer, txConfig.SignModeHandler()) + suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, txConfig.SignModeHandler()) suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index 78af9743ead..98e84658fad 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -63,7 +63,7 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { suite.clientCtx = client.Context{}. WithTxConfig(encodingConfig.TxConfig) - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, ante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler()) + suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler()) } // CreateTestAccounts creates `numAccs` accounts, and return all relevant diff --git a/x/auth/types/expected_keepers.go b/x/auth/types/expected_keepers.go index 0ab3f9c4308..b230682acb1 100644 --- a/x/auth/types/expected_keepers.go +++ b/x/auth/types/expected_keepers.go @@ -6,5 +6,6 @@ import ( // BankKeeper defines the contract needed for supply related APIs (noalias) type BankKeeper interface { + SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error } diff --git a/x/feegrant/ante/ante.go b/x/feegrant/ante/ante.go index 483868c23e1..3671edd6fc5 100644 --- a/x/feegrant/ante/ante.go +++ b/x/feegrant/ante/ante.go @@ -1,35 +1,35 @@ package ante -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - authante "github.com/cosmos/cosmos-sdk/x/auth/ante" - authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" - "github.com/cosmos/cosmos-sdk/x/auth/signing" - feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" - feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant/types" -) +// import ( +// sdk "github.com/cosmos/cosmos-sdk/types" +// authante "github.com/cosmos/cosmos-sdk/x/auth/ante" +// authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" +// "github.com/cosmos/cosmos-sdk/x/auth/signing" +// feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" +// feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant/types" +// ) -// NewAnteHandler returns an AnteHandler that checks and increments sequence -// numbers, checks signatures & account numbers, and deducts fees from the -// fee_payer or from fee_granter (if valid grant exist). -func NewAnteHandler( - ak authkeeper.AccountKeeper, bankKeeper feegranttypes.BankKeeper, feeGrantKeeper feegrantkeeper.Keeper, - sigGasConsumer authante.SignatureVerificationGasConsumer, signModeHandler signing.SignModeHandler, -) sdk.AnteHandler { +// // NewAnteHandler returns an AnteHandler that checks and increments sequence +// // numbers, checks signatures & account numbers, and deducts fees from the +// // fee_payer or from fee_granter (if valid grant exist). +// func NewAnteHandler( +// ak authkeeper.AccountKeeper, bankKeeper feegranttypes.BankKeeper, feeGrantKeeper feegrantkeeper.Keeper, +// sigGasConsumer authante.SignatureVerificationGasConsumer, signModeHandler signing.SignModeHandler, +// ) sdk.AnteHandler { - return sdk.ChainAnteDecorators( - authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first - authante.NewRejectExtensionOptionsDecorator(), - authante.NewMempoolFeeDecorator(), - authante.NewValidateBasicDecorator(), - authante.TxTimeoutHeightDecorator{}, - authante.NewValidateMemoDecorator(ak), - authante.NewConsumeGasForTxSizeDecorator(ak), - NewDeductGrantedFeeDecorator(ak, bankKeeper, feeGrantKeeper), - authante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators - authante.NewValidateSigCountDecorator(ak), - authante.NewSigGasConsumeDecorator(ak, sigGasConsumer), - authante.NewSigVerificationDecorator(ak, signModeHandler), - authante.NewIncrementSequenceDecorator(ak), // innermost AnteDecorator - ) -} +// return sdk.ChainAnteDecorators( +// authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first +// authante.NewRejectExtensionOptionsDecorator(), +// authante.NewMempoolFeeDecorator(), +// authante.NewValidateBasicDecorator(), +// authante.TxTimeoutHeightDecorator{}, +// authante.NewValidateMemoDecorator(ak), +// authante.NewConsumeGasForTxSizeDecorator(ak), +// NewDeductGrantedFeeDecorator(ak, bankKeeper, feeGrantKeeper), +// authante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators +// authante.NewValidateSigCountDecorator(ak), +// authante.NewSigGasConsumeDecorator(ak, sigGasConsumer), +// authante.NewSigVerificationDecorator(ak, signModeHandler), +// authante.NewIncrementSequenceDecorator(ak), // innermost AnteDecorator +// ) +// } diff --git a/x/feegrant/ante/fee.go b/x/feegrant/ante/fee.go index 52cbda7210e..f0843670e5d 100644 --- a/x/feegrant/ante/fee.go +++ b/x/feegrant/ante/fee.go @@ -1,82 +1,82 @@ package ante -import ( - "fmt" +// 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" - "github.com/cosmos/cosmos-sdk/x/feegrant/types" -) +// 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" +// "github.com/cosmos/cosmos-sdk/x/feegrant/types" +// ) -// DeductGrantedFeeDecorator deducts fees from fee_payer or fee_granter (if exists a valid fee allowance) of the tx -// If the fee_payer or fee_granter does not have the funds to pay for the fees, return with InsufficientFunds error -// Call next AnteHandler if fees successfully deducted -// CONTRACT: Tx must implement GrantedFeeTx interface to use DeductGrantedFeeDecorator -type DeductGrantedFeeDecorator struct { - ak types.AccountKeeper - k keeper.Keeper - bk types.BankKeeper -} +// // DeductGrantedFeeDecorator deducts fees from fee_payer or fee_granter (if exists a valid fee allowance) of the tx +// // If the fee_payer or fee_granter does not have the funds to pay for the fees, return with InsufficientFunds error +// // Call next AnteHandler if fees successfully deducted +// // CONTRACT: Tx must implement GrantedFeeTx interface to use DeductGrantedFeeDecorator +// type DeductGrantedFeeDecorator struct { +// ak types.AccountKeeper +// k keeper.Keeper +// bk types.BankKeeper +// } -func NewDeductGrantedFeeDecorator(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) DeductGrantedFeeDecorator { - return DeductGrantedFeeDecorator{ - ak: ak, - k: k, - bk: bk, - } -} +// func NewDeductGrantedFeeDecorator(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) DeductGrantedFeeDecorator { +// return DeductGrantedFeeDecorator{ +// ak: ak, +// k: k, +// bk: bk, +// } +// } -// AnteHandle performs a decorated ante-handler responsible for deducting transaction -// fees. Fees will be deducted from the account designated by the FeePayer on a -// transaction by default. However, if the fee payer differs from the transaction -// signer, the handler will check if a fee grant has been authorized. If the -// transaction's signer does not exist, it will be created. -func (d DeductGrantedFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { - feeTx, ok := tx.(sdk.FeeTx) - if !ok { - return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a GrantedFeeTx") - } +// // AnteHandle performs a decorated ante-handler responsible for deducting transaction +// // fees. Fees will be deducted from the account designated by the FeePayer on a +// // transaction by default. However, if the fee payer differs from the transaction +// // signer, the handler will check if a fee grant has been authorized. If the +// // transaction's signer does not exist, it will be created. +// func (d DeductGrantedFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { +// feeTx, ok := tx.(sdk.FeeTx) +// if !ok { +// 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)) - } +// // 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() +// fee := feeTx.GetFee() +// feePayer := feeTx.FeePayer() +// feeGranter := feeTx.FeeGranter() - deductFeesFrom := feePayer +// 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) - } +// // 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 - } +// 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) - } +// // 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) - } +// // 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 - } +// // deduct fee if non-zero +// err = authante.DeductFees(d.bk, ctx, deductFeesFromAcc, fee) +// if err != nil { +// return ctx, err +// } - return next(ctx, tx, simulate) -} +// return next(ctx, tx, simulate) +// } diff --git a/x/feegrant/ante/fee_test.go b/x/feegrant/ante/fee_test.go index 1a924db6a38..0ee33fbfe07 100644 --- a/x/feegrant/ante/fee_test.go +++ b/x/feegrant/ante/fee_test.go @@ -1,286 +1,286 @@ package ante_test -import ( - "math/rand" - "testing" - "time" - - "github.com/stretchr/testify/suite" - "github.com/tendermint/tendermint/crypto" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/codec" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - - "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/simapp/helpers" - "github.com/cosmos/cosmos-sdk/testutil/testdata" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/simulation" - "github.com/cosmos/cosmos-sdk/types/tx/signing" - authante "github.com/cosmos/cosmos-sdk/x/auth/ante" - authsign "github.com/cosmos/cosmos-sdk/x/auth/signing" - "github.com/cosmos/cosmos-sdk/x/auth/tx" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/cosmos/cosmos-sdk/x/feegrant/ante" - "github.com/cosmos/cosmos-sdk/x/feegrant/types" -) - -// AnteTestSuite is a test suite to be used with ante handler tests. -type AnteTestSuite struct { - suite.Suite - - app *simapp.SimApp - anteHandler sdk.AnteHandler - ctx sdk.Context - clientCtx client.Context - txBuilder client.TxBuilder -} - -// SetupTest setups a new test, with new app, context, and anteHandler. -func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { - suite.app, suite.ctx = createTestApp(isCheckTx) - suite.ctx = suite.ctx.WithBlockHeight(1) - - // Set up TxConfig. - encodingConfig := simapp.MakeTestEncodingConfig() - // We're using TestMsg encoding in some tests, so register it here. - encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil) - testdata.RegisterInterfaces(encodingConfig.InterfaceRegistry) - - suite.clientCtx = client.Context{}. - WithTxConfig(encodingConfig.TxConfig) - - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, authante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler()) -} - -func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { - suite.SetupTest(false) - // setup - app, ctx := suite.app, suite.ctx - - protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(app.InterfaceRegistry()), tx.DefaultSignModes) - - // this just tests our handler - dfd := ante.NewDeductGrantedFeeDecorator(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) - ourAnteHandler := sdk.ChainAnteDecorators(dfd) - - // this tests the whole stack - anteHandlerStack := suite.anteHandler - - // keys and addresses - priv1, _, addr1 := testdata.KeyTestPubAddr() - priv2, _, addr2 := testdata.KeyTestPubAddr() - priv3, _, addr3 := testdata.KeyTestPubAddr() - priv4, _, addr4 := testdata.KeyTestPubAddr() - priv5, _, addr5 := testdata.KeyTestPubAddr() - - // Set addr1 with insufficient funds - err := simapp.FundAccount(suite.app, suite.ctx, addr1, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))}) - suite.Require().NoError(err) - - // Set addr2 with more funds - err = simapp.FundAccount(suite.app, suite.ctx, addr2, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(99999))}) - suite.Require().NoError(err) - - // grant fee allowance from `addr2` to `addr3` (plenty to pay) - err = app.FeeGrantKeeper.GrantFeeAllowance(ctx, addr2, addr3, &types.BasicFeeAllowance{ - SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 500)), - }) - suite.Require().NoError(err) - - // grant low fee allowance (20atom), to check the tx requesting more than allowed. - err = app.FeeGrantKeeper.GrantFeeAllowance(ctx, addr2, addr4, &types.BasicFeeAllowance{ - SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 20)), - }) - suite.Require().NoError(err) - - cases := map[string]struct { - signerKey cryptotypes.PrivKey - signer sdk.AccAddress - feeAccount sdk.AccAddress - feeAccountKey cryptotypes.PrivKey - handler sdk.AnteHandler - fee int64 - valid bool - }{ - "paying with low funds (only ours)": { - signerKey: priv1, - signer: addr1, - fee: 50, - handler: ourAnteHandler, - valid: false, - }, - "paying with good funds (only ours)": { - signerKey: priv2, - signer: addr2, - fee: 50, - handler: ourAnteHandler, - valid: true, - }, - "paying with no account (only ours)": { - signerKey: priv3, - signer: addr3, - fee: 1, - handler: ourAnteHandler, - valid: false, - }, - "no fee with real account (only ours)": { - signerKey: priv1, - signer: addr1, - fee: 0, - handler: ourAnteHandler, - valid: true, - }, - "no fee with no account (only ours)": { - signerKey: priv5, - signer: addr5, - fee: 0, - handler: ourAnteHandler, - valid: false, - }, - "valid fee grant without account (only ours)": { - signerKey: priv3, - signer: addr3, - feeAccount: addr2, - fee: 50, - handler: ourAnteHandler, - valid: true, - }, - "no fee grant (only ours)": { - signerKey: priv3, - signer: addr3, - feeAccount: addr1, - fee: 2, - handler: ourAnteHandler, - valid: false, - }, - "allowance smaller than requested fee (only ours)": { - signerKey: priv4, - signer: addr4, - feeAccount: addr2, - fee: 50, - handler: ourAnteHandler, - valid: false, - }, - "granter cannot cover allowed fee grant (only ours)": { - signerKey: priv4, - signer: addr4, - feeAccount: addr1, - fee: 50, - handler: ourAnteHandler, - valid: false, - }, - } - - for name, stc := range cases { - tc := stc // to make scopelint happy - suite.T().Run(name, func(t *testing.T) { - fee := sdk.NewCoins(sdk.NewInt64Coin("atom", tc.fee)) - msgs := []sdk.Msg{testdata.NewTestMsg(tc.signer)} - - acc := app.AccountKeeper.GetAccount(ctx, tc.signer) - privs, accNums, seqs := []cryptotypes.PrivKey{tc.signerKey}, []uint64{0}, []uint64{0} - if acc != nil { - accNums, seqs = []uint64{acc.GetAccountNumber()}, []uint64{acc.GetSequence()} - } - - tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, helpers.DefaultGenTxGas, ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...) - suite.Require().NoError(err) - _, err = ourAnteHandler(ctx, tx, false) - if tc.valid { - suite.Require().NoError(err) - } else { - suite.Require().Error(err) - } - - _, err = anteHandlerStack(ctx, tx, false) - if tc.valid { - suite.Require().NoError(err) - } else { - suite.Require().Error(err) - } - }) - } -} - -// returns context and app with params set on account keeper -func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) { - app := simapp.Setup(isCheckTx) - ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{}) - app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams()) - - return app, ctx -} - -// don't consume any gas -func SigGasNoConsumer(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params authtypes.Params) error { - return nil -} - -func genTxWithFeeGranter(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, - accSeqs []uint64, feeGranter sdk.AccAddress, priv ...cryptotypes.PrivKey) (sdk.Tx, error) { - sigs := make([]signing.SignatureV2, len(priv)) - - // create a random length memo - r := rand.New(rand.NewSource(time.Now().UnixNano())) - - memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100)) - - signMode := gen.SignModeHandler().DefaultMode() - - // 1st round: set SignatureV2 with empty signatures, to set correct - // signer infos. - for i, p := range priv { - sigs[i] = signing.SignatureV2{ - PubKey: p.PubKey(), - Data: &signing.SingleSignatureData{ - SignMode: signMode, - }, - Sequence: accSeqs[i], - } - } - - tx := gen.NewTxBuilder() - err := tx.SetMsgs(msgs...) - if err != nil { - return nil, err - } - err = tx.SetSignatures(sigs...) - if err != nil { - return nil, err - } - tx.SetMemo(memo) - tx.SetFeeAmount(feeAmt) - tx.SetGasLimit(gas) - tx.SetFeeGranter(feeGranter) - - // 2nd round: once all signer infos are set, every signer can sign. - for i, p := range priv { - signerData := authsign.SignerData{ - ChainID: chainID, - AccountNumber: accNums[i], - Sequence: accSeqs[i], - } - signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) - if err != nil { - panic(err) - } - sig, err := p.Sign(signBytes) - if err != nil { - panic(err) - } - sigs[i].Data.(*signing.SingleSignatureData).Signature = sig - err = tx.SetSignatures(sigs...) - if err != nil { - panic(err) - } - } - - return tx.GetTx(), nil -} - -func TestAnteTestSuite(t *testing.T) { - suite.Run(t, new(AnteTestSuite)) -} +// import ( +// "math/rand" +// "testing" +// "time" + +// "github.com/stretchr/testify/suite" +// "github.com/tendermint/tendermint/crypto" +// tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + +// "github.com/cosmos/cosmos-sdk/client" +// "github.com/cosmos/cosmos-sdk/codec" +// cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + +// "github.com/cosmos/cosmos-sdk/simapp" +// "github.com/cosmos/cosmos-sdk/simapp/helpers" +// "github.com/cosmos/cosmos-sdk/testutil/testdata" +// sdk "github.com/cosmos/cosmos-sdk/types" +// "github.com/cosmos/cosmos-sdk/types/simulation" +// "github.com/cosmos/cosmos-sdk/types/tx/signing" +// authante "github.com/cosmos/cosmos-sdk/x/auth/ante" +// authsign "github.com/cosmos/cosmos-sdk/x/auth/signing" +// "github.com/cosmos/cosmos-sdk/x/auth/tx" +// authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" +// "github.com/cosmos/cosmos-sdk/x/feegrant/ante" +// "github.com/cosmos/cosmos-sdk/x/feegrant/types" +// ) + +// // AnteTestSuite is a test suite to be used with ante handler tests. +// type AnteTestSuite struct { +// suite.Suite + +// app *simapp.SimApp +// anteHandler sdk.AnteHandler +// ctx sdk.Context +// clientCtx client.Context +// txBuilder client.TxBuilder +// } + +// // SetupTest setups a new test, with new app, context, and anteHandler. +// func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { +// suite.app, suite.ctx = createTestApp(isCheckTx) +// suite.ctx = suite.ctx.WithBlockHeight(1) + +// // Set up TxConfig. +// encodingConfig := simapp.MakeTestEncodingConfig() +// // We're using TestMsg encoding in some tests, so register it here. +// encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil) +// testdata.RegisterInterfaces(encodingConfig.InterfaceRegistry) + +// suite.clientCtx = client.Context{}. +// WithTxConfig(encodingConfig.TxConfig) + +// suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, authante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler()) +// } + +// func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { +// suite.SetupTest(false) +// // setup +// app, ctx := suite.app, suite.ctx + +// protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(app.InterfaceRegistry()), tx.DefaultSignModes) + +// // this just tests our handler +// dfd := ante.NewDeductGrantedFeeDecorator(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) +// ourAnteHandler := sdk.ChainAnteDecorators(dfd) + +// // this tests the whole stack +// anteHandlerStack := suite.anteHandler + +// // keys and addresses +// priv1, _, addr1 := testdata.KeyTestPubAddr() +// priv2, _, addr2 := testdata.KeyTestPubAddr() +// priv3, _, addr3 := testdata.KeyTestPubAddr() +// priv4, _, addr4 := testdata.KeyTestPubAddr() +// priv5, _, addr5 := testdata.KeyTestPubAddr() + +// // Set addr1 with insufficient funds +// err := simapp.FundAccount(suite.app, suite.ctx, addr1, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))}) +// suite.Require().NoError(err) + +// // Set addr2 with more funds +// err = simapp.FundAccount(suite.app, suite.ctx, addr2, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(99999))}) +// suite.Require().NoError(err) + +// // grant fee allowance from `addr2` to `addr3` (plenty to pay) +// err = app.FeeGrantKeeper.GrantFeeAllowance(ctx, addr2, addr3, &types.BasicFeeAllowance{ +// SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 500)), +// }) +// suite.Require().NoError(err) + +// // grant low fee allowance (20atom), to check the tx requesting more than allowed. +// err = app.FeeGrantKeeper.GrantFeeAllowance(ctx, addr2, addr4, &types.BasicFeeAllowance{ +// SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 20)), +// }) +// suite.Require().NoError(err) + +// cases := map[string]struct { +// signerKey cryptotypes.PrivKey +// signer sdk.AccAddress +// feeAccount sdk.AccAddress +// feeAccountKey cryptotypes.PrivKey +// handler sdk.AnteHandler +// fee int64 +// valid bool +// }{ +// "paying with low funds (only ours)": { +// signerKey: priv1, +// signer: addr1, +// fee: 50, +// handler: ourAnteHandler, +// valid: false, +// }, +// "paying with good funds (only ours)": { +// signerKey: priv2, +// signer: addr2, +// fee: 50, +// handler: ourAnteHandler, +// valid: true, +// }, +// "paying with no account (only ours)": { +// signerKey: priv3, +// signer: addr3, +// fee: 1, +// handler: ourAnteHandler, +// valid: false, +// }, +// "no fee with real account (only ours)": { +// signerKey: priv1, +// signer: addr1, +// fee: 0, +// handler: ourAnteHandler, +// valid: true, +// }, +// "no fee with no account (only ours)": { +// signerKey: priv5, +// signer: addr5, +// fee: 0, +// handler: ourAnteHandler, +// valid: false, +// }, +// "valid fee grant without account (only ours)": { +// signerKey: priv3, +// signer: addr3, +// feeAccount: addr2, +// fee: 50, +// handler: ourAnteHandler, +// valid: true, +// }, +// "no fee grant (only ours)": { +// signerKey: priv3, +// signer: addr3, +// feeAccount: addr1, +// fee: 2, +// handler: ourAnteHandler, +// valid: false, +// }, +// "allowance smaller than requested fee (only ours)": { +// signerKey: priv4, +// signer: addr4, +// feeAccount: addr2, +// fee: 50, +// handler: ourAnteHandler, +// valid: false, +// }, +// "granter cannot cover allowed fee grant (only ours)": { +// signerKey: priv4, +// signer: addr4, +// feeAccount: addr1, +// fee: 50, +// handler: ourAnteHandler, +// valid: false, +// }, +// } + +// for name, stc := range cases { +// tc := stc // to make scopelint happy +// suite.T().Run(name, func(t *testing.T) { +// fee := sdk.NewCoins(sdk.NewInt64Coin("atom", tc.fee)) +// msgs := []sdk.Msg{testdata.NewTestMsg(tc.signer)} + +// acc := app.AccountKeeper.GetAccount(ctx, tc.signer) +// privs, accNums, seqs := []cryptotypes.PrivKey{tc.signerKey}, []uint64{0}, []uint64{0} +// if acc != nil { +// accNums, seqs = []uint64{acc.GetAccountNumber()}, []uint64{acc.GetSequence()} +// } + +// tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, helpers.DefaultGenTxGas, ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...) +// suite.Require().NoError(err) +// _, err = ourAnteHandler(ctx, tx, false) +// if tc.valid { +// suite.Require().NoError(err) +// } else { +// suite.Require().Error(err) +// } + +// _, err = anteHandlerStack(ctx, tx, false) +// if tc.valid { +// suite.Require().NoError(err) +// } else { +// suite.Require().Error(err) +// } +// }) +// } +// } + +// // returns context and app with params set on account keeper +// func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) { +// app := simapp.Setup(isCheckTx) +// ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{}) +// app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams()) + +// return app, ctx +// } + +// // don't consume any gas +// func SigGasNoConsumer(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params authtypes.Params) error { +// return nil +// } + +// func genTxWithFeeGranter(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, +// accSeqs []uint64, feeGranter sdk.AccAddress, priv ...cryptotypes.PrivKey) (sdk.Tx, error) { +// sigs := make([]signing.SignatureV2, len(priv)) + +// // create a random length memo +// r := rand.New(rand.NewSource(time.Now().UnixNano())) + +// memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100)) + +// signMode := gen.SignModeHandler().DefaultMode() + +// // 1st round: set SignatureV2 with empty signatures, to set correct +// // signer infos. +// for i, p := range priv { +// sigs[i] = signing.SignatureV2{ +// PubKey: p.PubKey(), +// Data: &signing.SingleSignatureData{ +// SignMode: signMode, +// }, +// Sequence: accSeqs[i], +// } +// } + +// tx := gen.NewTxBuilder() +// err := tx.SetMsgs(msgs...) +// if err != nil { +// return nil, err +// } +// err = tx.SetSignatures(sigs...) +// if err != nil { +// return nil, err +// } +// tx.SetMemo(memo) +// tx.SetFeeAmount(feeAmt) +// tx.SetGasLimit(gas) +// tx.SetFeeGranter(feeGranter) + +// // 2nd round: once all signer infos are set, every signer can sign. +// for i, p := range priv { +// signerData := authsign.SignerData{ +// ChainID: chainID, +// AccountNumber: accNums[i], +// Sequence: accSeqs[i], +// } +// signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) +// if err != nil { +// panic(err) +// } +// sig, err := p.Sign(signBytes) +// if err != nil { +// panic(err) +// } +// sigs[i].Data.(*signing.SingleSignatureData).Signature = sig +// err = tx.SetSignatures(sigs...) +// if err != nil { +// panic(err) +// } +// } + +// return tx.GetTx(), nil +// } + +// func TestAnteTestSuite(t *testing.T) { +// suite.Run(t, new(AnteTestSuite)) +// } From b2298127e860c03f8590792eea14f84ca4c87856 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 26 Feb 2021 12:50:02 +0530 Subject: [PATCH 02/15] update ante builder --- simapp/app.go | 2 +- x/auth/ante/ante.go | 19 +++++++++++++++---- x/auth/ante/ante_test.go | 2 +- x/auth/ante/feegrant_ante.go | 6 +++--- x/auth/ante/feegrant_ante_test.go | 2 +- x/auth/ante/sigverify_test.go | 2 +- x/auth/ante/testutil_test.go | 2 +- 7 files changed, 23 insertions(+), 12 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 40a6d274df4..d14e34e8604 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -435,7 +435,7 @@ func NewSimApp( app.SetBeginBlocker(app.BeginBlocker) app.SetAnteHandler( ante.NewAnteHandler( - app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, + app.AccountKeeper, app.BankKeeper, &app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler(), ), ) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 76151c7adca..594c42a83f8 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -11,11 +11,11 @@ import ( // numbers, checks signatures & account numbers, and deducts fees from the first // signer. func NewAnteHandler( - ak AccountKeeper, bankKeeper types.BankKeeper, feeGrantKeeper feegrantkeeper.Keeper, + ak AccountKeeper, bankKeeper types.BankKeeper, feeGrantKeeper *feegrantkeeper.Keeper, sigGasConsumer SignatureVerificationGasConsumer, signModeHandler signing.SignModeHandler, ) sdk.AnteHandler { - return sdk.ChainAnteDecorators( + anteDecorators := []sdk.AnteDecorator{ NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first NewRejectExtensionOptionsDecorator(), NewMempoolFeeDecorator(), @@ -23,13 +23,24 @@ func NewAnteHandler( TxTimeoutHeightDecorator{}, NewValidateMemoDecorator(ak), NewConsumeGasForTxSizeDecorator(ak), + } + + if feeGrantKeeper == nil { + anteDecorators = append( + anteDecorators, + NewRejectFeeGranterDecorator(), + ) + } + + anteDecorators = append( + anteDecorators, NewDeductGrantedFeeDecorator(ak, bankKeeper, feeGrantKeeper), - // NewRejectFeeGranterDecorator(), NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators NewValidateSigCountDecorator(ak), - // NewDeductFeeDecorator(ak, bankKeeper), NewSigGasConsumeDecorator(ak, sigGasConsumer), NewSigVerificationDecorator(ak, signModeHandler), NewIncrementSequenceDecorator(ak), ) + + return sdk.ChainAnteDecorators(anteDecorators...) } diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index cdc10f78296..6bd95f5a735 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1009,7 +1009,7 @@ 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, suite.app.FeeGrantKeeper, func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { + suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, &suite.app.FeeGrantKeeper, 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") diff --git a/x/auth/ante/feegrant_ante.go b/x/auth/ante/feegrant_ante.go index 37a3f28270c..f3d3b7285bb 100644 --- a/x/auth/ante/feegrant_ante.go +++ b/x/auth/ante/feegrant_ante.go @@ -6,7 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" 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" ) @@ -16,11 +16,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, diff --git a/x/auth/ante/feegrant_ante_test.go b/x/auth/ante/feegrant_ante_test.go index 9f9224e58c5..e03336271ab 100644 --- a/x/auth/ante/feegrant_ante_test.go +++ b/x/auth/ante/feegrant_ante_test.go @@ -32,7 +32,7 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(app.InterfaceRegistry()), tx.DefaultSignModes) // this just tests our handler - dfd := ante.NewDeductGrantedFeeDecorator(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) + dfd := ante.NewDeductGrantedFeeDecorator(app.AccountKeeper, app.BankKeeper, &app.FeeGrantKeeper) ourAnteHandler := sdk.ChainAnteDecorators(dfd) // this tests the whole stack diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index 28e1c70f2ba..9ffb11d9b00 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -200,7 +200,7 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { suite.clientCtx = client.Context{}. WithTxConfig(txConfig) - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, txConfig.SignModeHandler()) + suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, &suite.app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, txConfig.SignModeHandler()) suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index 98e84658fad..dc60b2716f3 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -63,7 +63,7 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { suite.clientCtx = client.Context{}. WithTxConfig(encodingConfig.TxConfig) - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler()) + suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, &suite.app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler()) } // CreateTestAccounts creates `numAccs` accounts, and return all relevant From c8b38fae4c9b1cbb67e6019d8013ee16e48abb96 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 26 Feb 2021 12:55:18 +0530 Subject: [PATCH 03/15] remove commented code --- x/feegrant/ante/ante.go | 35 ----- x/feegrant/ante/fee.go | 82 ----------- x/feegrant/ante/fee_test.go | 286 ------------------------------------ 3 files changed, 403 deletions(-) delete mode 100644 x/feegrant/ante/ante.go delete mode 100644 x/feegrant/ante/fee.go delete mode 100644 x/feegrant/ante/fee_test.go diff --git a/x/feegrant/ante/ante.go b/x/feegrant/ante/ante.go deleted file mode 100644 index 3671edd6fc5..00000000000 --- a/x/feegrant/ante/ante.go +++ /dev/null @@ -1,35 +0,0 @@ -package ante - -// import ( -// sdk "github.com/cosmos/cosmos-sdk/types" -// authante "github.com/cosmos/cosmos-sdk/x/auth/ante" -// authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" -// "github.com/cosmos/cosmos-sdk/x/auth/signing" -// feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" -// feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant/types" -// ) - -// // NewAnteHandler returns an AnteHandler that checks and increments sequence -// // numbers, checks signatures & account numbers, and deducts fees from the -// // fee_payer or from fee_granter (if valid grant exist). -// func NewAnteHandler( -// ak authkeeper.AccountKeeper, bankKeeper feegranttypes.BankKeeper, feeGrantKeeper feegrantkeeper.Keeper, -// sigGasConsumer authante.SignatureVerificationGasConsumer, signModeHandler signing.SignModeHandler, -// ) sdk.AnteHandler { - -// return sdk.ChainAnteDecorators( -// authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first -// authante.NewRejectExtensionOptionsDecorator(), -// authante.NewMempoolFeeDecorator(), -// authante.NewValidateBasicDecorator(), -// authante.TxTimeoutHeightDecorator{}, -// authante.NewValidateMemoDecorator(ak), -// authante.NewConsumeGasForTxSizeDecorator(ak), -// NewDeductGrantedFeeDecorator(ak, bankKeeper, feeGrantKeeper), -// authante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators -// authante.NewValidateSigCountDecorator(ak), -// authante.NewSigGasConsumeDecorator(ak, sigGasConsumer), -// authante.NewSigVerificationDecorator(ak, signModeHandler), -// authante.NewIncrementSequenceDecorator(ak), // innermost AnteDecorator -// ) -// } diff --git a/x/feegrant/ante/fee.go b/x/feegrant/ante/fee.go deleted file mode 100644 index f0843670e5d..00000000000 --- a/x/feegrant/ante/fee.go +++ /dev/null @@ -1,82 +0,0 @@ -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" -// "github.com/cosmos/cosmos-sdk/x/feegrant/types" -// ) - -// // DeductGrantedFeeDecorator deducts fees from fee_payer or fee_granter (if exists a valid fee allowance) of the tx -// // If the fee_payer or fee_granter does not have the funds to pay for the fees, return with InsufficientFunds error -// // Call next AnteHandler if fees successfully deducted -// // CONTRACT: Tx must implement GrantedFeeTx interface to use DeductGrantedFeeDecorator -// type DeductGrantedFeeDecorator struct { -// ak types.AccountKeeper -// k keeper.Keeper -// bk types.BankKeeper -// } - -// func NewDeductGrantedFeeDecorator(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) DeductGrantedFeeDecorator { -// return DeductGrantedFeeDecorator{ -// ak: ak, -// k: k, -// bk: bk, -// } -// } - -// // AnteHandle performs a decorated ante-handler responsible for deducting transaction -// // fees. Fees will be deducted from the account designated by the FeePayer on a -// // transaction by default. However, if the fee payer differs from the transaction -// // signer, the handler will check if a fee grant has been authorized. If the -// // transaction's signer does not exist, it will be created. -// func (d DeductGrantedFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { -// feeTx, ok := tx.(sdk.FeeTx) -// if !ok { -// 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) -// } diff --git a/x/feegrant/ante/fee_test.go b/x/feegrant/ante/fee_test.go deleted file mode 100644 index 0ee33fbfe07..00000000000 --- a/x/feegrant/ante/fee_test.go +++ /dev/null @@ -1,286 +0,0 @@ -package ante_test - -// import ( -// "math/rand" -// "testing" -// "time" - -// "github.com/stretchr/testify/suite" -// "github.com/tendermint/tendermint/crypto" -// tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - -// "github.com/cosmos/cosmos-sdk/client" -// "github.com/cosmos/cosmos-sdk/codec" -// cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - -// "github.com/cosmos/cosmos-sdk/simapp" -// "github.com/cosmos/cosmos-sdk/simapp/helpers" -// "github.com/cosmos/cosmos-sdk/testutil/testdata" -// sdk "github.com/cosmos/cosmos-sdk/types" -// "github.com/cosmos/cosmos-sdk/types/simulation" -// "github.com/cosmos/cosmos-sdk/types/tx/signing" -// authante "github.com/cosmos/cosmos-sdk/x/auth/ante" -// authsign "github.com/cosmos/cosmos-sdk/x/auth/signing" -// "github.com/cosmos/cosmos-sdk/x/auth/tx" -// authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" -// "github.com/cosmos/cosmos-sdk/x/feegrant/ante" -// "github.com/cosmos/cosmos-sdk/x/feegrant/types" -// ) - -// // AnteTestSuite is a test suite to be used with ante handler tests. -// type AnteTestSuite struct { -// suite.Suite - -// app *simapp.SimApp -// anteHandler sdk.AnteHandler -// ctx sdk.Context -// clientCtx client.Context -// txBuilder client.TxBuilder -// } - -// // SetupTest setups a new test, with new app, context, and anteHandler. -// func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { -// suite.app, suite.ctx = createTestApp(isCheckTx) -// suite.ctx = suite.ctx.WithBlockHeight(1) - -// // Set up TxConfig. -// encodingConfig := simapp.MakeTestEncodingConfig() -// // We're using TestMsg encoding in some tests, so register it here. -// encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil) -// testdata.RegisterInterfaces(encodingConfig.InterfaceRegistry) - -// suite.clientCtx = client.Context{}. -// WithTxConfig(encodingConfig.TxConfig) - -// suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, authante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler()) -// } - -// func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { -// suite.SetupTest(false) -// // setup -// app, ctx := suite.app, suite.ctx - -// protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(app.InterfaceRegistry()), tx.DefaultSignModes) - -// // this just tests our handler -// dfd := ante.NewDeductGrantedFeeDecorator(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) -// ourAnteHandler := sdk.ChainAnteDecorators(dfd) - -// // this tests the whole stack -// anteHandlerStack := suite.anteHandler - -// // keys and addresses -// priv1, _, addr1 := testdata.KeyTestPubAddr() -// priv2, _, addr2 := testdata.KeyTestPubAddr() -// priv3, _, addr3 := testdata.KeyTestPubAddr() -// priv4, _, addr4 := testdata.KeyTestPubAddr() -// priv5, _, addr5 := testdata.KeyTestPubAddr() - -// // Set addr1 with insufficient funds -// err := simapp.FundAccount(suite.app, suite.ctx, addr1, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))}) -// suite.Require().NoError(err) - -// // Set addr2 with more funds -// err = simapp.FundAccount(suite.app, suite.ctx, addr2, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(99999))}) -// suite.Require().NoError(err) - -// // grant fee allowance from `addr2` to `addr3` (plenty to pay) -// err = app.FeeGrantKeeper.GrantFeeAllowance(ctx, addr2, addr3, &types.BasicFeeAllowance{ -// SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 500)), -// }) -// suite.Require().NoError(err) - -// // grant low fee allowance (20atom), to check the tx requesting more than allowed. -// err = app.FeeGrantKeeper.GrantFeeAllowance(ctx, addr2, addr4, &types.BasicFeeAllowance{ -// SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 20)), -// }) -// suite.Require().NoError(err) - -// cases := map[string]struct { -// signerKey cryptotypes.PrivKey -// signer sdk.AccAddress -// feeAccount sdk.AccAddress -// feeAccountKey cryptotypes.PrivKey -// handler sdk.AnteHandler -// fee int64 -// valid bool -// }{ -// "paying with low funds (only ours)": { -// signerKey: priv1, -// signer: addr1, -// fee: 50, -// handler: ourAnteHandler, -// valid: false, -// }, -// "paying with good funds (only ours)": { -// signerKey: priv2, -// signer: addr2, -// fee: 50, -// handler: ourAnteHandler, -// valid: true, -// }, -// "paying with no account (only ours)": { -// signerKey: priv3, -// signer: addr3, -// fee: 1, -// handler: ourAnteHandler, -// valid: false, -// }, -// "no fee with real account (only ours)": { -// signerKey: priv1, -// signer: addr1, -// fee: 0, -// handler: ourAnteHandler, -// valid: true, -// }, -// "no fee with no account (only ours)": { -// signerKey: priv5, -// signer: addr5, -// fee: 0, -// handler: ourAnteHandler, -// valid: false, -// }, -// "valid fee grant without account (only ours)": { -// signerKey: priv3, -// signer: addr3, -// feeAccount: addr2, -// fee: 50, -// handler: ourAnteHandler, -// valid: true, -// }, -// "no fee grant (only ours)": { -// signerKey: priv3, -// signer: addr3, -// feeAccount: addr1, -// fee: 2, -// handler: ourAnteHandler, -// valid: false, -// }, -// "allowance smaller than requested fee (only ours)": { -// signerKey: priv4, -// signer: addr4, -// feeAccount: addr2, -// fee: 50, -// handler: ourAnteHandler, -// valid: false, -// }, -// "granter cannot cover allowed fee grant (only ours)": { -// signerKey: priv4, -// signer: addr4, -// feeAccount: addr1, -// fee: 50, -// handler: ourAnteHandler, -// valid: false, -// }, -// } - -// for name, stc := range cases { -// tc := stc // to make scopelint happy -// suite.T().Run(name, func(t *testing.T) { -// fee := sdk.NewCoins(sdk.NewInt64Coin("atom", tc.fee)) -// msgs := []sdk.Msg{testdata.NewTestMsg(tc.signer)} - -// acc := app.AccountKeeper.GetAccount(ctx, tc.signer) -// privs, accNums, seqs := []cryptotypes.PrivKey{tc.signerKey}, []uint64{0}, []uint64{0} -// if acc != nil { -// accNums, seqs = []uint64{acc.GetAccountNumber()}, []uint64{acc.GetSequence()} -// } - -// tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, helpers.DefaultGenTxGas, ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...) -// suite.Require().NoError(err) -// _, err = ourAnteHandler(ctx, tx, false) -// if tc.valid { -// suite.Require().NoError(err) -// } else { -// suite.Require().Error(err) -// } - -// _, err = anteHandlerStack(ctx, tx, false) -// if tc.valid { -// suite.Require().NoError(err) -// } else { -// suite.Require().Error(err) -// } -// }) -// } -// } - -// // returns context and app with params set on account keeper -// func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) { -// app := simapp.Setup(isCheckTx) -// ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{}) -// app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams()) - -// return app, ctx -// } - -// // don't consume any gas -// func SigGasNoConsumer(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params authtypes.Params) error { -// return nil -// } - -// func genTxWithFeeGranter(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, -// accSeqs []uint64, feeGranter sdk.AccAddress, priv ...cryptotypes.PrivKey) (sdk.Tx, error) { -// sigs := make([]signing.SignatureV2, len(priv)) - -// // create a random length memo -// r := rand.New(rand.NewSource(time.Now().UnixNano())) - -// memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100)) - -// signMode := gen.SignModeHandler().DefaultMode() - -// // 1st round: set SignatureV2 with empty signatures, to set correct -// // signer infos. -// for i, p := range priv { -// sigs[i] = signing.SignatureV2{ -// PubKey: p.PubKey(), -// Data: &signing.SingleSignatureData{ -// SignMode: signMode, -// }, -// Sequence: accSeqs[i], -// } -// } - -// tx := gen.NewTxBuilder() -// err := tx.SetMsgs(msgs...) -// if err != nil { -// return nil, err -// } -// err = tx.SetSignatures(sigs...) -// if err != nil { -// return nil, err -// } -// tx.SetMemo(memo) -// tx.SetFeeAmount(feeAmt) -// tx.SetGasLimit(gas) -// tx.SetFeeGranter(feeGranter) - -// // 2nd round: once all signer infos are set, every signer can sign. -// for i, p := range priv { -// signerData := authsign.SignerData{ -// ChainID: chainID, -// AccountNumber: accNums[i], -// Sequence: accSeqs[i], -// } -// signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) -// if err != nil { -// panic(err) -// } -// sig, err := p.Sign(signBytes) -// if err != nil { -// panic(err) -// } -// sigs[i].Data.(*signing.SingleSignatureData).Signature = sig -// err = tx.SetSignatures(sigs...) -// if err != nil { -// panic(err) -// } -// } - -// return tx.GetTx(), nil -// } - -// func TestAnteTestSuite(t *testing.T) { -// suite.Run(t, new(AnteTestSuite)) -// } From 8bd1cfe5bb2de2b2d31638fddb4839d5dd70adc8 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 5 Mar 2021 14:59:19 +0530 Subject: [PATCH 04/15] review changes --- simapp/app.go | 8 +- x/auth/ante/ante.go | 45 +++++----- x/auth/ante/ante_test.go | 24 ++++-- x/auth/ante/fee.go | 8 ++ x/auth/ante/feegrant_ante.go | 28 ------ x/auth/ante/feegrant_ante_test.go | 137 +++++++++++++++--------------- x/auth/ante/sigverify_test.go | 7 +- x/auth/ante/testutil_test.go | 8 +- 8 files changed, 136 insertions(+), 129 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 1078696c64f..3a98caf3f7c 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -437,8 +437,12 @@ func NewSimApp( app.SetBeginBlocker(app.BeginBlocker) app.SetAnteHandler( ante.NewAnteHandler( - app.AccountKeeper, app.BankKeeper, &app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, - encodingConfig.TxConfig.SignModeHandler(), + app.AccountKeeper, app.BankKeeper, + ante.AnteHandlerOptions{ + FeegrantKeeper: &app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + }, ), ) app.SetEndBlocker(app.EndBlocker) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 594c42a83f8..e1a5987fc4d 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -2,19 +2,36 @@ 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 + +// AnteHandlerOptions are the options for ante handler build +type AnteHandlerOptions struct { + FeegrantKeeper *feegrantkeeper.Keeper + SigGasConsumer DefaultSigVerificationGasConsumerHandler + SignModeHandler authsigning.SignModeHandler +} + // 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, feeGrantKeeper *feegrantkeeper.Keeper, - sigGasConsumer SignatureVerificationGasConsumer, - signModeHandler signing.SignModeHandler, + ak AccountKeeper, bk types.BankKeeper, + anteHandlerOptions AnteHandlerOptions, ) sdk.AnteHandler { + + 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(), @@ -23,24 +40,14 @@ func NewAnteHandler( TxTimeoutHeightDecorator{}, NewValidateMemoDecorator(ak), NewConsumeGasForTxSizeDecorator(ak), - } - - if feeGrantKeeper == nil { - anteDecorators = append( - anteDecorators, - NewRejectFeeGranterDecorator(), - ) - } - - anteDecorators = append( - anteDecorators, - NewDeductGrantedFeeDecorator(ak, bankKeeper, feeGrantKeeper), + feeGrantAnteHandler, + NewDeductFeeDecorator(ak, bk), NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators NewValidateSigCountDecorator(ak), - NewSigGasConsumeDecorator(ak, sigGasConsumer), - NewSigVerificationDecorator(ak, signModeHandler), + NewSigGasConsumeDecorator(ak, anteHandlerOptions.SigGasConsumer), + NewSigVerificationDecorator(ak, anteHandlerOptions.SignModeHandler), NewIncrementSequenceDecorator(ak), - ) + } return sdk.ChainAnteDecorators(anteDecorators...) } diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 6bd95f5a735..acafe301edd 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1009,15 +1009,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, &suite.app.FeeGrantKeeper, 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.AnteHandlerOptions{ + 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) diff --git a/x/auth/ante/fee.go b/x/auth/ante/fee.go index 5da4dbbaf5d..ae38c9888c1 100644 --- a/x/auth/ante/fee.go +++ b/x/auth/ante/fee.go @@ -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) if feePayerAcc == nil { diff --git a/x/auth/ante/feegrant_ante.go b/x/auth/ante/feegrant_ante.go index f3d3b7285bb..b3436c1157e 100644 --- a/x/auth/ante/feegrant_ante.go +++ b/x/auth/ante/feegrant_ante.go @@ -1,11 +1,8 @@ package ante import ( - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" "github.com/cosmos/cosmos-sdk/x/feegrant/types" ) @@ -39,17 +36,10 @@ 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) @@ -57,24 +47,6 @@ func (d DeductGrantedFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula 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 = DeductFees(d.bk, ctx, deductFeesFromAcc, fee) - if err != nil { - return ctx, err } return next(ctx, tx, simulate) diff --git a/x/auth/ante/feegrant_ante_test.go b/x/auth/ante/feegrant_ante_test.go index e03336271ab..2acc0e817bd 100644 --- a/x/auth/ante/feegrant_ante_test.go +++ b/x/auth/ante/feegrant_ante_test.go @@ -66,80 +66,79 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { suite.Require().NoError(err) cases := map[string]struct { - signerKey cryptotypes.PrivKey - signer sdk.AccAddress - feeAccount sdk.AccAddress - feeAccountKey cryptotypes.PrivKey - handler sdk.AnteHandler - fee int64 - valid bool + signerKey cryptotypes.PrivKey + signer sdk.AccAddress + feeAccount sdk.AccAddress + fee int64 + shouldPassFeegrantAnte bool + shouldPassWholeAnteStack bool }{ - "paying with low funds (only ours)": { - signerKey: priv1, - signer: addr1, - fee: 50, - handler: ourAnteHandler, - valid: false, + "paying with low funds": { + signerKey: priv1, + signer: addr1, + fee: 50, + shouldPassFeegrantAnte: true, + shouldPassWholeAnteStack: false, }, - "paying with good funds (only ours)": { - signerKey: priv2, - signer: addr2, - fee: 50, - handler: ourAnteHandler, - valid: true, + "paying with good funds": { + signerKey: priv2, + signer: addr2, + fee: 50, + shouldPassFeegrantAnte: true, + shouldPassWholeAnteStack: true, }, - "paying with no account (only ours)": { - signerKey: priv3, - signer: addr3, - fee: 1, - handler: ourAnteHandler, - valid: false, + "paying with no account": { + signerKey: priv3, + signer: addr3, + fee: 1, + shouldPassFeegrantAnte: true, + shouldPassWholeAnteStack: false, }, - "no fee with real account (only ours)": { - signerKey: priv1, - signer: addr1, - fee: 0, - handler: ourAnteHandler, - valid: true, + "no fee with real account": { + signerKey: priv1, + signer: addr1, + fee: 0, + shouldPassFeegrantAnte: true, + shouldPassWholeAnteStack: true, }, - "no fee with no account (only ours)": { - signerKey: priv5, - signer: addr5, - fee: 0, - handler: ourAnteHandler, - valid: false, + "no fee with no account": { + signerKey: priv5, + signer: addr5, + fee: 0, + shouldPassFeegrantAnte: true, + shouldPassWholeAnteStack: false, }, - "valid fee grant without account (only ours)": { - signerKey: priv3, - signer: addr3, - feeAccount: addr2, - fee: 50, - handler: ourAnteHandler, - valid: true, + "valid fee grant without account": { + signerKey: priv3, + signer: addr3, + feeAccount: addr2, + fee: 50, + shouldPassFeegrantAnte: true, + shouldPassWholeAnteStack: true, }, - "no fee grant (only ours)": { - signerKey: priv3, - signer: addr3, - feeAccount: addr1, - fee: 2, - handler: ourAnteHandler, - valid: false, + "no fee grant": { + signerKey: priv3, + signer: addr3, + feeAccount: addr1, + fee: 2, + shouldPassFeegrantAnte: false, + shouldPassWholeAnteStack: false, }, - "allowance smaller than requested fee (only ours)": { - signerKey: priv4, - signer: addr4, - feeAccount: addr2, - fee: 50, - handler: ourAnteHandler, - valid: false, + "allowance smaller than requested fee": { + signerKey: priv4, + signer: addr4, + feeAccount: addr2, + fee: 50, + shouldPassFeegrantAnte: false, + shouldPassWholeAnteStack: false, }, - "granter cannot cover allowed fee grant (only ours)": { - signerKey: priv4, - signer: addr4, - feeAccount: addr1, - fee: 50, - handler: ourAnteHandler, - valid: false, + "granter cannot cover allowed fee grant": { + signerKey: priv4, + signer: addr4, + feeAccount: addr1, + fee: 50, + shouldPassFeegrantAnte: false, + shouldPassWholeAnteStack: false, }, } @@ -157,15 +156,15 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, helpers.DefaultGenTxGas, ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...) suite.Require().NoError(err) - _, err = ourAnteHandler(ctx, tx, false) - if tc.valid { + _, err = ourAnteHandler(ctx, tx, false) // tests only feegrant ante + if tc.shouldPassFeegrantAnte { suite.Require().NoError(err) } else { suite.Require().Error(err) } - _, err = anteHandlerStack(ctx, tx, false) - if tc.valid { + _, err = anteHandlerStack(ctx, tx, false) // tests while stack + if tc.shouldPassWholeAnteStack { suite.Require().NoError(err) } else { suite.Require().Error(err) diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index 9ffb11d9b00..0faaae4d07d 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -200,7 +200,12 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { suite.clientCtx = client.Context{}. WithTxConfig(txConfig) - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, &suite.app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, txConfig.SignModeHandler()) + suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, + ante.AnteHandlerOptions{ + FeegrantKeeper: &suite.app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + SignModeHandler: txConfig.SignModeHandler(), + }) suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index dc60b2716f3..19fdc1e4603 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -63,7 +63,13 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { suite.clientCtx = client.Context{}. WithTxConfig(encodingConfig.TxConfig) - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, &suite.app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, encodingConfig.TxConfig.SignModeHandler()) + suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, + ante.AnteHandlerOptions{ + FeegrantKeeper: &suite.app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + }, + ) } // CreateTestAccounts creates `numAccs` accounts, and return all relevant From 5e2c190c588852e15794125b439290d4aa486e9d Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 5 Mar 2021 15:29:48 +0530 Subject: [PATCH 05/15] fix lint --- simapp/app.go | 2 +- x/auth/ante/ante.go | 6 +++--- x/auth/ante/ante_test.go | 2 +- x/auth/ante/sigverify_test.go | 2 +- x/auth/ante/testutil_test.go | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index e477d39ae0e..9974a3f675b 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -382,7 +382,7 @@ func NewSimApp( app.SetAnteHandler( ante.NewAnteHandler( app.AccountKeeper, app.BankKeeper, - ante.AnteHandlerOptions{ + ante.HandlerOptions{ FeegrantKeeper: &app.FeeGrantKeeper, SigGasConsumer: ante.DefaultSigVerificationGasConsumer, SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index e1a5987fc4d..ff885b23ec2 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -10,8 +10,8 @@ import ( type DefaultSigVerificationGasConsumerHandler func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error -// AnteHandlerOptions are the options for ante handler build -type AnteHandlerOptions struct { +// HandlerOptions are the options for ante handler build +type HandlerOptions struct { FeegrantKeeper *feegrantkeeper.Keeper SigGasConsumer DefaultSigVerificationGasConsumerHandler SignModeHandler authsigning.SignModeHandler @@ -22,7 +22,7 @@ type AnteHandlerOptions struct { // signer. func NewAnteHandler( ak AccountKeeper, bk types.BankKeeper, - anteHandlerOptions AnteHandlerOptions, + anteHandlerOptions HandlerOptions, ) sdk.AnteHandler { var feeGrantAnteHandler sdk.AnteDecorator diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 41b5d4d0a94..87288da950f 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1012,7 +1012,7 @@ func (suite *AnteTestSuite) TestCustomSignatureVerificationGasConsumer() { // setup an ante handler that only accepts PubKeyEd25519 suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, - ante.AnteHandlerOptions{ + ante.HandlerOptions{ FeegrantKeeper: &suite.app.FeeGrantKeeper, SigGasConsumer: func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { switch pubkey := sig.PubKey.(type) { diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index 89bd79c1afe..9e9b4e55c52 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -204,7 +204,7 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { WithTxConfig(txConfig) suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, - ante.AnteHandlerOptions{ + ante.HandlerOptions{ FeegrantKeeper: &suite.app.FeeGrantKeeper, SigGasConsumer: ante.DefaultSigVerificationGasConsumer, SignModeHandler: txConfig.SignModeHandler(), diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index 19fdc1e4603..0bcffc8573a 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -64,7 +64,7 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { WithTxConfig(encodingConfig.TxConfig) suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, - ante.AnteHandlerOptions{ + ante.HandlerOptions{ FeegrantKeeper: &suite.app.FeeGrantKeeper, SigGasConsumer: ante.DefaultSigVerificationGasConsumer, SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), From 3c6c8a11872cface05c1eeac1129690438efaad0 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 5 Mar 2021 22:46:40 +0530 Subject: [PATCH 06/15] review changes --- simapp/app.go | 2 +- x/auth/ante/ante.go | 14 +-- x/auth/ante/ante_test.go | 2 +- x/auth/ante/expected_keepers.go | 5 + x/auth/ante/fee.go | 35 ++++-- x/auth/ante/fee_grant.go | 27 ----- x/auth/ante/fee_grant_test.go | 32 ----- x/auth/ante/fee_test.go | 6 +- x/auth/ante/feegrant_ante.go | 53 --------- ...feegrant_ante_test.go => feegrant_test.go} | 110 ++++++++---------- x/auth/ante/sigverify_test.go | 2 +- x/auth/ante/testutil_test.go | 2 +- x/feegrant/keeper/keeper.go | 8 ++ 13 files changed, 99 insertions(+), 199 deletions(-) delete mode 100644 x/auth/ante/fee_grant.go delete mode 100644 x/auth/ante/fee_grant_test.go delete mode 100644 x/auth/ante/feegrant_ante.go rename x/auth/ante/{feegrant_ante_test.go => feegrant_test.go} (70%) diff --git a/simapp/app.go b/simapp/app.go index 9974a3f675b..8c4aab2eeea 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -383,7 +383,7 @@ func NewSimApp( ante.NewAnteHandler( app.AccountKeeper, app.BankKeeper, ante.HandlerOptions{ - FeegrantKeeper: &app.FeeGrantKeeper, + FeegrantKeeper: app.FeeGrantKeeper, SigGasConsumer: ante.DefaultSigVerificationGasConsumer, SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), }, diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index ff885b23ec2..085058ce7ed 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -5,14 +5,13 @@ import ( "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 // HandlerOptions are the options for ante handler build type HandlerOptions struct { - FeegrantKeeper *feegrantkeeper.Keeper + FeegrantKeeper FeegrantKeeper SigGasConsumer DefaultSigVerificationGasConsumerHandler SignModeHandler authsigning.SignModeHandler } @@ -24,14 +23,6 @@ func NewAnteHandler( ak AccountKeeper, bk types.BankKeeper, anteHandlerOptions HandlerOptions, ) sdk.AnteHandler { - - 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(), @@ -40,8 +31,7 @@ func NewAnteHandler( TxTimeoutHeightDecorator{}, NewValidateMemoDecorator(ak), NewConsumeGasForTxSizeDecorator(ak), - feeGrantAnteHandler, - NewDeductFeeDecorator(ak, bk), + NewDeductFeeDecorator(ak, bk, anteHandlerOptions.FeegrantKeeper), NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators NewValidateSigCountDecorator(ak), NewSigGasConsumeDecorator(ak, anteHandlerOptions.SigGasConsumer), diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 87288da950f..48ffd147666 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1013,7 +1013,7 @@ func (suite *AnteTestSuite) TestCustomSignatureVerificationGasConsumer() { // setup an ante handler that only accepts PubKeyEd25519 suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, ante.HandlerOptions{ - FeegrantKeeper: &suite.app.FeeGrantKeeper, + FeegrantKeeper: suite.app.FeeGrantKeeper, SigGasConsumer: func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { switch pubkey := sig.PubKey.(type) { case *ed25519.PubKey: diff --git a/x/auth/ante/expected_keepers.go b/x/auth/ante/expected_keepers.go index 1231092bfa9..19d30acc44e 100644 --- a/x/auth/ante/expected_keepers.go +++ b/x/auth/ante/expected_keepers.go @@ -15,3 +15,8 @@ type AccountKeeper interface { GetModuleAccount(ctx sdk.Context, moduleName string) types.ModuleAccountI NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) types.AccountI } + +// FeegrantKeeper defines the expected feegrant keeper. +type FeegrantKeeper interface { + UseGrantedFees(ctx sdk.Context, granter, grantee sdk.AccAddress, fee sdk.Coins) error +} diff --git a/x/auth/ante/fee.go b/x/auth/ante/fee.go index ae38c9888c1..2763323fa68 100644 --- a/x/auth/ante/fee.go +++ b/x/auth/ante/fee.go @@ -59,14 +59,16 @@ func (mfd MempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b // Call next AnteHandler if fees successfully deducted // CONTRACT: Tx must implement FeeTx interface to use DeductFeeDecorator type DeductFeeDecorator struct { - ak AccountKeeper - bankKeeper types.BankKeeper + ak AccountKeeper + bankKeeper types.BankKeeper + feegrantKeeper FeegrantKeeper } -func NewDeductFeeDecorator(ak AccountKeeper, bk types.BankKeeper) DeductFeeDecorator { +func NewDeductFeeDecorator(ak AccountKeeper, bk types.BankKeeper, fk FeegrantKeeper) DeductFeeDecorator { return DeductFeeDecorator{ - ak: ak, - bankKeeper: bk, + ak: ak, + bankKeeper: bk, + feegrantKeeper: fk, } } @@ -80,24 +82,37 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo panic(fmt.Sprintf("%s module account has not been set", types.FeeCollectorName)) } + fee := feeTx.GetFee() feePayer := feeTx.FeePayer() feeGranter := feeTx.FeeGranter() + deductFeesFrom := feePayer + // if feegranter set deduct fee from feegranter account. // this works with only when feegrant enabled. if feeGranter != nil { - feePayer = feeGranter + if dfd.feegrantKeeper == nil { + return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "fee grants are not enabled") + } else if !feeGranter.Equals(feePayer) { + err := dfd.feegrantKeeper.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 } - feePayerAcc := dfd.ak.GetAccount(ctx, feePayer) + deductFeesFromAcc := dfd.ak.GetAccount(ctx, deductFeesFrom) - if feePayerAcc == nil { - return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "fee payer address: %s does not exist", feePayer) + if deductFeesFromAcc == nil { + return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "fee payer address: %s does not exist", deductFeesFrom) } // deduct the fees if !feeTx.GetFee().IsZero() { - err = DeductFees(dfd.bankKeeper, ctx, feePayerAcc, feeTx.GetFee()) + err = DeductFees(dfd.bankKeeper, ctx, deductFeesFromAcc, feeTx.GetFee()) if err != nil { return ctx, err } diff --git a/x/auth/ante/fee_grant.go b/x/auth/ante/fee_grant.go deleted file mode 100644 index 2c37fffa098..00000000000 --- a/x/auth/ante/fee_grant.go +++ /dev/null @@ -1,27 +0,0 @@ -package ante - -import ( - "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" -) - -// RejectFeeGranterDecorator is an AnteDecorator which rejects transactions which -// have the Fee.granter field set. It is to be used by chains which do not support -// fee grants. -type RejectFeeGranterDecorator struct{} - -// NewRejectFeeGranterDecorator returns a new RejectFeeGranterDecorator. -func NewRejectFeeGranterDecorator() RejectFeeGranterDecorator { - return RejectFeeGranterDecorator{} -} - -var _ types.AnteDecorator = RejectFeeGranterDecorator{} - -func (d RejectFeeGranterDecorator) AnteHandle(ctx types.Context, tx types.Tx, simulate bool, next types.AnteHandler) (newCtx types.Context, err error) { - feeTx, ok := tx.(types.FeeTx) - if ok && len(feeTx.FeeGranter()) != 0 { - return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "fee grants are not supported") - } - - return next(ctx, tx, simulate) -} diff --git a/x/auth/ante/fee_grant_test.go b/x/auth/ante/fee_grant_test.go deleted file mode 100644 index 2f07300c7fc..00000000000 --- a/x/auth/ante/fee_grant_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package ante_test - -import ( - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/testutil/testdata" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/ante" - "github.com/cosmos/cosmos-sdk/x/auth/tx" -) - -type setFeeGranter interface { - SetFeeGranter(feeGranter sdk.AccAddress) -} - -func (suite *AnteTestSuite) TestRejectFeeGranter() { - suite.SetupTest(true) // setup - txConfig := tx.NewTxConfig(codec.NewProtoCodec(types.NewInterfaceRegistry()), tx.DefaultSignModes) - txBuilder := txConfig.NewTxBuilder() - d := ante.NewRejectFeeGranterDecorator() - antehandler := sdk.ChainAnteDecorators(d) - - _, err := antehandler(suite.ctx, txBuilder.GetTx(), false) - suite.Require().NoError(err) - - setGranterTx := txBuilder.(setFeeGranter) - _, _, addr := testdata.KeyTestPubAddr() - setGranterTx.SetFeeGranter(addr) - - _, err = antehandler(suite.ctx, txBuilder.GetTx(), false) - suite.Require().Error(err) -} diff --git a/x/auth/ante/fee_test.go b/x/auth/ante/fee_test.go index 343e814583d..5c10238730d 100644 --- a/x/auth/ante/fee_test.go +++ b/x/auth/ante/fee_test.go @@ -8,6 +8,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/ante" ) +type setFeeGranter interface { + SetFeeGranter(feeGranter sdk.AccAddress) +} + func (suite *AnteTestSuite) TestEnsureMempoolFees() { suite.SetupTest(true) // setup suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() @@ -86,7 +90,7 @@ func (suite *AnteTestSuite) TestDeductFees() { err = simapp.FundAccount(suite.app, suite.ctx, addr1, coins) suite.Require().NoError(err) - dfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper) + dfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, nil) antehandler := sdk.ChainAnteDecorators(dfd) _, err = antehandler(suite.ctx, tx, false) diff --git a/x/auth/ante/feegrant_ante.go b/x/auth/ante/feegrant_ante.go deleted file mode 100644 index b3436c1157e..00000000000 --- a/x/auth/ante/feegrant_ante.go +++ /dev/null @@ -1,53 +0,0 @@ -package ante - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" - "github.com/cosmos/cosmos-sdk/x/feegrant/types" -) - -// DeductGrantedFeeDecorator deducts fees from fee_payer or fee_granter (if exists a valid fee allowance) of the tx -// If the fee_payer or fee_granter does not have the funds to pay for the fees, return with InsufficientFunds error -// Call next AnteHandler if fees successfully deducted -// CONTRACT: Tx must implement GrantedFeeTx interface to use DeductGrantedFeeDecorator -type DeductGrantedFeeDecorator struct { - ak types.AccountKeeper - k *feegrantkeeper.Keeper - bk types.BankKeeper -} - -func NewDeductGrantedFeeDecorator(ak types.AccountKeeper, bk types.BankKeeper, k *feegrantkeeper.Keeper) DeductGrantedFeeDecorator { - return DeductGrantedFeeDecorator{ - ak: ak, - k: k, - bk: bk, - } -} - -// AnteHandle performs a decorated ante-handler responsible for deducting transaction -// fees. Fees will be deducted from the account designated by the FeePayer on a -// transaction by default. However, if the fee payer differs from the transaction -// signer, the handler will check if a fee grant has been authorized. If the -// transaction's signer does not exist, it will be created. -func (d DeductGrantedFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { - feeTx, ok := tx.(sdk.FeeTx) - if !ok { - return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a GrantedFeeTx") - } - - fee := feeTx.GetFee() - feePayer := feeTx.FeePayer() - feeGranter := feeTx.FeeGranter() - - // 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) - } - - } - - return next(ctx, tx, simulate) -} diff --git a/x/auth/ante/feegrant_ante_test.go b/x/auth/ante/feegrant_test.go similarity index 70% rename from x/auth/ante/feegrant_ante_test.go rename to x/auth/ante/feegrant_test.go index 2acc0e817bd..76ed106ec36 100644 --- a/x/auth/ante/feegrant_ante_test.go +++ b/x/auth/ante/feegrant_test.go @@ -32,8 +32,8 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(app.InterfaceRegistry()), tx.DefaultSignModes) // this just tests our handler - dfd := ante.NewDeductGrantedFeeDecorator(app.AccountKeeper, app.BankKeeper, &app.FeeGrantKeeper) - ourAnteHandler := sdk.ChainAnteDecorators(dfd) + dfd := ante.NewDeductFeeDecorator(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) + feeAnteHandler := sdk.ChainAnteDecorators(dfd) // this tests the whole stack anteHandlerStack := suite.anteHandler @@ -66,79 +66,69 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { suite.Require().NoError(err) cases := map[string]struct { - signerKey cryptotypes.PrivKey - signer sdk.AccAddress - feeAccount sdk.AccAddress - fee int64 - shouldPassFeegrantAnte bool - shouldPassWholeAnteStack bool + signerKey cryptotypes.PrivKey + signer sdk.AccAddress + feeAccount sdk.AccAddress + fee int64 + valid bool }{ "paying with low funds": { - signerKey: priv1, - signer: addr1, - fee: 50, - shouldPassFeegrantAnte: true, - shouldPassWholeAnteStack: false, + signerKey: priv1, + signer: addr1, + fee: 50, + valid: false, }, "paying with good funds": { - signerKey: priv2, - signer: addr2, - fee: 50, - shouldPassFeegrantAnte: true, - shouldPassWholeAnteStack: true, + signerKey: priv2, + signer: addr2, + fee: 50, + valid: true, }, "paying with no account": { - signerKey: priv3, - signer: addr3, - fee: 1, - shouldPassFeegrantAnte: true, - shouldPassWholeAnteStack: false, + signerKey: priv3, + signer: addr3, + fee: 1, + valid: false, }, "no fee with real account": { - signerKey: priv1, - signer: addr1, - fee: 0, - shouldPassFeegrantAnte: true, - shouldPassWholeAnteStack: true, + signerKey: priv1, + signer: addr1, + fee: 0, + valid: true, }, "no fee with no account": { - signerKey: priv5, - signer: addr5, - fee: 0, - shouldPassFeegrantAnte: true, - shouldPassWholeAnteStack: false, + signerKey: priv5, + signer: addr5, + fee: 0, + valid: false, }, "valid fee grant without account": { - signerKey: priv3, - signer: addr3, - feeAccount: addr2, - fee: 50, - shouldPassFeegrantAnte: true, - shouldPassWholeAnteStack: true, + signerKey: priv3, + signer: addr3, + feeAccount: addr2, + fee: 50, + valid: true, }, "no fee grant": { - signerKey: priv3, - signer: addr3, - feeAccount: addr1, - fee: 2, - shouldPassFeegrantAnte: false, - shouldPassWholeAnteStack: false, + signerKey: priv3, + signer: addr3, + feeAccount: addr1, + fee: 2, + valid: false, }, "allowance smaller than requested fee": { - signerKey: priv4, - signer: addr4, - feeAccount: addr2, - fee: 50, - shouldPassFeegrantAnte: false, - shouldPassWholeAnteStack: false, + signerKey: priv4, + signer: addr4, + feeAccount: addr2, + fee: 50, + valid: false, }, "granter cannot cover allowed fee grant": { - signerKey: priv4, - signer: addr4, - feeAccount: addr1, - fee: 50, - shouldPassFeegrantAnte: false, - shouldPassWholeAnteStack: false, + signerKey: priv4, + signer: addr4, + feeAccount: addr1, + fee: 50, + valid: false, }, } @@ -156,15 +146,15 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, helpers.DefaultGenTxGas, ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...) suite.Require().NoError(err) - _, err = ourAnteHandler(ctx, tx, false) // tests only feegrant ante - if tc.shouldPassFeegrantAnte { + _, err = feeAnteHandler(ctx, tx, false) // tests only feegrant ante + if tc.valid { suite.Require().NoError(err) } else { suite.Require().Error(err) } _, err = anteHandlerStack(ctx, tx, false) // tests while stack - if tc.shouldPassWholeAnteStack { + if tc.valid { suite.Require().NoError(err) } else { suite.Require().Error(err) diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index 9e9b4e55c52..f944b29e0f7 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -205,7 +205,7 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, ante.HandlerOptions{ - FeegrantKeeper: &suite.app.FeeGrantKeeper, + FeegrantKeeper: suite.app.FeeGrantKeeper, SigGasConsumer: ante.DefaultSigVerificationGasConsumer, SignModeHandler: txConfig.SignModeHandler(), }) diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index 0bcffc8573a..991a4674d13 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -65,7 +65,7 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, ante.HandlerOptions{ - FeegrantKeeper: &suite.app.FeeGrantKeeper, + FeegrantKeeper: suite.app.FeeGrantKeeper, SigGasConsumer: ante.DefaultSigVerificationGasConsumer, SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), }, diff --git a/x/feegrant/keeper/keeper.go b/x/feegrant/keeper/keeper.go index e6606bf0ca5..84bbb6bb039 100644 --- a/x/feegrant/keeper/keeper.go +++ b/x/feegrant/keeper/keeper.go @@ -11,6 +11,12 @@ import ( "github.com/cosmos/cosmos-sdk/x/feegrant/types" ) +// FeegrantKeeperI is the interface for x/feegrant's implements. +type FeegrantKeeperI interface { + // UseGrantedFees will try to pay the given fee from the granter's account as requested by the grantee + UseGrantedFees(ctx sdk.Context, granter, grantee sdk.AccAddress, fee sdk.Coins) error +} + // Keeper manages state of all fee grants, as well as calculating approval. // It must have a codec with all available allowances registered. type Keeper struct { @@ -19,6 +25,8 @@ type Keeper struct { authKeeper types.AccountKeeper } +var _ FeegrantKeeperI = &Keeper{} + // NewKeeper creates a fee grant Keeper func NewKeeper(cdc codec.BinaryMarshaler, storeKey sdk.StoreKey, ak types.AccountKeeper) Keeper { return Keeper{ From f29391f5a08bbc455edd7d8e088204e2ea063548 Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 8 Mar 2021 15:18:07 +0530 Subject: [PATCH 07/15] review changes --- x/auth/ante/ante.go | 2 +- x/auth/ante/expected_keepers.go | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 085058ce7ed..633f8f4f4aa 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -12,7 +12,7 @@ type DefaultSigVerificationGasConsumerHandler func(meter sdk.GasMeter, sig signi // HandlerOptions are the options for ante handler build type HandlerOptions struct { FeegrantKeeper FeegrantKeeper - SigGasConsumer DefaultSigVerificationGasConsumerHandler + SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error SignModeHandler authsigning.SignModeHandler } diff --git a/x/auth/ante/expected_keepers.go b/x/auth/ante/expected_keepers.go index 19d30acc44e..e9c2286f032 100644 --- a/x/auth/ante/expected_keepers.go +++ b/x/auth/ante/expected_keepers.go @@ -12,8 +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 } // FeegrantKeeper defines the expected feegrant keeper. From 81bfce66135e7f6048a7b882d503df5b2f6d354e Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 8 Mar 2021 15:45:55 +0530 Subject: [PATCH 08/15] review changes --- x/auth/ante/ante.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 633f8f4f4aa..ea69f33bd31 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -7,8 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" ) -type DefaultSigVerificationGasConsumerHandler func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error - // HandlerOptions are the options for ante handler build type HandlerOptions struct { FeegrantKeeper FeegrantKeeper @@ -23,6 +21,11 @@ func NewAnteHandler( ak AccountKeeper, bk types.BankKeeper, anteHandlerOptions HandlerOptions, ) sdk.AnteHandler { + var sigGasConsumer = anteHandlerOptions.SigGasConsumer + if sigGasConsumer == nil { + sigGasConsumer = DefaultSigVerificationGasConsumer + } + anteDecorators := []sdk.AnteDecorator{ NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first NewRejectExtensionOptionsDecorator(), @@ -34,7 +37,7 @@ func NewAnteHandler( NewDeductFeeDecorator(ak, bk, anteHandlerOptions.FeegrantKeeper), NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators NewValidateSigCountDecorator(ak), - NewSigGasConsumeDecorator(ak, anteHandlerOptions.SigGasConsumer), + NewSigGasConsumeDecorator(ak, sigGasConsumer), NewSigVerificationDecorator(ak, anteHandlerOptions.SignModeHandler), NewIncrementSequenceDecorator(ak), } From a652ace1085317fc6c49924b06c8ab8c9a20f8e4 Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 8 Mar 2021 18:45:12 +0530 Subject: [PATCH 09/15] review changes --- x/auth/ante/fee_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/x/auth/ante/fee_test.go b/x/auth/ante/fee_test.go index 5c10238730d..46ff5cebc7b 100644 --- a/x/auth/ante/fee_test.go +++ b/x/auth/ante/fee_test.go @@ -8,10 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/ante" ) -type setFeeGranter interface { - SetFeeGranter(feeGranter sdk.AccAddress) -} - func (suite *AnteTestSuite) TestEnsureMempoolFees() { suite.SetupTest(true) // setup suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() From 8fdf57bd3a29d20bced1570bf226568fa5d97691 Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 8 Mar 2021 22:09:26 +0530 Subject: [PATCH 10/15] fix ante builder --- simapp/app.go | 6 +++--- x/auth/ante/ante.go | 8 ++++---- x/auth/ante/ante_test.go | 6 ++++-- x/auth/ante/sigverify_test.go | 13 ++++++++----- x/auth/ante/testutil_test.go | 10 ++++++---- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 8c4aab2eeea..c206b21542e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -382,10 +382,10 @@ func NewSimApp( app.SetAnteHandler( ante.NewAnteHandler( app.AccountKeeper, app.BankKeeper, + encodingConfig.TxConfig.SignModeHandler(), ante.HandlerOptions{ - FeegrantKeeper: app.FeeGrantKeeper, - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, - SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + FeegrantKeeper: app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, }, ), ) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index ea69f33bd31..bc1594f655e 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -9,9 +9,8 @@ import ( // HandlerOptions are the options for ante handler build type HandlerOptions struct { - FeegrantKeeper FeegrantKeeper - SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error - SignModeHandler authsigning.SignModeHandler + FeegrantKeeper FeegrantKeeper + SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error } // NewAnteHandler returns an AnteHandler that checks and increments sequence @@ -19,6 +18,7 @@ type HandlerOptions struct { // signer. func NewAnteHandler( ak AccountKeeper, bk types.BankKeeper, + signModeHandler authsigning.SignModeHandler, anteHandlerOptions HandlerOptions, ) sdk.AnteHandler { var sigGasConsumer = anteHandlerOptions.SigGasConsumer @@ -38,7 +38,7 @@ func NewAnteHandler( NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators NewValidateSigCountDecorator(ak), NewSigGasConsumeDecorator(ak, sigGasConsumer), - NewSigVerificationDecorator(ak, anteHandlerOptions.SignModeHandler), + NewSigVerificationDecorator(ak, signModeHandler), NewIncrementSequenceDecorator(ak), } diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 48ffd147666..26d00b835fc 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1011,7 +1011,10 @@ 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, + suite.anteHandler = ante.NewAnteHandler( + suite.app.AccountKeeper, + suite.app.BankKeeper, + suite.clientCtx.TxConfig.SignModeHandler(), ante.HandlerOptions{ FeegrantKeeper: suite.app.FeeGrantKeeper, SigGasConsumer: func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { @@ -1023,7 +1026,6 @@ func (suite *AnteTestSuite) TestCustomSignatureVerificationGasConsumer() { return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey) } }, - SignModeHandler: suite.clientCtx.TxConfig.SignModeHandler(), }, ) diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index f944b29e0f7..94e22513dc6 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -203,12 +203,15 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { suite.clientCtx = client.Context{}. WithTxConfig(txConfig) - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, + suite.anteHandler = ante.NewAnteHandler( + suite.app.AccountKeeper, + suite.app.BankKeeper, + txConfig.SignModeHandler(), ante.HandlerOptions{ - FeegrantKeeper: suite.app.FeeGrantKeeper, - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, - SignModeHandler: txConfig.SignModeHandler(), - }) + FeegrantKeeper: suite.app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + }, + ) suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index 991a4674d13..f64fde7baf6 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -63,11 +63,13 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { suite.clientCtx = client.Context{}. WithTxConfig(encodingConfig.TxConfig) - suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, + suite.anteHandler = ante.NewAnteHandler( + suite.app.AccountKeeper, + suite.app.BankKeeper, + encodingConfig.TxConfig.SignModeHandler(), ante.HandlerOptions{ - FeegrantKeeper: suite.app.FeeGrantKeeper, - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, - SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + FeegrantKeeper: suite.app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, }, ) } From 88b38e528ee57ae600e9a97bf4f8619be0b1e9aa Mon Sep 17 00:00:00 2001 From: atheesh Date: Tue, 9 Mar 2021 15:57:56 +0530 Subject: [PATCH 11/15] review changes --- x/auth/types/expected_keepers.go | 1 - x/feegrant/keeper/keeper.go | 9 ++------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/x/auth/types/expected_keepers.go b/x/auth/types/expected_keepers.go index b230682acb1..0ab3f9c4308 100644 --- a/x/auth/types/expected_keepers.go +++ b/x/auth/types/expected_keepers.go @@ -6,6 +6,5 @@ import ( // BankKeeper defines the contract needed for supply related APIs (noalias) type BankKeeper interface { - SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error } diff --git a/x/feegrant/keeper/keeper.go b/x/feegrant/keeper/keeper.go index 84bbb6bb039..b8f30194ff6 100644 --- a/x/feegrant/keeper/keeper.go +++ b/x/feegrant/keeper/keeper.go @@ -8,15 +8,10 @@ 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/auth/ante" "github.com/cosmos/cosmos-sdk/x/feegrant/types" ) -// FeegrantKeeperI is the interface for x/feegrant's implements. -type FeegrantKeeperI interface { - // UseGrantedFees will try to pay the given fee from the granter's account as requested by the grantee - UseGrantedFees(ctx sdk.Context, granter, grantee sdk.AccAddress, fee sdk.Coins) error -} - // Keeper manages state of all fee grants, as well as calculating approval. // It must have a codec with all available allowances registered. type Keeper struct { @@ -25,7 +20,7 @@ type Keeper struct { authKeeper types.AccountKeeper } -var _ FeegrantKeeperI = &Keeper{} +var _ ante.FeegrantKeeper = &Keeper{} // NewKeeper creates a fee grant Keeper func NewKeeper(cdc codec.BinaryMarshaler, storeKey sdk.StoreKey, ak types.AccountKeeper) Keeper { From b5404aa52b279520007589548aefea3c7dcfbce4 Mon Sep 17 00:00:00 2001 From: atheesh Date: Thu, 18 Mar 2021 16:54:22 +0530 Subject: [PATCH 12/15] update ante builder with options struct --- simapp/app.go | 24 +++++++++++------- x/auth/ante/ante.go | 46 ++++++++++++++++++++++------------- x/auth/ante/ante_test.go | 13 ++++++---- x/auth/ante/sigverify_test.go | 15 +++++++----- x/auth/ante/testutil_test.go | 15 +++++++----- 5 files changed, 70 insertions(+), 43 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 069accd9c36..2bd00c913d6 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -374,16 +374,22 @@ func NewSimApp( // initialize BaseApp app.SetInitChainer(app.InitChainer) app.SetBeginBlocker(app.BeginBlocker) - app.SetAnteHandler( - ante.NewAnteHandler( - app.AccountKeeper, app.BankKeeper, - encodingConfig.TxConfig.SignModeHandler(), - ante.HandlerOptions{ - FeegrantKeeper: app.FeeGrantKeeper, - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, - }, - ), + + anteHandler, err := ante.NewAnteHandler( + ante.HandlerOptions{ + AccountKeeper: app.AccountKeeper, + BankKeeper: app.BankKeeper, + SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + FeegrantKeeper: app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + }, ) + + if err != nil { + panic(err) + } + + app.SetAnteHandler(anteHandler) app.SetEndBlocker(app.EndBlocker) if loadLatest { diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index bc1594f655e..35c425eabf0 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -2,6 +2,7 @@ package ante import ( sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "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" @@ -9,19 +10,30 @@ import ( // HandlerOptions are the options for ante handler build type HandlerOptions struct { - FeegrantKeeper FeegrantKeeper - SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error + AccountKeeper AccountKeeper + BankKeeper types.BankKeeper + FeegrantKeeper FeegrantKeeper + SignModeHandler authsigning.SignModeHandler + SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error } // 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, bk types.BankKeeper, - signModeHandler authsigning.SignModeHandler, - anteHandlerOptions HandlerOptions, -) sdk.AnteHandler { - var sigGasConsumer = anteHandlerOptions.SigGasConsumer +func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { + if options.AccountKeeper == nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Account keeper is required for ante builder") + } + + if options.BankKeeper == nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Bank keeper is required for ante builder") + } + + if options.SignModeHandler == nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Sign mode handler is required for ante builder") + } + + var sigGasConsumer = options.SigGasConsumer if sigGasConsumer == nil { sigGasConsumer = DefaultSigVerificationGasConsumer } @@ -32,15 +44,15 @@ func NewAnteHandler( NewMempoolFeeDecorator(), NewValidateBasicDecorator(), TxTimeoutHeightDecorator{}, - NewValidateMemoDecorator(ak), - NewConsumeGasForTxSizeDecorator(ak), - NewDeductFeeDecorator(ak, bk, anteHandlerOptions.FeegrantKeeper), - NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators - NewValidateSigCountDecorator(ak), - NewSigGasConsumeDecorator(ak, sigGasConsumer), - NewSigVerificationDecorator(ak, signModeHandler), - NewIncrementSequenceDecorator(ak), + NewValidateMemoDecorator(options.AccountKeeper), + NewConsumeGasForTxSizeDecorator(options.AccountKeeper), + NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper), + NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators + NewValidateSigCountDecorator(options.AccountKeeper), + NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer), + NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler), + NewIncrementSequenceDecorator(options.AccountKeeper), } - return sdk.ChainAnteDecorators(anteDecorators...) + return sdk.ChainAnteDecorators(anteDecorators...), nil } diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 26d00b835fc..51989e34633 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1011,12 +1011,12 @@ 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, - suite.clientCtx.TxConfig.SignModeHandler(), + anteHandler, err := ante.NewAnteHandler( ante.HandlerOptions{ - FeegrantKeeper: suite.app.FeeGrantKeeper, + AccountKeeper: suite.app.AccountKeeper, + BankKeeper: suite.app.BankKeeper, + FeegrantKeeper: suite.app.FeeGrantKeeper, + SignModeHandler: suite.clientCtx.TxConfig.SignModeHandler(), SigGasConsumer: func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { switch pubkey := sig.PubKey.(type) { case *ed25519.PubKey: @@ -1029,6 +1029,9 @@ func (suite *AnteTestSuite) TestCustomSignatureVerificationGasConsumer() { }, ) + suite.Require().NoError(err) + suite.anteHandler = anteHandler + // Same data for every test cases accounts := suite.CreateTestAccounts(1) feeAmount := testdata.NewTestFeeAmount() diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index 94e22513dc6..6e720ac2035 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -203,16 +203,19 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { suite.clientCtx = client.Context{}. WithTxConfig(txConfig) - suite.anteHandler = ante.NewAnteHandler( - suite.app.AccountKeeper, - suite.app.BankKeeper, - txConfig.SignModeHandler(), + anteHandler, err := ante.NewAnteHandler( ante.HandlerOptions{ - FeegrantKeeper: suite.app.FeeGrantKeeper, - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + AccountKeeper: suite.app.AccountKeeper, + BankKeeper: suite.app.BankKeeper, + FeegrantKeeper: suite.app.FeeGrantKeeper, + SignModeHandler: txConfig.SignModeHandler(), + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, }, ) + suite.Require().NoError(err) + suite.anteHandler = anteHandler + suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() // make block height non-zero to ensure account numbers part of signBytes diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index f64fde7baf6..74216420eb1 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -63,15 +63,18 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { suite.clientCtx = client.Context{}. WithTxConfig(encodingConfig.TxConfig) - suite.anteHandler = ante.NewAnteHandler( - suite.app.AccountKeeper, - suite.app.BankKeeper, - encodingConfig.TxConfig.SignModeHandler(), + anteHandler, err := ante.NewAnteHandler( ante.HandlerOptions{ - FeegrantKeeper: suite.app.FeeGrantKeeper, - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + AccountKeeper: suite.app.AccountKeeper, + BankKeeper: suite.app.BankKeeper, + FeegrantKeeper: suite.app.FeeGrantKeeper, + SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, }, ) + + suite.Require().NoError(err) + suite.anteHandler = anteHandler } // CreateTestAccounts creates `numAccs` accounts, and return all relevant From 576a93a1b0ef31b733eac3950d7fefac38b2011e Mon Sep 17 00:00:00 2001 From: atheesh Date: Thu, 18 Mar 2021 17:15:25 +0530 Subject: [PATCH 13/15] review changes --- x/auth/ante/ante.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 35c425eabf0..db608b1d478 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -22,15 +22,15 @@ type HandlerOptions struct { // signer. func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { if options.AccountKeeper == nil { - return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Account keeper is required for ante builder") + return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "Account keeper is required for ante builder") } if options.BankKeeper == nil { - return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Bank keeper is required for ante builder") + return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "Bank keeper is required for ante builder") } if options.SignModeHandler == nil { - return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Sign mode handler is required for ante builder") + return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "Sign mode handler is required for ante builder") } var sigGasConsumer = options.SigGasConsumer From 943190cb021a4043b834ecb7e496869e00363059 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 19 Mar 2021 21:45:17 +0530 Subject: [PATCH 14/15] review changes --- x/auth/ante/ante.go | 8 ++++---- x/auth/ante/fee.go | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index db608b1d478..8df98f0e42c 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -8,7 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" ) -// HandlerOptions are the options for ante handler build +// HandlerOptions are the options required for constructing a default SDK AnteHandler. type HandlerOptions struct { AccountKeeper AccountKeeper BankKeeper types.BankKeeper @@ -22,15 +22,15 @@ type HandlerOptions struct { // signer. func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { if options.AccountKeeper == nil { - return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "Account keeper is required for ante builder") + 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") + 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") + return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder") } var sigGasConsumer = options.SigGasConsumer diff --git a/x/auth/ante/fee.go b/x/auth/ante/fee.go index 2763323fa68..3cc6aee10cf 100644 --- a/x/auth/ante/fee.go +++ b/x/auth/ante/fee.go @@ -105,7 +105,6 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo } deductFeesFromAcc := dfd.ak.GetAccount(ctx, deductFeesFrom) - if deductFeesFromAcc == nil { return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "fee payer address: %s does not exist", deductFeesFrom) } From e852d056d776dce640c42d7e45581f00fd8ed1db Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 22 Mar 2021 11:52:27 +0530 Subject: [PATCH 15/15] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 551ab5614eb..41af12eb26e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/bank) [\#8517](https://github.com/cosmos/cosmos-sdk/pull/8517) `SupplyI` interface and `Supply` are removed and uses `sdk.Coins` for supply tracking * (x/upgrade) [\#8743](https://github.com/cosmos/cosmos-sdk/pull/8743) `UpgradeHandler` includes a new argument `VersionMap` which helps facilitate in-place migrations. * (x/auth) [\#8129](https://github.com/cosmos/cosmos-sdk/pull/8828) Updated `SigVerifiableTx.GetPubKeys` method signature to return error. +* [\#8682](https://github.com/cosmos/cosmos-sdk/pull/8682) `ante.NewAnteHandler` updated to receive all positional params as `ante.HandlerOptions` struct. If required fields aren't set, throws error accordingly. ### State Machine Breaking