Skip to content

Commit

Permalink
Adjust portal FindContent JSON-RPC according to latest specs (#1566)
Browse files Browse the repository at this point in the history
Also remove the FindContentFull unspecced JSON-RPC as the new
FindContent specification replaces this functionality basically.
  • Loading branch information
kdeme authored Apr 28, 2023
1 parent acc2d53 commit 2ba974a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 56 deletions.
8 changes: 6 additions & 2 deletions fluffy/network/wire/portal_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ type
case kind*: FoundContentKind
of Content:
content*: seq[byte]
utpTransfer*: bool
of Nodes:
nodes*: seq[Node]

Expand Down Expand Up @@ -651,7 +652,8 @@ proc findContent*(p: PortalProtocol, dst: Node, contentKey: ByteList):
debug "Socket read fully",
socketKey = socket.socketKey
socket.destroy()
return ok(FoundContent(src: dst, kind: Content, content: content))
return ok(FoundContent(
src: dst, kind: Content, content: content, utpTransfer: true))
else :
debug "Socket read time-out",
socketKey = socket.socketKey
Expand All @@ -667,7 +669,9 @@ proc findContent*(p: PortalProtocol, dst: Node, contentKey: ByteList):
socket.close()
raise exc
of contentType:
return ok(FoundContent(src: dst, kind: Content, content: m.content.asSeq()))
return ok(FoundContent(
src: dst,
kind: Content, content: m.content.asSeq(), utpTransfer: false))
of enrsType:
let records = recordsFromBytes(m.enrs)
if records.isOk():
Expand Down
24 changes: 3 additions & 21 deletions fluffy/rpc/rpc_calls/rpc_portal_calls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ proc portal_stateLookupEnr(nodeId: NodeId): Record
proc portal_statePing(enr: Record): tuple[
enrSeq: uint64, customPayload: string]
proc portal_stateFindNodes(enr: Record): seq[Record]
proc portal_stateFindContent(enr: Record, contentKey: string): tuple[
connectionId: Option[string],
content: Option[string],
enrs: Option[seq[Record]]]
proc portal_stateFindContentFull(enr: Record, contentKey: string): tuple[
content: Option[string],
enrs: Option[seq[Record]]]
proc portal_stateFindContent(enr: Record, contentKey: string): JsonNode
proc portal_stateOffer(
enr: Record, contentKey: string, contentValue: string): string
proc portal_stateRecursiveFindNodes(nodeId: NodeId): seq[Record]
Expand All @@ -35,13 +29,7 @@ proc portal_historyLookupEnr(nodeId: NodeId): Record
proc portal_historyPing(enr: Record): tuple[
enrSeq: uint64, customPayload: string]
proc portal_historyFindNodes(enr: Record): seq[Record]
proc portal_historyFindContent(enr: Record, contentKey: string): tuple[
connectionId: Option[string],
content: Option[string],
enrs: Option[seq[Record]]]
proc portal_historyFindContentFull(enr: Record, contentKey: string): tuple[
content: Option[string],
enrs: Option[seq[Record]]]
proc portal_historyFindContent(enr: Record, contentKey: string): JsonNode
proc portal_historyOffer(
enr: Record, contentKey: string, contentValue: string): string
proc portal_historyRecursiveFindNodes(nodeId: NodeId): seq[Record]
Expand All @@ -61,13 +49,7 @@ proc portal_beaconLightClientLookupEnr(nodeId: NodeId): Record
proc portal_beaconLightClientPing(enr: Record): tuple[
enrSeq: uint64, customPayload: string]
proc portal_beaconLightClientFindNodes(enr: Record): seq[Record]
proc portal_beaconLightClientFindContent(enr: Record, contentKey: string): tuple[
connectionId: Option[string],
content: Option[string],
enrs: Option[seq[Record]]]
proc portal_beaconLightClientFindContentFull(enr: Record, contentKey: string): tuple[
content: Option[string],
enrs: Option[seq[Record]]]
proc portal_beaconLightClientFindContent(enr: Record, contentKey: string): JsonNode
proc portal_beaconLightClientOffer(
enr: Record, contentKey: string, contentValue: string): string
proc portal_beaconLightClientRecursiveFindNodes(nodeId: NodeId): seq[Record]
Expand Down
42 changes: 9 additions & 33 deletions fluffy/rpc/rpc_portal_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,10 @@ proc installPortalApiHandlers*(

rpcServer.rpc("portal_" & network & "FindContent") do(
enr: Record, contentKey: string) -> JsonNode:
let
node = toNodeWithAddress(enr)
res = await p.findContentImpl(
node, ByteList.init(hexToSeqByte(contentKey)))
type ContentInfo = object
content: string
utpTransfer: bool

if res.isErr():
raise newException(ValueError, $res.error)

var rpcRes = newJObject()
let contentMessage = res.get()
case contentMessage.contentMessageType:
of connectionIdType:
rpcRes["connectionId"] = %contentMessage.connectionId.to0xHex()
of contentType:
rpcRes["content"] = %contentMessage.content.asSeq().to0xHex()
of enrsType:
let records = recordsFromBytes(contentMessage.enrs)
if records.isErr():
raise newException(ValueError, $records.error)
else:
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) -> JsonNode:
# Note: unspecified RPC, but useful as we can get content from uTP also
let
node = toNodeWithAddress(enr)
foundContentResult = await p.findContent(
Expand All @@ -149,15 +123,17 @@ proc installPortalApiHandlers*(
if foundContentResult.isErr():
raise newException(ValueError, $foundContentResult.error)
else:
var rpcRes = newJObject()
let foundContent = foundContentResult.get()
case foundContent.kind:
of Content:
rpcRes["content"] = %foundContent.content.to0xHex()
return %ContentInfo(
content: foundContent.content.to0xHex(),
utpTransfer: foundContent.utpTransfer
)
of Nodes:
var rpcRes = newJObject()
rpcRes["enrs"] = %foundContent.nodes.map(proc(n: Node): Record = n.record)

return rpcRes
return rpcRes

rpcServer.rpc("portal_" & network & "Offer") do(
enr: Record, contentKey: string, contentValue: string) -> string:
Expand Down

0 comments on commit 2ba974a

Please sign in to comment.