Further clean-up of Portal and discv5 JSON-RPC (#1565)

- Removal of TODOs fixed in specification instead
- Fix some other TODOs
- General clean-up such as to0xHex usage
This commit is contained in:
Kim De Mey 2023-04-27 14:07:57 +02:00 committed by GitHub
parent e1369a7c25
commit acc2d53b52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 46 deletions

View File

@ -87,11 +87,7 @@ 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 according to spec, missing optional enrSeq
# Can add `enrSeq: Option[uint64]` as parameter but Option appears to be
# not implemented as an option parameter in nim-json-rpc?
rpcServer.rpc("discv5_lookupEnr") do(nodeId: NodeId) -> Record:
let lookup = await d.resolve(nodeId)
if lookup.isSome():
return lookup.get().record

View File

@ -71,9 +71,6 @@ proc installPortalApiHandlers*(
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
# not implemented as an optional parameter in nim-json-rpc?
let lookup = await p.resolve(nodeId)
if lookup.isSome():
return lookup.get().record
@ -82,8 +79,6 @@ proc installPortalApiHandlers*(
rpcServer.rpc("portal_" & network & "Ping") do(
enr: Record) -> tuple[enrSeq: uint64, dataRadius: UInt256]:
# TODO: Not fully according to spec:
# - Missing optional dataRadius parameter
let
node = toNodeWithAddress(enr)
pong = await p.ping(node)
@ -114,13 +109,8 @@ proc installPortalApiHandlers*(
else:
return nodes.get().map(proc(n: Node): Record = n.record)
# TODO: This returns null values for the `none`s. Not sure what it should be
# according to spec, no k:v pair at all?
rpcServer.rpc("portal_" & network & "FindContent") do(
enr: Record, contentKey: string) -> tuple[
connectionId: Option[string],
content: Option[string],
enrs: Option[seq[Record]]]:
enr: Record, contentKey: string) -> JsonNode:
let
node = toNodeWithAddress(enr)
res = await p.findContentImpl(
@ -129,34 +119,27 @@ proc installPortalApiHandlers*(
if res.isErr():
raise newException(ValueError, $res.error)
var rpcRes = newJObject()
let contentMessage = res.get()
case contentMessage.contentMessageType:
of connectionIdType:
return (
some("0x" & contentMessage.connectionId.toHex()),
none(string),
none(seq[Record]))
rpcRes["connectionId"] = %contentMessage.connectionId.to0xHex()
of contentType:
return (
none(string),
some("0x" & contentMessage.content.asSeq().toHex()),
none(seq[Record]))
rpcRes["content"] = %contentMessage.content.asSeq().to0xHex()
of enrsType:
let records = recordsFromBytes(contentMessage.enrs)
if records.isErr():
raise newException(ValueError, $records.error)
else:
return (
none(string),
none(string),
# Note: Could also pass not verified nodes
some(verifyNodesRecords(
records.get(), node, enrsResultLimit).map(
proc(n: Node): Record = n.record)))
let nodes = verifyNodesRecords(
records.get(), node, enrsResultLimit).map(
proc(n: Node): Record = n.record)
rpcRes["enrs"] = %nodes
return rpcRes
rpcServer.rpc("portal_" & network & "FindContentFull") do(
enr: Record, contentKey: string) -> tuple[
content: Option[string], enrs: Option[seq[Record]]]:
enr: Record, contentKey: string) -> JsonNode:
# Note: unspecified RPC, but useful as we can get content from uTP also
let
node = toNodeWithAddress(enr)
@ -166,16 +149,15 @@ proc installPortalApiHandlers*(
if foundContentResult.isErr():
raise newException(ValueError, $foundContentResult.error)
else:
var rpcRes = newJObject()
let foundContent = foundContentResult.get()
case foundContent.kind:
of Content:
return (
some("0x" & foundContent.content.toHex()),
none(seq[Record]))
rpcRes["content"] = %foundContent.content.to0xHex()
of Nodes:
return (
none(string),
some(foundContent.nodes.map(proc(n: Node): Record = n.record)))
rpcRes["enrs"] = %foundContent.nodes.map(proc(n: Node): Record = n.record)
return rpcRes
rpcServer.rpc("portal_" & network & "Offer") do(
enr: Record, contentKey: string, contentValue: string) -> string:
@ -187,7 +169,7 @@ proc installPortalApiHandlers*(
res = await p.offer(node, @[contentInfo])
if res.isOk():
return "0x" & SSZ.encode(res.get()).toHex()
return SSZ.encode(res.get()).to0xHex()
else:
raise newException(ValueError, $res.error)
@ -206,7 +188,7 @@ proc installPortalApiHandlers*(
contentResult = (await p.contentLookup(key, contentId)).valueOr:
return "0x"
return "0x" & contentResult.content.toHex()
return contentResult.content.to0xHex()
rpcServer.rpc("portal_" & network & "Store") do(
contentKey: string, contentValue: string) -> bool:
@ -226,11 +208,10 @@ proc installPortalApiHandlers*(
contentId = p.toContentId(key).valueOr:
raise newException(ValueError, "Invalid content key")
let contentResult = p.dbGet(key, contentId)
if contentResult.isOk():
return "0x" & contentResult.get().toHex()
else:
return "0x"
contentResult = p.dbGet(key, contentId).valueOr:
return "0x"
return contentResult.to0xHex()
rpcServer.rpc("portal_" & network & "Gossip") do(
contentKey: string, contentValue: string) -> int: