2024-03-13 15:58:50 +00:00
|
|
|
# fluffy
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-12-03 08:51:25 +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.
|
|
|
|
|
|
|
|
import
|
2024-10-03 02:57:19 +00:00
|
|
|
json_serialization,
|
2024-10-02 04:23:13 +00:00
|
|
|
chronos,
|
|
|
|
stew/byteutils,
|
|
|
|
results,
|
2024-10-04 21:21:26 +00:00
|
|
|
eth/common/[headers_rlp, blocks_rlp, receipts_rlp],
|
2021-12-03 08:51:25 +00:00
|
|
|
json_rpc/rpcclient,
|
2024-10-02 04:23:13 +00:00
|
|
|
../common/common_types,
|
|
|
|
../network/history/[history_content, history_network],
|
2024-03-13 15:58:50 +00:00
|
|
|
./rpc_calls/[rpc_discovery_calls, rpc_portal_calls, rpc_portal_debug_calls]
|
2021-12-03 08:51:25 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
export rpcclient, rpc_discovery_calls, rpc_portal_calls, rpc_portal_debug_calls, results
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
PortalRpcClient* = distinct RpcClient
|
|
|
|
|
|
|
|
PortalRpcError* = enum
|
|
|
|
ContentNotFound
|
|
|
|
InvalidContentKey
|
|
|
|
InvalidContentValue
|
|
|
|
ContentValidationFailed
|
|
|
|
|
2024-10-03 02:57:19 +00:00
|
|
|
ErrorResponse = object
|
|
|
|
code*: int
|
|
|
|
message*: string
|
|
|
|
|
2024-10-02 04:23:13 +00:00
|
|
|
proc init*(T: type PortalRpcClient, rpcClient: RpcClient): T =
|
|
|
|
T(rpcClient)
|
|
|
|
|
|
|
|
func toPortalRpcError(e: ref CatchableError): PortalRpcError =
|
2024-10-03 02:57:19 +00:00
|
|
|
let error =
|
|
|
|
try:
|
|
|
|
Json.decode(e.msg, ErrorResponse)
|
|
|
|
except SerializationError as e:
|
|
|
|
raiseAssert(e.msg)
|
|
|
|
|
|
|
|
if error.code == -39001:
|
2024-10-02 04:23:13 +00:00
|
|
|
ContentNotFound
|
2024-10-03 02:57:19 +00:00
|
|
|
elif error.code == -32602:
|
2024-10-02 04:23:13 +00:00
|
|
|
InvalidContentKey
|
|
|
|
else:
|
2024-10-03 02:57:19 +00:00
|
|
|
raiseAssert(e.msg)
|
2024-10-02 04:23:13 +00:00
|
|
|
|
2024-10-14 08:46:35 +00:00
|
|
|
proc portal_historyLocalContent(
|
2024-10-02 04:23:13 +00:00
|
|
|
client: PortalRpcClient, contentKey: string
|
|
|
|
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
|
|
|
|
try:
|
|
|
|
let content = await RpcClient(client).portal_historyLocalContent(contentKey)
|
|
|
|
ok(content)
|
|
|
|
except CatchableError as e:
|
|
|
|
err(e.toPortalRpcError())
|
|
|
|
|
2024-10-14 08:46:35 +00:00
|
|
|
proc portal_historyGetContent(
|
2024-10-02 04:23:13 +00:00
|
|
|
client: PortalRpcClient, contentKey: string
|
|
|
|
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
|
|
|
|
try:
|
2024-10-14 08:46:35 +00:00
|
|
|
let contentInfo = await RpcClient(client).portal_historyGetContent(contentKey)
|
2024-10-02 04:23:13 +00:00
|
|
|
ok(contentInfo.content)
|
|
|
|
except CatchableError as e:
|
|
|
|
err(e.toPortalRpcError())
|
|
|
|
|
|
|
|
template toBytes(content: string): seq[byte] =
|
|
|
|
try:
|
|
|
|
hexToSeqByte(content)
|
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert(e.msg)
|
|
|
|
|
|
|
|
template valueOrErr[T](res: Result[T, string], error: PortalRpcError): auto =
|
|
|
|
if res.isOk():
|
|
|
|
ok(res.value)
|
|
|
|
else:
|
|
|
|
err(error)
|
|
|
|
|
|
|
|
proc historyGetContent(
|
|
|
|
client: PortalRpcClient, contentKey: string
|
|
|
|
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
|
|
|
|
# Look up the content from the local db before trying to get it from the network
|
2024-10-14 08:46:35 +00:00
|
|
|
let content = (await client.portal_historyLocalContent(contentKey)).valueOr:
|
2024-10-02 04:23:13 +00:00
|
|
|
if error == ContentNotFound:
|
2024-10-14 08:46:35 +00:00
|
|
|
?await client.portal_historyGetContent(contentKey)
|
2024-10-02 04:23:13 +00:00
|
|
|
else:
|
|
|
|
return err(error)
|
|
|
|
ok(content)
|
|
|
|
|
|
|
|
proc historyGetBlockHeader*(
|
2024-10-07 20:39:07 +00:00
|
|
|
client: PortalRpcClient, blockHash: Hash32, validateContent = true
|
2024-10-04 21:21:26 +00:00
|
|
|
): Future[Result[Header, PortalRpcError]] {.async: (raises: []).} =
|
2024-10-02 04:23:13 +00:00
|
|
|
## Fetches the block header for the given hash from the Portal History Network.
|
|
|
|
## The data is first looked up in the node's local database before trying to
|
|
|
|
## fetch it from the network.
|
|
|
|
##
|
|
|
|
## Note: This does not validate that the returned header is part of the canonical
|
|
|
|
## chain, it only validates that the header matches the block hash. For example,
|
|
|
|
## a malicious portal node could return a valid but non-canonical header such
|
|
|
|
## as an uncle block that matches the block hash. For this reason the caller
|
|
|
|
## needs to use another method to verify the header is part of the canonical chain.
|
|
|
|
|
|
|
|
let
|
|
|
|
contentKey = blockHeaderContentKey(blockHash).encode().asSeq().to0xHex()
|
|
|
|
content = ?await client.historyGetContent(contentKey)
|
|
|
|
headerWithProof = decodeSsz(content.toBytes(), BlockHeaderWithProof).valueOr:
|
|
|
|
return err(InvalidContentValue)
|
|
|
|
headerBytes = headerWithProof.header.asSeq()
|
|
|
|
|
|
|
|
if validateContent:
|
|
|
|
validateBlockHeaderBytes(headerBytes, blockHash).valueOrErr(ContentValidationFailed)
|
|
|
|
else:
|
2024-10-04 21:21:26 +00:00
|
|
|
decodeRlp(headerBytes, Header).valueOrErr(InvalidContentValue)
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
proc historyGetBlockBody*(
|
2024-10-07 20:39:07 +00:00
|
|
|
client: PortalRpcClient, blockHash: Hash32, validateContent = true
|
2024-10-02 04:23:13 +00:00
|
|
|
): Future[Result[BlockBody, PortalRpcError]] {.async: (raises: []).} =
|
|
|
|
## Fetches the block body for the given block hash from the Portal History
|
|
|
|
## Network. The data is first looked up in the node's local database before
|
|
|
|
## trying to fetch it from the network. If validateContent is true, the
|
|
|
|
## block header is fetched first in order to run the content validation.
|
|
|
|
|
|
|
|
let
|
|
|
|
contentKey = blockBodyContentKey(blockHash).encode().asSeq().to0xHex()
|
|
|
|
content = ?await client.historyGetContent(contentKey)
|
|
|
|
|
|
|
|
if validateContent:
|
|
|
|
let blockHeader = ?await client.historyGetBlockHeader(blockHash)
|
|
|
|
validateBlockBodyBytes(content.toBytes(), blockHeader).valueOrErr(
|
|
|
|
ContentValidationFailed
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
decodeBlockBodyBytes(content.toBytes()).valueOrErr(InvalidContentValue)
|
|
|
|
|
|
|
|
proc historyGetReceipts*(
|
2024-10-07 20:39:07 +00:00
|
|
|
client: PortalRpcClient, blockHash: Hash32, validateContent = true
|
2024-10-02 04:23:13 +00:00
|
|
|
): Future[Result[seq[Receipt], PortalRpcError]] {.async: (raises: []).} =
|
|
|
|
## Fetches the receipts for the given block hash from the Portal History
|
|
|
|
## Network. The data is first looked up in the node's local database before
|
|
|
|
## trying to fetch it from the network. If validateContent is true, the
|
|
|
|
## block header is fetched first in order to run the content validation.
|
|
|
|
|
|
|
|
let
|
|
|
|
contentKey = receiptsContentKey(blockHash).encode().asSeq().to0xHex()
|
|
|
|
content = ?await client.historyGetContent(contentKey)
|
|
|
|
|
|
|
|
if validateContent:
|
|
|
|
let blockHeader = ?await client.historyGetBlockHeader(blockHash)
|
|
|
|
validateReceiptsBytes(content.toBytes(), blockHeader.receiptsRoot).valueOrErr(
|
|
|
|
ContentValidationFailed
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
let receipts = decodeSsz(content.toBytes(), PortalReceipts).valueOr:
|
|
|
|
return err(InvalidContentValue)
|
|
|
|
seq[Receipt].fromPortalReceipts(receipts).valueOrErr(InvalidContentValue)
|