Skip to content

Commit

Permalink
chore(ante): fix ante handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ivivanov authored and Lockwarr committed Dec 22, 2023
1 parent dbefdf8 commit 2821812
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
27 changes: 16 additions & 11 deletions app/ante.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package app

import (
errorsmod "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
taxkeeper "github.com/Nolus-Protocol/nolus-core/x/tax/keeper"
taxtypes "github.com/Nolus-Protocol/nolus-core/x/tax/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/auth/types"
ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
"github.com/cosmos/ibc-go/v7/modules/core/keeper"

"github.com/cosmos/cosmos-sdk/x/auth/types"
taxkeeper "github.com/Nolus-Protocol/nolus-core/x/tax/keeper"
taxtypes "github.com/Nolus-Protocol/nolus-core/x/tax/types"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
Expand All @@ -22,28 +24,29 @@ type HandlerOptions struct {
BankKeeper taxtypes.BankKeeper
FeegrantKeeper ante.FeegrantKeeper
TaxKeeper taxkeeper.Keeper
TxCounterStoreKey sdk.StoreKey
TxCounterStoreKey storetypes.StoreKey
WasmConfig *wasmTypes.WasmConfig
SignModeHandler authsigning.SignModeHandler
SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error
IBCKeeper *keeper.Keeper
TxFeeChecker ante.TxFeeChecker
}

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, errorsmod.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, errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for ante builder")
}

if options.WasmConfig == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "wasm config 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, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}

sigGasConsumer := options.SigGasConsumer
Expand All @@ -55,13 +58,15 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit), // after setup context to enforce limits early
wasmkeeper.NewCountTXDecorator(options.TxCounterStoreKey),
ante.NewRejectExtensionOptionsDecorator(),
ante.NewMempoolFeeDecorator(),
ante.NewExtensionOptionsDecorator(nil),
// refactor: research if we can safely remove it. According to docs it is no longer needed https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc2/UPGRADING.md#go-api-changes
// ante.NewMempoolFeeDecorator(),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),

// Tax calculation must be called after fees
taxkeeper.NewDeductTaxDecorator(options.AccountKeeper, options.BankKeeper, options.TaxKeeper),
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
Expand Down
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ func New(
HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
TaxKeeper: app.TaxKeeper,
TaxKeeper: *app.TaxKeeper,
TxCounterStoreKey: app.GetKVStoreKey()[wasm.StoreKey],
WasmConfig: &app.WasmConfig,
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
IBCKeeper: app.IBCKeeper,
TxFeeChecker: nil, // when nil is provided NewDeductFeeDecorator uses default checkTxFeeWithValidatorMinGasPrices
},
)
if err != nil {
Expand Down
41 changes: 28 additions & 13 deletions app/keepers/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keepers

import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authztkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
Expand All @@ -16,7 +17,7 @@ import (

icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibchost "github.com/cosmos/ibc-go/v7/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"

minttypes "github.com/Nolus-Protocol/nolus-core/x/mint/types"
taxmoduletypes "github.com/Nolus-Protocol/nolus-core/x/tax/types"
Expand All @@ -34,12 +35,26 @@ func (appKeepers *AppKeepers) GenerateKeys() {
// Define what keys will be used in the cosmos-sdk key/value store.
// Cosmos-SDK modules each have a "key" that allows the application to reference what they've stored on the chain.
appKeepers.keys = sdk.NewKVStoreKeys(
authtypes.StoreKey, authztkeeper.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey,
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey,
upgradetypes.StoreKey, evidencetypes.StoreKey, ibctransfertypes.StoreKey,
taxmoduletypes.StoreKey, vestingstypes.StoreKey, icacontrollertypes.StoreKey, capabilitytypes.StoreKey,
interchainqueriestypes.StoreKey, contractmanagermoduletypes.StoreKey, interchaintxstypes.StoreKey,
authtypes.StoreKey,
authztkeeper.StoreKey,
banktypes.StoreKey,
stakingtypes.StoreKey,
minttypes.StoreKey,
distrtypes.StoreKey,
slashingtypes.StoreKey,
govtypes.StoreKey,
paramstypes.StoreKey,
ibcexported.StoreKey,
upgradetypes.StoreKey,
evidencetypes.StoreKey,
ibctransfertypes.StoreKey,
taxmoduletypes.StoreKey,
vestingstypes.StoreKey,
icacontrollertypes.StoreKey,
capabilitytypes.StoreKey,
interchainqueriestypes.StoreKey,
contractmanagermoduletypes.StoreKey,
interchaintxstypes.StoreKey,
wasm.StoreKey, feerefundertypes.StoreKey,
)

Expand All @@ -50,35 +65,35 @@ func (appKeepers *AppKeepers) GenerateKeys() {
appKeepers.memKeys = sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey, feerefundertypes.MemStoreKey)
}

func (appKeepers *AppKeepers) GetKVStoreKey() map[string]*sdk.KVStoreKey {
func (appKeepers *AppKeepers) GetKVStoreKey() map[string]*storetypes.KVStoreKey {
return appKeepers.keys
}

func (appKeepers *AppKeepers) GetTransientStoreKey() map[string]*sdk.TransientStoreKey {
func (appKeepers *AppKeepers) GetTransientStoreKey() map[string]*storetypes.TransientStoreKey {
return appKeepers.tkeys
}

func (appKeepers *AppKeepers) GetMemoryStoreKey() map[string]*sdk.MemoryStoreKey {
func (appKeepers *AppKeepers) GetMemoryStoreKey() map[string]*storetypes.MemoryStoreKey {
return appKeepers.memKeys
}

// GetKey returns the KVStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
func (appKeepers *AppKeepers) GetKey(storeKey string) *sdk.KVStoreKey {
func (appKeepers *AppKeepers) GetKey(storeKey string) *storetypes.KVStoreKey {
return appKeepers.keys[storeKey]
}

// GetTKey returns the TransientStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
func (appKeepers *AppKeepers) GetTKey(storeKey string) *sdk.TransientStoreKey {
func (appKeepers *AppKeepers) GetTKey(storeKey string) *storetypes.TransientStoreKey {
return appKeepers.tkeys[storeKey]
}

// GetMemKey returns the MemStoreKey for the provided mem key.
//
// NOTE: This is solely used for testing purposes.
func (appKeepers *AppKeepers) GetMemKey(storeKey string) *sdk.MemoryStoreKey {
func (appKeepers *AppKeepers) GetMemKey(storeKey string) *storetypes.MemoryStoreKey {
return appKeepers.memKeys[storeKey]
}
1 change: 1 addition & 0 deletions x/tax/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type AccountKeeper interface {

// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
Expand Down

0 comments on commit 2821812

Please sign in to comment.