2021-08-18 18:57:58 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-08-18 18:57:58 +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-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2021-08-18 18:57:58 +00:00
|
|
|
|
2021-08-18 21:03:20 +00:00
|
|
|
# This module exports SSZ.encode and SSZ.decode for spec types - don't use
|
|
|
|
# ssz_serialization directly! To bypass root updates, use `readSszBytes`
|
|
|
|
# without involving SSZ!
|
2021-08-18 18:57:58 +00:00
|
|
|
import
|
2021-10-21 20:27:06 +00:00
|
|
|
ssz_serialization,
|
2021-08-18 18:57:58 +00:00
|
|
|
./ssz_codec,
|
2022-10-27 06:29:24 +00:00
|
|
|
./datatypes/[phase0, altair, bellatrix, capella],
|
2021-08-18 18:57:58 +00:00
|
|
|
./eth2_merkleization
|
|
|
|
|
2023-02-20 08:45:49 +00:00
|
|
|
from ./datatypes/deneb import SignedBeaconBlock, TrustedSignedBeaconBlock
|
2024-04-19 19:20:45 +00:00
|
|
|
from ./datatypes/electra import SignedBeaconBlock, TrustedSignedBeaconBlock
|
2024-11-13 03:29:14 +00:00
|
|
|
from ./datatypes/fulu import SignedBeaconBlock, TrustedSignedBeaconBlock
|
2022-12-05 21:36:53 +00:00
|
|
|
|
2021-08-18 18:57:58 +00:00
|
|
|
export phase0, altair, ssz_codec, ssz_serialization, eth2_merkleization
|
|
|
|
|
2023-09-22 21:44:57 +00:00
|
|
|
proc readAndUpdateRoot(
|
|
|
|
data: openArray[byte], val: var auto, updateRoot = true
|
|
|
|
) {.raises: [SszError].} =
|
2021-08-18 18:57:58 +00:00
|
|
|
readSszValue(data, val)
|
|
|
|
if updateRoot:
|
|
|
|
val.root = hash_tree_root(val.message)
|
|
|
|
|
|
|
|
# TODO this is an ugly way to get a stronger match than the generic readSszBytes
|
|
|
|
# and avoid ambiguities - `var` + typeclasses are problematic
|
|
|
|
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var phase0.SignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var phase0.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var altair.SignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var altair.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
2021-09-11 08:01:05 +00:00
|
|
|
template readSszBytes*(
|
2022-01-06 11:25:35 +00:00
|
|
|
data: openArray[byte], val: var bellatrix.SignedBeaconBlock, updateRoot = true) =
|
2021-09-11 08:01:05 +00:00
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
2022-01-06 11:25:35 +00:00
|
|
|
data: openArray[byte], val: var bellatrix.TrustedSignedBeaconBlock, updateRoot = true) =
|
2021-09-11 08:01:05 +00:00
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
2022-10-27 06:29:24 +00:00
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var capella.SignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var capella.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
2022-12-05 21:36:53 +00:00
|
|
|
template readSszBytes*(
|
2023-02-20 08:45:49 +00:00
|
|
|
data: openArray[byte], val: var deneb.SignedBeaconBlock, updateRoot = true) =
|
2022-12-05 21:36:53 +00:00
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
2023-02-20 08:45:49 +00:00
|
|
|
data: openArray[byte], val: var deneb.TrustedSignedBeaconBlock, updateRoot = true) =
|
2022-12-05 21:36:53 +00:00
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
2024-04-19 19:20:45 +00:00
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var electra.SignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var electra.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
2024-11-13 03:29:14 +00:00
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var fulu.SignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var fulu.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
|
|
readAndUpdateRoot(data, val, updateRoot)
|
2021-08-18 21:03:20 +00:00
|
|
|
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var auto, updateRoot: bool) =
|
|
|
|
readSszValue(data, val)
|
|
|
|
|
2023-09-22 21:44:57 +00:00
|
|
|
func readSszBytes(
|
|
|
|
T: type, data: openArray[byte], updateRoot = true
|
|
|
|
): T {.raises: [SszError].} =
|
2021-08-18 21:03:20 +00:00
|
|
|
var res: T
|
|
|
|
readSszBytes(data, res, updateRoot)
|
|
|
|
res
|
2024-02-21 19:06:19 +00:00
|
|
|
|
|
|
|
proc fromSszBytes*(
|
|
|
|
T: type HashedValidatorPubKey, bytes: openArray[byte]
|
|
|
|
): T {.raises: [SszError].} =
|
|
|
|
let
|
|
|
|
key = ValidatorPubKey.fromSszBytes(bytes)
|
|
|
|
|
2024-04-19 19:20:45 +00:00
|
|
|
HashedValidatorPubKey.init(key)
|