erigon-pulse/cmd/hack/hack.go
Evgeny Danilenko 74847d77e6
Rename to Erigon (#2018)
* turbo-geth to erigon

* tg, turbo to erigon
2021-05-26 11:35:39 +01:00

2163 lines
55 KiB
Go

package main
import (
"bufio"
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"os"
"os/signal"
"path"
"runtime"
"runtime/pprof"
"sort"
"strings"
"syscall"
"time"
"github.com/holiman/uint256"
"github.com/valyala/fastjson"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/util"
"github.com/ledgerwatch/erigon/cmd/hack/db"
"github.com/ledgerwatch/erigon/cmd/hack/flow"
"github.com/ledgerwatch/erigon/cmd/hack/tool"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/changeset"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/common/paths"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/eth/stagedsync"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/mdbx"
"github.com/ledgerwatch/erigon/log"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/trie"
"github.com/ledgerwatch/lmdb-go/lmdb"
)
var (
emptyCodeHash = crypto.Keccak256(nil) //nolint
verbosity = flag.Uint("verbosity", 3, "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail (default 3)")
action = flag.String("action", "", "action to execute")
cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")
rewind = flag.Int("rewind", 1, "rewind to given number of blocks")
block = flag.Int("block", 1, "specifies a block number for operation")
blockTotal = flag.Int("blocktotal", 1, "specifies a total amount of blocks to process")
account = flag.String("account", "0x", "specifies account to investigate")
name = flag.String("name", "", "name to add to the file names")
chaindata = flag.String("chaindata", "chaindata", "path to the chaindata database file")
bucket = flag.String("bucket", "", "bucket in the database")
hash = flag.String("hash", "0x00", "image for preimage or state root for testBlockHashes action")
)
func readData(filename string) (blocks []float64, hours []float64, dbsize []float64, trienodes []float64, heap []float64) {
err := util.File.ReadByLines(filename, func(line string) error {
parts := strings.Split(line, ",")
blocks = append(blocks, tool.ParseFloat64(strings.Trim(parts[0], " ")))
hours = append(hours, tool.ParseFloat64(strings.Trim(parts[1], " ")))
dbsize = append(dbsize, tool.ParseFloat64(strings.Trim(parts[2], " ")))
trienodes = append(trienodes, tool.ParseFloat64(strings.Trim(parts[3], " ")))
heap = append(heap, tool.ParseFloat64(strings.Trim(parts[4], " ")))
return nil
})
if err != nil {
fmt.Println(err.Error())
}
return
}
func notables() []chart.GridLine {
return []chart.GridLine{
{Value: 1.0},
{Value: 2.0},
{Value: 3.0},
{Value: 4.0},
{Value: 5.0},
{Value: 6.0},
}
}
func days() []chart.GridLine {
return []chart.GridLine{
{Value: 24.0},
{Value: 48.0},
{Value: 72.0},
{Value: 96.0},
{Value: 120.0},
{Value: 144.0},
{Value: 168.0},
{Value: 192.0},
{Value: 216.0},
{Value: 240.0},
{Value: 264.0},
{Value: 288.0},
}
}
func mychart() {
blocks, hours, dbsize, trienodes, heap := readData("bolt.csv")
blocks0, hours0, dbsize0, _, _ := readData("badger.csv")
mainSeries := &chart.ContinuousSeries{
Name: "Cumulative sync time (bolt)",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorBlue,
FillColor: chart.ColorBlue.WithAlpha(100),
},
XValues: blocks,
YValues: hours,
}
badgerSeries := &chart.ContinuousSeries{
Name: "Cumulative sync time (badger)",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorRed,
FillColor: chart.ColorRed.WithAlpha(100),
},
XValues: blocks0,
YValues: hours0,
}
dbsizeSeries := &chart.ContinuousSeries{
Name: "Database size (bolt)",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorBlack,
},
YAxis: chart.YAxisSecondary,
XValues: blocks,
YValues: dbsize,
}
dbsizeSeries0 := &chart.ContinuousSeries{
Name: "Database size (badger)",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorOrange,
},
YAxis: chart.YAxisSecondary,
XValues: blocks,
YValues: dbsize0,
}
graph1 := chart.Chart{
Width: 1280,
Height: 720,
Background: chart.Style{
Padding: chart.Box{
Top: 50,
},
},
YAxis: chart.YAxis{
Name: "Elapsed time",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%d h", int(v.(float64)))
},
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.ColorBlue,
StrokeWidth: 1.0,
},
GridLines: days(),
},
YAxisSecondary: chart.YAxis{
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%d G", int(v.(float64)))
},
},
XAxis: chart.XAxis{
Name: "Blocks, million",
Style: chart.Style{
Show: true,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%.3fm", v.(float64))
},
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.ColorAlternateGray,
StrokeWidth: 1.0,
},
GridLines: notables(),
},
Series: []chart.Series{
mainSeries,
badgerSeries,
dbsizeSeries,
dbsizeSeries0,
},
}
graph1.Elements = []chart.Renderable{chart.LegendThin(&graph1)}
buffer := bytes.NewBuffer([]byte{})
err := graph1.Render(chart.PNG, buffer)
tool.Check(err)
err = ioutil.WriteFile("chart1.png", buffer.Bytes(), 0644)
tool.Check(err)
heapSeries := &chart.ContinuousSeries{
Name: "Allocated heap",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorYellow,
FillColor: chart.ColorYellow.WithAlpha(100),
},
XValues: blocks,
YValues: heap,
}
trienodesSeries := &chart.ContinuousSeries{
Name: "Trie nodes",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorGreen,
},
YAxis: chart.YAxisSecondary,
XValues: blocks,
YValues: trienodes,
}
graph2 := chart.Chart{
Width: 1280,
Height: 720,
Background: chart.Style{
Padding: chart.Box{
Top: 50,
},
},
YAxis: chart.YAxis{
Name: "Allocated heap",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%.1f G", v.(float64))
},
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.ColorYellow,
StrokeWidth: 1.0,
},
GridLines: days(),
},
YAxisSecondary: chart.YAxis{
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%.1f m", v.(float64))
},
},
XAxis: chart.XAxis{
Name: "Blocks, million",
Style: chart.Style{
Show: true,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%.3fm", v.(float64))
},
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.ColorAlternateGray,
StrokeWidth: 1.0,
},
GridLines: notables(),
},
Series: []chart.Series{
heapSeries,
trienodesSeries,
},
}
graph2.Elements = []chart.Renderable{chart.LegendThin(&graph2)}
buffer.Reset()
err = graph2.Render(chart.PNG, buffer)
tool.Check(err)
err = ioutil.WriteFile("chart2.png", buffer.Bytes(), 0644)
tool.Check(err)
}
//nolint
func accountSavings(db ethdb.RwKV) (int, int) {
emptyRoots := 0
emptyCodes := 0
tool.Check(db.View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.HashedAccountsBucket)
if err != nil {
return err
}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
if bytes.Contains(v, trie.EmptyRoot.Bytes()) {
emptyRoots++
}
if bytes.Contains(v, emptyCodeHash) {
emptyCodes++
}
}
return nil
}))
return emptyRoots, emptyCodes
}
func bucketStats(chaindata string) error {
ethDb := ethdb.MustOpenKV(chaindata)
defer ethDb.Close()
var bucketList []string
if err1 := ethDb.View(context.Background(), func(txa ethdb.Tx) error {
if bl, err := txa.(ethdb.BucketMigrator).ExistingBuckets(); err == nil {
bucketList = bl
} else {
return err
}
return nil
}); err1 != nil {
ethDb.Close()
return err1
}
fmt.Printf(",BranchPageN,LeafPageN,OverflowN,Entries\n")
switch kv := ethDb.(type) {
case *ethdb.LmdbKV:
type LmdbStat interface {
BucketStat(name string) (*lmdb.Stat, error)
}
if err := kv.View(context.Background(), func(tx ethdb.Tx) error {
for _, bucket := range bucketList {
bs, statErr := tx.(LmdbStat).BucketStat(bucket)
tool.Check(statErr)
fmt.Printf("%s,%d,%d,%d,%d\n", bucket,
bs.BranchPages, bs.LeafPages, bs.OverflowPages, bs.Entries)
}
bs, statErr := tx.(LmdbStat).BucketStat("freelist")
tool.Check(statErr)
fmt.Printf("%s,%d,%d,%d,%d\n", "freelist", bs.BranchPages, bs.LeafPages, bs.OverflowPages, bs.Entries)
return nil
}); err != nil {
panic(err)
}
case *ethdb.MdbxKV:
type MdbxStat interface {
BucketStat(name string) (*mdbx.Stat, error)
}
if err := kv.View(context.Background(), func(tx ethdb.Tx) error {
for _, bucket := range bucketList {
bs, statErr := tx.(MdbxStat).BucketStat(bucket)
tool.Check(statErr)
fmt.Printf("%s,%d,%d,%d,%d\n", bucket,
bs.BranchPages, bs.LeafPages, bs.OverflowPages, bs.Entries)
}
bs, statErr := tx.(MdbxStat).BucketStat("freelist")
tool.Check(statErr)
fmt.Printf("%s,%d,%d,%d,%d\n", "freelist", bs.BranchPages, bs.LeafPages, bs.OverflowPages, bs.Entries)
return nil
}); err != nil {
panic(err)
}
}
return nil
}
func readTrieLog() ([]float64, map[int][]float64, []float64) {
data, err := ioutil.ReadFile("dust/hack.log")
tool.Check(err)
thresholds := []float64{}
counts := map[int][]float64{}
for i := 2; i <= 16; i++ {
counts[i] = []float64{}
}
shorts := []float64{}
lines := bytes.Split(data, []byte("\n"))
for _, line := range lines {
if bytes.HasPrefix(line, []byte("Threshold:")) {
tokens := bytes.Split(line, []byte(" "))
if len(tokens) == 23 {
wei := tool.ParseFloat64(string(tokens[1]))
thresholds = append(thresholds, wei)
for i := 2; i <= 16; i++ {
pair := bytes.Split(tokens[i+3], []byte(":"))
counts[i] = append(counts[i], tool.ParseFloat64(string(pair[1])))
}
pair := bytes.Split(tokens[21], []byte(":"))
shorts = append(shorts, tool.ParseFloat64(string(pair[1])))
}
}
}
return thresholds, counts, shorts
}
func trieChart() {
thresholds, counts, shorts := readTrieLog()
fmt.Printf("%d %d %d\n", len(thresholds), len(counts), len(shorts))
shortsSeries := &chart.ContinuousSeries{
Name: "Short nodes",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorBlue,
FillColor: chart.ColorBlue.WithAlpha(100),
},
XValues: thresholds,
YValues: shorts,
}
countSeries := make(map[int]*chart.ContinuousSeries)
for i := 2; i <= 16; i++ {
countSeries[i] = &chart.ContinuousSeries{
Name: fmt.Sprintf("%d-nodes", i),
Style: chart.Style{
Show: true,
StrokeColor: chart.GetAlternateColor(i),
},
XValues: thresholds,
YValues: counts[i],
}
}
xaxis := &chart.XAxis{
Name: "Dust theshold",
Style: chart.Style{
Show: true,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%d wei", int(v.(float64)))
},
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.DefaultStrokeColor,
StrokeWidth: 1.0,
},
Range: &chart.ContinuousRange{
Min: thresholds[0],
Max: thresholds[len(thresholds)-1],
},
Ticks: []chart.Tick{
{Value: 0.0, Label: "0"},
{Value: 1.0, Label: "wei"},
{Value: 10.0, Label: "10"},
{Value: 100.0, Label: "100"},
{Value: 1e3, Label: "1e3"},
{Value: 1e4, Label: "1e4"},
{Value: 1e5, Label: "1e5"},
{Value: 1e6, Label: "1e6"},
{Value: 1e7, Label: "1e7"},
{Value: 1e8, Label: "1e8"},
{Value: 1e9, Label: "1e9"},
{Value: 1e10, Label: "1e10"},
//{1e15, "finney"},
//{1e18, "ether"},
},
}
graph3 := chart.Chart{
Width: 1280,
Height: 720,
Background: chart.Style{
Padding: chart.Box{
Top: 50,
},
},
XAxis: *xaxis,
YAxis: chart.YAxis{
Name: "Node count",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%dm", int(v.(float64)/1e6))
},
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.DefaultStrokeColor,
StrokeWidth: 1.0,
},
},
Series: []chart.Series{
shortsSeries,
},
}
graph3.Elements = []chart.Renderable{chart.LegendThin(&graph3)}
buffer := bytes.NewBuffer([]byte{})
err := graph3.Render(chart.PNG, buffer)
tool.Check(err)
err = ioutil.WriteFile("chart3.png", buffer.Bytes(), 0644)
tool.Check(err)
graph4 := chart.Chart{
Width: 1280,
Height: 720,
Background: chart.Style{
Padding: chart.Box{
Top: 50,
},
},
XAxis: *xaxis,
YAxis: chart.YAxis{
Name: "Node count",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%.2fm", v.(float64)/1e6)
},
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.DefaultStrokeColor,
StrokeWidth: 1.0,
},
},
Series: []chart.Series{
countSeries[2],
countSeries[3],
},
}
graph4.Elements = []chart.Renderable{chart.LegendThin(&graph4)}
buffer = bytes.NewBuffer([]byte{})
err = graph4.Render(chart.PNG, buffer)
tool.Check(err)
err = ioutil.WriteFile("chart4.png", buffer.Bytes(), 0644)
tool.Check(err)
graph5 := chart.Chart{
Width: 1280,
Height: 720,
Background: chart.Style{
Padding: chart.Box{
Top: 50,
},
},
XAxis: *xaxis,
YAxis: chart.YAxis{
Name: "Node count",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%.2fk", v.(float64)/1e3)
},
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.DefaultStrokeColor,
StrokeWidth: 1.0,
},
},
Series: []chart.Series{
countSeries[4],
countSeries[5],
countSeries[6],
countSeries[7],
countSeries[8],
countSeries[9],
countSeries[10],
countSeries[11],
countSeries[12],
countSeries[13],
countSeries[14],
countSeries[15],
countSeries[16],
},
}
graph5.Elements = []chart.Renderable{chart.LegendThin(&graph5)}
buffer = bytes.NewBuffer([]byte{})
err = graph5.Render(chart.PNG, buffer)
tool.Check(err)
err = ioutil.WriteFile("chart5.png", buffer.Bytes(), 0644)
tool.Check(err)
}
func dbSlice(chaindata string, bucket string, prefix []byte) {
db := ethdb.MustOpenKV(chaindata)
defer db.Close()
if err := db.View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(bucket)
if err != nil {
return err
}
for k, v, err := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v, err = c.Next() {
if err != nil {
return err
}
fmt.Printf("db.Put([]byte(\"%s\"), common.FromHex(\"%x\"), common.FromHex(\"%x\"))\n", bucket, k, v)
}
return nil
}); err != nil {
panic(err)
}
}
func hashFile() {
f, err := os.Open("/Users/alexeyakhunov/mygit/go-ethereum/geth.log")
tool.Check(err)
defer f.Close()
w, err := os.Create("/Users/alexeyakhunov/mygit/go-ethereum/geth_read.log")
tool.Check(err)
defer w.Close()
scanner := bufio.NewScanner(f)
count := 0
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "ResolveWithDb") || strings.HasPrefix(line, "Error") ||
strings.HasPrefix(line, "0000000000000000000000000000000000000000000000000000000000000000") ||
strings.HasPrefix(line, "ERROR") || strings.HasPrefix(line, "tc{") {
fmt.Printf("%d %s\n", count, line)
count++
} else if count == 66 {
w.WriteString(line)
w.WriteString("\n")
}
}
fmt.Printf("%d lines scanned\n", count)
}
func rlpIndices() {
keybuf := new(bytes.Buffer)
for i := 0; i < 512; i++ {
keybuf.Reset()
rlp.Encode(keybuf, uint(i))
fmt.Printf("Encoding of %d is %x\n", i, keybuf.Bytes())
}
}
func printFullNodeRLPs() {
trie.FullNode1()
trie.FullNode2()
trie.FullNode3()
trie.FullNode4()
trie.ShortNode1()
trie.ShortNode2()
trie.Hash1()
trie.Hash2()
trie.Hash3()
trie.Hash4()
trie.Hash5()
trie.Hash6()
trie.Hash7()
}
// Searches 1000 blocks from the given one to try to find the one with the given state root hash
func testBlockHashes(chaindata string, block int, stateRoot common.Hash) {
ethDb := ethdb.MustOpen(chaindata)
defer ethDb.Close()
blocksToSearch := 10000000
for i := uint64(block); i < uint64(block+blocksToSearch); i++ {
hash, err := rawdb.ReadCanonicalHash(ethDb, i)
if err != nil {
panic(err)
}
header := rawdb.ReadHeader(ethDb, hash, i)
if header.Root == stateRoot || stateRoot == (common.Hash{}) {
fmt.Printf("\n===============\nCanonical hash for %d: %x\n", i, hash)
fmt.Printf("Header.Root: %x\n", header.Root)
fmt.Printf("Header.TxHash: %x\n", header.TxHash)
fmt.Printf("Header.UncleHash: %x\n", header.UncleHash)
}
}
}
func printCurrentBlockNumber(chaindata string) {
ethDb := ethdb.MustOpenKV(chaindata)
defer ethDb.Close()
ethDb.View(context.Background(), func(tx ethdb.Tx) error {
hash := rawdb.ReadHeadBlockHash(tx)
number := rawdb.ReadHeaderNumber(tx, hash)
fmt.Printf("Block number: %d\n", *number)
return nil
})
}
func printTxHashes() {
db := ethdb.MustOpen(paths.DefaultDataDir() + "/geth/chaindata").RwKV()
defer db.Close()
if err := db.View(context.Background(), func(tx ethdb.Tx) error {
for b := uint64(0); b < uint64(100000); b++ {
hash, err := rawdb.ReadCanonicalHash(tx, b)
tool.Check(err)
block := rawdb.ReadBlock(tx, hash, b)
if block == nil {
break
}
for _, tx := range block.Transactions() {
fmt.Printf("%x\n", tx.Hash())
}
}
return nil
}); err != nil {
panic(err)
}
}
func readTrie(filename string) *trie.Trie {
f, err := os.Open(filename)
tool.Check(err)
defer f.Close()
t, err := trie.Load(f)
tool.Check(err)
return t
}
func invTree(wrong, right, diff string, name string) {
fmt.Printf("Reading trie...\n")
t1 := readTrie(fmt.Sprintf("%s_%s.txt", wrong, name))
fmt.Printf("Root hash: %x\n", t1.Hash())
fmt.Printf("Reading trie 2...\n")
t2 := readTrie(fmt.Sprintf("%s_%s.txt", right, name))
fmt.Printf("Root hash: %x\n", t2.Hash())
c, err := os.Create(fmt.Sprintf("%s_%s.txt", diff, name))
tool.Check(err)
defer c.Close()
t1.PrintDiff(t2, c)
}
func preimage(chaindata string, image common.Hash) {
ethDb := ethdb.MustOpen(chaindata)
defer ethDb.Close()
p, err := ethDb.Get(dbutils.PreimagePrefix, image[:])
tool.Check(err)
fmt.Printf("%x\n", p)
}
func printBranches(block uint64) {
//ethDb := ethdb.MustOpen("/home/akhounov/.ethereum/geth/chaindata")
ethDb := ethdb.MustOpen(paths.DefaultDataDir() + "/testnet/geth/chaindata")
defer ethDb.Close()
fmt.Printf("All headers at the same height %d\n", block)
{
var hashes []common.Hash
numberEnc := make([]byte, 8)
binary.BigEndian.PutUint64(numberEnc, block)
if err := ethDb.Walk("h", numberEnc, 8*8, func(k, v []byte) (bool, error) {
if len(k) == 8+32 {
hashes = append(hashes, common.BytesToHash(k[8:]))
}
return true, nil
}); err != nil {
panic(err)
}
for _, hash := range hashes {
h := rawdb.ReadHeader(ethDb, hash, block)
fmt.Printf("block hash: %x, root hash: %x\n", h.Hash(), h.Root)
}
}
}
func readAccount(chaindata string, account common.Address) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
var a accounts.Account
ok, err := rawdb.PlainReadAccount(db, account, &a)
if err != nil {
return err
} else if !ok {
return fmt.Errorf("acc not found")
}
fmt.Printf("CodeHash:%x\nIncarnation:%d\n", a.CodeHash, a.Incarnation)
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
return err
}
for k, v, e := c.Seek(account.Bytes()); k != nil && e == nil; k, v, e = c.Next() {
if e != nil {
return e
}
if !bytes.HasPrefix(k, account.Bytes()) {
break
}
fmt.Printf("%x => %x\n", k, v)
}
return nil
}); err != nil {
return err
}
return nil
}
func nextIncarnation(chaindata string, addrHash common.Hash) {
ethDb := ethdb.MustOpen(chaindata)
defer ethDb.Close()
var found bool
var incarnationBytes [common.IncarnationLength]byte
startkey := make([]byte, common.HashLength+common.IncarnationLength+common.HashLength)
var fixedbits = 8 * common.HashLength
copy(startkey, addrHash[:])
if err := ethDb.Walk(dbutils.HashedStorageBucket, startkey, fixedbits, func(k, v []byte) (bool, error) {
copy(incarnationBytes[:], k[common.HashLength:])
found = true
return false, nil
}); err != nil {
fmt.Printf("Incarnation(z): %d\n", 0)
return
}
if found {
fmt.Printf("Incarnation: %d\n", (binary.BigEndian.Uint64(incarnationBytes[:]))+1)
return
}
fmt.Printf("Incarnation(f): %d\n", state.FirstContractIncarnation)
}
func repairCurrent() {
historyDb := ethdb.MustOpen("/Volumes/tb4/erigon/ropsten/geth/chaindata")
defer historyDb.Close()
currentDb := ethdb.MustOpen("statedb")
defer currentDb.Close()
tool.Check(historyDb.ClearBuckets(dbutils.HashedStorageBucket))
tool.Check(historyDb.RwKV().Update(context.Background(), func(tx ethdb.RwTx) error {
newB, err := tx.RwCursor(dbutils.HashedStorageBucket)
if err != nil {
return err
}
count := 0
if err := currentDb.RwKV().View(context.Background(), func(ctx ethdb.Tx) error {
c, err := ctx.Cursor(dbutils.HashedStorageBucket)
if err != nil {
return err
}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
tool.Check(newB.Put(k, v))
count++
if count == 10000 {
fmt.Printf("Copied %d storage items\n", count)
}
}
return nil
}); err != nil {
return err
}
return nil
}))
}
func dumpStorage() {
db := ethdb.MustOpen(paths.DefaultDataDir() + "/geth/chaindata")
defer db.Close()
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.StorageHistoryBucket)
if err != nil {
return err
}
return ethdb.ForEach(c, func(k, v []byte) (bool, error) {
fmt.Printf("%x %x\n", k, v)
return true, nil
})
}); err != nil {
panic(err)
}
}
func printBucket(chaindata string) {
db := ethdb.MustOpen(chaindata)
defer db.Close()
f, err := os.Create("bucket.txt")
tool.Check(err)
defer f.Close()
fb := bufio.NewWriter(f)
defer fb.Flush()
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.StorageHistoryBucket)
if err != nil {
return err
}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
fmt.Fprintf(fb, "%x %x\n", k, v)
}
return nil
}); err != nil {
panic(err)
}
}
func ValidateTxLookups2(chaindata string) {
db := ethdb.MustOpen(chaindata)
defer db.Close()
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
}()
var blockNum uint64 = 1
validateTxLookups2(db, blockNum, interruptCh)
log.Info("All done", "duration", time.Since(startTime))
}
func validateTxLookups2(db ethdb.Database, startBlock uint64, interruptCh chan bool) {
tx, err := db.(ethdb.HasRwKV).RwKV().BeginRo(context.Background())
if err != nil {
panic(err)
}
defer tx.Rollback()
blockNum := startBlock
iterations := 0
var interrupt bool
// Validation Process
blockBytes := big.NewInt(0)
for !interrupt {
blockHash, err := rawdb.ReadCanonicalHash(tx, blockNum)
tool.Check(err)
body := rawdb.ReadBody(tx, blockHash, blockNum)
if body == nil {
break
}
select {
case interrupt = <-interruptCh:
log.Info("interrupted, please wait for cleanup...")
default:
}
blockBytes.SetUint64(blockNum)
bn := blockBytes.Bytes()
for _, txn := range body.Transactions {
val, err := tx.GetOne(dbutils.TxLookupPrefix, txn.Hash().Bytes())
iterations++
if iterations%100000 == 0 {
log.Info("Validated", "entries", iterations, "number", blockNum)
}
if !bytes.Equal(val, bn) {
tool.Check(err)
panic(fmt.Sprintf("Validation process failed(%d). Expected %b, got %b", iterations, bn, val))
}
}
blockNum++
}
}
func getModifiedAccounts(chaindata string) {
// TODO(tjayrush): The call to GetModifiedAccounts needs a database tx
fmt.Println("hack - getModiiedAccounts is temporarily disabled.")
// db := ethdb.MustOpen(chaindata)
// defer db.Close()
// addrs, err := ethdb.GetModifiedAccounts(db, 49300, 49400)
// check(err)
// fmt.Printf("Len(addrs)=%d\n", len(addrs))
}
type Receiver struct {
defaultReceiver *trie.DefaultReceiver
accountMap map[string]*accounts.Account
storageMap map[string][]byte
unfurlList []string
currentIdx int
}
func (r *Receiver) Root() common.Hash { panic("don't call me") }
func (r *Receiver) Receive(
itemType trie.StreamItem,
accountKey []byte,
storageKey []byte,
accountValue *accounts.Account,
storageValue []byte,
hash []byte,
hasTree bool,
cutoff int,
) error {
for r.currentIdx < len(r.unfurlList) {
ks := r.unfurlList[r.currentIdx]
k := []byte(ks)
var c int
switch itemType {
case trie.StorageStreamItem, trie.SHashStreamItem:
c = bytes.Compare(k, storageKey)
case trie.AccountStreamItem, trie.AHashStreamItem:
c = bytes.Compare(k, accountKey)
case trie.CutoffStreamItem:
c = -1
}
if c > 0 {
return r.defaultReceiver.Receive(itemType, accountKey, storageKey, accountValue, storageValue, hash, hasTree, cutoff)
}
if len(k) > common.HashLength {
v := r.storageMap[ks]
if len(v) > 0 {
if err := r.defaultReceiver.Receive(trie.StorageStreamItem, nil, k, nil, v, nil, hasTree, 0); err != nil {
return err
}
}
} else {
v := r.accountMap[ks]
if v != nil {
if err := r.defaultReceiver.Receive(trie.AccountStreamItem, k, nil, v, nil, nil, hasTree, 0); err != nil {
return err
}
}
}
r.currentIdx++
if c == 0 {
return nil
}
}
// We ran out of modifications, simply pass through
return r.defaultReceiver.Receive(itemType, accountKey, storageKey, accountValue, storageValue, hash, hasTree, cutoff)
}
func (r *Receiver) Result() trie.SubTries {
return r.defaultReceiver.Result()
}
func regenerate(chaindata string) error {
db := ethdb.MustOpenKV(chaindata)
defer db.Close()
tx, err := db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
tool.Check(stagedsync.ResetIH(tx))
to, err := stages.GetStageProgress(tx, stages.HashState)
if err != nil {
return err
}
hash, err := rawdb.ReadCanonicalHash(tx, to)
if err != nil {
return err
}
syncHeadHeader := rawdb.ReadHeader(tx, hash, to)
expectedRootHash := syncHeadHeader.Root
_, err = stagedsync.RegenerateIntermediateHashes("", tx, stagedsync.StageTrieCfg(db, true, true, ""), expectedRootHash, nil)
tool.Check(err)
log.Info("Regeneration ended")
return tx.Commit()
}
func testGetProof(chaindata string, address common.Address, rewind int, regen bool) error {
if regen {
if err := regenerate(chaindata); err != nil {
return err
}
}
storageKeys := []string{}
var m runtime.MemStats
runtime.ReadMemStats(&m)
db := ethdb.MustOpenKV(chaindata)
defer db.Close()
tx, err1 := db.BeginRo(context.Background())
if err1 != nil {
return err1
}
defer tx.Rollback()
headHash := rawdb.ReadHeadBlockHash(tx)
headNumber := rawdb.ReadHeaderNumber(tx, headHash)
block := *headNumber - uint64(rewind)
log.Info("GetProof", "address", address, "storage keys", len(storageKeys), "head", *headNumber, "block", block,
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC))
ts := dbutils.EncodeBlockNumber(block + 1)
accountMap := make(map[string]*accounts.Account)
if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.AccountChangeSetBucket, ts, 0, func(blockN uint64, address, v []byte) (bool, error) {
if blockN > *headNumber {
return false, nil
}
var addrHash, err = common.HashData(address)
if err != nil {
return false, err
}
k := addrHash[:]
if _, ok := accountMap[string(k)]; !ok {
if len(v) > 0 {
var a accounts.Account
if innerErr := a.DecodeForStorage(v); innerErr != nil {
return false, innerErr
}
accountMap[string(k)] = &a
} else {
accountMap[string(k)] = nil
}
}
return true, nil
}); err != nil {
return err
}
runtime.ReadMemStats(&m)
log.Info("Constructed account map", "size", len(accountMap),
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC))
storageMap := make(map[string][]byte)
if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.StorageChangeSetBucket, ts, 0, func(blockN uint64, address, v []byte) (bool, error) {
if blockN > *headNumber {
return false, nil
}
var addrHash, err = common.HashData(address)
if err != nil {
return false, err
}
k := addrHash[:]
if _, ok := storageMap[string(k)]; !ok {
storageMap[string(k)] = v
}
return true, nil
}); err != nil {
return err
}
runtime.ReadMemStats(&m)
log.Info("Constructed storage map", "size", len(storageMap),
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC))
var unfurlList = make([]string, len(accountMap)+len(storageMap))
unfurl := trie.NewRetainList(0)
i := 0
for ks, acc := range accountMap {
unfurlList[i] = ks
i++
unfurl.AddKey([]byte(ks))
if acc != nil {
// Fill the code hashes
if acc.Incarnation > 0 && acc.IsEmptyCodeHash() {
if codeHash, err1 := tx.GetOne(dbutils.ContractCodeBucket, dbutils.GenerateStoragePrefix([]byte(ks), acc.Incarnation)); err1 == nil {
copy(acc.CodeHash[:], codeHash)
} else {
return err1
}
}
}
}
for ks := range storageMap {
unfurlList[i] = ks
i++
unfurl.AddKey([]byte(ks))
}
rl := trie.NewRetainList(0)
addrHash, err := common.HashData(address[:])
if err != nil {
return err
}
rl.AddKey(addrHash[:])
unfurl.AddKey(addrHash[:])
for _, key := range storageKeys {
keyAsHash := common.HexToHash(key)
if keyHash, err1 := common.HashData(keyAsHash[:]); err1 == nil {
//TODO Add incarnation in the middle of this
trieKey := append(addrHash[:], keyHash[:]...)
rl.AddKey(trieKey)
unfurl.AddKey(trieKey)
} else {
return err1
}
}
sort.Strings(unfurlList)
runtime.ReadMemStats(&m)
log.Info("Constructed account unfurl lists",
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC))
loader := trie.NewFlatDBTrieLoader("checkRoots")
if err = loader.Reset(unfurl, nil, nil, false); err != nil {
panic(err)
}
_, err = loader.CalcTrieRoot(tx.(ethdb.HasTx).Tx(), nil, nil)
if err != nil {
return err
}
r := &Receiver{defaultReceiver: trie.NewDefaultReceiver(), unfurlList: unfurlList, accountMap: accountMap, storageMap: storageMap}
r.defaultReceiver.Reset(rl, nil /* HashCollector */, false)
loader.SetStreamReceiver(r)
root, err := loader.CalcTrieRoot(tx.(ethdb.HasTx).Tx(), nil, nil)
if err != nil {
return err
}
runtime.ReadMemStats(&m)
log.Info("Loaded subtries",
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC))
hash, err := rawdb.ReadCanonicalHash(tx, block)
tool.Check(err)
header := rawdb.ReadHeader(tx, hash, block)
runtime.ReadMemStats(&m)
log.Info("Constructed trie",
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC))
fmt.Printf("Resulting root: %x, expected root: %x\n", root, header.Root)
return nil
}
func dumpAddresses(chaindata string) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
f, err := os.Create("addresses")
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
defer w.Flush()
stAccounts := 0
stStorage := 0
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
return err
}
k, _, e := c.First()
for ; k != nil && e == nil; k, _, e = c.Next() {
if len(k) > 28 {
stStorage++
} else {
stAccounts++
if _, err1 := w.Write(k[:20]); err1 != nil {
return err1
}
}
if (stStorage+stAccounts)%100000 == 0 {
fmt.Printf("State records: %d\n", stStorage+stAccounts)
}
}
return e
}); err != nil {
return err
}
fmt.Printf("stAccounts = %d, stStorage = %d\n", stAccounts, stStorage)
return nil
}
func changeSetStats(chaindata string, block1, block2 uint64) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
fmt.Printf("State stats\n")
stAccounts := 0
stStorage := 0
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
return err
}
k, _, e := c.First()
for ; k != nil && e == nil; k, _, e = c.Next() {
if len(k) > 28 {
stStorage++
} else {
stAccounts++
}
if (stStorage+stAccounts)%100000 == 0 {
fmt.Printf("State records: %d\n", stStorage+stAccounts)
}
}
return e
}); err != nil {
return err
}
fmt.Printf("stAccounts = %d, stStorage = %d\n", stAccounts, stStorage)
fmt.Printf("Changeset stats from %d to %d\n", block1, block2)
accounts := make(map[string]struct{})
tx, err1 := db.Begin(context.Background(), ethdb.RW)
if err1 != nil {
return err1
}
defer tx.Rollback()
if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.AccountChangeSetBucket, dbutils.EncodeBlockNumber(block1), 0, func(blockN uint64, k, v []byte) (bool, error) {
if blockN >= block2 {
return false, nil
}
if (blockN-block1)%100000 == 0 {
fmt.Printf("at the block %d for accounts, booster size: %d\n", blockN, len(accounts))
}
accounts[string(common.CopyBytes(k))] = struct{}{}
return true, nil
}); err != nil {
return err
}
storage := make(map[string]struct{})
if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.StorageChangeSetBucket, dbutils.EncodeBlockNumber(block1), 0, func(blockN uint64, k, v []byte) (bool, error) {
if blockN >= block2 {
return false, nil
}
if (blockN-block1)%100000 == 0 {
fmt.Printf("at the block %d for accounts, booster size: %d\n", blockN, len(accounts))
}
storage[string(common.CopyBytes(k))] = struct{}{}
return true, nil
}); err != nil {
return err
}
fmt.Printf("accounts changed: %d, storage changed: %d\n", len(accounts), len(storage))
return nil
}
func searchChangeSet(chaindata string, key []byte, block uint64) error {
fmt.Printf("Searching changesets\n")
db := ethdb.MustOpen(chaindata)
defer db.Close()
tx, err1 := db.Begin(context.Background(), ethdb.RW)
if err1 != nil {
return err1
}
defer tx.Rollback()
if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.AccountChangeSetBucket, dbutils.EncodeBlockNumber(block), 0, func(blockN uint64, k, v []byte) (bool, error) {
if bytes.Equal(k, key) {
fmt.Printf("Found in block %d with value %x\n", blockN, v)
}
return true, nil
}); err != nil {
return err
}
return nil
}
func searchStorageChangeSet(chaindata string, key []byte, block uint64) error {
fmt.Printf("Searching storage changesets\n")
db := ethdb.MustOpen(chaindata)
defer db.Close()
tx, err1 := db.Begin(context.Background(), ethdb.RW)
if err1 != nil {
return err1
}
defer tx.Rollback()
if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.StorageChangeSetBucket, dbutils.EncodeBlockNumber(block), 0, func(blockN uint64, k, v []byte) (bool, error) {
if bytes.Equal(k, key) {
fmt.Printf("Found in block %d with value %x\n", blockN, v)
}
return true, nil
}); err != nil {
return err
}
return nil
}
func supply(chaindata string) error {
startTime := time.Now()
db := ethdb.MustOpen(chaindata)
defer db.Close()
count := 0
supply := uint256.NewInt()
var a accounts.Account
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
return err
}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
if len(k) != 20 {
continue
}
if err1 := a.DecodeForStorage(v); err1 != nil {
return err1
}
count++
supply.Add(supply, &a.Balance)
if count%100000 == 0 {
fmt.Printf("Processed %dK account records\n", count/1000)
}
}
return nil
}); err != nil {
return err
}
fmt.Printf("Total accounts: %d, supply: %d, took: %s\n", count, supply, time.Since(startTime))
return nil
}
func extractCode(chaindata string) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
var contractCount int
if err1 := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.CodeBucket)
if err != nil {
return err
}
// This is a mapping of CodeHash => Byte code
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
fmt.Printf("%x,%x", k, v)
contractCount++
}
return nil
}); err1 != nil {
return err1
}
fmt.Fprintf(os.Stderr, "contractCount: %d\n", contractCount)
return nil
}
func iterateOverCode(chaindata string) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
var contractCount int
var contractKeyTotalLength int
var contractValTotalLength int
var codeHashTotalLength int
var codeTotalLength int // Total length of all byte code (just to illustrate iterating)
if err1 := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.PlainContractCodeBucket)
if err != nil {
return err
}
// This is a mapping of contractAddress + incarnation => CodeHash
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
contractKeyTotalLength += len(k)
contractValTotalLength += len(v)
}
c, err = tx.Cursor(dbutils.CodeBucket)
if err != nil {
return err
}
// This is a mapping of CodeHash => Byte code
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
codeHashTotalLength += len(k)
codeTotalLength += len(v)
contractCount++
}
return nil
}); err1 != nil {
return err1
}
fmt.Printf("contractCount: %d,contractKeyTotalLength: %d, contractValTotalLength: %d, codeHashTotalLength: %d, codeTotalLength: %d\n",
contractCount, contractKeyTotalLength, contractValTotalLength, codeHashTotalLength, codeTotalLength)
return nil
}
func mint(chaindata string, block uint64) error {
f, err := os.Create("mint.csv")
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
defer w.Flush()
db := ethdb.MustOpen(chaindata).RwKV()
defer db.Close()
tx, err := db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
//chiTokenAddr = common.HexToAddress("0x0000000000004946c0e9F43F4Dee607b0eF1fA1c")
//mintFuncPrefix = common.FromHex("0xa0712d68")
var gwei uint256.Int
gwei.SetUint64(1000000000)
blockEncoded := dbutils.EncodeBlockNumber(block)
canonical := make(map[common.Hash]struct{})
c, err := tx.Cursor(dbutils.HeaderCanonicalBucket)
if err != nil {
return err
}
// This is a mapping of contractAddress + incarnation => CodeHash
for k, v, err := c.Seek(blockEncoded); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
// Skip non relevant records
canonical[common.BytesToHash(v)] = struct{}{}
if len(canonical)%100_000 == 0 {
log.Info("Read canonical hashes", "count", len(canonical))
}
}
log.Info("Read canonical hashes", "count", len(canonical))
c, err = tx.Cursor(dbutils.BlockBodyPrefix)
if err != nil {
return err
}
var prevBlock uint64
var burntGas uint64
for k, _, err := c.Seek(blockEncoded); k != nil; k, _, err = c.Next() {
if err != nil {
return err
}
blockNumber := binary.BigEndian.Uint64(k[:8])
blockHash := common.BytesToHash(k[8:])
if _, isCanonical := canonical[blockHash]; !isCanonical {
continue
}
if blockNumber != prevBlock && blockNumber != prevBlock+1 {
fmt.Printf("Gap [%d-%d]\n", prevBlock, blockNumber-1)
}
prevBlock = blockNumber
body := rawdb.ReadBody(tx, blockHash, blockNumber)
header := rawdb.ReadHeader(tx, blockHash, blockNumber)
senders, errSenders := rawdb.ReadSenders(tx, blockHash, blockNumber)
if errSenders != nil {
return errSenders
}
var ethSpent uint256.Int
var ethSpentTotal uint256.Int
var totalGas uint256.Int
count := 0
for i, tx := range body.Transactions {
ethSpent.SetUint64(tx.GetGas())
totalGas.Add(&totalGas, &ethSpent)
if senders[i] == header.Coinbase {
continue // Mining pool sending payout potentially with abnormally low fee, skip
}
ethSpent.Mul(&ethSpent, tx.GetPrice())
ethSpentTotal.Add(&ethSpentTotal, &ethSpent)
count++
}
if count > 0 {
ethSpentTotal.Div(&ethSpentTotal, &totalGas)
ethSpentTotal.Div(&ethSpentTotal, &gwei)
gasPrice := ethSpentTotal.Uint64()
burntGas += header.GasUsed
fmt.Fprintf(w, "%d, %d\n", burntGas, gasPrice)
}
if blockNumber%100_000 == 0 {
log.Info("Processed", "blocks", blockNumber)
}
}
return tx.Commit()
}
func extractHashes(chaindata string, blockStep uint64, blockTotal uint64, name string) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
f, err := os.Create(fmt.Sprintf("preverified_hashes_%s.go", name))
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
defer w.Flush()
fmt.Fprintf(w, "package headerdownload\n\n")
fmt.Fprintf(w, "var %sPreverifiedHashes = []string{\n", name)
b := uint64(0)
for b <= blockTotal {
hash, err := rawdb.ReadCanonicalHash(db, b)
if err != nil {
return err
}
if hash == (common.Hash{}) {
break
}
fmt.Fprintf(w, " \"%x\",\n", hash)
b += blockStep
}
b -= blockStep
fmt.Fprintf(w, "}\n\n")
fmt.Fprintf(w, "const %sPreverifiedHeight uint64 = %d\n", name, b)
fmt.Printf("Last block is %d\n", b)
return nil
}
func extractHeaders(chaindata string, blockStep uint64, blockTotal uint64, name string) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
f, err := os.Create(fmt.Sprintf("hard_coded_headers_%s.go", name))
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
defer w.Flush()
fmt.Fprintf(w, "package headerdownload\n\n")
fmt.Fprintf(w, "var %sHardCodedHeaders = []string{\n", name)
b := uint64(0)
i := 0
for {
hash, err := rawdb.ReadCanonicalHash(db, b)
if err != nil {
return err
}
if hash == (common.Hash{}) {
break
}
h := rawdb.ReadHeader(db, hash, b)
fmt.Fprintf(w, " \"")
base64writer := base64.NewEncoder(base64.RawStdEncoding, w)
gz, err := gzip.NewWriterLevel(base64writer, gzip.BestCompression)
if err != nil {
base64writer.Close()
return err
}
if err = rlp.Encode(gz, h); err != nil {
base64writer.Close()
gz.Close()
return err
}
gz.Close()
base64writer.Close()
fmt.Fprintf(w, "\",\n")
b += blockStep
i++
if b > blockTotal {
break
}
}
fmt.Fprintf(w, "}\n")
fmt.Printf("Last block is %d\n", b)
hash := rawdb.ReadHeadHeaderHash(db)
h, err := rawdb.ReadHeaderByHash(db, hash)
if err != nil {
return err
}
fmt.Printf("Latest header timestamp: %d, current time: %d\n", h.Time, uint64(time.Now().Unix()))
return nil
}
func extractBodies(chaindata string, block uint64) error {
db := ethdb.MustOpen(chaindata).RwKV()
defer db.Close()
tx, err := db.BeginRo(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
c, err := tx.(ethdb.HasTx).Tx().Cursor(dbutils.BlockBodyPrefix)
if err != nil {
return err
}
defer c.Close()
blockEncoded := dbutils.EncodeBlockNumber(block)
for k, _, err := c.Seek(blockEncoded); k != nil; k, _, err = c.Next() {
if err != nil {
return err
}
blockNumber := binary.BigEndian.Uint64(k[:8])
blockHash := common.BytesToHash(k[8:])
body := rawdb.ReadBody(tx, blockHash, blockNumber)
b, err := rlp.EncodeToBytes(body)
if err != nil {
return err
}
fmt.Printf("Body %d %x: %x\n", blockNumber, blockHash, b)
header := rawdb.ReadHeader(tx, blockHash, blockNumber)
b, err = rlp.EncodeToBytes(header)
if err != nil {
return err
}
fmt.Printf("Header %d %x: %x\n", blockNumber, blockHash, b)
if blockNumber > block+5 {
break
}
}
return nil
}
func applyBlock(chaindata string, hash common.Hash) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
f, err := os.Open("out.txt")
if err != nil {
return err
}
defer f.Close()
r := bufio.NewReader(f)
s := bufio.NewScanner(r)
s.Buffer(nil, 20000000)
count := 0
var body *types.Body
var header *types.Header
for s.Scan() {
fields := strings.Split(s.Text(), " ")
h := common.HexToHash(fields[2][:64])
if h != hash {
continue
}
switch fields[0] {
case "Body":
if err = rlp.DecodeBytes(common.FromHex(fields[3]), &body); err != nil {
return nil
}
case "Header":
if err = rlp.DecodeBytes(common.FromHex(fields[3]), &header); err != nil {
return nil
}
}
count++
}
if s.Err() != nil {
return s.Err()
}
fmt.Printf("Lines: %d\n", count)
if body == nil {
fmt.Printf("block body with given hash %x not found\n", hash)
return nil
}
if header == nil {
fmt.Printf("header with given hash not found\n")
return nil
}
block := types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
fmt.Printf("Formed block %d %x\n", block.NumberU64(), block.Hash())
if _, err = stagedsync.InsertBlockInStages(db, params.MainnetChainConfig, &vm.Config{}, ethash.NewFaker(), block, true /* checkRoot */); err != nil {
return err
}
if err = rawdb.WriteCanonicalHash(db, hash, block.NumberU64()); err != nil {
return err
}
return nil
}
func fixUnwind(chaindata string) error {
contractAddr := common.HexToAddress("0x577a32aa9c40cf4266e49fc1e44c749c356309bd")
db := ethdb.MustOpen(chaindata)
defer db.Close()
i, err := db.GetOne(dbutils.IncarnationMapBucket, contractAddr[:])
if err != nil {
return err
} else if i == nil {
fmt.Print("Not found\n")
var b [8]byte
binary.BigEndian.PutUint64(b[:], 1)
if err = db.Put(dbutils.IncarnationMapBucket, contractAddr[:], b[:]); err != nil {
return err
}
} else {
fmt.Printf("Inc: %x\n", i)
}
return nil
}
func snapSizes(chaindata string) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
dbtx, err := db.Begin(context.Background(), ethdb.RO)
if err != nil {
return err
}
defer dbtx.Rollback()
tx := dbtx.(ethdb.HasTx).Tx()
c, _ := tx.Cursor(dbutils.CliqueSeparateBucket)
defer c.Close()
sizes := make(map[int]int)
differentValues := make(map[string]struct{})
var (
total uint64
k, v []byte
)
for k, v, err = c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
sizes[len(v)]++
differentValues[string(v)] = struct{}{}
total += uint64(len(v) + len(k))
}
var lens = make([]int, len(sizes))
i := 0
for l := range sizes {
lens[i] = l
i++
}
sort.Ints(lens)
for _, l := range lens {
fmt.Printf("%6d - %d\n", l, sizes[l])
}
fmt.Printf("Different keys %d\n", len(differentValues))
fmt.Printf("Total size: %d bytes\n", total)
return nil
}
func post2(client *http.Client, url, request string) ([]byte, *fastjson.Value, error) {
fmt.Printf("Request=%s\n", request)
log.Info("Getting", "url", url, "request", request)
start := time.Now()
r, err := client.Post(url, "application/json", strings.NewReader(request))
if err != nil {
return nil, nil, err
}
defer r.Body.Close()
if r.StatusCode != 200 {
return nil, nil, fmt.Errorf("status %s", r.Status)
}
var buf bytes.Buffer
if _, err = buf.ReadFrom(r.Body); err != nil {
return nil, nil, fmt.Errorf("reading http response: %w", err)
}
var p fastjson.Parser
response := buf.Bytes()
v, err := p.ParseBytes(response)
if err != nil {
return nil, nil, fmt.Errorf("parsing http response: %w", err)
}
log.Info("Got in", "time", time.Since(start).Seconds())
return response, v, nil
}
func compareJsonValues(prefix string, v, vg *fastjson.Value) error {
var vType fastjson.Type = fastjson.TypeNull
var vgType fastjson.Type = fastjson.TypeNull
if v != nil {
vType = v.Type()
}
if vg != nil {
vgType = vg.Type()
}
if vType != vgType {
return fmt.Errorf("different types for prefix %s: %s / %s", prefix, vType.String(), vgType.String())
}
switch vType {
case fastjson.TypeNull:
// Nothing to do
case fastjson.TypeObject:
obj, err := v.Object()
if err != nil {
return fmt.Errorf("convering Erigon val to object at prefix %s: %w", prefix, err)
}
objg, errg := vg.Object()
if errg != nil {
return fmt.Errorf("convering g val to object at prefix %s: %w", prefix, errg)
}
objg.Visit(func(key []byte, vg1 *fastjson.Value) {
if err != nil {
return
}
v1 := obj.Get(string(key))
if v1 == nil && vg1.Type() != fastjson.TypeNull {
err = fmt.Errorf("erigon missing value at prefix: %s", prefix+"."+string(key))
return
}
if e := compareJsonValues(prefix+"."+string(key), v1, vg1); e != nil {
err = e
}
})
if err != nil {
return err
}
// Finding keys that are present in Erigon but missing in G
obj.Visit(func(key []byte, v1 *fastjson.Value) {
if err != nil {
return
}
if objg.Get(string(key)) == nil && v1.Type() != fastjson.TypeNull {
err = fmt.Errorf("g missing value at prefix: %s", prefix+"."+string(key))
return
}
})
if err != nil {
return err
}
case fastjson.TypeArray:
arr, err := v.Array()
if err != nil {
return fmt.Errorf("converting Erigon val to array at prefix %s: %w", prefix, err)
}
arrg, errg := vg.Array()
if errg != nil {
return fmt.Errorf("converting g val to array at prefix %s: %w", prefix, errg)
}
if len(arr) != len(arrg) {
return fmt.Errorf("arrays have different length at prefix %s: %d / %d", prefix, len(arr), len(arrg))
}
for i, item := range arr {
itemg := arrg[i]
if e := compareJsonValues(fmt.Sprintf("%s[%d]", prefix, i), item, itemg); e != nil {
return e
}
}
case fastjson.TypeString:
if v.String() != vg.String() {
return fmt.Errorf("different string values at prefix %s: %s / %s", prefix, v.String(), vg.String())
}
case fastjson.TypeNumber:
i, err := v.Int()
if err != nil {
return fmt.Errorf("converting Erigon val to int at prefix %s: %w", prefix, err)
}
ig, errg := vg.Int()
if errg != nil {
return fmt.Errorf("converting g val to int at prefix %s: %w", prefix, errg)
}
if i != ig {
return fmt.Errorf("different int values at prefix %s: %d / %d", prefix, i, ig)
}
}
return nil
}
func wrongReceipts() error {
dirname := "/Users/alexeysharp/Downloads/txtraces"
dir, err := os.Open(dirname)
if err != nil {
return err
}
defer dir.Close()
var names []string
if names, err = dir.Readdirnames(0); err != nil {
return err
}
var client = &http.Client{
Timeout: time.Second * 600,
}
url := "http://192.168.1.126:8545"
fmt.Printf("Read %d names\n", len(names))
for i, name := range names {
txhash := common.FromHex(name[len("txtrace_") : len(name)-len(".txt")])
fmt.Printf("%x\n", txhash)
request := fmt.Sprintf(`{"jsonrpc":"2.0","method":"debug_traceTransaction","params":["0x%x"],"id":%d}`, txhash, i)
response, v1, err1 := post2(client, url, request)
if err1 != nil {
return err
}
fmt.Printf("Response len = %d\n", len(response))
r := v1.Get("result").Get("structLogs")
file, err2 := ioutil.ReadFile(path.Join(dirname, name))
if err2 != nil {
return err2
}
v2, err3 := fastjson.ParseBytes(file)
if err3 != nil {
return err3
}
err = compareJsonValues("", r, v2)
if err != nil {
fmt.Printf("%v\n", err)
}
}
return nil
}
func advance5(chaindata string) error {
db := ethdb.MustOpenKV(chaindata)
defer db.Close()
tx, err := db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
stage5, err := stages.GetStageProgress(tx, stages.Execution)
if err != nil {
return err
}
log.Info("Stage 5", "progress", stage5)
if err = stages.SaveStageProgress(tx, stages.Execution, stage5+1); err != nil {
return err
}
stage5, err = stages.GetStageProgress(tx, stages.Execution)
if err != nil {
return err
}
log.Info("Stage 5", "changed to", stage5)
if err = tx.Commit(); err != nil {
return err
}
return nil
}
func main() {
flag.Parse()
log.SetupDefaultTerminalLogger(log.Lvl(*verbosity), "", "")
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Error("could not create CPU profile", "error", err)
return
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Error("could not start CPU profile", "error", err)
return
}
defer pprof.StopCPUProfile()
}
var err error
switch *action {
case "cfg":
flow.TestGenCfg()
case "bucketStats":
err = bucketStats(*chaindata)
case "syncChart":
mychart()
case "testBlockHashes":
testBlockHashes(*chaindata, *block, common.HexToHash(*hash))
case "invTree":
invTree("root", "right", "diff", *name)
case "readAccount":
if err := readAccount(*chaindata, common.HexToAddress(*account)); err != nil {
fmt.Printf("Error: %v\n", err)
}
case "nextIncarnation":
nextIncarnation(*chaindata, common.HexToHash(*account))
case "dumpStorage":
dumpStorage()
case "current":
printCurrentBlockNumber(*chaindata)
case "bucket":
printBucket(*chaindata)
case "val-tx-lookup-2":
ValidateTxLookups2(*chaindata)
case "modiAccounts":
getModifiedAccounts(*chaindata)
case "slice":
dbSlice(*chaindata, *bucket, common.FromHex(*hash))
case "getProof":
err = testGetProof(*chaindata, common.HexToAddress(*account), *rewind, false)
case "regenerateIH":
err = regenerate(*chaindata)
case "searchChangeSet":
err = searchChangeSet(*chaindata, common.FromHex(*hash), uint64(*block))
case "searchStorageChangeSet":
err = searchStorageChangeSet(*chaindata, common.FromHex(*hash), uint64(*block))
case "changeSetStats":
err = changeSetStats(*chaindata, uint64(*block), uint64(*block)+uint64(*rewind))
case "supply":
err = supply(*chaindata)
case "extractCode":
err = extractCode(*chaindata)
case "iterateOverCode":
err = iterateOverCode(*chaindata)
case "mint":
err = mint(*chaindata, uint64(*block))
case "extractHeaders":
err = extractHeaders(*chaindata, uint64(*block), uint64(*blockTotal), *name)
case "extractHashes":
err = extractHashes(*chaindata, uint64(*block), uint64(*blockTotal), *name)
case "defrag":
err = db.Defrag()
case "textInfo":
err = db.TextInfo(*chaindata, &strings.Builder{})
case "extractBodies":
err = extractBodies(*chaindata, uint64(*block))
case "applyBlock":
err = applyBlock(*chaindata, common.HexToHash(*hash))
case "fixUnwind":
err = fixUnwind(*chaindata)
case "repairCurrent":
repairCurrent()
case "printBranches":
printBranches(uint64(*block))
case "preimage":
preimage(*chaindata, common.HexToHash(*hash))
case "printFullNodeRLPs":
printFullNodeRLPs()
case "rlpIndices":
rlpIndices()
case "hashFile":
hashFile()
case "trieChart":
trieChart()
case "printTxHashes":
printTxHashes()
case "snapSizes":
err = snapSizes(*chaindata)
case "dumpAddresses":
err = dumpAddresses(*chaindata)
case "wrongReceipts":
err = wrongReceipts()
case "advance5":
err = advance5(*chaindata)
}
if err != nil {
fmt.Printf("Error: %v\n", err)
}
}