# Nimbus # 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.} {.push raises: [Defect].} import unittest2, stint, eth/common/eth_types_rlp, ../data/history_data_parser, ../network/history/[history_content, accumulator], ./test_helpers func buildProof( epochAccumulators: seq[EpochAccumulator], header: BlockHeader): Result[seq[Digest], string] = let epochIndex = getEpochIndex(header) epochAccumulator = epochAccumulators[epochIndex] headerRecordIndex = getHeaderRecordIndex(header, epochIndex) gIndex = GeneralizedIndex(epochSize*2*2 + (headerRecordIndex*2)) return epochAccumulator.build_proof(gIndex) suite "Header Accumulator": test "Header Accumulator Canonical Verification": const # Amount of headers to be created and added to the accumulator amount = mergeBlockNumber # Headers to test verification for. # Note: This test assumes at least 5 epochs headersToTest = [ 0, epochSize - 1, epochSize, epochSize*2 - 1, epochSize*2, epochSize*3 - 1, epochSize*3, epochSize*3 + 1, int(amount) - 1] var headers: seq[BlockHeader] for i in 0.. than latest header in accumulator) let header = BlockHeader(blockNumber: mergeBlockNumber.stuint(256)) check verifyHeader(accumulator, header, @[]).isErr() # Test altered block headers by altering the difficulty for i in headersToTest: let proof = buildProof( epochAccumulators, headers[i]) check: proof.isOk() # Alter the block header so the proof no longer matches let header = BlockHeader( blockNumber: i.stuint(256), difficulty: 2.stuint(256)) check verifyHeader(accumulator, header, proof.get()).isErr() block: # Test invalid proofs var proof: seq[Digest] for i in 0..14: var digest: Digest proof.add(digest) for i in headersToTest: check verifyHeader(accumulator, headers[i], proof).isErr() test "Header Accumulator - Not Finished": # Less headers than needed to finish the accumulator const amount = mergeBlockNumber - 1 var headers: seq[BlockHeader] for i in 0..