|
16 | 16 |
|
17 | 17 | import
|
18 | 18 | std/[tables, sets, sequtils],
|
19 |
| - eth/common/hashes, |
| 19 | + eth/common/[hashes, accounts_rlp], |
20 | 20 | results,
|
21 |
| - ./[aristo_desc, aristo_fetch, aristo_get, aristo_serialise, aristo_utils] |
| 21 | + ./[aristo_desc, aristo_fetch, aristo_get, aristo_serialise, aristo_utils, aristo_vid, aristo_layers] |
22 | 22 |
|
23 | 23 | const
|
24 | 24 | ChainRlpNodesNoEntry* = {
|
@@ -316,11 +316,184 @@ proc verifyProof*(
|
316 | 316 | proc verifyProof*(
|
317 | 317 | nodes: Table[Hash32, seq[byte]];
|
318 | 318 | root: Hash32;
|
319 |
| - path: Hash32): Result[Opt[seq[byte]], AristoError] = |
| 319 | + path: Hash32; |
| 320 | + visitedNodes: var HashSet[Hash32] |
| 321 | + ): Result[Opt[seq[byte]], AristoError] = |
320 | 322 | if nodes.len() == 0:
|
321 | 323 | return err(PartTrkEmptyProof)
|
322 | 324 |
|
323 | 325 | handleTrackRlpNodesResult():
|
324 |
| - var visitedNodes: HashSet[Hash32] |
325 | 326 | let nibbles = NibblesBuf.fromBytes(path.data)
|
326 | 327 | trackRlpNodes(nodes, visitedNodes, root.to(HashKey), nibbles, start = true)
|
| 328 | + |
| 329 | +proc verifyProof*( |
| 330 | + nodes: Table[Hash32, seq[byte]]; |
| 331 | + root: Hash32; |
| 332 | + path: Hash32; |
| 333 | + ): Result[Opt[seq[byte]], AristoError] = |
| 334 | + var visitedNodes: HashSet[Hash32] |
| 335 | + verifyProof(nodes, root, path, visitedNodes) |
| 336 | + |
| 337 | +proc convertLeaf( |
| 338 | + leafNode: openArray[byte], |
| 339 | + segm: NibblesBuf, |
| 340 | + isStorage: bool): Result[NodeRef, AristoError] {.gcsafe, raises: [RlpError]} = |
| 341 | + |
| 342 | + let node = |
| 343 | + if isStorage: |
| 344 | + let slotValue = rlp.decode(leafNode, UInt256) |
| 345 | + NodeRef(vtx: StoLeafRef.init(segm, slotValue)) |
| 346 | + else: # Account leaf |
| 347 | + let |
| 348 | + acc = rlp.decode(leafNode, Account) |
| 349 | + aristoAcc = AristoAccount( |
| 350 | + nonce: acc.nonce, |
| 351 | + balance: acc.balance, |
| 352 | + codeHash: acc.codeHash) |
| 353 | + stoID = (acc.storageRoot != EMPTY_ROOT_HASH, default(VertexID)) |
| 354 | + n = NodeRef(vtx: AccLeafRef.init(segm, aristoAcc, stoID)) |
| 355 | + |
| 356 | + n.key[0] = HashKey.fromBytes(acc.storageRoot.data).valueOr: |
| 357 | + return err(PartTrkLinkExpected) |
| 358 | + n |
| 359 | + |
| 360 | + ok(node) |
| 361 | + |
| 362 | +proc convertSubtrie( |
| 363 | + key: Hash32, |
| 364 | + src: Table[Hash32, seq[byte]], |
| 365 | + dst: var Table[HashKey, NodeRef], |
| 366 | + isStorage: static bool): Result[void, AristoError] {.gcsafe, raises: [RlpError]} = |
| 367 | + # Precondition: trieNodes have already been validated using verifyProof |
| 368 | + # Does not allocate any vertex ids when creating the VertexRef types. |
| 369 | + if key notin src: |
| 370 | + # Since we are processing a subtrie some nodes are expected to be missing |
| 371 | + return ok() |
| 372 | + |
| 373 | + var rlpNode = rlpFromBytes(src.getOrDefault(key)) |
| 374 | + |
| 375 | + let node = |
| 376 | + case rlpNode.listLen() |
| 377 | + of 2: |
| 378 | + let |
| 379 | + (isLeaf, segm) = NibblesBuf.fromHexPrefix(rlpNode.listElem(0).toBytes()) |
| 380 | + link = rlpNode.listElem(1).rlpNodeToBytes() # link or payload |
| 381 | + if isLeaf: |
| 382 | + let n = ?convertLeaf(link, segm, isStorage) |
| 383 | + if not isStorage and AccLeafRef(n.vtx).stoID.isValid: |
| 384 | + # Convert the storage subtrie |
| 385 | + ?convertSubtrie(n.key[0].to(Hash32), src, dst, isStorage = true) |
| 386 | + n |
| 387 | + else: # Extension node |
| 388 | + let k = HashKey.fromBytes(link).valueOr: |
| 389 | + return err(PartTrkLinkExpected) |
| 390 | + |
| 391 | + # Convert the child branch node which will be merged with this extension node |
| 392 | + ?convertSubtrie(k.to(Hash32), src, dst, isStorage) |
| 393 | + doAssert(dst.contains(k)) |
| 394 | + |
| 395 | + let |
| 396 | + childNode = dst.getOrDefault(k) |
| 397 | + childBranch = BranchRef(childNode.vtx) |
| 398 | + |
| 399 | + # Remove the childNode because it's branch was copied into this node |
| 400 | + dst.del(k) |
| 401 | + |
| 402 | + NodeRef( |
| 403 | + key: childNode.key, |
| 404 | + vtx: ExtBranchRef.init(segm, childBranch.startVid, childBranch.used)) |
| 405 | + |
| 406 | + of 17: # Branch node |
| 407 | + var key: array[16, HashKey] |
| 408 | + let branch = BranchRef.init(default(VertexID), 0) |
| 409 | + for i in 0 ..< 16: |
| 410 | + let |
| 411 | + link = rlpNode.listElem(i).rlpNodeToBytes() |
| 412 | + k = HashKey.fromBytes(link).valueOr: |
| 413 | + return err(PartTrkLinkExpected) |
| 414 | + if k.len() > 0: |
| 415 | + discard branch.setUsed(i.uint8, true) |
| 416 | + ?convertSubtrie(k.to(Hash32), src, dst, isStorage) |
| 417 | + key[i] = k |
| 418 | + NodeRef(key: key, vtx: branch) |
| 419 | + |
| 420 | + else: |
| 421 | + return err(PartTrkGarbledNode) |
| 422 | + |
| 423 | + let hashKey = HashKey.fromBytes(key.data).valueOr: |
| 424 | + return err(PartTrkLinkExpected) |
| 425 | + dst[hashKey] = node |
| 426 | + |
| 427 | + ok() |
| 428 | + |
| 429 | +proc putSubtrie( |
| 430 | + db: AristoTxRef, |
| 431 | + key: HashKey, |
| 432 | + nodes: Table[HashKey, NodeRef], |
| 433 | + rvid: RootedVertexID = (STATE_ROOT_VID, STATE_ROOT_VID)): Result[void, AristoError] = |
| 434 | + if key notin nodes: |
| 435 | + return err(PartTrkFollowUpKeyMismatch) |
| 436 | + |
| 437 | + let node = nodes.getOrDefault(key) |
| 438 | + case node.vtx.vType: |
| 439 | + of AccLeaf: |
| 440 | + let accVtx = AccLeafRef(node.vtx) |
| 441 | + if accVtx.stoID.isValid: |
| 442 | + let stoVid = db.vidFetch() |
| 443 | + accVtx.stoID = (true, stoVid) |
| 444 | + |
| 445 | + let |
| 446 | + k = node.key[0] |
| 447 | + r = (stoVid, stoVid) |
| 448 | + if nodes.contains(k): |
| 449 | + # Write the storage subtrie |
| 450 | + ?db.putSubtrie(k, nodes, r) |
| 451 | + else: |
| 452 | + # Write the known hash key setting the vtx to nil |
| 453 | + db.layersPutKey(r, BranchRef(nil), k) |
| 454 | + |
| 455 | + of StoLeaf: |
| 456 | + discard |
| 457 | + |
| 458 | + of Branch, ExtBranch: |
| 459 | + let bvtx = BranchRef(node.vtx) |
| 460 | + bvtx.startVid = db.vidFetch(16) |
| 461 | + |
| 462 | + for n, subvid in node.vtx.pairs(): |
| 463 | + let |
| 464 | + r = (rvid.root, subvid) |
| 465 | + k = if node.key[n].len() < 32: |
| 466 | + # Embedded nodes are stored in the nodes map indexed by |
| 467 | + # a HashKey containing a hash of the node data rather than |
| 468 | + # a HashKey containing the embedded node itself |
| 469 | + node.key[n].to(Hash32).to(HashKey) |
| 470 | + else: |
| 471 | + node.key[n] |
| 472 | + if nodes.contains(k): |
| 473 | + ?db.putSubtrie(k, nodes, r) |
| 474 | + else: |
| 475 | + # Write the known hash key setting the vtx to nil |
| 476 | + db.layersPutKey(r, BranchRef(nil), k) |
| 477 | + |
| 478 | + db.layersPutVtx(rvid, node.vtx) |
| 479 | + |
| 480 | + ok() |
| 481 | + |
| 482 | +proc putSubtrie*( |
| 483 | + db: AristoTxRef, |
| 484 | + stateRoot: Hash32, |
| 485 | + nodes: Table[Hash32, seq[byte]]): Result[void, AristoError] = |
| 486 | + if nodes.len() == 0: |
| 487 | + return err(PartTrkEmptyProof) |
| 488 | + |
| 489 | + let key = HashKey.fromBytes(stateRoot.data).valueOr: |
| 490 | + return err(PartTrkLinkExpected) |
| 491 | + |
| 492 | + try: |
| 493 | + var convertedNodes: Table[HashKey, NodeRef] |
| 494 | + ?convertSubtrie(stateRoot, nodes, convertedNodes, isStorage = false) |
| 495 | + ?db.putSubtrie(key, convertedNodes) |
| 496 | + except RlpError: |
| 497 | + return err(PartTrkRlpError) |
| 498 | + |
| 499 | + ok() |
0 commit comments