@@ -26,7 +26,7 @@ import (
26
26
27
27
var accountsDone atomic.Uint64
28
28
var numLoaders int
29
- var trieLoaders chan struct {}
29
+ var trieLoaders chan dbs
30
30
31
31
type dbs struct {
32
32
zkDb * leveldb.Database
@@ -48,40 +48,37 @@ func main() {
48
48
log .Println (http .ListenAndServe ("0.0.0.0:6060" , nil ))
49
49
}()
50
50
51
- zkDb , err := leveldb .NewCustom (* zkDbPath , "zkDb" , func (options * opt.Options ) {
52
- // Set default options
53
- options .ReadOnly = true
54
- options .BlockCacher = opt .NoCacher
55
- options .BlockCacheCapacity = 0
56
- options .OpenFilesCacher = opt .NoCacher
57
- options .OpenFilesCacheCapacity = 0
58
- options .DisableBlockCache = true
59
- })
60
- panicOnError (err , "" , "failed to open zk db" )
61
- mptDb , err := leveldb .NewCustom (* mptDbPath , "mptDb" , func (options * opt.Options ) {
62
- // Set default options
63
- options .ReadOnly = true
64
- options .BlockCacher = opt .NoCacher
65
- options .BlockCacheCapacity = 0
66
- options .OpenFilesCacher = opt .NoCacher
67
- options .OpenFilesCacheCapacity = 0
68
- options .DisableBlockCache = true
69
- })
70
- panicOnError (err , "" , "failed to open mpt db" )
71
-
72
51
zkRootHash := common .HexToHash (* zkRoot )
73
52
mptRootHash := common .HexToHash (* mptRoot )
74
53
75
54
numLoaders = runtime .GOMAXPROCS (0 ) * (* parallelismMultipler )
76
- trieLoaders = make (chan struct {} , numLoaders )
55
+ trieLoaders = make (chan dbs , numLoaders )
77
56
for i := 0 ; i < numLoaders ; i ++ {
78
- trieLoaders <- struct {}{}
57
+ zkDb , err := leveldb .NewCustom (* zkDbPath , "zkDb" , func (options * opt.Options ) {
58
+ // Set default options
59
+ options .ReadOnly = true
60
+ options .BlockCacher = opt .NoCacher
61
+ options .BlockCacheCapacity = 0
62
+ options .OpenFilesCacheCapacity = 128
63
+ options .DisableBlockCache = true
64
+ })
65
+ panicOnError (err , "" , "failed to open zk db" )
66
+ mptDb , err := leveldb .NewCustom (* mptDbPath , "mptDb" , func (options * opt.Options ) {
67
+ // Set default options
68
+ options .ReadOnly = true
69
+ options .BlockCacher = opt .NoCacher
70
+ options .BlockCacheCapacity = 0
71
+ options .OpenFilesCacheCapacity = 128
72
+ options .DisableBlockCache = true
73
+ })
74
+ panicOnError (err , "" , "failed to open mpt db" )
75
+ trieLoaders <- dbs {
76
+ zkDb : zkDb ,
77
+ mptDb : mptDb ,
78
+ }
79
79
}
80
80
81
- checkTrieEquality (& dbs {
82
- zkDb : zkDb ,
83
- mptDb : mptDb ,
84
- }, zkRootHash , mptRootHash , "" , checkAccountEquality , true , * paranoid )
81
+ checkTrieEquality (zkRootHash , mptRootHash , "" , checkAccountEquality , true , * paranoid )
85
82
86
83
for i := 0 ; i < numLoaders ; i ++ {
87
84
<- trieLoaders
@@ -97,7 +94,7 @@ func panicOnError(err error, label, msg string) {
97
94
func dup (s []byte ) []byte {
98
95
return append ([]byte {}, s ... )
99
96
}
100
- func checkTrieEquality (dbs * dbs , zkRoot , mptRoot common.Hash , label string , leafChecker func (string , * dbs , []byte , []byte , bool ), top , paranoid bool ) {
97
+ func checkTrieEquality (zkRoot , mptRoot common.Hash , label string , leafChecker func (string , []byte , []byte , bool ), top , paranoid bool ) {
101
98
done := make (chan struct {})
102
99
start := time .Now ()
103
100
if ! top {
@@ -114,13 +111,8 @@ func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leaf
114
111
}
115
112
defer close (done )
116
113
117
- zkTrie , err := trie .NewZkTrie (zkRoot , trie .NewZktrieDatabaseFromTriedb (trie .NewDatabaseWithConfig (dbs .zkDb , & trie.Config {Preimages : true })))
118
- panicOnError (err , label , "failed to create zk trie" )
119
- mptTrie , err := trie .NewSecureNoTracer (mptRoot , trie .NewDatabaseWithConfig (dbs .mptDb , & trie.Config {Preimages : true }))
120
- panicOnError (err , label , "failed to create mpt trie" )
121
-
122
- mptLeafCh := loadMPT (mptTrie , top )
123
- zkLeafCh := loadZkTrie (zkTrie , top , paranoid )
114
+ mptLeafCh := loadMPT (mptRoot , top )
115
+ zkLeafCh := loadZkTrie (zkRoot , top , paranoid )
124
116
125
117
mptLeafMap := <- mptLeafCh
126
118
zkLeafMap := <- zkLeafCh
@@ -154,11 +146,11 @@ func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leaf
154
146
panic (fmt .Sprintf ("%s key %s (preimage %s) not found in mpt" , label , hex .EncodeToString (mptKey ), hex .EncodeToString ([]byte (preimageKey ))))
155
147
}
156
148
157
- leafChecker (fmt .Sprintf ("%s key: %s" , label , hex .EncodeToString ([]byte (preimageKey ))), dbs , zkValue , mptVal , paranoid )
149
+ leafChecker (fmt .Sprintf ("%s key: %s" , label , hex .EncodeToString ([]byte (preimageKey ))), zkValue , mptVal , paranoid )
158
150
}
159
151
}
160
152
161
- func checkAccountEquality (label string , dbs * dbs , zkAccountBytes , mptAccountBytes []byte , paranoid bool ) {
153
+ func checkAccountEquality (label string , zkAccountBytes , mptAccountBytes []byte , paranoid bool ) {
162
154
mptAccount := & types.StateAccount {}
163
155
panicOnError (rlp .DecodeBytes (mptAccountBytes , mptAccount ), label , "failed to decode mpt account" )
164
156
zkAccount , err := types .UnmarshalStateAccount (zkAccountBytes )
@@ -181,17 +173,15 @@ func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountByte
181
173
} else if zkAccount .Root != (common.Hash {}) {
182
174
zkRoot := common .BytesToHash (zkAccount .Root [:])
183
175
mptRoot := common .BytesToHash (mptAccount .Root [:])
184
- <- trieLoaders
185
176
go func () {
186
- trieLoaders <- struct {}{}
187
177
defer func () {
188
178
if p := recover (); p != nil {
189
179
fmt .Println (p )
190
180
os .Exit (1 )
191
181
}
192
182
}()
193
183
194
- checkTrieEquality (dbs , zkRoot , mptRoot , label , checkStorageEquality , false , paranoid )
184
+ checkTrieEquality (zkRoot , mptRoot , label , checkStorageEquality , false , paranoid )
195
185
accountsDone .Add (1 )
196
186
fmt .Println ("Accounts done:" , accountsDone .Load ())
197
187
}()
@@ -201,7 +191,7 @@ func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountByte
201
191
}
202
192
}
203
193
204
- func checkStorageEquality (label string , _ * dbs , zkStorageBytes , mptStorageBytes []byte , _ bool ) {
194
+ func checkStorageEquality (label string , zkStorageBytes , mptStorageBytes []byte , _ bool ) {
205
195
zkValue := common .BytesToHash (zkStorageBytes )
206
196
_ , content , _ , err := rlp .Split (mptStorageBytes )
207
197
panicOnError (err , label , "failed to decode mpt storage" )
@@ -211,22 +201,25 @@ func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes
211
201
}
212
202
}
213
203
214
- func loadMPT (mptTrie * trie. SecureTrie , top bool ) chan map [string ][]byte {
204
+ func loadMPT (mptRoot common. Hash , top bool ) chan map [string ][]byte {
215
205
startKey := make ([]byte , 32 )
216
206
mptLeafMap := make (map [string ][]byte , 1000 )
217
207
var mptLeafMutex sync.Mutex
218
208
219
209
var mptWg sync.WaitGroup
220
210
for i := 0 ; i < 255 ; i ++ {
221
211
startKey [0 ] = byte (i )
212
+
213
+ loaderDbs := <- trieLoaders
214
+ mptTrie , err := trie .NewSecureNoTracer (mptRoot , trie .NewDatabaseWithConfig (loaderDbs .mptDb , & trie.Config {Preimages : true }))
215
+ panicOnError (err , mptRoot .Hex (), "failed to create mpt trie" )
222
216
trieIt := trie .NewIterator (mptTrie .NodeIterator (startKey ))
223
217
224
218
mptWg .Add (1 )
225
- <- trieLoaders
226
219
go func () {
227
220
defer func () {
228
221
mptWg .Done ()
229
- trieLoaders <- struct {}{}
222
+ trieLoaders <- loaderDbs
230
223
}()
231
224
for trieIt .Next () {
232
225
mptLeafMutex .Lock ()
@@ -252,12 +245,20 @@ func loadMPT(mptTrie *trie.SecureTrie, top bool) chan map[string][]byte {
252
245
return respChan
253
246
}
254
247
255
- func loadZkTrie (zkTrie * trie. ZkTrie , top , paranoid bool ) chan map [string ][]byte {
248
+ func loadZkTrie (zkRoot common. Hash , top , paranoid bool ) chan map [string ][]byte {
256
249
parallelismCutoffDepth := 8
257
250
zkLeafMap := make (map [string ][]byte , 1000 )
258
251
var zkLeafMutex sync.Mutex
259
252
zkDone := make (chan map [string ][]byte )
260
253
go func () {
254
+ loaderDbs := <- trieLoaders
255
+ defer func () {
256
+ trieLoaders <- loaderDbs
257
+ }()
258
+
259
+ zkTrie , err := trie .NewZkTrie (zkRoot , trie .NewZktrieDatabaseFromTriedb (trie .NewDatabaseWithConfig (loaderDbs .zkDb , & trie.Config {Preimages : true })))
260
+ panicOnError (err , zkRoot .Hex (), "failed to create zk trie" )
261
+
261
262
zkTrie .CountLeaves (func (key , value []byte ) {
262
263
preimageKey := zkTrie .GetKey (key )
263
264
if len (preimageKey ) == 0 {
@@ -272,10 +273,10 @@ func loadZkTrie(zkTrie *trie.ZkTrie, top, paranoid bool) chan map[string][]byte
272
273
fmt .Println ("ZK Accounts Loaded:" , len (zkLeafMap ))
273
274
}
274
275
}, func (f func ()) {
275
- <- trieLoaders
276
+ tempDb := <- trieLoaders
276
277
go func () {
277
278
defer func () {
278
- trieLoaders <- struct {}{}
279
+ trieLoaders <- tempDb
279
280
}()
280
281
f ()
281
282
}()
0 commit comments