From b23789d107fb513b3d8f0dfba572cad92012fd95 Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Fri, 10 Jun 2022 12:24:53 +0200 Subject: [PATCH] Add getting receipts from history network (#1118) * Add getting receipts from history network --- fluffy/network/history/history_network.nim | 179 +++++++++++++++++---- fluffy/tests/test_history_validation.nim | 45 +++++- 2 files changed, 184 insertions(+), 40 deletions(-) diff --git a/fluffy/network/history/history_network.nim b/fluffy/network/history/history_network.nim index 53787de65..0fc5484c2 100644 --- a/fluffy/network/history/history_network.nim +++ b/fluffy/network/history/history_network.nim @@ -13,7 +13,7 @@ import eth/[common/eth_types, rlp], eth/p2p/discoveryv5/[protocol, enr], ../../content_db, - ../../../nimbus/utils, + ../../../nimbus/[utils, constants], ../wire/[portal_protocol, portal_stream, portal_protocol_config], ./history_content @@ -57,46 +57,74 @@ func getEncodedKeyForContent( 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*( bytes: openArray[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 + + let headerResult = getContentFromBytes(bytes, BlockHeader) + + if headerResult.isErr(): + error "Failed to decode header ", msg = headerResult.error() return none(BlockHeader) + + let header = headerResult.unsafeGet() -proc validateBodyBytes*( - 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: - 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) + +proc validateBodyBytes*( + bytes: openArray[byte], + txRoot: KeccakHash, + ommersHash: KeccakHash):Option[BlockBody] = - let calculatedTxRoot = calcTxRoot(blockBody.transactions) - let calculatedOmmersHash = rlpHash(blockBody.uncles) + let bodyResult = getContentFromBytes(bytes, BlockBody) - 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 + if bodyResult.isErr(): + error "Failed to decode block body", msg = bodyResult.error() 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( h: HistoryNetwork, T: type, contentId: ContentId): Option[T] = @@ -157,7 +185,7 @@ proc getBlock*( 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 + # that received body really belong to it return none(Block) let header = maybeHeader.unsafeGet() @@ -194,11 +222,94 @@ proc getBlock*( bodyContent.content ) - # content is in range and valid, put into db h.portalProtocol.storeContent(contentId, bodyContent.content) 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 = let keyOpt = contentKey.decode() diff --git a/fluffy/tests/test_history_validation.nim b/fluffy/tests/test_history_validation.nim index 98e1f9813..29848152b 100644 --- a/fluffy/tests/test_history_validation.nim +++ b/fluffy/tests/test_history_validation.nim @@ -13,11 +13,16 @@ import eth/[common/eth_types, rlp], ../network/history/history_network -let blockBytes = "0xf910baf90218a07d7701cadd868037f2d6e2898fcaa03e9e892dfac9f85d046b00bcf6bb786ad9a0bf34d0addf61b0e6cd483d6396cadaae438346dc5d8e0a0f3d9135b52a0f11a1944bb96091ee9d802ed039c4d1a5f6216f90f81b01a030e3d6c1021ecb806a1022e07f6e1491a5a2c17f3aaa9efbe1afe2325f9ba241a0fcf98a24af539c43988cec8e157ceec62b6c3ffa7ccc62c41e6b5d63d7af9e24a058656d3e1b463465ded49b02b74ec7dc6700870b5fafee285b49ac5789c39f46b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000860acef5f96bd8830f1b40832fefd883075e308456bd199d98d783010400844765746887676f312e352e31856c696e7578a0ea6f705561212f5523522114b40c77fc735a159eafb0d2c9f134ae33b8e4915088e67c3494e678ba87f90c7ef86e8204ee85746a528800825208944d15e32435180fa21907e0d23a4c0021415fc21f88016345785d8a0000801ba04eeb6205a94672c865a68f7962ab0a4f4d5e152ea0b2ce3fa9821776a6e4d7fea0597fa300bf2dfa6b5631062d119a3b1c407e3a2ca5b2e3c4af4cd597f8aab0e8f907ed8227ac85104c533c00830f4240941194e966965418c7d73a42cceeb254d87586035601b90784d5064ed1000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000005200000000000000000000000000000000000000000000000000000000000000012455552555344000000000000000000000000000000000000000000000000000047425055534400000000000000000000000000000000000000000000000000005553444a505900000000000000000000000000000000000000000000000000005841555553440000000000000000000000000000000000000000000000000000584147555344000000000000000000000000000000000000000000000000000053503530300000000000000000000000000000000000000000000000000000004e415344415100000000000000000000000000000000000000000000000000004141504c00000000000000000000000000000000000000000000000000000000474f4f47000000000000000000000000000000000000000000000000000000004d53465400000000000000000000000000000000000000000000000000000000474d0000000000000000000000000000000000000000000000000000000000004745000000000000000000000000000000000000000000000000000000000000574d54000000000000000000000000000000000000000000000000000000000046000000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000555344545f455448000000000000000000000000000000000000000000000000555344545f4254430000000000000000000000000000000000000000000000004254435f45544800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000001144b400000000000000000000000000000000000000000000000000000000001617dc0000000000000000000000000000000000000000000000000000000006b3d5e8000000000000000000000000000000000000000000000000000000004a324b740000000000000000000000000000000000000000000000000000000000f0ebc8000000000000000000000000000000000000000000000000000000006d058bc0000000000000000000000000000000000000000000000000000000007fffffff000000000000000000000000000000000000000000000000000000000595bfa00000000000000000000000000000000000000000000000000000000028b76e700000000000000000000000000000000000000000000000000000000002f6359000000000000000000000000000000000000000000000000000000000019a76200000000000000000000000000000000000000000000000000000000001a2da900000000000000000000000000000000000000000000000000000000003e4b43f0000000000000000000000000000000000000000000000000000000000aa70d0000000000000000000000000000000000000000000000000000000000228855000000000000000000000000000000000000000000000000000000000005b8d80000000000000000000000000000000000000000000000000000000001724398f0000000000000000000000000000000000000000000000000000000000003e5000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19800000000000000000000000000000000000000000000000000000000056bd19800000000000000000000000000000000000000000000000000000000056bd19801ca01c66b2eb640b6af2df8c10de464eb08dd3a7ebfa808a9a3e285ba4efc98eabc7a00ee135bb46d3e99077e623aac3c5d9a5d46bf7665a75f2cf8cb561eb18a71895f86f820fee850ba43b740082562294fbd95cee484181c01b519906a953624acffb7172890fb5eaaf9d05c44800801ca0a20d248c7cf304b6464d5814c856b547ebf769fccf0080a0910d477f78f758fca00ee62d774a181a917e8555b60e418e496aed05421ed1ec8bed45ede7c1967474f87083028717850ba43b740083015f9094354662bcd38883b6e67497f06fc4fee20339e57488540791be2c8a5c00801ba067550f26c0bbf939532d93c25418dbb58324c8adc4d131683332d84b79a50bafa07bd92d2ac686de38635bd30d8445f08a0c2c1fdbf4d26f05e05035abb4574313f86e820fef850ba43b740082562294fd3a935174aeb79b8d5d3935de1188e37427561f888aa39c121a270000801ca00168095256eaec4fdd074470d02f32c017947010813ca51568d42c720ad439f7a03b2fe84a971e46a83011fddc66acd225c05b8ae75de1b41ccfb2946413a7e111f87083028718850ba43b740083015f909407d71bfc263af5758225fab79e1656a22ac9824a8854195f5b7434e800801ba041e5a49b9db51829041ea32fd0fbf4da8b79b7933c25f513aee8a678bffb3ac1a06758def764e58b71bca0f8507b93631a9980fb73d193e6073dd5aa71187f5720f87083028719850ba43b740083015f909477f190fc96c507f40dc13aadc48d2cafcde5358b882239e735fb73a400801ca049be4e119b1f25f2e7d8d1c8c0ba38b2f90e5a800d18a3d34fee51244d4e7fbca04c1748a1aae6a4316039aa89a43f46908d4a4b3eb53f435a81d003db136ced2bf9010c825907850a7a3582008307a120943375ee30428b2a71c428afa5e89e427905f95f7e80b8a47d242ae5000000000000000000000000000000000000000000000000000097951b766aaa000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000221220b38db07e10542ae76234e4efbde58897c8502b3bed7c5740129f173fa97c000e0000000000000000000000000000000000000000000000000000000000001ca0f8486eb8fe36b4e5e300346fc6d2ca71366dbe8bd4492e94adf7bfe6093f76aba05294c7096a206200f59a341ba2f730a6b2b2b77ed995e1c4684c1fd28b503314f86a827ae00a83015f90948b687892c6cf88925ddd772718782a864e5353df8844b5a0318a73d038801ba054df5dcb8c2bf3e09b6a4a783e2933164351d5bfd5e2abbc4b585524b0c13c9ea035df380bafa2beb16543c70b216d3d9bbd663f4ff61c5c35d2837eaf44513b35f86a827ae10a83015f90948850522874c32a49c3a2c65035c5dfb049334caf8844c99175caf20000801ba079195b9ec5b735de194ac4811bb7270c405d865ef285b63c2b8565ad912f1df9a0647f97c5ec482301853330ce69e9f545f54592ad488b3712cac7d752bf1a883ef9021bf90218a07a6e5d44a6651ff0d954584b3c481c71887361e9b50ee07ff981b9e363042bc4a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942a65aca4d5fc5b5c859090a6c34d164135398226a04e9811a2f1329b469835d279dfbbaea95095ffcedd3f55eb16dc68c82f343d7da027e7fa8561b7f12072d5685196a5669b5586e16c4144ca2ab882d161f92eb95da06914d51d84d4a47310bb541056d64c913a7e5ddce99fb7fbf317ca15b4e0a864b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000860ad304994503830f1b3d832fefd8830148208456bd193c98d783010303844765746887676f312e352e31856c696e7578a0f5b92fa9266dd54f08bea6cf98e4bce3af23d874fd20ea4a850d8454e552712d88e66214db217b12f6" -var rlpBytes = rlpFromHex(blockBytes) -let ethBlock = rlpBytes.read(EthBlock) -let blockHeader = ethBlock.header -let blockBody = BlockBody(transactions: ethBlock.txs, uncles: ethBlock.uncles) +let bytes = "0xf91c77f90218a07d7701cadd868037f2d6e2898fcaa03e9e892dfac9f85d046b00bcf6bb786ad9a0bf34d0addf61b0e6cd483d6396cadaae438346dc5d8e0a0f3d9135b52a0f11a1944bb96091ee9d802ed039c4d1a5f6216f90f81b01a030e3d6c1021ecb806a1022e07f6e1491a5a2c17f3aaa9efbe1afe2325f9ba241a0fcf98a24af539c43988cec8e157ceec62b6c3ffa7ccc62c41e6b5d63d7af9e24a058656d3e1b463465ded49b02b74ec7dc6700870b5fafee285b49ac5789c39f46b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000860acef5f96bd8830f1b40832fefd883075e308456bd199d98d783010400844765746887676f312e352e31856c696e7578a0ea6f705561212f5523522114b40c77fc735a159eafb0d2c9f134ae33b8e4915088e67c3494e678ba87f90e9ff90c7ef86e8204ee85746a528800825208944d15e32435180fa21907e0d23a4c0021415fc21f88016345785d8a0000801ba04eeb6205a94672c865a68f7962ab0a4f4d5e152ea0b2ce3fa9821776a6e4d7fea0597fa300bf2dfa6b5631062d119a3b1c407e3a2ca5b2e3c4af4cd597f8aab0e8f907ed8227ac85104c533c00830f4240941194e966965418c7d73a42cceeb254d87586035601b90784d5064ed1000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000005200000000000000000000000000000000000000000000000000000000000000012455552555344000000000000000000000000000000000000000000000000000047425055534400000000000000000000000000000000000000000000000000005553444a505900000000000000000000000000000000000000000000000000005841555553440000000000000000000000000000000000000000000000000000584147555344000000000000000000000000000000000000000000000000000053503530300000000000000000000000000000000000000000000000000000004e415344415100000000000000000000000000000000000000000000000000004141504c00000000000000000000000000000000000000000000000000000000474f4f47000000000000000000000000000000000000000000000000000000004d53465400000000000000000000000000000000000000000000000000000000474d0000000000000000000000000000000000000000000000000000000000004745000000000000000000000000000000000000000000000000000000000000574d54000000000000000000000000000000000000000000000000000000000046000000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000555344545f455448000000000000000000000000000000000000000000000000555344545f4254430000000000000000000000000000000000000000000000004254435f45544800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000001144b400000000000000000000000000000000000000000000000000000000001617dc0000000000000000000000000000000000000000000000000000000006b3d5e8000000000000000000000000000000000000000000000000000000004a324b740000000000000000000000000000000000000000000000000000000000f0ebc8000000000000000000000000000000000000000000000000000000006d058bc0000000000000000000000000000000000000000000000000000000007fffffff000000000000000000000000000000000000000000000000000000000595bfa00000000000000000000000000000000000000000000000000000000028b76e700000000000000000000000000000000000000000000000000000000002f6359000000000000000000000000000000000000000000000000000000000019a76200000000000000000000000000000000000000000000000000000000001a2da900000000000000000000000000000000000000000000000000000000003e4b43f0000000000000000000000000000000000000000000000000000000000aa70d0000000000000000000000000000000000000000000000000000000000228855000000000000000000000000000000000000000000000000000000000005b8d80000000000000000000000000000000000000000000000000000000001724398f0000000000000000000000000000000000000000000000000000000000003e5000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19780000000000000000000000000000000000000000000000000000000056bd19800000000000000000000000000000000000000000000000000000000056bd19800000000000000000000000000000000000000000000000000000000056bd19801ca01c66b2eb640b6af2df8c10de464eb08dd3a7ebfa808a9a3e285ba4efc98eabc7a00ee135bb46d3e99077e623aac3c5d9a5d46bf7665a75f2cf8cb561eb18a71895f86f820fee850ba43b740082562294fbd95cee484181c01b519906a953624acffb7172890fb5eaaf9d05c44800801ca0a20d248c7cf304b6464d5814c856b547ebf769fccf0080a0910d477f78f758fca00ee62d774a181a917e8555b60e418e496aed05421ed1ec8bed45ede7c1967474f87083028717850ba43b740083015f9094354662bcd38883b6e67497f06fc4fee20339e57488540791be2c8a5c00801ba067550f26c0bbf939532d93c25418dbb58324c8adc4d131683332d84b79a50bafa07bd92d2ac686de38635bd30d8445f08a0c2c1fdbf4d26f05e05035abb4574313f86e820fef850ba43b740082562294fd3a935174aeb79b8d5d3935de1188e37427561f888aa39c121a270000801ca00168095256eaec4fdd074470d02f32c017947010813ca51568d42c720ad439f7a03b2fe84a971e46a83011fddc66acd225c05b8ae75de1b41ccfb2946413a7e111f87083028718850ba43b740083015f909407d71bfc263af5758225fab79e1656a22ac9824a8854195f5b7434e800801ba041e5a49b9db51829041ea32fd0fbf4da8b79b7933c25f513aee8a678bffb3ac1a06758def764e58b71bca0f8507b93631a9980fb73d193e6073dd5aa71187f5720f87083028719850ba43b740083015f909477f190fc96c507f40dc13aadc48d2cafcde5358b882239e735fb73a400801ca049be4e119b1f25f2e7d8d1c8c0ba38b2f90e5a800d18a3d34fee51244d4e7fbca04c1748a1aae6a4316039aa89a43f46908d4a4b3eb53f435a81d003db136ced2bf9010c825907850a7a3582008307a120943375ee30428b2a71c428afa5e89e427905f95f7e80b8a47d242ae5000000000000000000000000000000000000000000000000000097951b766aaa000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000221220b38db07e10542ae76234e4efbde58897c8502b3bed7c5740129f173fa97c000e0000000000000000000000000000000000000000000000000000000000001ca0f8486eb8fe36b4e5e300346fc6d2ca71366dbe8bd4492e94adf7bfe6093f76aba05294c7096a206200f59a341ba2f730a6b2b2b77ed995e1c4684c1fd28b503314f86a827ae00a83015f90948b687892c6cf88925ddd772718782a864e5353df8844b5a0318a73d038801ba054df5dcb8c2bf3e09b6a4a783e2933164351d5bfd5e2abbc4b585524b0c13c9ea035df380bafa2beb16543c70b216d3d9bbd663f4ff61c5c35d2837eaf44513b35f86a827ae10a83015f90948850522874c32a49c3a2c65035c5dfb049334caf8844c99175caf20000801ba079195b9ec5b735de194ac4811bb7270c405d865ef285b63c2b8565ad912f1df9a0647f97c5ec482301853330ce69e9f545f54592ad488b3712cac7d752bf1a883ef9021bf90218a07a6e5d44a6651ff0d954584b3c481c71887361e9b50ee07ff981b9e363042bc4a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942a65aca4d5fc5b5c859090a6c34d164135398226a04e9811a2f1329b469835d279dfbbaea95095ffcedd3f55eb16dc68c82f343d7da027e7fa8561b7f12072d5685196a5669b5586e16c4144ca2ab882d161f92eb95da06914d51d84d4a47310bb541056d64c913a7e5ddce99fb7fbf317ca15b4e0a864b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000860ad304994503830f1b3d832fefd8830148208456bd193c98d783010303844765746887676f312e352e31856c696e7578a0f5b92fa9266dd54f08bea6cf98e4bce3af23d874fd20ea4a850d8454e552712d88e66214db217b12f6f90bb7f90128a05879cc9bb1b63f28493300c7d2ab6eda50487cd14255661e52287f56e457fec6825208b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a0a8892365c5f7d48bb7979f40f304d593139c88968aa6918c0304920ea2bacfc28303f735b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a01cef075979cdff0692a2c9cc3c0009fdfc5f225db0c91dc746a18783b05c330d8304493db9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a0ae9b051a50ea66ada1b71a19c5f23c12a576c131371faba59b978589c20d001983049b45b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a080007d613cec57228338d5c70dbb1cd2d9462b6d53b19b5f0bccb88868f649ec8304ed4db9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a093b357397f62c279ef538050d31fc90097659a05f629739eb97bee41acdfa04283053f55b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a070de2004c27aac1311d7084d749db6637c631f76eb3cf26e2d876c447e23f0328305915db9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a0edc5fb798fb43992a1f8019c81ccceb4468d43c4e8586924f53b4688638bf78a8306ba20b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a05ee6946d6b94ce0c74fc1d3f8cf48e194fbd7de102ae678fa77919abc9b9f88183070c28b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0f90129a007076f5fbf84924032494a0fd4891ed06bcf3f724416c707769737c9d3f2a0ac83075e30b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0" + +let rlpb = rlpFromHex(bytes) +var headerRlp = rlpb.listElem(0) +var bodyRlp = rlpb.listElem(1) +var receitsRlp = rlpb.listElem(2) + +let blockHeader = headerRlp.read(BlockHeader) +let blockBody = bodyRlp.read(BlockBody) +let receipts = receitsRlp.read(seq[Receipt]) suite "History network content validation": test "Correct header should pass validation": @@ -58,7 +63,7 @@ suite "History network content validation": check: maybeBody.isSome() - test "Malformed block body bytes should pass validation": + test "Malformed block body bytes should fail validation": let correctBodyBytes = rlp.encode(blockBody) let malformedBytes = correctBodyBytes[10..correctBodyBytes.high] @@ -94,3 +99,31 @@ suite "History network content validation": check: maybeBody.isNone() + + test "Correct receipts should pass validation": + let correctReceiptsBytes = rlp.encode(receipts) + + let maybeReceips = validateReceiptsBytes(correctReceiptsBytes, blockHeader.receiptRoot) + + check: + maybeReceips.isSome() + + test "Malformed receipts bytes should fail validation": + let correctReceiptsBytes = rlp.encode(receipts) + + let malformedBytes = correctReceiptsBytes[10..correctReceiptsBytes.high] + + let maybeReceips = validateReceiptsBytes(malformedBytes, blockHeader.receiptRoot) + + check: + maybeReceips.isNone() + + test "Modified receipts list should not pass validation": + var modifiedReceipts = receipts[1..receipts.high] + + let modifiedReceiptsBytes = rlp.encode(modifiedReceipts) + + let maybeReceips = validateReceiptsBytes(modifiedReceiptsBytes, blockHeader.receiptRoot) + + check: + maybeReceips.isNone()