2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2021-06-02 08:52:53 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
2021-06-07 11:00:14 +00:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2022-11-14 19:40:14 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2021-06-02 08:52:53 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
"github.com/evmos/ethermint/x/evm/statedb"
|
|
|
|
"github.com/evmos/ethermint/x/evm/types"
|
2022-09-15 11:54:02 +00:00
|
|
|
evm "github.com/evmos/ethermint/x/evm/vm"
|
2021-06-02 08:52:53 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2021-06-08 17:10:29 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-06-02 08:52:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2022-01-04 11:02:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-06-02 08:52:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// NewEVM generates a go-ethereum VM from the provided Message fields and the chain parameters
|
|
|
|
// (ChainConfig and module Params). It additionally sets the validator operator address as the
|
|
|
|
// coinbase address to make it available for the COINBASE opcode, even though there is no
|
|
|
|
// beneficiary of the coinbase transaction (since we're not mining).
|
2022-12-02 13:00:46 +00:00
|
|
|
//
|
|
|
|
// NOTE: the RANDOM opcode is currently not supported since it requires
|
|
|
|
// RANDAO implementation. See https://github.com/evmos/ethermint/pull/1520#pullrequestreview-1200504697
|
|
|
|
// for more information.
|
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
func (k *Keeper) NewEVM(
|
2022-01-05 07:28:27 +00:00
|
|
|
ctx sdk.Context,
|
2021-08-16 09:45:10 +00:00
|
|
|
msg core.Message,
|
2022-12-30 12:20:14 +00:00
|
|
|
cfg *statedb.EVMConfig,
|
2022-02-14 23:08:41 +00:00
|
|
|
tracer vm.EVMLogger,
|
2022-01-05 07:28:27 +00:00
|
|
|
stateDB vm.StateDB,
|
2022-09-15 11:54:02 +00:00
|
|
|
) evm.EVM {
|
2021-06-02 08:52:53 +00:00
|
|
|
blockCtx := vm.BlockContext{
|
|
|
|
CanTransfer: core.CanTransfer,
|
|
|
|
Transfer: core.Transfer,
|
2022-01-05 07:28:27 +00:00
|
|
|
GetHash: k.GetHashFn(ctx),
|
2021-10-22 17:21:03 +00:00
|
|
|
Coinbase: cfg.CoinBase,
|
2022-01-05 07:28:27 +00:00
|
|
|
GasLimit: ethermint.BlockGasLimit(ctx),
|
|
|
|
BlockNumber: big.NewInt(ctx.BlockHeight()),
|
|
|
|
Time: big.NewInt(ctx.BlockHeader().Time.Unix()),
|
2021-06-02 08:52:53 +00:00
|
|
|
Difficulty: big.NewInt(0), // unused. Only required in PoW context
|
2021-10-22 17:21:03 +00:00
|
|
|
BaseFee: cfg.BaseFee,
|
2022-12-02 13:00:46 +00:00
|
|
|
Random: nil, // not supported
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txCtx := core.NewEVMTxContext(msg)
|
2021-10-22 17:21:03 +00:00
|
|
|
if tracer == nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
tracer = k.Tracer(ctx, msg, cfg.ChainConfig)
|
2021-10-22 17:21:03 +00:00
|
|
|
}
|
2022-02-15 11:01:30 +00:00
|
|
|
vmConfig := k.VMConfig(ctx, msg, cfg, tracer)
|
2022-09-15 11:54:02 +00:00
|
|
|
return k.evmConstructor(blockCtx, txCtx, stateDB, cfg.ChainConfig, vmConfig, k.customPrecompiles)
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHashFn implements vm.GetHashFunc for Ethermint. It handles 3 cases:
|
|
|
|
// 1. The requested height matches the current height from context (and thus same epoch number)
|
|
|
|
// 2. The requested height is from an previous height from the same chain epoch
|
|
|
|
// 3. The requested height is from a height greater than the latest one
|
2022-01-05 07:28:27 +00:00
|
|
|
func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc {
|
2021-06-02 08:52:53 +00:00
|
|
|
return func(height uint64) common.Hash {
|
2021-09-28 09:48:11 +00:00
|
|
|
h, err := ethermint.SafeInt64(height)
|
|
|
|
if err != nil {
|
|
|
|
k.Logger(ctx).Error("failed to cast height to int64", "error", err)
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
2021-06-02 08:52:53 +00:00
|
|
|
switch {
|
2021-08-31 12:50:31 +00:00
|
|
|
case ctx.BlockHeight() == h:
|
2021-06-02 08:52:53 +00:00
|
|
|
// Case 1: The requested height matches the one from the context so we can retrieve the header
|
|
|
|
// hash directly from the context.
|
2021-07-19 01:52:44 +00:00
|
|
|
// Note: The headerHash is only set at begin block, it will be nil in case of a query context
|
2021-08-31 12:50:31 +00:00
|
|
|
headerHash := ctx.HeaderHash()
|
2021-07-19 01:52:44 +00:00
|
|
|
if len(headerHash) != 0 {
|
|
|
|
return common.BytesToHash(headerHash)
|
|
|
|
}
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// only recompute the hash if not set (eg: checkTxState)
|
2021-08-31 12:50:31 +00:00
|
|
|
contextBlockHeader := ctx.BlockHeader()
|
2021-07-19 01:52:44 +00:00
|
|
|
header, err := tmtypes.HeaderFromProto(&contextBlockHeader)
|
|
|
|
if err != nil {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Error("failed to cast tendermint header from proto", "error", err)
|
2021-07-19 01:52:44 +00:00
|
|
|
return common.Hash{}
|
|
|
|
}
|
2021-08-05 16:24:06 +00:00
|
|
|
|
2021-07-19 01:52:44 +00:00
|
|
|
headerHash = header.Hash()
|
|
|
|
return common.BytesToHash(headerHash)
|
2021-06-02 08:52:53 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
case ctx.BlockHeight() > h:
|
2021-06-02 08:52:53 +00:00
|
|
|
// Case 2: if the chain is not the current height we need to retrieve the hash from the store for the
|
|
|
|
// current chain epoch. This only applies if the current height is greater than the requested height.
|
2021-08-31 12:50:31 +00:00
|
|
|
histInfo, found := k.stakingKeeper.GetHistoricalInfo(ctx, h)
|
2021-06-07 11:00:14 +00:00
|
|
|
if !found {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug("historical info not found", "height", h)
|
2021-06-07 11:00:14 +00:00
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := tmtypes.HeaderFromProto(&histInfo.Header)
|
|
|
|
if err != nil {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Error("failed to cast tendermint header from proto", "error", err)
|
2021-06-07 11:00:14 +00:00
|
|
|
return common.Hash{}
|
|
|
|
}
|
2021-06-02 08:52:53 +00:00
|
|
|
|
2021-06-07 11:00:14 +00:00
|
|
|
return common.BytesToHash(header.Hash())
|
2021-06-02 08:52:53 +00:00
|
|
|
default:
|
|
|
|
// Case 3: heights greater than the current one returns an empty hash.
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
// ApplyTransaction runs and attempts to perform a state transition with the given transaction (i.e Message), that will
|
2021-08-05 16:24:06 +00:00
|
|
|
// only be persisted (committed) to the underlying KVStore if the transaction does not fail.
|
2021-06-02 08:52:53 +00:00
|
|
|
//
|
2022-08-08 08:17:10 +00:00
|
|
|
// # Gas tracking
|
2021-06-02 08:52:53 +00:00
|
|
|
//
|
|
|
|
// Ethereum consumes gas according to the EVM opcodes instead of general reads and writes to store. Because of this, the
|
|
|
|
// state transition needs to ignore the SDK gas consumption mechanism defined by the GasKVStore and instead consume the
|
|
|
|
// amount of gas used by the VM execution. The amount of gas used is tracked by the EVM and returned in the execution
|
|
|
|
// result.
|
|
|
|
//
|
|
|
|
// Prior to the execution, the starting tx gas meter is saved and replaced with an infinite gas meter in a new context
|
|
|
|
// in order to ignore the SDK gas consumption config values (read, write, has, delete).
|
|
|
|
// After the execution, the gas used from the message execution will be added to the starting gas consumed, taking into
|
|
|
|
// consideration the amount of gas returned. Finally, the context is updated with the EVM gas consumed value prior to
|
|
|
|
// returning.
|
|
|
|
//
|
|
|
|
// For relevant discussion see: https://github.com/cosmos/cosmos-sdk/discussions/9072
|
2022-01-05 07:28:27 +00:00
|
|
|
func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*types.MsgEthereumTxResponse, error) {
|
2022-01-04 11:02:41 +00:00
|
|
|
var (
|
|
|
|
bloom *big.Int
|
|
|
|
bloomReceipt ethtypes.Bloom
|
|
|
|
)
|
|
|
|
|
2022-11-18 18:19:09 +00:00
|
|
|
cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress), k.eip155ChainID)
|
2021-10-22 17:21:03 +00:00
|
|
|
if err != nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(err, "failed to load evm config")
|
2021-08-15 14:54:44 +00:00
|
|
|
}
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig := k.TxConfig(ctx, tx.Hash())
|
2021-08-15 14:54:44 +00:00
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
// get the signer according to the chain rules from the config and block height
|
2021-10-22 17:21:03 +00:00
|
|
|
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
|
2021-12-28 07:59:28 +00:00
|
|
|
msg, err := tx.AsMessage(signer, cfg.BaseFee)
|
2021-06-08 17:10:29 +00:00
|
|
|
if err != nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(err, "failed to return ethereum transaction as core message")
|
2021-06-08 17:10:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 17:21:03 +00:00
|
|
|
// snapshot to contain the tx processing and post processing in same scope
|
|
|
|
var commit func()
|
2022-01-05 07:28:27 +00:00
|
|
|
tmpCtx := ctx
|
2021-09-02 17:47:39 +00:00
|
|
|
if k.hooks != nil {
|
2021-10-22 17:21:03 +00:00
|
|
|
// Create a cache context to revert state when tx hooks fails,
|
|
|
|
// the cache context is only committed when both tx and hooks executed successfully.
|
|
|
|
// Didn't use `Snapshot` because the context stack has exponential complexity on certain operations,
|
|
|
|
// thus restricted to be used only inside `ApplyMessage`.
|
2022-01-05 07:28:27 +00:00
|
|
|
tmpCtx, commit = ctx.CacheContext()
|
2021-09-02 17:47:39 +00:00
|
|
|
}
|
2021-09-02 12:36:33 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// pass true to commit the StateDB
|
|
|
|
res, err := k.ApplyMessageWithConfig(tmpCtx, msg, nil, true, cfg, txConfig)
|
2021-06-02 08:52:53 +00:00
|
|
|
if err != nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(err, "failed to apply ethereum core message")
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
logs := types.LogsToEthereum(res.Logs)
|
2021-09-02 12:36:33 +00:00
|
|
|
|
2022-01-04 11:02:41 +00:00
|
|
|
// Compute block bloom filter
|
2021-08-10 07:22:46 +00:00
|
|
|
if len(logs) > 0 {
|
2022-01-05 07:28:27 +00:00
|
|
|
bloom = k.GetBlockBloomTransient(ctx)
|
2021-07-08 08:14:11 +00:00
|
|
|
bloom.Or(bloom, big.NewInt(0).SetBytes(ethtypes.LogsBloom(logs)))
|
2022-01-03 16:18:13 +00:00
|
|
|
bloomReceipt = ethtypes.BytesToBloom(bloom.Bytes())
|
2021-07-08 08:14:11 +00:00
|
|
|
}
|
2021-06-08 17:10:29 +00:00
|
|
|
|
2022-04-05 15:38:18 +00:00
|
|
|
cumulativeGasUsed := res.GasUsed
|
|
|
|
if ctx.BlockGasMeter() != nil {
|
|
|
|
limit := ctx.BlockGasMeter().Limit()
|
2022-11-18 21:35:51 +00:00
|
|
|
cumulativeGasUsed += ctx.BlockGasMeter().GasConsumed()
|
|
|
|
if cumulativeGasUsed > limit {
|
|
|
|
cumulativeGasUsed = limit
|
|
|
|
}
|
2022-04-05 15:38:18 +00:00
|
|
|
}
|
2022-01-03 16:18:13 +00:00
|
|
|
|
2022-04-05 15:38:18 +00:00
|
|
|
var contractAddr common.Address
|
|
|
|
if msg.To() == nil {
|
|
|
|
contractAddr = crypto.CreateAddress(msg.From(), msg.Nonce())
|
|
|
|
}
|
2022-01-04 11:02:41 +00:00
|
|
|
|
2022-04-05 15:38:18 +00:00
|
|
|
receipt := ðtypes.Receipt{
|
|
|
|
Type: tx.Type(),
|
|
|
|
PostState: nil, // TODO: intermediate state root
|
|
|
|
CumulativeGasUsed: cumulativeGasUsed,
|
|
|
|
Bloom: bloomReceipt,
|
|
|
|
Logs: logs,
|
|
|
|
TxHash: txConfig.TxHash,
|
|
|
|
ContractAddress: contractAddr,
|
|
|
|
GasUsed: res.GasUsed,
|
|
|
|
BlockHash: txConfig.BlockHash,
|
|
|
|
BlockNumber: big.NewInt(ctx.BlockHeight()),
|
|
|
|
TransactionIndex: txConfig.TxIndex,
|
|
|
|
}
|
2022-01-03 16:18:13 +00:00
|
|
|
|
2022-04-05 15:38:18 +00:00
|
|
|
if !res.Failed() {
|
|
|
|
receipt.Status = ethtypes.ReceiptStatusSuccessful
|
2022-01-03 16:18:13 +00:00
|
|
|
// Only call hooks if tx executed successfully.
|
2022-04-04 18:11:46 +00:00
|
|
|
if err = k.PostTxProcessing(tmpCtx, msg, receipt); err != nil {
|
2022-01-03 16:18:13 +00:00
|
|
|
// If hooks return error, revert the whole tx.
|
|
|
|
res.VmError = types.ErrPostTxProcessing.Error()
|
|
|
|
k.Logger(ctx).Error("tx post processing failed", "error", err)
|
2022-06-21 11:21:52 +00:00
|
|
|
|
|
|
|
// If the tx failed in post processing hooks, we should clear the logs
|
|
|
|
res.Logs = nil
|
2022-01-03 16:18:13 +00:00
|
|
|
} else if commit != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
// PostTxProcessing is successful, commit the tmpCtx
|
2022-01-03 16:18:13 +00:00
|
|
|
commit()
|
2023-01-05 16:24:18 +00:00
|
|
|
// Since the post-processing can alter the log, we need to update the result
|
2022-05-19 10:11:26 +00:00
|
|
|
res.Logs = types.NewLogsFromEth(receipt.Logs)
|
2022-01-05 07:28:27 +00:00
|
|
|
ctx.EventManager().EmitEvents(tmpCtx.EventManager().Events())
|
2022-01-03 16:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// refund gas in order to match the Ethereum gas consumption instead of the default SDK one.
|
|
|
|
if err = k.RefundGas(ctx, msg, msg.Gas()-res.GasUsed, cfg.Params.EvmDenom); err != nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrapf(err, "failed to refund gas leftover gas to sender %s", msg.From())
|
2022-01-04 11:02:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 15:38:18 +00:00
|
|
|
if len(receipt.Logs) > 0 {
|
2022-01-04 11:02:41 +00:00
|
|
|
// Update transient block bloom filter
|
2022-04-05 15:38:18 +00:00
|
|
|
k.SetBlockBloomTransient(ctx, receipt.Bloom.Big())
|
|
|
|
k.SetLogSizeTransient(ctx, uint64(txConfig.LogIndex)+uint64(len(receipt.Logs)))
|
2022-01-04 11:02:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
k.SetTxIndexTransient(ctx, uint64(txConfig.TxIndex)+1)
|
2022-01-04 11:02:41 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
totalGasUsed, err := k.AddTransientGasUsed(ctx, res.GasUsed)
|
|
|
|
if err != nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(err, "failed to add transient gas used")
|
2022-01-14 09:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset the gas meter for current cosmos transaction
|
|
|
|
k.ResetGasMeterAndConsumeGas(ctx, totalGasUsed)
|
2021-06-08 17:10:29 +00:00
|
|
|
return res, nil
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 12:20:14 +00:00
|
|
|
// ApplyMessage calls ApplyMessageWithConfig with an empty TxConfig.
|
|
|
|
func (k *Keeper) ApplyMessage(ctx sdk.Context, msg core.Message, tracer vm.EVMLogger, commit bool) (*types.MsgEthereumTxResponse, error) {
|
|
|
|
cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress), k.eip155ChainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errorsmod.Wrap(err, "failed to load evm config")
|
|
|
|
}
|
|
|
|
|
|
|
|
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
|
|
|
|
return k.ApplyMessageWithConfig(ctx, msg, tracer, commit, cfg, txConfig)
|
|
|
|
}
|
|
|
|
|
2021-10-22 17:21:03 +00:00
|
|
|
// ApplyMessageWithConfig computes the new state by applying the given message against the existing state.
|
2021-06-14 19:13:31 +00:00
|
|
|
// If the message fails, the VM execution error with the reason will be returned to the client
|
|
|
|
// and the transaction won't be committed to the store.
|
|
|
|
//
|
2022-08-08 08:17:10 +00:00
|
|
|
// # Reverted state
|
2021-06-14 19:13:31 +00:00
|
|
|
//
|
2022-01-05 07:28:27 +00:00
|
|
|
// The snapshot and rollback are supported by the `statedb.StateDB`.
|
2021-10-22 17:21:03 +00:00
|
|
|
//
|
2022-08-08 08:17:10 +00:00
|
|
|
// # Different Callers
|
2021-10-22 17:21:03 +00:00
|
|
|
//
|
|
|
|
// It's called in three scenarios:
|
|
|
|
// 1. `ApplyTransaction`, in the transaction processing flow.
|
|
|
|
// 2. `EthCall/EthEstimateGas` grpc query handler.
|
|
|
|
// 3. Called by other native modules directly.
|
2021-06-14 19:13:31 +00:00
|
|
|
//
|
2022-08-08 08:17:10 +00:00
|
|
|
// # Prechecks and Preprocessing
|
2021-06-14 19:13:31 +00:00
|
|
|
//
|
|
|
|
// All relevant state transition prechecks for the MsgEthereumTx are performed on the AnteHandler,
|
|
|
|
// prior to running the transaction against the state. The prechecks run are the following:
|
|
|
|
//
|
|
|
|
// 1. the nonce of the message caller is correct
|
|
|
|
// 2. caller has enough balance to cover transaction fee(gaslimit * gasprice)
|
|
|
|
// 3. the amount of gas required is available in the block
|
|
|
|
// 4. the purchased gas is enough to cover intrinsic usage
|
|
|
|
// 5. there is no overflow when calculating intrinsic gas
|
|
|
|
// 6. caller has enough balance to cover asset transfer for **topmost** call
|
|
|
|
//
|
|
|
|
// The preprocessing steps performed by the AnteHandler are:
|
|
|
|
//
|
|
|
|
// 1. set up the initial access list (iff fork > Berlin)
|
2021-07-15 06:01:05 +00:00
|
|
|
//
|
2022-08-08 08:17:10 +00:00
|
|
|
// # Tracer parameter
|
2021-07-15 06:01:05 +00:00
|
|
|
//
|
2021-10-22 17:21:03 +00:00
|
|
|
// It should be a `vm.Tracer` object or nil, if pass `nil`, it'll create a default one based on keeper options.
|
|
|
|
//
|
2022-08-08 08:17:10 +00:00
|
|
|
// # Commit parameter
|
2021-10-22 17:21:03 +00:00
|
|
|
//
|
2022-01-05 07:28:27 +00:00
|
|
|
// If commit is true, the `StateDB` will be committed, otherwise discarded.
|
2022-08-26 10:30:55 +00:00
|
|
|
func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
|
|
|
|
msg core.Message,
|
|
|
|
tracer vm.EVMLogger,
|
|
|
|
commit bool,
|
2022-12-30 12:20:14 +00:00
|
|
|
cfg *statedb.EVMConfig,
|
2022-08-26 10:30:55 +00:00
|
|
|
txConfig statedb.TxConfig,
|
|
|
|
) (*types.MsgEthereumTxResponse, error) {
|
2021-06-02 08:52:53 +00:00
|
|
|
var (
|
2022-01-04 11:02:41 +00:00
|
|
|
ret []byte // return bytes from evm execution
|
|
|
|
vmErr error // vm errors do not effect consensus and are therefore not assigned to err
|
2021-06-02 08:52:53 +00:00
|
|
|
)
|
|
|
|
|
2021-10-22 17:21:03 +00:00
|
|
|
// return error if contract creation or call are disabled through governance
|
|
|
|
if !cfg.Params.EnableCreate && msg.To() == nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(types.ErrCreateDisabled, "failed to create new contract")
|
2021-10-22 17:21:03 +00:00
|
|
|
} else if !cfg.Params.EnableCall && msg.To() != nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(types.ErrCallDisabled, "failed to call contract")
|
2021-10-22 17:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
stateDB := statedb.New(ctx, k, txConfig)
|
|
|
|
evm := k.NewEVM(ctx, msg, cfg, tracer, stateDB)
|
|
|
|
|
2022-07-18 20:16:28 +00:00
|
|
|
leftoverGas := msg.Gas()
|
2022-09-15 11:54:02 +00:00
|
|
|
|
2022-07-18 20:16:28 +00:00
|
|
|
// Allow the tracer captures the tx level events, mainly the gas consumption.
|
2022-09-15 11:54:02 +00:00
|
|
|
vmCfg := evm.Config()
|
|
|
|
if vmCfg.Debug {
|
|
|
|
vmCfg.Tracer.CaptureTxStart(leftoverGas)
|
2022-07-18 20:16:28 +00:00
|
|
|
defer func() {
|
2022-09-15 11:54:02 +00:00
|
|
|
vmCfg.Tracer.CaptureTxEnd(leftoverGas)
|
2022-07-18 20:16:28 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-06-02 08:52:53 +00:00
|
|
|
sender := vm.AccountRef(msg.From())
|
|
|
|
contractCreation := msg.To() == nil
|
2022-09-15 11:54:02 +00:00
|
|
|
isLondon := cfg.ChainConfig.IsLondon(evm.Context().BlockNumber)
|
2021-06-02 08:52:53 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
intrinsicGas, err := k.GetEthIntrinsicGas(ctx, msg, cfg.ChainConfig, contractCreation)
|
2021-06-30 09:31:30 +00:00
|
|
|
if err != nil {
|
|
|
|
// should have already been checked on Ante Handler
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(err, "intrinsic gas failed")
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
2022-06-27 09:58:44 +00:00
|
|
|
|
2021-07-15 06:01:05 +00:00
|
|
|
// Should check again even if it is checked on Ante Handler, because eth_call don't go through Ante Handler.
|
2022-07-18 20:16:28 +00:00
|
|
|
if leftoverGas < intrinsicGas {
|
2021-07-15 06:01:05 +00:00
|
|
|
// eth_estimateGas will check for this exact error
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(core.ErrIntrinsicGas, "apply message")
|
2021-07-15 06:01:05 +00:00
|
|
|
}
|
2022-07-18 20:16:28 +00:00
|
|
|
leftoverGas -= intrinsicGas
|
2021-06-02 08:52:53 +00:00
|
|
|
|
2022-03-06 14:09:28 +00:00
|
|
|
// access list preparation is moved from ante handler to here, because it's needed when `ApplyMessage` is called
|
2021-08-16 08:19:27 +00:00
|
|
|
// under contexts where ante handlers are not run, for example `eth_call` and `eth_estimateGas`.
|
2022-07-08 10:58:04 +00:00
|
|
|
if rules := cfg.ChainConfig.Rules(big.NewInt(ctx.BlockHeight()), cfg.ChainConfig.MergeNetsplitBlock != nil); rules.IsBerlin {
|
2022-09-15 11:54:02 +00:00
|
|
|
stateDB.PrepareAccessList(msg.From(), msg.To(), evm.ActivePrecompiles(rules), msg.AccessList())
|
2021-08-16 08:19:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:52:53 +00:00
|
|
|
if contractCreation {
|
2021-12-28 07:55:40 +00:00
|
|
|
// take over the nonce management from evm:
|
|
|
|
// - reset sender's nonce to msg.Nonce() before calling evm.
|
|
|
|
// - increase sender's nonce by one no matter the result.
|
2022-01-05 07:28:27 +00:00
|
|
|
stateDB.SetNonce(sender.Address(), msg.Nonce())
|
2022-01-04 11:02:41 +00:00
|
|
|
ret, _, leftoverGas, vmErr = evm.Create(sender, msg.Data(), leftoverGas, msg.Value())
|
2022-01-05 07:28:27 +00:00
|
|
|
stateDB.SetNonce(sender.Address(), msg.Nonce()+1)
|
2021-06-02 08:52:53 +00:00
|
|
|
} else {
|
|
|
|
ret, leftoverGas, vmErr = evm.Call(sender, *msg.To(), msg.Data(), leftoverGas, msg.Value())
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
refundQuotient := params.RefundQuotient
|
2021-08-25 14:45:51 +00:00
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
// After EIP-3529: refunds are capped to gasUsed / 5
|
|
|
|
if isLondon {
|
|
|
|
refundQuotient = params.RefundQuotientEIP3529
|
|
|
|
}
|
2021-10-04 14:58:06 +00:00
|
|
|
|
2021-10-22 17:21:03 +00:00
|
|
|
// calculate gas refund
|
|
|
|
if msg.Gas() < leftoverGas {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(types.ErrGasOverflow, "apply message")
|
2021-10-22 17:21:03 +00:00
|
|
|
}
|
2022-07-18 20:16:28 +00:00
|
|
|
// refund gas
|
2022-11-16 17:59:12 +00:00
|
|
|
temporaryGasUsed := msg.Gas() - leftoverGas
|
|
|
|
leftoverGas += GasToRefund(stateDB.GetRefund(), temporaryGasUsed, refundQuotient)
|
2021-07-15 06:01:05 +00:00
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// EVM execution error needs to be available for the JSON-RPC client
|
2021-07-15 06:01:05 +00:00
|
|
|
var vmError string
|
|
|
|
if vmErr != nil {
|
|
|
|
vmError = vmErr.Error()
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// The dirty states in `StateDB` is either committed or discarded after return
|
2021-10-22 17:21:03 +00:00
|
|
|
if commit {
|
2022-01-05 07:28:27 +00:00
|
|
|
if err := stateDB.Commit(); err != nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(err, "failed to commit stateDB")
|
2022-01-05 07:28:27 +00:00
|
|
|
}
|
2021-10-22 17:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 13:52:34 +00:00
|
|
|
// calculate a minimum amount of gas to be charged to sender if GasLimit
|
|
|
|
// is considerably higher than GasUsed to stay more aligned with Tendermint gas mechanics
|
2022-06-19 09:43:41 +00:00
|
|
|
// for more info https://github.com/evmos/ethermint/issues/1085
|
2022-05-25 13:52:34 +00:00
|
|
|
gasLimit := sdk.NewDec(int64(msg.Gas()))
|
2022-06-05 09:22:33 +00:00
|
|
|
minGasMultiplier := k.GetMinGasMultiplier(ctx)
|
|
|
|
minimumGasUsed := gasLimit.Mul(minGasMultiplier)
|
2022-07-18 20:16:28 +00:00
|
|
|
|
|
|
|
if msg.Gas() < leftoverGas {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrapf(types.ErrGasOverflow, "message gas limit < leftover gas (%d < %d)", msg.Gas(), leftoverGas)
|
2022-07-18 20:16:28 +00:00
|
|
|
}
|
2022-11-16 17:59:12 +00:00
|
|
|
|
2022-05-25 13:52:34 +00:00
|
|
|
gasUsed := sdk.MaxDec(minimumGasUsed, sdk.NewDec(int64(temporaryGasUsed))).TruncateInt().Uint64()
|
2022-07-18 20:16:28 +00:00
|
|
|
// reset leftoverGas, to be used by the tracer
|
|
|
|
leftoverGas = msg.Gas() - gasUsed
|
2022-05-25 13:52:34 +00:00
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
return &types.MsgEthereumTxResponse{
|
2022-01-04 11:02:41 +00:00
|
|
|
GasUsed: gasUsed,
|
|
|
|
VmError: vmError,
|
|
|
|
Ret: ret,
|
2022-01-05 07:28:27 +00:00
|
|
|
Logs: types.NewLogsFromEth(stateDB.Logs()),
|
|
|
|
Hash: txConfig.TxHash.Hex(),
|
2021-06-02 08:52:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|