2021-11-24 07:45:55 +00:00
|
|
|
# Nimbus
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2021-2023 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
|
2021-11-29 09:39:37 +00:00
|
|
|
std/sequtils,
|
2021-12-13 13:12:51 +00:00
|
|
|
json_rpc/[rpcproxy, rpcserver], stew/byteutils,
|
2021-12-20 10:57:55 +00:00
|
|
|
eth/p2p/discoveryv5/nodes_verification,
|
2021-11-24 07:45:55 +00:00
|
|
|
../network/wire/portal_protocol,
|
|
|
|
./rpc_types
|
|
|
|
|
2021-11-24 13:53:01 +00:00
|
|
|
export rpcserver
|
|
|
|
|
2023-01-17 13:47:10 +00:00
|
|
|
# Portal Network JSON-RPC impelentation as per specification:
|
|
|
|
# https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
|
|
|
|
|
2021-11-24 13:53:01 +00:00
|
|
|
# Note:
|
|
|
|
# Using a string for the network parameter will give an error in the rpc macro:
|
2021-11-24 07:45:55 +00:00
|
|
|
# Error: Invalid node kind nnkInfix for macros.`$`
|
2021-11-24 13:53:01 +00:00
|
|
|
# Using a static string works but some sandwich problem seems to be happening,
|
|
|
|
# as the proc becomes generic, where the rpc macro from router.nim can no longer
|
|
|
|
# be found, which is why we export rpcserver which should export router.
|
|
|
|
proc installPortalApiHandlers*(
|
2021-11-29 09:39:37 +00:00
|
|
|
rpcServer: RpcServer|RpcProxy, p: PortalProtocol, network: static string)
|
2023-01-31 12:38:08 +00:00
|
|
|
{.raises: [CatchableError].} =
|
2021-11-24 07:45:55 +00:00
|
|
|
|
2022-10-10 13:15:57 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "NodeInfo") do() -> NodeInfo:
|
2021-11-24 07:45:55 +00:00
|
|
|
return p.routingTable.getNodeInfo()
|
|
|
|
|
2022-10-10 13:15:57 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "RoutingTableInfo") do() -> RoutingTableInfo:
|
2021-11-24 07:45:55 +00:00
|
|
|
return getRoutingTableInfo(p.routingTable)
|
2021-11-29 09:39:37 +00:00
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "AddEnr") do(enr: Record) -> bool:
|
|
|
|
let node = newNode(enr).valueOr:
|
|
|
|
raise newException(ValueError, "Failed creating Node from ENR")
|
|
|
|
|
|
|
|
let addResult = p.addNode(node)
|
|
|
|
p.routingTable.setJustSeen(node)
|
|
|
|
return addResult == Added
|
2021-12-08 08:26:31 +00:00
|
|
|
|
2022-10-10 13:15:57 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "AddEnrs") do(enrs: seq[Record]) -> bool:
|
2022-12-16 07:49:18 +00:00
|
|
|
# Note: unspecified RPC, but useful for our local testnet test
|
2022-02-02 21:48:33 +00:00
|
|
|
for enr in enrs:
|
|
|
|
let nodeRes = newNode(enr)
|
|
|
|
if nodeRes.isOk():
|
|
|
|
let node = nodeRes.get()
|
|
|
|
discard p.addNode(node)
|
|
|
|
p.routingTable.setJustSeen(node)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "GetEnr") do(nodeId: NodeId) -> Record:
|
|
|
|
let node = p.getNode(nodeId)
|
|
|
|
if node.isSome():
|
|
|
|
return node.get().record
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not in local routing table.")
|
|
|
|
|
|
|
|
rpcServer.rpc("portal_" & network & "DeleteEnr") do(nodeId: NodeId) -> bool:
|
|
|
|
# TODO: Adjust `removeNode` to accept NodeId as param and to return bool.
|
|
|
|
let node = p.getNode(nodeId)
|
|
|
|
if node.isSome():
|
|
|
|
p.routingTable.removeNode(node.get())
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not in local routing table.")
|
|
|
|
|
|
|
|
rpcServer.rpc("portal_" & network & "LookupEnr") do(nodeId: NodeId) -> Record:
|
|
|
|
# TODO: Not fully according to spec, missing optional enrSeq
|
|
|
|
# Can add `enrSeq: Option[uint64]` as parameter but Option appears to be
|
2023-01-17 13:47:10 +00:00
|
|
|
# not implemented as an optional parameter in nim-json-rpc?
|
2022-12-16 07:49:18 +00:00
|
|
|
let lookup = await p.resolve(nodeId)
|
|
|
|
if lookup.isSome():
|
|
|
|
return lookup.get().record
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not found in DHT lookup.")
|
|
|
|
|
2022-10-10 13:15:57 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "Ping") do(
|
2022-12-16 07:49:18 +00:00
|
|
|
enr: Record) -> tuple[enrSeq: uint64, customPayload: string]:
|
|
|
|
# TODO: Not fully according to spec:
|
|
|
|
# - missing optional dataRadius
|
|
|
|
# - customPayload instead of dataRadius returned
|
2021-12-13 13:12:51 +00:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
|
|
|
pong = await p.ping(node)
|
|
|
|
|
|
|
|
if pong.isErr():
|
|
|
|
raise newException(ValueError, $pong.error)
|
|
|
|
else:
|
|
|
|
let p = pong.get()
|
|
|
|
return (p.enrSeq, p.customPayload.asSeq().toHex())
|
|
|
|
|
2022-10-10 13:15:57 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "FindNodes") do(
|
2021-12-13 13:12:51 +00:00
|
|
|
enr: Record, distances: seq[uint16]) -> seq[Record]:
|
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
2022-03-19 07:54:42 +00:00
|
|
|
nodes = await p.findNodes(node, distances)
|
2021-12-13 13:12:51 +00:00
|
|
|
if nodes.isErr():
|
|
|
|
raise newException(ValueError, $nodes.error)
|
|
|
|
else:
|
|
|
|
return nodes.get().map(proc(n: Node): Record = n.record)
|
|
|
|
|
2021-12-20 10:57:55 +00:00
|
|
|
# TODO: This returns null values for the `none`s. Not sure what it should be
|
|
|
|
# according to spec, no k:v pair at all?
|
2022-12-16 07:49:18 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "FindContent") do(
|
2021-12-20 10:57:55 +00:00
|
|
|
enr: Record, contentKey: string) -> tuple[
|
|
|
|
connectionId: Option[string],
|
|
|
|
content: Option[string],
|
|
|
|
enrs: Option[seq[Record]]]:
|
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
2022-12-16 07:49:18 +00:00
|
|
|
res = await p.findContentImpl(
|
2021-12-20 10:57:55 +00:00
|
|
|
node, ByteList.init(hexToSeqByte(contentKey)))
|
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
if res.isErr():
|
|
|
|
raise newException(ValueError, $res.error)
|
|
|
|
|
|
|
|
let contentMessage = res.get()
|
|
|
|
case contentMessage.contentMessageType:
|
|
|
|
of connectionIdType:
|
|
|
|
return (
|
|
|
|
some("0x" & contentMessage.connectionId.toHex()),
|
|
|
|
none(string),
|
|
|
|
none(seq[Record]))
|
|
|
|
of contentType:
|
|
|
|
return (
|
|
|
|
none(string),
|
|
|
|
some("0x" & contentMessage.content.asSeq().toHex()),
|
|
|
|
none(seq[Record]))
|
|
|
|
of enrsType:
|
|
|
|
let records = recordsFromBytes(contentMessage.enrs)
|
|
|
|
if records.isErr():
|
|
|
|
raise newException(ValueError, $records.error)
|
|
|
|
else:
|
2021-12-20 10:57:55 +00:00
|
|
|
return (
|
|
|
|
none(string),
|
|
|
|
none(string),
|
2022-12-16 07:49:18 +00:00
|
|
|
# Note: Could also pass not verified nodes
|
|
|
|
some(verifyNodesRecords(
|
|
|
|
records.get(), node, enrsResultLimit).map(
|
|
|
|
proc(n: Node): Record = n.record)))
|
2021-12-20 10:57:55 +00:00
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "FindContentFull") do(
|
2022-01-14 15:07:14 +00:00
|
|
|
enr: Record, contentKey: string) -> tuple[
|
|
|
|
content: Option[string], enrs: Option[seq[Record]]]:
|
2022-12-16 07:49:18 +00:00
|
|
|
# Note: unspecified RPC, but useful as we can get content from uTP also
|
2022-01-14 15:07:14 +00:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
|
|
|
foundContentResult = await p.findContent(
|
|
|
|
node, ByteList.init(hexToSeqByte(contentKey)))
|
|
|
|
|
|
|
|
if foundContentResult.isErr():
|
|
|
|
raise newException(ValueError, $foundContentResult.error)
|
|
|
|
else:
|
|
|
|
let foundContent = foundContentResult.get()
|
|
|
|
case foundContent.kind:
|
|
|
|
of Content:
|
|
|
|
return (
|
2022-01-20 20:21:20 +00:00
|
|
|
some("0x" & foundContent.content.toHex()),
|
2022-01-14 15:07:14 +00:00
|
|
|
none(seq[Record]))
|
|
|
|
of Nodes:
|
|
|
|
return (
|
|
|
|
none(string),
|
|
|
|
some(foundContent.nodes.map(proc(n: Node): Record = n.record)))
|
|
|
|
|
2023-01-17 13:47:10 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "Offer") do(
|
|
|
|
enr: Record, contentKey: string, contentValue: string) -> string:
|
2022-12-16 16:47:52 +00:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
|
|
|
key = hexToSeqByte(contentKey)
|
|
|
|
content = hexToSeqByte(contentValue)
|
|
|
|
contentInfo = ContentInfo(contentKey: ByteList.init(key), content: content)
|
|
|
|
res = await p.offer(node, @[contentInfo])
|
|
|
|
|
|
|
|
if res.isOk():
|
2023-01-17 13:47:10 +00:00
|
|
|
return "0x" & SSZ.encode(res.get()).toHex()
|
2022-12-16 16:47:52 +00:00
|
|
|
else:
|
|
|
|
raise newException(ValueError, $res.error)
|
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "RecursiveFindNodes") do(
|
|
|
|
nodeId: NodeId) -> seq[Record]:
|
|
|
|
let discovered = await p.lookup(nodeId)
|
2021-11-29 09:39:37 +00:00
|
|
|
return discovered.map(proc(n: Node): Record = n.record)
|
2022-12-13 18:22:36 +00:00
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "RecursiveFindContent") do(
|
|
|
|
contentKey: string) -> string:
|
|
|
|
let
|
|
|
|
key = ByteList.init(hexToSeqByte(contentKey))
|
|
|
|
contentId = p.toContentId(key).valueOr:
|
|
|
|
raise newException(ValueError, "Invalid content key")
|
|
|
|
|
|
|
|
contentResult = (await p.contentLookup(key, contentId)).valueOr:
|
|
|
|
raise newException(ValueError, "Content not found")
|
|
|
|
|
|
|
|
return contentResult.content.toHex()
|
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "Store") do(
|
2022-12-16 07:49:18 +00:00
|
|
|
contentKey: string, contentValue: string) -> bool:
|
2022-12-13 18:22:36 +00:00
|
|
|
let key = ByteList.init(hexToSeqByte(contentKey))
|
|
|
|
let contentId = p.toContentId(key)
|
|
|
|
|
|
|
|
if contentId.isSome():
|
2022-12-16 07:49:18 +00:00
|
|
|
p.storeContent(key, contentId.get(), hexToSeqByte(contentValue))
|
2022-12-13 18:22:36 +00:00
|
|
|
return true
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Invalid content key")
|
2022-12-16 10:00:10 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("portal_" & network & "LocalContent") do(
|
|
|
|
contentKey: string) -> string:
|
|
|
|
let
|
|
|
|
key = ByteList.init(hexToSeqByte(contentKey))
|
|
|
|
contentId = p.toContentId(key).valueOr:
|
|
|
|
raise newException(ValueError, "Invalid content key")
|
|
|
|
|
|
|
|
let contentResult = p.dbGet(key, contentId)
|
|
|
|
if contentResult.isOk():
|
|
|
|
return contentResult.get().toHex()
|
|
|
|
else:
|
|
|
|
return "0x0"
|
2023-01-17 13:47:10 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("portal_" & network & "Gossip") do(
|
|
|
|
contentKey: string, contentValue: string) -> int:
|
|
|
|
let
|
|
|
|
key = hexToSeqByte(contentKey)
|
|
|
|
content = hexToSeqByte(contentValue)
|
|
|
|
contentKeys = ContentKeysList(@[ByteList.init(key)])
|
|
|
|
numberOfPeers = await p.neighborhoodGossip(contentKeys, @[content])
|
|
|
|
|
|
|
|
return numberOfPeers
|