Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support evm #2776

Merged
merged 25 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 46 additions & 42 deletions ante/ante.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
package app
package ante

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"

guardiankeeper "github.com/irisnet/irishub/modules/guardian/keeper"

oraclekeeper "github.com/irisnet/irismod/modules/oracle/keeper"
tokenkeeper "github.com/irisnet/irismod/modules/token/keeper"
ethante "github.com/evmos/ethermint/app/ante"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
// channel keeper.
type HandlerOptions struct {
ante.HandlerOptions
BankKeeper bankkeeper.Keeper
TokenKeeper tokenkeeper.Keeper
OracleKeeper oraclekeeper.Keeper
GuardianKeeper guardiankeeper.Keeper
BypassMinFeeMsgTypes []string
}
// NewAnteHandler returns an ante handler responsible for attempting to route an
// Ethereum or SDK transaction to an internal ante handler for performing
// transaction-level processing (e.g. fee payment, signature verification) before
// being passed onto it's respective handler.
func NewAnteHandler(options HandlerOptions) sdk.AnteHandler {
return func(
ctx sdk.Context, tx sdk.Tx, sim bool,
) (newCtx sdk.Context, err error) {
var anteHandler sdk.AnteHandler

defer ethante.Recover(ctx.Logger(), &err)

txWithExtensions, ok := tx.(authante.HasExtensionOptionsTx)
if ok {
opts := txWithExtensions.GetExtensionOptions()
if len(opts) > 0 {
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 = newCosmosAnteHandlerEip712(options)
default:
return ctx, errorsmod.Wrapf(
errortypes.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(options)
default:
return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid transaction type: %T", tx)
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, and deducts fees from the first
// signer.
func NewAnteHandler(opts HandlerOptions) (sdk.AnteHandler, error) {
var sigGasConsumer = opts.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
return anteHandler(ctx, tx, sim)
}
return sdk.ChainAnteDecorators(
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewExtensionOptionsDecorator(opts.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(opts.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(opts.AccountKeeper),
ante.NewDeductFeeDecorator(opts.AccountKeeper, opts.BankKeeper, opts.FeegrantKeeper, opts.TxFeeChecker),
ante.NewSetPubKeyDecorator(opts.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(opts.AccountKeeper),
ante.NewSigGasConsumeDecorator(opts.AccountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(opts.AccountKeeper, opts.SignModeHandler),
NewValidateTokenDecorator(opts.TokenKeeper),
tokenkeeper.NewValidateTokenFeeDecorator(opts.TokenKeeper, opts.BankKeeper),
oraclekeeper.NewValidateOracleAuthDecorator(opts.OracleKeeper, opts.GuardianKeeper),
NewValidateServiceDecorator(),
ante.NewIncrementSequenceDecorator(opts.AccountKeeper),
), nil
}
2 changes: 1 addition & 1 deletion ante/decorators.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package ante

import (
"strings"
Expand Down
69 changes: 69 additions & 0 deletions ante/deduct_fee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ante

import (
"math"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

func CheckTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return nil, 0, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

feeCoins := feeTx.GetFee()
gas := feeTx.GetGas()

// Ensure that the provided fees meet a minimum threshold for the validator,
// if this is a CheckTx. This is only for local mempool purposes, and thus
// is only ran on check tx.
if ctx.IsCheckTx() {

minGasPrices := ctx.MinGasPrices()

if !minGasPrices.IsZero() {
requiredFees := make(sdk.Coins, len(minGasPrices))
required := false
// Determine the required fees by multiplying each required minimum gas
// price by the gas limit, where fee = ceil(minGasPrice * gasLimit).
glDec := sdk.NewDec(int64(gas))
for i, gp := range minGasPrices {
fee := gp.Amount.Mul(glDec)
tempCoin := sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
if feeCoins.IsAnyGTE(sdk.NewCoins(tempCoin)) {
required = true
}
requiredFees[i] = tempCoin
}
if !required {
return nil, 0, sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; got: %s required in: %s", feeCoins, requiredFees)
}
}

}

priority := getTxPriority(feeCoins, int64(gas))
return feeCoins, priority, nil
}

// getTxPriority returns a naive tx priority based on the amount of the smallest denomination of the gas price
// provided in a transaction.
// NOTE: This implementation should be used with a great consideration as it opens potential attack vectors
// where txs with multiple coins could not be prioritize as expected.
func getTxPriority(fee sdk.Coins, gas int64) int64 {
var priority int64
for _, c := range fee {
p := int64(math.MaxInt64)
gasPrice := c.Amount.QuoRaw(gas)
if gasPrice.IsInt64() {
p = gasPrice.Int64()
}
if priority == 0 || p < priority {
priority = p
}
}

return priority
}
96 changes: 96 additions & 0 deletions ante/handler_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

ethante "github.com/evmos/ethermint/app/ante"

oraclekeeper "github.com/irisnet/irismod/modules/oracle/keeper"
tokenkeeper "github.com/irisnet/irismod/modules/token/keeper"

guardiankeeper "github.com/irisnet/irishub/modules/guardian/keeper"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
// channel keeper.
type HandlerOptions struct {
ante.HandlerOptions
BankKeeper bankkeeper.Keeper
AccountKeeper authkeeper.AccountKeeper
TokenKeeper tokenkeeper.Keeper
OracleKeeper oraclekeeper.Keeper
GuardianKeeper guardiankeeper.Keeper
EvmKeeper ethante.EVMKeeper
FeeMarketKeeper ethante.FeeMarketKeeper
BypassMinFeeMsgTypes []string
MaxTxGasWanted uint64
}

// newCosmosAnteHandler creates the default ante handler for Ethereum transactions
func newEthAnteHandler(options HandlerOptions) sdk.AnteHandler {
return sdk.ChainAnteDecorators(
ethante.NewEthSetUpContextDecorator(options.EvmKeeper), // outermost AnteDecorator. SetUpContext must be called first
ethante.NewEthMempoolFeeDecorator(options.EvmKeeper), // Check eth effective gas price against the node's minimal-gas-prices config
ethante.NewEthMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper), // Check eth effective gas price against the global MinGasPrice
ethante.NewEthValidateBasicDecorator(options.EvmKeeper),
ethante.NewEthSigVerificationDecorator(options.EvmKeeper),
ethante.NewEthAccountVerificationDecorator(options.AccountKeeper, options.EvmKeeper),
ethante.NewCanTransferDecorator(options.EvmKeeper),
ethante.NewEthGasConsumeDecorator(options.EvmKeeper, options.MaxTxGasWanted),
ethante.NewEthIncrementSenderSequenceDecorator(options.AccountKeeper),
ethante.NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper),
ethante.NewEthEmitEventDecorator(options.EvmKeeper), // emit eth tx hash and index at the very last ante handler.
)
}

// newCosmosAnteHandler creates the default ante handler for Cosmos transactions
func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler {
var sigGasConsumer = options.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
}
return sdk.ChainAnteDecorators(
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, CheckTxFeeWithValidatorMinGasPrices),
//ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
NewValidateTokenDecorator(options.TokenKeeper),
tokenkeeper.NewValidateTokenFeeDecorator(options.TokenKeeper, options.BankKeeper),
oraclekeeper.NewValidateOracleAuthDecorator(options.OracleKeeper, options.GuardianKeeper),
NewValidateServiceDecorator(),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
)
}

// newCosmosAnteHandlerEip712 creates the ante handler for transactions signed with EIP712
func newCosmosAnteHandlerEip712(options HandlerOptions) sdk.AnteHandler {
return sdk.ChainAnteDecorators(
ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs
ante.NewSetUpContextDecorator(),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ethante.NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
// Note: signature verification uses EIP instead of the cosmos signature validator
ethante.NewLegacyEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ethante.NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper),
)
}
Loading