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
|
|
|
|
std/[options, sugar],
|
2022-04-13 05:56:01 +00:00
|
|
|
stew/results, chronos, chronicles,
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
# TODO: Extract common parts from the different networks
|
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-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))
|
|
|
|
|
2022-04-01 16:01:50 +00:00
|
|
|
func getEncodedKeyForContent(
|
|
|
|
cType: ContentType, chainId: uint16, hash: BlockHash):
|
|
|
|
(ByteList, ContentId) =
|
2022-03-02 14:29:12 +00:00
|
|
|
let contentKeyType = ContentKeyType(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-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
return encodeKey(contentKey)
|
|
|
|
|
2022-04-01 16:01:50 +00:00
|
|
|
proc validateHeaderBytes*(
|
2022-04-11 17:42:38 +00:00
|
|
|
bytes: openArray[byte], hash: BlockHash): Option[BlockHeader] =
|
2022-03-02 14:29:12 +00:00
|
|
|
try:
|
|
|
|
var rlp = rlpFromBytes(bytes)
|
|
|
|
|
|
|
|
let blockHeader = rlp.read(BlockHeader)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
if not (blockHeader.blockHash() == hash):
|
2022-04-11 17:42:38 +00:00
|
|
|
# TODO: Header with different hash than expected, maybe we should punish
|
2022-04-01 16:01:50 +00:00
|
|
|
# peer which sent us this ?
|
2022-03-02 14:29:12 +00:00
|
|
|
return none(BlockHeader)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
return some(blockHeader)
|
|
|
|
|
|
|
|
except MalformedRlpError, UnsupportedRlpError, RlpTypeMismatch:
|
|
|
|
# TODO add some logging about failed decoding
|
|
|
|
return none(BlockHeader)
|
|
|
|
|
2022-04-01 16:01:50 +00:00
|
|
|
proc validateBodyBytes*(
|
2022-04-11 17:42:38 +00:00
|
|
|
bytes: openArray[byte], txRoot: KeccakHash, ommersHash: KeccakHash):
|
2022-04-01 16:01:50 +00:00
|
|
|
Option[BlockBody] =
|
2022-03-02 14:29:12 +00:00
|
|
|
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)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
let calculatedTxRoot = calcTxRoot(blockBody.transactions)
|
|
|
|
let calculatedOmmersHash = rlpHash(blockBody.uncles)
|
|
|
|
|
|
|
|
if txRoot != calculatedTxRoot or ommersHash != calculatedOmmersHash:
|
2022-04-01 16:01:50 +00:00
|
|
|
# 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?
|
2022-03-02 14:29:12 +00:00
|
|
|
return none(BlockBody)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
return some(blockBody)
|
|
|
|
|
|
|
|
except RlpError, MalformedRlpError, UnsupportedRlpError, RlpTypeMismatch:
|
|
|
|
# TODO add some logging about failed decoding
|
|
|
|
return none(BlockBody)
|
|
|
|
|
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):
|
|
|
|
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:
|
2022-04-01 16:01:50 +00:00
|
|
|
# Content in db should always have valid formatting, so this should not
|
|
|
|
# happen
|
2022-03-02 14:29:12 +00:00
|
|
|
raiseAssert(e.msg)
|
|
|
|
else:
|
|
|
|
return none(T)
|
|
|
|
else:
|
|
|
|
return none(T)
|
|
|
|
|
2022-04-01 16:01:50 +00:00
|
|
|
proc getBlockHeader*(
|
|
|
|
h: HistoryNetwork, chainId: uint16, hash: BlockHash):
|
|
|
|
Future[Option[BlockHeader]] {.async.} =
|
2022-03-02 14:29:12 +00:00
|
|
|
let (keyEncoded, contentId) = getEncodedKeyForContent(blockHeader, chainId, hash)
|
|
|
|
|
|
|
|
let maybeHeaderFromDb = h.getContentFromDb(BlockHeader, contentId)
|
|
|
|
|
|
|
|
if maybeHeaderFromDb.isSome():
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Fetched block header from database", hash
|
2022-03-02 14:29:12 +00:00
|
|
|
return maybeHeaderFromDb
|
|
|
|
|
|
|
|
let maybeHeaderContent = await h.portalProtocol.contentLookup(keyEncoded, contentId)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
if maybeHeaderContent.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-03-02 14:29:12 +00:00
|
|
|
let headerContent = maybeHeaderContent.unsafeGet()
|
|
|
|
|
2022-04-06 11:47:23 +00:00
|
|
|
let maybeHeader = validateHeaderBytes(headerContent.content, hash)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-04-11 09:25:36 +00:00
|
|
|
if maybeHeader.isSome():
|
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-04-11 09:25:36 +00:00
|
|
|
if h.portalProtocol.inRange(contentId):
|
|
|
|
# content is valid and in our range, save it into our db
|
|
|
|
h.contentDB.put(contentId, headerContent.content)
|
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
return maybeHeader
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-04-01 16:01:50 +00:00
|
|
|
proc getBlock*(
|
|
|
|
h: HistoryNetwork, chainId: uint16, hash: BlockHash):
|
|
|
|
Future[Option[Block]] {.async.} =
|
2022-03-02 14:29:12 +00:00
|
|
|
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)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
let header = maybeHeader.unsafeGet()
|
|
|
|
|
|
|
|
let (keyEncoded, contentId) = getEncodedKeyForContent(blockBody, chainId, hash)
|
|
|
|
|
|
|
|
let maybeBodyFromDb = h.getContentFromDb(BlockBody, contentId)
|
|
|
|
|
|
|
|
if maybeBodyFromDb.isSome():
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Fetched block body from database", hash
|
2022-03-02 14:29:12 +00:00
|
|
|
return some[Block]((header, maybeBodyFromDb.unsafeGet()))
|
|
|
|
|
|
|
|
let maybeBodyContent = await h.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
|
|
|
|
if maybeBodyContent.isNone():
|
2022-04-13 05:56:01 +00:00
|
|
|
warn "Failed fetching block body from the network", hash
|
2022-03-02 14:29:12 +00:00
|
|
|
return none(Block)
|
2022-03-18 12:06:57 +00:00
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
let bodyContent = maybeBodyContent.unsafeGet()
|
|
|
|
|
2022-04-06 11:47:23 +00:00
|
|
|
let maybeBody = validateBodyBytes(bodyContent.content, header.txRoot, header.ommersHash)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
|
|
|
if maybeBody.isNone():
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Fetched block body from the network", hash
|
2022-03-02 14:29:12 +00:00
|
|
|
return none(Block)
|
|
|
|
|
|
|
|
let blockBody = maybeBody.unsafeGet()
|
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-03-02 14:29:12 +00:00
|
|
|
# content is in range and valid, put into db
|
|
|
|
if h.portalProtocol.inRange(contentId):
|
2022-04-06 11:47:23 +00:00
|
|
|
h.contentDB.put(contentId, bodyContent.content)
|
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-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:
|
|
|
|
validateHeaderBytes(content, key.blockHeaderKey.blockHash).isSome()
|
|
|
|
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
|
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-04-11 17:42:38 +00:00
|
|
|
baseProtocol, historyProtocolId, contentDB,
|
|
|
|
toContentIdHandler, validateContent,
|
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) =
|
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()
|