2021-08-18 18:57:58 +00:00
|
|
|
# beacon_chain
|
2023-01-20 14:14:37 +00:00
|
|
|
# Copyright (c) 2018-2023 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
|
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
|
|
|
|
|
|
|
|
proc readAndUpdateRoot(data: openArray[byte], val: var auto, updateRoot = true) {.
|
2023-08-25 09:29:07 +00:00
|
|
|
raises: [MalformedSszError, SszSizeMismatchError].} =
|
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)
|
2021-08-18 21:03:20 +00:00
|
|
|
|
|
|
|
template readSszBytes*(
|
|
|
|
data: openArray[byte], val: var auto, updateRoot: bool) =
|
|
|
|
readSszValue(data, val)
|
|
|
|
|
|
|
|
func readSszBytes(T: type, data: openArray[byte], updateRoot = true): T {.
|
2023-08-25 09:29:07 +00:00
|
|
|
raises: [MalformedSszError, SszSizeMismatchError].} =
|
2021-08-18 21:03:20 +00:00
|
|
|
var res: T
|
|
|
|
readSszBytes(data, res, updateRoot)
|
|
|
|
res
|