2021-11-24 07:45:55 +00:00
|
|
|
# Nimbus
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2021-2024 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-11-24 07:45:55 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/sequtils,
|
2024-02-28 17:31:45 +00:00
|
|
|
json_rpc/[rpcproxy, rpcserver],
|
|
|
|
stew/byteutils,
|
2021-11-24 07:45:55 +00:00
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
|
|
|
./rpc_types
|
|
|
|
|
2021-11-29 09:39:37 +00:00
|
|
|
export rpc_types # tasty sandwich
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
type PongResponse* = object
|
|
|
|
enrSeq: uint64
|
|
|
|
recipientIP: string
|
|
|
|
recipientPort: uint16
|
2021-11-29 09:39:37 +00:00
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
PongResponse.useDefaultSerializationIn JrpcConv
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc installDiscoveryApiHandlers*(
|
|
|
|
rpcServer: RpcServer | RpcProxy, d: discv5_protocol.Protocol
|
|
|
|
) =
|
2021-11-24 07:45:55 +00:00
|
|
|
## Discovery v5 JSON-RPC API such as defined here:
|
2022-12-15 15:24:23 +00:00
|
|
|
## https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
|
2021-11-24 07:45:55 +00:00
|
|
|
|
2021-11-29 09:39:37 +00:00
|
|
|
rpcServer.rpc("discv5_nodeInfo") do() -> NodeInfo:
|
2021-11-24 07:45:55 +00:00
|
|
|
return d.routingTable.getNodeInfo()
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
rpcServer.rpc("discv5_updateNodeInfo") do(kvPairs: seq[(string, string)]) -> NodeInfo:
|
2023-05-09 12:34:01 +00:00
|
|
|
# TODO: Not according to spec, as spec only allows socket address.
|
|
|
|
# portal-specs PR has been created with suggested change as is here.
|
2021-11-29 09:39:37 +00:00
|
|
|
let enrFields = kvPairs.map(
|
2023-01-26 12:37:19 +00:00
|
|
|
proc(n: (string, string)): (string, seq[byte]) {.raises: [ValueError].} =
|
2021-11-29 09:39:37 +00:00
|
|
|
(n[0], hexToSeqByte(n[1]))
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2021-11-29 09:39:37 +00:00
|
|
|
let updated = d.updateRecord(enrFields)
|
|
|
|
if updated.isErr():
|
|
|
|
raise newException(ValueError, $updated.error)
|
|
|
|
|
|
|
|
return d.routingTable.getNodeInfo()
|
|
|
|
|
2022-12-15 15:24:23 +00:00
|
|
|
rpcServer.rpc("discv5_routingTableInfo") do() -> RoutingTableInfo:
|
|
|
|
return getRoutingTableInfo(d.routingTable)
|
|
|
|
|
|
|
|
rpcServer.rpc("discv5_addEnr") do(enr: Record) -> bool:
|
|
|
|
let nodeRes = newNode(enr)
|
|
|
|
if nodeRes.isOk():
|
|
|
|
let node = nodeRes.get()
|
|
|
|
let res = d.addNode(node)
|
|
|
|
d.routingTable.setJustSeen(node)
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Failed creating Node from ENR")
|
|
|
|
|
2022-02-02 21:48:33 +00:00
|
|
|
rpcServer.rpc("discv5_addEnrs") do(enrs: seq[Record]) -> bool:
|
2022-12-15 15:24:23 +00:00
|
|
|
# Note: unspecified RPC, but useful for our local testnet test
|
2022-02-02 21:48:33 +00:00
|
|
|
# TODO: We could also adjust the API of addNode & newNode to accept a seen
|
|
|
|
# parameter, but perhaps only if that makes sense on other locations in
|
|
|
|
# discv5/portal that are not testing/debug related.
|
|
|
|
for enr in enrs:
|
|
|
|
let nodeRes = newNode(enr)
|
|
|
|
if nodeRes.isOk():
|
|
|
|
let node = nodeRes.get()
|
|
|
|
discard d.addNode(node)
|
|
|
|
d.routingTable.setJustSeen(node)
|
|
|
|
|
|
|
|
return true
|
2021-11-29 09:39:37 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("discv5_getEnr") do(nodeId: NodeId) -> Record:
|
|
|
|
let node = d.getNode(nodeId)
|
|
|
|
if node.isSome():
|
|
|
|
return node.get().record
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not in local routing table.")
|
|
|
|
|
|
|
|
rpcServer.rpc("discv5_deleteEnr") do(nodeId: NodeId) -> bool:
|
|
|
|
# TODO: Adjust `removeNode` to accept NodeId as param and to return bool.
|
|
|
|
let node = d.getNode(nodeId)
|
|
|
|
if node.isSome():
|
|
|
|
d.routingTable.removeNode(node.get())
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not in local routing table.")
|
|
|
|
|
2023-04-27 12:07:57 +00:00
|
|
|
rpcServer.rpc("discv5_lookupEnr") do(nodeId: NodeId) -> Record:
|
2021-11-29 09:39:37 +00:00
|
|
|
let lookup = await d.resolve(nodeId)
|
|
|
|
if lookup.isSome():
|
|
|
|
return lookup.get().record
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not found in DHT lookup.")
|
|
|
|
|
|
|
|
rpcServer.rpc("discv5_ping") do(enr: Record) -> PongResponse:
|
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
|
|
|
pong = await d.ping(node)
|
|
|
|
|
|
|
|
if pong.isErr():
|
|
|
|
raise newException(ValueError, $pong.error)
|
|
|
|
else:
|
|
|
|
let p = pong.get()
|
2024-02-28 17:31:45 +00:00
|
|
|
return PongResponse(enrSeq: p.enrSeq, recipientIP: $p.ip, recipientPort: p.port)
|
2021-11-29 09:39:37 +00:00
|
|
|
|
2021-12-13 13:12:51 +00:00
|
|
|
rpcServer.rpc("discv5_findNode") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
enr: Record, distances: seq[uint16]
|
|
|
|
) -> seq[Record]:
|
2021-11-29 09:39:37 +00:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
|
|
|
nodes = await d.findNode(node, distances)
|
|
|
|
if nodes.isErr():
|
|
|
|
raise newException(ValueError, $nodes.error)
|
|
|
|
else:
|
2024-02-28 17:31:45 +00:00
|
|
|
return nodes.get().map(
|
|
|
|
proc(n: Node): Record =
|
|
|
|
n.record
|
|
|
|
)
|
2021-11-29 09:39:37 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
rpcServer.rpc("discv5_talkReq") do(enr: Record, protocol, payload: string) -> string:
|
2021-11-29 09:39:37 +00:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
2024-02-28 17:31:45 +00:00
|
|
|
talkresp = await d.talkReq(node, hexToSeqByte(protocol), hexToSeqByte(payload))
|
2021-11-29 09:39:37 +00:00
|
|
|
if talkresp.isErr():
|
|
|
|
raise newException(ValueError, $talkresp.error)
|
|
|
|
else:
|
|
|
|
return talkresp.get().toHex()
|
2021-11-24 07:45:55 +00:00
|
|
|
|
2023-04-13 16:39:46 +00:00
|
|
|
rpcServer.rpc("discv5_recursiveFindNodes") do(nodeId: NodeId) -> seq[Record]:
|
2022-12-15 15:24:23 +00:00
|
|
|
let discovered = await d.lookup(nodeId)
|
2024-02-28 17:31:45 +00:00
|
|
|
return discovered.map(
|
|
|
|
proc(n: Node): Record =
|
|
|
|
n.record
|
|
|
|
)
|