2021-10-09 11:22:03 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# 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
|
|
|
|
std/[options, sugar],
|
2021-11-18 11:06:53 +00:00
|
|
|
stew/results, chronos,
|
2022-03-02 14:29:12 +00:00
|
|
|
eth/[common/eth_types, rlp],
|
2022-01-06 08:06:05 +00:00
|
|
|
eth/p2p/discoveryv5/[protocol, enr],
|
2021-10-09 11:22:03 +00:00
|
|
|
../../content_db,
|
2022-03-02 14:29:12 +00:00
|
|
|
../../../nimbus/utils,
|
2022-01-18 08:01:22 +00:00
|
|
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2021-10-09 11:22:03 +00:00
|
|
|
./history_content
|
|
|
|
|
|
|
|
const
|
2021-12-08 10:54:22 +00:00
|
|
|
historyProtocolId* = [byte 0x50, 0x0B]
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
# TODO: Extract common parts from the different networks
|
2022-03-02 14:29:12 +00:00
|
|
|
type
|
|
|
|
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-01-06 08:06:05 +00:00
|
|
|
proc toContentIdHandler(contentKey: ByteList): Option[ContentId] =
|
|
|
|
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))
|
|
|
|
|
|
|
|
func getEncodedKeyForContent(cType: ContentType, chainId: uint16, hash: BlockHash): (ByteList, ContentId) =
|
|
|
|
let contentKeyType = ContentKeyType(chainId: chainId, blockHash: hash)
|
|
|
|
|
|
|
|
let contentKey =
|
|
|
|
case cType
|
|
|
|
of blockHeader:
|
|
|
|
ContentKey(contentType: cType, blockHeaderKey: contentKeyType)
|
|
|
|
of blockBody:
|
|
|
|
ContentKey(contentType: cType, blockBodyKey: contentKeyType)
|
|
|
|
of receipts:
|
|
|
|
ContentKey(contentType: cType, receiptsKey: contentKeyType)
|
|
|
|
|
|
|
|
return encodeKey(contentKey)
|
|
|
|
|
|
|
|
proc validateHeaderBytes*(bytes: seq[byte], hash: BlockHash): Option[BlockHeader] =
|
|
|
|
try:
|
|
|
|
var rlp = rlpFromBytes(bytes)
|
|
|
|
|
|
|
|
let blockHeader = rlp.read(BlockHeader)
|
|
|
|
|
|
|
|
if not (blockHeader.blockHash() == hash):
|
|
|
|
# TODO: Header with different hash than expected maybe we should punish peer which sent
|
|
|
|
# us this ?
|
|
|
|
return none(BlockHeader)
|
|
|
|
|
|
|
|
return some(blockHeader)
|
|
|
|
|
|
|
|
except MalformedRlpError, UnsupportedRlpError, RlpTypeMismatch:
|
|
|
|
# TODO add some logging about failed decoding
|
|
|
|
return none(BlockHeader)
|
|
|
|
|
|
|
|
proc validateBodyBytes*(bytes: seq[byte], txRoot: KeccakHash, ommersHash: KeccakHash): Option[BlockBody] =
|
|
|
|
try:
|
|
|
|
var rlp = rlpFromBytes(bytes)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
let blockBody = rlp.read(BlockBody)
|
|
|
|
|
|
|
|
let calculatedTxRoot = calcTxRoot(blockBody.transactions)
|
|
|
|
let calculatedOmmersHash = rlpHash(blockBody.uncles)
|
|
|
|
|
|
|
|
if txRoot != calculatedTxRoot or ommersHash != calculatedOmmersHash:
|
|
|
|
# we got block body (bundle of transactions and uncles) which do not match
|
|
|
|
# header. For now just ignore it, but maybe we should penalize peer sending us such data?
|
|
|
|
return none(BlockBody)
|
|
|
|
|
|
|
|
return some(blockBody)
|
|
|
|
|
|
|
|
except RlpError, MalformedRlpError, UnsupportedRlpError, RlpTypeMismatch:
|
|
|
|
# TODO add some logging about failed decoding
|
|
|
|
return none(BlockBody)
|
|
|
|
|
|
|
|
proc getContentFromDb(h: HistoryNetwork, T: type, contentId: ContentId): Option[T] =
|
|
|
|
if h.portalProtocol.inRange(contentId):
|
|
|
|
let contentFromDB = h.contentDB.get(contentId)
|
2021-10-09 11:22:03 +00:00
|
|
|
if contentFromDB.isSome():
|
2022-03-02 14:29:12 +00:00
|
|
|
var rlp = rlpFromBytes(contentFromDB.unsafeGet())
|
|
|
|
try:
|
|
|
|
let content = rlp.read(T)
|
|
|
|
return some(content)
|
|
|
|
except CatchableError as e:
|
|
|
|
# Content in db should always have valid formatting, so this should not happen
|
|
|
|
raiseAssert(e.msg)
|
|
|
|
else:
|
|
|
|
return none(T)
|
|
|
|
else:
|
|
|
|
return none(T)
|
|
|
|
|
|
|
|
proc getBlockHeader*(h: HistoryNetwork, chainId: uint16, hash: BlockHash): Future[Option[BlockHeader]] {.async.} =
|
|
|
|
let (keyEncoded, contentId) = getEncodedKeyForContent(blockHeader, chainId, hash)
|
|
|
|
|
|
|
|
let maybeHeaderFromDb = h.getContentFromDb(BlockHeader, contentId)
|
|
|
|
|
|
|
|
if maybeHeaderFromDb.isSome():
|
|
|
|
return maybeHeaderFromDb
|
|
|
|
|
|
|
|
let maybeHeaderContent = await h.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
|
|
|
|
if maybeHeaderContent.isNone():
|
|
|
|
return none(BlockHeader)
|
|
|
|
|
|
|
|
let headerContent = maybeHeaderContent.unsafeGet()
|
|
|
|
|
|
|
|
let maybeHeader = validateHeaderBytes(headerContent, hash)
|
|
|
|
|
|
|
|
# content is in range and valid, put into db
|
|
|
|
if maybeHeader.isSome() and h.portalProtocol.inRange(contentId):
|
|
|
|
h.contentDB.put(contentId, headerContent)
|
|
|
|
|
|
|
|
return maybeHeader
|
|
|
|
|
|
|
|
proc getBlock*(h: HistoryNetwork, chainId: uint16, hash: BlockHash): Future[Option[Block]] {.async.} =
|
|
|
|
let maybeHeader = await h.getBlockHeader(chainId, hash)
|
|
|
|
|
|
|
|
if maybeHeader.isNone():
|
|
|
|
# we do not have header for given hash,so we would not be able to validate
|
|
|
|
# that received body really belong it
|
|
|
|
return none(Block)
|
|
|
|
|
|
|
|
let header = maybeHeader.unsafeGet()
|
|
|
|
|
|
|
|
let (keyEncoded, contentId) = getEncodedKeyForContent(blockBody, chainId, hash)
|
|
|
|
|
|
|
|
let maybeBodyFromDb = h.getContentFromDb(BlockBody, contentId)
|
|
|
|
|
|
|
|
if maybeBodyFromDb.isSome():
|
|
|
|
return some[Block]((header, maybeBodyFromDb.unsafeGet()))
|
|
|
|
|
|
|
|
let maybeBodyContent = await h.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
|
|
|
|
if maybeBodyContent.isNone():
|
|
|
|
return none(Block)
|
|
|
|
|
|
|
|
let bodyContent = maybeBodyContent.unsafeGet()
|
|
|
|
|
|
|
|
let maybeBody = validateBodyBytes(bodyContent, header.txRoot, header.ommersHash)
|
|
|
|
|
|
|
|
if maybeBody.isNone():
|
|
|
|
return none(Block)
|
|
|
|
|
|
|
|
let blockBody = maybeBody.unsafeGet()
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
# content is in range and valid, put into db
|
|
|
|
if h.portalProtocol.inRange(contentId):
|
|
|
|
h.contentDB.put(contentId, bodyContent)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
return some[Block]((header, blockBody))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
# TODO Add getRecepits call
|
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,
|
|
|
|
dataRadius = UInt256.high(),
|
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-01-06 08:06:05 +00:00
|
|
|
baseProtocol, historyProtocolId, contentDB, toContentIdHandler,
|
2022-02-11 13:43:10 +00:00
|
|
|
dataRadius, 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) =
|
|
|
|
p.portalProtocol.start()
|
|
|
|
|
|
|
|
proc stop*(p: HistoryNetwork) =
|
|
|
|
p.portalProtocol.stop()
|