2018-10-04 20:12:32 +00:00
|
|
|
package importer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-10-04 21:40:30 +00:00
|
|
|
"io"
|
2018-10-23 02:45:57 +00:00
|
|
|
"math/big"
|
2018-10-04 20:12:32 +00:00
|
|
|
"os"
|
2018-10-24 12:30:52 +00:00
|
|
|
"os/signal"
|
2018-10-04 21:40:30 +00:00
|
|
|
"runtime/pprof"
|
2018-10-04 20:12:32 +00:00
|
|
|
"sort"
|
2018-10-24 12:30:52 +00:00
|
|
|
"syscall"
|
2018-10-04 20:12:32 +00:00
|
|
|
"testing"
|
2018-10-04 21:40:30 +00:00
|
|
|
"time"
|
2018-10-04 20:12:32 +00:00
|
|
|
|
2020-07-02 15:19:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
sdkcodec "github.com/cosmos/cosmos-sdk/codec"
|
2018-10-04 20:12:32 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
2019-07-04 19:46:54 +00:00
|
|
|
sdkstore "github.com/cosmos/cosmos-sdk/store/types"
|
2018-10-04 20:12:32 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2020-04-22 19:26:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2019-07-04 19:46:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2018-10-04 20:12:32 +00:00
|
|
|
|
2018-10-04 21:40:30 +00:00
|
|
|
"github.com/cosmos/ethermint/core"
|
2020-10-06 18:57:55 +00:00
|
|
|
cryptocodec "github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
2018-10-04 20:12:32 +00:00
|
|
|
"github.com/cosmos/ethermint/types"
|
2020-09-02 19:41:05 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm"
|
2018-10-24 13:46:36 +00:00
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
2018-10-04 20:12:32 +00:00
|
|
|
|
2018-10-04 21:40:30 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2018-10-23 02:45:57 +00:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2018-10-04 21:40:30 +00:00
|
|
|
ethcore "github.com/ethereum/go-ethereum/core"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
ethvm "github.com/ethereum/go-ethereum/core/vm"
|
2019-07-11 18:05:34 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2018-10-04 21:40:30 +00:00
|
|
|
ethparams "github.com/ethereum/go-ethereum/params"
|
|
|
|
ethrlp "github.com/ethereum/go-ethereum/rlp"
|
|
|
|
|
2018-10-04 20:12:32 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
tmlog "github.com/tendermint/tendermint/libs/log"
|
2019-09-27 14:08:45 +00:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
2018-10-04 20:12:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-04 21:40:30 +00:00
|
|
|
flagDataDir string
|
|
|
|
flagBlockchain string
|
|
|
|
flagCPUProfile string
|
2018-10-04 20:12:32 +00:00
|
|
|
|
|
|
|
genInvestor = ethcmn.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0")
|
|
|
|
|
|
|
|
logger = tmlog.NewNopLogger()
|
2018-10-23 02:45:57 +00:00
|
|
|
|
|
|
|
rewardBig8 = big.NewInt(8)
|
|
|
|
rewardBig32 = big.NewInt(32)
|
2018-10-04 20:12:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-10-04 21:40:30 +00:00
|
|
|
flag.StringVar(&flagCPUProfile, "cpu-profile", "", "write CPU profile")
|
|
|
|
flag.StringVar(&flagDataDir, "datadir", "", "test data directory for state storage")
|
2018-10-23 15:09:03 +00:00
|
|
|
flag.StringVar(&flagBlockchain, "blockchain", "blockchain", "ethereum block export file (blocks to import)")
|
2019-10-18 23:23:09 +00:00
|
|
|
testing.Init()
|
2018-10-04 20:12:32 +00:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
func newTestCodec() *sdkcodec.Codec {
|
|
|
|
cdc := sdkcodec.New()
|
2018-10-04 20:12:32 +00:00
|
|
|
|
2018-11-28 22:19:22 +00:00
|
|
|
evmtypes.RegisterCodec(cdc)
|
2018-10-24 14:49:37 +00:00
|
|
|
types.RegisterCodec(cdc)
|
|
|
|
auth.RegisterCodec(cdc)
|
2020-04-22 19:26:01 +00:00
|
|
|
bank.RegisterCodec(cdc)
|
2018-11-28 22:19:22 +00:00
|
|
|
sdk.RegisterCodec(cdc)
|
2020-10-06 18:57:55 +00:00
|
|
|
cryptocodec.RegisterCodec(cdc)
|
2020-04-22 19:26:01 +00:00
|
|
|
sdkcodec.RegisterCrypto(cdc)
|
2018-10-04 20:12:32 +00:00
|
|
|
|
2018-10-24 14:49:37 +00:00
|
|
|
return cdc
|
2018-10-04 20:12:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 12:30:52 +00:00
|
|
|
func cleanup() {
|
|
|
|
fmt.Println("cleaning up test execution...")
|
|
|
|
os.RemoveAll(flagDataDir)
|
|
|
|
|
|
|
|
if flagCPUProfile != "" {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func trapSignals() {
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-sigs
|
|
|
|
cleanup()
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
// nolint: interfacer
|
2021-01-07 11:55:01 +00:00
|
|
|
func createAndTestGenesis(t *testing.T, cms sdk.CommitMultiStore, ak auth.AccountKeeper, evmKeeper *evm.Keeper) {
|
2018-10-04 20:12:32 +00:00
|
|
|
genBlock := ethcore.DefaultGenesisBlock()
|
|
|
|
ms := cms.CacheMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{}, false, logger)
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
// Set the default Ethermint parameters to the parameter keeper store
|
|
|
|
evmKeeper.SetParams(ctx, evmtypes.DefaultParams())
|
2018-10-04 20:12:32 +00:00
|
|
|
|
|
|
|
// sort the addresses and insertion of key/value pairs matters
|
|
|
|
genAddrs := make([]string, len(genBlock.Alloc))
|
|
|
|
i := 0
|
|
|
|
for addr := range genBlock.Alloc {
|
|
|
|
genAddrs[i] = addr.String()
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(genAddrs)
|
|
|
|
|
|
|
|
for _, addrStr := range genAddrs {
|
|
|
|
addr := ethcmn.HexToAddress(addrStr)
|
|
|
|
acc := genBlock.Alloc[addr]
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
evmKeeper.AddBalance(ctx, addr, acc.Balance)
|
|
|
|
evmKeeper.SetCode(ctx, addr, acc.Code)
|
|
|
|
evmKeeper.SetNonce(ctx, addr, acc.Nonce)
|
2018-10-04 20:12:32 +00:00
|
|
|
|
|
|
|
for key, value := range acc.Storage {
|
2020-09-02 19:41:05 +00:00
|
|
|
evmKeeper.SetState(ctx, addr, key, value)
|
2018-10-04 20:12:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
// get balance of one of the genesis account having 400 ETH
|
2020-09-02 19:41:05 +00:00
|
|
|
b := evmKeeper.GetBalance(ctx, genInvestor)
|
2018-10-04 20:12:32 +00:00
|
|
|
require.Equal(t, "200000000000000000000", b.String())
|
|
|
|
|
|
|
|
// commit the stateDB with 'false' to delete empty objects
|
|
|
|
//
|
|
|
|
// NOTE: Commit does not yet return the intra merkle root (version)
|
2020-09-02 19:41:05 +00:00
|
|
|
_, err := evmKeeper.Commit(ctx, false)
|
2018-10-04 20:12:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// persist multi-store cache state
|
|
|
|
ms.Write()
|
|
|
|
|
|
|
|
// persist multi-store root state
|
2018-12-18 16:10:04 +00:00
|
|
|
cms.Commit()
|
2018-10-04 20:12:32 +00:00
|
|
|
|
|
|
|
// verify account mapper state
|
2018-10-24 14:49:37 +00:00
|
|
|
genAcc := ak.GetAccount(ctx, sdk.AccAddress(genInvestor.Bytes()))
|
2018-10-04 20:12:32 +00:00
|
|
|
require.NotNil(t, genAcc)
|
2020-08-23 21:41:54 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
evmDenom := evmKeeper.GetParams(ctx).EvmDenom
|
|
|
|
balance := sdk.NewCoin(evmDenom, genAcc.GetCoins().AmountOf(evmDenom))
|
2020-04-22 19:26:01 +00:00
|
|
|
require.Equal(t, sdk.NewIntFromBigInt(b), balance.Amount)
|
2018-10-04 20:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestImportBlocks(t *testing.T) {
|
2018-10-04 21:40:30 +00:00
|
|
|
if flagDataDir == "" {
|
|
|
|
flagDataDir = os.TempDir()
|
2018-10-04 20:12:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 21:40:30 +00:00
|
|
|
if flagCPUProfile != "" {
|
|
|
|
f, err := os.Create(flagCPUProfile)
|
|
|
|
require.NoError(t, err, "failed to create CPU profile")
|
2018-10-04 20:12:32 +00:00
|
|
|
|
2018-10-04 21:40:30 +00:00
|
|
|
err = pprof.StartCPUProfile(f)
|
|
|
|
require.NoError(t, err, "failed to start CPU profile")
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:46:54 +00:00
|
|
|
db := dbm.NewDB("state", dbm.GoLevelDBBackend, flagDataDir)
|
2018-10-24 12:30:52 +00:00
|
|
|
defer cleanup()
|
|
|
|
trapSignals()
|
2018-10-04 20:12:32 +00:00
|
|
|
|
|
|
|
cdc := newTestCodec()
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2018-10-04 20:12:32 +00:00
|
|
|
cms := store.NewCommitMultiStore(db)
|
2019-07-04 19:46:54 +00:00
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
authStoreKey := sdk.NewKVStoreKey(auth.StoreKey)
|
|
|
|
evmStoreKey := sdk.NewKVStoreKey(evmtypes.StoreKey)
|
|
|
|
paramsStoreKey := sdk.NewKVStoreKey(params.StoreKey)
|
|
|
|
paramsTransientStoreKey := sdk.NewTransientStoreKey(params.TStoreKey)
|
2018-10-04 20:12:32 +00:00
|
|
|
|
|
|
|
// mount stores
|
2020-09-02 19:41:05 +00:00
|
|
|
keys := []*sdk.KVStoreKey{authStoreKey, evmStoreKey, paramsStoreKey}
|
2018-10-04 20:12:32 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, nil)
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
cms.MountStoreWithDB(paramsTransientStoreKey, sdk.StoreTypeTransient, nil)
|
|
|
|
|
|
|
|
paramsKeeper := params.NewKeeper(cdc, paramsStoreKey, paramsTransientStoreKey)
|
|
|
|
|
|
|
|
// Set specific subspaces
|
|
|
|
authSubspace := paramsKeeper.Subspace(auth.DefaultParamspace)
|
|
|
|
evmSubspace := paramsKeeper.Subspace(evmtypes.DefaultParamspace).WithKeyTable(evmtypes.ParamKeyTable())
|
|
|
|
ak := auth.NewAccountKeeper(cdc, authStoreKey, authSubspace, types.ProtoAccount)
|
|
|
|
evmKeeper := evm.NewKeeper(cdc, evmStoreKey, evmSubspace, ak)
|
|
|
|
|
2019-07-04 19:46:54 +00:00
|
|
|
cms.SetPruning(sdkstore.PruneNothing)
|
2018-10-04 21:51:19 +00:00
|
|
|
|
2018-10-04 20:12:32 +00:00
|
|
|
// load latest version (root)
|
|
|
|
err := cms.LoadLatestVersion()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// set and test genesis block
|
2020-09-02 19:41:05 +00:00
|
|
|
createAndTestGenesis(t, cms, ak, evmKeeper)
|
2018-10-04 20:12:32 +00:00
|
|
|
|
2018-10-04 21:40:30 +00:00
|
|
|
// open blockchain export file
|
|
|
|
blockchainInput, err := os.Open(flagBlockchain)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-07-24 18:54:25 +00:00
|
|
|
defer func() {
|
|
|
|
err := blockchainInput.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
2018-10-04 21:40:30 +00:00
|
|
|
|
|
|
|
// ethereum mainnet config
|
|
|
|
chainContext := core.NewChainContext()
|
|
|
|
vmConfig := ethvm.Config{}
|
|
|
|
chainConfig := ethparams.MainnetChainConfig
|
|
|
|
|
|
|
|
// create RLP stream for exported blocks
|
|
|
|
stream := ethrlp.NewStream(blockchainInput, 0)
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
var block ethtypes.Block
|
|
|
|
for {
|
|
|
|
err = stream.Decode(&block)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err, "failed to decode block")
|
|
|
|
|
|
|
|
var (
|
|
|
|
usedGas = new(uint64)
|
|
|
|
gp = new(ethcore.GasPool).AddGas(block.GasLimit())
|
|
|
|
)
|
|
|
|
|
|
|
|
header := block.Header()
|
|
|
|
chainContext.Coinbase = header.Coinbase
|
|
|
|
|
|
|
|
chainContext.SetHeader(block.NumberU64(), header)
|
|
|
|
|
|
|
|
// Create a cached-wrapped multi-store based on the commit multi-store and
|
|
|
|
// create a new context based off of that.
|
|
|
|
ms := cms.CacheMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{}, false, logger)
|
2018-10-23 02:45:57 +00:00
|
|
|
ctx = ctx.WithBlockHeight(int64(block.NumberU64()))
|
2018-10-04 21:40:30 +00:00
|
|
|
|
2018-10-23 02:45:57 +00:00
|
|
|
if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
|
2020-09-02 19:41:05 +00:00
|
|
|
applyDAOHardFork(evmKeeper)
|
2018-10-23 02:45:57 +00:00
|
|
|
}
|
2018-10-04 21:40:30 +00:00
|
|
|
|
|
|
|
for i, tx := range block.Transactions() {
|
2021-03-19 03:04:29 +00:00
|
|
|
evmKeeper.Prepare(ctx, tx.Hash(), i)
|
|
|
|
evmKeeper.CommitStateDB.SetBlockHash(block.Hash())
|
2018-10-04 21:40:30 +00:00
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
receipt, gas, err := applyTransaction(
|
2020-09-02 19:41:05 +00:00
|
|
|
chainConfig, chainContext, nil, gp, evmKeeper, header, tx, usedGas, vmConfig,
|
2018-10-04 21:40:30 +00:00
|
|
|
)
|
2020-04-22 19:26:01 +00:00
|
|
|
require.NoError(t, err, "failed to apply tx at block %d; tx: %X; gas %d; receipt:%v", block.NumberU64(), tx.Hash(), gas, receipt)
|
|
|
|
require.NotNil(t, receipt)
|
2018-10-04 21:40:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:45:57 +00:00
|
|
|
// apply mining rewards
|
2020-09-02 19:41:05 +00:00
|
|
|
accumulateRewards(chainConfig, evmKeeper, header, block.Uncles())
|
2018-10-23 02:45:57 +00:00
|
|
|
|
|
|
|
// commit stateDB
|
2020-09-02 19:41:05 +00:00
|
|
|
_, err := evmKeeper.CommitStateDB.Commit(chainConfig.IsEIP158(block.Number()))
|
2018-10-23 02:45:57 +00:00
|
|
|
require.NoError(t, err, "failed to commit StateDB")
|
|
|
|
|
|
|
|
// simulate BaseApp EndBlocker commitment
|
2018-10-04 21:40:30 +00:00
|
|
|
ms.Write()
|
|
|
|
cms.Commit()
|
|
|
|
|
2018-10-23 02:45:57 +00:00
|
|
|
// block debugging output
|
2018-10-04 21:40:30 +00:00
|
|
|
if block.NumberU64() > 0 && block.NumberU64()%1000 == 0 {
|
|
|
|
fmt.Printf("processed block: %d (time so far: %v)\n", block.NumberU64(), time.Since(startTime))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 02:45:57 +00:00
|
|
|
|
|
|
|
// accumulateRewards credits the coinbase of the given block with the mining
|
|
|
|
// reward. The total reward consists of the static block reward and rewards for
|
|
|
|
// included uncles. The coinbase of each uncle block is also rewarded.
|
|
|
|
func accumulateRewards(
|
2021-01-07 11:55:01 +00:00
|
|
|
config *ethparams.ChainConfig, evmKeeper *evm.Keeper,
|
2018-10-23 02:45:57 +00:00
|
|
|
header *ethtypes.Header, uncles []*ethtypes.Header,
|
|
|
|
) {
|
|
|
|
|
|
|
|
// select the correct block reward based on chain progression
|
|
|
|
blockReward := ethash.FrontierBlockReward
|
|
|
|
if config.IsByzantium(header.Number) {
|
|
|
|
blockReward = ethash.ByzantiumBlockReward
|
|
|
|
}
|
|
|
|
|
|
|
|
// accumulate the rewards for the miner and any included uncles
|
|
|
|
reward := new(big.Int).Set(blockReward)
|
|
|
|
r := new(big.Int)
|
|
|
|
|
|
|
|
for _, uncle := range uncles {
|
|
|
|
r.Add(uncle.Number, rewardBig8)
|
|
|
|
r.Sub(r, header.Number)
|
|
|
|
r.Mul(r, blockReward)
|
|
|
|
r.Div(r, rewardBig8)
|
2020-09-02 19:41:05 +00:00
|
|
|
evmKeeper.CommitStateDB.AddBalance(uncle.Coinbase, r)
|
2018-10-23 02:45:57 +00:00
|
|
|
r.Div(blockReward, rewardBig32)
|
|
|
|
reward.Add(reward, r)
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
evmKeeper.CommitStateDB.AddBalance(header.Coinbase, reward)
|
2018-10-23 02:45:57 +00:00
|
|
|
}
|
2019-07-11 18:05:34 +00:00
|
|
|
|
|
|
|
// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
|
|
|
|
// rules, transferring all balances of a set of DAO accounts to a single refund
|
|
|
|
// contract.
|
2019-07-24 22:14:12 +00:00
|
|
|
// Code is pulled from go-ethereum 1.9 because the StateDB interface does not include the
|
2019-07-11 18:05:34 +00:00
|
|
|
// SetBalance function implementation
|
|
|
|
// Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/consensus/misc/dao.go#L74
|
2021-01-07 11:55:01 +00:00
|
|
|
func applyDAOHardFork(evmKeeper *evm.Keeper) {
|
2019-07-11 18:05:34 +00:00
|
|
|
// Retrieve the contract to refund balances into
|
2020-09-02 19:41:05 +00:00
|
|
|
if !evmKeeper.CommitStateDB.Exist(ethparams.DAORefundContract) {
|
|
|
|
evmKeeper.CommitStateDB.CreateAccount(ethparams.DAORefundContract)
|
2019-07-11 18:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move every DAO account and extra-balance account funds into the refund contract
|
|
|
|
for _, addr := range ethparams.DAODrainList() {
|
2020-09-02 19:41:05 +00:00
|
|
|
evmKeeper.CommitStateDB.AddBalance(ethparams.DAORefundContract, evmKeeper.CommitStateDB.GetBalance(addr))
|
|
|
|
evmKeeper.CommitStateDB.SetBalance(addr, new(big.Int))
|
2019-07-11 18:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyTransaction attempts to apply a transaction to the given state database
|
|
|
|
// and uses the input parameters for its environment. It returns the receipt
|
|
|
|
// for the transaction, gas used and an error if the transaction failed,
|
|
|
|
// indicating the block was invalid.
|
2019-09-27 14:08:45 +00:00
|
|
|
// Function is also pulled from go-ethereum 1.9 because of the incompatible usage
|
2019-07-11 18:05:34 +00:00
|
|
|
// Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/core/state_processor.go#L88
|
2020-04-22 19:26:01 +00:00
|
|
|
func applyTransaction(
|
|
|
|
config *ethparams.ChainConfig, bc ethcore.ChainContext, author *ethcmn.Address,
|
2021-01-07 11:55:01 +00:00
|
|
|
gp *ethcore.GasPool, evmKeeper *evm.Keeper, header *ethtypes.Header,
|
2020-04-22 19:26:01 +00:00
|
|
|
tx *ethtypes.Transaction, usedGas *uint64, cfg ethvm.Config,
|
|
|
|
) (*ethtypes.Receipt, uint64, error) {
|
2019-07-11 18:05:34 +00:00
|
|
|
msg, err := tx.AsMessage(ethtypes.MakeSigner(config, header.Number))
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2019-07-11 18:05:34 +00:00
|
|
|
// Create a new context to be used in the EVM environment
|
2020-12-11 12:47:18 +00:00
|
|
|
blockCtx := ethcore.NewEVMBlockContext(header, bc, author)
|
|
|
|
txCtx := ethcore.NewEVMTxContext(msg)
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2019-07-11 18:05:34 +00:00
|
|
|
// Create a new environment which holds all relevant information
|
|
|
|
// about the transaction and calling mechanisms.
|
2020-12-11 12:47:18 +00:00
|
|
|
vmenv := ethvm.NewEVM(blockCtx, txCtx, evmKeeper.CommitStateDB, config, cfg)
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2019-07-11 18:05:34 +00:00
|
|
|
// Apply the transaction to the current state (included in the env)
|
2020-05-18 22:55:51 +00:00
|
|
|
execResult, err := ethcore.ApplyMessage(vmenv, msg, gp)
|
2019-07-11 18:05:34 +00:00
|
|
|
if err != nil {
|
2020-07-27 19:33:16 +00:00
|
|
|
// NOTE: ignore vm execution error (eg: tx out of gas at block 51169) as we care only about state transition errors
|
|
|
|
return ðtypes.Receipt{}, 0, nil
|
2019-07-11 18:05:34 +00:00
|
|
|
}
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2019-07-11 18:05:34 +00:00
|
|
|
// Update the state with pending changes
|
2020-04-01 18:49:21 +00:00
|
|
|
var intRoot ethcmn.Hash
|
2019-07-11 18:05:34 +00:00
|
|
|
if config.IsByzantium(header.Number) {
|
2020-09-02 19:41:05 +00:00
|
|
|
err = evmKeeper.CommitStateDB.Finalise(true)
|
2019-07-11 18:05:34 +00:00
|
|
|
} else {
|
2020-09-02 19:41:05 +00:00
|
|
|
intRoot, err = evmKeeper.CommitStateDB.IntermediateRoot(config.IsEIP158(header.Number))
|
2019-07-11 18:05:34 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-18 22:55:51 +00:00
|
|
|
return nil, execResult.UsedGas, err
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
root := intRoot.Bytes()
|
2020-05-18 22:55:51 +00:00
|
|
|
*usedGas += execResult.UsedGas
|
2019-07-11 18:05:34 +00:00
|
|
|
|
|
|
|
// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
|
|
|
|
// based on the eip phase, we're passing whether the root touch-delete accounts.
|
2020-05-18 22:55:51 +00:00
|
|
|
receipt := ethtypes.NewReceipt(root, execResult.Failed(), *usedGas)
|
2019-07-11 18:05:34 +00:00
|
|
|
receipt.TxHash = tx.Hash()
|
2020-05-18 22:55:51 +00:00
|
|
|
receipt.GasUsed = execResult.UsedGas
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2019-07-11 18:05:34 +00:00
|
|
|
// if the transaction created a contract, store the creation address in the receipt.
|
|
|
|
if msg.To() == nil {
|
2020-12-11 12:47:18 +00:00
|
|
|
receipt.ContractAddress = ethcrypto.CreateAddress(vmenv.TxContext.Origin, tx.Nonce())
|
2019-07-11 18:05:34 +00:00
|
|
|
}
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2019-07-11 18:05:34 +00:00
|
|
|
// Set the receipt logs and create a bloom for filtering
|
2020-09-02 19:41:05 +00:00
|
|
|
receipt.Logs, err = evmKeeper.CommitStateDB.GetLogs(tx.Hash())
|
2019-07-11 18:05:34 +00:00
|
|
|
receipt.Bloom = ethtypes.CreateBloom(ethtypes.Receipts{receipt})
|
2020-09-02 19:41:05 +00:00
|
|
|
receipt.BlockHash = evmKeeper.CommitStateDB.BlockHash()
|
2019-07-11 18:05:34 +00:00
|
|
|
receipt.BlockNumber = header.Number
|
2020-09-02 19:41:05 +00:00
|
|
|
receipt.TransactionIndex = uint(evmKeeper.CommitStateDB.TxIndex())
|
2019-07-11 18:05:34 +00:00
|
|
|
|
2020-05-18 22:55:51 +00:00
|
|
|
return receipt, execResult.UsedGas, err
|
2019-07-11 18:05:34 +00:00
|
|
|
}
|