Skip to content

Commit 6ed543b

Browse files
committed
allow parallel storage trie loading
1 parent d623b3f commit 6ed543b

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

cmd/migration-checker/main.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes
199199
}
200200
}
201201

202-
func loadMPT(mptTrie *trie.SecureTrie, parallel bool) chan map[string][]byte {
202+
func loadMPT(mptTrie *trie.SecureTrie, top bool) chan map[string][]byte {
203203
startKey := make([]byte, 32)
204204
workers := 1 << 5
205-
if !parallel {
206-
workers = 1
205+
if !top {
206+
workers = 1 << 3
207207
}
208208
step := byte(0xFF) / byte(workers)
209209

@@ -219,9 +219,7 @@ func loadMPT(mptTrie *trie.SecureTrie, parallel bool) chan map[string][]byte {
219219
go func() {
220220
defer mptWg.Done()
221221
for trieIt.Next() {
222-
if parallel {
223-
mptLeafMutex.Lock()
224-
}
222+
mptLeafMutex.Lock()
225223

226224
if _, ok := mptLeafMap[string(trieIt.Key)]; ok {
227225
mptLeafMutex.Unlock()
@@ -230,11 +228,9 @@ func loadMPT(mptTrie *trie.SecureTrie, parallel bool) chan map[string][]byte {
230228

231229
mptLeafMap[string(dup(trieIt.Key))] = dup(trieIt.Value)
232230

233-
if parallel {
234-
mptLeafMutex.Unlock()
235-
}
231+
mptLeafMutex.Unlock()
236232

237-
if parallel && len(mptLeafMap)%10000 == 0 {
233+
if top && len(mptLeafMap)%10000 == 0 {
238234
fmt.Println("MPT Accounts Loaded:", len(mptLeafMap))
239235
}
240236
}
@@ -249,7 +245,7 @@ func loadMPT(mptTrie *trie.SecureTrie, parallel bool) chan map[string][]byte {
249245
return respChan
250246
}
251247

252-
func loadZkTrie(zkTrie *trie.ZkTrie, parallel, paranoid bool) chan map[string][]byte {
248+
func loadZkTrie(zkTrie *trie.ZkTrie, top, paranoid bool) chan map[string][]byte {
253249
zkLeafMap := make(map[string][]byte, 1000)
254250
var zkLeafMutex sync.Mutex
255251
zkDone := make(chan map[string][]byte)
@@ -260,20 +256,16 @@ func loadZkTrie(zkTrie *trie.ZkTrie, parallel, paranoid bool) chan map[string][]
260256
panic(fmt.Sprintf("preimage not found zk trie %s", hex.EncodeToString(key)))
261257
}
262258

263-
if parallel {
264-
zkLeafMutex.Lock()
265-
}
259+
zkLeafMutex.Lock()
266260

267261
zkLeafMap[string(dup(preimageKey))] = value
268262

269-
if parallel {
270-
zkLeafMutex.Unlock()
271-
}
263+
zkLeafMutex.Unlock()
272264

273-
if parallel && len(zkLeafMap)%10000 == 0 {
265+
if top && len(zkLeafMap)%10000 == 0 {
274266
fmt.Println("ZK Accounts Loaded:", len(zkLeafMap))
275267
}
276-
}, parallel, paranoid)
268+
}, top, paranoid)
277269
zkDone <- zkLeafMap
278270
}()
279271
return zkDone

trie/zk_trie.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ func (t *ZkTrie) Witness() map[string]struct{} {
239239
panic("not implemented")
240240
}
241241

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 {
243243
root, err := t.ZkTrie.Tree().Root()
244244
if err != nil {
245245
panic("CountLeaves cannot get root")
246246
}
247-
return t.countLeaves(root, cb, 0, parallel, verifyNodeHashes)
247+
return t.countLeaves(root, cb, 0, top, verifyNodeHashes)
248248
}
249249

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 {
251251
if root == nil {
252252
return 0
253253
}
@@ -271,19 +271,24 @@ func (t *ZkTrie) countLeaves(root *zkt.Hash, cb func(key, value []byte), depth i
271271
cb(append([]byte{}, rootNode.NodeKey.Bytes()...), append([]byte{}, rootNode.Data()...))
272272
return 1
273273
} else {
274-
if parallel && depth < 5 {
274+
parallelismDepth := 5
275+
if !top {
276+
parallelismDepth = 3
277+
}
278+
279+
if depth < parallelismDepth {
275280
count := make(chan uint64)
276281
leftT := t.Copy()
277282
rightT := t.Copy()
278283
go func() {
279-
count <- leftT.countLeaves(rootNode.ChildL, cb, depth+1, parallel, verifyNodeHashes)
284+
count <- leftT.countLeaves(rootNode.ChildL, cb, depth+1, top, verifyNodeHashes)
280285
}()
281286
go func() {
282-
count <- rightT.countLeaves(rootNode.ChildR, cb, depth+1, parallel, verifyNodeHashes)
287+
count <- rightT.countLeaves(rootNode.ChildR, cb, depth+1, top, verifyNodeHashes)
283288
}()
284289
return <-count + <-count
285290
} 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)
287292
}
288293
}
289294
}

0 commit comments

Comments
 (0)