2023-12-01 16:20:52 +00:00
|
|
|
# Fluffy
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-10-09 11:22:03 +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: [].}
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
import
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2024-02-28 17:31:45 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
2024-10-09 07:44:15 +00:00
|
|
|
eth/trie/ordered_trie,
|
2024-10-04 21:21:26 +00:00
|
|
|
eth/common/[hashes, headers_rlp, blocks_rlp, receipts_rlp, transactions_rlp],
|
2022-01-06 08:06:05 +00:00
|
|
|
eth/p2p/discoveryv5/[protocol, enr],
|
2023-05-30 04:56:54 +00:00
|
|
|
../../common/common_types,
|
2023-12-01 16:20:52 +00:00
|
|
|
../../database/content_db,
|
2023-05-30 04:56:54 +00:00
|
|
|
../../network_metadata,
|
2022-01-18 08:01:22 +00:00
|
|
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2024-11-06 07:49:55 +00:00
|
|
|
"."/[history_content, history_validation, history_type_conversions],
|
2024-09-24 11:07:20 +00:00
|
|
|
../beacon/beacon_chain_historical_roots,
|
2024-09-12 17:03:58 +00:00
|
|
|
./content/content_deprecated
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
|
|
|
from eth/common/accounts import EMPTY_ROOT_HASH
|
|
|
|
|
2022-04-13 05:56:01 +00:00
|
|
|
logScope:
|
|
|
|
topics = "portal_hist"
|
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
export blocks_rlp
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2022-03-18 12:06:57 +00:00
|
|
|
type
|
2022-03-02 14:29:12 +00:00
|
|
|
HistoryNetwork* = ref object
|
|
|
|
portalProtocol*: PortalProtocol
|
|
|
|
contentDB*: ContentDB
|
2023-09-04 10:21:01 +00:00
|
|
|
contentQueue*: AsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])]
|
2024-09-27 20:25:26 +00:00
|
|
|
accumulator*: FinishedHistoricalHashesAccumulator
|
2024-04-17 15:58:47 +00:00
|
|
|
historicalRoots*: HistoricalRoots
|
2022-07-11 14:29:16 +00:00
|
|
|
processContentLoop: Future[void]
|
2023-03-31 16:52:21 +00:00
|
|
|
statusLogLoop: Future[void]
|
2024-10-25 07:15:01 +00:00
|
|
|
contentRequestRetries: int
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
Block* = (Header, BlockBody)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-07-17 15:07:27 +00:00
|
|
|
func toContentIdHandler(contentKey: ContentKeyByteList): results.Opt[ContentId] =
|
2022-11-08 17:31:45 +00:00
|
|
|
ok(toContentId(contentKey))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
## Get local content calls
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
proc getLocalContent(
|
2024-10-17 08:25:53 +00:00
|
|
|
n: HistoryNetwork,
|
|
|
|
T: type Header,
|
|
|
|
contentKey: ContentKeyByteList,
|
|
|
|
contentId: ContentId,
|
|
|
|
): Opt[T] =
|
2024-11-06 07:49:55 +00:00
|
|
|
let
|
|
|
|
localContent = n.portalProtocol.getLocalContent(contentKey, contentId).valueOr:
|
|
|
|
return Opt.none(T)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
# Stored data should always be serialized correctly
|
|
|
|
headerWithProof = decodeSszOrRaise(localContent, BlockHeaderWithProof)
|
|
|
|
header = decodeRlpOrRaise(headerWithProof.header.asSeq(), T)
|
2024-10-17 08:25:53 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
Opt.some(header)
|
2024-10-17 08:25:53 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
proc getLocalContent(
|
2024-10-17 08:25:53 +00:00
|
|
|
n: HistoryNetwork,
|
|
|
|
T: type BlockBody,
|
|
|
|
contentKey: ContentKeyByteList,
|
|
|
|
contentId: ContentId,
|
|
|
|
header: Header,
|
2024-02-28 17:31:45 +00:00
|
|
|
): Opt[T] =
|
2024-10-17 08:25:53 +00:00
|
|
|
let localContent = n.portalProtocol.getLocalContent(contentKey, contentId).valueOr:
|
2023-05-30 04:56:54 +00:00
|
|
|
return Opt.none(T)
|
|
|
|
|
2023-06-27 17:43:32 +00:00
|
|
|
let
|
2023-10-18 02:16:11 +00:00
|
|
|
timestamp = Moment.init(header.timestamp.int64, Second)
|
2023-06-27 17:43:32 +00:00
|
|
|
body =
|
|
|
|
if isShanghai(chainConfig, timestamp):
|
|
|
|
BlockBody.fromPortalBlockBodyOrRaise(
|
2024-10-17 08:25:53 +00:00
|
|
|
decodeSszOrRaise(localContent, PortalBlockBodyShanghai)
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2024-06-14 07:31:08 +00:00
|
|
|
elif isPoSBlock(chainConfig, header.number):
|
2023-06-27 17:43:32 +00:00
|
|
|
BlockBody.fromPortalBlockBodyOrRaise(
|
2024-10-17 08:25:53 +00:00
|
|
|
decodeSszOrRaise(localContent, PortalBlockBodyLegacy)
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2023-06-27 17:43:32 +00:00
|
|
|
else:
|
|
|
|
BlockBody.fromPortalBlockBodyOrRaise(
|
2024-10-17 08:25:53 +00:00
|
|
|
decodeSszOrRaise(localContent, PortalBlockBodyLegacy)
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2023-05-30 04:56:54 +00:00
|
|
|
|
|
|
|
Opt.some(body)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
proc getLocalContent(
|
2024-10-17 08:25:53 +00:00
|
|
|
n: HistoryNetwork,
|
|
|
|
T: type seq[Receipt],
|
|
|
|
contentKey: ContentKeyByteList,
|
|
|
|
contentId: ContentId,
|
|
|
|
): Opt[T] =
|
2024-11-06 07:49:55 +00:00
|
|
|
let
|
|
|
|
localContent = n.portalProtocol.getLocalContent(contentKey, contentId).valueOr:
|
|
|
|
return Opt.none(T)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
# Stored data should always be serialized correctly
|
|
|
|
portalReceipts = decodeSszOrRaise(localContent, PortalReceipts)
|
|
|
|
receipts = T.fromPortalReceipts(portalReceipts).valueOr:
|
|
|
|
raiseAssert(error)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
Opt.some(receipts)
|
2024-10-17 08:25:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
## Public API to get the history network specific types, either from database
|
|
|
|
## or through a lookup on the Portal Network
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
# TODO: Currently doing retries on lookups but only when the validation fails.
|
2022-07-05 12:42:55 +00:00
|
|
|
# This is to avoid nodes that provide garbage from blocking us with getting the
|
|
|
|
# requested data. Might want to also do that on a failed lookup, as perhaps this
|
|
|
|
# could occur when being really unlucky with nodes timing out on requests.
|
|
|
|
# Additionally, more improvements could be done with the lookup, as currently
|
|
|
|
# ongoing requests are cancelled after the receival of the first response,
|
|
|
|
# however that response is not yet validated at that moment.
|
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
proc getVerifiedBlockHeader*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: HistoryNetwork, id: Hash32 | uint64
|
2024-10-04 21:21:26 +00:00
|
|
|
): Future[Opt[Header]] {.async: (raises: [CancelledError]).} =
|
2022-12-09 16:59:36 +00:00
|
|
|
let
|
2024-09-12 17:03:58 +00:00
|
|
|
contentKey = blockHeaderContentKey(id).encode()
|
|
|
|
contentId = history_content.toContentId(contentKey)
|
2022-12-09 16:59:36 +00:00
|
|
|
|
|
|
|
logScope:
|
2024-09-12 17:03:58 +00:00
|
|
|
id
|
2022-12-09 16:59:36 +00:00
|
|
|
contentKey
|
2022-11-04 08:27:01 +00:00
|
|
|
|
|
|
|
# Note: This still requests a BlockHeaderWithProof from the database, as that
|
2022-12-09 16:59:36 +00:00
|
|
|
# is what is stored. But the proof doesn't need to be verified as it gets
|
|
|
|
# gets verified before storing.
|
2024-11-06 07:49:55 +00:00
|
|
|
let localContent = n.getLocalContent(Header, contentKey, contentId)
|
|
|
|
if localContent.isSome():
|
2024-11-06 14:44:20 +00:00
|
|
|
debug "Fetched block header locally"
|
2024-11-06 07:49:55 +00:00
|
|
|
return localContent
|
2022-11-04 08:27:01 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
for i in 0 ..< (1 + n.contentRequestRetries):
|
2022-12-09 16:59:36 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
headerContent = (await n.portalProtocol.contentLookup(contentKey, contentId)).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Failed fetching block header with proof from the network"
|
2024-10-04 21:21:26 +00:00
|
|
|
return Opt.none(Header)
|
2022-12-09 16:59:36 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
header = validateCanonicalHeaderBytes(headerContent.content, id, n.accumulator).valueOr:
|
2024-10-06 12:15:54 +00:00
|
|
|
warn "Validation of block header failed", error = error
|
2022-12-09 16:59:36 +00:00
|
|
|
continue
|
|
|
|
|
2024-11-06 14:44:20 +00:00
|
|
|
debug "Fetched valid block header from the network"
|
2022-12-09 16:59:36 +00:00
|
|
|
# Content is valid, it can be stored and propagated to interested peers
|
2024-10-17 08:25:53 +00:00
|
|
|
n.portalProtocol.storeContent(
|
|
|
|
contentKey, contentId, headerContent.content, cacheContent = true
|
|
|
|
)
|
2022-12-09 16:59:36 +00:00
|
|
|
n.portalProtocol.triggerPoke(
|
2024-02-28 17:31:45 +00:00
|
|
|
headerContent.nodesInterestedInContent, contentKey, headerContent.content
|
2022-12-09 16:59:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return Opt.some(header)
|
2022-11-04 08:27:01 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
# Headers were requested `1 + requestRetries` times and all failed on validation
|
2024-11-06 07:49:55 +00:00
|
|
|
Opt.none(Header)
|
2022-11-04 08:27:01 +00:00
|
|
|
|
2022-06-29 15:44:08 +00:00
|
|
|
proc getBlockBody*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: HistoryNetwork, blockHash: Hash32, header: Header
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[BlockBody]] {.async: (raises: [CancelledError]).} =
|
2022-09-03 18:15:35 +00:00
|
|
|
if header.txRoot == EMPTY_ROOT_HASH and header.ommersHash == EMPTY_UNCLE_HASH:
|
2022-12-09 16:59:36 +00:00
|
|
|
# Short path for empty body indicated by txRoot and ommersHash
|
|
|
|
return Opt.some(BlockBody(transactions: @[], uncles: @[]))
|
2022-07-20 10:46:42 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let
|
2024-10-07 20:39:07 +00:00
|
|
|
contentKey = blockBodyContentKey(blockHash).encode()
|
2022-12-09 16:59:36 +00:00
|
|
|
contentId = contentKey.toContentId()
|
|
|
|
|
|
|
|
logScope:
|
2024-10-07 20:39:07 +00:00
|
|
|
blockHash
|
2022-12-09 16:59:36 +00:00
|
|
|
contentKey
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
let localContent = n.getLocalContent(BlockBody, contentKey, contentId, header)
|
|
|
|
if localContent.isSome():
|
2024-11-06 14:44:20 +00:00
|
|
|
debug "Fetched block body locally"
|
2024-11-06 07:49:55 +00:00
|
|
|
return localContent
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
for i in 0 ..< (1 + n.contentRequestRetries):
|
2022-12-09 16:59:36 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
bodyContent = (await n.portalProtocol.contentLookup(contentKey, contentId)).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Failed fetching block body from the network"
|
|
|
|
return Opt.none(BlockBody)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
body = validateBlockBodyBytes(bodyContent.content, header).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Validation of block body failed", error
|
|
|
|
continue
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-11-06 14:44:20 +00:00
|
|
|
debug "Fetched block body from the network"
|
2022-12-09 16:59:36 +00:00
|
|
|
# Content is valid, it can be stored and propagated to interested peers
|
2024-10-17 08:25:53 +00:00
|
|
|
n.portalProtocol.storeContent(
|
|
|
|
contentKey, contentId, bodyContent.content, cacheContent = true
|
|
|
|
)
|
2022-12-09 16:59:36 +00:00
|
|
|
n.portalProtocol.triggerPoke(
|
2024-02-28 17:31:45 +00:00
|
|
|
bodyContent.nodesInterestedInContent, contentKey, bodyContent.content
|
2022-12-09 16:59:36 +00:00
|
|
|
)
|
2022-04-11 09:25:36 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.some(body)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
# Bodies were requested `1 + requestRetries` times and all failed on validation
|
2024-11-06 07:49:55 +00:00
|
|
|
Opt.none(BlockBody)
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2024-06-14 12:21:30 +00:00
|
|
|
proc getBlock*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: HistoryNetwork, id: Hash32 | uint64
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[Block]] {.async: (raises: [CancelledError]).} =
|
2024-09-12 17:03:58 +00:00
|
|
|
debug "Trying to retrieve block", id
|
2022-07-29 12:24:07 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
# Note: Using `getVerifiedBlockHeader` instead of getBlockHeader even though
|
|
|
|
# proofs are not necessiarly needed, in order to avoid having to inject
|
|
|
|
# also the original type into the network.
|
2022-12-09 16:59:36 +00:00
|
|
|
let
|
2024-09-12 17:03:58 +00:00
|
|
|
header = (await n.getVerifiedBlockHeader(id)).valueOr:
|
|
|
|
warn "Failed to get header when getting block", id
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.none(Block)
|
2024-09-12 17:03:58 +00:00
|
|
|
hash =
|
2024-10-07 20:39:07 +00:00
|
|
|
when id is Hash32:
|
2024-09-12 17:03:58 +00:00
|
|
|
id
|
|
|
|
else:
|
2024-10-04 21:21:26 +00:00
|
|
|
header.rlpHash()
|
2022-12-09 16:59:36 +00:00
|
|
|
body = (await n.getBlockBody(hash, header)).valueOr:
|
|
|
|
warn "Failed to get body when getting block", hash
|
|
|
|
return Opt.none(Block)
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
Opt.some((header, body))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-09-12 17:03:58 +00:00
|
|
|
proc getBlockHashByNumber*(
|
|
|
|
n: HistoryNetwork, blockNumber: uint64
|
2024-10-07 20:39:07 +00:00
|
|
|
): Future[Result[Hash32, string]] {.async: (raises: [CancelledError]).} =
|
2024-09-12 17:03:58 +00:00
|
|
|
let header = (await n.getVerifiedBlockHeader(blockNumber)).valueOr:
|
|
|
|
return err("Cannot retrieve block header for given block number")
|
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
ok(header.rlpHash())
|
2024-09-12 17:03:58 +00:00
|
|
|
|
2022-06-10 10:24:53 +00:00
|
|
|
proc getReceipts*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: HistoryNetwork, blockHash: Hash32, header: Header
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[seq[Receipt]]] {.async: (raises: [CancelledError]).} =
|
2024-06-14 07:31:08 +00:00
|
|
|
if header.receiptsRoot == EMPTY_ROOT_HASH:
|
2022-07-05 12:42:55 +00:00
|
|
|
# Short path for empty receipts indicated by receipts root
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.some(newSeq[Receipt]())
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
let
|
2024-10-07 20:39:07 +00:00
|
|
|
contentKey = receiptsContentKey(blockHash).encode()
|
2022-12-09 16:59:36 +00:00
|
|
|
contentId = contentKey.toContentId()
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
logScope:
|
2024-10-07 20:39:07 +00:00
|
|
|
blockHash
|
2022-12-09 16:59:36 +00:00
|
|
|
contentKey
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
let localContent = n.getLocalContent(seq[Receipt], contentKey, contentId)
|
|
|
|
if localContent.isSome():
|
2024-11-06 14:44:20 +00:00
|
|
|
debug "Fetched receipts locally"
|
2024-11-06 07:49:55 +00:00
|
|
|
return localContent
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
for i in 0 ..< (1 + n.contentRequestRetries):
|
2022-12-09 16:59:36 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
receiptsContent = (await n.portalProtocol.contentLookup(contentKey, contentId)).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Failed fetching receipts from the network"
|
|
|
|
return Opt.none(seq[Receipt])
|
2024-06-14 07:31:08 +00:00
|
|
|
receipts = validateReceiptsBytes(receiptsContent.content, header.receiptsRoot).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Validation of receipts failed", error
|
|
|
|
continue
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2024-11-06 14:44:20 +00:00
|
|
|
debug "Fetched receipts from the network"
|
2022-12-09 16:59:36 +00:00
|
|
|
# Content is valid, it can be stored and propagated to interested peers
|
2024-10-17 08:25:53 +00:00
|
|
|
n.portalProtocol.storeContent(
|
|
|
|
contentKey, contentId, receiptsContent.content, cacheContent = true
|
|
|
|
)
|
2022-12-09 16:59:36 +00:00
|
|
|
n.portalProtocol.triggerPoke(
|
2024-02-28 17:31:45 +00:00
|
|
|
receiptsContent.nodesInterestedInContent, contentKey, receiptsContent.content
|
2022-12-09 16:59:36 +00:00
|
|
|
)
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.some(receipts)
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
# Receipts were requested `1 + requestRetries` times and all failed on validation
|
2024-11-06 07:49:55 +00:00
|
|
|
Opt.none(seq[Receipt])
|
2024-10-25 07:15:01 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
proc validateContent(
|
2024-11-06 07:49:55 +00:00
|
|
|
n: HistoryNetwork, content: seq[byte], contentKeyBytes: ContentKeyByteList
|
|
|
|
): Future[Result[void, string]] {.async: (raises: [CancelledError]).} =
|
|
|
|
let contentKey = contentKeyBytes.decode().valueOr:
|
|
|
|
return err("Error decoding content key")
|
2022-04-11 17:42:38 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
case contentKey.contentType
|
2022-04-11 17:42:38 +00:00
|
|
|
of blockHeader:
|
2024-11-06 07:49:55 +00:00
|
|
|
let _ = validateCanonicalHeaderBytes(
|
|
|
|
content, contentKey.blockHeaderKey.blockHash, n.accumulator
|
|
|
|
).valueOr:
|
|
|
|
return err("Failed validating block header: " & error)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
ok()
|
2022-11-04 08:27:01 +00:00
|
|
|
of blockBody:
|
2024-11-06 07:49:55 +00:00
|
|
|
let
|
|
|
|
header = (await n.getVerifiedBlockHeader(contentKey.blockBodyKey.blockHash)).valueOr:
|
|
|
|
return err("Failed getting canonical header for block")
|
|
|
|
_ = validateBlockBodyBytes(content, header).valueOr:
|
|
|
|
return err("Failed validating block body: " & error)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
ok()
|
2022-11-04 08:27:01 +00:00
|
|
|
of receipts:
|
2024-11-06 07:49:55 +00:00
|
|
|
let
|
|
|
|
header = (await n.getVerifiedBlockHeader(contentKey.receiptsKey.blockHash)).valueOr:
|
|
|
|
return err("Failed getting canonical header for receipts")
|
|
|
|
_ = validateReceiptsBytes(content, header.receiptsRoot).valueOr:
|
|
|
|
return err("Failed validating receipts: " & error)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
ok()
|
2024-09-12 17:03:58 +00:00
|
|
|
of blockNumber:
|
2024-11-06 07:49:55 +00:00
|
|
|
let _ = validateCanonicalHeaderBytes(
|
|
|
|
content, contentKey.blockNumberKey.blockNumber, n.accumulator
|
|
|
|
).valueOr:
|
|
|
|
return err("Failed validating block header: " & error)
|
2022-10-17 18:38:51 +00:00
|
|
|
|
2024-11-06 07:49:55 +00:00
|
|
|
ok()
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc new*(
|
|
|
|
T: type HistoryNetwork,
|
2024-06-18 07:32:57 +00:00
|
|
|
portalNetwork: PortalNetwork,
|
2022-01-06 08:06:05 +00:00
|
|
|
baseProtocol: protocol.Protocol,
|
|
|
|
contentDB: ContentDB,
|
2022-08-17 07:32:06 +00:00
|
|
|
streamManager: StreamManager,
|
2024-09-27 20:25:26 +00:00
|
|
|
accumulator: FinishedHistoricalHashesAccumulator,
|
2024-04-17 15:58:47 +00:00
|
|
|
historicalRoots: HistoricalRoots = loadHistoricalRoots(),
|
2022-01-18 08:01:22 +00:00
|
|
|
bootstrapRecords: openArray[Record] = [],
|
2024-02-28 17:31:45 +00:00
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig,
|
2024-10-25 07:15:01 +00:00
|
|
|
contentRequestRetries = 1,
|
2024-02-28 17:31:45 +00:00
|
|
|
): T =
|
2022-10-17 18:38:51 +00:00
|
|
|
let
|
2023-09-04 10:21:01 +00:00
|
|
|
contentQueue = newAsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])](50)
|
2022-08-17 07:32:06 +00:00
|
|
|
|
2022-10-17 18:38:51 +00:00
|
|
|
stream = streamManager.registerNewStream(contentQueue)
|
2022-11-08 17:31:45 +00:00
|
|
|
|
2022-10-17 18:38:51 +00:00
|
|
|
portalProtocol = PortalProtocol.new(
|
2024-02-28 17:31:45 +00:00
|
|
|
baseProtocol,
|
2024-06-18 07:32:57 +00:00
|
|
|
getProtocolId(portalNetwork, PortalSubnetwork.history),
|
2024-02-28 17:31:45 +00:00
|
|
|
toContentIdHandler,
|
|
|
|
createGetHandler(contentDB),
|
2024-09-09 15:52:11 +00:00
|
|
|
createStoreHandler(contentDB, portalConfig.radiusConfig),
|
2024-11-04 14:02:51 +00:00
|
|
|
createContainsHandler(contentDB),
|
2024-09-05 16:31:55 +00:00
|
|
|
createRadiusHandler(contentDB),
|
2024-02-28 17:31:45 +00:00
|
|
|
stream,
|
|
|
|
bootstrapRecords,
|
|
|
|
config = portalConfig,
|
|
|
|
)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-10-17 18:38:51 +00:00
|
|
|
HistoryNetwork(
|
2022-08-17 07:32:06 +00:00
|
|
|
portalProtocol: portalProtocol,
|
|
|
|
contentDB: contentDB,
|
2022-10-17 18:38:51 +00:00
|
|
|
contentQueue: contentQueue,
|
2024-02-28 17:31:45 +00:00
|
|
|
accumulator: accumulator,
|
2024-04-17 15:58:47 +00:00
|
|
|
historicalRoots: historicalRoots,
|
2024-10-25 07:15:01 +00:00
|
|
|
contentRequestRetries: contentRequestRetries,
|
2022-08-17 07:32:06 +00:00
|
|
|
)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-09-29 06:42:54 +00:00
|
|
|
proc validateContent(
|
2024-11-21 12:17:38 +00:00
|
|
|
n: HistoryNetwork,
|
|
|
|
srcNodeId: Opt[NodeId],
|
|
|
|
contentKeys: ContentKeysList,
|
|
|
|
contentItems: seq[seq[byte]],
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
2022-09-29 06:42:54 +00:00
|
|
|
# content passed here can have less items then contentKeys, but not more.
|
|
|
|
for i, contentItem in contentItems:
|
|
|
|
let contentKey = contentKeys[i]
|
2024-11-06 07:49:55 +00:00
|
|
|
let res = await n.validateContent(contentItem, contentKey)
|
|
|
|
if res.isOk():
|
2023-06-27 17:43:32 +00:00
|
|
|
let contentId = n.portalProtocol.toContentId(contentKey).valueOr:
|
2024-11-21 12:17:38 +00:00
|
|
|
warn "Received offered content with invalid content key", srcNodeId, contentKey
|
2022-09-29 06:42:54 +00:00
|
|
|
return false
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(contentKey, contentId, contentItem)
|
2022-09-29 06:42:54 +00:00
|
|
|
|
2024-11-21 12:17:38 +00:00
|
|
|
debug "Received offered content validated successfully", srcNodeId, contentKey
|
2022-09-29 06:42:54 +00:00
|
|
|
else:
|
2024-11-21 12:17:38 +00:00
|
|
|
debug "Received offered content failed validation",
|
|
|
|
srcNodeId, contentKey, error = res.error
|
2022-09-29 06:42:54 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2024-06-14 12:21:30 +00:00
|
|
|
proc processContentLoop(n: HistoryNetwork) {.async: (raises: []).} =
|
2022-07-11 14:29:16 +00:00
|
|
|
try:
|
|
|
|
while true:
|
2024-02-28 17:31:45 +00:00
|
|
|
let (srcNodeId, contentKeys, contentItems) = await n.contentQueue.popFirst()
|
2022-07-11 14:29:16 +00:00
|
|
|
|
2022-09-29 06:42:54 +00:00
|
|
|
# When there is one invalid content item, all other content items are
|
|
|
|
# dropped and not gossiped around.
|
|
|
|
# TODO: Differentiate between failures due to invalid data and failures
|
|
|
|
# due to missing network data for validation.
|
2024-11-21 12:17:38 +00:00
|
|
|
if await n.validateContent(srcNodeId, contentKeys, contentItems):
|
2022-10-11 10:10:54 +00:00
|
|
|
asyncSpawn n.portalProtocol.neighborhoodGossipDiscardPeers(
|
2023-09-04 10:21:01 +00:00
|
|
|
srcNodeId, contentKeys, contentItems
|
2022-10-11 10:10:54 +00:00
|
|
|
)
|
2022-07-11 14:29:16 +00:00
|
|
|
except CancelledError:
|
|
|
|
trace "processContentLoop canceled"
|
|
|
|
|
2024-06-14 12:21:30 +00:00
|
|
|
proc statusLogLoop(n: HistoryNetwork) {.async: (raises: []).} =
|
2023-03-31 16:52:21 +00:00
|
|
|
try:
|
|
|
|
while true:
|
|
|
|
info "History network status",
|
|
|
|
routingTableNodes = n.portalProtocol.routingTable.len()
|
|
|
|
|
|
|
|
await sleepAsync(60.seconds)
|
|
|
|
except CancelledError:
|
|
|
|
trace "statusLogLoop canceled"
|
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
proc start*(n: HistoryNetwork) =
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Starting Portal execution history network",
|
2022-10-17 18:38:51 +00:00
|
|
|
protocolId = n.portalProtocol.protocolId,
|
|
|
|
accumulatorRoot = hash_tree_root(n.accumulator)
|
2024-09-19 13:38:49 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
n.portalProtocol.start()
|
|
|
|
|
|
|
|
n.processContentLoop = processContentLoop(n)
|
2023-03-31 16:52:21 +00:00
|
|
|
n.statusLogLoop = statusLogLoop(n)
|
2024-09-12 17:03:58 +00:00
|
|
|
pruneDeprecatedAccumulatorRecords(n.accumulator, n.contentDB)
|
2022-07-11 14:29:16 +00:00
|
|
|
|
2024-09-20 12:54:36 +00:00
|
|
|
proc stop*(n: HistoryNetwork) {.async: (raises: []).} =
|
2024-09-19 13:38:49 +00:00
|
|
|
info "Stopping Portal execution history network"
|
|
|
|
|
2024-09-20 12:54:36 +00:00
|
|
|
var futures: seq[Future[void]]
|
|
|
|
futures.add(n.portalProtocol.stop())
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
if not n.processContentLoop.isNil:
|
2024-09-20 12:54:36 +00:00
|
|
|
futures.add(n.processContentLoop.cancelAndWait())
|
2024-09-19 13:38:49 +00:00
|
|
|
if not n.statusLogLoop.isNil:
|
2024-09-20 12:54:36 +00:00
|
|
|
futures.add(n.statusLogLoop.cancelAndWait())
|
|
|
|
await noCancel(allFutures(futures))
|
|
|
|
|
|
|
|
n.processContentLoop = nil
|
|
|
|
n.statusLogLoop = nil
|