Skip to content

Commit 50b1577

Browse files
fynnssMariusVanDerWijden
authored andcommitted
cmd/dbcmd: fix inspect trie second review
1 parent 4d5cf4c commit 50b1577

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

trie/inspect_trie.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ import (
1818
"golang.org/x/sync/semaphore"
1919
)
2020

21-
const (
22-
// defaultTriedbcacheSize default triedb cache size in hash scheme. To prevent memory leaks.
23-
defaultTriedbcacheSize = 1024 * 1024 * 1024
24-
)
25-
2621
type Inspector struct {
2722
trie *Trie // traverse trie
2823
db *Database
@@ -38,7 +33,7 @@ type Inspector struct {
3833
}
3934

4035
type trieTreeStat struct {
41-
is_account_trie bool
36+
isAccountTrie bool
4237
theNodeStatByLevel [15]nodeStat
4338
totalNodeStat nodeStat
4439
}
@@ -72,16 +67,8 @@ func (trieStat *trieTreeStat) AtomicAdd(theNode node, height uint32) {
7267
}
7368
}
7469

75-
type stringWriter struct {
76-
builder strings.Builder
77-
}
78-
79-
func (sw stringWriter) Write(p []byte) (n int, err error) {
80-
return sw.builder.Write(p)
81-
}
82-
8370
func (trieStat *trieTreeStat) Display(ownerAddress string, treeType string) string {
84-
sw := stringWriter{}
71+
sw := new(strings.Builder)
8572
table := tablewriter.NewWriter(sw)
8673
table.SetHeader([]string{"-", "Level", "ShortNodeCnt", "FullNodeCnt", "ValueNodeCnt"})
8774
if ownerAddress == "" {
@@ -104,7 +91,7 @@ func (trieStat *trieTreeStat) Display(ownerAddress string, treeType string) stri
10491
{"Total", "-", fmt.Sprintf("%d", trieStat.totalNodeStat.ShortNodeCnt.Load()), fmt.Sprintf("%d", trieStat.totalNodeStat.FullNodeCnt.Load()), fmt.Sprintf("%d", trieStat.totalNodeStat.ValueNodeCnt.Load())},
10592
})
10693
table.Render()
107-
return sw.builder.String()
94+
return sw.String()
10895
}
10996

