2024-01-26 22:38:12 +00:00
|
|
|
# fluffy
|
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-05-11 17:08:34 +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.
|
|
|
|
|
|
|
|
#
|
2024-05-24 21:15:04 +00:00
|
|
|
# Implementation of the beacon state historical_summaries provided with a Merkle
|
|
|
|
# proof that can be verified against the right beacon state root.
|
|
|
|
#
|
|
|
|
# As per spec:
|
|
|
|
# https://github.com/ethereum/portal-network-specs/blob/master/beacon-chain/beacon-network.md#historicalsummaries
|
2023-05-11 17:08:34 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-10-24 18:31:17 +00:00
|
|
|
import stew/arrayops, results, beacon_chain/spec/forks, ../../common/common_types
|
2023-05-11 17:08:34 +00:00
|
|
|
|
|
|
|
export results
|
|
|
|
|
|
|
|
type
|
|
|
|
HistoricalSummaries* = HashList[HistoricalSummary, Limit HISTORICAL_ROOTS_LIMIT]
|
|
|
|
HistoricalSummariesProof* = array[5, Digest]
|
|
|
|
HistoricalSummariesWithProof* = object
|
2024-05-24 21:15:04 +00:00
|
|
|
epoch*: Epoch
|
|
|
|
# TODO:
|
|
|
|
# can epoch instead of slot cause any issues? E.g. to verify with right state?
|
|
|
|
# To revise when we fully implement this in validateHistoricalSummaries,
|
|
|
|
# for now follow specification.
|
|
|
|
# finalized_slot*: Slot
|
2024-01-26 22:38:12 +00:00
|
|
|
historical_summaries*: HistoricalSummaries
|
|
|
|
proof*: HistoricalSummariesProof
|
2023-05-11 17:08:34 +00:00
|
|
|
|
2024-05-24 21:15:04 +00:00
|
|
|
# TODO: prefixing the summaries with the forkDigest is currently not necessary
|
|
|
|
# and might never be. Perhaps we should drop this until it is needed. Propose
|
|
|
|
# spec change?
|
|
|
|
func encodeSsz*(obj: HistoricalSummariesWithProof, forkDigest: ForkDigest): seq[byte] =
|
|
|
|
var res: seq[byte]
|
|
|
|
res.add(distinctBase(forkDigest))
|
|
|
|
res.add(SSZ.encode(obj))
|
|
|
|
|
|
|
|
res
|
|
|
|
|
|
|
|
func decodeSsz*(
|
|
|
|
forkDigests: ForkDigests,
|
|
|
|
data: openArray[byte],
|
|
|
|
T: type HistoricalSummariesWithProof,
|
|
|
|
): Result[HistoricalSummariesWithProof, string] =
|
|
|
|
if len(data) < 4:
|
|
|
|
return
|
|
|
|
Result[HistoricalSummariesWithProof, string].err("Not enough data for forkDigest")
|
|
|
|
|
|
|
|
let
|
|
|
|
forkDigest = ForkDigest(array[4, byte].initCopyFrom(data))
|
|
|
|
contextFork = forkDigests.consensusForkForDigest(forkDigest).valueOr:
|
|
|
|
return Result[HistoricalSummariesWithProof, string].err("Unknown fork")
|
|
|
|
|
|
|
|
if contextFork > ConsensusFork.Bellatrix:
|
|
|
|
# There is only one version of HistoricalSummaries starting since Capella
|
|
|
|
decodeSsz(data.toOpenArray(4, len(data) - 1), HistoricalSummariesWithProof)
|
|
|
|
else:
|
|
|
|
Result[HistoricalSummariesWithProof, string].err(
|
|
|
|
"Invalid Fork for HistoricalSummaries"
|
|
|
|
)
|
|
|
|
|
2023-05-11 17:08:34 +00:00
|
|
|
func buildProof*(
|
2024-02-28 17:31:45 +00:00
|
|
|
state: ForkedHashedBeaconState
|
|
|
|
): Result[HistoricalSummariesProof, string] =
|
2023-05-11 17:08:34 +00:00
|
|
|
let gIndex = GeneralizedIndex(59) # 31 + 28 = 59
|
|
|
|
|
|
|
|
var proof: HistoricalSummariesProof
|
|
|
|
withState(state):
|
2024-02-28 17:31:45 +00:00
|
|
|
?forkyState.data.build_proof(gIndex, proof)
|
2023-05-11 17:08:34 +00:00
|
|
|
|
|
|
|
ok(proof)
|
|
|
|
|
|
|
|
func verifyProof*(
|
|
|
|
historical_summaries: HistoricalSummaries,
|
|
|
|
proof: HistoricalSummariesProof,
|
2024-02-28 17:31:45 +00:00
|
|
|
stateRoot: Digest,
|
|
|
|
): bool =
|
2023-05-11 17:08:34 +00:00
|
|
|
let
|
|
|
|
gIndex = GeneralizedIndex(59)
|
|
|
|
leave = hash_tree_root(historical_summaries)
|
|
|
|
|
|
|
|
verify_merkle_multiproof(@[leave], proof, @[gIndex], stateRoot)
|
2024-01-26 22:38:12 +00:00
|
|
|
|
|
|
|
func verifyProof*(
|
2024-02-28 17:31:45 +00:00
|
|
|
summariesWithProof: HistoricalSummariesWithProof, stateRoot: Digest
|
|
|
|
): bool =
|
2024-01-26 22:38:12 +00:00
|
|
|
verifyProof(
|
2024-02-28 17:31:45 +00:00
|
|
|
summariesWithProof.historical_summaries, summariesWithProof.proof, stateRoot
|
|
|
|
)
|