Skip to content

Commit b66c822

Browse files
trie: fixed bugs
1 parent 1649310 commit b66c822

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

cmd/geth/dbcmd.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"os/signal"
2525
"path/filepath"
26+
"runtime"
2627
"slices"
2728
"strconv"
2829
"strings"
@@ -316,10 +317,6 @@ func confirmAndRemoveDB(paths []string, kind string, ctx *cli.Context, removeFla
316317
}
317318

318319
func inspectTrie(ctx *cli.Context) error {
319-
if ctx.NArg() < 1 {
320-
return fmt.Errorf("required arguments: %v", ctx.Command.ArgsUsage)
321-
}
322-
323320
if ctx.NArg() > 2 {
324321
return fmt.Errorf("excessive number of arguments: %v", ctx.Command.ArgsUsage)
325322
}
@@ -337,7 +334,7 @@ func inspectTrie(ctx *cli.Context) error {
337334
defer db.Close()
338335

339336
var headerBlockHash common.Hash
340-
if ctx.Args().Get(0) == "latest" {
337+
if ctx.NArg() == 0 || ctx.Args().Get(0) == "latest" {
341338
headerHash := rawdb.ReadHeadHeaderHash(db)
342339
if num := rawdb.ReadHeaderNumber(db, headerHash); num != nil {
343340
blockNumber = *num
@@ -353,13 +350,13 @@ func inspectTrie(ctx *cli.Context) error {
353350
}
354351
}
355352

356-
if ctx.NArg() == 1 {
357-
jobnum = 1000
353+
if ctx.NArg() <= 1 {
354+
jobnum = uint64(runtime.NumCPU())
358355
} else {
359356
var err error
360357
jobnum, err = strconv.ParseUint(ctx.Args().Get(1), 10, 64)
361358
if err != nil {
362-
return fmt.Errorf("failed to Parse jobnum, Args[1]: %v, err: %v", ctx.Args().Get(1), err)
359+
return fmt.Errorf("failed to parse jobnum, Args[1]: %v, err: %v", ctx.Args().Get(1), err)
363360
}
364361
}
365362

@@ -384,12 +381,12 @@ func inspectTrie(ctx *cli.Context) error {
384381
fmt.Printf("fail to new trie tree, err: %v, rootHash: %v\n", err, trieRootHash.String())
385382
return err
386383
}
387-
theInspect, err := trie.NewInspector(theTrie, triedb, trieRootHash, blockNumber, jobnum)
384+
inspector, err := trie.NewInspector(theTrie, triedb, trieRootHash, blockNumber, jobnum)
388385
if err != nil {
389386
return err
390387
}
391-
theInspect.Run()
392-
theInspect.DisplayResult()
388+
inspector.Run()
389+
inspector.DisplayResult()
393390
return nil
394391
}
395392

trie/inspect_trie.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"runtime"
8+
"slices"
89
"sort"
910
"strings"
1011
"sync"
@@ -25,12 +26,12 @@ type Inspector struct {
2526
stateRootHash common.Hash
2627
blocknum uint64
2728
root node // root of triedb
28-
totalNum uint64
29+
totalNum atomic.Uint64
2930
wg sync.WaitGroup
3031
statLock sync.RWMutex
3132
result map[string]*trieTreeStat
3233
sem *semaphore.Weighted
33-
eoaAccountNums uint64
34+
eoaAccountNums atomic.Uint64
3435
}
3536

3637
type trieTreeStat struct {
@@ -106,16 +107,13 @@ func NewInspector(tr *Trie, db database.NodeDatabase, stateRootHash common.Hash,
106107
}
107108

108109
ins := &Inspector{
109-
trie: tr,
110-
db: db,
111-
stateRootHash: stateRootHash,
112-
blocknum: blocknum,
113-
root: tr.root,
114-
result: make(map[string]*trieTreeStat),
115-
totalNum: (uint64)(0),
116-
wg: sync.WaitGroup{},
117-
sem: semaphore.NewWeighted(int64(jobnum)),
118-
eoaAccountNums: 0,
110+
trie: tr,
111+
db: db,
112+
stateRootHash: stateRootHash,
113+
blocknum: blocknum,
114+
root: tr.root,
115+
result: make(map[string]*trieTreeStat),
116+
sem: semaphore.NewWeighted(int64(jobnum)),
119117
}
120118

121119
return ins, nil
@@ -138,7 +136,7 @@ func (inspect *Inspector) Run() {
138136

139137
func (inspect *Inspector) concurrentTraversal(theTrie *Trie, theTrieTreeStat *trieTreeStat, theNode node, height uint32, path []byte) {
140138
// print process progress
141-
totalNum := atomic.AddUint64(&inspect.totalNum, 1)
139+
totalNum := inspect.totalNum.Add(1)
142140
if totalNum%100000 == 0 {
143141
fmt.Printf("Complete progress: %v, go routines Num: %v\n", totalNum, runtime.NumGoroutine())
144142
}
@@ -159,10 +157,8 @@ func (inspect *Inspector) concurrentTraversal(theTrie *Trie, theTrieTreeStat *tr
159157
childPath := append(path, byte(idx))
160158
if inspect.sem.TryAcquire(1) {
161159
inspect.wg.Add(1)
162-
dst := make([]byte, len(childPath))
163-
copy(dst, childPath)
164160
go func() {
165-
inspect.concurrentTraversal(theTrie, theTrieTreeStat, theNode, height, path)
161+
inspect.concurrentTraversal(theTrie, theTrieTreeStat, child, height+1, slices.Clone(childPath))
166162
inspect.wg.Done()
167163
}()
168164
} else {
@@ -186,7 +182,7 @@ func (inspect *Inspector) concurrentTraversal(theTrie *Trie, theTrieTreeStat *tr
186182
break
187183
}
188184
if common.BytesToHash(account.CodeHash) == types.EmptyCodeHash {
189-
inspect.eoaAccountNums++
185+
inspect.eoaAccountNums.Add(1)
190186
}
191187
if account.Root == (common.Hash{}) || account.Root == types.EmptyRootHash {
192188
break
@@ -208,7 +204,6 @@ func (inspect *Inspector) concurrentTraversal(theTrie *Trie, theTrieTreeStat *tr
208204
}
209205
inspect.statLock.Unlock()
210206

211-
// log.Info("Find Contract Trie Tree, rootHash: ", contractTrie.Hash().String(), "")
212207
inspect.wg.Add(1)
213208
go func() {
214209
inspect.concurrentTraversal(contractTrie, trieStat, contractTrie.root, 0, []byte{})
@@ -226,7 +221,7 @@ func (inspect *Inspector) DisplayResult() {
226221
log.Info("Display result error", "missing account trie")
227222
return
228223
}
229-
fmt.Printf(inspect.result[""].Display("", "AccountTrie"))
224+
fmt.Print(inspect.result[""].Display("", "AccountTrie"))
230225

231226
type sortedTrie struct {
232227
totalNum uint64
@@ -251,7 +246,7 @@ func (inspect *Inspector) DisplayResult() {
251246
sort.Slice(sortedTriesByNums, func(i, j int) bool {
252247
return sortedTriesByNums[i].totalNum > sortedTriesByNums[j].totalNum
253248
})
254-
fmt.Println("EOA accounts num: ", inspect.eoaAccountNums)
249+
fmt.Println("EOA accounts num: ", inspect.eoaAccountNums.Load())
255250
// only display top 5
256251
for i, t := range sortedTriesByNums {
257252
if i > 5 {
@@ -260,7 +255,7 @@ func (inspect *Inspector) DisplayResult() {
260255
if stat, ok := inspect.result[t.ownerAddress]; !ok {
261256
log.Error("Storage trie stat not found", "ownerAddress", t.ownerAddress)
262257
} else {
263-
fmt.Printf(stat.Display(t.ownerAddress, "ContractTrie"))
258+
fmt.Print(stat.Display(t.ownerAddress, "ContractTrie"))
264259
}
265260
}
266261
fmt.Printf("Contract Trie, total trie num: %v, ShortNodeCnt: %v, FullNodeCnt: %v, ValueNodeCnt: %v\n",

0 commit comments

Comments
 (0)