2020-04-17 22:32:01 +00:00
|
|
|
|
package ante
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"math/big"
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
log "github.com/xlab/suplog"
|
|
|
|
|
|
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2020-04-17 22:32:01 +00:00
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
|
|
|
|
|
2021-04-18 17:23:26 +00:00
|
|
|
|
ethermint "github.com/cosmos/ethermint/types"
|
2020-04-17 22:32:01 +00:00
|
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
|
|
2020-06-22 16:07:35 +00:00
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-04-17 22:32:01 +00:00
|
|
|
|
ethcore "github.com/ethereum/go-ethereum/core"
|
|
|
|
|
)
|
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
|
// EVMKeeper defines the expected keeper interface used on the Eth AnteHandler
|
|
|
|
|
type EVMKeeper interface {
|
|
|
|
|
GetParams(ctx sdk.Context) evmtypes.Params
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 22:32:01 +00:00
|
|
|
|
// EthSetupContextDecorator sets the infinite GasMeter in the Context and wraps
|
|
|
|
|
// the next AnteHandler with a defer clause to recover from any downstream
|
|
|
|
|
// OutOfGas panics in the AnteHandler chain to return an error with information
|
|
|
|
|
// on gas provided and gas used.
|
|
|
|
|
// CONTRACT: Must be first decorator in the chain
|
|
|
|
|
// CONTRACT: Tx must implement GasTx interface
|
|
|
|
|
type EthSetupContextDecorator struct{}
|
|
|
|
|
|
|
|
|
|
// NewEthSetupContextDecorator creates a new EthSetupContextDecorator
|
|
|
|
|
func NewEthSetupContextDecorator() EthSetupContextDecorator {
|
|
|
|
|
return EthSetupContextDecorator{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AnteHandle sets the infinite gas meter to done to ignore costs in AnteHandler checks.
|
|
|
|
|
// This is undone at the EthGasConsumeDecorator, where the context is set with the
|
|
|
|
|
// ethereum tx GasLimit.
|
|
|
|
|
func (escd EthSetupContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
|
|
2020-04-17 22:32:01 +00:00
|
|
|
|
// all transactions must implement GasTx
|
|
|
|
|
gasTx, ok := tx.(authante.GasTx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be GasTx")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decorator will catch an OutOfGasPanic caused in the next antehandler
|
|
|
|
|
// AnteHandlers must have their own defer/recover in order for the BaseApp
|
|
|
|
|
// to know how much gas was used! This is because the GasMeter is created in
|
|
|
|
|
// the AnteHandler, but if it panics the context won't be set properly in
|
|
|
|
|
// runTx's recover call.
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
switch rType := r.(type) {
|
|
|
|
|
case sdk.ErrorOutOfGas:
|
|
|
|
|
log := fmt.Sprintf(
|
|
|
|
|
"out of gas in location: %v; gasLimit: %d, gasUsed: %d",
|
|
|
|
|
rType.Descriptor, gasTx.GetGas(), ctx.GasMeter().GasConsumed(),
|
|
|
|
|
)
|
|
|
|
|
err = sdkerrors.Wrap(sdkerrors.ErrOutOfGas, log)
|
|
|
|
|
default:
|
2021-04-18 16:39:15 +00:00
|
|
|
|
log.Errorln(r)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
panic(r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EthMempoolFeeDecorator validates that sufficient fees have been provided that
|
|
|
|
|
// meet a minimum threshold defined by the proposer (for mempool purposes during CheckTx).
|
2020-09-02 19:41:05 +00:00
|
|
|
|
type EthMempoolFeeDecorator struct {
|
|
|
|
|
evmKeeper EVMKeeper
|
|
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
|
|
|
|
|
// NewEthMempoolFeeDecorator creates a new EthMempoolFeeDecorator
|
2020-09-02 19:41:05 +00:00
|
|
|
|
func NewEthMempoolFeeDecorator(ek EVMKeeper) EthMempoolFeeDecorator {
|
|
|
|
|
return EthMempoolFeeDecorator{
|
|
|
|
|
evmKeeper: ek,
|
|
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AnteHandle verifies that enough fees have been provided by the
|
|
|
|
|
// Ethereum transaction that meet the minimum threshold set by the block
|
|
|
|
|
// proposer.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: This should only be run during a CheckTx mode.
|
|
|
|
|
func (emfd EthMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
|
|
|
|
if !ctx.IsCheckTx() {
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
msgEthTx, ok := tx.(sdk.FeeTx)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if !ok {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type, not implements sdk.FeeTx: %T", tx)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
|
evmDenom := emfd.evmKeeper.GetParams(ctx).EvmDenom
|
2021-04-18 16:39:15 +00:00
|
|
|
|
txFee := msgEthTx.GetFee().AmountOf(evmDenom).Int64()
|
|
|
|
|
if txFee < 0 {
|
|
|
|
|
return ctx, sdkerrors.Wrap(
|
|
|
|
|
sdkerrors.ErrInsufficientFee,
|
|
|
|
|
"negative fee not allowed",
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-09-02 19:41:05 +00:00
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// txFee = GP * GL
|
|
|
|
|
fee := sdk.NewInt64DecCoin(evmDenom, txFee)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
|
|
|
|
|
minGasPrices := ctx.MinGasPrices()
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// check that fee provided is greater than the minimum
|
|
|
|
|
// NOTE: we only check if injs are present in min gas prices. It is up to the
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// sender if they want to send additional fees in other denominations.
|
|
|
|
|
var hasEnoughFees bool
|
2021-04-18 16:39:15 +00:00
|
|
|
|
if fee.Amount.GTE(minGasPrices.AmountOf(evmDenom)) {
|
2020-05-04 22:41:17 +00:00
|
|
|
|
hasEnoughFees = true
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// reject transaction if minimum gas price is positive and the transaction does not
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// meet the minimum fee
|
|
|
|
|
if !ctx.MinGasPrices().IsZero() && !hasEnoughFees {
|
2020-04-17 22:32:01 +00:00
|
|
|
|
return ctx, sdkerrors.Wrap(
|
|
|
|
|
sdkerrors.ErrInsufficientFee,
|
2021-04-18 16:39:15 +00:00
|
|
|
|
fmt.Sprintf("insufficient fee, got: %q required: %q", fee, ctx.MinGasPrices()),
|
2020-04-17 22:32:01 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// EthValidateBasicDecorator will call tx.ValidateBasic and return any non-nil error.
|
|
|
|
|
// If ValidateBasic passes, decorator calls next AnteHandler in chain. Note,
|
|
|
|
|
// EthValidateBasicDecorator decorator will not get executed on ReCheckTx since it
|
|
|
|
|
// is not dependent on application state.
|
|
|
|
|
type EthValidateBasicDecorator struct{}
|
|
|
|
|
|
|
|
|
|
func NewEthValidateBasicDecorator() EthValidateBasicDecorator {
|
|
|
|
|
return EthValidateBasicDecorator{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
|
|
|
|
// no need to validate basic on recheck tx, call next antehandler
|
|
|
|
|
if ctx.IsReCheckTx() {
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msgEthTx, ok := getTxMsg(tx).(*evmtypes.MsgEthereumTx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", getTxMsg(tx))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := msgEthTx.ValidateBasic(); err != nil {
|
|
|
|
|
return ctx, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 22:32:01 +00:00
|
|
|
|
// EthSigVerificationDecorator validates an ethereum signature
|
2021-04-18 16:39:15 +00:00
|
|
|
|
type EthSigVerificationDecorator struct {
|
|
|
|
|
interfaceRegistry codectypes.InterfaceRegistry
|
|
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
|
|
|
|
|
// NewEthSigVerificationDecorator creates a new EthSigVerificationDecorator
|
|
|
|
|
func NewEthSigVerificationDecorator() EthSigVerificationDecorator {
|
|
|
|
|
return EthSigVerificationDecorator{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AnteHandle validates the signature and returns sender address
|
|
|
|
|
func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
if simulate {
|
|
|
|
|
// when simulating, no signatures required and the from address is explicitly set
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msgEthTx, ok := getTxMsg(tx).(*evmtypes.MsgEthereumTx)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if !ok {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", getTxMsg(tx))
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse the chainID from a string to a base-10 integer
|
2021-04-18 17:23:26 +00:00
|
|
|
|
chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
|
2020-09-24 17:50:47 +00:00
|
|
|
|
if err != nil {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
fmt.Println("chain id parsing failed")
|
|
|
|
|
|
2020-09-24 17:50:47 +00:00
|
|
|
|
return ctx, err
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// validate sender/signature
|
|
|
|
|
_, eip155Err := msgEthTx.VerifySig(chainIDEpoch)
|
|
|
|
|
if eip155Err != nil {
|
|
|
|
|
_, homesteadErr := msgEthTx.VerifySigHomestead()
|
|
|
|
|
if homesteadErr != nil {
|
|
|
|
|
errMsg := fmt.Sprintf("signature verification failed for both EIP155 and Homestead signers: (%s, %s)",
|
|
|
|
|
eip155Err.Error(), homesteadErr.Error())
|
|
|
|
|
err := sdkerrors.Wrap(sdkerrors.ErrUnauthorized, errMsg)
|
|
|
|
|
return ctx, err
|
|
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// NOTE: when signature verification succeeds, a non-empty signer address can be
|
|
|
|
|
// retrieved from the transaction on the next AnteDecorators.
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type noMessages struct{}
|
|
|
|
|
|
|
|
|
|
func getTxMsg(tx sdk.Tx) interface{} {
|
|
|
|
|
msgs := tx.GetMsgs()
|
|
|
|
|
if len(msgs) == 0 {
|
|
|
|
|
return &noMessages{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msgs[0]
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// EthAccountVerificationDecorator validates an account balance checks
|
|
|
|
|
type EthAccountVerificationDecorator struct {
|
2021-04-17 10:00:07 +00:00
|
|
|
|
ak AccountKeeper
|
|
|
|
|
bankKeeper BankKeeper
|
|
|
|
|
evmKeeper EVMKeeper
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// NewEthAccountVerificationDecorator creates a new EthAccountVerificationDecorator
|
|
|
|
|
func NewEthAccountVerificationDecorator(ak AccountKeeper, bankKeeper BankKeeper, ek EVMKeeper) EthAccountVerificationDecorator {
|
|
|
|
|
return EthAccountVerificationDecorator{
|
2021-04-17 10:00:07 +00:00
|
|
|
|
ak: ak,
|
|
|
|
|
bankKeeper: bankKeeper,
|
|
|
|
|
evmKeeper: ek,
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AnteHandle validates the signature and returns sender address
|
2021-04-18 16:39:15 +00:00
|
|
|
|
func (avd EthAccountVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if !ctx.IsCheckTx() {
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
msgEthTx, ok := getTxMsg(tx).(*evmtypes.MsgEthereumTx)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if !ok {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", getTxMsg(tx))
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// sender address should be in the tx cache from the previous AnteHandle call
|
2021-04-17 10:00:07 +00:00
|
|
|
|
address := msgEthTx.GetFrom()
|
2020-05-04 22:41:17 +00:00
|
|
|
|
if address.Empty() {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
log.Panicln("sender address cannot be empty")
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acc := avd.ak.GetAccount(ctx, address)
|
|
|
|
|
if acc == nil {
|
2020-09-17 22:04:36 +00:00
|
|
|
|
acc = avd.ak.NewAccountWithAddress(ctx, address)
|
|
|
|
|
avd.ak.SetAccount(ctx, acc)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// on InitChain make sure account number == 0
|
|
|
|
|
if ctx.BlockHeight() == 0 && acc.GetAccountNumber() != 0 {
|
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
|
sdkerrors.ErrInvalidSequence,
|
|
|
|
|
"invalid account number for height zero (got %d)", acc.GetAccountNumber(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
|
evmDenom := avd.evmKeeper.GetParams(ctx).EvmDenom
|
|
|
|
|
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// validate sender has enough funds to pay for gas cost
|
2021-04-17 10:00:07 +00:00
|
|
|
|
balance := avd.bankKeeper.GetBalance(ctx, address, evmDenom)
|
|
|
|
|
if balance.Amount.BigInt().Cmp(msgEthTx.Cost()) < 0 {
|
2020-04-17 22:32:01 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
|
sdkerrors.ErrInsufficientFunds,
|
2020-09-02 19:41:05 +00:00
|
|
|
|
"sender balance < tx gas cost (%s%s < %s%s)", balance.String(), evmDenom, msgEthTx.Cost().String(), evmDenom,
|
2020-04-17 22:32:01 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// EthNonceVerificationDecorator checks that the account nonce from the transaction matches
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// the sender account sequence.
|
2021-04-18 16:39:15 +00:00
|
|
|
|
type EthNonceVerificationDecorator struct {
|
2021-04-17 10:00:07 +00:00
|
|
|
|
ak AccountKeeper
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// NewEthNonceVerificationDecorator creates a new EthNonceVerificationDecorator
|
|
|
|
|
func NewEthNonceVerificationDecorator(ak AccountKeeper) EthNonceVerificationDecorator {
|
|
|
|
|
return EthNonceVerificationDecorator{
|
2020-04-17 22:32:01 +00:00
|
|
|
|
ak: ak,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AnteHandle validates that the transaction nonce is valid (equivalent to the sender account’s
|
|
|
|
|
// current nonce).
|
2021-04-18 16:39:15 +00:00
|
|
|
|
func (nvd EthNonceVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
|
|
|
|
msgEthTx, ok := getTxMsg(tx).(*evmtypes.MsgEthereumTx)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if !ok {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", getTxMsg(tx))
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// sender address should be in the tx cache from the previous AnteHandle call
|
2021-04-17 10:00:07 +00:00
|
|
|
|
address := msgEthTx.GetFrom()
|
2020-05-04 22:41:17 +00:00
|
|
|
|
if address.Empty() {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
log.Panicln("sender address cannot be empty")
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acc := nvd.ak.GetAccount(ctx, address)
|
|
|
|
|
if acc == nil {
|
2020-09-23 14:49:20 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
|
sdkerrors.ErrUnknownAddress,
|
|
|
|
|
"account %s (%s) is nil", common.BytesToAddress(address.Bytes()), address,
|
|
|
|
|
)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seq := acc.GetSequence()
|
2020-09-29 14:39:15 +00:00
|
|
|
|
// if multiple transactions are submitted in succession with increasing nonces,
|
|
|
|
|
// all will be rejected except the first, since the first needs to be included in a block
|
|
|
|
|
// before the sequence increments
|
2021-01-09 01:44:50 +00:00
|
|
|
|
if msgEthTx.Data.AccountNonce != seq {
|
2020-09-23 14:49:20 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
2020-04-17 22:32:01 +00:00
|
|
|
|
sdkerrors.ErrInvalidSequence,
|
2020-09-23 14:49:20 +00:00
|
|
|
|
"invalid nonce; got %d, expected %d", msgEthTx.Data.AccountNonce, seq,
|
2020-04-17 22:32:01 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EthGasConsumeDecorator validates enough intrinsic gas for the transaction and
|
|
|
|
|
// gas consumption.
|
|
|
|
|
type EthGasConsumeDecorator struct {
|
2021-04-17 10:00:07 +00:00
|
|
|
|
ak AccountKeeper
|
|
|
|
|
bankKeeper BankKeeper
|
|
|
|
|
evmKeeper EVMKeeper
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewEthGasConsumeDecorator creates a new EthGasConsumeDecorator
|
2021-04-17 10:00:07 +00:00
|
|
|
|
func NewEthGasConsumeDecorator(ak AccountKeeper, bankKeeper BankKeeper, ek EVMKeeper) EthGasConsumeDecorator {
|
2020-04-17 22:32:01 +00:00
|
|
|
|
return EthGasConsumeDecorator{
|
2021-04-17 10:00:07 +00:00
|
|
|
|
ak: ak,
|
|
|
|
|
bankKeeper: bankKeeper,
|
|
|
|
|
evmKeeper: ek,
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AnteHandle validates that the Ethereum tx message has enough to cover intrinsic gas
|
|
|
|
|
// (during CheckTx only) and that the sender has enough balance to pay for the gas cost.
|
|
|
|
|
//
|
|
|
|
|
// Intrinsic gas for a transaction is the amount of gas
|
|
|
|
|
// that the transaction uses before the transaction is executed. The gas is a
|
|
|
|
|
// constant value of 21000 plus any cost inccured by additional bytes of data
|
|
|
|
|
// supplied with the transaction.
|
|
|
|
|
func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
msgEthTx, ok := getTxMsg(tx).(*evmtypes.MsgEthereumTx)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if !ok {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", getTxMsg(tx))
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 14:49:20 +00:00
|
|
|
|
// sender address should be in the tx cache from the previous AnteHandle call
|
2021-04-17 10:00:07 +00:00
|
|
|
|
address := msgEthTx.GetFrom()
|
2020-05-04 22:41:17 +00:00
|
|
|
|
if address.Empty() {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
log.Panicln("sender address cannot be empty")
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 14:49:20 +00:00
|
|
|
|
// fetch sender account from signature
|
2021-04-17 10:00:07 +00:00
|
|
|
|
senderAcc, err := authante.GetSignerAcc(ctx, egcd.ak, address)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if senderAcc == nil {
|
2020-09-23 14:49:20 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
|
sdkerrors.ErrUnknownAddress,
|
|
|
|
|
"sender account %s (%s) is nil", common.BytesToAddress(address.Bytes()), address,
|
|
|
|
|
)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gasLimit := msgEthTx.GetGas()
|
2020-04-23 17:21:23 +00:00
|
|
|
|
gas, err := ethcore.IntrinsicGas(msgEthTx.Data.Payload, msgEthTx.To() == nil, true, false)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx, sdkerrors.Wrap(err, "failed to compute intrinsic gas cost")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// intrinsic gas verification during CheckTx
|
|
|
|
|
if ctx.IsCheckTx() && gasLimit < gas {
|
2020-09-23 14:49:20 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrOutOfGas, "intrinsic gas too low: %d < %d", gasLimit, gas)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Charge sender for gas up to limit
|
|
|
|
|
if gasLimit != 0 {
|
|
|
|
|
// Cost calculates the fees paid to validators based on gas limit and price
|
2021-04-18 16:39:15 +00:00
|
|
|
|
cost := new(big.Int).Mul(new(big.Int).SetBytes(msgEthTx.Data.Price), new(big.Int).SetUint64(gasLimit))
|
2020-04-17 22:32:01 +00:00
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
|
evmDenom := egcd.evmKeeper.GetParams(ctx).EvmDenom
|
|
|
|
|
|
2020-04-17 22:32:01 +00:00
|
|
|
|
feeAmt := sdk.NewCoins(
|
2020-09-02 19:41:05 +00:00
|
|
|
|
sdk.NewCoin(evmDenom, sdk.NewIntFromBigInt(cost)),
|
2020-04-17 22:32:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
err = authante.DeductFees(egcd.bankKeeper, ctx, senderAcc, feeAmt)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set gas meter after ante handler to ignore gaskv costs
|
2021-04-17 10:00:07 +00:00
|
|
|
|
newCtx = authante.SetGasMeter(simulate, ctx, gasLimit)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
return next(newCtx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// EthIncrementSenderSequenceDecorator increments the sequence of the signers. The
|
2020-04-17 22:32:01 +00:00
|
|
|
|
// main difference with the SDK's IncrementSequenceDecorator is that the MsgEthereumTx
|
|
|
|
|
// doesn't implement the SigVerifiableTx interface.
|
|
|
|
|
//
|
|
|
|
|
// CONTRACT: must be called after msg.VerifySig in order to cache the sender address.
|
2021-04-18 16:39:15 +00:00
|
|
|
|
type EthIncrementSenderSequenceDecorator struct {
|
2021-04-17 10:00:07 +00:00
|
|
|
|
ak AccountKeeper
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
// NewEthIncrementSenderSequenceDecorator creates a new EthIncrementSenderSequenceDecorator.
|
|
|
|
|
func NewEthIncrementSenderSequenceDecorator(ak AccountKeeper) EthIncrementSenderSequenceDecorator {
|
|
|
|
|
return EthIncrementSenderSequenceDecorator{
|
2020-04-17 22:32:01 +00:00
|
|
|
|
ak: ak,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AnteHandle handles incrementing the sequence of the sender.
|
2021-04-18 16:39:15 +00:00
|
|
|
|
func (issd EthIncrementSenderSequenceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// get and set account must be called with an infinite gas meter in order to prevent
|
|
|
|
|
// additional gas from being deducted.
|
|
|
|
|
gasMeter := ctx.GasMeter()
|
|
|
|
|
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
|
msgEthTx, ok := getTxMsg(tx).(*evmtypes.MsgEthereumTx)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if !ok {
|
2020-05-04 22:41:17 +00:00
|
|
|
|
ctx = ctx.WithGasMeter(gasMeter)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", getTxMsg(tx))
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// increment sequence of all signers
|
|
|
|
|
for _, addr := range msgEthTx.GetSigners() {
|
2020-05-04 22:41:17 +00:00
|
|
|
|
acc := issd.ak.GetAccount(ctx, addr)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
if err := acc.SetSequence(acc.GetSequence() + 1); err != nil {
|
2021-04-18 16:39:15 +00:00
|
|
|
|
log.WithError(err).Panicln("failed to set acc sequence")
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
2020-05-04 22:41:17 +00:00
|
|
|
|
issd.ak.SetAccount(ctx, acc)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 22:41:17 +00:00
|
|
|
|
// set the original gas meter
|
|
|
|
|
ctx = ctx.WithGasMeter(gasMeter)
|
2020-04-17 22:32:01 +00:00
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
|
|
// EthAccountSetupDecorator sets an account to state if it's not stored already. This only applies for MsgEthermint.
|
|
|
|
|
type EthAccountSetupDecorator struct {
|
|
|
|
|
ak AccountKeeper
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewEthAccountSetupDecorator creates a new EthAccountSetupDecorator instance
|
|
|
|
|
func NewEthAccountSetupDecorator(ak AccountKeeper) EthAccountSetupDecorator {
|
|
|
|
|
return EthAccountSetupDecorator{
|
|
|
|
|
ak: ak,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AnteHandle sets an account for MsgEthereumTx (evm) if the sender is registered.
|
|
|
|
|
func (asd EthAccountSetupDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
|
|
|
|
// get and set account must be called with an infinite gas meter in order to prevent
|
|
|
|
|
// additional gas from being deducted.
|
|
|
|
|
gasMeter := ctx.GasMeter()
|
|
|
|
|
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
|
|
|
|
|
|
msgEthTx, ok := getTxMsg(tx).(*evmtypes.MsgEthereumTx)
|
|
|
|
|
if !ok {
|
|
|
|
|
ctx = ctx.WithGasMeter(gasMeter)
|
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", getTxMsg(tx))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setupAccount(asd.ak, ctx, msgEthTx.GetFrom())
|
|
|
|
|
|
|
|
|
|
// set the original gas meter
|
|
|
|
|
ctx = ctx.WithGasMeter(gasMeter)
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setupAccount(ak AccountKeeper, ctx sdk.Context, addr sdk.AccAddress) {
|
|
|
|
|
acc := ak.GetAccount(ctx, addr)
|
|
|
|
|
if acc != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acc = ak.NewAccountWithAddress(ctx, addr)
|
|
|
|
|
ak.SetAccount(ctx, acc)
|
|
|
|
|
}
|