|
| 1 | +# Nimbus |
| 2 | +# Copyright (c) 2022-2025 Status Research & Development GmbH |
| 3 | +# Licensed and distributed under either of |
| 4 | +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). |
| 5 | +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). |
| 6 | +# at your option. This file may not be copied, modified, or distributed except according to those terms. |
| 7 | + |
| 8 | +{.push raises: [].} |
| 9 | + |
| 10 | +import |
| 11 | + eth/common/[headers_rlp], |
| 12 | + ssz_serialization, |
| 13 | + ssz_serialization/[proofs, merkleization], |
| 14 | + ../../../common/common_types, |
| 15 | + ../history_content, |
| 16 | + ./historical_hashes_accumulator |
| 17 | + |
| 18 | +from eth/common/eth_types_rlp import rlpHash |
| 19 | + |
| 20 | +export |
| 21 | + ssz_serialization, merkleization, proofs, common_types, historical_hashes_accumulator |
| 22 | + |
| 23 | +# |
| 24 | +# Implementation of pre-merge block proofs by making use of the frozen HistoricalHashesAccumulator |
| 25 | +# |
| 26 | +# Types are defined here: |
| 27 | +# https://github.com/ethereum/portal-network-specs/blob/31bc7e58e2e8acfba895d5a12a9ae3472894d398/history/history-network.md#block-header |
| 28 | +# |
| 29 | +# Proof system explained here: |
| 30 | +# https://github.com/ethereum/portal-network-specs/blob/31bc7e58e2e8acfba895d5a12a9ae3472894d398/history/history-network.md#blockproofhistoricalhashesaccumulator |
| 31 | +# |
| 32 | +# The HistoricalHashesAccumulator is frozen at TheMerge, this means that it can |
| 33 | +# only be used for the blocks before TheMerge. |
| 34 | +# |
| 35 | +# Requirements: |
| 36 | +# |
| 37 | +# - For building the proofs: |
| 38 | +# Portal node/bridge that has access to all the EL chain historical data (blocks) |
| 39 | +# for that specific period. This can be provided through era1 files. |
| 40 | +# |
| 41 | +# - For verifying the proofs: |
| 42 | +# To verify the proof the HistoricalHashesAccumulator is required. |
| 43 | +# As this field is frozen, it can be baked into the client. |
| 44 | +# Root of the HistoricalHashesAccumulator can be cross-verified with EIP-7643 data: |
| 45 | +# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7643.md#pre-pos-root |
| 46 | +# |
| 47 | + |
| 48 | +# Total size: 15 * 32 bytes = 480 bytes |
| 49 | +type HistoricalHashesAccumulatorProof* = array[15, Digest] |
| 50 | + |
| 51 | +func getEpochIndex*(blockNumber: uint64): uint64 = |
| 52 | + blockNumber div EPOCH_SIZE |
| 53 | + |
| 54 | +func getEpochIndex*(header: Header): uint64 = |
| 55 | + ## Get the index for the historical epochs |
| 56 | + getEpochIndex(header.number) |
| 57 | + |
| 58 | +func getHeaderRecordIndex*(blockNumber: uint64, epochIndex: uint64): uint64 = |
| 59 | + ## Get the relative header index for the epoch accumulator |
| 60 | + uint64(blockNumber - epochIndex * EPOCH_SIZE) |
| 61 | + |
| 62 | +func getHeaderRecordIndex*(header: Header, epochIndex: uint64): uint64 = |
| 63 | + ## Get the relative header index for the epoch accumulator |
| 64 | + getHeaderRecordIndex(header.number, epochIndex) |
| 65 | + |
| 66 | +func isPreMerge*(blockNumber: uint64): bool = |
| 67 | + blockNumber < mergeBlockNumber |
| 68 | + |
| 69 | +func isPreMerge*(header: Header): bool = |
| 70 | + isPreMerge(header.number) |
| 71 | + |
| 72 | +func verifyProof*( |
| 73 | + a: FinishedHistoricalHashesAccumulator, |
| 74 | + header: Header, |
| 75 | + proof: HistoricalHashesAccumulatorProof, |
| 76 | +): bool = |
| 77 | + let |
| 78 | + epochIndex = getEpochIndex(header) |
| 79 | + epochRecordHash = Digest(data: a.historicalEpochs[epochIndex]) |
| 80 | + |
| 81 | + leave = hash_tree_root(header.rlpHash()) |
| 82 | + headerRecordIndex = getHeaderRecordIndex(header, epochIndex) |
| 83 | + |
| 84 | + # For lists, leaves starts at epochSize*2 (because of the extra len branch). |
| 85 | + # Then another *2 to enter one layer deeper in the `HeaderRecord`. |
| 86 | + # list_root |
| 87 | + # / \ |
| 88 | + # list_data_root len(list) |
| 89 | + # / \ |
| 90 | + # /\ /\ |
| 91 | + # ....... |
| 92 | + # / ... /\ |
| 93 | + # hr_root |
| 94 | + # / \ |
| 95 | + # blockhash |
| 96 | + gIndex = GeneralizedIndex(EPOCH_SIZE * 2 * 2 + (headerRecordIndex * 2)) |
| 97 | + |
| 98 | + verify_merkle_multiproof(@[leave], proof, @[gIndex], epochRecordHash) |
| 99 | + |
| 100 | +func buildProof*( |
| 101 | + header: Header, epochRecord: EpochRecord | EpochRecordCached |
| 102 | +): Result[HistoricalHashesAccumulatorProof, string] = |
| 103 | + doAssert(header.isPreMerge(), "Must be pre merge header") |
| 104 | + |
| 105 | + let |
| 106 | + epochIndex = getEpochIndex(header) |
| 107 | + headerRecordIndex = getHeaderRecordIndex(header, epochIndex) |
| 108 | + |
| 109 | + gIndex = GeneralizedIndex(EPOCH_SIZE * 2 * 2 + (headerRecordIndex * 2)) |
| 110 | + |
| 111 | + var proof: HistoricalHashesAccumulatorProof |
| 112 | + ?epochRecord.build_proof(gIndex, proof) |
| 113 | + |
| 114 | + ok(proof) |
| 115 | + |
| 116 | +func buildHeaderWithProof*( |
| 117 | + header: Header, epochRecord: EpochRecord | EpochRecordCached |
| 118 | +): Result[BlockHeaderWithProof, string] = |
| 119 | + let proof = ?buildProof(header, epochRecord) |
| 120 | + |
| 121 | + ok( |
| 122 | + BlockHeaderWithProof( |
| 123 | + header: ByteList[MAX_HEADER_LENGTH].init(rlp.encode(header)), |
| 124 | + proof: ByteList[MAX_HEADER_PROOF_LENGTH].init(SSZ.encode(proof)), |
| 125 | + ) |
| 126 | + ) |
0 commit comments