2024-01-25 10:04:09 +00:00
|
|
|
# fluffy
|
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
|
2024-01-13 01:41:57 +00:00
|
|
|
std/[sequtils, json],
|
|
|
|
json_rpc/[rpcproxy, rpcserver],
|
|
|
|
json_serialization/std/tables,
|
|
|
|
stew/byteutils,
|
2021-11-24 07:45:55 +00:00
|
|
|
../network/wire/portal_protocol,
|
2024-06-11 13:01:35 +00:00
|
|
|
../network/state/state_content,
|
2021-11-24 07:45:55 +00:00
|
|
|
./rpc_types
|
|
|
|
|
2024-03-06 07:57:09 +00:00
|
|
|
{.warning[UnusedImport]: off.}
|
|
|
|
import json_rpc/errors
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
export rpcserver, tables
|
2021-11-24 13:53:01 +00:00
|
|
|
|
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
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
type ContentInfo = object
|
|
|
|
content: string
|
|
|
|
utpTransfer: bool
|
2023-08-24 16:19:29 +00:00
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
ContentInfo.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceContentLookupResult.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceObject.useDefaultSerializationIn JrpcConv
|
|
|
|
NodeMetadata.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceResponse.useDefaultSerializationIn JrpcConv
|
2023-10-30 14:48:06 +00:00
|
|
|
|
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*(
|
2024-02-28 17:31:45 +00:00
|
|
|
rpcServer: RpcServer | RpcProxy, p: PortalProtocol, network: static string
|
|
|
|
) =
|
2024-06-11 13:01:35 +00:00
|
|
|
let
|
|
|
|
invalidKeyErr =
|
|
|
|
(ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
|
|
|
|
invalidValueErr =
|
|
|
|
(ref errors.InvalidRequest)(code: -32602, msg: "Invalid content value")
|
|
|
|
|
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:
|
2024-06-27 15:59:08 +00:00
|
|
|
let node = Node.fromRecord(enr)
|
2022-12-16 07:49:18 +00:00
|
|
|
let addResult = p.addNode(node)
|
2024-06-27 15:59:08 +00:00
|
|
|
if addResult == Added:
|
|
|
|
p.routingTable.setJustSeen(node)
|
2022-12-16 07:49:18 +00:00
|
|
|
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:
|
2024-06-27 15:59:08 +00:00
|
|
|
let node = Node.fromRecord(enr)
|
|
|
|
if p.addNode(node) == Added:
|
2022-02-02 21:48:33 +00:00
|
|
|
p.routingTable.setJustSeen(node)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "GetEnr") do(nodeId: NodeId) -> Record:
|
2023-08-23 16:18:03 +00:00
|
|
|
if p.localNode.id == nodeId:
|
|
|
|
return p.localNode.record
|
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
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:
|
2023-08-23 19:04:17 +00:00
|
|
|
return false
|
2022-12-16 07:49:18 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("portal_" & network & "LookupEnr") do(nodeId: NodeId) -> Record:
|
|
|
|
let lookup = await p.resolve(nodeId)
|
|
|
|
if lookup.isSome():
|
|
|
|
return lookup.get().record
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not found in DHT lookup.")
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "Ping") do(enr: Record) -> PingResult:
|
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:
|
2023-04-21 15:00:55 +00:00
|
|
|
let
|
|
|
|
p = pong.get()
|
|
|
|
# Note: the SSZ.decode cannot fail here as it has already been verified
|
|
|
|
# in the ping call.
|
|
|
|
decodedPayload =
|
2024-02-28 17:31:45 +00:00
|
|
|
try:
|
|
|
|
SSZ.decode(p.customPayload.asSeq(), CustomPayload)
|
2023-04-21 15:00:55 +00:00
|
|
|
except MalformedSszError, SszSizeMismatchError:
|
|
|
|
raiseAssert("Already verified")
|
2024-02-28 17:31:45 +00:00
|
|
|
return (p.enrSeq, decodedPayload.dataRadius)
|
2021-12-13 13:12:51 +00:00
|
|
|
|
2022-10-10 13:15:57 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "FindNodes") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
enr: Record, distances: seq[uint16]
|
|
|
|
) -> seq[Record]:
|
2021-12-13 13:12:51 +00:00
|
|
|
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:
|
2024-02-28 17:31:45 +00:00
|
|
|
return nodes.get().map(
|
|
|
|
proc(n: Node): Record =
|
|
|
|
n.record
|
|
|
|
)
|
2021-12-13 13:12:51 +00:00
|
|
|
|
2022-12-16 07:49:18 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "FindContent") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
enr: Record, contentKey: string
|
|
|
|
) -> JsonString:
|
2022-01-14 15:07:14 +00:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
2024-02-28 17:31:45 +00:00
|
|
|
foundContentResult =
|
2024-07-17 15:07:27 +00:00
|
|
|
await p.findContent(node, ContentKeyByteList.init(hexToSeqByte(contentKey)))
|
2022-01-14 15:07:14 +00:00
|
|
|
|
|
|
|
if foundContentResult.isErr():
|
|
|
|
raise newException(ValueError, $foundContentResult.error)
|
|
|
|
else:
|
|
|
|
let foundContent = foundContentResult.get()
|
2024-02-28 17:31:45 +00:00
|
|
|
case foundContent.kind
|
2022-01-14 15:07:14 +00:00
|
|
|
of Content:
|
2024-01-13 01:41:57 +00:00
|
|
|
let res = ContentInfo(
|
2024-02-28 17:31:45 +00:00
|
|
|
content: foundContent.content.to0xHex(), utpTransfer: foundContent.utpTransfer
|
2023-04-28 07:04:21 +00:00
|
|
|
)
|
2024-01-13 01:41:57 +00:00
|
|
|
return JrpcConv.encode(res).JsonString
|
2022-01-14 15:07:14 +00:00
|
|
|
of Nodes:
|
2024-02-28 17:31:45 +00:00
|
|
|
let enrs = foundContent.nodes.map(
|
|
|
|
proc(n: Node): Record =
|
|
|
|
n.record
|
|
|
|
)
|
2024-01-13 01:41:57 +00:00
|
|
|
let jsonEnrs = JrpcConv.encode(enrs)
|
|
|
|
return ("{\"enrs\":" & jsonEnrs & "}").JsonString
|
2022-01-14 15:07:14 +00:00
|
|
|
|
2023-01-17 13:47:10 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "Offer") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
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)
|
2024-07-17 15:07:27 +00:00
|
|
|
contentKV = ContentKV(contentKey: ContentKeyByteList.init(key), content: content)
|
2023-08-24 16:19:29 +00:00
|
|
|
res = await p.offer(node, @[contentKV])
|
2022-12-16 16:47:52 +00:00
|
|
|
|
|
|
|
if res.isOk():
|
2023-04-27 12:07:57 +00:00
|
|
|
return SSZ.encode(res.get()).to0xHex()
|
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(
|
2024-02-28 17:31:45 +00:00
|
|
|
nodeId: NodeId
|
|
|
|
) -> seq[Record]:
|
2022-12-16 07:49:18 +00:00
|
|
|
let discovered = await p.lookup(nodeId)
|
2024-02-28 17:31:45 +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(
|
2024-02-28 17:31:45 +00:00
|
|
|
contentKey: string
|
|
|
|
) -> ContentInfo:
|
2022-12-16 07:49:18 +00:00
|
|
|
let
|
2024-07-17 15:07:27 +00:00
|
|
|
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
2022-12-16 07:49:18 +00:00
|
|
|
contentId = p.toContentId(key).valueOr:
|
2024-03-06 07:57:09 +00:00
|
|
|
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
|
2022-12-16 07:49:18 +00:00
|
|
|
|
|
|
|
contentResult = (await p.contentLookup(key, contentId)).valueOr:
|
2024-03-06 07:57:09 +00:00
|
|
|
raise (ref ApplicationError)(code: -39001, msg: "Content not found")
|
2022-12-16 07:49:18 +00:00
|
|
|
|
2023-08-24 16:19:29 +00:00
|
|
|
return ContentInfo(
|
2024-02-28 17:31:45 +00:00
|
|
|
content: contentResult.content.to0xHex(), utpTransfer: contentResult.utpTransfer
|
|
|
|
)
|
2022-12-16 07:49:18 +00:00
|
|
|
|
2023-10-30 14:48:06 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "TraceRecursiveFindContent") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
contentKey: string
|
|
|
|
) -> TraceContentLookupResult:
|
2023-10-30 14:48:06 +00:00
|
|
|
let
|
2024-07-17 15:07:27 +00:00
|
|
|
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
2023-10-30 14:48:06 +00:00
|
|
|
contentId = p.toContentId(key).valueOr:
|
2024-03-06 07:57:09 +00:00
|
|
|
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
|
|
|
|
|
|
|
|
res = await p.traceContentLookup(key, contentId)
|
2023-10-30 14:48:06 +00:00
|
|
|
|
2024-03-06 07:57:09 +00:00
|
|
|
# TODO: Might want to restructure the lookup result here. Potentially doing
|
|
|
|
# the json conversion in this module.
|
|
|
|
if res.content.isSome():
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
let data = Opt.some(JrpcConv.encode(res.trace).JsonString)
|
|
|
|
raise (ref ApplicationError)(code: -39001, msg: "Content not found", data: data)
|
2023-10-30 14:48:06 +00:00
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "Store") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
contentKey: string, contentValue: string
|
|
|
|
) -> bool:
|
2024-06-11 13:01:35 +00:00
|
|
|
let
|
2024-07-17 15:07:27 +00:00
|
|
|
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
2024-06-11 13:01:35 +00:00
|
|
|
contentValueBytes = hexToSeqByte(contentValue)
|
|
|
|
|
|
|
|
let valueToStore =
|
|
|
|
if network == "state":
|
|
|
|
let decodedKey = ContentKey.decode(key).valueOr:
|
|
|
|
raise invalidKeyErr
|
|
|
|
|
|
|
|
case decodedKey.contentType
|
|
|
|
of unused:
|
|
|
|
raise invalidKeyErr
|
|
|
|
of accountTrieNode:
|
|
|
|
let offerValue = AccountTrieNodeOffer.decode(contentValueBytes).valueOr:
|
|
|
|
raise invalidValueErr
|
|
|
|
offerValue.toRetrievalValue.encode()
|
|
|
|
of contractTrieNode:
|
|
|
|
let offerValue = ContractTrieNodeOffer.decode(contentValueBytes).valueOr:
|
|
|
|
raise invalidValueErr
|
|
|
|
offerValue.toRetrievalValue.encode()
|
|
|
|
of contractCode:
|
|
|
|
let offerValue = ContractCodeOffer.decode(contentValueBytes).valueOr:
|
|
|
|
raise invalidValueErr
|
|
|
|
offerValue.toRetrievalValue.encode()
|
|
|
|
else:
|
|
|
|
contentValueBytes
|
2022-12-13 18:22:36 +00:00
|
|
|
|
2024-06-11 13:01:35 +00:00
|
|
|
let contentId = p.toContentId(key)
|
2022-12-13 18:22:36 +00:00
|
|
|
if contentId.isSome():
|
2024-06-11 13:01:35 +00:00
|
|
|
p.storeContent(key, contentId.get(), valueToStore)
|
2022-12-13 18:22:36 +00:00
|
|
|
return true
|
|
|
|
else:
|
2024-06-11 13:01:35 +00:00
|
|
|
raise invalidKeyErr
|
2022-12-16 10:00:10 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
rpcServer.rpc("portal_" & network & "LocalContent") do(contentKey: string) -> string:
|
2022-12-16 10:00:10 +00:00
|
|
|
let
|
2024-07-17 15:07:27 +00:00
|
|
|
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
2022-12-16 10:00:10 +00:00
|
|
|
contentId = p.toContentId(key).valueOr:
|
2024-03-06 07:57:09 +00:00
|
|
|
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
|
2022-12-16 10:00:10 +00:00
|
|
|
|
2023-04-27 12:07:57 +00:00
|
|
|
contentResult = p.dbGet(key, contentId).valueOr:
|
2024-03-06 07:57:09 +00:00
|
|
|
raise (ref ApplicationError)(code: -39001, msg: "Content not found")
|
2023-04-27 12:07:57 +00:00
|
|
|
|
|
|
|
return contentResult.to0xHex()
|
2023-01-17 13:47:10 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("portal_" & network & "Gossip") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
contentKey: string, contentValue: string
|
|
|
|
) -> int:
|
2023-01-17 13:47:10 +00:00
|
|
|
let
|
|
|
|
key = hexToSeqByte(contentKey)
|
|
|
|
content = hexToSeqByte(contentValue)
|
2024-07-17 15:07:27 +00:00
|
|
|
contentKeys = ContentKeysList(@[ContentKeyByteList.init(key)])
|
2024-02-28 17:31:45 +00:00
|
|
|
numberOfPeers =
|
|
|
|
await p.neighborhoodGossip(Opt.none(NodeId), contentKeys, @[content])
|
2023-01-17 13:47:10 +00:00
|
|
|
|
|
|
|
return numberOfPeers
|
2023-10-05 17:29:39 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("portal_" & network & "RandomGossip") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
contentKey: string, contentValue: string
|
|
|
|
) -> int:
|
2023-10-05 17:29:39 +00:00
|
|
|
let
|
|
|
|
key = hexToSeqByte(contentKey)
|
|
|
|
content = hexToSeqByte(contentValue)
|
2024-07-17 15:07:27 +00:00
|
|
|
contentKeys = ContentKeysList(@[ContentKeyByteList.init(key)])
|
2023-10-05 17:29:39 +00:00
|
|
|
numberOfPeers = await p.randomGossip(Opt.none(NodeId), contentKeys, @[content])
|
|
|
|
|
|
|
|
return numberOfPeers
|