2022-06-16 06:50:29 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2022-08-01 19:00:21 +00:00
|
|
|
std/hashes,
|
2022-09-03 18:15:35 +00:00
|
|
|
eth/common/eth_types_rlp,
|
2022-06-16 06:50:29 +00:00
|
|
|
ssz_serialization, ssz_serialization/[proofs, merkleization],
|
|
|
|
../../common/common_types,
|
|
|
|
./history_content
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
export ssz_serialization, merkleization, proofs
|
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-06-16 06:50:29 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
epochSize* = 8192 # blocks
|
|
|
|
maxHistoricalEpochs = 131072 # 2^17
|
|
|
|
|
|
|
|
type
|
|
|
|
HeaderRecord* = object
|
|
|
|
blockHash*: BlockHash
|
|
|
|
totalDifficulty*: UInt256
|
|
|
|
|
|
|
|
EpochAccumulator* = List[HeaderRecord, epochSize]
|
|
|
|
|
|
|
|
Accumulator* = object
|
|
|
|
historicalEpochs*: List[Bytes32, maxHistoricalEpochs]
|
|
|
|
currentEpoch*: EpochAccumulator
|
|
|
|
|
2022-08-04 06:34:53 +00:00
|
|
|
BlockHashResultType* = enum
|
|
|
|
BHash, HEpoch, UnknownBlockNumber
|
|
|
|
|
|
|
|
BlockHashResult* = object
|
|
|
|
case kind*: BlockHashResultType
|
|
|
|
of BHash:
|
|
|
|
blockHash*: BlockHash
|
|
|
|
of HEpoch:
|
|
|
|
epochHash*: Bytes32
|
|
|
|
epochIndex*: uint64
|
|
|
|
blockRelativeIndex*: uint64
|
|
|
|
of UnknownBlockNumber:
|
|
|
|
discard
|
|
|
|
|
2022-08-24 20:12:56 +00:00
|
|
|
func init*(T: type Accumulator): T =
|
|
|
|
Accumulator(
|
2022-08-04 06:34:53 +00:00
|
|
|
historicalEpochs: List[Bytes32, maxHistoricalEpochs].init(@[]),
|
2022-08-24 20:12:56 +00:00
|
|
|
currentEpoch: EpochAccumulator.init(@[])
|
2022-08-04 06:34:53 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
func updateAccumulator*(a: var Accumulator, header: BlockHeader) =
|
|
|
|
let lastTotalDifficulty =
|
|
|
|
if a.currentEpoch.len() == 0:
|
|
|
|
0.stuint(256)
|
|
|
|
else:
|
|
|
|
a.currentEpoch[^1].totalDifficulty
|
|
|
|
|
|
|
|
if a.currentEpoch.len() == epochSize:
|
|
|
|
let epochHash = hash_tree_root(a.currentEpoch)
|
|
|
|
|
|
|
|
doAssert(a.historicalEpochs.add(epochHash.data))
|
|
|
|
a.currentEpoch = EpochAccumulator.init(@[])
|
|
|
|
|
|
|
|
let headerRecord =
|
|
|
|
HeaderRecord(
|
|
|
|
blockHash: header.blockHash(),
|
|
|
|
totalDifficulty: lastTotalDifficulty + header.difficulty)
|
|
|
|
|
|
|
|
let res = a.currentEpoch.add(headerRecord)
|
|
|
|
doAssert(res, "Can't fail because of currentEpoch length check")
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
func hash*(a: Accumulator): hashes.Hash =
|
|
|
|
# TODO: This is used for the CountTable but it will be expensive.
|
|
|
|
hash(hash_tree_root(a).data)
|
2022-06-16 06:50:29 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
func buildAccumulator*(headers: seq[BlockHeader]): Accumulator =
|
2022-06-16 06:50:29 +00:00
|
|
|
var accumulator: Accumulator
|
|
|
|
for header in headers:
|
|
|
|
updateAccumulator(accumulator, header)
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
accumulator
|
2022-06-16 06:50:29 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
func buildAccumulatorData*(headers: seq[BlockHeader]):
|
|
|
|
seq[(ContentKey, EpochAccumulator)] =
|
2022-06-16 06:50:29 +00:00
|
|
|
var accumulator: Accumulator
|
2022-08-01 19:00:21 +00:00
|
|
|
var epochAccumulators: seq[(ContentKey, EpochAccumulator)]
|
2022-06-16 06:50:29 +00:00
|
|
|
for header in headers:
|
|
|
|
updateAccumulator(accumulator, header)
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
if accumulator.currentEpoch.len() == epochSize:
|
|
|
|
let
|
|
|
|
rootHash = accumulator.currentEpoch.hash_tree_root()
|
|
|
|
key = ContentKey(
|
|
|
|
contentType: epochAccumulator,
|
|
|
|
epochAccumulatorKey: EpochAccumulatorKey(
|
|
|
|
epochHash: rootHash))
|
2022-06-16 06:50:29 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
epochAccumulators.add((key, accumulator.currentEpoch))
|
2022-06-16 06:50:29 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
epochAccumulators
|
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 inCurrentEpoch*(blockNumber: uint64, a: Accumulator): bool =
|
2022-09-09 11:12:09 +00:00
|
|
|
# Note:
|
|
|
|
# Block numbers start at 0, so historical epochs are set as:
|
|
|
|
# 0 -> 8191 -> len = 1 * 8192
|
|
|
|
# 8192 -> 16383 -> len = 2 * 8192
|
|
|
|
# ...
|
|
|
|
# A block number is in the current epoch if it is bigger than the last block
|
|
|
|
# number in the last historical epoch. Which is the same as being equal or
|
|
|
|
# bigger than current length of historical epochs * epochSize.
|
|
|
|
blockNumber >= uint64(a.historicalEpochs.len() * epochSize)
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-06-23 19:00:59 +00:00
|
|
|
func inCurrentEpoch*(header: BlockHeader, a: Accumulator): bool =
|
|
|
|
let blockNumber = header.blockNumber.truncate(uint64)
|
2022-08-24 20:12:56 +00:00
|
|
|
blockNumber.inCurrentEpoch(a)
|
2022-06-23 19:00:59 +00:00
|
|
|
|
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-08-04 06:34:53 +00:00
|
|
|
let blockNumber = header.blockNumber.truncate(uint64)
|
2022-06-23 19:00:59 +00:00
|
|
|
## Get the index for the historical epochs
|
2022-08-24 20:12:56 +00:00
|
|
|
getEpochIndex(blockNumber)
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-08-24 20:12:56 +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
|
2022-08-24 20:12:56 +00:00
|
|
|
getHeaderRecordIndex(header.blockNumber.truncate(uint64), epochIndex)
|
2022-06-23 19:00:59 +00:00
|
|
|
|
|
|
|
func verifyProof*(
|
2022-08-24 20:12:56 +00:00
|
|
|
a: Accumulator, 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`
|
|
|
|
gIndex = GeneralizedIndex(epochSize*2*2 + (headerRecordIndex*2))
|
|
|
|
|
|
|
|
verify_merkle_multiproof(@[leave], proof, @[gIndex], epochAccumulatorHash)
|
|
|
|
|
2022-08-24 20:12:56 +00:00
|
|
|
func verifyHeader*(
|
2022-08-01 19:00:21 +00:00
|
|
|
accumulator: Accumulator, header: BlockHeader, proof: Option[seq[Digest]]):
|
2022-06-23 19:00:59 +00:00
|
|
|
Result[void, string] =
|
|
|
|
if header.inCurrentEpoch(accumulator):
|
|
|
|
let blockNumber = header.blockNumber.truncate(uint64)
|
|
|
|
let relIndex = blockNumber - uint64(accumulator.historicalEpochs.len()) * epochSize
|
|
|
|
|
|
|
|
if relIndex > uint64(accumulator.currentEpoch.len() - 1):
|
|
|
|
return err("Blocknumber ahead of accumulator")
|
|
|
|
|
|
|
|
if accumulator.currentEpoch[relIndex].blockHash == header.blockHash():
|
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
err("Header not part of canonical chain")
|
|
|
|
else:
|
|
|
|
if proof.isSome():
|
2022-08-24 20:12:56 +00:00
|
|
|
if accumulator.verifyProof(header, proof.get):
|
2022-06-23 19:00:59 +00:00
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
err("Proof verification failed")
|
|
|
|
else:
|
|
|
|
err("Need proof to verify header")
|
2022-08-04 06:34:53 +00:00
|
|
|
|
|
|
|
func getHeaderHashForBlockNumber*(a: Accumulator, bn: UInt256): BlockHashResult=
|
|
|
|
let blockNumber = bn.truncate(uint64)
|
2022-08-24 20:12:56 +00:00
|
|
|
if blockNumber.inCurrentEpoch(a):
|
2022-08-04 06:34:53 +00:00
|
|
|
let relIndex = blockNumber - uint64(a.historicalEpochs.len()) * epochSize
|
|
|
|
|
|
|
|
if relIndex > uint64(a.currentEpoch.len() - 1):
|
|
|
|
return BlockHashResult(kind: UnknownBlockNumber)
|
|
|
|
|
|
|
|
return BlockHashResult(kind: BHash, blockHash: a.currentEpoch[relIndex].blockHash)
|
|
|
|
else:
|
|
|
|
let epochIndex = getEpochIndex(blockNumber)
|
|
|
|
return BlockHashResult(
|
|
|
|
kind: HEpoch,
|
|
|
|
epochHash: a.historicalEpochs[epochIndex],
|
|
|
|
epochIndex: epochIndex,
|
|
|
|
blockRelativeIndex: getHeaderRecordIndex(blockNumber, epochIndex)
|
|
|
|
)
|