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-05-22 20:44:15 +00:00
|
|
|
std/options,
|
2022-07-01 19:51:51 +00:00
|
|
|
stew/results, chronos, chronicles, nimcrypto/[keccak, hash],
|
|
|
|
eth/[common/eth_types, 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-07-01 19:51:51 +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"
|
|
|
|
|
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
|
|
|
|
|
|
|
|
Block* = (BlockHeader, BlockBody)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
func setStreamTransport*(n: HistoryNetwork, transport: UtpDiscv5Protocol) =
|
|
|
|
setTransport(n.portalProtocol.stream, transport)
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func toContentIdHandler(contentKey: ByteList): Option[ContentId] =
|
2022-01-06 08:06:05 +00:00
|
|
|
some(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(
|
|
|
|
cType: ContentType, chainId: uint16, hash: BlockHash):
|
|
|
|
(ByteList, ContentId) =
|
2022-06-14 21:38:34 +00:00
|
|
|
let contentKeyType = BlockKey(chainId: chainId, 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")
|
|
|
|
of masterAccumulator:
|
|
|
|
raiseAssert("Not implemented")
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
return encodeKey(contentKey)
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func decodeRlp*(bytes: openArray[byte], T: type): Result[T, string] =
|
2022-03-02 14:29:12 +00:00
|
|
|
try:
|
2022-07-01 19:51:51 +00:00
|
|
|
ok(rlp.decode(bytes, 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-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)
|
|
|
|
|
|
|
|
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.
|
|
|
|
let calculatedOmmersHash = keccak256.digest(body.uncles.asSeq())
|
|
|
|
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.
|
|
|
|
let body =
|
|
|
|
try:
|
|
|
|
SSZ.decode(bytes, BlockBodySSZ)
|
|
|
|
except SszError as e:
|
|
|
|
return err("Failed to decode block body" & e.msg)
|
|
|
|
|
|
|
|
? 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.
|
|
|
|
let receipts =
|
|
|
|
try:
|
|
|
|
SSZ.decode(bytes, ReceiptsSSZ)
|
|
|
|
except SszError as e:
|
|
|
|
return err("Failed to decode receipts" & e.msg)
|
|
|
|
|
|
|
|
? validateReceipts(receipts, receiptsRoot)
|
|
|
|
|
|
|
|
seq[Receipt].fromReceipts(receipts)
|
|
|
|
|
|
|
|
## ContentDB getters for specific history network types
|
|
|
|
|
|
|
|
proc getSszDecoded(
|
|
|
|
db: ContentDB, contentId: ContentID,
|
|
|
|
T: type auto): Option[T] =
|
|
|
|
let res = db.get(contentId)
|
|
|
|
if res.isSome():
|
|
|
|
try:
|
|
|
|
some(SSZ.decode(res.get(), T))
|
|
|
|
except SszError as e:
|
|
|
|
raiseAssert("Stored data should always be serialized correctly: " & e.msg)
|
|
|
|
else:
|
|
|
|
none(T)
|
|
|
|
|
|
|
|
proc get(db: ContentDB, T: type BlockHeader, contentId: ContentID): Option[T] =
|
|
|
|
let contentFromDB = db.get(contentId)
|
|
|
|
if contentFromDB.isSome():
|
|
|
|
let res = decodeRlp(contentFromDB.get(), T)
|
|
|
|
if res.isErr():
|
|
|
|
raiseAssert(res.error)
|
|
|
|
else:
|
|
|
|
some(res.get())
|
|
|
|
else:
|
|
|
|
none(T)
|
|
|
|
|
|
|
|
proc get(db: ContentDB, T: type BlockBody, contentId: ContentID): Option[T] =
|
|
|
|
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)
|
|
|
|
|
|
|
|
proc get(db: ContentDB, T: type seq[Receipt], contentId: ContentID): Option[T] =
|
|
|
|
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-04-01 16:01:50 +00:00
|
|
|
proc getContentFromDb(
|
|
|
|
h: HistoryNetwork, T: type, contentId: ContentId): Option[T] =
|
2022-03-02 14:29:12 +00:00
|
|
|
if h.portalProtocol.inRange(contentId):
|
2022-07-01 19:51:51 +00:00
|
|
|
h.contentDB.get(T, contentId)
|
2022-03-02 14:29:12 +00:00
|
|
|
else:
|
2022-07-01 19:51:51 +00:00
|
|
|
none(T)
|
|
|
|
|
|
|
|
## 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-04-01 16:01:50 +00:00
|
|
|
proc getBlockHeader*(
|
|
|
|
h: HistoryNetwork, chainId: uint16, hash: BlockHash):
|
|
|
|
Future[Option[BlockHeader]] {.async.} =
|
2022-07-01 19:51:51 +00:00
|
|
|
let (keyEncoded, contentId) =
|
|
|
|
getEncodedKeyForContent(blockHeader, chainId, hash)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let headerFromDb = h.getContentFromDb(BlockHeader, contentId)
|
|
|
|
if headerFromDb.isSome():
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Fetched block header from database", hash
|
2022-07-01 19:51:51 +00:00
|
|
|
return headerFromDb
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let headerContentLookup =
|
|
|
|
await h.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
if headerContentLookup.isNone():
|
2022-04-13 05:56:01 +00:00
|
|
|
warn "Failed fetching block header from the network", hash
|
2022-03-02 14:29:12 +00:00
|
|
|
return none(BlockHeader)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let headerContent = headerContentLookup.unsafeGet()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let res = validateBlockHeaderBytes(headerContent.content, hash)
|
|
|
|
# TODO: If the validation fails, a new request could be done.
|
|
|
|
if res.isOk():
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Fetched block header from the network", hash
|
2022-04-11 09:25:36 +00:00
|
|
|
# Content is valid we can propagate it to interested peers
|
2022-04-06 11:47:23 +00:00
|
|
|
h.portalProtocol.triggerPoke(
|
|
|
|
headerContent.nodesInterestedInContent,
|
2022-04-11 17:42:38 +00:00
|
|
|
keyEncoded,
|
2022-04-11 09:25:36 +00:00
|
|
|
headerContent.content
|
2022-04-06 11:47:23 +00:00
|
|
|
)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-05-12 16:04:37 +00:00
|
|
|
h.portalProtocol.storeContent(contentId, headerContent.content)
|
2022-04-11 09:25:36 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
return some(res.get())
|
|
|
|
else:
|
|
|
|
return none(BlockHeader)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-06-29 15:44:08 +00:00
|
|
|
proc getBlockBody*(
|
|
|
|
h: HistoryNetwork,
|
|
|
|
chainId: uint16,
|
|
|
|
hash: BlockHash,
|
|
|
|
header: BlockHeader):Future[Option[BlockBody]] {.async.} =
|
2022-07-01 19:51:51 +00:00
|
|
|
let
|
|
|
|
(keyEncoded, contentId) = getEncodedKeyForContent(blockBody, chainId, hash)
|
|
|
|
bodyFromDb = h.getContentFromDb(BlockBody, contentId)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
if bodyFromDb.isSome():
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Fetched block body from database", hash
|
2022-07-01 19:51:51 +00:00
|
|
|
return some(bodyFromDb.unsafeGet())
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let bodyContentLookup =
|
|
|
|
await h.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
if bodyContentLookup.isNone():
|
2022-04-13 05:56:01 +00:00
|
|
|
warn "Failed fetching block body from the network", hash
|
2022-06-29 15:44:08 +00:00
|
|
|
return none(BlockBody)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let bodyContent = bodyContentLookup.unsafeGet()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let res = validateBlockBodyBytes(
|
|
|
|
bodyContent.content, header.txRoot, header.ommersHash)
|
|
|
|
if res.isErr():
|
2022-06-29 15:44:08 +00:00
|
|
|
return none(BlockBody)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-04-15 14:10:05 +00:00
|
|
|
info "Fetched block body from the network", hash
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let blockBody = res.get()
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-04-11 09:25:36 +00:00
|
|
|
# body is valid, propagate it to interested peers
|
|
|
|
h.portalProtocol.triggerPoke(
|
|
|
|
bodyContent.nodesInterestedInContent,
|
2022-04-11 17:42:38 +00:00
|
|
|
keyEncoded,
|
2022-04-11 09:25:36 +00:00
|
|
|
bodyContent.content
|
|
|
|
)
|
|
|
|
|
2022-05-12 16:04:37 +00:00
|
|
|
h.portalProtocol.storeContent(contentId, bodyContent.content)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-06-29 15:44:08 +00:00
|
|
|
return some(blockBody)
|
|
|
|
|
|
|
|
proc getBlock*(
|
|
|
|
h: HistoryNetwork, chainId: uint16, hash: BlockHash):
|
|
|
|
Future[Option[Block]] {.async.} =
|
2022-07-01 19:51:51 +00:00
|
|
|
let headerOpt = await h.getBlockHeader(chainId, hash)
|
|
|
|
if headerOpt.isNone():
|
|
|
|
# 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-07-01 19:51:51 +00:00
|
|
|
let bodyOpt = await h.getBlockBody(chainId, hash, header)
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
if bodyOpt.isNone():
|
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
|
|
|
|
|
|
|
return some[Block]((header, body))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-06-10 10:24:53 +00:00
|
|
|
proc getReceipts*(
|
|
|
|
h: HistoryNetwork,
|
2022-06-29 15:44:08 +00:00
|
|
|
chainId: uint16,
|
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-06-10 10:24:53 +00:00
|
|
|
if header.receiptRoot == BLANK_ROOT_HASH:
|
2022-07-01 19:51:51 +00:00
|
|
|
# The header has no receipts, return early with empty receipts
|
2022-06-10 10:24:53 +00:00
|
|
|
return some(newSeq[Receipt]())
|
|
|
|
|
|
|
|
let (keyEncoded, contentId) = getEncodedKeyForContent(receipts, chainId, hash)
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let receiptsFromDb = h.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-01 19:51:51 +00:00
|
|
|
return some(receiptsFromDb.unsafeGet())
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let receiptsContentLookup =
|
|
|
|
await h.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
if receiptsContentLookup.isNone():
|
2022-06-10 10:24:53 +00:00
|
|
|
warn "Failed fetching receipts from the network", hash
|
|
|
|
return none[seq[Receipt]]()
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let receiptsContent = receiptsContentLookup.unsafeGet()
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let res = validateReceiptsBytes(receiptsContent.content, header.receiptRoot)
|
|
|
|
if res.isErr():
|
2022-06-10 10:24:53 +00:00
|
|
|
return none[seq[Receipt]]()
|
|
|
|
|
|
|
|
info "Fetched receipts from the network", hash
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let receipts = res.get()
|
2022-06-10 10:24:53 +00:00
|
|
|
|
|
|
|
# receips are valid, propagate it to interested peers
|
|
|
|
h.portalProtocol.triggerPoke(
|
|
|
|
receiptsContent.nodesInterestedInContent,
|
|
|
|
keyEncoded,
|
|
|
|
receiptsContent.content
|
|
|
|
)
|
|
|
|
|
|
|
|
h.portalProtocol.storeContent(contentId, receiptsContent.content)
|
|
|
|
|
|
|
|
return some(receipts)
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
func validateEpochAccumulator(bytes: openArray[byte]): bool =
|
|
|
|
# For now just validate by checking if de-serialization works
|
|
|
|
try:
|
|
|
|
discard SSZ.decode(bytes, EpochAccumulator)
|
|
|
|
true
|
|
|
|
except SszError:
|
|
|
|
false
|
|
|
|
|
|
|
|
func validateMasterAccumulator(bytes: openArray[byte]): bool =
|
|
|
|
# For now just validate by checking if de-serialization works
|
|
|
|
try:
|
|
|
|
discard SSZ.decode(bytes, Accumulator)
|
|
|
|
true
|
|
|
|
except SszError:
|
|
|
|
false
|
|
|
|
|
2022-04-11 17:42:38 +00:00
|
|
|
proc validateContent(content: openArray[byte], contentKey: ByteList): bool =
|
|
|
|
let keyOpt = contentKey.decode()
|
|
|
|
|
|
|
|
if keyOpt.isNone():
|
|
|
|
return false
|
|
|
|
|
|
|
|
let key = keyOpt.get()
|
|
|
|
|
|
|
|
case key.contentType:
|
|
|
|
of blockHeader:
|
2022-07-01 19:51:51 +00:00
|
|
|
validateBlockHeaderBytes(content, key.blockHeaderKey.blockHash).isOk()
|
2022-04-11 17:42:38 +00:00
|
|
|
of blockBody:
|
|
|
|
true
|
|
|
|
# TODO: Need to get the header from the db or the network for this. Or how
|
|
|
|
# to deal with this?
|
|
|
|
of receipts:
|
|
|
|
true
|
2022-06-14 21:38:34 +00:00
|
|
|
of epochAccumulator:
|
2022-07-01 19:51:51 +00:00
|
|
|
validateEpochAccumulator(content)
|
2022-06-14 21:38:34 +00:00
|
|
|
of masterAccumulator:
|
2022-07-01 19:51:51 +00:00
|
|
|
validateMasterAccumulator(content)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc new*(
|
|
|
|
T: type HistoryNetwork,
|
|
|
|
baseProtocol: protocol.Protocol,
|
|
|
|
contentDB: ContentDB,
|
2022-01-18 08:01:22 +00:00
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig): T =
|
2021-10-09 11:22:03 +00:00
|
|
|
let portalProtocol = PortalProtocol.new(
|
2022-04-11 17:42:38 +00:00
|
|
|
baseProtocol, historyProtocolId, contentDB,
|
2022-05-12 16:04:37 +00:00
|
|
|
toContentIdHandler, validateContent, bootstrapRecords,
|
2022-01-18 08:01:22 +00:00
|
|
|
config = portalConfig)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
return HistoryNetwork(portalProtocol: portalProtocol, contentDB: contentDB)
|
|
|
|
|
|
|
|
proc start*(p: HistoryNetwork) =
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Starting Portal execution history network",
|
2022-03-18 12:06:57 +00:00
|
|
|
protocolId = p.portalProtocol.protocolId
|
2021-10-09 11:22:03 +00:00
|
|
|
p.portalProtocol.start()
|
|
|
|
|
|
|
|
proc stop*(p: HistoryNetwork) =
|
|
|
|
p.portalProtocol.stop()
|