|
| 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 raises: [], gcsafe.} |
| 9 | + |
| 10 | +import |
| 11 | + stint, |
| 12 | + std/strutils, |
| 13 | + json_rpc/[rpcserver, rpcproxy], |
| 14 | + web3/[eth_api, eth_api_types], |
| 15 | + ../execution_chain/rpc/cors, |
| 16 | + ./engine/types, |
| 17 | + ./nimbus_verified_proxy_conf |
| 18 | + |
| 19 | +type JsonRpcServer* = ref object |
| 20 | + case kind*: ClientKind #we reuse clientKind for servers also |
| 21 | + of Http: |
| 22 | + httpServer: RpcHttpServer |
| 23 | + of WebSocket: |
| 24 | + wsServer: RpcWebSocketServer |
| 25 | + |
| 26 | +proc init*( |
| 27 | + T: type JsonRpcServer, url: Web3Url |
| 28 | +): JsonRpcServer {.raises: [JsonRpcError, ValueError, TransportAddressError].} = |
| 29 | + let |
| 30 | + auth = @[httpCors(@[])] # TODO: for now we serve all cross origin requests |
| 31 | + parsedUrl = parseUri(url.web3Url) |
| 32 | + hostname = if parsedUrl.hostname == "": "127.0.0.1" else: parsedUrl.hostname |
| 33 | + port = |
| 34 | + if parsedUrl.port == "": |
| 35 | + 8545 |
| 36 | + else: |
| 37 | + parseInt(parsedUrl.port) |
| 38 | + listenAddress = initTAddress(hostname, port) |
| 39 | + |
| 40 | + case url.kind |
| 41 | + of HttpUrl: |
| 42 | + JsonRpcServer( |
| 43 | + kind: Http, httpServer: newRpcHttpServer([listenAddress], RpcRouter.init(), auth) |
| 44 | + ) |
| 45 | + of WsUrl: |
| 46 | + let server = |
| 47 | + JsonRpcServer(kind: WebSocket, wsServer: newRpcWebSocketServer(listenAddress)) |
| 48 | + |
| 49 | + server.wsServer.router = RpcRouter.init() |
| 50 | + server |
| 51 | + |
| 52 | +func getServer(server: JsonRpcServer): RpcServer = |
| 53 | + case server.kind |
| 54 | + of Http: server.httpServer |
| 55 | + of WebSocket: server.wsServer |
| 56 | + |
| 57 | +proc start*(server: JsonRpcServer): Result[void, string] = |
| 58 | + try: |
| 59 | + case server.kind |
| 60 | + of Http: |
| 61 | + server.httpServer.start() |
| 62 | + of WebSocket: |
| 63 | + server.wsServer.start() |
| 64 | + except CatchableError as e: |
| 65 | + return err(e.msg) |
| 66 | + |
| 67 | + ok() |
| 68 | + |
| 69 | +proc injectEngineFrontend*(server: JsonRpcServer, frontend: EthApiFrontend) = |
| 70 | + server.getServer().rpc("eth_blockNumber") do() -> uint64: |
| 71 | + await frontend.eth_blockNumber() |
| 72 | + |
| 73 | + server.getServer().rpc("eth_getBalance") do( |
| 74 | + address: Address, quantityTag: BlockTag |
| 75 | + ) -> UInt256: |
| 76 | + await frontend.eth_getBalance(address, quantityTag) |
| 77 | + |
| 78 | + server.getServer().rpc("eth_getStorageAt") do( |
| 79 | + address: Address, slot: UInt256, quantityTag: BlockTag |
| 80 | + ) -> FixedBytes[32]: |
| 81 | + await frontend.eth_getStorageAt(address, slot, quantityTag) |
| 82 | + |
| 83 | + server.getServer().rpc("eth_getTransactionCount") do( |
| 84 | + address: Address, quantityTag: BlockTag |
| 85 | + ) -> Quantity: |
| 86 | + await frontend.eth_getTransactionCount(address, quantityTag) |
| 87 | + |
| 88 | + server.getServer().rpc("eth_getCode") do( |
| 89 | + address: Address, quantityTag: BlockTag |
| 90 | + ) -> seq[byte]: |
| 91 | + await frontend.eth_getCode(address, quantityTag) |
| 92 | + |
| 93 | + server.getServer().rpc("eth_getBlockByHash") do( |
| 94 | + blockHash: Hash32, fullTransactions: bool |
| 95 | + ) -> BlockObject: |
| 96 | + await frontend.eth_getBlockByHash(blockHash, fullTransactions) |
| 97 | + |
| 98 | + server.getServer().rpc("eth_getBlockByNumber") do( |
| 99 | + blockTag: BlockTag, fullTransactions: bool |
| 100 | + ) -> BlockObject: |
| 101 | + await frontend.eth_getBlockByNumber(blockTag, fullTransactions) |
| 102 | + |
| 103 | + server.getServer().rpc("eth_getUncleCountByBlockNumber") do( |
| 104 | + blockTag: BlockTag |
| 105 | + ) -> Quantity: |
| 106 | + await frontend.eth_getUncleCountByBlockNumber(blockTag) |
| 107 | + |
| 108 | + server.getServer().rpc("eth_getUncleCountByBlockHash") do( |
| 109 | + blockHash: Hash32 |
| 110 | + ) -> Quantity: |
| 111 | + await frontend.eth_getUncleCountByBlockHash(blockHash) |
| 112 | + |
| 113 | + server.getServer().rpc("eth_getBlockTransactionCountByNumber") do( |
| 114 | + blockTag: BlockTag |
| 115 | + ) -> Quantity: |
| 116 | + await frontend.eth_getBlockTransactionCountByNumber(blockTag) |
| 117 | + |
| 118 | + server.getServer().rpc("eth_getBlockTransactionCountByHash") do( |
| 119 | + blockHash: Hash32 |
| 120 | + ) -> Quantity: |
| 121 | + await frontend.eth_getBlockTransactionCountByHash(blockHash) |
| 122 | + |
| 123 | + server.getServer().rpc("eth_getTransactionByBlockNumberAndIndex") do( |
| 124 | + blockTag: BlockTag, index: Quantity |
| 125 | + ) -> TransactionObject: |
| 126 | + await frontend.eth_getTransactionByBlockNumberAndIndex(blockTag, index) |
| 127 | + |
| 128 | + server.getServer().rpc("eth_getTransactionByBlockHashAndIndex") do( |
| 129 | + blockHash: Hash32, index: Quantity |
| 130 | + ) -> TransactionObject: |
| 131 | + await frontend.eth_getTransactionByBlockHashAndIndex(blockHash, index) |
| 132 | + |
| 133 | + server.getServer().rpc("eth_call") do( |
| 134 | + tx: TransactionArgs, blockTag: BlockTag, optimisticStateFetch: Opt[bool] |
| 135 | + ) -> seq[byte]: |
| 136 | + await frontend.eth_call(tx, blockTag, optimisticStateFetch.get(true)) |
| 137 | + |
| 138 | + server.getServer().rpc("eth_createAccessList") do( |
| 139 | + tx: TransactionArgs, blockTag: BlockTag, optimisticStateFetch: Opt[bool] |
| 140 | + ) -> AccessListResult: |
| 141 | + await frontend.eth_createAccessList(tx, blockTag, optimisticStateFetch.get(true)) |
| 142 | + |
| 143 | + server.getServer().rpc("eth_estimateGas") do( |
| 144 | + tx: TransactionArgs, blockTag: BlockTag, optimisticStateFetch: Opt[bool] |
| 145 | + ) -> Quantity: |
| 146 | + await frontend.eth_estimateGas(tx, blockTag, optimisticStateFetch.get(true)) |
| 147 | + |
| 148 | + server.getServer().rpc("eth_getTransactionByHash") do( |
| 149 | + txHash: Hash32 |
| 150 | + ) -> TransactionObject: |
| 151 | + await frontend.eth_getTransactionByHash(txHash) |
| 152 | + |
| 153 | + server.getServer().rpc("eth_getBlockReceipts") do( |
| 154 | + blockTag: BlockTag |
| 155 | + ) -> Opt[seq[ReceiptObject]]: |
| 156 | + await frontend.eth_getBlockReceipts(blockTag) |
| 157 | + |
| 158 | + server.getServer().rpc("eth_getTransactionReceipt") do( |
| 159 | + txHash: Hash32 |
| 160 | + ) -> ReceiptObject: |
| 161 | + await frontend.eth_getTransactionReceipt(txHash) |
| 162 | + |
| 163 | + server.getServer().rpc("eth_getLogs") do( |
| 164 | + filterOptions: FilterOptions |
| 165 | + ) -> seq[LogObject]: |
| 166 | + await frontend.eth_getLogs(filterOptions) |
| 167 | + |
| 168 | + server.getServer().rpc("eth_newFilter") do(filterOptions: FilterOptions) -> string: |
| 169 | + await frontend.eth_newFilter(filterOptions) |
| 170 | + |
| 171 | + server.getServer().rpc("eth_uninstallFilter") do(filterId: string) -> bool: |
| 172 | + await frontend.eth_uninstallFilter(filterId) |
| 173 | + |
| 174 | + server.getServer().rpc("eth_getFilterLogs") do(filterId: string) -> seq[LogObject]: |
| 175 | + await frontend.eth_getFilterLogs(filterId) |
| 176 | + |
| 177 | + server.getServer().rpc("eth_getFilterChanges") do(filterId: string) -> seq[LogObject]: |
| 178 | + await frontend.eth_getFilterChanges(filterId) |
| 179 | + |
| 180 | + server.getServer().rpc("eth_blobBaseFee") do() -> UInt256: |
| 181 | + await frontend.eth_blobBaseFee() |
| 182 | + |
| 183 | + server.getServer().rpc("eth_gasPrice") do() -> Quantity: |
| 184 | + await frontend.eth_gasPrice() |
| 185 | + |
| 186 | + server.getServer().rpc("eth_maxPriorityFeePerGas") do() -> Quantity: |
| 187 | + await frontend.eth_maxPriorityFeePerGas() |
| 188 | + |
| 189 | +proc stop*(server: JsonRpcServer) {.async: (raises: [CancelledError]).} = |
| 190 | + try: |
| 191 | + case server.kind |
| 192 | + of Http: |
| 193 | + await server.httpServer.closeWait() |
| 194 | + of WebSocket: |
| 195 | + await server.wsServer.closeWait() |
| 196 | + except CatchableError as e: |
| 197 | + raise newException(CancelledError, e.msg) |
0 commit comments