fix: commented out importer test (#691)
* fix: commented out importer test (fixes #659) updated the importer test as a test suite plus added the block export file from https://github.com/cosmos/ethermint/blob/development/importer/blockchain * remove a dead branch * re-added CI check * go 1.17
This commit is contained in:
parent
ac75a9a4a4
commit
1474c70719
39
.github/workflows/test.yml
vendored
39
.github/workflows/test.yml
vendored
@ -55,26 +55,25 @@ jobs:
|
|||||||
fail_ci_if_error: true
|
fail_ci_if_error: true
|
||||||
if: env.GIT_DIFF
|
if: env.GIT_DIFF
|
||||||
|
|
||||||
# TODO: refactor before enabling
|
test-importer:
|
||||||
# test-importer:
|
runs-on: ubuntu-latest
|
||||||
# runs-on: ubuntu-latest
|
timeout-minutes: 10
|
||||||
# timeout-minutes: 10
|
steps:
|
||||||
# steps:
|
- uses: actions/setup-go@v2.1.4
|
||||||
# - uses: actions/checkout@v2.3.5
|
with:
|
||||||
# - uses: actions/setup-go@v2.1.4
|
go-version: 1.17
|
||||||
# with:
|
- uses: actions/checkout@v2
|
||||||
# go-version: 1.17
|
- uses: technote-space/get-diff-action@v5
|
||||||
# - uses: technote-space/get-diff-action@v5
|
id: git_diff
|
||||||
# id: git_diff
|
with:
|
||||||
# with:
|
SUFFIX_FILTER: |
|
||||||
# SUFFIX_FILTER: |
|
.go
|
||||||
# .go
|
.mod
|
||||||
# .mod
|
.sum
|
||||||
# .sum
|
- name: test-importer
|
||||||
# - name: test-importer
|
run: |
|
||||||
# run: |
|
make test-import
|
||||||
# make test-import
|
if: "env.GIT_DIFF != ''"
|
||||||
# if: "env.GIT_DIFF != ''"
|
|
||||||
|
|
||||||
test-solidity:
|
test-solidity:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
6
Makefile
6
Makefile
@ -257,7 +257,7 @@ godocs:
|
|||||||
|
|
||||||
test: test-unit
|
test: test-unit
|
||||||
test-all: test-unit test-race
|
test-all: test-unit test-race
|
||||||
PACKAGES_UNIT=$(shell go list ./...)
|
PACKAGES_UNIT=$(shell go list ./... | grep -Ev 'vendor|importer')
|
||||||
TEST_PACKAGES=./...
|
TEST_PACKAGES=./...
|
||||||
TEST_TARGETS := test-unit test-unit-cover test-race
|
TEST_TARGETS := test-unit test-unit-cover test-race
|
||||||
|
|
||||||
@ -282,9 +282,7 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
test-import:
|
test-import:
|
||||||
@go test ./tests/importer -v --vet=off --run=TestImportBlocks --datadir tmp \
|
go test -run TestImporterTestSuite -v --vet=off github.com/tharsis/ethermint/tests/importer
|
||||||
--blockchain blockchain
|
|
||||||
rm -rf tests/importer/tmp
|
|
||||||
|
|
||||||
test-rpc:
|
test-rpc:
|
||||||
./scripts/integration-test-all.sh -t "rpc" -q 1 -z 1 -s 2 -m "rpc" -r "true"
|
./scripts/integration-test-all.sh -t "rpc" -q 1 -z 1 -s 2 -m "rpc" -r "true"
|
||||||
|
BIN
tests/importer/blockchain
Normal file
BIN
tests/importer/blockchain
Normal file
Binary file not shown.
@ -1,401 +1,273 @@
|
|||||||
package importer
|
package importer
|
||||||
|
|
||||||
// import (
|
import (
|
||||||
// "flag"
|
"flag"
|
||||||
// "fmt"
|
"fmt"
|
||||||
// "io"
|
"io"
|
||||||
// "math/big"
|
"math/big"
|
||||||
// "os"
|
"os"
|
||||||
// "os/signal"
|
"testing"
|
||||||
// "runtime/pprof"
|
"time"
|
||||||
// "sort"
|
|
||||||
// "syscall"
|
"github.com/tharsis/ethermint/app"
|
||||||
// "testing"
|
|
||||||
// "time"
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
// "github.com/google/uuid"
|
|
||||||
// "github.com/stretchr/testify/require"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
// sdkcodec "github.com/cosmos/cosmos-sdk/codec"
|
evmkeeper "github.com/tharsis/ethermint/x/evm/keeper"
|
||||||
// codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
||||||
// "github.com/cosmos/cosmos-sdk/store"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
// sdkstore "github.com/cosmos/cosmos-sdk/store/types"
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||||
// sdk "github.com/cosmos/cosmos-sdk/types"
|
ethcore "github.com/ethereum/go-ethereum/core"
|
||||||
// authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
||||||
// authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
ethvm "github.com/ethereum/go-ethereum/core/vm"
|
||||||
// bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
// banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
ethparams "github.com/ethereum/go-ethereum/params"
|
||||||
// paramkeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
|
ethrlp "github.com/ethereum/go-ethereum/rlp"
|
||||||
// paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
|
||||||
// stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
"github.com/tendermint/tendermint/abci/types"
|
||||||
// stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
"github.com/tendermint/tendermint/crypto/tmhash"
|
||||||
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||||
// "github.com/tharsis/ethermint/encoding/codec"
|
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
|
||||||
// "github.com/tharsis/ethermint/types"
|
"github.com/tendermint/tendermint/version"
|
||||||
// evmkeeper "github.com/tharsis/ethermint/x/evm/keeper"
|
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
|
||||||
// evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
)
|
||||||
|
|
||||||
// "github.com/ethereum/go-ethereum/common"
|
var (
|
||||||
// "github.com/ethereum/go-ethereum/consensus/ethash"
|
flagBlockchain string
|
||||||
// ethcore "github.com/ethereum/go-ethereum/core"
|
|
||||||
// ethtypes "github.com/ethereum/go-ethereum/core/types"
|
rewardBig8 = big.NewInt(8)
|
||||||
// ethvm "github.com/ethereum/go-ethereum/core/vm"
|
rewardBig32 = big.NewInt(32)
|
||||||
// "github.com/ethereum/go-ethereum/crypto"
|
)
|
||||||
// ethparams "github.com/ethereum/go-ethereum/params"
|
|
||||||
// ethrlp "github.com/ethereum/go-ethereum/rlp"
|
func init() {
|
||||||
|
flag.StringVar(&flagBlockchain, "blockchain", "blockchain", "ethereum block export file (blocks to import)")
|
||||||
// tmlog "github.com/tendermint/tendermint/libs/log"
|
testing.Init()
|
||||||
// tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
flag.Parse()
|
||||||
// dbm "github.com/tendermint/tm-db"
|
}
|
||||||
// )
|
|
||||||
|
type ImporterTestSuite struct {
|
||||||
// TODO: update and rewrite as testing suite with app.
|
suite.Suite
|
||||||
|
|
||||||
// var (
|
app *app.EthermintApp
|
||||||
// flagDataDir string
|
ctx sdk.Context
|
||||||
// flagBlockchain string
|
}
|
||||||
// flagCPUProfile string
|
|
||||||
|
/// DoSetupTest setup test environment, it uses`require.TestingT` to support both `testing.T` and `testing.B`.
|
||||||
// genInvestor = common.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0")
|
func (suite *ImporterTestSuite) DoSetupTest(t require.TestingT) {
|
||||||
|
checkTx := false
|
||||||
// logger = tmlog.NewNopLogger()
|
suite.app = app.Setup(checkTx, nil)
|
||||||
|
// consensus key
|
||||||
// rewardBig8 = big.NewInt(8)
|
priv, err := ethsecp256k1.GenerateKey()
|
||||||
// rewardBig32 = big.NewInt(32)
|
require.NoError(t, err)
|
||||||
// )
|
consAddress := sdk.ConsAddress(priv.PubKey().Address())
|
||||||
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
|
||||||
// func init() {
|
Height: 1,
|
||||||
// flag.StringVar(&flagCPUProfile, "cpu-profile", "", "write CPU profile")
|
ChainID: "ethermint_9000-1",
|
||||||
// flag.StringVar(&flagDataDir, "datadir", "", "test data directory for state storage")
|
Time: time.Now().UTC(),
|
||||||
// flag.StringVar(&flagBlockchain, "blockchain", "blockchain", "ethereum block export file (blocks to import)")
|
ProposerAddress: consAddress.Bytes(),
|
||||||
// testing.Init()
|
Version: tmversion.Consensus{
|
||||||
// flag.Parse()
|
Block: version.BlockProtocol,
|
||||||
// }
|
},
|
||||||
|
LastBlockId: tmproto.BlockID{
|
||||||
// func newTestCodec() (sdkcodec.BinaryMarshaler, *sdkcodec.LegacyAmino) {
|
Hash: tmhash.Sum([]byte("block_id")),
|
||||||
// interfaceRegistry := codectypes.NewInterfaceRegistry()
|
PartSetHeader: tmproto.PartSetHeader{
|
||||||
// cdc := sdkcodec.NewProtoCodec(interfaceRegistry)
|
Total: 11,
|
||||||
// amino := sdkcodec.NewLegacyAmino()
|
Hash: tmhash.Sum([]byte("partset_header")),
|
||||||
|
},
|
||||||
// sdk.RegisterLegacyAminoCodec(amino)
|
},
|
||||||
|
AppHash: tmhash.Sum([]byte("app")),
|
||||||
// codec.RegisterInterfaces(interfaceRegistry)
|
DataHash: tmhash.Sum([]byte("data")),
|
||||||
|
EvidenceHash: tmhash.Sum([]byte("evidence")),
|
||||||
// return cdc, amino
|
ValidatorsHash: tmhash.Sum([]byte("validators")),
|
||||||
// }
|
NextValidatorsHash: tmhash.Sum([]byte("next_validators")),
|
||||||
|
ConsensusHash: tmhash.Sum([]byte("consensus")),
|
||||||
// func cleanup() {
|
LastResultsHash: tmhash.Sum([]byte("last_result")),
|
||||||
// fmt.Println("cleaning up test execution...")
|
})
|
||||||
// os.RemoveAll(flagDataDir)
|
}
|
||||||
|
|
||||||
// if flagCPUProfile != "" {
|
func (suite *ImporterTestSuite) SetupTest() {
|
||||||
// pprof.StopCPUProfile()
|
suite.DoSetupTest(suite.T())
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
func TestImporterTestSuite(t *testing.T) {
|
||||||
// func trapSignals() {
|
suite.Run(t, new(ImporterTestSuite))
|
||||||
// sigs := make(chan os.Signal, 1)
|
}
|
||||||
// signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
|
func (suite *ImporterTestSuite) TestImportBlocks() {
|
||||||
// go func() {
|
chainContext := NewChainContext()
|
||||||
// <-sigs
|
chainConfig := ethparams.MainnetChainConfig
|
||||||
// cleanup()
|
vmConfig := ethvm.Config{}
|
||||||
// os.Exit(1)
|
|
||||||
// }()
|
// open blockchain export file
|
||||||
// }
|
blockchainInput, err := os.Open(flagBlockchain)
|
||||||
|
suite.Require().Nil(err)
|
||||||
//
|
|
||||||
// func createAndTestGenesis(t *testing.T, cms sdk.CommitMultiStore, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, evmKeeper *evmkeeper.Keeper) {
|
defer func() {
|
||||||
// genBlock := ethcore.DefaultGenesisBlock()
|
err := blockchainInput.Close()
|
||||||
// ms := cms.CacheMultiStore()
|
suite.Require().NoError(err)
|
||||||
// ctx := sdk.NewContext(ms, tmproto.Header{}, false, logger)
|
}()
|
||||||
// evmKeeper.WithContext(ctx)
|
|
||||||
|
stream := ethrlp.NewStream(blockchainInput, 0)
|
||||||
// // Set the default Ethermint parameters to the parameter keeper store
|
startTime := time.Now()
|
||||||
// evmKeeper.SetParams(ctx, evmtypes.DefaultParams())
|
|
||||||
|
var block ethtypes.Block
|
||||||
// // sort the addresses and insertion of key/value pairs matters
|
|
||||||
// genAddrs := make([]string, len(genBlock.Alloc))
|
for {
|
||||||
// i := 0
|
err := stream.Decode(&block)
|
||||||
// for addr := range genBlock.Alloc {
|
if err == io.EOF {
|
||||||
// genAddrs[i] = addr.String()
|
break
|
||||||
// i++
|
}
|
||||||
// }
|
|
||||||
|
suite.Require().NoError(err, "failed to decode block")
|
||||||
// sort.Strings(genAddrs)
|
|
||||||
|
var (
|
||||||
// for _, addrStr := range genAddrs {
|
usedGas = new(uint64)
|
||||||
// addr := common.HexToAddress(addrStr)
|
gp = new(ethcore.GasPool).AddGas(block.GasLimit())
|
||||||
// acc := genBlock.Alloc[addr]
|
)
|
||||||
|
header := block.Header()
|
||||||
// evmKeeper.AddBalance(addr, acc.Balance)
|
chainContext.Coinbase = header.Coinbase
|
||||||
// evmKeeper.SetCode(addr, acc.Code)
|
|
||||||
// evmKeeper.SetNonce(addr, acc.Nonce)
|
chainContext.SetHeader(block.NumberU64(), header)
|
||||||
|
tmheader := suite.ctx.BlockHeader()
|
||||||
// for key, value := range acc.Storage {
|
// fix due to that begin block can't have height 0
|
||||||
// evmKeeper.SetState(addr, key, value)
|
tmheader.Height = int64(block.NumberU64()) + 1
|
||||||
// }
|
suite.app.BeginBlock(types.RequestBeginBlock{
|
||||||
// }
|
Header: tmheader,
|
||||||
|
})
|
||||||
// // get balance of one of the genesis account having 400 ETH
|
ctx := suite.app.NewContext(false, tmheader)
|
||||||
// b := evmKeeper.GetBalance(genInvestor)
|
ctx = ctx.WithBlockHeight(tmheader.Height)
|
||||||
// require.Equal(t, "200000000000000000000", b.String())
|
suite.app.EvmKeeper.WithContext(ctx)
|
||||||
|
|
||||||
// // persist multi-store cache state
|
if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
|
||||||
// ms.Write()
|
applyDAOHardFork(suite.app.EvmKeeper)
|
||||||
|
}
|
||||||
// // persist multi-store root state
|
|
||||||
// cms.Commit()
|
for _, tx := range block.Transactions() {
|
||||||
|
|
||||||
// // verify account mapper state
|
receipt, gas, err := applyTransaction(
|
||||||
// genAcc := ak.GetAccount(ctx, sdk.AccAddress(genInvestor.Bytes()))
|
chainConfig, chainContext, nil, gp, suite.app.EvmKeeper, header, tx, usedGas, vmConfig,
|
||||||
// require.NotNil(t, genAcc)
|
)
|
||||||
|
suite.Require().NoError(err, "failed to apply tx at block %d; tx: %X; gas %d; receipt:%v", block.NumberU64(), tx.Hash(), gas, receipt)
|
||||||
// evmDenom := evmKeeper.GetParams(ctx).EvmDenom
|
suite.Require().NotNil(receipt)
|
||||||
// balance := bk.GetBalance(ctx, genAcc.GetAddress(), evmDenom)
|
}
|
||||||
// require.Equal(t, sdk.NewIntFromBigInt(b), balance.Amount)
|
|
||||||
// }
|
// apply mining rewards
|
||||||
|
accumulateRewards(chainConfig, suite.app.EvmKeeper, header, block.Uncles())
|
||||||
// func TestImportBlocks(t *testing.T) {
|
|
||||||
// if flagDataDir == "" {
|
// simulate BaseApp EndBlocker commitment
|
||||||
// flagDataDir = os.TempDir()
|
endBR := types.RequestEndBlock{Height: tmheader.Height}
|
||||||
// }
|
suite.app.EndBlocker(ctx, endBR)
|
||||||
|
suite.app.Commit()
|
||||||
// if flagCPUProfile != "" {
|
|
||||||
// f, err := os.Create(flagCPUProfile)
|
// block debugging output
|
||||||
// require.NoError(t, err, "failed to create CPU profile")
|
if block.NumberU64() > 0 && block.NumberU64()%1000 == 0 {
|
||||||
|
fmt.Printf("processed block: %d (time so far: %v)\n", block.NumberU64(), time.Since(startTime))
|
||||||
// err = pprof.StartCPUProfile(f)
|
}
|
||||||
// require.NoError(t, err, "failed to start CPU profile")
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// db, err := dbm.NewDB("state_test"+uuid.New().String(), dbm.GoLevelDBBackend, flagDataDir)
|
// accumulateRewards credits the coinbase of the given block with the mining
|
||||||
// require.NoError(t, err)
|
// reward. The total reward consists of the static block reward and rewards for
|
||||||
|
// included uncles. The coinbase of each uncle block is also rewarded.
|
||||||
// defer cleanup()
|
func accumulateRewards(
|
||||||
// trapSignals()
|
config *ethparams.ChainConfig, evmKeeper *evmkeeper.Keeper,
|
||||||
|
header *ethtypes.Header, uncles []*ethtypes.Header,
|
||||||
// cdc, amino := newTestCodec()
|
) {
|
||||||
|
// select the correct block reward based on chain progression
|
||||||
// cms := store.NewCommitMultiStore(db)
|
blockReward := ethash.FrontierBlockReward
|
||||||
|
if config.IsByzantium(header.Number) {
|
||||||
// authStoreKey := sdk.NewKVStoreKey(authtypes.StoreKey)
|
blockReward = ethash.ByzantiumBlockReward
|
||||||
// bankStoreKey := sdk.NewKVStoreKey(banktypes.StoreKey)
|
}
|
||||||
// stakingStoreKey := sdk.NewKVStoreKey(stakingtypes.StoreKey)
|
|
||||||
// evmStoreKey := sdk.NewKVStoreKey(evmtypes.StoreKey)
|
// accumulate the rewards for the miner and any included uncles
|
||||||
// paramsStoreKey := sdk.NewKVStoreKey(paramtypes.StoreKey)
|
reward := new(big.Int).Set(blockReward)
|
||||||
// evmTransientStoreKey := sdk.NewTransientStoreKey(evmtypes.TransientKey)
|
r := new(big.Int)
|
||||||
// paramsTransientStoreKey := sdk.NewTransientStoreKey(paramtypes.TStoreKey)
|
|
||||||
|
for _, uncle := range uncles {
|
||||||
// // mount stores
|
r.Add(uncle.Number, rewardBig8)
|
||||||
// keys := []*sdk.KVStoreKey{authStoreKey, bankStoreKey, stakingStoreKey, evmStoreKey, paramsStoreKey}
|
r.Sub(r, header.Number)
|
||||||
// tkeys := []*sdk.TransientStoreKey{paramsTransientStoreKey, evmTransientStoreKey}
|
r.Mul(r, blockReward)
|
||||||
// for _, key := range keys {
|
r.Div(r, rewardBig8)
|
||||||
// cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, nil)
|
evmKeeper.AddBalance(uncle.Coinbase, r)
|
||||||
// }
|
r.Div(blockReward, rewardBig32)
|
||||||
|
reward.Add(reward, r)
|
||||||
// for _, tkey := range tkeys {
|
}
|
||||||
// cms.MountStoreWithDB(tkey, sdk.StoreTypeTransient, nil)
|
|
||||||
// }
|
evmKeeper.AddBalance(header.Coinbase, reward)
|
||||||
|
}
|
||||||
// paramsKeeper := paramkeeper.NewKeeper(cdc, amino, paramsStoreKey, paramsTransientStoreKey)
|
|
||||||
|
// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
|
||||||
// // Set specific subspaces
|
// rules, transferring all balances of a set of DAO accounts to a single refund
|
||||||
// authSubspace := paramsKeeper.Subspace(authtypes.ModuleName)
|
// contract.
|
||||||
// bankSubspace := paramsKeeper.Subspace(banktypes.ModuleName)
|
// Code is pulled from go-ethereum 1.9 because the StateDB interface does not include the
|
||||||
// stakingSubspace := paramsKeeper.Subspace(stakingtypes.ModuleName)
|
// SetBalance function implementation
|
||||||
// evmSubspace := paramsKeeper.Subspace(evmtypes.ModuleName).WithKeyTable(evmtypes.ParamKeyTable())
|
// Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/consensus/misc/dao.go#L74
|
||||||
|
func applyDAOHardFork(evmKeeper *evmkeeper.Keeper) {
|
||||||
// // create keepers
|
// Retrieve the contract to refund balances into
|
||||||
// ak := authkeeper.NewAccountKeeper(cdc, authStoreKey, authSubspace, types.ProtoAccount, nil)
|
if !evmKeeper.Exist(ethparams.DAORefundContract) {
|
||||||
// bk := bankkeeper.NewBaseKeeper(cdc, bankStoreKey, ak, bankSubspace, nil)
|
evmKeeper.CreateAccount(ethparams.DAORefundContract)
|
||||||
// sk := stakingkeeper.NewKeeper(cdc, stakingStoreKey, ak, bk, stakingSubspace)
|
}
|
||||||
// evmKeeper := evmkeeper.NewKeeper(cdc, evmStoreKey, evmTransientStoreKey, evmSubspace, ak, bk, sk)
|
|
||||||
|
// Move every DAO account and extra-balance account funds into the refund contract
|
||||||
// cms.SetPruning(sdkstore.PruneNothing)
|
for _, addr := range ethparams.DAODrainList() {
|
||||||
|
evmKeeper.AddBalance(ethparams.DAORefundContract, evmKeeper.GetBalance(addr))
|
||||||
// // load latest version (root)
|
}
|
||||||
// err = cms.LoadLatestVersion()
|
}
|
||||||
// require.NoError(t, err)
|
|
||||||
|
// ApplyTransaction attempts to apply a transaction to the given state database
|
||||||
// // set and test genesis block
|
// and uses the input parameters for its environment. It returns the receipt
|
||||||
// createAndTestGenesis(t, cms, ak, bk, evmKeeper)
|
// for the transaction, gas used and an error if the transaction failed,
|
||||||
|
// indicating the block was invalid.
|
||||||
// // open blockchain export file
|
// Function is also pulled from go-ethereum 1.9 because of the incompatible usage
|
||||||
// blockchainInput, err := os.Open(flagBlockchain)
|
// Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/core/state_processor.go#L88
|
||||||
// require.Nil(t, err)
|
func applyTransaction(
|
||||||
|
config *ethparams.ChainConfig, bc ethcore.ChainContext, author *common.Address,
|
||||||
// defer func() {
|
gp *ethcore.GasPool, evmKeeper *evmkeeper.Keeper, header *ethtypes.Header,
|
||||||
// err := blockchainInput.Close()
|
tx *ethtypes.Transaction, usedGas *uint64, cfg ethvm.Config,
|
||||||
// require.NoError(t, err)
|
) (*ethtypes.Receipt, uint64, error) {
|
||||||
// }()
|
msg, err := tx.AsMessage(ethtypes.MakeSigner(config, header.Number), sdk.ZeroInt().BigInt())
|
||||||
|
if err != nil {
|
||||||
// // ethereum mainnet config
|
return nil, 0, err
|
||||||
// chainContext := NewChainContext()
|
}
|
||||||
// vmConfig := ethvm.Config{}
|
|
||||||
// chainConfig := ethparams.MainnetChainConfig
|
// Create a new context to be used in the EVM environment
|
||||||
|
blockCtx := ethcore.NewEVMBlockContext(header, bc, author)
|
||||||
// // create RLP stream for exported blocks
|
txCtx := ethcore.NewEVMTxContext(msg)
|
||||||
// stream := ethrlp.NewStream(blockchainInput, 0)
|
|
||||||
// startTime := time.Now()
|
// Create a new environment which holds all relevant information
|
||||||
|
// about the transaction and calling mechanisms.
|
||||||
// var block ethtypes.Block
|
vmenv := ethvm.NewEVM(blockCtx, txCtx, evmKeeper, config, cfg)
|
||||||
// for {
|
|
||||||
// err = stream.Decode(&block)
|
// Apply the transaction to the current state (included in the env)
|
||||||
// if err == io.EOF {
|
execResult, err := ethcore.ApplyMessage(vmenv, msg, gp)
|
||||||
// break
|
if err != nil {
|
||||||
// }
|
// 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
|
||||||
// require.NoError(t, err, "failed to decode block")
|
}
|
||||||
|
|
||||||
// var (
|
root := common.Hash{}.Bytes()
|
||||||
// usedGas = new(uint64)
|
*usedGas += execResult.UsedGas
|
||||||
// gp = new(ethcore.GasPool).AddGas(block.GasLimit())
|
|
||||||
// )
|
// 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.
|
||||||
// header := block.Header()
|
receipt := ethtypes.NewReceipt(root, execResult.Failed(), *usedGas)
|
||||||
// chainContext.Coinbase = header.Coinbase
|
receipt.TxHash = tx.Hash()
|
||||||
|
receipt.GasUsed = execResult.UsedGas
|
||||||
// chainContext.SetHeader(block.NumberU64(), header)
|
|
||||||
|
// if the transaction created a contract, store the creation address in the receipt.
|
||||||
// // Create a cached-wrapped multi-store based on the commit multi-store and
|
if msg.To() == nil {
|
||||||
// // create a new context based off of that.
|
receipt.ContractAddress = crypto.CreateAddress(vmenv.TxContext.Origin, tx.Nonce())
|
||||||
// ms := cms.CacheMultiStore()
|
}
|
||||||
// ctx := sdk.NewContext(ms, tmproto.Header{}, false, logger)
|
|
||||||
// ctx = ctx.WithBlockHeight(int64(block.NumberU64()))
|
// Set the receipt logs and create a bloom for filtering
|
||||||
// evmKeeper.WithContext(ctx)
|
receipt.Logs = evmKeeper.GetTxLogsTransient(tx.Hash())
|
||||||
|
receipt.Bloom = ethtypes.CreateBloom(ethtypes.Receipts{receipt})
|
||||||
// if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
|
receipt.BlockHash = header.Hash()
|
||||||
// applyDAOHardFork(evmKeeper)
|
receipt.BlockNumber = header.Number
|
||||||
// }
|
receipt.TransactionIndex = uint(evmKeeper.GetTxIndexTransient())
|
||||||
|
|
||||||
// for _, tx := range block.Transactions() {
|
return receipt, execResult.UsedGas, err
|
||||||
|
}
|
||||||
// receipt, gas, err := applyTransaction(
|
|
||||||
// chainConfig, chainContext, nil, gp, evmKeeper, header, tx, usedGas, vmConfig,
|
|
||||||
// )
|
|
||||||
// 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)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // apply mining rewards
|
|
||||||
// accumulateRewards(chainConfig, evmKeeper, header, block.Uncles())
|
|
||||||
|
|
||||||
// // simulate BaseApp EndBlocker commitment
|
|
||||||
// ms.Write()
|
|
||||||
// cms.Commit()
|
|
||||||
|
|
||||||
// // block debugging output
|
|
||||||
// if block.NumberU64() > 0 && block.NumberU64()%1000 == 0 {
|
|
||||||
// fmt.Printf("processed block: %d (time so far: %v)\n", block.NumberU64(), time.Since(startTime))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 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(
|
|
||||||
// config *ethparams.ChainConfig, evmKeeper *evmkeeper.Keeper,
|
|
||||||
// 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)
|
|
||||||
// evmKeeper.AddBalance(uncle.Coinbase, r)
|
|
||||||
// r.Div(blockReward, rewardBig32)
|
|
||||||
// reward.Add(reward, r)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// evmKeeper.AddBalance(header.Coinbase, reward)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 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.
|
|
||||||
// // Code is pulled from go-ethereum 1.9 because the StateDB interface does not include the
|
|
||||||
// // SetBalance function implementation
|
|
||||||
// // Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/consensus/misc/dao.go#L74
|
|
||||||
// func applyDAOHardFork(evmKeeper *evmkeeper.Keeper) {
|
|
||||||
// // Retrieve the contract to refund balances into
|
|
||||||
// if !evmKeeper.Exist(ethparams.DAORefundContract) {
|
|
||||||
// evmKeeper.CreateAccount(ethparams.DAORefundContract)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Move every DAO account and extra-balance account funds into the refund contract
|
|
||||||
// for _, addr := range ethparams.DAODrainList() {
|
|
||||||
// evmKeeper.AddBalance(ethparams.DAORefundContract, evmKeeper.GetBalance(addr))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 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.
|
|
||||||
// // Function is also pulled from go-ethereum 1.9 because of the incompatible usage
|
|
||||||
// // Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/core/state_processor.go#L88
|
|
||||||
// func applyTransaction(
|
|
||||||
// config *ethparams.ChainConfig, bc ethcore.ChainContext, author *common.Address,
|
|
||||||
// gp *ethcore.GasPool, evmKeeper *evmkeeper.Keeper, header *ethtypes.Header,
|
|
||||||
// tx *ethtypes.Transaction, usedGas *uint64, cfg ethvm.Config,
|
|
||||||
// ) (*ethtypes.Receipt, uint64, error) {
|
|
||||||
// msg, err := tx.AsMessage(ethtypes.MakeSigner(config, header.Number))
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, 0, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Create a new context to be used in the EVM environment
|
|
||||||
// blockCtx := ethcore.NewEVMBlockContext(header, bc, author)
|
|
||||||
// txCtx := ethcore.NewEVMTxContext(msg)
|
|
||||||
|
|
||||||
// // Create a new environment which holds all relevant information
|
|
||||||
// // about the transaction and calling mechanisms.
|
|
||||||
// vmenv := ethvm.NewEVM(blockCtx, txCtx, evmKeeper, config, cfg)
|
|
||||||
|
|
||||||
// // Apply the transaction to the current state (included in the env)
|
|
||||||
// execResult, err := ethcore.ApplyMessage(vmenv, msg, gp)
|
|
||||||
// if err != nil {
|
|
||||||
// // 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
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, execResult.UsedGas, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// root := common.Hash{}.Bytes()
|
|
||||||
// *usedGas += execResult.UsedGas
|
|
||||||
|
|
||||||
// // 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.
|
|
||||||
// receipt := ethtypes.NewReceipt(root, execResult.Failed(), *usedGas)
|
|
||||||
// receipt.TxHash = tx.Hash()
|
|
||||||
// receipt.GasUsed = execResult.UsedGas
|
|
||||||
|
|
||||||
// // if the transaction created a contract, store the creation address in the receipt.
|
|
||||||
// if msg.To() == nil {
|
|
||||||
// receipt.ContractAddress = crypto.CreateAddress(vmenv.TxContext.Origin, tx.Nonce())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Set the receipt logs and create a bloom for filtering
|
|
||||||
// receipt.Logs = evmKeeper.GetTxLogs(tx.Hash())
|
|
||||||
// receipt.Bloom = ethtypes.CreateBloom(ethtypes.Receipts{receipt})
|
|
||||||
// receipt.BlockHash = header.Hash()
|
|
||||||
// receipt.BlockNumber = header.Number
|
|
||||||
// receipt.TransactionIndex = uint(evmKeeper.GetTxIndexTransient())
|
|
||||||
|
|
||||||
// return receipt, execResult.UsedGas, err
|
|
||||||
// }
|
|
||||||
|
Loading…
Reference in New Issue
Block a user