2021-11-24 07:45:55 +00:00
|
|
|
# Nimbus
|
2022-12-13 18:22:36 +00:00
|
|
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
2021-11-24 07:45:55 +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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2021-11-29 09:39:37 +00:00
|
|
|
json_rpc/jsonmarshal,
|
|
|
|
stew/results,
|
2021-11-24 07:45:55 +00:00
|
|
|
eth/p2p/discoveryv5/[routing_table, enr, node]
|
|
|
|
|
2021-12-03 08:51:25 +00:00
|
|
|
export jsonmarshal, routing_table, enr, node
|
2021-11-29 09:39:37 +00:00
|
|
|
|
2021-11-24 07:45:55 +00:00
|
|
|
type
|
|
|
|
NodeInfo* = object
|
2022-12-13 18:22:36 +00:00
|
|
|
enr*: Record
|
2021-12-03 08:51:25 +00:00
|
|
|
nodeId*: NodeId
|
2021-11-24 07:45:55 +00:00
|
|
|
|
|
|
|
RoutingTableInfo* = object
|
2022-12-15 15:24:23 +00:00
|
|
|
localNodeId*: NodeId
|
2021-12-03 08:51:25 +00:00
|
|
|
buckets*: seq[seq[NodeId]]
|
2021-11-24 07:45:55 +00:00
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
func getNodeInfo*(r: RoutingTable): NodeInfo =
|
|
|
|
NodeInfo(enr: r.localNode.record, nodeId: r.localNode.id)
|
2021-11-24 07:45:55 +00:00
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
func getRoutingTableInfo*(r: RoutingTable): RoutingTableInfo =
|
2021-11-24 07:45:55 +00:00
|
|
|
var info: RoutingTableInfo
|
|
|
|
for b in r.buckets:
|
2021-11-29 09:39:37 +00:00
|
|
|
var bucket: seq[NodeId]
|
2021-11-24 07:45:55 +00:00
|
|
|
for n in b.nodes:
|
2021-11-29 09:39:37 +00:00
|
|
|
bucket.add(n.id)
|
2021-11-24 07:45:55 +00:00
|
|
|
|
|
|
|
info.buckets.add(bucket)
|
|
|
|
|
2022-12-15 15:24:23 +00:00
|
|
|
info.localNodeId = r.localNode.id
|
2021-11-24 07:45:55 +00:00
|
|
|
|
|
|
|
info
|
2021-11-29 09:39:37 +00:00
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
func toNodeWithAddress*(enr: Record): Node {.raises: [Defect, ValueError].} =
|
2021-11-29 09:39:37 +00:00
|
|
|
let nodeRes = newNode(enr)
|
|
|
|
if nodeRes.isErr():
|
|
|
|
raise newException(ValueError, $nodeRes.error)
|
|
|
|
|
|
|
|
let node = nodeRes.get()
|
|
|
|
if node.address.isNone():
|
|
|
|
raise newException(ValueError, "ENR without address")
|
|
|
|
else:
|
|
|
|
node
|
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
func `%`*(value: Record): JsonNode =
|
2021-11-29 09:39:37 +00:00
|
|
|
newJString(value.toURI())
|
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
func fromJson*(n: JsonNode, argName: string, result: var Record)
|
2021-11-29 09:39:37 +00:00
|
|
|
{.raises: [Defect, ValueError].} =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
if not fromURI(result, n.getStr()):
|
|
|
|
raise newException(ValueError, "Invalid ENR")
|
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
func `%`*(value: NodeId): JsonNode =
|
2021-11-29 09:39:37 +00:00
|
|
|
%("0x" & value.toHex())
|
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
func fromJson*(n: JsonNode, argName: string, result: var NodeId)
|
2021-11-29 09:39:37 +00:00
|
|
|
{.raises: [Defect, ValueError].} =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
|
|
|
|
# TODO: fromHex (and thus parse) call seems to let pass several invalid
|
|
|
|
# UInt256.
|
|
|
|
result = UInt256.fromHex(n.getStr())
|
|
|
|
|
|
|
|
# TODO: This one should go to nim-json-rpc but before we can do that we will
|
|
|
|
# have to update the vendor module to the current latest.
|
2022-12-13 18:22:36 +00:00
|
|
|
func fromJson*(n: JsonNode, argName: string, result: var uint16)
|
2021-11-29 09:39:37 +00:00
|
|
|
{.raises: [Defect, ValueError].} =
|
|
|
|
n.kind.expect(JInt, argName)
|
|
|
|
let asInt = n.getBiggestInt()
|
|
|
|
if asInt < 0:
|
|
|
|
raise newException(
|
|
|
|
ValueError, "JSON-RPC input is an unexpected negative value")
|
|
|
|
if asInt > BiggestInt(uint16.high()):
|
|
|
|
raise newException(
|
|
|
|
ValueError, "JSON-RPC input is too large for uint32")
|
|
|
|
|
|
|
|
result = uint16(asInt)
|