Skip to content

Commit

Permalink
cmd/geth: adapt pbss
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnss committed Dec 26, 2023
1 parent e2a7dea commit 530d566
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
17 changes: 16 additions & 1 deletion cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/ethereum/go-ethereum/internal/flags"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -383,13 +384,27 @@ func inspectTrie(ctx *cli.Context) error {
log.Error("Empty root hash")
}
fmt.Printf("ReadBlockHeader, root: %v, blocknum: %v\n", trieRootHash, blockNumber)
triedb := trie.NewDatabase(db, nil)

dbScheme := rawdb.ReadStateScheme(db)
var config *trie.Config
if dbScheme == rawdb.PathScheme {
config = &trie.Config{
PathDB: pathdb.ReadOnly,
}
} else if dbScheme == rawdb.HashScheme {
config = trie.HashDefaults
}

triedb := trie.NewDatabase(db, config)
theTrie, err := trie.New(trie.TrieID(trieRootHash), triedb)
if err != nil {
fmt.Printf("fail to new trie tree, err: %v, rootHash: %v\n", err, trieRootHash.String())
return err
}
theInspect, err := trie.NewInspector(theTrie, triedb, trieRootHash, blockNumber, jobnum)
if err != nil {
return err
}
theInspect.Run()
theInspect.DisplayResult()
}
Expand Down
17 changes: 6 additions & 11 deletions trie/inspect_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Inspector struct {
stateRootHash common.Hash
blocknum uint64
root node // root of triedb
num uint64 // block number
result *TotalTrieTreeStat // inspector result
totalNum uint64
concurrentQueue chan struct{}
Expand Down Expand Up @@ -195,7 +194,6 @@ func (inspect *Inspector) SubConcurrentTraversal(theTrie *Trie, theTrieTreeStat
inspect.ConcurrentTraversal(theTrie, theTrieTreeStat, theNode, height, path)
<-inspect.concurrentQueue
inspect.wg.Done()
return
}

func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *TrieTreeStat, theNode node, height uint32, path []byte) {
Expand All @@ -212,25 +210,24 @@ func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *Tr

switch current := (theNode).(type) {
case *shortNode:
path = append(path, current.Key...)
inspect.ConcurrentTraversal(theTrie, theTrieTreeStat, current.Val, height+1, path)
path = path[:len(path)-len(current.Key)]
inspect.ConcurrentTraversal(theTrie, theTrieTreeStat, current.Val, height, append(path, current.Key...))
case *fullNode:
for idx, child := range current.Children {
if child == nil {
continue
}
childPath := path
childPath = append(childPath, byte(idx))
childPath := append(path, byte(idx))
if len(inspect.concurrentQueue)*2 < cap(inspect.concurrentQueue) {
inspect.wg.Add(1)
go inspect.SubConcurrentTraversal(theTrie, theTrieTreeStat, child, height+1, childPath)
dst := make([]byte, len(childPath))
copy(dst, childPath)
go inspect.SubConcurrentTraversal(theTrie, theTrieTreeStat, child, height+1, dst)
} else {
inspect.ConcurrentTraversal(theTrie, theTrieTreeStat, child, height+1, childPath)
}
}
case hashNode:
n, err := theTrie.resloveWithoutTrack(current, nil)
n, err := theTrie.resloveWithoutTrack(current, path)
if err != nil {
fmt.Printf("Resolve HashNode error: %v, TrieRoot: %v, Height: %v, Path: %v\n", err, theTrie.Hash().String(), height+1, path)
return
Expand Down Expand Up @@ -266,7 +263,6 @@ func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *Tr
panic(errors.New("Invalid node type to traverse."))
}
theTrieTreeStat.AtomicAdd(theNode, height)
return
}

func (inspect *Inspector) DisplayResult() {
Expand Down Expand Up @@ -304,6 +300,5 @@ func (inspect *Inspector) DisplayResult() {
}
stat, _ := inspect.result.theTrieTreeStats.Get(cntHash[1])
stat.Display(cntHash[1], "ContractTrie")
i++
}
}

0 comments on commit 530d566

Please sign in to comment.