diff --git a/fluffy/network/wire/portal_protocol.nim b/fluffy/network/wire/portal_protocol.nim index be019aca2..2bd61d412 100644 --- a/fluffy/network/wire/portal_protocol.nim +++ b/fluffy/network/wire/portal_protocol.nim @@ -216,14 +216,14 @@ type TraceObject* = object origin*: NodeId targetId: UInt256 - receivedFrom*: NodeId + receivedFrom*: Opt[NodeId] responses*: Table[string, TraceResponse] metadata*: Table[string, NodeMetadata] cancelled*: seq[NodeId] startedAtMs*: int64 TraceContentLookupResult* = object - content*: seq[byte] + content*: Opt[seq[byte]] utpTransfer*: bool trace*: TraceObject @@ -1106,7 +1106,7 @@ proc contentLookup*(p: PortalProtocol, target: ByteList, targetId: UInt256): return Opt.none(ContentLookupResult) proc traceContentLookup*(p: PortalProtocol, target: ByteList, targetId: UInt256): - Future[Opt[TraceContentLookupResult]] {.async.} = + Future[TraceContentLookupResult] {.async.} = ## Perform a lookup for the given target, return the closest n nodes to the ## target. Maximum value for n is `BUCKET_SIZE`. # `closestNodes` holds the k closest nodes to target found, sorted by distance @@ -1261,26 +1261,38 @@ proc traceContentLookup*(p: PortalProtocol, target: ByteList, targetId: UInt256) distance: p.distance(pn.id, targetId) ) - return Opt.some(TraceContentLookupResult( - content: content.content, + return TraceContentLookupResult( + content: Opt.some(content.content), utpTransfer: content.utpTransfer, trace: TraceObject( origin: p.localNode.id, targetId: targetId, - receivedFrom: content.src.id, + receivedFrom: Opt.some(content.src.id), responses: responses, metadata: metadata, cancelled: pendingNodeIds, startedAtMs: chronos.epochNanoSeconds(ts) div 1_000_000 # nanoseconds to milliseconds ) - )) + ) else: # TODO: Should we do something with the node that failed responding our # query? discard portal_lookup_content_failures.inc() - return Opt.none(TraceContentLookupResult) + return TraceContentLookupResult( + content: Opt.none(seq[byte]), + utpTransfer: false, + trace: TraceObject( + origin: p.localNode.id, + targetId: targetId, + receivedFrom: Opt.none(NodeId), + responses: responses, + metadata: metadata, + cancelled: newSeq[NodeId](), + startedAtMs: chronos.epochNanoSeconds(ts) div 1_000_000 # nanoseconds to milliseconds + ) + ) proc query*(p: PortalProtocol, target: NodeId, k = BUCKET_SIZE): Future[seq[Node]] {.async.} = diff --git a/fluffy/rpc/rpc_portal_api.nim b/fluffy/rpc/rpc_portal_api.nim index c41374ae9..4c06ddff2 100644 --- a/fluffy/rpc/rpc_portal_api.nim +++ b/fluffy/rpc/rpc_portal_api.nim @@ -24,11 +24,6 @@ type content: string utpTransfer: bool - TraceContentInfo* = object - content*: string - utpTransfer: bool - trace*: TraceObject - # Note: # Using a string for the network parameter will give an error in the rpc macro: @@ -180,21 +175,14 @@ proc installPortalApiHandlers*( ) rpcServer.rpc("portal_" & network & "TraceRecursiveFindContent") do( - contentKey: string) -> TraceContentInfo: + contentKey: string) -> TraceContentLookupResult: let key = ByteList.init(hexToSeqByte(contentKey)) contentId = p.toContentId(key).valueOr: raise newException(ValueError, "Invalid content key") - contentResult = (await p.traceContentLookup(key, contentId)).valueOr: - return TraceContentInfo(content: "0x") - - return TraceContentInfo( - content: contentResult.content.to0xHex(), - utpTransfer: contentResult.utpTransfer, - trace: contentResult.trace - ) + await p.traceContentLookup(key, contentId) rpcServer.rpc("portal_" & network & "Store") do( contentKey: string, contentValue: string) -> bool: diff --git a/fluffy/rpc/rpc_types.nim b/fluffy/rpc/rpc_types.nim index 4d6f7d076..e0b7cc4ce 100644 --- a/fluffy/rpc/rpc_types.nim +++ b/fluffy/rpc/rpc_types.nim @@ -9,7 +9,7 @@ import json_rpc/jsonmarshal, - stew/results, + stew/[results, byteutils], eth/p2p/discoveryv5/[routing_table, enr, node] export jsonmarshal, routing_table, enr, node @@ -62,6 +62,18 @@ func fromJson*(n: JsonNode, argName: string, result: var Record) func `%`*(value: NodeId): JsonNode = %("0x" & value.toHex()) +func `%`*(value: Opt[NodeId]): JsonNode = + if value.isSome(): + %("0x" & value.get().toHex()) + else: + %("0x") + +func `%`*(value: Opt[seq[byte]]): JsonNode = + if value.isSome(): + %(value.get().to0xHex()) + else: + %("0x") + func fromJson*(n: JsonNode, argName: string, result: var NodeId) {.raises: [ValueError].} = n.kind.expect(JString, argName)