@@ -239,15 +239,15 @@ func (t *ZkTrie) Witness() map[string]struct{} {
239
239
panic ("not implemented" )
240
240
}
241
241
242
- func (t * ZkTrie ) CountLeaves (cb func (key , value []byte ), parallel , verifyNodeHashes bool ) uint64 {
242
+ func (t * ZkTrie ) CountLeaves (cb func (key , value []byte ), top , verifyNodeHashes bool ) uint64 {
243
243
root , err := t .ZkTrie .Tree ().Root ()
244
244
if err != nil {
245
245
panic ("CountLeaves cannot get root" )
246
246
}
247
- return t .countLeaves (root , cb , 0 , parallel , verifyNodeHashes )
247
+ return t .countLeaves (root , cb , 0 , top , verifyNodeHashes )
248
248
}
249
249
250
- func (t * ZkTrie ) countLeaves (root * zkt.Hash , cb func (key , value []byte ), depth int , parallel , verifyNodeHashes bool ) uint64 {
250
+ func (t * ZkTrie ) countLeaves (root * zkt.Hash , cb func (key , value []byte ), depth int , top , verifyNodeHashes bool ) uint64 {
251
251
if root == nil {
252
252
return 0
253
253
}
@@ -271,19 +271,24 @@ func (t *ZkTrie) countLeaves(root *zkt.Hash, cb func(key, value []byte), depth i
271
271
cb (append ([]byte {}, rootNode .NodeKey .Bytes ()... ), append ([]byte {}, rootNode .Data ()... ))
272
272
return 1
273
273
} else {
274
- if parallel && depth < 5 {
274
+ parallelismDepth := 5
275
+ if ! top {
276
+ parallelismDepth = 3
277
+ }
278
+
279
+ if depth < parallelismDepth {
275
280
count := make (chan uint64 )
276
281
leftT := t .Copy ()
277
282
rightT := t .Copy ()
278
283
go func () {
279
- count <- leftT .countLeaves (rootNode .ChildL , cb , depth + 1 , parallel , verifyNodeHashes )
284
+ count <- leftT .countLeaves (rootNode .ChildL , cb , depth + 1 , top , verifyNodeHashes )
280
285
}()
281
286
go func () {
282
- count <- rightT .countLeaves (rootNode .ChildR , cb , depth + 1 , parallel , verifyNodeHashes )
287
+ count <- rightT .countLeaves (rootNode .ChildR , cb , depth + 1 , top , verifyNodeHashes )
283
288
}()
284
289
return <- count + <- count
285
290
} else {
286
- return t .countLeaves (rootNode .ChildL , cb , depth + 1 , parallel , verifyNodeHashes ) + t .countLeaves (rootNode .ChildR , cb , depth + 1 , parallel , verifyNodeHashes )
291
+ return t .countLeaves (rootNode .ChildL , cb , depth + 1 , top , verifyNodeHashes ) + t .countLeaves (rootNode .ChildR , cb , depth + 1 , top , verifyNodeHashes )
287
292
}
288
293
}
289
294
}
0 commit comments