Add getting receipts from history network (#1118)
* Add getting receipts from history network
This commit is contained in:
parent
c1f7503402
commit
b23789d107
|
@ -13,7 +13,7 @@ import
|
||||||
eth/[common/eth_types, rlp],
|
eth/[common/eth_types, rlp],
|
||||||
eth/p2p/discoveryv5/[protocol, enr],
|
eth/p2p/discoveryv5/[protocol, enr],
|
||||||
../../content_db,
|
../../content_db,
|
||||||
../../../nimbus/utils,
|
../../../nimbus/[utils, constants],
|
||||||
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
||||||
./history_content
|
./history_content
|
||||||
|
|
||||||
|
@ -57,47 +57,75 @@ func getEncodedKeyForContent(
|
||||||
|
|
||||||
return encodeKey(contentKey)
|
return encodeKey(contentKey)
|
||||||
|
|
||||||
|
proc getContentFromBytes(bytes: openArray[byte], T: type): Result[T, string] =
|
||||||
|
var rlp = rlpFromBytes(bytes)
|
||||||
|
try:
|
||||||
|
let content = rlp.read(T)
|
||||||
|
ok[T](content)
|
||||||
|
except RlpError as e:
|
||||||
|
err(e.msg)
|
||||||
|
|
||||||
proc validateHeaderBytes*(
|
proc validateHeaderBytes*(
|
||||||
bytes: openArray[byte], hash: BlockHash): Option[BlockHeader] =
|
bytes: openArray[byte], hash: BlockHash): Option[BlockHeader] =
|
||||||
try:
|
|
||||||
var rlp = rlpFromBytes(bytes)
|
|
||||||
|
|
||||||
let blockHeader = rlp.read(BlockHeader)
|
let headerResult = getContentFromBytes(bytes, BlockHeader)
|
||||||
|
|
||||||
if not (blockHeader.blockHash() == hash):
|
if headerResult.isErr():
|
||||||
# TODO: Header with different hash than expected, maybe we should punish
|
error "Failed to decode header ", msg = headerResult.error()
|
||||||
# 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)
|
return none(BlockHeader)
|
||||||
|
|
||||||
proc validateBodyBytes*(
|
let header = headerResult.unsafeGet()
|
||||||
bytes: openArray[byte], txRoot: KeccakHash, ommersHash: KeccakHash):
|
|
||||||
Option[BlockBody] =
|
if not (header.blockHash() == hash):
|
||||||
|
# TODO: Header with different hash than expected, maybe we should punish
|
||||||
|
# peer which sent us this ?
|
||||||
|
return none(BlockHeader)
|
||||||
|
|
||||||
|
return some(header)
|
||||||
|
|
||||||
|
proc validateExpectedBody(
|
||||||
|
bb: BlockBody,
|
||||||
|
txRoot: KeccakHash,
|
||||||
|
ommersHash: KeccakHash): Result[void, string] =
|
||||||
try:
|
try:
|
||||||
var rlp = rlpFromBytes(bytes)
|
let calculatedTxRoot = calcTxRoot(bb.transactions)
|
||||||
|
let calculatedOmmersHash = rlpHash(bb.uncles)
|
||||||
|
|
||||||
let blockBody = rlp.read(BlockBody)
|
if calculatedTxRoot != txRoot:
|
||||||
|
return err("Unexpected transaction root")
|
||||||
|
elif calculatedOmmersHash != ommersHash:
|
||||||
|
return err("Unexpected ommers hash")
|
||||||
|
else:
|
||||||
|
return ok()
|
||||||
|
except RlpError as e:
|
||||||
|
return err(e.msg)
|
||||||
|
|
||||||
let calculatedTxRoot = calcTxRoot(blockBody.transactions)
|
proc validateBodyBytes*(
|
||||||
let calculatedOmmersHash = rlpHash(blockBody.uncles)
|
bytes: openArray[byte],
|
||||||
|
txRoot: KeccakHash,
|
||||||
|
ommersHash: KeccakHash):Option[BlockBody] =
|
||||||
|
|
||||||
if txRoot != calculatedTxRoot or ommersHash != calculatedOmmersHash:
|
let bodyResult = getContentFromBytes(bytes, BlockBody)
|
||||||
# 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)
|
if bodyResult.isErr():
|
||||||
|
error "Failed to decode block body", msg = bodyResult.error()
|
||||||
except RlpError, MalformedRlpError, UnsupportedRlpError, RlpTypeMismatch:
|
|
||||||
# TODO add some logging about failed decoding
|
|
||||||
return none(BlockBody)
|
return none(BlockBody)
|
||||||
|
|
||||||
|
let blockBody = bodyResult.unsafeGet()
|
||||||
|
|
||||||
|
let expectedResult = validateExpectedBody(blockBody, txRoot, ommersHash)
|
||||||
|
|
||||||
|
if expectedResult.isErr():
|
||||||
|
error "Failed to validate if block body matches header",
|
||||||
|
msg = expectedResult.error()
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
proc getContentFromDb(
|
proc getContentFromDb(
|
||||||
h: HistoryNetwork, T: type, contentId: ContentId): Option[T] =
|
h: HistoryNetwork, T: type, contentId: ContentId): Option[T] =
|
||||||
if h.portalProtocol.inRange(contentId):
|
if h.portalProtocol.inRange(contentId):
|
||||||
|
@ -157,7 +185,7 @@ proc getBlock*(
|
||||||
|
|
||||||
if maybeHeader.isNone():
|
if maybeHeader.isNone():
|
||||||
# we do not have header for given hash,so we would not be able to validate
|
# we do not have header for given hash,so we would not be able to validate
|
||||||
# that received body really belong it
|
# that received body really belong to it
|
||||||
return none(Block)
|
return none(Block)
|
||||||
|
|
||||||
let header = maybeHeader.unsafeGet()
|
let header = maybeHeader.unsafeGet()
|
||||||
|
@ -194,11 +222,94 @@ proc getBlock*(
|
||||||
bodyContent.content
|
bodyContent.content
|
||||||
)
|
)
|
||||||
|
|
||||||
# content is in range and valid, put into db
|
|
||||||
h.portalProtocol.storeContent(contentId, bodyContent.content)
|
h.portalProtocol.storeContent(contentId, bodyContent.content)
|
||||||
|
|
||||||
return some[Block]((header, blockBody))
|
return some[Block]((header, blockBody))
|
||||||
|
|
||||||
|
proc validateExpectedReceipts(
|
||||||
|
receipts: seq[Receipt],
|
||||||
|
receiptRoot: KeccakHash): Result[void, string] =
|
||||||
|
try:
|
||||||
|
let calculatedReceiptRoot = calcReceiptRoot(receipts)
|
||||||
|
|
||||||
|
if calculatedReceiptRoot != receiptRoot:
|
||||||
|
return err("Unexpected receipt root")
|
||||||
|
else:
|
||||||
|
return ok()
|
||||||
|
except RlpError as e:
|
||||||
|
return err(e.msg)
|
||||||
|
|
||||||
|
proc validateReceiptsBytes*(
|
||||||
|
bytes: openArray[byte],
|
||||||
|
receiptRoot: KeccakHash): Option[seq[Receipt]] =
|
||||||
|
|
||||||
|
let receiptResult = getContentFromBytes(bytes, seq[Receipt])
|
||||||
|
|
||||||
|
if receiptResult.isErr():
|
||||||
|
error "Failed to decode receipts", msg = receiptResult.error()
|
||||||
|
return none(seq[Receipt])
|
||||||
|
|
||||||
|
let receipts = receiptResult.unsafeGet()
|
||||||
|
|
||||||
|
let expectedReceiptsResult = validateExpectedReceipts(receipts, receiptRoot)
|
||||||
|
|
||||||
|
if expectedReceiptsResult.isErr():
|
||||||
|
error "Failed to validate if receipts matches header",
|
||||||
|
msg = expectedReceiptsResult.error()
|
||||||
|
|
||||||
|
# we got receipts which do not match
|
||||||
|
# header. For now just ignore it, but maybe we should penalize peer
|
||||||
|
# sending us such data?
|
||||||
|
return none(seq[Receipt])
|
||||||
|
|
||||||
|
return some(receipts)
|
||||||
|
|
||||||
|
proc getReceipts*(
|
||||||
|
h: HistoryNetwork,
|
||||||
|
hash: BlockHash,
|
||||||
|
header: BlockHeader,
|
||||||
|
chainId: uint16): Future[Option[seq[Receipt]]] {.async.} =
|
||||||
|
# header does not have any receipts, return early and do not save empty bytes
|
||||||
|
# into the database
|
||||||
|
if header.receiptRoot == BLANK_ROOT_HASH:
|
||||||
|
return some(newSeq[Receipt]())
|
||||||
|
|
||||||
|
let (keyEncoded, contentId) = getEncodedKeyForContent(receipts, chainId, hash)
|
||||||
|
|
||||||
|
let maybeReceiptsFromDb = h.getContentFromDb(seq[Receipt], contentId)
|
||||||
|
|
||||||
|
if maybeReceiptsFromDb.isSome():
|
||||||
|
info "Fetched receipts from database", hash
|
||||||
|
return some(maybeReceiptsFromDb.unsafeGet())
|
||||||
|
|
||||||
|
let maybeReceiptsContent = await h.portalProtocol.contentLookup(keyEncoded, contentId)
|
||||||
|
|
||||||
|
if maybeReceiptsContent.isNone():
|
||||||
|
warn "Failed fetching receipts from the network", hash
|
||||||
|
return none[seq[Receipt]]()
|
||||||
|
|
||||||
|
let receiptsContent = maybeReceiptsContent.unsafeGet()
|
||||||
|
|
||||||
|
let maybeReceipts = validateReceiptsBytes(receiptsContent.content, header.receiptRoot)
|
||||||
|
|
||||||
|
if maybeReceipts.isNone():
|
||||||
|
return none[seq[Receipt]]()
|
||||||
|
|
||||||
|
info "Fetched receipts from the network", hash
|
||||||
|
|
||||||
|
let receipts = maybeReceipts.unsafeGet()
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
proc validateContent(content: openArray[byte], contentKey: ByteList): bool =
|
proc validateContent(content: openArray[byte], contentKey: ByteList): bool =
|
||||||
let keyOpt = contentKey.decode()
|
let keyOpt = contentKey.decode()
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue