@@ -7,13 +7,13 @@ import (
7
7
"fmt"
8
8
"os"
9
9
"runtime"
10
+ "sort"
10
11
"sync"
11
12
"sync/atomic"
12
13
"time"
13
14
14
15
"github.com/scroll-tech/go-ethereum/common"
15
16
"github.com/scroll-tech/go-ethereum/core/types"
16
- "github.com/scroll-tech/go-ethereum/crypto"
17
17
"github.com/scroll-tech/go-ethereum/ethdb/leveldb"
18
18
"github.com/scroll-tech/go-ethereum/rlp"
19
19
"github.com/scroll-tech/go-ethereum/trie"
@@ -110,39 +110,16 @@ func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leaf
110
110
mptLeafCh := loadMPT (mptTrie , top )
111
111
zkLeafCh := loadZkTrie (zkTrie , top , paranoid )
112
112
113
- mptLeafMap := <- mptLeafCh
114
- zkLeafMap := <- zkLeafCh
113
+ mptLeafs := <- mptLeafCh
114
+ zkLeafs := <- zkLeafCh
115
115
116
- if len (mptLeafMap ) != len (zkLeafMap ) {
117
- panic (fmt .Sprintf ("%s MPT and ZK trie leaf count mismatch: MPT: %d, ZK: %d" , label , len (mptLeafMap ), len (zkLeafMap )))
116
+ if len (mptLeafs ) != len (zkLeafs ) {
117
+ panic (fmt .Sprintf ("%s MPT and ZK trie leaf count mismatch: MPT: %d, ZK: %d" , label , len (mptLeafs ), len (zkLeafs )))
118
118
}
119
119
120
- for preimageKey , zkValue := range zkLeafMap {
121
- if top {
122
- // ZkTrie pads preimages with 0s to make them 32 bytes.
123
- // So we might need to clear those zeroes here since we need 20 byte addresses at top level (ie state trie)
124
- if len (preimageKey ) > 20 {
125
- for _ , b := range []byte (preimageKey )[20 :] {
126
- if b != 0 {
127
- panic (fmt .Sprintf ("%s padded byte is not 0 (preimage %s)" , label , hex .EncodeToString ([]byte (preimageKey ))))
128
- }
129
- }
130
- preimageKey = preimageKey [:20 ]
131
- }
132
- } else if len (preimageKey ) != 32 {
133
- // storage leafs should have 32 byte keys, pad them if needed
134
- zeroes := make ([]byte , 32 )
135
- copy (zeroes , []byte (preimageKey ))
136
- preimageKey = string (zeroes )
137
- }
138
-
139
- mptKey := crypto .Keccak256 ([]byte (preimageKey ))
140
- mptVal , ok := mptLeafMap [string (mptKey )]
141
- if ! ok {
142
- panic (fmt .Sprintf ("%s key %s (preimage %s) not found in mpt" , label , hex .EncodeToString (mptKey ), hex .EncodeToString ([]byte (preimageKey ))))
143
- }
144
-
145
- leafChecker (fmt .Sprintf ("%s key: %s" , label , hex .EncodeToString ([]byte (preimageKey ))), dbs , zkValue , mptVal , paranoid )
120
+ for index , zkKv := range zkLeafs {
121
+ mptKv := mptLeafs [index ]
122
+ leafChecker (fmt .Sprintf ("%s key: %s" , label , hex .EncodeToString ([]byte (zkKv .key ))), dbs , zkKv .value , mptKv .value , paranoid )
146
123
}
147
124
}
148
125
@@ -199,74 +176,103 @@ func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes
199
176
}
200
177
}
201
178
202
- func loadMPT (mptTrie * trie.SecureTrie , top bool ) chan map [string ][]byte {
179
+ type kv struct {
180
+ key , value []byte
181
+ }
182
+
183
+ func loadMPT (mptTrie * trie.SecureTrie , top bool ) chan []kv {
203
184
startKey := make ([]byte , 32 )
204
185
workers := 1 << 5
205
186
if ! top {
206
187
workers = 1 << 3
207
188
}
208
189
step := byte (0xFF ) / byte (workers )
209
190
210
- mptLeafMap := make (map [ string ][] byte , 1000 )
191
+ mptLeafs := make ([] kv , 0 , 1000 )
211
192
var mptLeafMutex sync.Mutex
212
193
213
194
var mptWg sync.WaitGroup
214
195
for i := 0 ; i < workers ; i ++ {
215
196
startKey [0 ] = byte (i ) * step
216
197
trieIt := trie .NewIterator (mptTrie .NodeIterator (startKey ))
217
198
199
+ stopKey := byte (i + 1 ) * step
218
200
mptWg .Add (1 )
219
201
go func () {
220
202
defer mptWg .Done ()
221
203
for trieIt .Next () {
222
- mptLeafMutex .Lock ()
223
-
224
- if _ , ok := mptLeafMap [string (trieIt .Key )]; ok {
225
- mptLeafMutex .Unlock ()
204
+ if trieIt .Key [0 ] >= stopKey {
226
205
break
227
206
}
228
207
229
- mptLeafMap [string (dup (trieIt .Key ))] = dup (trieIt .Value )
208
+ preimageKey := mptTrie .GetKey (trieIt .Key )
209
+ if len (preimageKey ) == 0 {
210
+ panic (fmt .Sprintf ("preimage not found mpt trie %s" , hex .EncodeToString (trieIt .Key )))
211
+ }
230
212
213
+ mptLeafMutex .Lock ()
214
+ mptLeafs = append (mptLeafs , kv {key : preimageKey , value : dup (trieIt .Value )})
231
215
mptLeafMutex .Unlock ()
232
-
233
- if top && len (mptLeafMap )% 10000 == 0 {
234
- fmt .Println ("MPT Accounts Loaded:" , len (mptLeafMap ))
216
+ if top && len (mptLeafs )% 10000 == 0 {
217
+ fmt .Println ("MPT Accounts Loaded:" , len (mptLeafs ))
235
218
}
236
219
}
237
220
}()
238
221
}
239
222
240
- respChan := make (chan map [ string ][] byte )
223
+ respChan := make (chan [] kv )
241
224
go func () {
242
225
mptWg .Wait ()
243
- respChan <- mptLeafMap
226
+ sort .Slice (mptLeafs , func (i , j int ) bool {
227
+ return bytes .Compare (mptLeafs [i ].key , mptLeafs [j ].key ) < 0
228
+ })
229
+ respChan <- mptLeafs
244
230
}()
245
231
return respChan
246
232
}
247
233
248
- func loadZkTrie (zkTrie * trie.ZkTrie , top , paranoid bool ) chan map [ string ][] byte {
249
- zkLeafMap := make (map [ string ][] byte , 1000 )
234
+ func loadZkTrie (zkTrie * trie.ZkTrie , top , paranoid bool ) chan [] kv {
235
+ zkLeafs := make ([] kv , 0 , 1000 )
250
236
var zkLeafMutex sync.Mutex
251
- zkDone := make (chan map [ string ][] byte )
237
+ zkDone := make (chan [] kv )
252
238
go func () {
253
239
zkTrie .CountLeaves (func (key , value []byte ) {
254
240
preimageKey := zkTrie .GetKey (key )
255
241
if len (preimageKey ) == 0 {
256
242
panic (fmt .Sprintf ("preimage not found zk trie %s" , hex .EncodeToString (key )))
257
243
}
258
244
259
- zkLeafMutex .Lock ()
260
-
261
- zkLeafMap [string (dup (preimageKey ))] = value
245
+ if top {
246
+ // ZkTrie pads preimages with 0s to make them 32 bytes.
247
+ // So we might need to clear those zeroes here since we need 20 byte addresses at top level (ie state trie)
248
+ if len (preimageKey ) > 20 {
249
+ for _ , b := range []byte (preimageKey )[20 :] {
250
+ if b != 0 {
251
+ panic (fmt .Sprintf ("padded byte is not 0 (preimage %s)" , hex .EncodeToString ([]byte (preimageKey ))))
252
+ }
253
+ }
254
+ preimageKey = preimageKey [:20 ]
255
+ }
256
+ } else if len (preimageKey ) != 32 {
257
+ // storage leafs should have 32 byte keys, pad them if needed
258
+ zeroes := make ([]byte , 32 )
259
+ copy (zeroes , []byte (preimageKey ))
260
+ preimageKey = zeroes
261
+ }
262
262
263
+ zkLeafMutex .Lock ()
264
+ zkLeafs = append (zkLeafs , kv {key : preimageKey , value : value })
263
265
zkLeafMutex .Unlock ()
264
266
265
- if top && len (zkLeafMap )% 10000 == 0 {
266
- fmt .Println ("ZK Accounts Loaded:" , len (zkLeafMap ))
267
+ if top && len (zkLeafs )% 10000 == 0 {
268
+ fmt .Println ("ZK Accounts Loaded:" , len (zkLeafs ))
267
269
}
268
270
}, top , paranoid )
269
- zkDone <- zkLeafMap
271
+
272
+ sort .Slice (zkLeafs , func (i , j int ) bool {
273
+ return bytes .Compare (zkLeafs [i ].key , zkLeafs [j ].key ) < 0
274
+ })
275
+ zkDone <- zkLeafs
270
276
}()
271
277
return zkDone
272
278
}
0 commit comments