2021-10-09 11:22:03 +00:00
|
|
|
# Nimbus
|
2022-04-13 05:56:01 +00:00
|
|
|
# Copyright (c) 2021-2022 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.
|
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
import
|
2022-08-01 19:00:21 +00:00
|
|
|
std/[options, tables],
|
2022-09-03 18:15:35 +00:00
|
|
|
stew/results, chronos, chronicles,
|
|
|
|
eth/[common/eth_types_rlp, rlp, trie, trie/db],
|
2022-01-06 08:06:05 +00:00
|
|
|
eth/p2p/discoveryv5/[protocol, enr],
|
2021-10-09 11:22:03 +00:00
|
|
|
../../content_db,
|
2022-07-01 19:51:51 +00:00
|
|
|
../../../nimbus/constants,
|
2022-01-18 08:01:22 +00:00
|
|
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2022-09-09 19:21:48 +00:00
|
|
|
"."/[history_content, accumulator]
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-04-13 05:56:01 +00:00
|
|
|
logScope:
|
|
|
|
topics = "portal_hist"
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
export accumulator
|
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
const
|
2021-12-08 10:54:22 +00:00
|
|
|
historyProtocolId* = [byte 0x50, 0x0B]
|
2021-10-09 11:22:03 +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
|
2022-08-17 07:32:06 +00:00
|
|
|
contentQueue*: AsyncQueue[(ContentKeysList, seq[seq[byte]])]
|
2022-10-17 18:38:51 +00:00
|
|
|
accumulator*: FinishedAccumulator
|
2022-07-11 14:29:16 +00:00
|
|
|
processContentLoop: Future[void]
|
2022-03-02 14:29:12 +00:00
|
|
|
|
|
|
|
Block* = (BlockHeader, BlockBody)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
func toContentIdHandler(contentKey: ByteList): results.Opt[ContentId] =
|
|
|
|
ok(toContentId(contentKey))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
func encodeKey(k: ContentKey): (ByteList, ContentId) =
|
|
|
|
let keyEncoded = encode(k)
|
|
|
|
return (keyEncoded, toContentId(keyEncoded))
|
|
|
|
|
2022-04-01 16:01:50 +00:00
|
|
|
func getEncodedKeyForContent(
|
2022-09-28 07:09:38 +00:00
|
|
|
cType: ContentType, hash: BlockHash):
|
2022-04-01 16:01:50 +00:00
|
|
|
(ByteList, ContentId) =
|
2022-09-28 07:09:38 +00:00
|
|
|
let contentKeyType = BlockKey(blockHash: hash)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
|
|
|
let contentKey =
|
2022-03-02 14:29:12 +00:00
|
|
|
case cType
|
|
|
|
of blockHeader:
|
|
|
|
ContentKey(contentType: cType, blockHeaderKey: contentKeyType)
|
|
|
|
of blockBody:
|
|
|
|
ContentKey(contentType: cType, blockBodyKey: contentKeyType)
|
|
|
|
of receipts:
|
|
|
|
ContentKey(contentType: cType, receiptsKey: contentKeyType)
|
2022-06-14 21:38:34 +00:00
|
|
|
of epochAccumulator:
|
|
|
|
raiseAssert("Not implemented")
|
2022-11-04 08:27:01 +00:00
|
|
|
of blockHeaderWithProof:
|
|
|
|
ContentKey(contentType: cType, blockHeaderWithProofKey: contentKeyType)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
return encodeKey(contentKey)
|
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
func decodeRlp*(input: openArray[byte], T: type): Result[T, string] =
|
2022-03-02 14:29:12 +00:00
|
|
|
try:
|
2022-11-04 08:27:01 +00:00
|
|
|
ok(rlp.decode(input, T))
|
2022-06-10 10:24:53 +00:00
|
|
|
except RlpError as e:
|
|
|
|
err(e.msg)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
func decodeSsz*(input: openArray[byte], T: type): Result[T, string] =
|
|
|
|
try:
|
|
|
|
ok(SSZ.decode(input, T))
|
|
|
|
except SszError as e:
|
|
|
|
err(e.msg)
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
## Calls to go from SSZ decoded types to RLP fully decoded types
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func fromPortalBlockBody(
|
|
|
|
T: type BlockBody, body: BlockBodySSZ): Result[T, string] =
|
|
|
|
## Get the full decoded BlockBody from the SSZ-decoded `PortalBlockBody`.
|
|
|
|
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
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let uncles = rlp.decode(body.uncles.asSeq(), seq[BlockHeader])
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
ok(BlockBody(transactions: transactions, uncles: uncles))
|
|
|
|
except RlpError as e:
|
|
|
|
err("RLP decoding failed: " & e.msg)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func fromReceipts(
|
|
|
|
T: type seq[Receipt], receipts: ReceiptsSSZ): Result[T, string] =
|
|
|
|
## Get the full decoded seq[Receipt] from the SSZ-decoded `Receipts`.
|
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
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
## Calls to encode Block types to the SSZ types.
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func fromBlockBody(T: type BlockBodySSZ, body: BlockBody): T =
|
|
|
|
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
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
BlockBodySSZ(transactions: transactions, uncles: uncles)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func fromReceipts(T: type ReceiptsSSZ, receipts: seq[Receipt]): T =
|
|
|
|
var receiptsSSZ: ReceiptsSSZ
|
|
|
|
for receipt in receipts:
|
|
|
|
discard receiptsSSZ.add(ReceiptByteList(rlp.encode(receipt)))
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
receiptsSSZ
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func encode*(blockBody: BlockBody): seq[byte] =
|
|
|
|
let portalBlockBody = BlockBodySSZ.fromBlockBody(blockBody)
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
SSZ.encode(portalBlockBody)
|
|
|
|
|
|
|
|
func encode*(receipts: seq[Receipt]): seq[byte] =
|
|
|
|
let portalReceipts = ReceiptsSSZ.fromReceipts(receipts)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
proc calcRootHash(items: Transactions | ReceiptsSSZ): Hash256 =
|
|
|
|
var tr = initHexaryTrie(newMemoryDB())
|
|
|
|
for i, t in items:
|
|
|
|
try:
|
|
|
|
tr.put(rlp.encode(i), t.asSeq())
|
|
|
|
except RlpError as e:
|
|
|
|
# TODO: Investigate this RlpError as it doesn't sound like this is
|
|
|
|
# something that can actually occur.
|
|
|
|
raiseAssert(e.msg)
|
|
|
|
|
|
|
|
return tr.rootHash
|
|
|
|
|
|
|
|
template calcTxsRoot*(transactions: Transactions): Hash256 =
|
|
|
|
calcRootHash(transactions)
|
|
|
|
|
|
|
|
template calcReceiptsRoot*(receipts: ReceiptsSSZ): Hash256 =
|
|
|
|
calcRootHash(receipts)
|
|
|
|
|
|
|
|
func validateBlockHeaderBytes*(
|
|
|
|
bytes: openArray[byte], hash: BlockHash): Result[BlockHeader, string] =
|
|
|
|
|
|
|
|
let header = ? decodeRlp(bytes, BlockHeader)
|
|
|
|
|
2022-11-26 14:59:19 +00:00
|
|
|
if header.withdrawalsRoot.isSome:
|
|
|
|
return err("Withdrawals not yet implemented")
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
if not (header.blockHash() == hash):
|
|
|
|
err("Block header hash does not match")
|
|
|
|
else:
|
|
|
|
ok(header)
|
|
|
|
|
|
|
|
proc validateBlockBody(
|
|
|
|
body: BlockBodySSZ, txsRoot, ommersHash: KeccakHash):
|
|
|
|
Result[void, string] =
|
|
|
|
## Validate the block body against the txRoot amd ommersHash from the header.
|
2022-09-03 18:15:35 +00:00
|
|
|
let calculatedOmmersHash = keccakHash(body.uncles.asSeq())
|
2022-07-01 19:51:51 +00:00
|
|
|
if calculatedOmmersHash != ommersHash:
|
|
|
|
return err("Invalid ommers hash")
|
|
|
|
|
|
|
|
let calculatedTxsRoot = calcTxsRoot(body.transactions)
|
|
|
|
if calculatedTxsRoot != txsRoot:
|
|
|
|
return err("Invalid transactions root")
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc validateBlockBodyBytes*(
|
|
|
|
bytes: openArray[byte], txRoot, ommersHash: KeccakHash):
|
|
|
|
Result[BlockBody, string] =
|
|
|
|
## Fully decode the SSZ Block Body and validate it against the header.
|
2022-11-04 08:27:01 +00:00
|
|
|
let body = ? decodeSsz(bytes, BlockBodySSZ)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
|
|
|
? validateBlockBody(body, txRoot, ommersHash)
|
|
|
|
|
|
|
|
BlockBody.fromPortalBlockBody(body)
|
|
|
|
|
|
|
|
proc validateReceipts(
|
|
|
|
receipts: ReceiptsSSZ, receiptsRoot: KeccakHash): Result[void, string] =
|
|
|
|
let calculatedReceiptsRoot = calcReceiptsRoot(receipts)
|
|
|
|
|
|
|
|
if calculatedReceiptsRoot != receiptsRoot:
|
|
|
|
return err("Unexpected receipt root")
|
|
|
|
else:
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
proc validateReceiptsBytes*(
|
|
|
|
bytes: openArray[byte],
|
|
|
|
receiptsRoot: KeccakHash): Result[seq[Receipt], string] =
|
|
|
|
## Fully decode the SSZ Block Body and validate it against the header.
|
2022-11-04 08:27:01 +00:00
|
|
|
let receipts = ? decodeSsz(bytes, ReceiptsSSZ)
|
2022-07-01 19:51:51 +00:00
|
|
|
|
|
|
|
? validateReceipts(receipts, receiptsRoot)
|
|
|
|
|
|
|
|
seq[Receipt].fromReceipts(receipts)
|
|
|
|
|
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-09-10 19:00:27 +00:00
|
|
|
proc get(db: ContentDB, T: type BlockHeader, contentId: ContentId): Option[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)
|
|
|
|
except SszError as e:
|
|
|
|
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:
|
|
|
|
some(res.get())
|
|
|
|
else:
|
|
|
|
none(T)
|
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
proc get(db: ContentDB, T: type BlockBody, contentId: ContentId): Option[T] =
|
2022-07-01 19:51:51 +00:00
|
|
|
let contentFromDB = db.getSszDecoded(contentId, BlockBodySSZ)
|
|
|
|
if contentFromDB.isSome():
|
|
|
|
let res = T.fromPortalBlockBody(contentFromDB.get())
|
|
|
|
if res.isErr():
|
|
|
|
raiseAssert(res.error)
|
|
|
|
else:
|
|
|
|
some(res.get())
|
|
|
|
else:
|
|
|
|
none(T)
|
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
proc get(db: ContentDB, T: type seq[Receipt], contentId: ContentId): Option[T] =
|
2022-07-01 19:51:51 +00:00
|
|
|
let contentFromDB = db.getSszDecoded(contentId, ReceiptsSSZ)
|
|
|
|
if contentFromDB.isSome():
|
|
|
|
let res = T.fromReceipts(contentFromDB.get())
|
|
|
|
if res.isErr():
|
|
|
|
raiseAssert(res.error)
|
|
|
|
else:
|
|
|
|
some(res.get())
|
|
|
|
else:
|
|
|
|
none(T)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
proc get(
|
2022-09-10 19:00:27 +00:00
|
|
|
db: ContentDB, T: type EpochAccumulator, contentId: ContentId): Option[T] =
|
2022-08-01 19:00:21 +00:00
|
|
|
db.getSszDecoded(contentId, T)
|
|
|
|
|
2022-04-01 16:01:50 +00:00
|
|
|
proc getContentFromDb(
|
2022-07-11 14:29:16 +00:00
|
|
|
n: HistoryNetwork, T: type, contentId: ContentId): Option[T] =
|
|
|
|
if n.portalProtocol.inRange(contentId):
|
|
|
|
n.contentDB.get(T, contentId)
|
2022-03-02 14:29:12 +00:00
|
|
|
else:
|
2022-07-01 19:51:51 +00:00
|
|
|
none(T)
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
|
2022-08-01 19:00:21 +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(
|
|
|
|
n: HistoryNetwork, header: BlockHeader, proof: BlockHeaderProof):
|
|
|
|
Result[void, string] =
|
|
|
|
verifyHeader(n.accumulator, header, proof)
|
|
|
|
|
|
|
|
proc getVerifiedBlockHeader*(
|
|
|
|
n: HistoryNetwork, hash: BlockHash):
|
|
|
|
Future[Option[BlockHeader]] {.async.} =
|
|
|
|
let (keyEncoded, contentId) =
|
|
|
|
getEncodedKeyForContent(blockHeaderWithProof, hash)
|
|
|
|
|
|
|
|
# Note: This still requests a BlockHeaderWithProof from the database, as that
|
|
|
|
# is what is stored. But the proof doesn't need to be checked as everthing
|
|
|
|
# should get checked before storing.
|
|
|
|
let headerFromDb = n.getContentFromDb(BlockHeader, contentId)
|
|
|
|
|
|
|
|
if headerFromDb.isSome():
|
|
|
|
info "Fetched block header from database", hash, contentKey = keyEncoded
|
|
|
|
return headerFromDb
|
|
|
|
|
|
|
|
for i in 0..<requestRetries:
|
|
|
|
let headerContentLookup =
|
|
|
|
await n.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
if headerContentLookup.isNone():
|
|
|
|
warn "Failed fetching block header with proof from the network",
|
|
|
|
hash, contentKey = keyEncoded
|
|
|
|
return none(BlockHeader)
|
|
|
|
|
|
|
|
let headerContent = headerContentLookup.unsafeGet()
|
|
|
|
|
|
|
|
let headerWithProofRes = decodeSsz(headerContent.content, BlockHeaderWithProof)
|
|
|
|
if headerWithProofRes.isErr():
|
|
|
|
warn "Failed decoding header with proof", err = headerWithProofRes.error
|
|
|
|
return none(BlockHeader)
|
|
|
|
|
|
|
|
let headerWithProof = headerWithProofRes.get()
|
|
|
|
|
|
|
|
let res = validateBlockHeaderBytes(headerWithProof.header.asSeq(), hash)
|
|
|
|
if res.isOk():
|
|
|
|
let isCanonical = n.verifyHeader(res.get(), headerWithProof.proof)
|
|
|
|
|
|
|
|
if isCanonical.isOk():
|
|
|
|
info "Fetched block header from the network", hash, contentKey = keyEncoded
|
|
|
|
# Content is valid, it can be propagated to interested peers
|
|
|
|
n.portalProtocol.triggerPoke(
|
|
|
|
headerContent.nodesInterestedInContent,
|
|
|
|
keyEncoded,
|
|
|
|
headerContent.content
|
|
|
|
)
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(keyEncoded, contentId, headerContent.content)
|
2022-11-04 08:27:01 +00:00
|
|
|
|
|
|
|
return some(res.get())
|
|
|
|
else:
|
|
|
|
warn "Validation of block header failed", err = res.error, hash, contentKey = keyEncoded
|
|
|
|
|
|
|
|
# Headers were requested `requestRetries` times and all failed on validation
|
|
|
|
return none(BlockHeader)
|
|
|
|
|
|
|
|
# TODO: To be deprecated or not? Should there be the case for requesting a
|
|
|
|
# block header without proofs?
|
2022-04-01 16:01:50 +00:00
|
|
|
proc getBlockHeader*(
|
2022-09-28 07:09:38 +00:00
|
|
|
n: HistoryNetwork, hash: BlockHash):
|
2022-04-01 16:01:50 +00:00
|
|
|
Future[Option[BlockHeader]] {.async.} =
|
2022-07-01 19:51:51 +00:00
|
|
|
let (keyEncoded, contentId) =
|
2022-09-28 07:09:38 +00:00
|
|
|
getEncodedKeyForContent(blockHeader, hash)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
let headerFromDb = n.getContentFromDb(BlockHeader, contentId)
|
2022-07-01 19:51:51 +00:00
|
|
|
if headerFromDb.isSome():
|
2022-07-29 12:24:07 +00:00
|
|
|
info "Fetched block header from database", hash, contentKey = keyEncoded
|
2022-07-01 19:51:51 +00:00
|
|
|
return headerFromDb
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
for i in 0..<requestRetries:
|
|
|
|
let headerContentLookup =
|
2022-07-11 14:29:16 +00:00
|
|
|
await n.portalProtocol.contentLookup(keyEncoded, contentId)
|
2022-07-05 12:42:55 +00:00
|
|
|
if headerContentLookup.isNone():
|
2022-07-29 12:24:07 +00:00
|
|
|
warn "Failed fetching block header from the network", hash, contentKey = keyEncoded
|
2022-07-05 12:42:55 +00:00
|
|
|
return none(BlockHeader)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
let headerContent = headerContentLookup.unsafeGet()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
let res = validateBlockHeaderBytes(headerContent.content, hash)
|
|
|
|
if res.isOk():
|
2022-07-29 12:24:07 +00:00
|
|
|
info "Fetched block header from the network", hash, contentKey = keyEncoded
|
2022-07-05 12:42:55 +00:00
|
|
|
# Content is valid we can propagate it to interested peers
|
2022-07-11 14:29:16 +00:00
|
|
|
n.portalProtocol.triggerPoke(
|
2022-07-05 12:42:55 +00:00
|
|
|
headerContent.nodesInterestedInContent,
|
|
|
|
keyEncoded,
|
|
|
|
headerContent.content
|
|
|
|
)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(keyEncoded, contentId, headerContent.content)
|
2022-04-11 09:25:36 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
return some(res.get())
|
|
|
|
else:
|
2022-07-29 12:24:07 +00:00
|
|
|
warn "Validation of block header failed", err = res.error, hash, contentKey = keyEncoded
|
2022-07-05 12:42:55 +00:00
|
|
|
|
|
|
|
# Headers were requested `requestRetries` times and all failed on validation
|
|
|
|
return none(BlockHeader)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-06-29 15:44:08 +00:00
|
|
|
proc getBlockBody*(
|
2022-09-28 07:09:38 +00:00
|
|
|
n: HistoryNetwork, hash: BlockHash, header: BlockHeader):
|
2022-07-05 12:42:55 +00:00
|
|
|
Future[Option[BlockBody]] {.async.} =
|
2022-07-20 10:46:42 +00:00
|
|
|
|
|
|
|
# Got header with empty body, no need to make any db calls or network requests
|
2022-09-03 18:15:35 +00:00
|
|
|
if header.txRoot == EMPTY_ROOT_HASH and header.ommersHash == EMPTY_UNCLE_HASH:
|
2022-07-20 10:46:42 +00:00
|
|
|
return some(BlockBody(transactions: @[], uncles: @[]))
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let
|
2022-09-28 07:09:38 +00:00
|
|
|
(keyEncoded, contentId) = getEncodedKeyForContent(blockBody, hash)
|
2022-07-11 14:29:16 +00:00
|
|
|
bodyFromDb = n.getContentFromDb(BlockBody, contentId)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
if bodyFromDb.isSome():
|
2022-07-29 12:24:07 +00:00
|
|
|
info "Fetched block body from database", hash, contentKey = keyEncoded
|
2022-07-05 12:42:55 +00:00
|
|
|
return bodyFromDb
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
for i in 0..<requestRetries:
|
|
|
|
let bodyContentLookup =
|
2022-07-11 14:29:16 +00:00
|
|
|
await n.portalProtocol.contentLookup(keyEncoded, contentId)
|
2022-07-20 10:46:42 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
if bodyContentLookup.isNone():
|
2022-07-29 12:24:07 +00:00
|
|
|
warn "Failed fetching block body from the network", hash, contentKey = keyEncoded
|
2022-07-20 10:46:42 +00:00
|
|
|
return none(BlockBody)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
let bodyContent = bodyContentLookup.unsafeGet()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
let res = validateBlockBodyBytes(
|
|
|
|
bodyContent.content, header.txRoot, header.ommersHash)
|
|
|
|
if res.isOk():
|
2022-07-29 12:24:07 +00:00
|
|
|
info "Fetched block body from the network", hash, contentKey = keyEncoded
|
2022-04-15 14:10:05 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
# body is valid, propagate it to interested peers
|
2022-07-11 14:29:16 +00:00
|
|
|
n.portalProtocol.triggerPoke(
|
2022-07-05 12:42:55 +00:00
|
|
|
bodyContent.nodesInterestedInContent,
|
|
|
|
keyEncoded,
|
|
|
|
bodyContent.content
|
|
|
|
)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(keyEncoded, contentId, bodyContent.content)
|
2022-04-11 09:25:36 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
return some(res.get())
|
|
|
|
else:
|
2022-07-29 12:24:07 +00:00
|
|
|
warn "Validation of block body failed", err = res.error, hash, contentKey = keyEncoded
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
return none(BlockBody)
|
2022-06-29 15:44:08 +00:00
|
|
|
|
|
|
|
proc getBlock*(
|
2022-09-28 07:09:38 +00:00
|
|
|
n: HistoryNetwork, hash: BlockHash):
|
2022-06-29 15:44:08 +00:00
|
|
|
Future[Option[Block]] {.async.} =
|
2022-07-29 12:24:07 +00:00
|
|
|
debug "Trying to retrieve block with hash", hash
|
|
|
|
|
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.
|
|
|
|
let headerOpt = await n.getVerifiedBlockHeader(hash)
|
2022-07-01 19:51:51 +00:00
|
|
|
if headerOpt.isNone():
|
2022-07-29 12:24:07 +00:00
|
|
|
warn "Failed to get header when getting block with hash", hash
|
2022-07-01 19:51:51 +00:00
|
|
|
# Cannot validate block without header.
|
2022-06-29 15:44:08 +00:00
|
|
|
return none(Block)
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let header = headerOpt.unsafeGet()
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2022-09-28 07:09:38 +00:00
|
|
|
let bodyOpt = await n.getBlockBody(hash, header)
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
if bodyOpt.isNone():
|
2022-07-29 12:24:07 +00:00
|
|
|
warn "Failed to get body when gettin block with hash", hash
|
2022-06-29 15:44:08 +00:00
|
|
|
return none(Block)
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let body = bodyOpt.unsafeGet()
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
return some((header, body))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-06-10 10:24:53 +00:00
|
|
|
proc getReceipts*(
|
2022-07-11 14:29:16 +00:00
|
|
|
n: HistoryNetwork,
|
2022-06-10 10:24:53 +00:00
|
|
|
hash: BlockHash,
|
2022-06-29 15:44:08 +00:00
|
|
|
header: BlockHeader): Future[Option[seq[Receipt]]] {.async.} =
|
2022-09-03 18:15:35 +00:00
|
|
|
if header.receiptRoot == EMPTY_ROOT_HASH:
|
2022-07-05 12:42:55 +00:00
|
|
|
# Short path for empty receipts indicated by receipts root
|
2022-06-10 10:24:53 +00:00
|
|
|
return some(newSeq[Receipt]())
|
|
|
|
|
2022-09-28 07:09:38 +00:00
|
|
|
let (keyEncoded, contentId) = getEncodedKeyForContent(receipts, hash)
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
let receiptsFromDb = n.getContentFromDb(seq[Receipt], contentId)
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
if receiptsFromDb.isSome():
|
2022-06-10 10:24:53 +00:00
|
|
|
info "Fetched receipts from database", hash
|
2022-07-05 12:42:55 +00:00
|
|
|
return receiptsFromDb
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
for i in 0..<requestRetries:
|
|
|
|
let receiptsContentLookup =
|
2022-07-11 14:29:16 +00:00
|
|
|
await n.portalProtocol.contentLookup(keyEncoded, contentId)
|
2022-07-05 12:42:55 +00:00
|
|
|
if receiptsContentLookup.isNone():
|
2022-07-29 12:24:07 +00:00
|
|
|
warn "Failed fetching receipts from the network", hash, contentKey = keyEncoded
|
2022-07-05 12:42:55 +00:00
|
|
|
return none(seq[Receipt])
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
let receiptsContent = receiptsContentLookup.unsafeGet()
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
let res = validateReceiptsBytes(receiptsContent.content, header.receiptRoot)
|
|
|
|
if res.isOk():
|
2022-07-29 12:24:07 +00:00
|
|
|
info "Fetched receipts from the network", hash, contentKey = keyEncoded
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
let receipts = res.get()
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
# receipts are valid, propagate it to interested peers
|
2022-07-11 14:29:16 +00:00
|
|
|
n.portalProtocol.triggerPoke(
|
2022-07-05 12:42:55 +00:00
|
|
|
receiptsContent.nodesInterestedInContent,
|
|
|
|
keyEncoded,
|
|
|
|
receiptsContent.content
|
|
|
|
)
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(keyEncoded, contentId, receiptsContent.content)
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
return some(res.get())
|
|
|
|
else:
|
2022-07-29 12:24:07 +00:00
|
|
|
warn "Validation of receipts failed", err = res.error, hash, contentKey = keyEncoded
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
return none(seq[Receipt])
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
proc getEpochAccumulator(
|
|
|
|
n: HistoryNetwork, epochHash: Digest):
|
|
|
|
Future[Option[EpochAccumulator]] {.async.} =
|
|
|
|
let
|
|
|
|
contentKey = ContentKey(
|
|
|
|
contentType: epochAccumulator,
|
|
|
|
epochAccumulatorKey: EpochAccumulatorKey(epochHash: epochHash))
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
keyEncoded = encode(contentKey)
|
|
|
|
contentId = toContentId(keyEncoded)
|
|
|
|
|
|
|
|
accumulatorFromDb = n.getContentFromDb(EpochAccumulator, contentId)
|
|
|
|
|
|
|
|
if accumulatorFromDb.isSome():
|
|
|
|
info "Fetched epoch accumulator from database", epochHash
|
|
|
|
return accumulatorFromDb
|
|
|
|
|
|
|
|
for i in 0..<requestRetries:
|
|
|
|
let contentLookup =
|
|
|
|
await n.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
if contentLookup.isNone():
|
|
|
|
warn "Failed fetching epoch accumulator from the network", epochHash
|
|
|
|
return none(EpochAccumulator)
|
|
|
|
|
|
|
|
let accumulatorContent = contentLookup.unsafeGet()
|
|
|
|
|
|
|
|
let epochAccumulator =
|
|
|
|
try:
|
|
|
|
SSZ.decode(accumulatorContent.content, EpochAccumulator)
|
|
|
|
except SszError:
|
|
|
|
continue
|
|
|
|
# return none(EpochAccumulator)
|
|
|
|
|
|
|
|
let hash = hash_tree_root(epochAccumulator)
|
|
|
|
if hash == epochHash:
|
|
|
|
info "Fetched epoch accumulator from the network", epochHash
|
|
|
|
|
|
|
|
n.portalProtocol.triggerPoke(
|
|
|
|
accumulatorContent.nodesInterestedInContent,
|
|
|
|
keyEncoded,
|
|
|
|
accumulatorContent.content
|
|
|
|
)
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(keyEncoded, contentId, accumulatorContent.content)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
|
|
|
return some(epochAccumulator)
|
|
|
|
else:
|
|
|
|
warn "Validation of epoch accumulator failed",
|
|
|
|
hash, expectedHash = epochHash
|
|
|
|
|
|
|
|
return none(EpochAccumulator)
|
|
|
|
|
2022-08-04 06:34:53 +00:00
|
|
|
proc getBlock*(
|
2022-09-28 07:09:38 +00:00
|
|
|
n: HistoryNetwork, bn: UInt256):
|
2022-08-04 06:34:53 +00:00
|
|
|
Future[Result[Option[Block], string]] {.async.} =
|
2022-10-17 18:38:51 +00:00
|
|
|
let epochDataRes = n.accumulator.getBlockEpochDataForBlockNumber(bn)
|
2022-10-10 10:59:55 +00:00
|
|
|
if epochDataRes.isOk():
|
|
|
|
let
|
|
|
|
epochData = epochDataRes.get()
|
|
|
|
digest = Digest(data: epochData.epochHash)
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
epochOpt = await n.getEpochAccumulator(digest)
|
2022-08-04 06:34:53 +00:00
|
|
|
if epochOpt.isNone():
|
|
|
|
return err("Cannot retrieve epoch accumulator for given block number")
|
|
|
|
|
|
|
|
let
|
|
|
|
epoch = epochOpt.unsafeGet()
|
2022-10-10 10:59:55 +00:00
|
|
|
blockHash = epoch[epochData.blockRelativeIndex].blockHash
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-09-28 07:09:38 +00:00
|
|
|
let maybeBlock = await n.getBlock(blockHash)
|
2022-08-04 06:34:53 +00:00
|
|
|
|
|
|
|
return ok(maybeBlock)
|
2022-10-10 10:59:55 +00:00
|
|
|
else:
|
|
|
|
return err(epochDataRes.error)
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
proc validateContent(
|
|
|
|
n: HistoryNetwork, content: seq[byte], contentKey: ByteList):
|
|
|
|
Future[bool] {.async.} =
|
2022-04-11 17:42:38 +00:00
|
|
|
let keyOpt = contentKey.decode()
|
|
|
|
|
|
|
|
if keyOpt.isNone():
|
|
|
|
return false
|
|
|
|
|
|
|
|
let key = keyOpt.get()
|
|
|
|
|
|
|
|
case key.contentType:
|
|
|
|
of blockHeader:
|
2022-11-04 08:27:01 +00:00
|
|
|
# Note: For now we still accept regular block header type to remain
|
|
|
|
# compatible with the current specs. However, a verification is done by
|
|
|
|
# basically requesting the header with proofs from somewhere else.
|
|
|
|
# This all doesn't make much sense aside from compatibility and should
|
|
|
|
# eventually be removed.
|
2022-08-01 19:00:21 +00:00
|
|
|
let validateResult =
|
|
|
|
validateBlockHeaderBytes(content, key.blockHeaderKey.blockHash)
|
|
|
|
if validateResult.isErr():
|
|
|
|
warn "Invalid block header offered", error = validateResult.error
|
|
|
|
return false
|
|
|
|
|
|
|
|
let header = validateResult.get()
|
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
let res = await n.getVerifiedBlockHeader(key.blockHeaderKey.blockHash)
|
|
|
|
if res.isNone():
|
|
|
|
warn "Block header failed canonical verification"
|
2022-08-01 19:00:21 +00:00
|
|
|
return false
|
|
|
|
else:
|
|
|
|
return true
|
2022-07-11 14:29:16 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
of blockBody:
|
|
|
|
let res = await n.getVerifiedBlockHeader(key.blockBodyKey.blockHash)
|
|
|
|
if res.isNone():
|
|
|
|
warn "Block body Failed canonical verification"
|
2022-07-11 14:29:16 +00:00
|
|
|
return false
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
let header = res.get()
|
2022-08-01 19:00:21 +00:00
|
|
|
let validationResult =
|
|
|
|
validateBlockBodyBytes(content, header.txRoot, header.ommersHash)
|
|
|
|
|
|
|
|
if validationResult.isErr():
|
|
|
|
warn "Failed validating block body", error = validationResult.error
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
return true
|
2022-07-11 14:29:16 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
of receipts:
|
|
|
|
let res = await n.getVerifiedBlockHeader(key.receiptsKey.blockHash)
|
|
|
|
if res.isNone():
|
|
|
|
warn "Receipts failed canonical verification"
|
2022-08-01 19:00:21 +00:00
|
|
|
return false
|
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
let header = res.get()
|
2022-08-01 19:00:21 +00:00
|
|
|
let validationResult =
|
|
|
|
validateReceiptsBytes(content, header.receiptRoot)
|
|
|
|
|
|
|
|
if validationResult.isErr():
|
|
|
|
warn "Failed validating receipts", error = validationResult.error
|
2022-07-11 14:29:16 +00:00
|
|
|
return false
|
2022-08-01 19:00:21 +00:00
|
|
|
else:
|
|
|
|
return true
|
2022-11-04 08:27:01 +00:00
|
|
|
|
2022-06-14 21:38:34 +00:00
|
|
|
of epochAccumulator:
|
2022-08-01 19:00:21 +00:00
|
|
|
# Check first if epochHash is part of master accumulator
|
|
|
|
let epochHash = key.epochAccumulatorKey.epochHash
|
2022-10-17 18:38:51 +00:00
|
|
|
if not n.accumulator.historicalEpochs.contains(epochHash.data):
|
|
|
|
warn "Offered epoch accumulator is not part of master accumulator",
|
|
|
|
epochHash
|
2022-08-01 19:00:21 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
let epochAccumulator =
|
|
|
|
try:
|
|
|
|
SSZ.decode(content, EpochAccumulator)
|
|
|
|
except SszError:
|
|
|
|
warn "Failed decoding epoch accumulator"
|
|
|
|
return false
|
2022-10-17 18:38:51 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
# Next check the hash tree root, as this is probably more expensive
|
|
|
|
let hash = hash_tree_root(epochAccumulator)
|
|
|
|
if hash != epochHash:
|
|
|
|
warn "Epoch accumulator has invalid root hash"
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
return true
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
of blockHeaderWithProof:
|
|
|
|
let headerWithProofRes = decodeSsz(content, BlockHeaderWithProof)
|
|
|
|
if headerWithProofRes.isErr():
|
|
|
|
warn "Failed decoding header with proof", err = headerWithProofRes.error
|
|
|
|
return false
|
|
|
|
|
|
|
|
let headerWithProof = headerWithProofRes.get()
|
|
|
|
|
|
|
|
let validateResult = validateBlockHeaderBytes(
|
|
|
|
headerWithProof.header.asSeq(), key.blockHeaderWithProofKey.blockHash)
|
|
|
|
if validateResult.isErr():
|
|
|
|
warn "Invalid block header offered", error = validateResult.error
|
|
|
|
return false
|
|
|
|
|
|
|
|
let header = validateResult.get()
|
|
|
|
|
|
|
|
let isCanonical = n.verifyHeader(header, headerWithProof.proof)
|
|
|
|
if isCanonical.isErr():
|
|
|
|
warn "Failed on check if header is part of canonical chain",
|
|
|
|
error = isCanonical.error
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
return true
|
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc new*(
|
|
|
|
T: type HistoryNetwork,
|
|
|
|
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,
|
2022-01-18 08:01:22 +00:00
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig): T =
|
2022-10-17 18:38:51 +00:00
|
|
|
let
|
|
|
|
contentQueue = newAsyncQueue[(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(
|
2022-11-08 17:31:45 +00:00
|
|
|
baseProtocol, historyProtocolId,
|
|
|
|
toContentIdHandler, createGetHandler(contentDB), stream, bootstrapRecords,
|
2022-10-17 18:38:51 +00:00
|
|
|
config = portalConfig)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
portalProtocol.dbPut = createStoreHandler(contentDB, portalConfig.radiusConfig, portalProtocol)
|
|
|
|
|
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,
|
|
|
|
accumulator: accumulator
|
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(
|
|
|
|
n: HistoryNetwork,
|
|
|
|
contentKeys: ContentKeysList,
|
|
|
|
contentItems: seq[seq[byte]]): Future[bool] {.async.} =
|
|
|
|
# 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):
|
|
|
|
let contentIdOpt = n.portalProtocol.toContentId(contentKey)
|
|
|
|
if contentIdOpt.isNone():
|
|
|
|
error "Received offered content with invalid content key", contentKey
|
|
|
|
return false
|
|
|
|
|
|
|
|
let contentId = contentIdOpt.get()
|
|
|
|
|
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
|
|
|
|
|
2022-10-11 10:10:54 +00:00
|
|
|
proc neighborhoodGossipDiscardPeers(
|
|
|
|
p: PortalProtocol,
|
|
|
|
contentKeys: ContentKeysList,
|
|
|
|
content: seq[seq[byte]]): Future[void] {.async.} =
|
|
|
|
discard await p.neighborhoodGossip(contentKeys, content)
|
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
proc processContentLoop(n: HistoryNetwork) {.async.} =
|
|
|
|
try:
|
|
|
|
while true:
|
|
|
|
let (contentKeys, contentItems) =
|
2022-08-17 07:32:06 +00:00
|
|
|
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(
|
|
|
|
contentKeys, contentItems
|
|
|
|
)
|
2022-07-11 14:29:16 +00:00
|
|
|
|
|
|
|
except CancelledError:
|
|
|
|
trace "processContentLoop canceled"
|
|
|
|
|
|
|
|
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)
|
2022-07-11 14:29:16 +00:00
|
|
|
n.portalProtocol.start()
|
|
|
|
|
|
|
|
n.processContentLoop = processContentLoop(n)
|
|
|
|
|
|
|
|
proc stop*(n: HistoryNetwork) =
|
|
|
|
n.portalProtocol.stop()
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
if not n.processContentLoop.isNil:
|
|
|
|
n.processContentLoop.cancel()
|