2022-03-30 06:55:38 +00:00
|
|
|
# Nimbus
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-03-30 06:55:38 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# Testing tool to verify that a specific range of blocks can be fetched from
|
|
|
|
# the Portal network.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-03-30 06:55:38 +00:00
|
|
|
|
|
|
|
import
|
2022-04-12 13:49:19 +00:00
|
|
|
std/strutils,
|
2024-02-28 17:31:45 +00:00
|
|
|
confutils,
|
|
|
|
chronicles,
|
|
|
|
chronicles/topics_registry,
|
|
|
|
stew/byteutils,
|
2024-03-13 15:58:50 +00:00
|
|
|
web3/primitives,
|
2022-03-30 06:55:38 +00:00
|
|
|
../rpc/eth_rpc_client
|
|
|
|
|
2024-03-13 15:58:50 +00:00
|
|
|
type BlockWalkConf* = object
|
|
|
|
logLevel* {.
|
|
|
|
defaultValue: LogLevel.INFO,
|
|
|
|
defaultValueDesc: $LogLevel.INFO,
|
|
|
|
desc: "Sets the log level",
|
|
|
|
name: "log-level"
|
|
|
|
.}: LogLevel
|
|
|
|
|
|
|
|
rpcAddress* {.
|
|
|
|
desc: "Address of the JSON-RPC service",
|
|
|
|
defaultValue: "127.0.0.1",
|
|
|
|
name: "rpc-address"
|
|
|
|
.}: string
|
|
|
|
|
|
|
|
rpcPort* {.
|
|
|
|
defaultValue: 8545, desc: "Port of the JSON-RPC service", name: "rpc-port"
|
|
|
|
.}: uint16
|
|
|
|
|
|
|
|
blockHash* {.
|
|
|
|
desc: "The block hash from where to start walking the blocks backwards",
|
|
|
|
name: "block-hash"
|
|
|
|
.}: BlockHash
|
|
|
|
|
|
|
|
proc parseCmdArg*(T: type BlockHash, p: string): T {.raises: [ValueError].} =
|
|
|
|
var hash: array[32, byte]
|
2022-03-30 06:55:38 +00:00
|
|
|
try:
|
2024-03-13 15:58:50 +00:00
|
|
|
hexToByteArray(p, hash)
|
2022-03-30 06:55:38 +00:00
|
|
|
except ValueError:
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, "Invalid Hash256")
|
2022-03-30 06:55:38 +00:00
|
|
|
|
2024-03-13 15:58:50 +00:00
|
|
|
return BlockHash(hash)
|
2022-03-30 06:55:38 +00:00
|
|
|
|
2024-03-13 15:58:50 +00:00
|
|
|
proc completeCmdArg*(T: type BlockHash, val: string): seq[string] =
|
2022-03-30 06:55:38 +00:00
|
|
|
return @[]
|
|
|
|
|
2024-03-13 15:58:50 +00:00
|
|
|
proc walkBlocks(client: RpcClient, startHash: BlockHash) {.async: (raises: []).} =
|
|
|
|
var parentHash = startHash
|
2023-12-08 09:35:50 +00:00
|
|
|
var blockNumber: Quantity
|
2022-03-30 06:55:38 +00:00
|
|
|
|
|
|
|
# Should be 0x0, but block 0 does not exist in the json data file
|
2023-12-08 09:35:50 +00:00
|
|
|
while blockNumber != Quantity(0x1):
|
2022-03-30 06:55:38 +00:00
|
|
|
let parentBlockOpt =
|
|
|
|
try:
|
2023-12-08 09:35:50 +00:00
|
|
|
await client.eth_getBlockByHash(parentHash, false)
|
2022-04-12 13:49:19 +00:00
|
|
|
except RpcPostError as e:
|
2022-03-30 06:55:38 +00:00
|
|
|
# RpcPostError when for example timing out on the request. Could retry
|
|
|
|
# in this case.
|
|
|
|
fatal "Error occured on JSON-RPC request", error = e.msg
|
|
|
|
quit 1
|
2024-03-13 15:58:50 +00:00
|
|
|
except CatchableError as e:
|
2022-03-30 06:55:38 +00:00
|
|
|
fatal "Error occured on JSON-RPC request", error = e.msg
|
|
|
|
quit 1
|
|
|
|
|
2022-04-11 17:39:45 +00:00
|
|
|
# Using the http connection re-use seems to slow down these sequentual
|
|
|
|
# requests considerably. Force a new connection setup by doing a close after
|
|
|
|
# each request.
|
2024-03-13 15:58:50 +00:00
|
|
|
try:
|
|
|
|
await client.close()
|
|
|
|
except CatchableError as e:
|
|
|
|
fatal "Error closing RPC client connection", error = e.msg
|
|
|
|
quit 1
|
2022-04-11 17:39:45 +00:00
|
|
|
|
2022-03-30 06:55:38 +00:00
|
|
|
if parentBlockOpt.isNone():
|
2023-12-08 09:35:50 +00:00
|
|
|
fatal "Failed getting parent block", hash = parentHash
|
2022-03-30 06:55:38 +00:00
|
|
|
quit 1
|
|
|
|
|
|
|
|
let parentBlock = parentBlockOpt.get()
|
2023-12-08 09:35:50 +00:00
|
|
|
blockNumber = parentBlock.number
|
2022-03-30 06:55:38 +00:00
|
|
|
parentHash = parentBlock.parentHash
|
|
|
|
|
2024-03-13 15:58:50 +00:00
|
|
|
echo "Block " & $distinctBase(blockNumber) & ": " & $parentBlock.hash
|
2022-03-30 06:55:38 +00:00
|
|
|
|
2024-03-13 15:58:50 +00:00
|
|
|
proc run(config: BlockWalkConf) {.async: (raises: []).} =
|
2022-03-30 06:55:38 +00:00
|
|
|
let client = newRpcHttpClient()
|
2024-03-13 15:58:50 +00:00
|
|
|
try:
|
|
|
|
await client.connect(config.rpcAddress, Port(config.rpcPort), false)
|
|
|
|
except CatchableError as e:
|
|
|
|
fatal "Error connecting to JSON-RPC service", error = e.msg
|
|
|
|
quit 1
|
2022-03-30 06:55:38 +00:00
|
|
|
|
|
|
|
await walkBlocks(client, config.blockHash)
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
{.pop.}
|
|
|
|
let config = BlockWalkConf.load()
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-03-30 06:55:38 +00:00
|
|
|
|
|
|
|
setLogLevel(config.logLevel)
|
|
|
|
|
|
|
|
waitFor run(config)
|