2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2020-2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2019-01-06 15:21:34 +00:00
|
|
|
import
|
2022-12-02 04:39:12 +00:00
|
|
|
std/[json, strutils],
|
|
|
|
json_rpc/[rpcclient], httputils,
|
|
|
|
eth/[common, rlp], chronicles,
|
|
|
|
../nimbus/utils/utils,
|
|
|
|
./parser
|
2019-01-06 15:21:34 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "downloader"
|
|
|
|
|
|
|
|
type
|
|
|
|
Block* = object
|
|
|
|
header*: BlockHeader
|
|
|
|
body*: BlockBody
|
|
|
|
traces*: JsonNode
|
|
|
|
receipts*: seq[Receipt]
|
|
|
|
jsonData*: JsonNode
|
|
|
|
|
|
|
|
DownloadFlags* = enum
|
|
|
|
DownloadReceipts
|
|
|
|
DownloadTxTrace
|
2019-03-13 02:07:05 +00:00
|
|
|
DownloadAndValidate
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2022-06-22 06:50:58 +00:00
|
|
|
proc request*(
|
|
|
|
methodName: string,
|
|
|
|
params: JsonNode,
|
|
|
|
client: Option[RpcClient] = none[RpcClient]()): JsonNode =
|
|
|
|
if client.isSome():
|
|
|
|
result = waitFor client.unsafeGet().call(methodName, params)
|
|
|
|
else:
|
|
|
|
var client = newRpcHttpClient()
|
|
|
|
#client.httpMethod(MethodPost)
|
|
|
|
waitFor client.connect("127.0.0.1", Port(8545), false)
|
|
|
|
result = waitFor client.call(methodName, params)
|
|
|
|
waitFor client.close()
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2022-06-22 06:50:58 +00:00
|
|
|
proc requestBlockBody(
|
|
|
|
n: JsonNode,
|
|
|
|
blockNumber: BlockNumber,
|
|
|
|
client: Option[RpcClient] = none[RpcClient]()): BlockBody =
|
2019-01-06 15:21:34 +00:00
|
|
|
let txs = n["transactions"]
|
|
|
|
if txs.len > 0:
|
|
|
|
result.transactions = newSeqOfCap[Transaction](txs.len)
|
|
|
|
for tx in txs:
|
|
|
|
let txn = parseTransaction(tx)
|
2021-09-14 09:49:31 +00:00
|
|
|
validateTxSenderAndHash(tx, txn)
|
2019-01-06 15:21:34 +00:00
|
|
|
result.transactions.add txn
|
|
|
|
|
|
|
|
let uncles = n["uncles"]
|
|
|
|
if uncles.len > 0:
|
|
|
|
result.uncles = newSeqOfCap[BlockHeader](uncles.len)
|
|
|
|
let blockNumber = blockNumber.prefixHex
|
|
|
|
for i in 0 ..< uncles.len:
|
|
|
|
let idx = i.prefixHex
|
2022-06-22 06:50:58 +00:00
|
|
|
let uncle = request("eth_getUncleByBlockNumberAndIndex", %[%blockNumber, %idx], client)
|
2019-01-06 15:21:34 +00:00
|
|
|
if uncle.kind == JNull:
|
|
|
|
error "requested uncle not available", blockNumber=blockNumber, uncleIdx=i
|
|
|
|
raise newException(ValueError, "Error when retrieving block uncles")
|
|
|
|
result.uncles.add parseBlockHeader(uncle)
|
|
|
|
|
2022-06-22 06:50:58 +00:00
|
|
|
proc requestReceipts(
|
|
|
|
n: JsonNode,
|
|
|
|
client: Option[RpcClient] = none[RpcClient]()): seq[Receipt] =
|
2019-01-06 15:21:34 +00:00
|
|
|
let txs = n["transactions"]
|
|
|
|
if txs.len > 0:
|
|
|
|
result = newSeqOfCap[Receipt](txs.len)
|
|
|
|
for tx in txs:
|
|
|
|
let txHash = tx["hash"]
|
2022-06-22 06:50:58 +00:00
|
|
|
let rec = request("eth_getTransactionReceipt", %[txHash], client)
|
2019-01-06 15:21:34 +00:00
|
|
|
if rec.kind == JNull:
|
|
|
|
error "requested receipt not available", txHash=txHash
|
|
|
|
raise newException(ValueError, "Error when retrieving block receipts")
|
|
|
|
result.add parseReceipt(rec)
|
|
|
|
|
2022-06-22 06:50:58 +00:00
|
|
|
proc requestTxTraces(
|
|
|
|
n: JsonNode,
|
|
|
|
client: Option[RpcClient] = none[RpcClient]()): JsonNode =
|
2019-01-11 06:53:18 +00:00
|
|
|
result = newJArray()
|
2019-01-06 15:21:34 +00:00
|
|
|
let txs = n["transactions"]
|
2019-01-11 06:53:18 +00:00
|
|
|
if txs.len == 0: return
|
|
|
|
for tx in txs:
|
|
|
|
let txHash = tx["hash"]
|
2022-06-22 06:50:58 +00:00
|
|
|
let txTrace = request("debug_traceTransaction", %[txHash], client)
|
2019-01-11 06:53:18 +00:00
|
|
|
if txTrace.kind == JNull:
|
|
|
|
error "requested trace not available", txHash=txHash
|
|
|
|
raise newException(ValueError, "Error when retrieving transaction trace")
|
|
|
|
result.add txTrace
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2022-06-22 06:50:58 +00:00
|
|
|
proc requestHeader*(
|
|
|
|
blockNumber: BlockNumber,
|
|
|
|
client: Option[RpcClient] = none[RpcClient]()): JsonNode =
|
|
|
|
result = request("eth_getBlockByNumber", %[%blockNumber.prefixHex, %true], client)
|
2019-02-22 08:52:53 +00:00
|
|
|
if result.kind == JNull:
|
2019-01-06 15:21:34 +00:00
|
|
|
error "requested block not available", blockNumber=blockNumber
|
|
|
|
raise newException(ValueError, "Error when retrieving block header")
|
|
|
|
|
2022-06-22 06:50:58 +00:00
|
|
|
proc requestBlock*(
|
|
|
|
blockNumber: BlockNumber,
|
|
|
|
flags: set[DownloadFlags] = {},
|
|
|
|
client: Option[RpcClient] = none[RpcClient]()): Block =
|
|
|
|
let header = requestHeader(blockNumber, client)
|
2019-01-06 15:21:34 +00:00
|
|
|
result.jsonData = header
|
|
|
|
result.header = parseBlockHeader(header)
|
2022-06-22 06:50:58 +00:00
|
|
|
result.body = requestBlockBody(header, blockNumber, client)
|
2019-01-06 15:21:34 +00:00
|
|
|
|
|
|
|
if DownloadTxTrace in flags:
|
2022-06-22 06:50:58 +00:00
|
|
|
result.traces = requestTxTraces(header, client)
|
2019-01-06 15:21:34 +00:00
|
|
|
|
|
|
|
if DownloadReceipts in flags:
|
2022-06-22 06:50:58 +00:00
|
|
|
result.receipts = requestReceipts(header, client)
|
2019-01-06 15:21:34 +00:00
|
|
|
let
|
|
|
|
receiptRoot = calcReceiptRoot(result.receipts).prefixHex
|
|
|
|
receiptRootOK = result.header.receiptRoot.prefixHex
|
|
|
|
if receiptRoot != receiptRootOK:
|
|
|
|
debug "wrong receipt root", receiptRoot, receiptRootOK, blockNumber
|
|
|
|
raise newException(ValueError, "Error when validating receipt root")
|
|
|
|
|
2019-03-13 02:07:05 +00:00
|
|
|
if DownloadAndValidate in flags:
|
|
|
|
let
|
|
|
|
txRoot = calcTxRoot(result.body.transactions).prefixHex
|
|
|
|
txRootOK = result.header.txRoot.prefixHex
|
|
|
|
ommersHash = rlpHash(result.body.uncles).prefixHex
|
|
|
|
ommersHashOK = result.header.ommersHash.prefixHex
|
|
|
|
headerHash = rlpHash(result.header).prefixHex
|
|
|
|
headerHashOK = header["hash"].getStr().toLowerAscii
|
|
|
|
|
|
|
|
if txRoot != txRootOK:
|
|
|
|
debug "wrong tx root", txRoot, txRootOK, blockNumber
|
|
|
|
raise newException(ValueError, "Error when validating tx root")
|
|
|
|
|
|
|
|
if ommersHash != ommersHashOK:
|
|
|
|
debug "wrong ommers hash", ommersHash, ommersHashOK, blockNumber
|
|
|
|
raise newException(ValueError, "Error when validating ommers hash")
|
|
|
|
|
|
|
|
if headerHash != headerHashOK:
|
|
|
|
debug "wrong header hash", headerHash, headerHashOK, blockNumber
|
|
|
|
raise newException(ValueError, "Error when validating block header hash")
|