Fix Portal discv5 JSON-RPC API where it deviates from spec (#1378)

This commit is contained in:
Kim De Mey 2022-12-15 16:24:23 +01:00 committed by GitHub
parent 706effea2a
commit 095854107c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 21 deletions

View File

@ -1,8 +1,9 @@
# Discovery v5 json-rpc calls
proc discv5_routingTableInfo(): RoutingTableInfo
proc discv5_nodeInfo(): NodeInfo
proc discv5_updateNodeInfo(kvPairs: seq[(string, string)]): RoutingTableInfo
proc discv5_routingTableInfo(): RoutingTableInfo
proc discv5_addEnr(enr: Record): bool
proc discv5_addEnrs(enrs: seq[Record]): bool
proc discv5_getEnr(nodeId: NodeId): Record
proc discv5_deleteEnr(nodeId: NodeId): bool
@ -12,4 +13,4 @@ proc discv5_ping(nodeId: Record): PongResponse
proc discv5_findNode(nodeId: Record, distances: seq[uint16]): seq[Record]
proc discv5_talkReq(nodeId: Record, protocol, payload: string): string
proc discv5_recursiveFindNodes(): seq[Record]
proc discv5_recursiveFindNode(nodeId: NodeId): seq[Record]

View File

@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2021 Status Research & Development GmbH
# Copyright (c) 2021-2022 Status Research & Development GmbH
# 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).
@ -24,20 +24,16 @@ type
proc installDiscoveryApiHandlers*(rpcServer: RpcServer|RpcProxy,
d: discv5_protocol.Protocol) {.raises: [Defect, CatchableError].} =
## Discovery v5 JSON-RPC API such as defined here:
## https://ddht.readthedocs.io/en/latest/jsonrpc.html
## and here:
## https://github.com/ethereum/portal-network-specs/pull/88
## Note: There are quite some discrepancies between the two, can only
## implement exactly once specification has settled.
rpcServer.rpc("discv5_routingTableInfo") do() -> RoutingTableInfo:
return getRoutingTableInfo(d.routingTable)
## https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
rpcServer.rpc("discv5_nodeInfo") do() -> NodeInfo:
return d.routingTable.getNodeInfo()
rpcServer.rpc("discv5_updateNodeInfo") do(
kvPairs: seq[(string, string)]) -> NodeInfo:
# TODO: Not according to spec, as spec parameters are weird.
# It is currently as in
# https://ddht.readthedocs.io/en/latest/jsonrpc.html#discv5-updatenodeinfo
let enrFields = kvPairs.map(
proc(n: (string, string)): (string, seq[byte]) =
(n[0], hexToSeqByte(n[1]))
@ -48,7 +44,21 @@ proc installDiscoveryApiHandlers*(rpcServer: RpcServer|RpcProxy,
return d.routingTable.getNodeInfo()
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")
rpcServer.rpc("discv5_addEnrs") do(enrs: seq[Record]) -> bool:
# Note: unspecified RPC, but useful for our local testnet test
# 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.
@ -77,14 +87,42 @@ proc installDiscoveryApiHandlers*(rpcServer: RpcServer|RpcProxy,
else:
raise newException(ValueError, "Record not in local routing table.")
rpcServer.rpc("discv5_lookupEnr") do(nodeId: NodeId) -> Record:
# TODO: Not using seqNum, what is the use case of this?
rpcServer.rpc("discv5_lookupEnr") do(
nodeId: NodeId) -> Record:
# TODO: Not according to spec, missing optional seqNum
# Can add `seqNum: Option[uint64]` as parameter but Option appears to be
# not implemented as an option parameter in nim-json-rpc?
let lookup = await d.resolve(nodeId)
if lookup.isSome():
return lookup.get().record
else:
raise newException(ValueError, "Record not found in DHT lookup.")
# Note:
# These are difficult to support in our current discv5 implementation and it
# should be proven first that they are deemed useful before implementing them.
rpcServer.rpc("discv5_sendPing") do(enr: Record) -> string:
raise newException(ValueError, "This JSON-RPC is not supported")
rpcServer.rpc("discv5_sendPong") do(enr: Record, requestId: string) -> bool:
raise newException(ValueError, "This JSON-RPC is not supported")
rpcServer.rpc("discv5_sendFindNode") do(
enr: Record, distances: seq[uint16]) -> string:
raise newException(ValueError, "This JSON-RPC is not supported")
rpcServer.rpc("discv5_sendNodes") do(
enr: Record, nodes: seq[Record], requestId: string) -> uint8:
raise newException(ValueError, "This JSON-RPC is not supported")
rpcServer.rpc("discv5_sendTalkRequest") do(
enr: Record, protocolId: string, payload: string) -> string:
raise newException(ValueError, "This JSON-RPC is not supported")
rpcServer.rpc("discv5_sendTalkResponse") do(
enr: Record, payload: string, requestId: string) -> bool:
raise newException(ValueError, "This JSON-RPC is not supported")
rpcServer.rpc("discv5_ping") do(enr: Record) -> PongResponse:
let
node = toNodeWithAddress(enr)
@ -110,7 +148,8 @@ proc installDiscoveryApiHandlers*(rpcServer: RpcServer|RpcProxy,
else:
return nodes.get().map(proc(n: Node): Record = n.record)
rpcServer.rpc("discv5_talkReq") do(enr: Record, protocol, payload: string) -> string:
rpcServer.rpc("discv5_talkReq") do(
enr: Record, protocol, payload: string) -> string:
let
node = toNodeWithAddress(enr)
talkresp = await d.talkReq(
@ -120,9 +159,6 @@ proc installDiscoveryApiHandlers*(rpcServer: RpcServer|RpcProxy,
else:
return talkresp.get().toHex()
rpcServer.rpc("discv5_recursiveFindNodes") do() -> seq[Record]:
# TODO: Not according to the specification currently as the node_id is a
# parameter to be passed, but in that case it would be very similar to
# discv5_lookupEnr.
let discovered = await d.lookup(NodeId.random(d.rng[]))
rpcServer.rpc("discv5_recursiveFindNode") do(nodeId: NodeId) -> seq[Record]:
let discovered = await d.lookup(nodeId)
return discovered.map(proc(n: Node): Record = n.record)

View File

@ -20,7 +20,7 @@ type
nodeId*: NodeId
RoutingTableInfo* = object
localKey*: NodeId
localNodeId*: NodeId
buckets*: seq[seq[NodeId]]
func getNodeInfo*(r: RoutingTable): NodeInfo =
@ -35,7 +35,7 @@ func getRoutingTableInfo*(r: RoutingTable): RoutingTableInfo =
info.buckets.add(bucket)
info.localKey = r.localNode.id
info.localNodeId = r.localNode.id
info