|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "runtime" |
| 10 | + "sync" |
| 11 | + "sync/atomic" |
| 12 | + |
| 13 | + "github.com/scroll-tech/go-ethereum/common" |
| 14 | + "github.com/scroll-tech/go-ethereum/core/types" |
| 15 | + "github.com/scroll-tech/go-ethereum/crypto" |
| 16 | + "github.com/scroll-tech/go-ethereum/ethdb/leveldb" |
| 17 | + "github.com/scroll-tech/go-ethereum/rlp" |
| 18 | + "github.com/scroll-tech/go-ethereum/trie" |
| 19 | +) |
| 20 | + |
| 21 | +var accountsDone atomic.Uint64 |
| 22 | +var trieCheckers = make(chan struct{}, runtime.GOMAXPROCS(0)*4) |
| 23 | + |
| 24 | +type dbs struct { |
| 25 | + zkDb *leveldb.Database |
| 26 | + mptDb *leveldb.Database |
| 27 | +} |
| 28 | + |
| 29 | +func main() { |
| 30 | + var ( |
| 31 | + mptDbPath = flag.String("mpt-db", "", "path to the MPT node DB") |
| 32 | + zkDbPath = flag.String("zk-db", "", "path to the ZK node DB") |
| 33 | + mptRoot = flag.String("mpt-root", "", "root hash of the MPT node") |
| 34 | + zkRoot = flag.String("zk-root", "", "root hash of the ZK node") |
| 35 | + ) |
| 36 | + flag.Parse() |
| 37 | + |
| 38 | + zkDb, err := leveldb.New(*zkDbPath, 1024, 128, "", true) |
| 39 | + panicOnError(err, "", "failed to open zk db") |
| 40 | + mptDb, err := leveldb.New(*mptDbPath, 1024, 128, "", true) |
| 41 | + panicOnError(err, "", "failed to open mpt db") |
| 42 | + |
| 43 | + zkRootHash := common.HexToHash(*zkRoot) |
| 44 | + mptRootHash := common.HexToHash(*mptRoot) |
| 45 | + |
| 46 | + for i := 0; i < runtime.GOMAXPROCS(0)*4; i++ { |
| 47 | + trieCheckers <- struct{}{} |
| 48 | + } |
| 49 | + |
| 50 | + checkTrieEquality(&dbs{ |
| 51 | + zkDb: zkDb, |
| 52 | + mptDb: mptDb, |
| 53 | + }, zkRootHash, mptRootHash, "", checkAccountEquality, true) |
| 54 | +} |
| 55 | + |
| 56 | +func panicOnError(err error, label, msg string) { |
| 57 | + if err != nil { |
| 58 | + panic(fmt.Sprint(label, " error: ", msg, " ", err)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func dup(s []byte) []byte { |
| 63 | + return append([]byte{}, s...) |
| 64 | +} |
| 65 | +func checkTrieEquality(dbs *dbs, zkRoot, mptRoot common.Hash, label string, leafChecker func(string, *dbs, []byte, []byte), top bool) { |
| 66 | + zkTrie, err := trie.NewZkTrie(zkRoot, trie.NewZktrieDatabaseFromTriedb(trie.NewDatabaseWithConfig(dbs.zkDb, &trie.Config{Preimages: true}))) |
| 67 | + panicOnError(err, label, "failed to create zk trie") |
| 68 | + mptTrie, err := trie.NewSecureNoTracer(mptRoot, trie.NewDatabaseWithConfig(dbs.mptDb, &trie.Config{Preimages: true})) |
| 69 | + panicOnError(err, label, "failed to create mpt trie") |
| 70 | + |
| 71 | + mptLeafCh := loadMPT(mptTrie, top) |
| 72 | + zkLeafCh := loadZkTrie(zkTrie, top) |
| 73 | + |
| 74 | + mptLeafMap := <-mptLeafCh |
| 75 | + zkLeafMap := <-zkLeafCh |
| 76 | + |
| 77 | + if len(mptLeafMap) != len(zkLeafMap) { |
| 78 | + panic(fmt.Sprintf("%s MPT and ZK trie leaf count mismatch: MPT: %d, ZK: %d", label, len(mptLeafMap), len(zkLeafMap))) |
| 79 | + } |
| 80 | + |
| 81 | + for preimageKey, zkValue := range zkLeafMap { |
| 82 | + if top { |
| 83 | + // ZkTrie pads preimages with 0s to make them 32 bytes. |
| 84 | + // So we might need to clear those zeroes here since we need 20 byte addresses at top level (ie state trie) |
| 85 | + if len(preimageKey) > 20 { |
| 86 | + for _, b := range []byte(preimageKey)[20:] { |
| 87 | + if b != 0 { |
| 88 | + panic(fmt.Sprintf("%s padded byte is not 0 (preimage %s)", label, hex.EncodeToString([]byte(preimageKey)))) |
| 89 | + } |
| 90 | + } |
| 91 | + preimageKey = preimageKey[:20] |
| 92 | + } |
| 93 | + } else if len(preimageKey) != 32 { |
| 94 | + // storage leafs should have 32 byte keys, pad them if needed |
| 95 | + zeroes := make([]byte, 32) |
| 96 | + copy(zeroes, []byte(preimageKey)) |
| 97 | + preimageKey = string(zeroes) |
| 98 | + } |
| 99 | + |
| 100 | + mptKey := crypto.Keccak256([]byte(preimageKey)) |
| 101 | + mptVal, ok := mptLeafMap[string(mptKey)] |
| 102 | + if !ok { |
| 103 | + panic(fmt.Sprintf("%s key %s (preimage %s) not found in mpt", label, hex.EncodeToString([]byte(mptKey)), hex.EncodeToString([]byte(preimageKey)))) |
| 104 | + } |
| 105 | + |
| 106 | + leafChecker(fmt.Sprintf("%s key: %s", label, hex.EncodeToString([]byte(preimageKey))), dbs, zkValue, mptVal) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func checkAccountEquality(label string, dbs *dbs, zkAccountBytes, mptAccountBytes []byte) { |
| 111 | + mptAccount := &types.StateAccount{} |
| 112 | + panicOnError(rlp.DecodeBytes(mptAccountBytes, mptAccount), label, "failed to decode mpt account") |
| 113 | + zkAccount, err := types.UnmarshalStateAccount(zkAccountBytes) |
| 114 | + panicOnError(err, label, "failed to decode zk account") |
| 115 | + |
| 116 | + if mptAccount.Nonce != zkAccount.Nonce { |
| 117 | + panic(fmt.Sprintf("%s nonce mismatch: zk: %d, mpt: %d", label, zkAccount.Nonce, mptAccount.Nonce)) |
| 118 | + } |
| 119 | + |
| 120 | + if mptAccount.Balance.Cmp(zkAccount.Balance) != 0 { |
| 121 | + panic(fmt.Sprintf("%s balance mismatch: zk: %s, mpt: %s", label, zkAccount.Balance.String(), mptAccount.Balance.String())) |
| 122 | + } |
| 123 | + |
| 124 | + if !bytes.Equal(mptAccount.KeccakCodeHash, zkAccount.KeccakCodeHash) { |
| 125 | + panic(fmt.Sprintf("%s code hash mismatch: zk: %s, mpt: %s", label, hex.EncodeToString(zkAccount.KeccakCodeHash), hex.EncodeToString(mptAccount.KeccakCodeHash))) |
| 126 | + } |
| 127 | + |
| 128 | + if (zkAccount.Root == common.Hash{}) != (mptAccount.Root == types.EmptyRootHash) { |
| 129 | + panic(fmt.Sprintf("%s empty account root mismatch", label)) |
| 130 | + } else if zkAccount.Root != (common.Hash{}) { |
| 131 | + zkRoot := common.BytesToHash(zkAccount.Root[:]) |
| 132 | + mptRoot := common.BytesToHash(mptAccount.Root[:]) |
| 133 | + <-trieCheckers |
| 134 | + go func() { |
| 135 | + defer func() { |
| 136 | + if p := recover(); p != nil { |
| 137 | + fmt.Println(p) |
| 138 | + os.Exit(1) |
| 139 | + } |
| 140 | + }() |
| 141 | + |
| 142 | + checkTrieEquality(dbs, zkRoot, mptRoot, label, checkStorageEquality, false) |
| 143 | + accountsDone.Add(1) |
| 144 | + fmt.Println("Accounts done:", accountsDone.Load()) |
| 145 | + trieCheckers <- struct{}{} |
| 146 | + }() |
| 147 | + } else { |
| 148 | + accountsDone.Add(1) |
| 149 | + fmt.Println("Accounts done:", accountsDone.Load()) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func checkStorageEquality(label string, _ *dbs, zkStorageBytes, mptStorageBytes []byte) { |
| 154 | + zkValue := common.BytesToHash(zkStorageBytes) |
| 155 | + _, content, _, err := rlp.Split(mptStorageBytes) |
| 156 | + panicOnError(err, label, "failed to decode mpt storage") |
| 157 | + mptValue := common.BytesToHash(content) |
| 158 | + if !bytes.Equal(zkValue[:], mptValue[:]) { |
| 159 | + panic(fmt.Sprintf("%s storage mismatch: zk: %s, mpt: %s", label, zkValue.Hex(), mptValue.Hex())) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func loadMPT(mptTrie *trie.SecureTrie, parallel bool) chan map[string][]byte { |
| 164 | + startKey := make([]byte, 32) |
| 165 | + workers := 1 << 5 |
| 166 | + if !parallel { |
| 167 | + workers = 1 |
| 168 | + } |
| 169 | + step := byte(0xFF) / byte(workers) |
| 170 | + |
| 171 | + mptLeafMap := make(map[string][]byte, 1000) |
| 172 | + var mptLeafMutex sync.Mutex |
| 173 | + |
| 174 | + var mptWg sync.WaitGroup |
| 175 | + for i := 0; i < workers; i++ { |
| 176 | + startKey[0] = byte(i) * step |
| 177 | + trieIt := trie.NewIterator(mptTrie.NodeIterator(startKey)) |
| 178 | + |
| 179 | + mptWg.Add(1) |
| 180 | + go func() { |
| 181 | + defer mptWg.Done() |
| 182 | + for trieIt.Next() { |
| 183 | + if parallel { |
| 184 | + mptLeafMutex.Lock() |
| 185 | + } |
| 186 | + |
| 187 | + if _, ok := mptLeafMap[string(trieIt.Key)]; ok { |
| 188 | + mptLeafMutex.Unlock() |
| 189 | + break |
| 190 | + } |
| 191 | + |
| 192 | + mptLeafMap[string(dup(trieIt.Key))] = dup(trieIt.Value) |
| 193 | + |
| 194 | + if parallel { |
| 195 | + mptLeafMutex.Unlock() |
| 196 | + } |
| 197 | + |
| 198 | + if parallel && len(mptLeafMap)%10000 == 0 { |
| 199 | + fmt.Println("MPT Accounts Loaded:", len(mptLeafMap)) |
| 200 | + } |
| 201 | + } |
| 202 | + }() |
| 203 | + } |
| 204 | + |
| 205 | + respChan := make(chan map[string][]byte) |
| 206 | + go func() { |
| 207 | + mptWg.Wait() |
| 208 | + respChan <- mptLeafMap |
| 209 | + }() |
| 210 | + return respChan |
| 211 | +} |
| 212 | + |
| 213 | +func loadZkTrie(zkTrie *trie.ZkTrie, parallel bool) chan map[string][]byte { |
| 214 | + zkLeafMap := make(map[string][]byte, 1000) |
| 215 | + var zkLeafMutex sync.Mutex |
| 216 | + zkDone := make(chan map[string][]byte) |
| 217 | + go func() { |
| 218 | + zkTrie.CountLeaves(func(key, value []byte) { |
| 219 | + preimageKey := zkTrie.GetKey(key) |
| 220 | + if len(preimageKey) == 0 { |
| 221 | + panic(fmt.Sprintf("preimage not found zk trie %s", hex.EncodeToString(key))) |
| 222 | + } |
| 223 | + |
| 224 | + if parallel { |
| 225 | + zkLeafMutex.Lock() |
| 226 | + } |
| 227 | + |
| 228 | + zkLeafMap[string(dup(preimageKey))] = value |
| 229 | + |
| 230 | + if parallel { |
| 231 | + zkLeafMutex.Unlock() |
| 232 | + } |
| 233 | + |
| 234 | + if parallel && len(zkLeafMap)%10000 == 0 { |
| 235 | + fmt.Println("ZK Accounts Loaded:", len(zkLeafMap)) |
| 236 | + } |
| 237 | + }, parallel) |
| 238 | + zkDone <- zkLeafMap |
| 239 | + }() |
| 240 | + return zkDone |
| 241 | +} |
0 commit comments