mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 18:15:19 +00:00
614d4e40fe
* Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
198 lines
7.3 KiB
Go
198 lines
7.3 KiB
Go
package ante
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime/debug"
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
tmlog "github.com/cometbft/cometbft/libs/log"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
|
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
|
ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
|
|
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
|
|
evmante "github.com/evmos/ethermint/app/ante"
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
|
)
|
|
|
|
// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
|
|
// channel keeper, EVM Keeper and Fee Market Keeper.
|
|
type HandlerOptions struct {
|
|
AccountKeeper evmtypes.AccountKeeper
|
|
BankKeeper evmtypes.BankKeeper
|
|
IBCKeeper *ibckeeper.Keeper
|
|
EvmKeeper evmante.EVMKeeper
|
|
FeegrantKeeper authante.FeegrantKeeper
|
|
SignModeHandler authsigning.SignModeHandler
|
|
SigGasConsumer authante.SignatureVerificationGasConsumer
|
|
FeeMarketKeeper evmtypes.FeeMarketKeeper
|
|
MaxTxGasWanted uint64
|
|
AddressFetchers []AddressFetcher
|
|
ExtensionOptionChecker authante.ExtensionOptionChecker
|
|
TxFeeChecker authante.TxFeeChecker
|
|
}
|
|
|
|
func (options HandlerOptions) Validate() error {
|
|
if options.AccountKeeper == nil {
|
|
return errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
|
|
}
|
|
if options.BankKeeper == nil {
|
|
return errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
|
|
}
|
|
if options.SignModeHandler == nil {
|
|
return errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
|
|
}
|
|
if options.EvmKeeper == nil {
|
|
return errorsmod.Wrap(sdkerrors.ErrLogic, "evm keeper is required for AnteHandler")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// cosmosHandlerOptions extends HandlerOptions to provide some Cosmos specific configurations
|
|
type cosmosHandlerOptions struct {
|
|
HandlerOptions
|
|
isEIP712 bool
|
|
}
|
|
|
|
// NewAnteHandler returns an 'AnteHandler' that will run actions before a tx is sent to a module's handler.
|
|
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
|
|
if err := options.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return func(
|
|
ctx sdk.Context, tx sdk.Tx, sim bool,
|
|
) (newCtx sdk.Context, err error) {
|
|
var anteHandler sdk.AnteHandler
|
|
|
|
defer Recover(ctx.Logger(), &err)
|
|
|
|
txWithExtensions, ok := tx.(authante.HasExtensionOptionsTx)
|
|
if ok {
|
|
opts := txWithExtensions.GetExtensionOptions()
|
|
if len(opts) > 1 {
|
|
return ctx, errorsmod.Wrap(
|
|
sdkerrors.ErrInvalidRequest,
|
|
"rejecting tx with more than 1 extension option",
|
|
)
|
|
}
|
|
|
|
if len(opts) == 1 {
|
|
switch typeURL := opts[0].GetTypeUrl(); typeURL {
|
|
case "/ethermint.evm.v1.ExtensionOptionsEthereumTx":
|
|
// handle as *evmtypes.MsgEthereumTx
|
|
anteHandler = newEthAnteHandler(options)
|
|
case "/ethermint.types.v1.ExtensionOptionsWeb3Tx":
|
|
// handle as normal Cosmos SDK tx, except signature is checked for EIP712 representation
|
|
anteHandler = newCosmosAnteHandler(cosmosHandlerOptions{
|
|
HandlerOptions: options,
|
|
isEIP712: true,
|
|
})
|
|
default:
|
|
return ctx, errorsmod.Wrapf(
|
|
sdkerrors.ErrUnknownExtensionOptions,
|
|
"rejecting tx with unsupported extension option: %s", typeURL,
|
|
)
|
|
}
|
|
|
|
return anteHandler(ctx, tx, sim)
|
|
}
|
|
}
|
|
|
|
// handle as totally normal Cosmos SDK tx
|
|
switch tx.(type) {
|
|
case sdk.Tx:
|
|
anteHandler = newCosmosAnteHandler(cosmosHandlerOptions{
|
|
HandlerOptions: options,
|
|
isEIP712: false,
|
|
})
|
|
default:
|
|
return ctx, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
|
|
}
|
|
|
|
return anteHandler(ctx, tx, sim)
|
|
}, nil
|
|
}
|
|
|
|
func newCosmosAnteHandler(options cosmosHandlerOptions) sdk.AnteHandler {
|
|
decorators := []sdk.AnteDecorator{}
|
|
|
|
decorators = append(decorators,
|
|
evmante.RejectMessagesDecorator{}, // reject MsgEthereumTxs
|
|
authante.NewSetUpContextDecorator(), // second decorator. SetUpContext must be called before other decorators
|
|
)
|
|
|
|
if !options.isEIP712 {
|
|
decorators = append(decorators, authante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker))
|
|
}
|
|
|
|
if len(options.AddressFetchers) > 0 {
|
|
decorators = append(decorators, NewAuthenticatedMempoolDecorator(options.AddressFetchers...))
|
|
}
|
|
|
|
var sigVerification sdk.AnteDecorator = authante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler)
|
|
if options.isEIP712 {
|
|
sigVerification = evmante.NewLegacyEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, options.EvmKeeper)
|
|
}
|
|
|
|
decorators = append(decorators,
|
|
NewEvmMinGasFilter(options.EvmKeeper), // filter out evm denom from min-gas-prices
|
|
NewVestingAccountDecorator(),
|
|
NewAuthzLimiterDecorator(
|
|
sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}),
|
|
sdk.MsgTypeURL(&vesting.MsgCreateVestingAccount{}),
|
|
sdk.MsgTypeURL(&vesting.MsgCreatePermanentLockedAccount{}),
|
|
sdk.MsgTypeURL(&vesting.MsgCreatePeriodicVestingAccount{}),
|
|
),
|
|
authante.NewValidateBasicDecorator(),
|
|
authante.NewTxTimeoutHeightDecorator(),
|
|
// If ethermint x/feemarket is enabled, align Cosmos min fee with the EVM
|
|
// evmante.NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper),
|
|
authante.NewValidateMemoDecorator(options.AccountKeeper),
|
|
authante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
|
|
authante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
|
|
authante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
|
|
authante.NewValidateSigCountDecorator(options.AccountKeeper),
|
|
authante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
|
|
sigVerification,
|
|
authante.NewIncrementSequenceDecorator(options.AccountKeeper), // innermost AnteDecorator
|
|
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
|
|
)
|
|
return sdk.ChainAnteDecorators(decorators...)
|
|
}
|
|
|
|
func newEthAnteHandler(options HandlerOptions) sdk.AnteHandler {
|
|
return sdk.ChainAnteDecorators(
|
|
evmante.NewEthSetUpContextDecorator(options.EvmKeeper), // outermost AnteDecorator. SetUpContext must be called first
|
|
evmante.NewEthMempoolFeeDecorator(options.EvmKeeper), // Check eth effective gas price against minimal-gas-prices
|
|
evmante.NewEthValidateBasicDecorator(options.EvmKeeper),
|
|
evmante.NewEthSigVerificationDecorator(options.EvmKeeper),
|
|
evmante.NewEthAccountVerificationDecorator(options.AccountKeeper, options.EvmKeeper),
|
|
evmante.NewCanTransferDecorator(options.EvmKeeper),
|
|
evmante.NewEthGasConsumeDecorator(options.EvmKeeper, options.MaxTxGasWanted),
|
|
evmante.NewEthIncrementSenderSequenceDecorator(options.AccountKeeper), // innermost AnteDecorator.
|
|
evmante.NewEthEmitEventDecorator(options.EvmKeeper), // emit eth tx hash and index at the very last ante handler.
|
|
)
|
|
}
|
|
|
|
func Recover(logger tmlog.Logger, err *error) {
|
|
if r := recover(); r != nil {
|
|
*err = errorsmod.Wrapf(sdkerrors.ErrPanic, "%v", r)
|
|
|
|
if e, ok := r.(error); ok {
|
|
logger.Error(
|
|
"ante handler panicked",
|
|
"error", e,
|
|
"stack trace", string(debug.Stack()),
|
|
)
|
|
} else {
|
|
logger.Error(
|
|
"ante handler panicked",
|
|
"recover", fmt.Sprintf("%v", r),
|
|
)
|
|
}
|
|
}
|
|
}
|