Skip to content

Commit b006ae5

Browse files
committed
move to slices
1 parent 6ed543b commit b006ae5

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

cmd/migration-checker/main.go

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
"fmt"
88
"os"
99
"runtime"
10+
"sort"
1011
"sync"
1112
"sync/atomic"
1213
"time"
1314

1415
"github.com/scroll-tech/go-ethereum/common"
1516
"github.com/scroll-tech/go-ethereum/core/types"
16-
"github.com/scroll-tech/go-ethereum/crypto"
1717
"github.com/scroll-tech/go-ethereum/ethdb/leveldb"
1818
"github.com/scroll-tech/go-ethereum/rlp"
1919
"github.com/scroll-tech/go-ethereum/trie"
@@ -110,39 +110,16 @@ func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leaf
110110
mptLeafCh := loadMPT(mptTrie, top)
111111
zkLeafCh := loadZkTrie(zkTrie, top, paranoid)
112112

113-
mptLeafMap := <-mptLeafCh
114-
zkLeafMap := <-zkLeafCh
113+
mptLeafs := <-mptLeafCh
114+
zkLeafs := <-zkLeafCh
115115

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)))
118118
}
119119

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)
146123
}
147124
}
148125

@@ -199,74 +176,103 @@ func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes
199176
}
200177
}
201178

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 {
203184
startKey := make([]byte, 32)
204185
workers := 1 << 5
205186
if !top {
206187
workers = 1 << 3
207188
}
208189
step := byte(0xFF) / byte(workers)
209190

210-
mptLeafMap := make(map[string][]byte, 1000)
191+
mptLeafs := make([]kv, 0, 1000)
211192
var mptLeafMutex sync.Mutex
212193

213194
var mptWg sync.WaitGroup
214195
for i := 0; i < workers; i++ {
215196
startKey[0] = byte(i) * step
216197
trieIt := trie.NewIterator(mptTrie.NodeIterator(startKey))
217198

199+
stopKey := byte(i+1) * step
218200
mptWg.Add(1)
219201
go func() {
220202
defer mptWg.Done()
221203
for trieIt.Next() {
222-
mptLeafMutex.Lock()
223-
224-
if _, ok := mptLeafMap[string(trieIt.Key)]; ok {
225-
mptLeafMutex.Unlock()
204+
if trieIt.Key[0] >= stopKey {
226205
break
227206
}
228207

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+
}
230212

213+
mptLeafMutex.Lock()
214+
mptLeafs = append(mptLeafs, kv{key: preimageKey, value: dup(trieIt.Value)})
231215
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))
235218
}
236219
}
237220
}()
238221
}
239222

240-
respChan := make(chan map[string][]byte)
223+
respChan := make(chan []kv)
241224
go func() {
242225
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
244230
}()
245231
return respChan
246232
}
247233

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)
250236
var zkLeafMutex sync.Mutex
251-
zkDone := make(chan map[string][]byte)
237+
zkDone := make(chan []kv)
252238
go func() {
253239
zkTrie.CountLeaves(func(key, value []byte) {
254240
preimageKey := zkTrie.GetKey(key)
255241
if len(preimageKey) == 0 {
256242
panic(fmt.Sprintf("preimage not found zk trie %s", hex.EncodeToString(key)))
257243
}
258244

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+
}
262262

263+
zkLeafMutex.Lock()
264+
zkLeafs = append(zkLeafs, kv{key: preimageKey, value: value})
263265
zkLeafMutex.Unlock()
264266

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))
267269
}
268270
}, 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
270276
}()
271277
return zkDone
272278
}

0 commit comments

Comments
 (0)