2024-01-25 10:04:09 +00:00
|
|
|
# fluffy
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-11-24 07:45:55 +00: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 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-11-24 07:45:55 +00:00
|
|
|
|
|
|
|
import
|
2024-01-13 01:41:57 +00:00
|
|
|
std/[sequtils, json],
|
2024-09-18 07:46:50 +00:00
|
|
|
json_rpc/rpcserver,
|
2024-01-13 01:41:57 +00:00
|
|
|
json_serialization/std/tables,
|
|
|
|
stew/byteutils,
|
2021-11-24 07:45:55 +00:00
|
|
|
../network/wire/portal_protocol,
|
2024-10-09 12:23:46 +00:00
|
|
|
../network/state/[state_content, state_validation],
|
2021-11-24 07:45:55 +00:00
|
|
|
./rpc_types
|
|
|
|
|
2024-03-06 07:57:09 +00:00
|
|
|
{.warning[UnusedImport]: off.}
|
|
|
|
import json_rpc/errors
|
|
|
|
|
2024-10-08 01:15:29 +00:00
|
|
|
export tables
|
2021-11-24 13:53:01 +00:00
|
|
|
|
2024-10-08 01:15:29 +00:00
|
|
|
# Portal Network JSON-RPC implementation as per specification:
|
2023-01-17 13:47:10 +00:00
|
|
|
# https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
|
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
ContentInfo.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceContentLookupResult.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceObject.useDefaultSerializationIn JrpcConv
|
|
|
|
NodeMetadata.useDefaultSerializationIn JrpcConv
|
|
|
|
TraceResponse.useDefaultSerializationIn JrpcConv
|
2023-10-30 14:48:06 +00:00
|
|
|
|
2024-10-07 14:33:02 +00:00
|
|
|
proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
|
|
|
|
rpcServer.rpc("portal_stateFindContent") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
enr: Record, contentKey: string
|
|
|
|
) -> JsonString:
|
2022-01-14 15:07:14 +00:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
2024-10-10 13:24:39 +00:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
(key, _) = validateGetContentKey(keyBytes).valueOr:
|
|
|
|
raise invalidKeyErr()
|
|
|
|
foundContent = (await p.findContent(node, keyBytes)).valueOr:
|
|
|
|
raise newException(ValueError, $error)
|
|
|
|
|
|
|
|
case foundContent.kind
|
|
|
|
of Content:
|
|
|
|
let contentValue = foundContent.content
|
|
|
|
validateRetrieval(key, contentValue).isOkOr:
|
|
|
|
raise invalidValueErr()
|
|
|
|
|
|
|
|
let res = ContentInfo(
|
|
|
|
content: contentValue.to0xHex(), utpTransfer: foundContent.utpTransfer
|
|
|
|
)
|
|
|
|
JrpcConv.encode(res).JsonString
|
|
|
|
of Nodes:
|
|
|
|
let enrs = foundContent.nodes.map(
|
|
|
|
proc(n: Node): Record =
|
|
|
|
n.record
|
|
|
|
)
|
|
|
|
let jsonEnrs = JrpcConv.encode(enrs)
|
|
|
|
("{\"enrs\":" & jsonEnrs & "}").JsonString
|
2022-01-14 15:07:14 +00:00
|
|
|
|
2024-10-07 14:33:02 +00:00
|
|
|
rpcServer.rpc("portal_stateOffer") do(
|
2024-10-07 08:49:04 +00:00
|
|
|
enr: Record, contentItems: seq[ContentItem]
|
2024-02-28 17:31:45 +00:00
|
|
|
) -> string:
|
2024-10-07 08:49:04 +00:00
|
|
|
let node = toNodeWithAddress(enr)
|
2022-12-16 16:47:52 +00:00
|
|
|
|
2024-10-07 08:49:04 +00:00
|
|
|
var contentItemsToOffer: seq[ContentKV]
|
|
|
|
for contentItem in contentItems:
|
|
|
|
let
|
2024-10-10 13:24:39 +00:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentItem[0]))
|
|
|
|
(key, _) = validateGetContentKey(keyBytes).valueOr:
|
|
|
|
raise invalidKeyErr()
|
|
|
|
contentBytes = hexToSeqByte(contentItem[1])
|
|
|
|
contentKV = ContentKV(contentKey: keyBytes, content: contentBytes)
|
|
|
|
|
|
|
|
discard validateOfferGetValue(Opt.none(Hash32), key, contentBytes).valueOr:
|
|
|
|
raise invalidValueErr()
|
2024-10-07 08:49:04 +00:00
|
|
|
contentItemsToOffer.add(contentKV)
|
|
|
|
|
|
|
|
let offerResult = (await p.offer(node, contentItemsToOffer)).valueOr:
|
|
|
|
raise newException(ValueError, $error)
|
|
|
|
|
|
|
|
SSZ.encode(offerResult).to0xHex()
|
2022-12-16 16:47:52 +00:00
|
|
|
|
2024-10-14 08:46:35 +00:00
|
|
|
rpcServer.rpc("portal_stateGetContent") do(contentKey: string) -> ContentInfo:
|
2022-12-16 07:49:18 +00:00
|
|
|
let
|
2024-10-10 13:24:39 +00:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
(key, contentId) = validateGetContentKey(keyBytes).valueOr:
|
2024-10-08 07:21:27 +00:00
|
|
|
raise invalidKeyErr()
|
2024-10-10 13:24:39 +00:00
|
|
|
maybeContent = p.dbGet(keyBytes, contentId)
|
|
|
|
if maybeContent.isSome():
|
|
|
|
return ContentInfo(content: maybeContent.get().to0xHex(), utpTransfer: false)
|
2022-12-16 07:49:18 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
let
|
|
|
|
foundContent = (await p.contentLookup(keyBytes, contentId)).valueOr:
|
2024-10-08 07:21:27 +00:00
|
|
|
raise contentNotFoundErr()
|
2024-10-10 13:24:39 +00:00
|
|
|
contentValue = foundContent.content
|
2022-12-16 07:49:18 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
validateRetrieval(key, contentValue).isOkOr:
|
|
|
|
raise invalidValueErr()
|
|
|
|
p.storeContent(keyBytes, contentId, contentValue)
|
|
|
|
|
|
|
|
ContentInfo(content: contentValue.to0xHex(), utpTransfer: foundContent.utpTransfer)
|
2022-12-16 07:49:18 +00:00
|
|
|
|
2024-10-14 08:46:35 +00:00
|
|
|
rpcServer.rpc("portal_stateTraceGetContent") do(
|
2024-02-28 17:31:45 +00:00
|
|
|
contentKey: string
|
|
|
|
) -> TraceContentLookupResult:
|
2023-10-30 14:48:06 +00:00
|
|
|
let
|
2024-10-10 13:24:39 +00:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
(key, contentId) = validateGetContentKey(keyBytes).valueOr:
|
2024-10-08 07:21:27 +00:00
|
|
|
raise invalidKeyErr()
|
2024-10-10 13:24:39 +00:00
|
|
|
maybeContent = p.dbGet(keyBytes, contentId)
|
|
|
|
if maybeContent.isSome():
|
|
|
|
return TraceContentLookupResult(content: maybeContent, utpTransfer: false)
|
2023-10-30 14:48:06 +00:00
|
|
|
|
2024-03-06 07:57:09 +00:00
|
|
|
# TODO: Might want to restructure the lookup result here. Potentially doing
|
|
|
|
# the json conversion in this module.
|
2024-10-10 13:24:39 +00:00
|
|
|
let
|
|
|
|
res = await p.traceContentLookup(keyBytes, contentId)
|
|
|
|
contentValue = res.content.valueOr:
|
|
|
|
let data = Opt.some(JrpcConv.encode(res.trace).JsonString)
|
|
|
|
raise contentNotFoundErrWithTrace(data)
|
|
|
|
|
|
|
|
validateRetrieval(key, contentValue).isOkOr:
|
|
|
|
raise invalidValueErr()
|
|
|
|
p.storeContent(keyBytes, contentId, contentValue)
|
|
|
|
|
|
|
|
res
|
|
|
|
|
|
|
|
rpcServer.rpc("portal_stateStore") do(contentKey: string, content: string) -> bool:
|
2024-06-11 13:01:35 +00:00
|
|
|
let
|
2024-10-09 12:23:46 +00:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
2024-10-10 13:24:39 +00:00
|
|
|
(key, contentId) = validateGetContentKey(keyBytes).valueOr:
|
2024-10-08 07:21:27 +00:00
|
|
|
raise invalidKeyErr()
|
2024-10-10 13:24:39 +00:00
|
|
|
contentBytes = hexToSeqByte(content)
|
|
|
|
contentValue = validateOfferGetValue(Opt.none(Hash32), key, contentBytes).valueOr:
|
|
|
|
raise invalidValueErr()
|
2024-10-09 12:23:46 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
p.storeContent(keyBytes, contentId, contentValue)
|
2022-12-16 10:00:10 +00:00
|
|
|
|
2024-10-07 14:33:02 +00:00
|
|
|
rpcServer.rpc("portal_stateLocalContent") do(contentKey: string) -> string:
|
2022-12-16 10:00:10 +00:00
|
|
|
let
|
2024-10-09 12:23:46 +00:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
2024-10-10 13:24:39 +00:00
|
|
|
(_, contentId) = validateGetContentKey(keyBytes).valueOr:
|
2024-10-08 07:21:27 +00:00
|
|
|
raise invalidKeyErr()
|
2022-12-16 10:00:10 +00:00
|
|
|
|
2024-10-09 12:23:46 +00:00
|
|
|
contentResult = p.dbGet(keyBytes, contentId).valueOr:
|
2024-10-08 07:21:27 +00:00
|
|
|
raise contentNotFoundErr()
|
2023-04-27 12:07:57 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
contentResult.to0xHex()
|
2023-01-17 13:47:10 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
rpcServer.rpc("portal_stateGossip") do(contentKey: string, content: string) -> int:
|
2023-01-17 13:47:10 +00:00
|
|
|
let
|
2024-10-10 13:24:39 +00:00
|
|
|
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
|
|
|
|
(key, contentId) = validateGetContentKey(keyBytes).valueOr:
|
|
|
|
raise invalidKeyErr()
|
|
|
|
contentBytes = hexToSeqByte(content)
|
|
|
|
contentValue = validateOfferGetValue(Opt.none(Hash32), key, contentBytes).valueOr:
|
|
|
|
raise invalidValueErr()
|
2023-01-17 13:47:10 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
p.storeContent(keyBytes, contentId, contentValue)
|
|
|
|
|
|
|
|
await p.neighborhoodGossip(
|
|
|
|
Opt.none(NodeId), ContentKeysList(@[keyBytes]), @[contentBytes]
|
|
|
|
)
|