97 lines
11 KiB
Nim
97 lines
11 KiB
Nim
|
# 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.}
|
||
|
|
||
|
import
|
||
|
unittest2, stint,
|
||
|
stew/[byteutils, results],
|
||
|
eth/[common/eth_types, rlp],
|
||
|
../network/history/history_network
|
||
|
|
||
|
let blockBytes = "0xf910baf90218a07d7701cadd868037f2d6e2898fcaa03e9e892dfac9f85d046b00bcf6bb786ad9a0bf34d0addf61b0e6cd483d6396cadaae438346dc5d8e0a0f3d9135b52a0f11a1944bb96091ee9d802ed039c4d1a5f6216f90f81b01a030e3d6c1021ecb806a1022e07f6e1491a5a2c17f3aaa9efbe1afe2325f9ba241a0fcf98a24af539c43988cec8e157ceec62b6c3ffa7ccc62c41e6b5d63d7af9e24a058656d3e1b463465ded49b02b74ec7dc6700870b5fafee285b49ac5789c39f46b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000860acef5f96bd8830f1b40832fefd883075e308456bd199d98d783010400844765746887676f312e352e31856c696e7578a0ea6f705561212f5523522114b40c77fc735a159eafb0d2c9f134ae33b8e4915088e67c3494e678ba87f90c7ef86e8204ee85746a528800825208944d15e32435180fa21907e0d23a4c0021415fc21f88016345785d8a0000801ba04eeb6205a94672c865a68f7962ab0a4f4d5e152ea0b2ce3fa9821776a6e4d7fea0597fa300bf2dfa6b5631062d119a3b1c407e3a2ca5b2e3c4af4cd597f8aab0e8f907ed8227ac85104c533c00830f4240941194e966965418c7d73a42cceeb254d87586035601b90784d5064ed1000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000005200000000000000000000000000000000000000000000000000000000000000012455552555344000000000000000000000000000000000000000000000000000047425055534400000000000000000000000000000000000000000000000000005553444a505900000000000000000000000000000000000000000000000000005841555553440000000000000000000000000000000000000000000000000000584147555344000000000000000000000000000000000000000000000000000053503530300000000000000000000000000000000000000000000000000000004e415344415100000000000000000000000000000000000000000000000000004141504c00000000000000000000000000000000000000000000000000000000474f4f47000000000000000000000000000000000000000000000000000000004d53465400000000000000000000000000000000000000000000000000000000474d0000000000000000000000000000000000000000000000000000000000004745000000000000000000000000000000000000000000000000000000000000574d54000000000000000000000000000000000000000000000000000000000046000000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000555344545f455448000000000000000000000000000000000000000000000000555344545f4254430000000000000000000000000000000000000000000000004254435f45544800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000001144b400000000000000000000000000000000000000000000000000000000001617dc0000000000000000000000000000000000000000000000000000000006b3d5e8000000000000000000000000000000000000000000000000000000004a324b740000000000000000000000000000000000000000000000000000000000f0ebc8000000000000000000000000000000000000000000000000000000006d058bc0000000000000000000000000000000000000000000000000000000007fffffff000000000000000000000000000000000000000000000000000000000595bfa00000000000000000000000000000000000000000000000000000000028b76e700000000000000000000000000000000000000000000000000000000002f6359000000000000000000000000000000000000000000000000000000000019a76200000000000000000000000000000000000000000000000000000000001a2da900000000000000000000000000000000000000000000000000000000003e4b43f0000000000000000000000000000000000000000000000000000000000aa70d0000000000000000000000000000000000000000000000000000000000228855000000000000000000000000000000000000000000000000000000000005b8d80000000000000000000000000000000000000000000000000000000001724398f0000000000000000000000000000000000000000000000000000000000003e5000000000000000000000000000000000000000000000000
|
||
|
var rlpBytes = rlpFromHex(blockBytes)
|
||
|
let ethBlock = rlpBytes.read(EthBlock)
|
||
|
let blockHeader = ethBlock.header
|
||
|
let blockBody = BlockBody(transactions: ethBlock.txs, uncles: ethBlock.uncles)
|
||
|
|
||
|
suite "History network content validation":
|
||
|
test "Correct header should pass validation":
|
||
|
let correctHeaderBytes = rlp.encode(blockHeader)
|
||
|
|
||
|
let maybeHeader = validateHeaderBytes(correctHeaderBytes, blockHeader.blockHash())
|
||
|
|
||
|
check:
|
||
|
maybeHeader.isSome()
|
||
|
|
||
|
test "Malformed header bytes should not pass validation":
|
||
|
let correctHeaderBytes = rlp.encode(blockHeader)
|
||
|
|
||
|
let malformedBytes = correctHeaderBytes[10..correctHeaderBytes.high]
|
||
|
|
||
|
let maybeHeader = validateHeaderBytes(malformedBytes, blockHeader.blockHash())
|
||
|
|
||
|
check:
|
||
|
maybeHeader.isNone()
|
||
|
|
||
|
test "Header different than expected should not pass validation":
|
||
|
var modifiedHeader = blockHeader
|
||
|
|
||
|
modifiedHeader.gasUsed = modifiedHeader.gasUsed + 1
|
||
|
|
||
|
let differentHeaderBytes = rlp.encode(modifiedHeader)
|
||
|
|
||
|
let maybeHeader = validateHeaderBytes(differentHeaderBytes, blockHeader.blockHash())
|
||
|
|
||
|
check:
|
||
|
maybeHeader.isNone()
|
||
|
|
||
|
test "Correct block body should pass validation":
|
||
|
let correctBodyBytes = rlp.encode(blockBody)
|
||
|
|
||
|
let maybeBody = validateBodyBytes(correctBodyBytes, blockHeader.txRoot, blockHeader.ommersHash)
|
||
|
|
||
|
check:
|
||
|
maybeBody.isSome()
|
||
|
|
||
|
test "Malformed block body bytes should pass validation":
|
||
|
let correctBodyBytes = rlp.encode(blockBody)
|
||
|
|
||
|
let malformedBytes = correctBodyBytes[10..correctBodyBytes.high]
|
||
|
|
||
|
let maybeBody = validateBodyBytes(malformedBytes, blockHeader.txRoot, blockHeader.ommersHash)
|
||
|
|
||
|
check:
|
||
|
maybeBody.isNone()
|
||
|
|
||
|
test "Block body with modified transactions list should not pass validation":
|
||
|
var modifiedBody = blockBody
|
||
|
|
||
|
# drop first transaction
|
||
|
let modifiedTransactionList = blockBody.transactions[1..blockBody.transactions.high]
|
||
|
|
||
|
modifiedBody.transactions = modifiedTransactionList
|
||
|
|
||
|
let modifiedBodyBytes = rlp.encode(modifiedBody)
|
||
|
|
||
|
let maybeBody = validateBodyBytes(modifiedBodyBytes, blockHeader.txRoot, blockHeader.ommersHash)
|
||
|
|
||
|
check:
|
||
|
maybeBody.isNone()
|
||
|
|
||
|
test "Block body with modified uncles list should not pass validation":
|
||
|
var modifiedBody = blockBody
|
||
|
|
||
|
modifiedBody.uncles = @[]
|
||
|
|
||
|
let modifiedBodyBytes = rlp.encode(modifiedBody)
|
||
|
|
||
|
let maybeBody = validateBodyBytes(modifiedBodyBytes, blockHeader.txRoot, blockHeader.ommersHash)
|
||
|
|
||
|
check:
|
||
|
maybeBody.isNone()
|