Skip to content

Commit 81ab44f

Browse files
committed
paranoid flag
1 parent a87c094 commit 81ab44f

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

cmd/migration-checker/main.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func main() {
3232
zkDbPath = flag.String("zk-db", "", "path to the ZK node DB")
3333
mptRoot = flag.String("mpt-root", "", "root hash of the MPT node")
3434
zkRoot = flag.String("zk-root", "", "root hash of the ZK node")
35+
paranoid = flag.Bool("paranoid", false, "verifies all node contents against their expected hash")
3536
)
3637
flag.Parse()
3738

@@ -50,7 +51,7 @@ func main() {
5051
checkTrieEquality(&dbs{
5152
zkDb: zkDb,
5253
mptDb: mptDb,
53-
}, zkRootHash, mptRootHash, "", checkAccountEquality, true)
54+
}, zkRootHash, mptRootHash, "", checkAccountEquality, true, *paranoid)
5455

5556
for i := 0; i < runtime.GOMAXPROCS(0)*4; i++ {
5657
<-trieCheckers
@@ -66,14 +67,14 @@ func panicOnError(err error, label, msg string) {
6667
func dup(s []byte) []byte {
6768
return append([]byte{}, s...)
6869
}
69-
func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leafChecker func(string, *dbs, []byte, []byte), top bool) {
70+
func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leafChecker func(string, *dbs, []byte, []byte, bool), top, paranoid bool) {
7071
zkTrie, err := trie.NewZkTrie(zkRoot, trie.NewZktrieDatabaseFromTriedb(trie.NewDatabaseWithConfig(dbs.zkDb, &trie.Config{Preimages: true})))
7172
panicOnError(err, label, "failed to create zk trie")
7273
mptTrie, err := trie.NewSecureNoTracer(mptRoot, trie.NewDatabaseWithConfig(dbs.mptDb, &trie.Config{Preimages: true}))
7374
panicOnError(err, label, "failed to create mpt trie")
7475

7576
mptLeafCh := loadMPT(mptTrie, top)
76-
zkLeafCh := loadZkTrie(zkTrie, top)
77+
zkLeafCh := loadZkTrie(zkTrie, top, paranoid)
7778

7879
mptLeafMap := <-mptLeafCh
7980
zkLeafMap := <-zkLeafCh
@@ -107,11 +108,11 @@ func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leaf
107108
panic(fmt.Sprintf("%s key %s (preimage %s) not found in mpt", label, hex.EncodeToString([]byte(mptKey)), hex.EncodeToString([]byte(preimageKey))))
108109
}
109110

110-
leafChecker(fmt.Sprintf("%s key: %s", label, hex.EncodeToString([]byte(preimageKey))), dbs, zkValue, mptVal)
111+
leafChecker(fmt.Sprintf("%s key: %s", label, hex.EncodeToString([]byte(preimageKey))), dbs, zkValue, mptVal, paranoid)
111112
}
112113
}
113114

114-
func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountBytes []byte) {
115+
func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountBytes []byte, paranoid bool) {
115116
mptAccount := &types.StateAccount{}
116117
panicOnError(rlp.DecodeBytes(mptAccountBytes, mptAccount), label, "failed to decode mpt account")
117118
zkAccount, err := types.UnmarshalStateAccount(zkAccountBytes)
@@ -143,7 +144,7 @@ func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountByte
143144
}
144145
}()
145146

146-
checkTrieEquality(dbs, zkRoot, mptRoot, label, checkStorageEquality, false)
147+
checkTrieEquality(dbs, zkRoot, mptRoot, label, checkStorageEquality, false, paranoid)
147148
accountsDone.Add(1)
148149
fmt.Println("Accounts done:", accountsDone.Load())
149150
trieCheckers <- struct{}{}
@@ -154,7 +155,7 @@ func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountByte
154155
}
155156
}
156157

157-
func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes []byte) {
158+
func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes []byte, _ bool) {
158159
zkValue := common.BytesToHash(zkStorageBytes)
159160
_, content, _, err := rlp.Split(mptStorageBytes)
160161
panicOnError(err, label, "failed to decode mpt storage")
@@ -214,7 +215,7 @@ func loadMPT(mptTrie *trie.SecureTrie, parallel bool) chan map[string][]byte {
214215
return respChan
215216
}
216217

217-
func loadZkTrie(zkTrie *trie.ZkTrie, parallel bool) chan map[string][]byte {
218+
func loadZkTrie(zkTrie *trie.ZkTrie, parallel, paranoid bool) chan map[string][]byte {
218219
zkLeafMap := make(map[string][]byte, 1000)
219220
var zkLeafMutex sync.Mutex
220221
zkDone := make(chan map[string][]byte)
@@ -238,7 +239,7 @@ func loadZkTrie(zkTrie *trie.ZkTrie, parallel bool) chan map[string][]byte {
238239
if parallel && len(zkLeafMap)%10000 == 0 {
239240
fmt.Println("ZK Accounts Loaded:", len(zkLeafMap))
240241
}
241-
}, parallel)
242+
}, parallel, paranoid)
242243
zkDone <- zkLeafMap
243244
}()
244245
return zkDone

trie/zk_trie.go

+13-3
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 bool) uint64 {
242+
func (t *ZkTrie) CountLeaves(cb func(key, value []byte), parallel, 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)
247+
return t.countLeaves(root, cb, 0, parallel, verifyNodeHashes)
248248
}
249249

250-
func (t *ZkTrie) countLeaves(root *zkt.Hash, cb func(key, value []byte), depth int, parallel bool) uint64 {
250+
func (t *ZkTrie) countLeaves(root *zkt.Hash, cb func(key, value []byte), depth int, parallel, verifyNodeHashes bool) uint64 {
251251
if root == nil {
252252
return 0
253253
}
@@ -258,6 +258,16 @@ func (t *ZkTrie) countLeaves(root *zkt.Hash, cb func(key, value []byte), depth i
258258
}
259259

260260
if rootNode.Type == zktrie.NodeTypeLeaf_New {
261+
if verifyNodeHashes {
262+
calculatedNodeHash, err := rootNode.NodeHash()
263+
if err != nil {
264+
panic("countLeaves cannot get calculatedNodeHash")
265+
}
266+
if *calculatedNodeHash != *root {
267+
panic("countLeaves node hash mismatch")
268+
}
269+
}
270+
261271
cb(append([]byte{}, rootNode.NodeKey.Bytes()...), append([]byte{}, rootNode.Data()...))
262272
return 1
263273
} else {

0 commit comments

Comments
 (0)