2022-06-16 06:50:29 +00:00
|
|
|
# Nimbus
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-06-16 06:50:29 +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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-06-16 06:50:29 +00:00
|
|
|
|
|
|
|
import
|
2024-02-28 17:31:45 +00:00
|
|
|
unittest2,
|
|
|
|
stint,
|
2022-09-03 18:15:35 +00:00
|
|
|
eth/common/eth_types_rlp,
|
2023-01-05 14:26:58 +00:00
|
|
|
../eth_data/history_data_json_store,
|
2022-10-18 11:07:32 +00:00
|
|
|
../network/history/[history_content, accumulator],
|
|
|
|
./test_helpers
|
2022-08-24 14:41:04 +00:00
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
suite "Header Accumulator":
|
2022-09-09 11:12:09 +00:00
|
|
|
test "Header Accumulator Canonical Verification":
|
2022-06-23 19:00:59 +00:00
|
|
|
const
|
|
|
|
# Amount of headers to be created and added to the accumulator
|
2022-10-10 10:59:55 +00:00
|
|
|
amount = mergeBlockNumber
|
|
|
|
# Headers to test verification for.
|
|
|
|
# Note: This test assumes at least 5 epochs
|
2022-06-23 19:00:59 +00:00
|
|
|
headersToTest = [
|
|
|
|
0,
|
|
|
|
epochSize - 1,
|
|
|
|
epochSize,
|
2024-02-28 17:31:45 +00:00
|
|
|
epochSize * 2 - 1,
|
|
|
|
epochSize * 2,
|
|
|
|
epochSize * 3 - 1,
|
|
|
|
epochSize * 3,
|
|
|
|
epochSize * 3 + 1,
|
|
|
|
int(amount) - 1,
|
|
|
|
]
|
2022-06-23 19:00:59 +00:00
|
|
|
|
|
|
|
var headers: seq[BlockHeader]
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< amount:
|
2022-06-23 19:00:59 +00:00
|
|
|
# Note: These test headers will not be a blockchain, as the parent hashes
|
|
|
|
# are not properly filled in. That's fine however for this test, as that
|
|
|
|
# is not the way the headers are verified with the accumulator.
|
2024-02-28 17:31:45 +00:00
|
|
|
headers.add(BlockHeader(blockNumber: i.stuint(256), difficulty: 1.stuint(256)))
|
2022-06-23 19:00:59 +00:00
|
|
|
|
2022-10-18 11:07:32 +00:00
|
|
|
let accumulatorRes = buildAccumulatorData(headers)
|
2022-10-17 18:38:51 +00:00
|
|
|
check accumulatorRes.isOk()
|
2022-10-18 11:07:32 +00:00
|
|
|
let (accumulator, epochAccumulators) = accumulatorRes.get()
|
2022-10-17 18:38:51 +00:00
|
|
|
|
2022-06-23 19:00:59 +00:00
|
|
|
block: # Test valid headers
|
|
|
|
for i in headersToTest:
|
|
|
|
let header = headers[i]
|
2022-11-04 08:27:01 +00:00
|
|
|
let proof = buildProof(header, epochAccumulators)
|
2022-10-10 10:59:55 +00:00
|
|
|
check:
|
|
|
|
proof.isOk()
|
2022-11-04 08:27:01 +00:00
|
|
|
verifyAccumulatorProof(accumulator, header, proof.get()).isOk()
|
2022-06-23 19:00:59 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
block: # Test invalid headers
|
|
|
|
# Post merge block number must fail (> than latest header in accumulator)
|
2022-11-04 08:27:01 +00:00
|
|
|
var proof: AccumulatorProof
|
2022-10-10 10:59:55 +00:00
|
|
|
let header = BlockHeader(blockNumber: mergeBlockNumber.stuint(256))
|
2022-11-04 08:27:01 +00:00
|
|
|
check verifyAccumulatorProof(accumulator, header, proof).isErr()
|
2022-06-23 19:00:59 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
# Test altered block headers by altering the difficulty
|
2022-06-23 19:00:59 +00:00
|
|
|
for i in headersToTest:
|
2022-11-04 08:27:01 +00:00
|
|
|
let proof = buildProof(headers[i], epochAccumulators)
|
2022-10-10 10:59:55 +00:00
|
|
|
check:
|
|
|
|
proof.isOk()
|
|
|
|
# Alter the block header so the proof no longer matches
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = BlockHeader(blockNumber: i.stuint(256), difficulty: 2.stuint(256))
|
2022-06-23 19:00:59 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
check verifyAccumulatorProof(accumulator, header, proof.get()).isErr()
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
block: # Test invalid proofs
|
2022-11-04 08:27:01 +00:00
|
|
|
var proof: AccumulatorProof
|
2022-09-09 11:12:09 +00:00
|
|
|
|
|
|
|
for i in headersToTest:
|
2022-11-04 08:27:01 +00:00
|
|
|
check verifyAccumulatorProof(accumulator, headers[i], proof).isErr()
|
2022-09-09 11:12:09 +00:00
|
|
|
|
2022-10-17 18:38:51 +00:00
|
|
|
test "Header Accumulator - Not Finished":
|
|
|
|
# Less headers than needed to finish the accumulator
|
|
|
|
const amount = mergeBlockNumber - 1
|
|
|
|
|
|
|
|
var headers: seq[BlockHeader]
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< amount:
|
|
|
|
headers.add(BlockHeader(blockNumber: i.stuint(256), difficulty: 1.stuint(256)))
|
2022-10-17 18:38:51 +00:00
|
|
|
|
|
|
|
let accumulatorRes = buildAccumulator(headers)
|
|
|
|
|
|
|
|
check accumulatorRes.isErr()
|
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
test "Header BlockNumber to EpochAccumulator Root":
|
|
|
|
# Note: This test assumes at least 3 epochs
|
|
|
|
const amount = mergeBlockNumber
|
2022-09-09 11:12:09 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
var
|
|
|
|
headerHashes: seq[Hash256] = @[]
|
|
|
|
headers: seq[BlockHeader]
|
2022-09-09 11:12:09 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< amount:
|
2022-10-10 10:59:55 +00:00
|
|
|
let header = BlockHeader(blockNumber: u256(i), difficulty: u256(1))
|
|
|
|
headers.add(header)
|
|
|
|
headerHashes.add(header.blockHash())
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-10-17 18:38:51 +00:00
|
|
|
let accumulatorRes = buildAccumulator(headers)
|
|
|
|
check accumulatorRes.isOk()
|
|
|
|
let accumulator = accumulatorRes.get()
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
# Valid response for block numbers in epoch 0
|
2022-08-04 06:34:53 +00:00
|
|
|
block:
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< epochSize:
|
2022-10-10 10:59:55 +00:00
|
|
|
let res = accumulator.getBlockEpochDataForBlockNumber(u256(i))
|
2022-08-04 06:34:53 +00:00
|
|
|
check:
|
2022-10-10 10:59:55 +00:00
|
|
|
res.isOk()
|
|
|
|
res.get().epochHash == accumulator.historicalEpochs[0]
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
# Valid response for block numbers in epoch 1
|
2022-08-04 06:34:53 +00:00
|
|
|
block:
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in epochSize ..< (2 * epochSize):
|
2022-10-10 10:59:55 +00:00
|
|
|
let res = accumulator.getBlockEpochDataForBlockNumber(u256(i))
|
2022-08-04 06:34:53 +00:00
|
|
|
check:
|
2022-10-10 10:59:55 +00:00
|
|
|
res.isOk()
|
|
|
|
res.get().epochHash == accumulator.historicalEpochs[1]
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
# Valid response for block numbers in the incomplete (= last) epoch
|
2022-08-04 06:34:53 +00:00
|
|
|
block:
|
2022-10-10 10:59:55 +00:00
|
|
|
const startIndex = mergeBlockNumber - (mergeBlockNumber mod epochSize)
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in startIndex ..< mergeBlockNumber:
|
2022-10-10 10:59:55 +00:00
|
|
|
let res = accumulator.getBlockEpochDataForBlockNumber(u256(i))
|
2022-08-04 06:34:53 +00:00
|
|
|
check:
|
2022-10-10 10:59:55 +00:00
|
|
|
res.isOk()
|
2024-02-28 17:31:45 +00:00
|
|
|
res.get().epochHash == accumulator.historicalEpochs[preMergeEpochs - 1]
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
# Error for block number at and past merge
|
2022-08-04 06:34:53 +00:00
|
|
|
block:
|
|
|
|
check:
|
2024-02-28 17:31:45 +00:00
|
|
|
accumulator.getBlockEpochDataForBlockNumber(u256(mergeBlockNumber)).isErr()
|
|
|
|
|
|
|
|
accumulator.getBlockEpochDataForBlockNumber(u256(mergeBlockNumber + 1)).isErr()
|