2024-01-25 11:04:09 +01:00
|
|
|
# fluffy
|
2025-01-02 16:28:12 +08:00
|
|
|
# Copyright (c) 2021-2025 Status Research & Development GmbH
|
2021-11-24 08:45:55 +01:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-31 13:38:08 +01:00
|
|
|
{.push raises: [].}
|
2021-11-24 08:45:55 +01:00
|
|
|
|
|
|
|
import
|
2024-01-13 08:41:57 +07:00
|
|
|
std/[sequtils, json],
|
2024-09-18 15:46:50 +08:00
|
|
|
json_rpc/rpcserver,
|
2024-01-13 08:41:57 +07:00
|
|
|
json_serialization/std/tables,
|
|
|
|
stew/byteutils,
|
2021-11-24 08:45:55 +01:00
|
|
|
../network/wire/portal_protocol,
|
|
|
|
./rpc_types
|
|
|
|
|
2024-03-06 08:57:09 +01:00
|
|
|
{.warning[UnusedImport]: off.}
|
|
|
|
import json_rpc/errors
|
|
|
|
|
2024-10-08 03:15:29 +02:00
|
|
|
export tables
|
2021-11-24 14:53:01 +01:00
|
|
|
|
2024-10-08 03:15:29 +02:00
|
|
|
# Portal Network JSON-RPC implementation as per specification:
|
2023-01-17 14:47:10 +01:00
|
|
|
# https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
|
|
|
|
|
2024-01-13 08:41:57 +07:00
|
|
|
ContentInfo.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceContentLookupResult.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceObject.useDefaultSerializationIn JrpcConv
|
|
|
|
NodeMetadata.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceResponse.useDefaultSerializationIn JrpcConv
|
2023-10-30 17:48:06 +03:00
|
|
|
|
2024-10-07 22:33:02 +08:00
|
|
|
proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
|
|
|
|
rpcServer.rpc("portal_historyFindContent") do(
|
2024-02-28 18:31:45 +01:00
|
|
|
enr: Record, contentKey: string
|
|
|
|
) -> JsonString:
|
2022-01-14 16:07:14 +01:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
2024-02-28 18:31:45 +01:00
|
|
|
foundContentResult =
|
2024-07-17 17:07:27 +02:00
|
|
|
await p.findContent(node, ContentKeyByteList.init(hexToSeqByte(contentKey)))
|
2022-01-14 16:07:14 +01:00
|
|
|
|
|
|
|
if foundContentResult.isErr():
|
|
|
|
raise newException(ValueError, $foundContentResult.error)
|
|
|
|
else:
|
|
|
|
let foundContent = foundContentResult.get()
|
2024-02-28 18:31:45 +01:00
|
|
|
case foundContent.kind
|
2022-01-14 16:07:14 +01:00
|
|
|
of Content:
|
2024-01-13 08:41:57 +07:00
|
|
|
let res = ContentInfo(
|
2024-02-28 18:31:45 +01:00
|
|
|
content: foundContent.content.to0xHex(), utpTransfer: foundContent.utpTransfer
|
2023-04-28 09:04:21 +02:00
|
|
|
)
|
2025-01-03 15:24:12 +08:00
|
|
|
JrpcConv.encode(res).JsonString
|
2022-01-14 16:07:14 +01:00
|
|
|
of Nodes:
|
2024-02-28 18:31:45 +01:00
|
|
|
let enrs = foundContent.nodes.map(
|
|
|
|
proc(n: Node): Record =
|
|
|
|
n.record
|
|
|
|
)
|
2024-01-13 08:41:57 +07:00
|
|
|
let jsonEnrs = JrpcConv.encode(enrs)
|
2025-01-03 15:24:12 +08:00
|
|
|
("{\"enrs\":" & jsonEnrs & "}").JsonString
|
2022-01-14 16:07:14 +01:00
|
|
|
|
2024-10-07 22:33:02 +08:00
|
|
|
rpcServer.rpc("portal_historyOffer") do(
|
2024-10-07 10:49:04 +02:00
|
|
|
enr: Record, contentItems: seq[ContentItem]
|
2024-02-28 18:31:45 +01:00
|
|
|
) -> string:
|
2024-10-07 10:49:04 +02:00
|
|
|
let node = toNodeWithAddress(enr)
|
2022-12-16 17:47:52 +01:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
var contentOffers: seq[ContentKV]
|
2024-10-07 10:49:04 +02:00
|
|
|
for contentItem in contentItems:
|
|
|
|
let
|
2025-01-03 15:24:12 +08:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentItem[0]))
|
|
|
|
offerValueBytes = hexToSeqByte(contentItem[1])
|
|
|
|
|
|
|
|
contentOffers.add(ContentKV(contentKey: keyBytes, content: offerValueBytes))
|
2024-10-07 10:49:04 +02:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
let offerResult = (await p.offer(node, contentOffers)).valueOr:
|
2024-10-07 10:49:04 +02:00
|
|
|
raise newException(ValueError, $error)
|
|
|
|
|
|
|
|
SSZ.encode(offerResult).to0xHex()
|
2022-12-16 17:47:52 +01:00
|
|
|
|
2024-10-14 16:46:35 +08:00
|
|
|
rpcServer.rpc("portal_historyGetContent") do(contentKey: string) -> ContentInfo:
|
2022-12-16 08:49:18 +01:00
|
|
|
let
|
2025-01-03 15:24:12 +08:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
contentId = p.toContentId(keyBytes).valueOr:
|
2024-10-08 15:21:27 +08:00
|
|
|
raise invalidKeyErr()
|
2025-01-03 15:24:12 +08:00
|
|
|
maybeValueBytes = p.getLocalContent(keyBytes, contentId)
|
2022-12-16 08:49:18 +01:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
if maybeValueBytes.isSome():
|
|
|
|
return ContentInfo(content: maybeValueBytes.get().to0xHex(), utpTransfer: false)
|
2024-11-21 20:20:09 +08:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
let contentLookupResult = (await p.contentLookup(keyBytes, contentId)).valueOr:
|
2024-11-21 20:20:09 +08:00
|
|
|
raise contentNotFoundErr()
|
2022-12-16 08:49:18 +01:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
ContentInfo(
|
|
|
|
content: contentLookupResult.content.to0xHex(),
|
|
|
|
utpTransfer: contentLookupResult.utpTransfer,
|
2024-02-28 18:31:45 +01:00
|
|
|
)
|
2022-12-16 08:49:18 +01:00
|
|
|
|
2024-10-14 16:46:35 +08:00
|
|
|
rpcServer.rpc("portal_historyTraceGetContent") do(
|
2024-02-28 18:31:45 +01:00
|
|
|
contentKey: string
|
|
|
|
) -> TraceContentLookupResult:
|
2023-10-30 17:48:06 +03:00
|
|
|
let
|
2025-01-03 15:24:12 +08:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
contentId = p.toContentId(keyBytes).valueOr:
|
2024-10-08 15:21:27 +08:00
|
|
|
raise invalidKeyErr()
|
2025-01-03 15:24:12 +08:00
|
|
|
maybeValueBytes = p.getLocalContent(keyBytes, contentId)
|
2024-11-21 20:20:09 +08:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
if maybeValueBytes.isSome():
|
|
|
|
return TraceContentLookupResult(content: maybeValueBytes, utpTransfer: false)
|
2023-10-30 17:48:06 +03:00
|
|
|
|
2024-03-06 08:57:09 +01:00
|
|
|
# TODO: Might want to restructure the lookup result here. Potentially doing
|
|
|
|
# the json conversion in this module.
|
2025-01-03 15:24:12 +08:00
|
|
|
let
|
|
|
|
res = await p.traceContentLookup(keyBytes, contentId)
|
|
|
|
_ = res.content.valueOr:
|
|
|
|
let data = Opt.some(JrpcConv.encode(res.trace).JsonString)
|
|
|
|
raise contentNotFoundErrWithTrace(data)
|
|
|
|
|
|
|
|
res
|
2023-10-30 17:48:06 +03:00
|
|
|
|
2024-10-07 22:33:02 +08:00
|
|
|
rpcServer.rpc("portal_historyStore") do(
|
2024-02-28 18:31:45 +01:00
|
|
|
contentKey: string, contentValue: string
|
|
|
|
) -> bool:
|
2024-06-11 21:01:35 +08:00
|
|
|
let
|
2025-01-03 15:24:12 +08:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
offerValueBytes = hexToSeqByte(contentValue)
|
|
|
|
contentId = p.toContentId(keyBytes).valueOr:
|
2024-10-09 20:23:46 +08:00
|
|
|
raise invalidKeyErr()
|
2024-06-11 21:01:35 +08:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
# TODO: Do we need to convert the received offer to a value without proofs before storing?
|
|
|
|
|
|
|
|
p.storeContent(keyBytes, contentId, offerValueBytes)
|
2022-12-16 11:00:10 +01:00
|
|
|
|
2024-10-07 22:33:02 +08:00
|
|
|
rpcServer.rpc("portal_historyLocalContent") do(contentKey: string) -> string:
|
2022-12-16 11:00:10 +01:00
|
|
|
let
|
2025-01-03 15:24:12 +08:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
contentId = p.toContentId(keyBytes).valueOr:
|
2024-10-08 15:21:27 +08:00
|
|
|
raise invalidKeyErr()
|
2022-12-16 11:00:10 +01:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
valueBytes = p.dbGet(keyBytes, contentId).valueOr:
|
2024-10-08 15:21:27 +08:00
|
|
|
raise contentNotFoundErr()
|
2023-04-27 14:07:57 +02:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
valueBytes.to0xHex()
|
2023-01-17 14:47:10 +01:00
|
|
|
|
2024-12-23 15:09:48 +08:00
|
|
|
rpcServer.rpc("portal_historyPutContent") do(
|
2024-02-28 18:31:45 +01:00
|
|
|
contentKey: string, contentValue: string
|
2025-01-02 16:28:12 +08:00
|
|
|
) -> PutContentResult:
|
2023-01-17 14:47:10 +01:00
|
|
|
let
|
2025-01-03 15:24:12 +08:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
contentId = p.toContentId(keyBytes).valueOr:
|
2025-01-02 16:28:12 +08:00
|
|
|
raise invalidKeyErr()
|
2025-01-03 15:24:12 +08:00
|
|
|
offerValueBytes = hexToSeqByte(contentValue)
|
2025-01-02 16:28:12 +08:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
# TODO: Do we need to convert the received offer to a value without proofs before storing?
|
|
|
|
# TODO: validate and store content locally
|
|
|
|
# storedLocally = p.storeContent(keyBytes, contentId, valueBytes)
|
|
|
|
peerCount = await p.neighborhoodGossip(
|
|
|
|
Opt.none(NodeId), ContentKeysList(@[keyBytes]), @[offerValueBytes]
|
|
|
|
)
|
2023-01-17 14:47:10 +01:00
|
|
|
|
2025-01-03 15:24:12 +08:00
|
|
|
PutContentResult(storedLocally: false, peerCount: peerCount)
|