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:
parent
e1369a7c25
commit
acc2d53b52
|
@ -87,11 +87,7 @@ proc installDiscoveryApiHandlers*(rpcServer: RpcServer|RpcProxy,
|
||||||
else:
|
else:
|
||||||
raise newException(ValueError, "Record not in local routing table.")
|
raise newException(ValueError, "Record not in local routing table.")
|
||||||
|
|
||||||
rpcServer.rpc("discv5_lookupEnr") do(
|
rpcServer.rpc("discv5_lookupEnr") do(nodeId: NodeId) -> Record:
|
||||||
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?
|
|
||||||
let lookup = await d.resolve(nodeId)
|
let lookup = await d.resolve(nodeId)
|
||||||
if lookup.isSome():
|
if lookup.isSome():
|
||||||
return lookup.get().record
|
return lookup.get().record
|
||||||
|
|
|
@ -71,9 +71,6 @@ proc installPortalApiHandlers*(
|
||||||
raise newException(ValueError, "Record not in local routing table.")
|
raise newException(ValueError, "Record not in local routing table.")
|
||||||
|
|
||||||
rpcServer.rpc("portal_" & network & "LookupEnr") do(nodeId: NodeId) -> Record:
|
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)
|
let lookup = await p.resolve(nodeId)
|
||||||
if lookup.isSome():
|
if lookup.isSome():
|
||||||
return lookup.get().record
|
return lookup.get().record
|
||||||
|
@ -82,8 +79,6 @@ proc installPortalApiHandlers*(
|
||||||
|
|
||||||
rpcServer.rpc("portal_" & network & "Ping") do(
|
rpcServer.rpc("portal_" & network & "Ping") do(
|
||||||
enr: Record) -> tuple[enrSeq: uint64, dataRadius: UInt256]:
|
enr: Record) -> tuple[enrSeq: uint64, dataRadius: UInt256]:
|
||||||
# TODO: Not fully according to spec:
|
|
||||||
# - Missing optional dataRadius parameter
|
|
||||||
let
|
let
|
||||||
node = toNodeWithAddress(enr)
|
node = toNodeWithAddress(enr)
|
||||||
pong = await p.ping(node)
|
pong = await p.ping(node)
|
||||||
|
@ -114,13 +109,8 @@ proc installPortalApiHandlers*(
|
||||||
else:
|
else:
|
||||||
return nodes.get().map(proc(n: Node): Record = n.record)
|
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(
|
rpcServer.rpc("portal_" & network & "FindContent") do(
|
||||||
enr: Record, contentKey: string) -> tuple[
|
enr: Record, contentKey: string) -> JsonNode:
|
||||||
connectionId: Option[string],
|
|
||||||
content: Option[string],
|
|
||||||
enrs: Option[seq[Record]]]:
|
|
||||||
let
|
let
|
||||||
node = toNodeWithAddress(enr)
|
node = toNodeWithAddress(enr)
|
||||||
res = await p.findContentImpl(
|
res = await p.findContentImpl(
|
||||||
|
@ -129,34 +119,27 @@ proc installPortalApiHandlers*(
|
||||||
if res.isErr():
|
if res.isErr():
|
||||||
raise newException(ValueError, $res.error)
|
raise newException(ValueError, $res.error)
|
||||||
|
|
||||||
|
var rpcRes = newJObject()
|
||||||
let contentMessage = res.get()
|
let contentMessage = res.get()
|
||||||
case contentMessage.contentMessageType:
|
case contentMessage.contentMessageType:
|
||||||
of connectionIdType:
|
of connectionIdType:
|
||||||
return (
|
rpcRes["connectionId"] = %contentMessage.connectionId.to0xHex()
|
||||||
some("0x" & contentMessage.connectionId.toHex()),
|
|
||||||
none(string),
|
|
||||||
none(seq[Record]))
|
|
||||||
of contentType:
|
of contentType:
|
||||||
return (
|
rpcRes["content"] = %contentMessage.content.asSeq().to0xHex()
|
||||||
none(string),
|
|
||||||
some("0x" & contentMessage.content.asSeq().toHex()),
|
|
||||||
none(seq[Record]))
|
|
||||||
of enrsType:
|
of enrsType:
|
||||||
let records = recordsFromBytes(contentMessage.enrs)
|
let records = recordsFromBytes(contentMessage.enrs)
|
||||||
if records.isErr():
|
if records.isErr():
|
||||||
raise newException(ValueError, $records.error)
|
raise newException(ValueError, $records.error)
|
||||||
else:
|
else:
|
||||||
return (
|
let nodes = verifyNodesRecords(
|
||||||
none(string),
|
|
||||||
none(string),
|
|
||||||
# Note: Could also pass not verified nodes
|
|
||||||
some(verifyNodesRecords(
|
|
||||||
records.get(), node, enrsResultLimit).map(
|
records.get(), node, enrsResultLimit).map(
|
||||||
proc(n: Node): Record = n.record)))
|
proc(n: Node): Record = n.record)
|
||||||
|
rpcRes["enrs"] = %nodes
|
||||||
|
|
||||||
|
return rpcRes
|
||||||
|
|
||||||
rpcServer.rpc("portal_" & network & "FindContentFull") do(
|
rpcServer.rpc("portal_" & network & "FindContentFull") do(
|
||||||
enr: Record, contentKey: string) -> tuple[
|
enr: Record, contentKey: string) -> JsonNode:
|
||||||
content: Option[string], enrs: Option[seq[Record]]]:
|
|
||||||
# Note: unspecified RPC, but useful as we can get content from uTP also
|
# Note: unspecified RPC, but useful as we can get content from uTP also
|
||||||
let
|
let
|
||||||
node = toNodeWithAddress(enr)
|
node = toNodeWithAddress(enr)
|
||||||
|
@ -166,16 +149,15 @@ proc installPortalApiHandlers*(
|
||||||
if foundContentResult.isErr():
|
if foundContentResult.isErr():
|
||||||
raise newException(ValueError, $foundContentResult.error)
|
raise newException(ValueError, $foundContentResult.error)
|
||||||
else:
|
else:
|
||||||
|
var rpcRes = newJObject()
|
||||||
let foundContent = foundContentResult.get()
|
let foundContent = foundContentResult.get()
|
||||||
case foundContent.kind:
|
case foundContent.kind:
|
||||||
of Content:
|
of Content:
|
||||||
return (
|
rpcRes["content"] = %foundContent.content.to0xHex()
|
||||||
some("0x" & foundContent.content.toHex()),
|
|
||||||
none(seq[Record]))
|
|
||||||
of Nodes:
|
of Nodes:
|
||||||
return (
|
rpcRes["enrs"] = %foundContent.nodes.map(proc(n: Node): Record = n.record)
|
||||||
none(string),
|
|
||||||
some(foundContent.nodes.map(proc(n: Node): Record = n.record)))
|
return rpcRes
|
||||||
|
|
||||||
rpcServer.rpc("portal_" & network & "Offer") do(
|
rpcServer.rpc("portal_" & network & "Offer") do(
|
||||||
enr: Record, contentKey: string, contentValue: string) -> string:
|
enr: Record, contentKey: string, contentValue: string) -> string:
|
||||||
|
@ -187,7 +169,7 @@ proc installPortalApiHandlers*(
|
||||||
res = await p.offer(node, @[contentInfo])
|
res = await p.offer(node, @[contentInfo])
|
||||||
|
|
||||||
if res.isOk():
|
if res.isOk():
|
||||||
return "0x" & SSZ.encode(res.get()).toHex()
|
return SSZ.encode(res.get()).to0xHex()
|
||||||
else:
|
else:
|
||||||
raise newException(ValueError, $res.error)
|
raise newException(ValueError, $res.error)
|
||||||
|
|
||||||
|
@ -206,7 +188,7 @@ proc installPortalApiHandlers*(
|
||||||
contentResult = (await p.contentLookup(key, contentId)).valueOr:
|
contentResult = (await p.contentLookup(key, contentId)).valueOr:
|
||||||
return "0x"
|
return "0x"
|
||||||
|
|
||||||
return "0x" & contentResult.content.toHex()
|
return contentResult.content.to0xHex()
|
||||||
|
|
||||||
rpcServer.rpc("portal_" & network & "Store") do(
|
rpcServer.rpc("portal_" & network & "Store") do(
|
||||||
contentKey: string, contentValue: string) -> bool:
|
contentKey: string, contentValue: string) -> bool:
|
||||||
|
@ -226,12 +208,11 @@ proc installPortalApiHandlers*(
|
||||||
contentId = p.toContentId(key).valueOr:
|
contentId = p.toContentId(key).valueOr:
|
||||||
raise newException(ValueError, "Invalid content key")
|
raise newException(ValueError, "Invalid content key")
|
||||||
|
|
||||||
let contentResult = p.dbGet(key, contentId)
|
contentResult = p.dbGet(key, contentId).valueOr:
|
||||||
if contentResult.isOk():
|
|
||||||
return "0x" & contentResult.get().toHex()
|
|
||||||
else:
|
|
||||||
return "0x"
|
return "0x"
|
||||||
|
|
||||||
|
return contentResult.to0xHex()
|
||||||
|
|
||||||
rpcServer.rpc("portal_" & network & "Gossip") do(
|
rpcServer.rpc("portal_" & network & "Gossip") do(
|
||||||
contentKey: string, contentValue: string) -> int:
|
contentKey: string, contentValue: string) -> int:
|
||||||
let
|
let
|
||||||
|
|
Loading…
Reference in New Issue