2019-05-27 13:51:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
chart "github.com/wcharczuk/go-chart"
|
|
|
|
"github.com/wcharczuk/go-chart/drawing"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/consensus/ethash"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/consensus/misc"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/vm"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
2019-11-21 14:09:21 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/trie"
|
2019-05-27 13:51:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var chartColors = []drawing.Color{
|
|
|
|
chart.ColorBlack,
|
|
|
|
chart.ColorRed,
|
|
|
|
chart.ColorBlue,
|
|
|
|
chart.ColorYellow,
|
|
|
|
chart.ColorOrange,
|
|
|
|
chart.ColorGreen,
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
func runBlock(dbstate *state.Stateless, chainConfig *params.ChainConfig,
|
2019-05-27 13:51:49 +00:00
|
|
|
bcb core.ChainContext, header *types.Header, block *types.Block, trace bool, checkRoot bool,
|
|
|
|
) error {
|
|
|
|
vmConfig := vm.Config{}
|
|
|
|
engine := ethash.NewFullFaker()
|
|
|
|
statedb := state.New(dbstate)
|
|
|
|
statedb.SetTrace(trace)
|
|
|
|
gp := new(core.GasPool).AddGas(block.GasLimit())
|
|
|
|
usedGas := new(uint64)
|
|
|
|
var receipts types.Receipts
|
|
|
|
if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
|
|
|
|
misc.ApplyDAOHardFork(statedb)
|
|
|
|
}
|
|
|
|
for _, tx := range block.Transactions() {
|
|
|
|
receipt, err := core.ApplyTransaction(chainConfig, bcb, nil, gp, statedb, dbstate, header, tx, usedGas, vmConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("tx %x failed: %v", tx.Hash(), err)
|
|
|
|
}
|
|
|
|
receipts = append(receipts, receipt)
|
|
|
|
}
|
|
|
|
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
|
|
|
|
if _, err := engine.FinalizeAndAssemble(chainConfig, header, statedb, block.Transactions(), block.Uncles(), receipts); err != nil {
|
|
|
|
return fmt.Errorf("finalize of block %d failed: %v", block.NumberU64(), err)
|
|
|
|
}
|
|
|
|
dbstate.SetBlockNr(block.NumberU64())
|
|
|
|
|
|
|
|
ctx := chainConfig.WithEIPsFlags(context.Background(), header.Number)
|
|
|
|
if err := statedb.CommitBlock(ctx, dbstate); err != nil {
|
|
|
|
return fmt.Errorf("commiting block %d failed: %v", block.NumberU64(), err)
|
|
|
|
}
|
2019-10-21 12:54:47 +00:00
|
|
|
if err := dbstate.CheckRoot(header.Root); err != nil {
|
2019-05-27 13:51:49 +00:00
|
|
|
return fmt.Errorf("error processing block %d: %v", block.NumberU64(), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:57:44 +00:00
|
|
|
type CreateDbFunc func(string) (ethdb.Database, error)
|
|
|
|
|
|
|
|
func stateless(chaindata string,
|
|
|
|
statefile string,
|
|
|
|
triesize int,
|
|
|
|
tryPreRoot bool,
|
|
|
|
interval uint64,
|
|
|
|
ignoreOlderThan uint64,
|
|
|
|
witnessThreshold uint64,
|
2019-11-21 14:09:21 +00:00
|
|
|
statsfile string,
|
2019-11-21 13:57:44 +00:00
|
|
|
createDb CreateDbFunc) {
|
|
|
|
|
2019-11-07 10:36:27 +00:00
|
|
|
state.MaxTrieCacheGen = uint32(triesize)
|
2019-05-27 13:51:49 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
interruptCh := make(chan bool, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-sigs
|
|
|
|
interruptCh <- true
|
|
|
|
}()
|
|
|
|
|
2019-11-21 13:57:44 +00:00
|
|
|
ethDb, err := createDb(chaindata)
|
2019-05-27 13:51:49 +00:00
|
|
|
check(err)
|
|
|
|
defer ethDb.Close()
|
|
|
|
chainConfig := params.MainnetChainConfig
|
2019-11-21 14:09:21 +00:00
|
|
|
|
|
|
|
stats, err := NewStatsFile(statsfile)
|
2019-05-27 13:51:49 +00:00
|
|
|
check(err)
|
2019-11-21 14:09:21 +00:00
|
|
|
defer stats.Close()
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
vmConfig := vm.Config{}
|
|
|
|
engine := ethash.NewFullFaker()
|
|
|
|
bcb, err := core.NewBlockChain(ethDb, nil, chainConfig, engine, vm.Config{}, nil)
|
|
|
|
check(err)
|
2019-11-21 13:57:44 +00:00
|
|
|
stateDb, err := createDb(statefile)
|
2019-05-27 13:51:49 +00:00
|
|
|
check(err)
|
2019-11-13 02:26:07 +00:00
|
|
|
defer stateDb.Close()
|
2019-05-27 13:51:49 +00:00
|
|
|
blockNum := uint64(*block)
|
|
|
|
var preRoot common.Hash
|
|
|
|
if blockNum == 1 {
|
|
|
|
_, _, _, err = core.SetupGenesisBlock(stateDb, core.DefaultGenesisBlock())
|
|
|
|
check(err)
|
|
|
|
genesisBlock, _, _, err := core.DefaultGenesisBlock().ToBlock(nil)
|
|
|
|
check(err)
|
|
|
|
preRoot = genesisBlock.Header().Root
|
|
|
|
} else {
|
|
|
|
//load_snapshot(db, fmt.Sprintf("/Volumes/tb4/turbo-geth-copy/state_%d", blockNum-1))
|
|
|
|
//loadCodes(db, ethDb)
|
|
|
|
block := bcb.GetBlockByNumber(blockNum - 1)
|
|
|
|
fmt.Printf("Block number: %d\n", blockNum-1)
|
|
|
|
fmt.Printf("Block root hash: %x\n", block.Root())
|
|
|
|
preRoot = block.Root()
|
2019-11-21 13:57:44 +00:00
|
|
|
checkRoots(stateDb, preRoot, blockNum-1)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
batch := stateDb.NewBatch()
|
2019-11-07 11:14:30 +00:00
|
|
|
defer func() {
|
2019-11-07 11:17:29 +00:00
|
|
|
if _, err = batch.Commit(); err != nil {
|
2019-11-07 11:14:30 +00:00
|
|
|
fmt.Printf("Failed to commit batch: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-05-27 13:51:49 +00:00
|
|
|
tds, err := state.NewTrieDbState(preRoot, batch, blockNum-1)
|
|
|
|
check(err)
|
|
|
|
if blockNum > 1 {
|
|
|
|
tds.Rebuild()
|
|
|
|
}
|
|
|
|
tds.SetResolveReads(false)
|
|
|
|
tds.SetNoHistory(true)
|
|
|
|
interrupt := false
|
2019-10-21 12:54:47 +00:00
|
|
|
var witness []byte
|
2019-05-27 13:51:49 +00:00
|
|
|
for !interrupt {
|
2019-10-21 12:54:47 +00:00
|
|
|
trace := false // blockNum == 545080
|
2019-11-21 13:57:44 +00:00
|
|
|
tds.SetResolveReads(blockNum >= witnessThreshold)
|
2019-05-27 13:51:49 +00:00
|
|
|
block := bcb.GetBlockByNumber(blockNum)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
statedb := state.New(tds)
|
|
|
|
gp := new(core.GasPool).AddGas(block.GasLimit())
|
|
|
|
usedGas := new(uint64)
|
|
|
|
header := block.Header()
|
|
|
|
tds.StartNewBuffer()
|
|
|
|
var receipts types.Receipts
|
|
|
|
if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
|
|
|
|
misc.ApplyDAOHardFork(statedb)
|
|
|
|
}
|
|
|
|
for i, tx := range block.Transactions() {
|
|
|
|
statedb.Prepare(tx.Hash(), block.Hash(), i)
|
|
|
|
receipt, err := core.ApplyTransaction(chainConfig, bcb, nil, gp, statedb, tds.TrieStateWriter(), header, tx, usedGas, vmConfig)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("tx %x failed: %v\n", tx.Hash(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !chainConfig.IsByzantium(header.Number) {
|
|
|
|
tds.StartNewBuffer()
|
|
|
|
}
|
|
|
|
receipts = append(receipts, receipt)
|
|
|
|
}
|
|
|
|
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
|
|
|
|
if _, err = engine.FinalizeAndAssemble(chainConfig, header, statedb, block.Transactions(), block.Uncles(), receipts); err != nil {
|
|
|
|
fmt.Printf("Finalize of block %d failed: %v\n", blockNum, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := chainConfig.WithEIPsFlags(context.Background(), header.Number)
|
|
|
|
if err := statedb.FinalizeTx(ctx, tds.TrieStateWriter()); err != nil {
|
|
|
|
fmt.Printf("FinalizeTx of block %d failed: %v\n", blockNum, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
if err = tds.ResolveStateTrie(); err != nil {
|
|
|
|
fmt.Printf("Failed to resolve state trie: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
witness = nil
|
2019-11-21 13:57:44 +00:00
|
|
|
if blockNum >= witnessThreshold {
|
2019-10-21 12:54:47 +00:00
|
|
|
// Witness has to be extracted before the state trie is modified
|
2019-11-21 14:09:21 +00:00
|
|
|
var tapeStats trie.WitnessTapeStats
|
|
|
|
witness, tapeStats, err = tds.ExtractWitness(trace)
|
2019-10-21 12:54:47 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error extracting witness for block %d: %v\n", blockNum, err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-21 14:09:21 +00:00
|
|
|
|
|
|
|
err = stats.AddRow(blockNum, witness, tapeStats)
|
|
|
|
check(err)
|
2019-10-21 12:54:47 +00:00
|
|
|
}
|
|
|
|
finalRootFail := false
|
2019-11-21 13:57:44 +00:00
|
|
|
if blockNum >= witnessThreshold && witness != nil { // witness == nil means the extraction fails
|
2019-10-21 12:54:47 +00:00
|
|
|
var s *state.Stateless
|
|
|
|
s, err = state.NewStateless(preRoot, witness, blockNum-1, trace)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error making stateless2 for block %d: %v\n", blockNum, err)
|
|
|
|
filename := fmt.Sprintf("right_%d.txt", blockNum-1)
|
|
|
|
f, err1 := os.Create(filename)
|
|
|
|
if err1 == nil {
|
|
|
|
defer f.Close()
|
|
|
|
tds.PrintTrie(f)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := runBlock(s, chainConfig, bcb, header, block, trace, true); err != nil {
|
|
|
|
fmt.Printf("Error running block %d through stateless2: %v\n", blockNum, err)
|
|
|
|
finalRootFail = true
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
var preCalculatedRoot common.Hash
|
|
|
|
if tryPreRoot {
|
2019-11-21 15:56:39 +00:00
|
|
|
preCalculatedRoot, err = tds.CalcTrieRoots(blockNum == 2703827)
|
2019-11-15 22:48:49 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to calculate preRoot for block %d: %v\n", blockNum, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 12:54:47 +00:00
|
|
|
roots, err := tds.UpdateStateTrie()
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
2019-10-21 12:54:47 +00:00
|
|
|
fmt.Printf("failed to calculate IntermediateRoot: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
if tryPreRoot && tds.LastRoot() != preCalculatedRoot {
|
|
|
|
filename := fmt.Sprintf("right_%d.txt", blockNum)
|
|
|
|
f, err1 := os.Create(filename)
|
|
|
|
if err1 == nil {
|
|
|
|
defer f.Close()
|
|
|
|
tds.PrintTrie(f)
|
|
|
|
}
|
|
|
|
fmt.Printf("block %d, preCalculatedRoot %x != lastRoot %x\n", blockNum, preCalculatedRoot, tds.LastRoot())
|
|
|
|
return
|
|
|
|
}
|
2019-10-21 12:54:47 +00:00
|
|
|
if finalRootFail {
|
|
|
|
filename := fmt.Sprintf("right_%d.txt", blockNum)
|
|
|
|
f, err1 := os.Create(filename)
|
|
|
|
if err1 == nil {
|
|
|
|
defer f.Close()
|
|
|
|
tds.PrintTrie(f)
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !chainConfig.IsByzantium(header.Number) {
|
|
|
|
for i, receipt := range receipts {
|
|
|
|
receipt.PostState = roots[i].Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextRoot := roots[len(roots)-1]
|
|
|
|
if nextRoot != block.Root() {
|
|
|
|
fmt.Printf("Root hash does not match for block %d, expected %x, was %x\n", blockNum, block.Root(), nextRoot)
|
2019-11-21 13:57:44 +00:00
|
|
|
return
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
tds.SetBlockNr(blockNum)
|
|
|
|
|
|
|
|
err = statedb.CommitBlock(ctx, tds.DbStateWriter())
|
|
|
|
if err != nil {
|
2019-10-21 12:54:47 +00:00
|
|
|
fmt.Printf("Commiting block %d failed: %v", blockNum, err)
|
2019-05-27 13:51:49 +00:00
|
|
|
return
|
|
|
|
}
|
2019-11-21 13:36:24 +00:00
|
|
|
|
|
|
|
willSnapshot := interval > 0 && blockNum > 0 && blockNum >= ignoreOlderThan && blockNum%interval == 0
|
|
|
|
|
|
|
|
if batch.BatchSize() >= 100000 || willSnapshot {
|
2019-10-21 12:54:47 +00:00
|
|
|
if _, err := batch.Commit(); err != nil {
|
|
|
|
fmt.Printf("Failed to commit batch: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-13 02:26:07 +00:00
|
|
|
tds.PruneTries(false)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-11-21 13:36:24 +00:00
|
|
|
|
|
|
|
if willSnapshot {
|
2019-10-21 12:54:47 +00:00
|
|
|
// Snapshots of the state will be written to the same directory as the state file
|
2019-11-21 13:36:24 +00:00
|
|
|
fmt.Printf("\nSaving snapshot at block %d, hash %x\n", blockNum, block.Root())
|
2019-11-21 13:57:44 +00:00
|
|
|
|
|
|
|
saveSnapshot(stateDb, fmt.Sprintf("%s_%d", statefile, blockNum), createDb)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-11-21 13:36:24 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
preRoot = header.Root
|
|
|
|
blockNum++
|
2019-11-21 13:36:24 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if blockNum%1000 == 0 {
|
2019-11-21 13:36:24 +00:00
|
|
|
// overwrite terminal line, if no snapshot was made and not the first line
|
|
|
|
if blockNum > 0 && !willSnapshot {
|
|
|
|
fmt.Printf("\r")
|
|
|
|
}
|
|
|
|
fmt.Printf("Processed %d blocks", blockNum)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
// Check for interrupts
|
|
|
|
select {
|
|
|
|
case interrupt = <-interruptCh:
|
|
|
|
fmt.Println("interrupted, please wait for cleanup...")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("Processed %d blocks\n", blockNum)
|
|
|
|
fmt.Printf("Next time specify -block %d\n", blockNum)
|
|
|
|
fmt.Printf("Stateless client analysis took %s\n", time.Since(startTime))
|
|
|
|
}
|