Remove the use of ByteList in PortalStream and for FoundContent (#937)

This commit is contained in:
Kim De Mey 2022-01-20 21:21:20 +01:00 committed by GitHub
parent 3909675f29
commit d2f56463ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 14 deletions

View File

@ -41,11 +41,11 @@ proc getContent*(n: HistoryNetwork, key: ContentKey):
# When content is found and is in the radius range, store it.
if content.isSome() and contentInRange:
n.contentDB.put(contentId, content.get().asSeq())
n.contentDB.put(contentId, content.get())
# TODO: for now returning bytes, ultimately it would be nice to return proper
# domain types.
return content.map(x => x.asSeq())
return content
proc new*(
T: type HistoryNetwork,

View File

@ -41,11 +41,11 @@ proc getContent*(n: StateNetwork, key: ContentKey):
# When content is found on the network and is in the radius range, store it.
if content.isSome() and contentInRange:
n.contentDB.put(contentId, content.get().asSeq())
n.contentDB.put(contentId, content.get())
# TODO: for now returning bytes, ultimately it would be nice to return proper
# domain types.
return content.map(x => x.asSeq())
return content
proc new*(
T: type StateNetwork,

View File

@ -66,7 +66,7 @@ type
FoundContent* = object
case kind*: FoundContentKind
of Content:
content*: ByteList
content*: seq[byte]
of Nodes:
nodes*: seq[Node]
@ -149,7 +149,7 @@ proc handleFindContent(
encodeMessage(ContentMessage(
contentMessageType: contentType, content: ByteList(content)))
else:
let connectionId = p.stream.addContentRequest(srcId, ByteList(content))
let connectionId = p.stream.addContentRequest(srcId, content)
encodeMessage(ContentMessage(
contentMessageType: connectionIdType, connectionId: connectionId))
@ -392,12 +392,12 @@ proc findContent*(p: PortalProtocol, dst: Node, contentKey: ByteList):
if await readData.withTimeout(p.stream.readTimeout):
let content = readData.read
await socket.destroyWait()
return ok(FoundContent(kind: Content, content: ByteList.init(content)))
return ok(FoundContent(kind: Content, content: content))
else:
socket.close()
return err("Reading data from socket timed out, content request failed")
of contentType:
return ok(FoundContent(kind: Content, content: m.content))
return ok(FoundContent(kind: Content, content: m.content.asSeq()))
of enrsType:
let records = recordsFromBytes(m.enrs)
if records.isOk():
@ -544,7 +544,7 @@ proc lookup*(p: PortalProtocol, target: NodeId): Future[seq[Node]] {.async.} =
# networks will probably be very similar. Extract lookup function to separate module
# and make it more generaic
proc contentLookup*(p: PortalProtocol, target: ByteList, targetId: UInt256):
Future[Option[ByteList]] {.async.} =
Future[Option[seq[byte]]] {.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
@ -614,7 +614,7 @@ proc contentLookup*(p: PortalProtocol, target: ByteList, targetId: UInt256):
# query?
discard
return none[ByteList]()
return none[seq[byte]]()
proc query*(p: PortalProtocol, target: NodeId, k = BUCKET_SIZE): Future[seq[Node]]
{.async.} =

View File

@ -27,7 +27,7 @@ type
ContentRequest = object
connectionId: uint16
nodeId: NodeId
content: ByteList
content: seq[byte]
timeout: Moment
ContentOffer = object
@ -74,7 +74,7 @@ proc addContentOffer*(
return connectionId
proc addContentRequest*(
stream: PortalStream, nodeId: NodeId, content: ByteList): Bytes2 =
stream: PortalStream, nodeId: NodeId, content: seq[byte]): Bytes2 =
var connectionId: Bytes2
brHmacDrbgGenerate(stream.rng[], connectionId)
@ -141,7 +141,7 @@ proc registerIncomingSocketCallback(
for i, request in stream.contentRequests:
if request.connectionId == client.connectionId and
request.nodeId == client.remoteAddress.id:
let fut = client.writeAndClose(request.content.asSeq())
let fut = client.writeAndClose(request.content)
stream.contentRequests.del(i)
return fut

View File

@ -122,7 +122,7 @@ proc installPortalApiHandlers*(
case foundContent.kind:
of Content:
return (
some("0x" & foundContent.content.asSeq().toHex()),
some("0x" & foundContent.content.toHex()),
none(seq[Record]))
of Nodes:
return (