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-04-25 10:22:21 +00:00
|
|
|
eth/[common/eth_types_rlp, rlp, trie, trie/db],
|
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-09-24 11:07:20 +00:00
|
|
|
"."/[history_content, validation/historical_hashes_accumulator],
|
|
|
|
../beacon/beacon_chain_historical_roots,
|
2024-09-12 17:03:58 +00:00
|
|
|
./content/content_deprecated
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-04-13 05:56:01 +00:00
|
|
|
logScope:
|
|
|
|
topics = "portal_hist"
|
|
|
|
|
2024-09-23 16:56:28 +00:00
|
|
|
export historical_hashes_accumulator
|
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]])]
|
2022-10-17 18:38:51 +00:00
|
|
|
accumulator*: FinishedAccumulator
|
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]
|
2022-03-02 14:29:12 +00:00
|
|
|
|
|
|
|
Block* = (BlockHeader, 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
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
## Calls to go from SSZ decoded Portal types to RLP fully decoded EL types
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
func fromPortalBlockBody*(
|
2024-02-28 17:31:45 +00:00
|
|
|
T: type BlockBody, body: PortalBlockBodyLegacy
|
|
|
|
): Result[T, string] =
|
2023-05-30 04:56:54 +00:00
|
|
|
## Get the EL BlockBody from the SSZ-decoded `PortalBlockBodyLegacy`.
|
2022-11-04 08:27:01 +00:00
|
|
|
try:
|
2023-05-30 04:56:54 +00:00
|
|
|
var transactions: seq[Transaction]
|
|
|
|
for tx in body.transactions:
|
|
|
|
transactions.add(rlp.decode(tx.asSeq(), Transaction))
|
2022-11-04 08:27:01 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
let uncles = rlp.decode(body.uncles.asSeq(), seq[BlockHeader])
|
|
|
|
|
|
|
|
ok(BlockBody(transactions: transactions, uncles: uncles))
|
|
|
|
except RlpError as e:
|
|
|
|
err("RLP decoding failed: " & e.msg)
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2023-03-30 12:21:09 +00:00
|
|
|
func fromPortalBlockBody*(
|
2024-02-28 17:31:45 +00:00
|
|
|
T: type BlockBody, body: PortalBlockBodyShanghai
|
|
|
|
): Result[T, string] =
|
2023-05-30 04:56:54 +00:00
|
|
|
## Get the EL BlockBody from the SSZ-decoded `PortalBlockBodyShanghai`.
|
2022-07-01 19:51:51 +00:00
|
|
|
try:
|
|
|
|
var transactions: seq[Transaction]
|
|
|
|
for tx in body.transactions:
|
|
|
|
transactions.add(rlp.decode(tx.asSeq(), Transaction))
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
var withdrawals: seq[Withdrawal]
|
|
|
|
for w in body.withdrawals:
|
|
|
|
withdrawals.add(rlp.decode(w.asSeq(), Withdrawal))
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
ok(
|
|
|
|
BlockBody(
|
|
|
|
transactions: transactions,
|
2024-07-24 18:42:12 +00:00
|
|
|
uncles: @[], # Uncles must be empty, this is verified in `validateBlockBody`
|
2024-06-14 07:31:08 +00:00
|
|
|
withdrawals: Opt.some(withdrawals),
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
|
|
|
)
|
2022-07-01 19:51:51 +00:00
|
|
|
except RlpError as e:
|
|
|
|
err("RLP decoding failed: " & e.msg)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
func fromPortalBlockBodyOrRaise*(
|
2024-02-28 17:31:45 +00:00
|
|
|
T: type BlockBody, body: PortalBlockBodyLegacy | PortalBlockBodyShanghai
|
|
|
|
): T =
|
2023-05-30 04:56:54 +00:00
|
|
|
## Get the EL BlockBody from one of the SSZ-decoded Portal BlockBody types.
|
|
|
|
## Will raise Assertion in case of invalid RLP encodings. Only use of data
|
|
|
|
## has been validated before!
|
|
|
|
let res = BlockBody.fromPortalBlockBody(body)
|
|
|
|
if res.isOk():
|
|
|
|
res.get()
|
|
|
|
else:
|
|
|
|
raiseAssert(res.error)
|
|
|
|
|
|
|
|
func fromPortalReceipts*(
|
2024-02-28 17:31:45 +00:00
|
|
|
T: type seq[Receipt], receipts: PortalReceipts
|
|
|
|
): Result[T, string] =
|
2023-05-30 04:56:54 +00:00
|
|
|
## Get the full decoded EL seq[Receipt] from the SSZ-decoded `PortalReceipts`.
|
2022-03-02 14:29:12 +00:00
|
|
|
try:
|
2022-07-01 19:51:51 +00:00
|
|
|
var res: seq[Receipt]
|
|
|
|
for receipt in receipts:
|
|
|
|
res.add(rlp.decode(receipt.asSeq(), Receipt))
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
ok(res)
|
2022-06-10 10:24:53 +00:00
|
|
|
except RlpError as e:
|
2022-07-01 19:51:51 +00:00
|
|
|
err("RLP decoding failed: " & e.msg)
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
## Calls to encode EL block types to the SSZ encoded Portal types.
|
|
|
|
|
|
|
|
# TODO: The fact that we have different Portal BlockBody types for the different
|
|
|
|
# forks but not for the EL BlockBody (usage of Option) does not play so well
|
|
|
|
# together.
|
|
|
|
|
2024-03-26 21:27:31 +00:00
|
|
|
func fromBlockBody*(T: type PortalBlockBodyLegacy, body: BlockBody): T =
|
2023-05-30 04:56:54 +00:00
|
|
|
var transactions: Transactions
|
|
|
|
for tx in body.transactions:
|
|
|
|
discard transactions.add(TransactionByteList(rlp.encode(tx)))
|
|
|
|
|
|
|
|
let uncles = Uncles(rlp.encode(body.uncles))
|
|
|
|
|
|
|
|
PortalBlockBodyLegacy(transactions: transactions, uncles: uncles)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-03-26 21:27:31 +00:00
|
|
|
func fromBlockBody*(T: type PortalBlockBodyShanghai, body: BlockBody): T =
|
2022-07-01 19:51:51 +00:00
|
|
|
var transactions: Transactions
|
|
|
|
for tx in body.transactions:
|
|
|
|
discard transactions.add(TransactionByteList(rlp.encode(tx)))
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let uncles = Uncles(rlp.encode(body.uncles))
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
doAssert(body.withdrawals.isSome())
|
|
|
|
|
|
|
|
var withdrawals: Withdrawals
|
|
|
|
for w in body.withdrawals.get():
|
|
|
|
discard withdrawals.add(WithdrawalByteList(rlp.encode(w)))
|
2024-02-28 17:31:45 +00:00
|
|
|
PortalBlockBodyShanghai(
|
|
|
|
transactions: transactions, uncles: uncles, withdrawals: withdrawals
|
|
|
|
)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
func fromReceipts*(T: type PortalReceipts, receipts: seq[Receipt]): T =
|
|
|
|
var portalReceipts: PortalReceipts
|
2022-07-01 19:51:51 +00:00
|
|
|
for receipt in receipts:
|
2023-05-30 04:56:54 +00:00
|
|
|
discard portalReceipts.add(ReceiptByteList(rlp.encode(receipt)))
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
portalReceipts
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func encode*(blockBody: BlockBody): seq[byte] =
|
2023-05-30 04:56:54 +00:00
|
|
|
if blockBody.withdrawals.isSome():
|
|
|
|
SSZ.encode(PortalBlockBodyShanghai.fromBlockBody(blockBody))
|
|
|
|
else:
|
|
|
|
SSZ.encode(PortalBlockBodyLegacy.fromBlockBody(blockBody))
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func encode*(receipts: seq[Receipt]): seq[byte] =
|
2023-05-30 04:56:54 +00:00
|
|
|
let portalReceipts = PortalReceipts.fromReceipts(receipts)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
|
|
|
SSZ.encode(portalReceipts)
|
|
|
|
|
|
|
|
## Calls and helper calls to do validation of block header, body and receipts
|
|
|
|
# TODO: Failures on validation and perhaps deserialisation should be punished
|
|
|
|
# for if/when peer scoring/banning is added.
|
|
|
|
|
2023-06-04 06:00:50 +00:00
|
|
|
proc calcRootHash(items: Transactions | PortalReceipts | Withdrawals): Hash256 =
|
2024-04-25 10:22:21 +00:00
|
|
|
var tr = initHexaryTrie(newMemoryDB(), isPruning = false)
|
2023-01-31 12:38:08 +00:00
|
|
|
for i, item in items:
|
2022-07-01 19:51:51 +00:00
|
|
|
try:
|
2024-06-14 07:31:08 +00:00
|
|
|
tr.put(rlp.encode(i.uint), item.asSeq())
|
2024-07-24 18:42:12 +00:00
|
|
|
except RlpError as e:
|
|
|
|
# RlpError should not occur
|
|
|
|
# TODO: trace down why it might raise this
|
2022-07-01 19:51:51 +00:00
|
|
|
raiseAssert(e.msg)
|
|
|
|
|
|
|
|
return tr.rootHash
|
|
|
|
|
|
|
|
template calcTxsRoot*(transactions: Transactions): Hash256 =
|
|
|
|
calcRootHash(transactions)
|
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
template calcReceiptsRoot*(receipts: PortalReceipts): Hash256 =
|
|
|
|
calcRootHash(receipts)
|
|
|
|
|
|
|
|
template calcWithdrawalsRoot*(receipts: Withdrawals): Hash256 =
|
2022-07-01 19:51:51 +00:00
|
|
|
calcRootHash(receipts)
|
|
|
|
|
2024-09-12 17:03:58 +00:00
|
|
|
func validateBlockHeader*(header: BlockHeader, hash: BlockHash): Result[void, string] =
|
|
|
|
if not (header.blockHash() == hash):
|
|
|
|
err("Block header hash does not match")
|
|
|
|
else:
|
|
|
|
ok()
|
|
|
|
|
|
|
|
func validateBlockHeader*(header: BlockHeader, number: uint64): Result[void, string] =
|
|
|
|
if not (header.number == number):
|
|
|
|
err("Block header number does not match")
|
|
|
|
else:
|
|
|
|
ok()
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func validateBlockHeaderBytes*(
|
2024-09-12 17:03:58 +00:00
|
|
|
bytes: openArray[byte], id: uint64 | BlockHash
|
2024-02-28 17:31:45 +00:00
|
|
|
): Result[BlockHeader, string] =
|
|
|
|
let header = ?decodeRlp(bytes, BlockHeader)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2024-02-09 14:50:37 +00:00
|
|
|
# Note:
|
|
|
|
# One could do additional quick-checks here such as timestamp vs the optional
|
|
|
|
# (later forks) added fields. E.g. Shanghai field, Cancun fields,
|
|
|
|
# zero ommersHash, etc.
|
|
|
|
# However, the block hash comparison will obviously catch these and it is
|
|
|
|
# pretty trivial to provide a non-canonical valid header.
|
|
|
|
# It might be somewhat more useful if just done (temporarily) for the headers
|
|
|
|
# post-merge which are currently provided without proof.
|
2024-09-12 17:03:58 +00:00
|
|
|
# For comparison by number this is obviously not sufficient as any other field
|
|
|
|
# could be manipulated and because of this a block header proof will always
|
|
|
|
# be needed.
|
2022-11-26 14:59:19 +00:00
|
|
|
|
2024-09-12 17:03:58 +00:00
|
|
|
?header.validateBlockHeader(id)
|
|
|
|
|
|
|
|
ok(header)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2024-03-11 17:20:29 +00:00
|
|
|
proc validateBlockBody*(
|
2024-02-28 17:31:45 +00:00
|
|
|
body: PortalBlockBodyLegacy, header: BlockHeader
|
|
|
|
): Result[void, string] =
|
2023-05-30 04:56:54 +00:00
|
|
|
## Validate the block body against the txRoot and ommersHash from the header.
|
2022-09-03 18:15:35 +00:00
|
|
|
let calculatedOmmersHash = keccakHash(body.uncles.asSeq())
|
2023-05-30 04:56:54 +00:00
|
|
|
if calculatedOmmersHash != header.ommersHash:
|
|
|
|
return err("Invalid ommers hash")
|
|
|
|
|
|
|
|
let calculatedTxsRoot = calcTxsRoot(body.transactions)
|
|
|
|
if calculatedTxsRoot != header.txRoot:
|
2024-02-28 17:31:45 +00:00
|
|
|
return err(
|
|
|
|
"Invalid transactions root: expected " & $header.txRoot & " - got " &
|
|
|
|
$calculatedTxsRoot
|
|
|
|
)
|
2023-05-30 04:56:54 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
2024-03-11 17:20:29 +00:00
|
|
|
proc validateBlockBody*(
|
2024-02-28 17:31:45 +00:00
|
|
|
body: PortalBlockBodyShanghai, header: BlockHeader
|
|
|
|
): Result[void, string] =
|
2023-05-30 04:56:54 +00:00
|
|
|
## Validate the block body against the txRoot, ommersHash and withdrawalsRoot
|
|
|
|
## from the header.
|
2023-09-18 12:50:07 +00:00
|
|
|
# Shortcut the ommersHash calculation as uncles must be an RLP encoded
|
|
|
|
# empty list
|
|
|
|
if body.uncles.asSeq() != @[byte 0xc0]:
|
|
|
|
return err("Invalid ommers hash, uncles list is not empty")
|
2022-07-01 19:51:51 +00:00
|
|
|
|
|
|
|
let calculatedTxsRoot = calcTxsRoot(body.transactions)
|
2023-05-30 04:56:54 +00:00
|
|
|
if calculatedTxsRoot != header.txRoot:
|
2024-02-28 17:31:45 +00:00
|
|
|
return err(
|
|
|
|
"Invalid transactions root: expected " & $header.txRoot & " - got " &
|
|
|
|
$calculatedTxsRoot
|
|
|
|
)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
# TODO: This check is done higher up but perhaps this can become cleaner with
|
|
|
|
# some refactor.
|
|
|
|
doAssert(header.withdrawalsRoot.isSome())
|
|
|
|
|
2023-09-18 12:50:07 +00:00
|
|
|
let
|
|
|
|
calculatedWithdrawalsRoot = calcWithdrawalsRoot(body.withdrawals)
|
|
|
|
headerWithdrawalsRoot = header.withdrawalsRoot.get()
|
|
|
|
if calculatedWithdrawalsRoot != headerWithdrawalsRoot:
|
2024-02-28 17:31:45 +00:00
|
|
|
return err(
|
|
|
|
"Invalid withdrawals root: expected " & $headerWithdrawalsRoot & " - got " &
|
|
|
|
$calculatedWithdrawalsRoot
|
|
|
|
)
|
2023-04-12 12:20:52 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
ok()
|
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
proc decodeBlockBodyBytes*(bytes: openArray[byte]): Result[BlockBody, string] =
|
|
|
|
if (let body = decodeSsz(bytes, PortalBlockBodyShanghai); body.isOk()):
|
|
|
|
BlockBody.fromPortalBlockBody(body.get())
|
|
|
|
elif (let body = decodeSsz(bytes, PortalBlockBodyLegacy); body.isOk()):
|
|
|
|
BlockBody.fromPortalBlockBody(body.get())
|
|
|
|
else:
|
|
|
|
err("All Portal block body decodings failed")
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
proc validateBlockBodyBytes*(
|
2024-02-28 17:31:45 +00:00
|
|
|
bytes: openArray[byte], header: BlockHeader
|
|
|
|
): Result[BlockBody, string] =
|
2023-05-30 04:56:54 +00:00
|
|
|
## Fully decode the SSZ encoded Portal Block Body and validate it against the
|
|
|
|
## header.
|
|
|
|
## TODO: improve this decoding in combination with the block body validation
|
|
|
|
## calls.
|
2023-10-18 02:16:11 +00:00
|
|
|
let timestamp = Moment.init(header.timestamp.int64, Second)
|
2023-05-30 04:56:54 +00:00
|
|
|
# TODO: The additional header checks are not needed as header is implicitly
|
|
|
|
# verified by means of the accumulator? Except that we don't use this yet
|
|
|
|
# post merge, so the checks are still useful, for now.
|
|
|
|
if isShanghai(chainConfig, timestamp):
|
|
|
|
if header.withdrawalsRoot.isNone():
|
2024-09-12 17:03:58 +00:00
|
|
|
err("Expected withdrawalsRoot for Shanghai block")
|
2023-05-30 04:56:54 +00:00
|
|
|
elif header.ommersHash != EMPTY_UNCLE_HASH:
|
2024-09-12 17:03:58 +00:00
|
|
|
err("Expected empty uncles for a Shanghai block")
|
2023-05-30 04:56:54 +00:00
|
|
|
else:
|
2024-02-28 17:31:45 +00:00
|
|
|
let body = ?decodeSsz(bytes, PortalBlockBodyShanghai)
|
|
|
|
?validateBlockBody(body, header)
|
2023-05-30 04:56:54 +00:00
|
|
|
BlockBody.fromPortalBlockBody(body)
|
2024-06-14 07:31:08 +00:00
|
|
|
elif isPoSBlock(chainConfig, header.number):
|
2023-05-30 04:56:54 +00:00
|
|
|
if header.withdrawalsRoot.isSome():
|
2024-09-12 17:03:58 +00:00
|
|
|
err("Expected no withdrawalsRoot for pre Shanghai block")
|
2023-05-30 04:56:54 +00:00
|
|
|
elif header.ommersHash != EMPTY_UNCLE_HASH:
|
2024-09-12 17:03:58 +00:00
|
|
|
err("Expected empty uncles for a PoS block")
|
2023-05-30 04:56:54 +00:00
|
|
|
else:
|
2024-02-28 17:31:45 +00:00
|
|
|
let body = ?decodeSsz(bytes, PortalBlockBodyLegacy)
|
|
|
|
?validateBlockBody(body, header)
|
2023-05-30 04:56:54 +00:00
|
|
|
BlockBody.fromPortalBlockBody(body)
|
|
|
|
else:
|
|
|
|
if header.withdrawalsRoot.isSome():
|
2024-09-12 17:03:58 +00:00
|
|
|
err("Expected no withdrawalsRoot for pre Shanghai block")
|
2023-05-30 04:56:54 +00:00
|
|
|
else:
|
2024-02-28 17:31:45 +00:00
|
|
|
let body = ?decodeSsz(bytes, PortalBlockBodyLegacy)
|
|
|
|
?validateBlockBody(body, header)
|
2023-05-30 04:56:54 +00:00
|
|
|
BlockBody.fromPortalBlockBody(body)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2023-03-28 13:50:23 +00:00
|
|
|
proc validateReceipts*(
|
2024-02-28 17:31:45 +00:00
|
|
|
receipts: PortalReceipts, receiptsRoot: KeccakHash
|
|
|
|
): Result[void, string] =
|
2024-09-12 17:03:58 +00:00
|
|
|
if calcReceiptsRoot(receipts) != receiptsRoot:
|
|
|
|
err("Unexpected receipt root")
|
2022-07-01 19:51:51 +00:00
|
|
|
else:
|
2024-09-12 17:03:58 +00:00
|
|
|
ok()
|
2022-07-01 19:51:51 +00:00
|
|
|
|
|
|
|
proc validateReceiptsBytes*(
|
2024-02-28 17:31:45 +00:00
|
|
|
bytes: openArray[byte], receiptsRoot: KeccakHash
|
|
|
|
): Result[seq[Receipt], string] =
|
2024-07-24 18:42:12 +00:00
|
|
|
## Fully decode the SSZ encoded receipts and validate it against the header's
|
|
|
|
## receipts root.
|
2024-02-28 17:31:45 +00:00
|
|
|
let receipts = ?decodeSsz(bytes, PortalReceipts)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
?validateReceipts(receipts, receiptsRoot)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
seq[Receipt].fromPortalReceipts(receipts)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
## ContentDB helper calls for specific history network types
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc get(db: ContentDB, T: type BlockHeader, contentId: ContentId): Opt[T] =
|
2022-07-01 19:51:51 +00:00
|
|
|
let contentFromDB = db.get(contentId)
|
|
|
|
if contentFromDB.isSome():
|
2022-11-04 08:27:01 +00:00
|
|
|
let headerWithProof =
|
|
|
|
try:
|
|
|
|
SSZ.decode(contentFromDB.get(), BlockHeaderWithProof)
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError as e:
|
2022-11-04 08:27:01 +00:00
|
|
|
raiseAssert(e.msg)
|
|
|
|
|
|
|
|
let res = decodeRlp(headerWithProof.header.asSeq(), T)
|
2022-07-01 19:51:51 +00:00
|
|
|
if res.isErr():
|
|
|
|
raiseAssert(res.error)
|
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.some(res.get())
|
2022-07-01 19:51:51 +00:00
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.none(T)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc get(
|
|
|
|
db: ContentDB, T: type BlockBody, contentId: ContentId, header: BlockHeader
|
|
|
|
): Opt[T] =
|
2023-06-27 17:43:32 +00:00
|
|
|
let encoded = db.get(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-02-28 17:31:45 +00:00
|
|
|
decodeSszOrRaise(encoded, PortalBlockBodyShanghai)
|
|
|
|
)
|
2024-06-14 07:31:08 +00:00
|
|
|
elif isPoSBlock(chainConfig, header.number):
|
2023-06-27 17:43:32 +00:00
|
|
|
BlockBody.fromPortalBlockBodyOrRaise(
|
2024-02-28 17:31:45 +00:00
|
|
|
decodeSszOrRaise(encoded, PortalBlockBodyLegacy)
|
|
|
|
)
|
2023-06-27 17:43:32 +00:00
|
|
|
else:
|
|
|
|
BlockBody.fromPortalBlockBodyOrRaise(
|
2024-02-28 17:31:45 +00:00
|
|
|
decodeSszOrRaise(encoded, PortalBlockBodyLegacy)
|
|
|
|
)
|
2023-05-30 04:56:54 +00:00
|
|
|
|
|
|
|
Opt.some(body)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc get(db: ContentDB, T: type seq[Receipt], contentId: ContentId): Opt[T] =
|
2023-05-30 04:56:54 +00:00
|
|
|
let contentFromDB = db.getSszDecoded(contentId, PortalReceipts)
|
2022-07-01 19:51:51 +00:00
|
|
|
if contentFromDB.isSome():
|
2023-05-30 04:56:54 +00:00
|
|
|
let res = T.fromPortalReceipts(contentFromDB.get())
|
2022-07-01 19:51:51 +00:00
|
|
|
if res.isErr():
|
|
|
|
raiseAssert(res.error)
|
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.some(res.get())
|
2022-07-01 19:51:51 +00:00
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.none(T)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2024-07-11 15:42:45 +00:00
|
|
|
proc get(db: ContentDB, T: type EpochRecord, contentId: ContentId): Opt[T] =
|
2022-08-01 19:00:21 +00:00
|
|
|
db.getSszDecoded(contentId, T)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc getContentFromDb(n: HistoryNetwork, T: type, contentId: ContentId): Opt[T] =
|
2022-07-11 14:29:16 +00:00
|
|
|
if n.portalProtocol.inRange(contentId):
|
|
|
|
n.contentDB.get(T, contentId)
|
2022-03-02 14:29:12 +00:00
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.none(T)
|
2022-11-08 17:31:45 +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
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
const requestRetries = 4
|
|
|
|
# TODO: Currently doing 4 retries on lookups but only when the validation fails.
|
|
|
|
# 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
|
|
|
func verifyHeader(
|
2024-02-28 17:31:45 +00:00
|
|
|
n: HistoryNetwork, header: BlockHeader, proof: BlockHeaderProof
|
|
|
|
): Result[void, string] =
|
2022-11-04 08:27:01 +00:00
|
|
|
verifyHeader(n.accumulator, header, proof)
|
|
|
|
|
|
|
|
proc getVerifiedBlockHeader*(
|
2024-09-12 17:03:58 +00:00
|
|
|
n: HistoryNetwork, id: BlockHash | uint64
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[BlockHeader]] {.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.
|
2022-11-04 08:27:01 +00:00
|
|
|
let headerFromDb = n.getContentFromDb(BlockHeader, contentId)
|
|
|
|
if headerFromDb.isSome():
|
2022-12-09 16:59:36 +00:00
|
|
|
info "Fetched block header from database"
|
2022-11-04 08:27:01 +00:00
|
|
|
return headerFromDb
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< requestRetries:
|
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"
|
|
|
|
return Opt.none(BlockHeader)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
headerWithProof = decodeSsz(headerContent.content, BlockHeaderWithProof).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Failed decoding header with proof", error
|
|
|
|
continue
|
|
|
|
|
2024-09-12 17:03:58 +00:00
|
|
|
header = validateBlockHeaderBytes(headerWithProof.header.asSeq(), id).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Validation of block header failed", error
|
|
|
|
continue
|
|
|
|
|
|
|
|
if (let r = n.verifyHeader(header, headerWithProof.proof); r.isErr):
|
|
|
|
warn "Verification of block header failed", error = r.error
|
|
|
|
continue
|
|
|
|
|
|
|
|
info "Fetched valid block header from the network"
|
|
|
|
# Content is valid, it can be stored and propagated to interested peers
|
|
|
|
n.portalProtocol.storeContent(contentKey, contentId, headerContent.content)
|
|
|
|
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
|
|
|
|
|
|
|
# Headers were requested `requestRetries` times and all failed on validation
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.none(BlockHeader)
|
2022-11-04 08:27:01 +00:00
|
|
|
|
2022-06-29 15:44:08 +00:00
|
|
|
proc getBlockBody*(
|
2024-02-28 17:31:45 +00:00
|
|
|
n: HistoryNetwork, hash: BlockHash, header: BlockHeader
|
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-09-12 17:03:58 +00:00
|
|
|
contentKey = blockBodyContentKey(hash).encode()
|
2022-12-09 16:59:36 +00:00
|
|
|
contentId = contentKey.toContentId()
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
hash
|
|
|
|
contentKey
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
let bodyFromDb = n.contentDB.get(BlockBody, contentId, header)
|
2022-07-01 19:51:51 +00:00
|
|
|
if bodyFromDb.isSome():
|
2022-12-09 16:59:36 +00:00
|
|
|
info "Fetched block body from database"
|
2022-07-05 12:42:55 +00:00
|
|
|
return bodyFromDb
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< requestRetries:
|
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
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
info "Fetched block body from the network"
|
|
|
|
# Content is valid, it can be stored and propagated to interested peers
|
|
|
|
n.portalProtocol.storeContent(contentKey, contentId, bodyContent.content)
|
|
|
|
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
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
# Bodies were requested `requestRetries` times and all failed on validation
|
|
|
|
return Opt.none(BlockBody)
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2024-06-14 12:21:30 +00:00
|
|
|
proc getBlock*(
|
2024-09-12 17:03:58 +00:00
|
|
|
n: HistoryNetwork, id: BlockHash | 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 =
|
|
|
|
when id is BlockHash:
|
|
|
|
id
|
|
|
|
else:
|
|
|
|
header.blockHash()
|
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
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
return 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
|
|
|
|
): Future[Result[BlockHash, string]] {.async: (raises: [CancelledError]).} =
|
|
|
|
let header = (await n.getVerifiedBlockHeader(blockNumber)).valueOr:
|
|
|
|
return err("Cannot retrieve block header for given block number")
|
|
|
|
|
|
|
|
ok(header.blockHash())
|
|
|
|
|
2022-06-10 10:24:53 +00:00
|
|
|
proc getReceipts*(
|
2024-02-28 17:31:45 +00:00
|
|
|
n: HistoryNetwork, hash: BlockHash, header: BlockHeader
|
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-09-12 17:03:58 +00:00
|
|
|
contentKey = receiptsContentKey(hash).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:
|
|
|
|
hash
|
|
|
|
contentKey
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
let receiptsFromDb = n.getContentFromDb(seq[Receipt], contentId)
|
2022-07-01 19:51:51 +00:00
|
|
|
if receiptsFromDb.isSome():
|
2022-12-09 16:59:36 +00:00
|
|
|
info "Fetched receipts from database"
|
2022-07-05 12:42:55 +00:00
|
|
|
return receiptsFromDb
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< requestRetries:
|
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
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
info "Fetched receipts from the network"
|
|
|
|
# Content is valid, it can be stored and propagated to interested peers
|
|
|
|
n.portalProtocol.storeContent(contentKey, contentId, receiptsContent.content)
|
|
|
|
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
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
proc validateContent(
|
2024-07-17 15:07:27 +00:00
|
|
|
n: HistoryNetwork, content: seq[byte], contentKey: ContentKeyByteList
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
2022-12-09 16:59:36 +00:00
|
|
|
let key = contentKey.decode().valueOr:
|
2022-04-11 17:42:38 +00:00
|
|
|
return false
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
case key.contentType
|
2022-04-11 17:42:38 +00:00
|
|
|
of blockHeader:
|
2023-01-20 22:04:58 +00:00
|
|
|
let
|
|
|
|
headerWithProof = decodeSsz(content, BlockHeaderWithProof).valueOr:
|
|
|
|
warn "Failed decoding header with proof", error
|
|
|
|
return false
|
|
|
|
header = validateBlockHeaderBytes(
|
2024-02-28 17:31:45 +00:00
|
|
|
headerWithProof.header.asSeq(), key.blockHeaderKey.blockHash
|
|
|
|
).valueOr:
|
2023-01-20 22:04:58 +00:00
|
|
|
warn "Invalid block header offered", error
|
|
|
|
return false
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2023-01-20 22:04:58 +00:00
|
|
|
let res = n.verifyHeader(header, headerWithProof.proof)
|
|
|
|
if res.isErr():
|
2024-02-28 17:31:45 +00:00
|
|
|
warn "Failed on check if header is part of canonical chain", error = res.error
|
2022-08-01 19:00:21 +00:00
|
|
|
return false
|
|
|
|
else:
|
|
|
|
return true
|
2022-11-04 08:27:01 +00:00
|
|
|
of blockBody:
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = (await n.getVerifiedBlockHeader(key.blockBodyKey.blockHash)).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Failed getting canonical header for block"
|
2022-07-11 14:29:16 +00:00
|
|
|
return false
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
let res = validateBlockBodyBytes(content, header)
|
2022-12-09 16:59:36 +00:00
|
|
|
if res.isErr():
|
|
|
|
warn "Failed validating block body", error = res.error
|
2022-08-01 19:00:21 +00:00
|
|
|
return false
|
|
|
|
else:
|
|
|
|
return true
|
2022-11-04 08:27:01 +00:00
|
|
|
of receipts:
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = (await n.getVerifiedBlockHeader(key.receiptsKey.blockHash)).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
warn "Failed getting canonical header for receipts"
|
2022-08-01 19:00:21 +00:00
|
|
|
return false
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
let res = validateReceiptsBytes(content, header.receiptsRoot)
|
2022-12-09 16:59:36 +00:00
|
|
|
if res.isErr():
|
|
|
|
warn "Failed validating receipts", error = res.error
|
2022-07-11 14:29:16 +00:00
|
|
|
return false
|
2022-08-01 19:00:21 +00:00
|
|
|
else:
|
|
|
|
return true
|
2024-09-12 17:03:58 +00:00
|
|
|
of blockNumber:
|
|
|
|
let
|
|
|
|
headerWithProof = decodeSsz(content, BlockHeaderWithProof).valueOr:
|
|
|
|
warn "Failed decoding header with proof", error
|
|
|
|
return false
|
|
|
|
header = validateBlockHeaderBytes(
|
|
|
|
headerWithProof.header.asSeq(), key.blockNumberKey.blockNumber
|
|
|
|
).valueOr:
|
|
|
|
warn "Invalid block header offered", error
|
2022-08-01 19:00:21 +00:00
|
|
|
return false
|
2022-10-17 18:38:51 +00:00
|
|
|
|
2024-09-12 17:03:58 +00:00
|
|
|
let res = n.verifyHeader(header, headerWithProof.proof)
|
|
|
|
if res.isErr():
|
|
|
|
warn "Failed on check if header is part of canonical chain", error = res.error
|
2022-08-01 19:00:21 +00:00
|
|
|
return false
|
|
|
|
else:
|
|
|
|
return true
|
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,
|
2022-10-17 18:38:51 +00:00
|
|
|
accumulator: FinishedAccumulator,
|
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,
|
|
|
|
): 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-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,
|
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-02-28 17:31:45 +00:00
|
|
|
n: HistoryNetwork, 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]
|
|
|
|
if await n.validateContent(contentItem, contentKey):
|
2023-06-27 17:43:32 +00:00
|
|
|
let contentId = n.portalProtocol.toContentId(contentKey).valueOr:
|
2022-09-29 06:42:54 +00:00
|
|
|
error "Received offered content with invalid content key", contentKey
|
|
|
|
return false
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(contentKey, contentId, contentItem)
|
2022-09-29 06:42:54 +00:00
|
|
|
|
|
|
|
info "Received offered content validated successfully", contentKey
|
|
|
|
else:
|
|
|
|
error "Received offered content failed validation", contentKey
|
|
|
|
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.
|
|
|
|
if await n.validateContent(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
|