11097
// NewInspector return an inspector obj
@@ -136,28 +123,23 @@ func NewInspector(tr *Trie, db *Database, stateRootHash common.Hash, blocknum ui
136123
// Run statistics, external call
137124
func (inspect *Inspector) Run() {
138125
accountTrieStat := &trieTreeStat{
139-
is_account_trie: true,
126+
isAccountTrie: true,
140127
}
141128

142129
if _, ok := inspect.result[""]; !ok {
143130
inspect.result[""] = accountTrieStat
144131
}
145132
log.Info("Find Account Trie Tree", "rootHash: ", inspect.trie.Hash().String(), "BlockNum: ", inspect.blocknum)
146133

147-
inspect.ConcurrentTraversal(inspect.trie, accountTrieStat, inspect.root, 0, []byte{})
134+
inspect.concurrentTraversal(inspect.trie, accountTrieStat, inspect.root, 0, []byte{})
148135
inspect.wg.Wait()
149136
}
150137

151-
func (inspect *Inspector) SubConcurrentTraversal(theTrie *Trie, theTrieTreeStat *trieTreeStat, theNode node, height uint32, path []byte) {
152-
inspect.ConcurrentTraversal(theTrie, theTrieTreeStat, theNode, height, path)
153-
inspect.wg.Done()
154-
}
155-
156-
func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *trieTreeStat, theNode node, height uint32, path []byte) {
138+
func (inspect *Inspector) concurrentTraversal(theTrie *Trie, theTrieTreeStat *trieTreeStat, theNode node, height uint32, path []byte) {
157139
// print process progress
158-
total_num := atomic.AddUint64(&inspect.totalNum, 1)
159-
if total_num%100000 == 0 {
160-
fmt.Printf("Complete progress: %v, go routines Num: %v\n", total_num, runtime.NumGoroutine())
140+
totalNum := atomic.AddUint64(&inspect.totalNum, 1)
141+
if totalNum%100000 == 0 {
142+
fmt.Printf("Complete progress: %v, go routines Num: %v\n", totalNum, runtime.NumGoroutine())
161143
}
162144

163145
// nil node
@@ -167,7 +149,7 @@ func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *tr
167149

168150
switch current := (theNode).(type) {
169151
case *shortNode:
170-
inspect.ConcurrentTraversal(theTrie, theTrieTreeStat, current.Val, height, append(path, current.Key...))
152+
inspect.concurrentTraversal(theTrie, theTrieTreeStat, current.Val, height, append(path, current.Key...))
171153
case *fullNode:
172154
for idx, child := range current.Children {
173155
if child == nil {
@@ -178,9 +160,12 @@ func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *tr
178160
inspect.wg.Add(1)
179161
dst := make([]byte, len(childPath))
180162
copy(dst, childPath)
181-
go inspect.SubConcurrentTraversal(theTrie, theTrieTreeStat, child, height+1, dst)
163+
go func() {
164+
inspect.concurrentTraversal(theTrie, theTrieTreeStat, theNode, height, path)
165+
inspect.wg.Done()
166+
}()
182167
} else {
183-
inspect.ConcurrentTraversal(theTrie, theTrieTreeStat, child, height+1, childPath)
168+
inspect.concurrentTraversal(theTrie, theTrieTreeStat, child, height+1, childPath)
184169
}
185170
}
186171
case hashNode:
@@ -189,7 +174,7 @@ func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *tr
189174
fmt.Printf("Resolve HashNode error: %v, TrieRoot: %v, Height: %v, Path: %v\n", err, theTrie.Hash().String(), height+1, path)
190175
return
191176
}
192-
inspect.ConcurrentTraversal(theTrie, theTrieTreeStat, n, height, path)
177+
inspect.concurrentTraversal(theTrie, theTrieTreeStat, n, height, path)
193178
return
194179
case valueNode:
195180
if !hasTerm(path) {
@@ -213,7 +198,7 @@ func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *tr
213198
}
214199
contractTrie.tracer.reset()
215200
trieStat := &trieTreeStat{
216-
is_account_trie: false,
201+
isAccountTrie: false,
217202
}
218203

219204
inspect.statLock.Lock()
@@ -224,7 +209,10 @@ func (inspect *Inspector) ConcurrentTraversal(theTrie *Trie, theTrieTreeStat *tr
224209

225210
// log.Info("Find Contract Trie Tree, rootHash: ", contractTrie.Hash().String(), "")
226211
inspect.wg.Add(1)
227-
go inspect.SubConcurrentTraversal(contractTrie, trieStat, contractTrie.root, 0, []byte{})
212+
go func() {
213+
inspect.concurrentTraversal(contractTrie, trieStat, contractTrie.root, 0, []byte{})
214+
inspect.wg.Done()
215+
}()
228216
default:
229217
panic(errors.New("invalid node type for traverse"))
230218
}
@@ -239,12 +227,12 @@ func (inspect *Inspector) DisplayResult() {
239227
}
240228
fmt.Printf(inspect.result[""].Display("", "AccountTrie"))
241229

242-
type SortedTrie struct {
230+
type sortedTrie struct {
243231
totalNum uint64
244232
ownerAddress string
245233
}
246234
// display contract trie
247-
var sortedTriesByNums []SortedTrie
235+
var sortedTriesByNums []sortedTrie
248236
var totalContactsNodeStat nodeStat
249237
var contractTrieCnt uint64 = 0
250238

@@ -257,7 +245,7 @@ func (inspect *Inspector) DisplayResult() {
257245
totalContactsNodeStat.FullNodeCnt.Add(stat.totalNodeStat.FullNodeCnt.Load())
258246
totalContactsNodeStat.ValueNodeCnt.Add(stat.totalNodeStat.ValueNodeCnt.Load())
259247
totalNodeCnt := stat.totalNodeStat.ShortNodeCnt.Load() + stat.totalNodeStat.ValueNodeCnt.Load() + stat.totalNodeStat.FullNodeCnt.Load()
260-
sortedTriesByNums = append(sortedTriesByNums, SortedTrie{totalNum: totalNodeCnt, ownerAddress: ownerAddress})
248+
sortedTriesByNums = append(sortedTriesByNums, sortedTrie{totalNum: totalNodeCnt, ownerAddress: ownerAddress})
261249
}
262250
sort.Slice(sortedTriesByNums, func(i, j int) bool {
263251
return sortedTriesByNums[i].totalNum > sortedTriesByNums[j].totalNum

0 commit comments

Comments
 (0)