laconicd-deprecated/importer/importer_test.go

300 lines
8.0 KiB
Go
Raw Normal View History

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
2018-10-24 14:49:37 +00:00
"github.com/cosmos/cosmos-sdk/codec"
2018-10-04 20:12:32 +00:00
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
2018-10-04 21:40:30 +00:00
"github.com/cosmos/ethermint/core"
2018-10-04 20:12:32 +00:00
"github.com/cosmos/ethermint/types"
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"
ethmisc "github.com/ethereum/go-ethereum/consensus/misc"
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"
ethparams "github.com/ethereum/go-ethereum/params"
ethrlp "github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require"
2018-10-04 20:12:32 +00:00
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
tmlog "github.com/tendermint/tendermint/libs/log"
)
var (
2018-10-04 21:40:30 +00:00
flagDataDir string
flagBlockchain string
flagCPUProfile string
2018-10-04 20:12:32 +00:00
miner501 = ethcmn.HexToAddress("0x35e8e5dC5FBd97c5b421A80B596C030a2Be2A04D")
genInvestor = ethcmn.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0")
paramsKey = sdk.NewKVStoreKey("params")
tParamsKey = sdk.NewTransientStoreKey("transient_params")
2018-10-04 20:12:32 +00:00
accKey = sdk.NewKVStoreKey("acc")
storageKey = sdk.NewKVStoreKey("storage")
codeKey = sdk.NewKVStoreKey("code")
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)")
2018-10-04 20:12:32 +00:00
flag.Parse()
}
2018-10-24 14:49:37 +00:00
func newTestCodec() *codec.Codec {
cdc := codec.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)
2018-11-28 22:19:22 +00:00
sdk.RegisterCodec(cdc)
2018-10-24 14:49:37 +00:00
codec.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)
}()
}
2018-10-24 14:49:37 +00:00
func createAndTestGenesis(t *testing.T, cms sdk.CommitMultiStore, ak auth.AccountKeeper) {
2018-10-04 20:12:32 +00:00
genBlock := ethcore.DefaultGenesisBlock()
ms := cms.CacheMultiStore()
ctx := sdk.NewContext(ms, abci.Header{}, false, logger)
2018-10-24 14:49:37 +00:00
stateDB, err := evmtypes.NewCommitStateDB(ctx, ak, storageKey, codeKey)
2018-10-04 20:12:32 +00:00
require.NoError(t, err, "failed to create a StateDB instance")
// 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]
stateDB.AddBalance(addr, acc.Balance)
stateDB.SetCode(addr, acc.Code)
stateDB.SetNonce(addr, acc.Nonce)
for key, value := range acc.Storage {
stateDB.SetState(addr, key, value)
}
}
// get balance of one of the genesis account having 200 ETH
b := stateDB.GetBalance(genInvestor)
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)
_, err = stateDB.Commit(false)
require.NoError(t, err)
// persist multi-store cache state
ms.Write()
// persist multi-store root state
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)
2018-10-24 13:46:36 +00:00
require.Equal(t, sdk.NewIntFromBigInt(b), genAcc.GetCoins().AmountOf(types.DenomDefault))
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")
}
db := dbm.NewDB("state", dbm.LevelDBBackend, flagDataDir)
2018-10-24 12:30:52 +00:00
defer cleanup()
trapSignals()
2018-10-04 20:12:32 +00:00
// create logger, codec and root multi-store
cdc := newTestCodec()
cms := store.NewCommitMultiStore(db)
ak := auth.NewAccountKeeper(cdc, accKey, types.ProtoBaseAccount)
2018-10-04 20:12:32 +00:00
// mount stores
keys := []*sdk.KVStoreKey{accKey, storageKey, codeKey}
for _, key := range keys {
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, nil)
}
2018-10-04 21:51:19 +00:00
cms.SetPruning(sdk.PruneNothing)
2018-10-04 20:12:32 +00:00
// load latest version (root)
err := cms.LoadLatestVersion()
require.NoError(t, err)
// set and test genesis block
2018-11-28 22:19:22 +00:00
createAndTestGenesis(t, cms, ak)
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)
defer blockchainInput.Close()
// 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-11-28 22:19:22 +00:00
stateDB := createStateDB(t, ctx, ak)
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 {
ethmisc.ApplyDAOHardFork(stateDB)
}
2018-10-04 21:40:30 +00:00
for i, tx := range block.Transactions() {
stateDB.Prepare(tx.Hash(), block.Hash(), i)
_, _, err = ethcore.ApplyTransaction(
chainConfig, chainContext, nil, gp, stateDB, header, tx, usedGas, vmConfig,
)
2018-10-04 21:51:19 +00:00
require.NoError(t, err, "failed to apply tx at block %d; tx: %X", block.NumberU64(), tx.Hash())
2018-10-04 21:40:30 +00:00
}
2018-10-23 02:45:57 +00:00
// apply mining rewards
accumulateRewards(chainConfig, stateDB, header, block.Uncles())
// commit stateDB
_, err := stateDB.Commit(chainConfig.IsEIP158(block.Number()))
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
2018-10-24 14:49:37 +00:00
func createStateDB(t *testing.T, ctx sdk.Context, ak auth.AccountKeeper) *evmtypes.CommitStateDB {
stateDB, err := evmtypes.NewCommitStateDB(ctx, ak, storageKey, codeKey)
2018-10-23 02:45:57 +00:00
require.NoError(t, err, "failed to create a StateDB instance")
return stateDB
}
// 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(
2018-10-24 13:46:36 +00:00
config *ethparams.ChainConfig, stateDB *evmtypes.CommitStateDB,
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)
stateDB.AddBalance(uncle.Coinbase, r)
r.Div(blockReward, rewardBig32)
reward.Add(reward, r)
}
stateDB.AddBalance(header.Coinbase, reward)
}