always return tracing result even if no content was found (#1908)

This commit is contained in:
Daniel Sobol 2023-11-27 12:21:19 +03:00 committed by GitHub
parent 5462c05dc6
commit 47e03919fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 23 deletions

View File

@ -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.} =

View File

@ -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:

View File

@ -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)