|
| 1 | +# nimbus_verified_proxy |
| 2 | +# Copyright (c) 2025 Status Research & Development GmbH |
| 3 | +# Licensed and distributed under either of |
| 4 | +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). |
| 5 | +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). |
| 6 | +# at your option. This file may not be copied, modified, or distributed except according to those terms. |
| 7 | + |
| 8 | +{.push gcsafe, raises: [].} |
| 9 | + |
| 10 | +import |
| 11 | + std/sequtils, |
| 12 | + results, |
| 13 | + eth/common/eth_types_rlp, |
| 14 | + eth/trie/[ordered_trie, trie_defs], |
| 15 | + json_rpc/[rpcserver, rpcclient], |
| 16 | + web3/[eth_api_types, eth_api], |
| 17 | + ../../execution_chain/beacon/web3_eth_conv, |
| 18 | + ../../execution_chain/rpc/filters, |
| 19 | + ../types, |
| 20 | + ./blocks |
| 21 | + |
| 22 | +template toLog(lg: LogObject): Log = |
| 23 | + Log(address: lg.address, topics: lg.topics, data: lg.data) |
| 24 | + |
| 25 | +func toLogs(logs: openArray[LogObject]): seq[Log] = |
| 26 | + logs.mapIt(it.toLog) |
| 27 | + |
| 28 | +func toReceipt(rec: ReceiptObject): Receipt = |
| 29 | + let isHash = not rec.status.isSome() |
| 30 | + |
| 31 | + let status = rec.status.isSome() and rec.status.get() == 1.Quantity |
| 32 | + |
| 33 | + return Receipt( |
| 34 | + hash: rec.transactionHash, |
| 35 | + isHash: isHash, |
| 36 | + status: status, |
| 37 | + cumulativeGasUsed: rec.cumulativeGasUsed.GasInt, |
| 38 | + logs: toLogs(rec.logs), |
| 39 | + logsBloom: rec.logsBloom, |
| 40 | + receiptType: rec.`type`.get(0.Web3Quantity).ReceiptType, |
| 41 | + ) |
| 42 | + |
| 43 | +func toReceipts(recs: openArray[ReceiptObject]): seq[Receipt] = |
| 44 | + recs.mapIt(it.toReceipt) |
| 45 | + |
| 46 | +proc getReceipts( |
| 47 | + vp: VerifiedRpcProxy, header: Header, blockTag: types.BlockTag |
| 48 | +): Future[Result[seq[ReceiptObject], string]] {.async.} = |
| 49 | + let rxs = |
| 50 | + try: |
| 51 | + await vp.rpcClient.eth_getBlockReceipts(blockTag) |
| 52 | + except CatchableError as e: |
| 53 | + return err(e.msg) |
| 54 | + |
| 55 | + if rxs.isSome(): |
| 56 | + if orderedTrieRoot(toReceipts(rxs.get())) != header.receiptsRoot: |
| 57 | + return |
| 58 | + err("downloaded receipts do not evaluate to the receipts root of the block") |
| 59 | + else: |
| 60 | + return err("error downloading the receipts") |
| 61 | + |
| 62 | + return ok(rxs.get()) |
| 63 | + |
| 64 | +proc getReceipts*( |
| 65 | + vp: VerifiedRpcProxy, blockTag: types.BlockTag |
| 66 | +): Future[Result[seq[ReceiptObject], string]] {.async.} = |
| 67 | + let |
| 68 | + header = (await vp.getHeader(blockTag)).valueOr: |
| 69 | + return err(error) |
| 70 | + # all other tags are automatically resolved while getting the header |
| 71 | + numberTag = types.BlockTag( |
| 72 | + kind: BlockIdentifierKind.bidNumber, number: Quantity(header.number) |
| 73 | + ) |
| 74 | + |
| 75 | + await vp.getReceipts(header, numberTag) |
| 76 | + |
| 77 | +proc getReceipts*( |
| 78 | + vp: VerifiedRpcProxy, blockHash: Hash32 |
| 79 | +): Future[Result[seq[ReceiptObject], string]] {.async.} = |
| 80 | + let |
| 81 | + header = (await vp.getHeader(blockHash)).valueOr: |
| 82 | + return err(error) |
| 83 | + numberTag = types.BlockTag( |
| 84 | + kind: BlockIdentifierKind.bidNumber, number: Quantity(header.number) |
| 85 | + ) |
| 86 | + |
| 87 | + await vp.getReceipts(header, numberTag) |
| 88 | + |
| 89 | +proc getLogs*( |
| 90 | + vp: VerifiedRpcProxy, filterOptions: FilterOptions |
| 91 | +): Future[Result[seq[LogObject], string]] {.async.} = |
| 92 | + let logObjs = |
| 93 | + try: |
| 94 | + await vp.rpcClient.eth_getLogs(filterOptions) |
| 95 | + except CatchableError as e: |
| 96 | + return err(e.msg) |
| 97 | + |
| 98 | + # store block hashes contains the logs so that we can batch receipt requests |
| 99 | + var |
| 100 | + prevBlockHash: Hash32 |
| 101 | + rxs: seq[ReceiptObject] |
| 102 | + |
| 103 | + for lg in logObjs: |
| 104 | + # none only for pending logs before block is built |
| 105 | + if lg.blockHash.isSome() and lg.transactionIndex.isSome() and lg.logIndex.isSome(): |
| 106 | + # exploit sequentiality of logs |
| 107 | + if prevBlockHash != lg.blockHash.get(): |
| 108 | + # TODO: a cache will solve downloading the same block receipts for multiple logs |
| 109 | + rxs = (await vp.getReceipts(lg.blockHash.get())).valueOr: |
| 110 | + return err(error) |
| 111 | + prevBlockHash = lg.blockHash.get() |
| 112 | + |
| 113 | + let |
| 114 | + txIdx = distinctBase(lg.transactionIndex.get()) |
| 115 | + logIdx = |
| 116 | + distinctBase(lg.logIndex.get()) - |
| 117 | + distinctBase(rxs[txIdx].logs[0].logIndex.get()) |
| 118 | + rxLog = rxs[txIdx].logs[logIdx] |
| 119 | + |
| 120 | + if rxLog.address != lg.address or rxLog.data != lg.data or |
| 121 | + rxLog.topics != lg.topics or |
| 122 | + (not match(toLog(lg), filterOptions.address, filterOptions.topics)): |
| 123 | + return err("one of the returned logs is invalid") |
| 124 | + |
| 125 | + return ok(logObjs) |
0 commit comments