2022-06-16 06:50:29 +00:00
|
|
|
# Nimbus
|
2024-02-09 10:13:12 +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.
|
|
|
|
|
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
|
|
|
eth/rlp,
|
|
|
|
eth/common/eth_types_rlp,
|
|
|
|
ssz_serialization,
|
|
|
|
ssz_serialization/[proofs, merkleization],
|
2022-06-16 06:50:29 +00:00
|
|
|
../../common/common_types,
|
|
|
|
./history_content
|
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
export ssz_serialization, merkleization, proofs, eth_types_rlp
|
2022-06-16 06:50:29 +00:00
|
|
|
|
2022-08-24 20:12:56 +00:00
|
|
|
# Header Accumulator, as per specification:
|
|
|
|
# https://github.com/ethereum/portal-network-specs/blob/master/history-network.md#the-header-accumulator
|
2022-10-10 10:59:55 +00:00
|
|
|
# But with the adjustment to finish the accumulator at merge point.
|
2022-06-16 06:50:29 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
epochSize* = 8192 # blocks
|
2022-11-04 08:27:01 +00:00
|
|
|
# Allow this to be adjusted at compile time for testing. If more constants
|
2022-10-17 18:38:51 +00:00
|
|
|
# need to be adjusted we can add some presets file.
|
2022-10-10 10:59:55 +00:00
|
|
|
mergeBlockNumber* {.intdefine.}: uint64 = 15537394
|
|
|
|
|
|
|
|
# Note: This is like a ceil(mergeBlockNumber / epochSize)
|
|
|
|
# Could use ceilDiv(mergeBlockNumber, epochSize) in future versions
|
|
|
|
preMergeEpochs* = (mergeBlockNumber + epochSize - 1) div epochSize
|
2022-06-16 06:50:29 +00:00
|
|
|
|
2022-10-17 18:38:51 +00:00
|
|
|
# TODO:
|
2022-11-04 08:27:01 +00:00
|
|
|
# Currently disabled, because issue when testing with other
|
|
|
|
# `mergeBlockNumber`, but it could be used as value to double check on at
|
|
|
|
# merge block.
|
2022-10-17 18:38:51 +00:00
|
|
|
# TODO: Could also be used as value to actual finish the accumulator, instead
|
|
|
|
# of `mergeBlockNumber`, but:
|
|
|
|
# - Still need to store the actual `mergeBlockNumber` and run-time somewhere
|
|
|
|
# as it allows for each pre vs post merge block header checking.
|
|
|
|
# - Can't limit `historicalEpochs` SSZ list at `preMergeEpochs` value.
|
|
|
|
# - Should probably be stated in the portal network specs.
|
|
|
|
# TERMINAL_TOTAL_DIFFICULTY = u256"58750000000000000000000"
|
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
type
|
|
|
|
HeaderRecord* = object
|
|
|
|
blockHash*: BlockHash
|
|
|
|
totalDifficulty*: UInt256
|
|
|
|
|
|
|
|
EpochAccumulator* = List[HeaderRecord, epochSize]
|
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
# In the core code of Fluffy the `EpochAccumulator` type is solely used, as
|
|
|
|
# `hash_tree_root` is done either once or never on this object after
|
|
|
|
# serialization.
|
|
|
|
# However for the generation of the proofs for all the headers in an epoch, it
|
|
|
|
# needs to be run many times and the cached version of the SSZ list is
|
|
|
|
# obviously much faster, so this second type is added for this usage.
|
|
|
|
EpochAccumulatorCached* = HashList[HeaderRecord, epochSize]
|
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
Accumulator* = object
|
2022-10-10 10:59:55 +00:00
|
|
|
historicalEpochs*: List[Bytes32, int(preMergeEpochs)]
|
2022-06-16 06:50:29 +00:00
|
|
|
currentEpoch*: EpochAccumulator
|
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
FinishedAccumulator* = object
|
|
|
|
historicalEpochs*: List[Bytes32, int(preMergeEpochs)]
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
BlockEpochData* = object
|
|
|
|
epochHash*: Bytes32
|
|
|
|
blockRelativeIndex*: uint64
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-08-24 20:12:56 +00:00
|
|
|
func init*(T: type Accumulator): T =
|
|
|
|
Accumulator(
|
2022-10-10 10:59:55 +00:00
|
|
|
historicalEpochs: List[Bytes32, int(preMergeEpochs)].init(@[]),
|
2024-02-28 17:31:45 +00:00
|
|
|
currentEpoch: EpochAccumulator.init(@[]),
|
2022-08-04 06:34:53 +00:00
|
|
|
)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
func getEpochAccumulatorRoot*(headerRecords: openArray[HeaderRecord]): Digest =
|
2024-02-09 10:13:12 +00:00
|
|
|
let epochAccumulator = EpochAccumulator.init(@headerRecords)
|
|
|
|
|
|
|
|
hash_tree_root(epochAccumulator)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
func updateAccumulator*(a: var Accumulator, header: BlockHeader) =
|
|
|
|
doAssert(
|
2024-06-14 07:31:08 +00:00
|
|
|
header.number < mergeBlockNumber, "No post merge blocks for header accumulator"
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2022-10-10 10:59:55 +00:00
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
let lastTotalDifficulty =
|
|
|
|
if a.currentEpoch.len() == 0:
|
|
|
|
0.stuint(256)
|
|
|
|
else:
|
|
|
|
a.currentEpoch[^1].totalDifficulty
|
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
# TODO: It is a bit annoying to require an extra header + update call to
|
|
|
|
# finish an epoch. However, if we were to move this after adding the
|
|
|
|
# `HeaderRecord`, there would be no way to get the current total difficulty,
|
|
|
|
# unless another field is introduced in the `Accumulator` object.
|
2022-06-16 06:50:29 +00:00
|
|
|
if a.currentEpoch.len() == epochSize:
|
|
|
|
let epochHash = hash_tree_root(a.currentEpoch)
|
|
|
|
|
|
|
|
doAssert(a.historicalEpochs.add(epochHash.data))
|
|
|
|
a.currentEpoch = EpochAccumulator.init(@[])
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let headerRecord = HeaderRecord(
|
|
|
|
blockHash: header.blockHash(),
|
|
|
|
totalDifficulty: lastTotalDifficulty + header.difficulty,
|
|
|
|
)
|
2022-06-16 06:50:29 +00:00
|
|
|
|
|
|
|
let res = a.currentEpoch.add(headerRecord)
|
|
|
|
doAssert(res, "Can't fail because of currentEpoch length check")
|
|
|
|
|
2022-10-17 18:38:51 +00:00
|
|
|
func finishAccumulator*(a: var Accumulator): FinishedAccumulator =
|
|
|
|
# doAssert(a.currentEpoch[^2].totalDifficulty < TERMINAL_TOTAL_DIFFICULTY)
|
|
|
|
# doAssert(a.currentEpoch[^1].totalDifficulty >= TERMINAL_TOTAL_DIFFICULTY)
|
2022-10-10 10:59:55 +00:00
|
|
|
let epochHash = hash_tree_root(a.currentEpoch)
|
|
|
|
|
|
|
|
doAssert(a.historicalEpochs.add(epochHash.data))
|
|
|
|
|
2022-10-17 18:38:51 +00:00
|
|
|
FinishedAccumulator(historicalEpochs: a.historicalEpochs)
|
2022-06-16 06:50:29 +00:00
|
|
|
|
2022-06-23 19:00:59 +00:00
|
|
|
## Calls and helper calls for building header proofs and verifying headers
|
|
|
|
## against the Accumulator and the header proofs.
|
|
|
|
|
2022-08-24 20:12:56 +00:00
|
|
|
func getEpochIndex*(blockNumber: uint64): uint64 =
|
|
|
|
blockNumber div epochSize
|
2022-06-23 19:00:59 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
func getEpochIndex*(header: BlockHeader): uint64 =
|
2022-06-23 19:00:59 +00:00
|
|
|
## Get the index for the historical epochs
|
2024-06-14 07:31:08 +00:00
|
|
|
getEpochIndex(header.number)
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2023-04-11 12:06:45 +00:00
|
|
|
func getHeaderRecordIndex*(blockNumber: uint64, epochIndex: uint64): uint64 =
|
2022-08-04 06:34:53 +00:00
|
|
|
## Get the relative header index for the epoch accumulator
|
2022-08-24 20:12:56 +00:00
|
|
|
uint64(blockNumber - epochIndex * epochSize)
|
2022-06-23 19:00:59 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
func getHeaderRecordIndex*(header: BlockHeader, epochIndex: uint64): uint64 =
|
2022-06-23 19:00:59 +00:00
|
|
|
## Get the relative header index for the epoch accumulator
|
2024-06-14 07:31:08 +00:00
|
|
|
getHeaderRecordIndex(header.number, epochIndex)
|
2022-06-23 19:00:59 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
func isPreMerge*(blockNumber: uint64): bool =
|
|
|
|
blockNumber < mergeBlockNumber
|
|
|
|
|
|
|
|
func isPreMerge*(header: BlockHeader): bool =
|
2024-06-14 07:31:08 +00:00
|
|
|
isPreMerge(header.number)
|
2022-10-10 10:59:55 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
func verifyProof(
|
2024-02-28 17:31:45 +00:00
|
|
|
a: FinishedAccumulator, header: BlockHeader, proof: openArray[Digest]
|
|
|
|
): bool =
|
2022-06-23 19:00:59 +00:00
|
|
|
let
|
|
|
|
epochIndex = getEpochIndex(header)
|
|
|
|
epochAccumulatorHash = Digest(data: a.historicalEpochs[epochIndex])
|
|
|
|
|
|
|
|
leave = hash_tree_root(header.blockHash())
|
|
|
|
headerRecordIndex = getHeaderRecordIndex(header, epochIndex)
|
|
|
|
|
|
|
|
# TODO: Implement more generalized `get_generalized_index`
|
2024-02-28 17:31:45 +00:00
|
|
|
gIndex = GeneralizedIndex(epochSize * 2 * 2 + (headerRecordIndex * 2))
|
2022-06-23 19:00:59 +00:00
|
|
|
|
|
|
|
verify_merkle_multiproof(@[leave], proof, @[gIndex], epochAccumulatorHash)
|
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
func verifyAccumulatorProof*(
|
2024-02-28 17:31:45 +00:00
|
|
|
a: FinishedAccumulator, header: BlockHeader, proof: AccumulatorProof
|
|
|
|
): Result[void, string] =
|
2022-10-10 10:59:55 +00:00
|
|
|
if header.isPreMerge():
|
2022-11-04 08:27:01 +00:00
|
|
|
# Note: The proof is typed with correct depth, so no check on this is
|
|
|
|
# required here.
|
2022-10-10 10:59:55 +00:00
|
|
|
if a.verifyProof(header, proof):
|
2022-06-23 19:00:59 +00:00
|
|
|
ok()
|
|
|
|
else:
|
2022-10-10 10:59:55 +00:00
|
|
|
err("Proof verification failed")
|
2022-06-23 19:00:59 +00:00
|
|
|
else:
|
2022-10-10 10:59:55 +00:00
|
|
|
err("Cannot verify post merge header with accumulator proof")
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-11-04 08:27:01 +00:00
|
|
|
func verifyHeader*(
|
2024-02-28 17:31:45 +00:00
|
|
|
a: FinishedAccumulator, header: BlockHeader, proof: BlockHeaderProof
|
|
|
|
): Result[void, string] =
|
|
|
|
case proof.proofType
|
2022-11-04 08:27:01 +00:00
|
|
|
of BlockHeaderProofType.accumulatorProof:
|
|
|
|
a.verifyAccumulatorProof(header, proof.accumulatorProof)
|
|
|
|
of BlockHeaderProofType.none:
|
2023-01-17 13:47:22 +00:00
|
|
|
if header.isPreMerge():
|
|
|
|
err("Pre merge header requires AccumulatorProof")
|
|
|
|
else:
|
|
|
|
# TODO:
|
|
|
|
# Currently there is no proof solution for verifying headers post-merge.
|
|
|
|
# Skipping canonical verification will allow for nodes to push block data
|
|
|
|
# that is not part of the canonical chain.
|
|
|
|
# For now we accept this flaw as the focus lies on testing data
|
|
|
|
# availability up to the head of the chain.
|
|
|
|
ok()
|
2022-11-04 08:27:01 +00:00
|
|
|
|
|
|
|
func buildProof*(
|
2024-02-28 17:31:45 +00:00
|
|
|
header: BlockHeader, epochAccumulator: EpochAccumulator | EpochAccumulatorCached
|
|
|
|
): Result[AccumulatorProof, string] =
|
2022-11-04 08:27:01 +00:00
|
|
|
doAssert(header.isPreMerge(), "Must be pre merge header")
|
|
|
|
|
|
|
|
let
|
|
|
|
epochIndex = getEpochIndex(header)
|
|
|
|
headerRecordIndex = getHeaderRecordIndex(header, epochIndex)
|
|
|
|
|
|
|
|
# TODO: Implement more generalized `get_generalized_index`
|
2024-02-28 17:31:45 +00:00
|
|
|
gIndex = GeneralizedIndex(epochSize * 2 * 2 + (headerRecordIndex * 2))
|
2022-11-04 08:27:01 +00:00
|
|
|
|
|
|
|
var proof: AccumulatorProof
|
2024-02-28 17:31:45 +00:00
|
|
|
?epochAccumulator.build_proof(gIndex, proof)
|
2022-11-04 08:27:01 +00:00
|
|
|
|
|
|
|
ok(proof)
|
|
|
|
|
|
|
|
func buildHeaderWithProof*(
|
2024-02-28 17:31:45 +00:00
|
|
|
header: BlockHeader, epochAccumulator: EpochAccumulator | EpochAccumulatorCached
|
|
|
|
): Result[BlockHeaderWithProof, string] =
|
|
|
|
let proof = ?buildProof(header, epochAccumulator)
|
|
|
|
|
|
|
|
ok(
|
|
|
|
BlockHeaderWithProof(
|
|
|
|
header: ByteList.init(rlp.encode(header)), proof: BlockHeaderProof.init(proof)
|
|
|
|
)
|
|
|
|
)
|
2022-11-04 08:27:01 +00:00
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
func getBlockEpochDataForBlockNumber*(
|
2024-02-28 17:31:45 +00:00
|
|
|
a: FinishedAccumulator, bn: UInt256
|
|
|
|
): Result[BlockEpochData, string] =
|
2022-08-04 06:34:53 +00:00
|
|
|
let blockNumber = bn.truncate(uint64)
|
|
|
|
|
2022-10-10 10:59:55 +00:00
|
|
|
if blockNumber.isPreMerge:
|
2022-08-04 06:34:53 +00:00
|
|
|
let epochIndex = getEpochIndex(blockNumber)
|
2022-10-10 10:59:55 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
ok(
|
|
|
|
BlockEpochData(
|
|
|
|
epochHash: a.historicalEpochs[epochIndex],
|
|
|
|
blockRelativeIndex: getHeaderRecordIndex(blockNumber, epochIndex),
|
2022-10-10 10:59:55 +00:00
|
|
|
)
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2022-10-10 10:59:55 +00:00
|
|
|
else:
|
|
|
|
err("Block number is post merge: " & $blockNumber)
|