2022-03-30 06:55:38 +00:00
|
|
|
# Nimbus
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2022-2023 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,
|
2022-03-30 06:55:38 +00:00
|
|
|
confutils, chronicles, chronicles/topics_registry, stew/byteutils,
|
|
|
|
eth/common/eth_types,
|
2023-12-08 09:35:50 +00:00
|
|
|
../../nimbus/rpc/[rpc_types], ../../nimbus/errors,
|
2022-03-30 06:55:38 +00:00
|
|
|
../rpc/eth_rpc_client
|
|
|
|
|
|
|
|
type
|
2023-12-08 09:35:50 +00:00
|
|
|
Hash256 = eth_types.Hash256
|
|
|
|
|
2022-03-30 06:55:38 +00:00
|
|
|
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" .}: Hash256
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type Hash256, p: string): T
|
2023-08-04 12:43:30 +00:00
|
|
|
{.raises: [ValueError].} =
|
2022-03-30 06:55:38 +00:00
|
|
|
var hash: Hash256
|
|
|
|
try:
|
|
|
|
hexToByteArray(p, hash.data)
|
|
|
|
except ValueError:
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, "Invalid Hash256")
|
2022-03-30 06:55:38 +00:00
|
|
|
|
|
|
|
return hash
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type Hash256, val: string): seq[string] =
|
2022-03-30 06:55:38 +00:00
|
|
|
return @[]
|
|
|
|
|
|
|
|
proc walkBlocks(client: RpcClient, startHash: Hash256) {.async.} =
|
2023-12-08 09:35:50 +00:00
|
|
|
var parentHash = w3Hash startHash
|
|
|
|
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
|
2022-04-12 13:49:19 +00:00
|
|
|
except ValidationError as e:
|
2022-03-30 06:55:38 +00:00
|
|
|
# ValidationError from buildBlockObject, should not occur with proper
|
|
|
|
# blocks
|
|
|
|
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.
|
|
|
|
await client.close()
|
|
|
|
|
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
|
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
echo "Block " & $blockNumber & ": " & $parentBlock.hash
|
2022-03-30 06:55:38 +00:00
|
|
|
|
|
|
|
proc run(config: BlockWalkConf) {.async.} =
|
|
|
|
let client = newRpcHttpClient()
|
|
|
|
await client.connect(config.rpcAddress, Port(config.rpcPort), false)
|
|
|
|
|
|
|
|
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)
|