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
3637type 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
139137func (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