Skip to content

Commit 8d26cb4

Browse files
committed
separate dbs
1 parent e4e565d commit 8d26cb4

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

cmd/migration-checker/main.go

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
var accountsDone atomic.Uint64
2828
var numLoaders int
29-
var trieLoaders chan struct{}
29+
var trieLoaders chan dbs
3030

3131
type dbs struct {
3232
zkDb *leveldb.Database
@@ -48,40 +48,37 @@ func main() {
4848
log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
4949
}()
5050

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-
7251
zkRootHash := common.HexToHash(*zkRoot)
7352
mptRootHash := common.HexToHash(*mptRoot)
7453

7554
numLoaders = runtime.GOMAXPROCS(0) * (*parallelismMultipler)
76-
trieLoaders = make(chan struct{}, numLoaders)
55+
trieLoaders = make(chan dbs, numLoaders)
7756
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+
}
7979
}
8080

81-
checkTrieEquality(&dbs{
82-
zkDb: zkDb,
83-
mptDb: mptDb,
84-
}, zkRootHash, mptRootHash, "", checkAccountEquality, true, *paranoid)
81+
checkTrieEquality(zkRootHash, mptRootHash, "", checkAccountEquality, true, *paranoid)
8582

8683
for i := 0; i < numLoaders; i++ {
8784
<-trieLoaders
@@ -97,7 +94,7 @@ func panicOnError(err error, label, msg string) {
9794
func dup(s []byte) []byte {
9895
return append([]byte{}, s...)
9996
}
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) {
10198
done := make(chan struct{})
10299
start := time.Now()
103100
if !top {
@@ -114,13 +111,8 @@ func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leaf
114111
}
115112
defer close(done)
116113

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)
124116

125117
mptLeafMap := <-mptLeafCh
126118
zkLeafMap := <-zkLeafCh
@@ -154,11 +146,11 @@ func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leaf
154146
panic(fmt.Sprintf("%s key %s (preimage %s) not found in mpt", label, hex.EncodeToString(mptKey), hex.EncodeToString([]byte(preimageKey))))
155147
}
156148

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)
158150
}
159151
}
160152

161-
func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountBytes []byte, paranoid bool) {
153+
func checkAccountEquality(label string, zkAccountBytes, mptAccountBytes []byte, paranoid bool) {
162154
mptAccount := &types.StateAccount{}
163155
panicOnError(rlp.DecodeBytes(mptAccountBytes, mptAccount), label, "failed to decode mpt account")
164156
zkAccount, err := types.UnmarshalStateAccount(zkAccountBytes)
@@ -181,17 +173,15 @@ func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountByte
181173
} else if zkAccount.Root != (common.Hash{}) {
182174
zkRoot := common.BytesToHash(zkAccount.Root[:])
183175
mptRoot := common.BytesToHash(mptAccount.Root[:])
184-
<-trieLoaders
185176
go func() {
186-
trieLoaders <- struct{}{}
187177
defer func() {
188178
if p := recover(); p != nil {
189179
fmt.Println(p)
190180
os.Exit(1)
191181
}
192182
}()
193183

194-
checkTrieEquality(dbs, zkRoot, mptRoot, label, checkStorageEquality, false, paranoid)
184+
checkTrieEquality(zkRoot, mptRoot, label, checkStorageEquality, false, paranoid)
195185
accountsDone.Add(1)
196186
fmt.Println("Accounts done:", accountsDone.Load())
197187
}()
@@ -201,7 +191,7 @@ func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountByte
201191
}
202192
}
203193

204-
func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes []byte, _ bool) {
194+
func checkStorageEquality(label string, zkStorageBytes, mptStorageBytes []byte, _ bool) {
205195
zkValue := common.BytesToHash(zkStorageBytes)
206196
_, content, _, err := rlp.Split(mptStorageBytes)
207197
panicOnError(err, label, "failed to decode mpt storage")
@@ -211,22 +201,25 @@ func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes
211201
}
212202
}
213203

214-
func loadMPT(mptTrie *trie.SecureTrie, top bool) chan map[string][]byte {
204+
func loadMPT(mptRoot common.Hash, top bool) chan map[string][]byte {
215205
startKey := make([]byte, 32)
216206
mptLeafMap := make(map[string][]byte, 1000)
217207
var mptLeafMutex sync.Mutex
218208

219209
var mptWg sync.WaitGroup
220210
for i := 0; i < 255; i++ {
221211
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")
222216
trieIt := trie.NewIterator(mptTrie.NodeIterator(startKey))
223217

224218
mptWg.Add(1)
225-
<-trieLoaders
226219
go func() {
227220
defer func() {
228221
mptWg.Done()
229-
trieLoaders <- struct{}{}
222+
trieLoaders <- loaderDbs
230223
}()
231224
for trieIt.Next() {
232225
mptLeafMutex.Lock()
@@ -252,12 +245,20 @@ func loadMPT(mptTrie *trie.SecureTrie, top bool) chan map[string][]byte {
252245
return respChan
253246
}
254247

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 {
256249
parallelismCutoffDepth := 8
257250
zkLeafMap := make(map[string][]byte, 1000)
258251
var zkLeafMutex sync.Mutex
259252
zkDone := make(chan map[string][]byte)
260253
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+
261262
zkTrie.CountLeaves(func(key, value []byte) {
262263
preimageKey := zkTrie.GetKey(key)
263264
if len(preimageKey) == 0 {
@@ -272,10 +273,10 @@ func loadZkTrie(zkTrie *trie.ZkTrie, top, paranoid bool) chan map[string][]byte
272273
fmt.Println("ZK Accounts Loaded:", len(zkLeafMap))
273274
}
274275
}, func(f func()) {
275-
<-trieLoaders
276+
tempDb := <-trieLoaders
276277
go func() {
277278
defer func() {
278-
trieLoaders <- struct{}{}
279+
trieLoaders <- tempDb
279280
}()
280281
f()
281282
}()

0 commit comments

Comments
 (0)