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]).} =
|
||||
if len(content) > contentKeysLimit:
|
||||
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 req = OfferRequest(dst: dst, kind: Direct, contentList: contentList)
|
||||
|
|
|
@ -161,19 +161,24 @@ proc installPortalApiHandlers*(
|
|||
return ("{\"enrs\":" & jsonEnrs & "}").JsonString
|
||||
|
||||
rpcServer.rpc("portal_" & network & "Offer") do(
|
||||
enr: Record, contentKey: string, contentValue: string
|
||||
enr: Record, contentItems: seq[ContentItem]
|
||||
) -> string:
|
||||
let
|
||||
node = toNodeWithAddress(enr)
|
||||
key = hexToSeqByte(contentKey)
|
||||
content = hexToSeqByte(contentValue)
|
||||
contentKV = ContentKV(contentKey: ContentKeyByteList.init(key), content: content)
|
||||
res = await p.offer(node, @[contentKV])
|
||||
let node = toNodeWithAddress(enr)
|
||||
|
||||
if res.isOk():
|
||||
return SSZ.encode(res.get()).to0xHex()
|
||||
else:
|
||||
raise newException(ValueError, $res.error)
|
||||
var contentItemsToOffer: seq[ContentKV]
|
||||
for contentItem in contentItems:
|
||||
let
|
||||
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(
|
||||
nodeId: NodeId
|
||||
|
|
|
@ -31,6 +31,8 @@ type
|
|||
content*: string
|
||||
utpTransfer*: bool
|
||||
|
||||
ContentItem* = array[2, string]
|
||||
|
||||
NodeInfo.useDefaultSerializationIn JrpcConv
|
||||
RoutingTableInfo.useDefaultSerializationIn JrpcConv
|
||||
(string, string).useDefaultSerializationIn JrpcConv
|
||||
|
@ -125,3 +127,22 @@ proc readValue*(
|
|||
discard
|
||||
except ValueError as exc:
|
||||
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