cleanup main function

This commit is contained in:
Aleksandr Bezobchuk 2018-07-05 11:06:31 -04:00
parent cb5c081858
commit c6269e23b3

385
main.go
View File

@ -4,169 +4,232 @@
// as Ethermint. // as Ethermint.
package main package main
// eth_common "github.com/ethereum/go-ethereum/common" import (
// eth_misc "github.com/ethereum/go-ethereum/consensus/misc" "bytes"
// eth_core "github.com/ethereum/go-ethereum/core" "encoding/json"
// eth_state "github.com/ethereum/go-ethereum/core/state" "fmt"
// eth_types "github.com/ethereum/go-ethereum/core/types" "io"
// eth_vm "github.com/ethereum/go-ethereum/core/vm" "os"
// eth_params "github.com/ethereum/go-ethereum/params"
// eth_rlp "github.com/ethereum/go-ethereum/rlp"
// dbm "github.com/tendermint/tendermint/libs/db"
// var ( ethcommon "github.com/ethereum/go-ethereum/common"
// // TODO: Document... ethmisc "github.com/ethereum/go-ethereum/consensus/misc"
// miner501 = eth_common.HexToAddress("0x35e8e5dC5FBd97c5b421A80B596C030a2Be2A04D") ethcore "github.com/ethereum/go-ethereum/core"
// ) ethstate "github.com/ethereum/go-ethereum/core/state"
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/ledgerwatch/ethermint/core"
"github.com/ledgerwatch/ethermint/state"
dbm "github.com/tendermint/tendermint/libs/db"
)
var (
// TODO: Document...
miner501 = ethcommon.HexToAddress("0x35e8e5dC5FBd97c5b421A80B596C030a2Be2A04D")
genInvestor = ethcommon.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0")
)
// TODO: Document...
func main() { func main() {
// stateDb := dbm.NewDB("state" /* name */, dbm.MemDBBackend, "" /* dir */) stateDB := dbm.NewDB("state", dbm.MemDBBackend, "")
// codeDB := dbm.NewDB("code" /* name */, dbm.MemDBBackend, "" /* dir */) codeDB := dbm.NewDB("code", dbm.MemDBBackend, "")
// d, err := OurNewDatabase(stateDb, codeDB)
// if err != nil {
// panic(err)
// }
// fmt.Printf("Instantiating state.StateDB\n")
// // With empty root hash, i.e. empty state
// statedb, err := eth_state.New(eth_common.Hash{}, d)
// if err != nil {
// panic(err)
// }
// g := eth_core.DefaultGenesisBlock()
// for addr, account := range g.Alloc {
// statedb.AddBalance(addr, account.Balance)
// statedb.SetCode(addr, account.Code)
// statedb.SetNonce(addr, account.Nonce)
// for key, value := range account.Storage {
// statedb.SetState(addr, key, value)
// }
// }
// // One of the genesis account having 200 ETH ethermintDB, err := state.NewDatabase(stateDB, codeDB)
// b := statedb.GetBalance(eth_common.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0")) if err != nil {
// fmt.Printf("Balance: %s\n", b) panic(err)
// genesis_root, err := statedb.Commit(false /* deleteEmptyObjects */) }
// if err != nil {
// panic(err) fmt.Println("instantiating new geth state.StateDB")
// }
// commitID := d.stateStore.Commit() // start with empty root hash (i.e. empty state)
// fmt.Printf("CommitID after genesis: %v\n", commitID) gethStateDB, err := ethstate.New(ethcommon.Hash{}, ethermintDB)
// fmt.Printf("Genesis state root hash: %x\n", genesis_root[:]) if err != nil {
// // File with blockchain data exported from geth by using "geth expordb" command panic(err)
// input, err := os.Open("/Users/alexeyakhunov/mygit/blockchain") }
// if err != nil {
// panic(err) genBlock := ethcore.DefaultGenesisBlock()
// } for addr, account := range genBlock.Alloc {
// defer input.Close() gethStateDB.AddBalance(addr, account.Balance)
// // Ethereum mainnet config gethStateDB.SetCode(addr, account.Code)
// chainConfig := eth_params.MainnetChainConfig gethStateDB.SetNonce(addr, account.Nonce)
// stream := eth_rlp.NewStream(input, 0)
// var block eth_types.Block for key, value := range account.Storage {
// n := 0 gethStateDB.SetState(addr, key, value)
// var root500 eth_common.Hash // Root hash after block 500 }
// var root501 eth_common.Hash // Root hash after block 501 }
// prev_root := genesis_root
// d.tracing = true // get balance of one of the genesis account having 200 ETH
// chainContext := &OurChainContext{} b := gethStateDB.GetBalance(genInvestor)
// vmConfig := eth_vm.Config{} fmt.Printf("balance of %s: %s\n", genInvestor.String(), b)
// for {
// if err = stream.Decode(&block); err == io.EOF { // commit the geth stateDB with 'false' to delete empty objects
// err = nil // Clear it genRoot, err := gethStateDB.Commit(false)
// break if err != nil {
// } else if err != nil { panic(err)
// panic(fmt.Errorf("at block %d: %v", block.NumberU64(), err)) }
// }
// // don't import first block commitID := ethermintDB.Commit()
// if block.NumberU64() == 0 {
// continue fmt.Printf("commitID after genesis: %v\n", commitID)
// } fmt.Printf("genesis state root hash: %x\n", genRoot[:])
// header := block.Header()
// chainContext.coinbase = header.Coinbase // file with blockchain data exported from geth by using "geth exportdb"
// statedb, err := eth_state.New(prev_root, d) // command.
// if err != nil { //
// panic(fmt.Errorf("at block %d: %v", block.NumberU64(), err)) // TODO: Allow this to be configurable
// } input, err := os.Open("/Users/alexeyakhunov/mygit/blockchain")
// var ( if err != nil {
// receipts eth_types.Receipts panic(err)
// usedGas = new(uint64) }
// allLogs []*eth_types.Log
// gp = new(eth_core.GasPool).AddGas(block.GasLimit()) defer input.Close()
// )
// if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 { // ethereum mainnet config
// eth_misc.ApplyDAOHardFork(statedb) chainConfig := ethparams.MainnetChainConfig
// }
// for i, tx := range block.Transactions() { // create RLP stream for exported blocks
// statedb.Prepare(tx.Hash(), block.Hash(), i) stream := ethrlp.NewStream(input, 0)
// var h eth_common.Hash = tx.Hash()
// if bytes.Equal(h[:], eth_common.FromHex("0xc438cfcc3b74a28741bda361032f1c6362c34aa0e1cedff693f31ec7d6a12717")) { var (
// vmConfig.Tracer = eth_vm.NewStructLogger(&eth_vm.LogConfig{}) block ethtypes.Block
// vmConfig.Debug = true root500 ethcommon.Hash // root hash after block 500
// } root501 ethcommon.Hash // root hash after block 501
// receipt, _, err := eth_core.ApplyTransaction(chainConfig, chainContext, nil, gp, statedb, header, tx, usedGas, vmConfig) )
// if vmConfig.Tracer != nil {
// w, err := os.Create("structlogs.txt") prevRoot := genRoot
// if err != nil { ethermintDB.Tracing = true
// panic(err) chainContext := &core.ChainContext{}
// } vmConfig := ethvm.Config{}
// encoder := json.NewEncoder(w)
// logs := FormatLogs(vmConfig.Tracer.(*eth_vm.StructLogger).StructLogs()) n := 0
// if err := encoder.Encode(logs); err != nil { for {
// panic(err) if err = stream.Decode(&block); err == io.EOF {
// } err = nil
// if err := w.Close(); err != nil { break
// panic(err) } else if err != nil {
// } panic(fmt.Errorf("failed to decode at block %d: %s", block.NumberU64(), err))
// vmConfig.Debug = false }
// vmConfig.Tracer = nil
// } // don't import first block
// if err != nil { if block.NumberU64() == 0 {
// panic(fmt.Errorf("at block %d, tx %x: %v", block.NumberU64(), tx.Hash(), err)) continue
// } }
// receipts = append(receipts, receipt)
// allLogs = append(allLogs, receipt.Logs...) header := block.Header()
// } chainContext.Coinbase = header.Coinbase
// // Apply mining rewards to the statedb
// accumulateRewards(chainConfig, statedb, header, block.Uncles()) gethStateDB, err := ethstate.New(prevRoot, ethermintDB)
// // Commit block if err != nil {
// prev_root, err = statedb.Commit(chainConfig.IsEIP158(block.Number()) /* deleteEmptyObjects */) panic(fmt.Errorf("failed to instantiate geth state.StateDB at block %d: %v", block.NumberU64(), err))
// if err != nil { }
// panic(fmt.Errorf("at block %d: %v", block.NumberU64(), err))
// } var (
// //fmt.Printf("State root after block %d: %x\n", block.NumberU64(), prev_root) receipts ethtypes.Receipts
// d.stateStore.Commit() usedGas = new(uint64)
// //fmt.Printf("CommitID after block %d: %v\n", block.NumberU64(), commitID) allLogs []*ethtypes.Log
// switch block.NumberU64() { gp = new(ethcore.GasPool).AddGas(block.GasLimit())
// case 500: )
// root500 = prev_root
// case 501: if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
// root501 = prev_root ethmisc.ApplyDAOHardFork(gethStateDB)
// } }
// n++
// if n%10000 == 0 { for i, tx := range block.Transactions() {
// fmt.Printf("Processed %d blocks\n", n) gethStateDB.Prepare(tx.Hash(), block.Hash(), i)
// }
// if n >= 100000 { txHash := tx.Hash()
// break // TODO: Why this address?
// } if bytes.Equal(txHash[:], ethcommon.FromHex("0xc438cfcc3b74a28741bda361032f1c6362c34aa0e1cedff693f31ec7d6a12717")) {
// } vmConfig.Tracer = ethvm.NewStructLogger(&ethvm.LogConfig{})
// fmt.Printf("Processed %d blocks\n", n) vmConfig.Debug = true
// d.tracing = true }
// genesis_state, err := eth_state.New(genesis_root, d)
// fmt.Printf("Balance of one of the genesis investors: %s\n", genesis_state.GetBalance(eth_common.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0"))) receipt, _, err := ethcore.ApplyTransaction(chainConfig, chainContext, nil, gp, gethStateDB, header, tx, usedGas, vmConfig)
// //miner501 := eth_common.HexToAddress("0x35e8e5dC5FBd97c5b421A80B596C030a2Be2A04D") // Miner of the block 501 if vmConfig.Tracer != nil {
// // Try to create a new statedb from root of the block 500 w, err := os.Create("structlogs.txt")
// fmt.Printf("root500: %x\n", root500[:]) if err != nil {
// state500, err := eth_state.New(root500, d) panic(err)
// if err != nil { }
// panic(err)
// } encoder := json.NewEncoder(w)
// miner501_balance_at_500 := state500.GetBalance(miner501) logs := FormatLogs(vmConfig.Tracer.(*ethvm.StructLogger).StructLogs())
// state501, err := eth_state.New(root501, d)
// if err != nil { if err := encoder.Encode(logs); err != nil {
// panic(err) panic(err)
// } }
// miner501_balance_at_501 := state501.GetBalance(miner501)
// fmt.Printf("Investor's balance after block 500: %d\n", state500.GetBalance(eth_common.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0"))) if err := w.Close(); err != nil {
// fmt.Printf("Miner of block 501's balance after block 500: %d\n", miner501_balance_at_500) panic(err)
// fmt.Printf("Miner of block 501's balance after block 501: %d\n", miner501_balance_at_501) }
vmConfig.Debug = false
vmConfig.Tracer = nil
}
if err != nil {
panic(fmt.Errorf("at block %d, tx %x: %v", block.NumberU64(), tx.Hash(), err))
}
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
}
// apply mining rewards to the geth stateDB
accumulateRewards(chainConfig, gethStateDB, header, block.Uncles())
// commit block in geth
prevRoot, err = gethStateDB.Commit(chainConfig.IsEIP158(block.Number()))
if err != nil {
panic(fmt.Errorf("at block %d: %v", block.NumberU64(), err))
}
// commit block in Ethermint
ethermintDB.Commit()
switch block.NumberU64() {
case 500:
root500 = prevRoot
case 501:
root501 = prevRoot
}
n++
if n%10000 == 0 {
fmt.Printf("processed %d blocks\n", n)
}
if n >= 100000 {
break
}
}
fmt.Printf("processed %d blocks\n", n)
ethermintDB.Tracing = true
genState, err := ethstate.New(genRoot, ethermintDB)
if err != nil {
panic(err)
}
fmt.Printf("balance of one of the genesis investors: %s\n", genState.GetBalance(genInvestor))
// try to create a new geth stateDB from root of the block 500
fmt.Printf("root500: %x\n", root500[:])
state500, err := ethstate.New(root500, ethermintDB)
if err != nil {
panic(err)
}
miner501BalanceAt500 := state500.GetBalance(miner501)
state501, err := ethstate.New(root501, ethermintDB)
if err != nil {
panic(err)
}
miner501BalanceAt501 := state501.GetBalance(miner501)
fmt.Printf("investor's balance after block 500: %d\n", state500.GetBalance(genInvestor))
fmt.Printf("miner of block 501's balance after block 500: %d\n", miner501BalanceAt500)
fmt.Printf("miner of block 501's balance after block 501: %d\n", miner501BalanceAt501)
} }