Adapt portal_*Offer method to the latest specification (#2689)
This commit is contained in:
parent
1ac6ee1aef
commit
778c1d589f
|
@ -984,6 +984,8 @@ proc offer*(
|
||||||
): Future[PortalResult[ContentKeysBitList]] {.async: (raises: [CancelledError]).} =
|
): Future[PortalResult[ContentKeysBitList]] {.async: (raises: [CancelledError]).} =
|
||||||
if len(content) > contentKeysLimit:
|
if len(content) > contentKeysLimit:
|
||||||
return err("Cannot offer more than 64 content items")
|
return err("Cannot offer more than 64 content items")
|
||||||
|
if len(content) == 0:
|
||||||
|
return err("Cannot offer empty content list")
|
||||||
|
|
||||||
let contentList = List[ContentKV, contentKeysLimit].init(content)
|
let contentList = List[ContentKV, contentKeysLimit].init(content)
|
||||||
let req = OfferRequest(dst: dst, kind: Direct, contentList: contentList)
|
let req = OfferRequest(dst: dst, kind: Direct, contentList: contentList)
|
||||||
|
|
|
@ -161,19 +161,24 @@ proc installPortalApiHandlers*(
|
||||||
return ("{\"enrs\":" & jsonEnrs & "}").JsonString
|
return ("{\"enrs\":" & jsonEnrs & "}").JsonString
|
||||||
|
|
||||||
rpcServer.rpc("portal_" & network & "Offer") do(
|
rpcServer.rpc("portal_" & network & "Offer") do(
|
||||||
enr: Record, contentKey: string, contentValue: string
|
enr: Record, contentItems: seq[ContentItem]
|
||||||
) -> string:
|
) -> string:
|
||||||
let
|
let node = toNodeWithAddress(enr)
|
||||||
node = toNodeWithAddress(enr)
|
|
||||||
key = hexToSeqByte(contentKey)
|
|
||||||
content = hexToSeqByte(contentValue)
|
|
||||||
contentKV = ContentKV(contentKey: ContentKeyByteList.init(key), content: content)
|
|
||||||
res = await p.offer(node, @[contentKV])
|
|
||||||
|
|
||||||
if res.isOk():
|
var contentItemsToOffer: seq[ContentKV]
|
||||||
return SSZ.encode(res.get()).to0xHex()
|
for contentItem in contentItems:
|
||||||
else:
|
let
|
||||||
raise newException(ValueError, $res.error)
|
contentKey = hexToSeqByte(contentItem[0])
|
||||||
|
contentValue = hexToSeqByte(contentItem[1])
|
||||||
|
contentKV = ContentKV(
|
||||||
|
contentKey: ContentKeyByteList.init(contentKey), content: contentValue
|
||||||
|
)
|
||||||
|
contentItemsToOffer.add(contentKV)
|
||||||
|
|
||||||
|
let offerResult = (await p.offer(node, contentItemsToOffer)).valueOr:
|
||||||
|
raise newException(ValueError, $error)
|
||||||
|
|
||||||
|
SSZ.encode(offerResult).to0xHex()
|
||||||
|
|
||||||
rpcServer.rpc("portal_" & network & "RecursiveFindNodes") do(
|
rpcServer.rpc("portal_" & network & "RecursiveFindNodes") do(
|
||||||
nodeId: NodeId
|
nodeId: NodeId
|
||||||
|
|
|
@ -31,6 +31,8 @@ type
|
||||||
content*: string
|
content*: string
|
||||||
utpTransfer*: bool
|
utpTransfer*: bool
|
||||||
|
|
||||||
|
ContentItem* = array[2, string]
|
||||||
|
|
||||||
NodeInfo.useDefaultSerializationIn JrpcConv
|
NodeInfo.useDefaultSerializationIn JrpcConv
|
||||||
RoutingTableInfo.useDefaultSerializationIn JrpcConv
|
RoutingTableInfo.useDefaultSerializationIn JrpcConv
|
||||||
(string, string).useDefaultSerializationIn JrpcConv
|
(string, string).useDefaultSerializationIn JrpcConv
|
||||||
|
@ -125,3 +127,22 @@ proc readValue*(
|
||||||
discard
|
discard
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
r.raiseUnexpectedValue("PingResult parser error: " & exc.msg)
|
r.raiseUnexpectedValue("PingResult parser error: " & exc.msg)
|
||||||
|
|
||||||
|
# Note:
|
||||||
|
# This is a similar readValue as the default one in nim-json-serialization but
|
||||||
|
# with an added exact length check. The default one will successfully parse when
|
||||||
|
# a JSON array with less than size n items is provided. And default objects
|
||||||
|
# (in this case empty string) will be applied for the missing items.
|
||||||
|
proc readValue*(
|
||||||
|
r: var JsonReader[JrpcConv], value: var ContentItem
|
||||||
|
) {.gcsafe, raises: [IOError, SerializationError].} =
|
||||||
|
type IDX = typeof low(value)
|
||||||
|
var count = 0
|
||||||
|
r.parseArray(idx):
|
||||||
|
let i = IDX(idx + low(value).int)
|
||||||
|
if i <= high(value):
|
||||||
|
readValue(r, value[i])
|
||||||
|
count.inc
|
||||||
|
|
||||||
|
if count != value.len():
|
||||||
|
r.raiseUnexpectedValue("Array length mismatch")
|
||||||
|
|
Loading…
Reference in New Issue