2022-03-02 14:29:12 +00:00
|
|
|
# Nimbus - Portal Network
|
|
|
|
# Copyright (c) 2022 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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-03-02 14:29:12 +00:00
|
|
|
import
|
2022-07-01 19:51:51 +00:00
|
|
|
unittest2, stint,
|
|
|
|
stew/[byteutils, results],
|
2022-03-02 14:29:12 +00:00
|
|
|
eth/[common/eth_types, rlp],
|
2022-07-01 19:51:51 +00:00
|
|
|
../common/common_types,
|
2022-09-09 19:21:48 +00:00
|
|
|
../data/history_data_parser,
|
2022-03-02 14:29:12 +00:00
|
|
|
../network/history/history_network
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
const
|
2022-07-05 14:14:52 +00:00
|
|
|
dataFile = "./fluffy/tests/blocks/mainnet_blocks_selected.json"
|
2022-07-01 19:51:51 +00:00
|
|
|
# Block that will be validated
|
|
|
|
blockHashStr =
|
|
|
|
"0xce8f770a56203e10afe19c7dd7e2deafc356e6cce0a560a30a85add03da56137"
|
|
|
|
|
|
|
|
suite "History Network Content Validation":
|
2022-08-01 19:00:21 +00:00
|
|
|
let blockDataTable = readJsonType(dataFile, BlockDataTable).expect(
|
2022-07-01 19:51:51 +00:00
|
|
|
"Valid data file should parse")
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let blockData =
|
|
|
|
try:
|
|
|
|
blockDataTable[blockHashStr]
|
|
|
|
except KeyError:
|
|
|
|
raiseAssert("Block must be in test file")
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let
|
|
|
|
blockHeaderBytes = blockData.header.hexToSeqByte()
|
|
|
|
blockBodyBytes = blockData.body.hexToSeqByte()
|
|
|
|
receiptsBytes = blockData.receipts.hexToSeqByte()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
blockHash = BlockHash.fromHex(blockHashStr)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
blockHeader = decodeRlp(blockHeaderBytes, BlockHeader).expect(
|
|
|
|
"Valid header should decode")
|
|
|
|
blockBody = validateBlockBodyBytes(
|
|
|
|
blockBodyBytes, blockHeader.txRoot, blockHeader.ommersHash).expect(
|
|
|
|
"Should be Valid decoded block body")
|
|
|
|
receipts = validateReceiptsBytes(
|
|
|
|
receiptsBytes, blockHeader.receiptRoot).expect(
|
|
|
|
"Should be Valid decoded receipts")
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Valid Header":
|
|
|
|
check validateBlockHeaderBytes(blockHeaderBytes, blockHash).isOk()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Malformed Header":
|
|
|
|
let malformedBytes = blockHeaderBytes[10..blockHeaderBytes.high]
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
check validateBlockHeaderBytes(malformedBytes, blockHash).isErr()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Invalid Header - Different gasUsed":
|
2022-03-02 14:29:12 +00:00
|
|
|
var modifiedHeader = blockHeader
|
|
|
|
|
|
|
|
modifiedHeader.gasUsed = modifiedHeader.gasUsed + 1
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let modifiedHeaderBytes = rlp.encode(modifiedHeader)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
check validateBlockHeaderBytes(modifiedHeaderBytes, blockHash).isErr()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Valid Block Body":
|
|
|
|
check validateBlockBodyBytes(
|
|
|
|
blockBodyBytes, blockHeader.txRoot, blockHeader.ommersHash).isOk()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Malformed Block Body":
|
|
|
|
let malformedBytes = blockBodyBytes[10..blockBodyBytes.high]
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
check validateBlockBodyBytes(
|
|
|
|
malformedBytes, blockHeader.txRoot, blockHeader.ommersHash).isErr()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Invalid Block Body - Modified Transaction List":
|
2022-03-02 14:29:12 +00:00
|
|
|
var modifiedBody = blockBody
|
|
|
|
|
|
|
|
# drop first transaction
|
2022-07-01 19:51:51 +00:00
|
|
|
let modifiedTransactionList =
|
|
|
|
blockBody.transactions[1..blockBody.transactions.high]
|
2022-03-02 14:29:12 +00:00
|
|
|
|
|
|
|
modifiedBody.transactions = modifiedTransactionList
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let modifiedBodyBytes = encode(modifiedBody)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
check validateBlockBodyBytes(
|
|
|
|
modifiedBodyBytes, blockHeader.txRoot, blockHeader.ommersHash).isErr()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Invalid Block Body - Modified Uncles List":
|
2022-03-02 14:29:12 +00:00
|
|
|
var modifiedBody = blockBody
|
|
|
|
|
|
|
|
modifiedBody.uncles = @[]
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let modifiedBodyBytes = encode(modifiedBody)
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
check validateBlockBodyBytes(
|
|
|
|
modifiedBodyBytes, blockHeader.txRoot, blockHeader.ommersHash).isErr()
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Valid Receipts":
|
|
|
|
check validateReceiptsBytes(receiptsBytes, blockHeader.receiptRoot).isOk()
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Malformed Receipts":
|
|
|
|
let malformedBytes = receiptsBytes[10..receiptsBytes.high]
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
check validateReceiptsBytes(malformedBytes, blockHeader.receiptRoot).isErr()
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
test "Invalid Receipts - Modified Receipts List":
|
2022-06-10 10:24:53 +00:00
|
|
|
var modifiedReceipts = receipts[1..receipts.high]
|
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
let modifiedReceiptsBytes = encode(modifiedReceipts)
|
2022-06-10 10:24:53 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
check validateReceiptsBytes(
|
|
|
|
modifiedReceiptsBytes, blockHeader.receiptRoot).isErr()
|