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
|
|
|
|
|
|
|
import
|
|
|
|
std/[typetraits],
|
2021-10-21 20:27:06 +00:00
|
|
|
ssz_serialization/codec,
|
2022-01-06 07:38:40 +00:00
|
|
|
./datatypes/base
|
2021-08-18 18:57:58 +00:00
|
|
|
|
2022-06-20 05:38:56 +00:00
|
|
|
from ./datatypes/altair import
|
2022-12-19 12:01:49 +00:00
|
|
|
ParticipationFlags, EpochParticipationFlags
|
2022-06-20 05:38:56 +00:00
|
|
|
|
|
|
|
export codec, base, typetraits, EpochParticipationFlags
|
2021-08-18 18:57:58 +00:00
|
|
|
|
|
|
|
# Coding and decoding of SSZ to spec-specific types
|
|
|
|
|
2022-03-11 12:04:08 +00:00
|
|
|
template toSszType*(v: Slot|Epoch|SyncCommitteePeriod): auto = uint64(v)
|
2021-08-18 18:57:58 +00:00
|
|
|
template toSszType*(v: BlsCurveType): auto = toRaw(v)
|
|
|
|
template toSszType*(v: ForkDigest|GraffitiBytes): auto = distinctBase(v)
|
|
|
|
template toSszType*(v: Version): auto = distinctBase(v)
|
2022-01-06 07:38:40 +00:00
|
|
|
template toSszType*(v: JustificationBits): auto = distinctBase(v)
|
2022-12-19 12:01:49 +00:00
|
|
|
template toSszType*(v: EpochParticipationFlags): auto = asList v
|
2021-08-18 18:57:58 +00:00
|
|
|
|
2023-09-22 21:44:57 +00:00
|
|
|
func fromSszBytes*(
|
|
|
|
T: type GraffitiBytes, data: openArray[byte]): T {.raises: [SszError].} =
|
2021-08-18 18:57:58 +00:00
|
|
|
if data.len != sizeof(result):
|
|
|
|
raiseIncorrectSize T
|
|
|
|
copyMem(result.addr, unsafeAddr data[0], sizeof(result))
|
|
|
|
|
|
|
|
template fromSszBytes*(T: type Slot, bytes: openArray[byte]): T =
|
|
|
|
T fromSszBytes(uint64, bytes)
|
|
|
|
|
|
|
|
template fromSszBytes*(T: type Epoch, bytes: openArray[byte]): T =
|
|
|
|
T fromSszBytes(uint64, bytes)
|
|
|
|
|
2022-03-11 12:04:08 +00:00
|
|
|
template fromSszBytes*(T: type SyncCommitteePeriod, bytes: openArray[byte]): T =
|
|
|
|
T fromSszBytes(uint64, bytes)
|
|
|
|
|
2023-09-22 21:44:57 +00:00
|
|
|
func fromSszBytes*(
|
|
|
|
T: type ForkDigest, bytes: openArray[byte]): T {.raises: [SszError].} =
|
2021-08-18 18:57:58 +00:00
|
|
|
if bytes.len != sizeof(result):
|
|
|
|
raiseIncorrectSize T
|
|
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
|
|
|
|
2023-09-22 21:44:57 +00:00
|
|
|
func fromSszBytes*(
|
|
|
|
T: type Version, bytes: openArray[byte]): T {.raises: [SszError].} =
|
2021-08-18 18:57:58 +00:00
|
|
|
if bytes.len != sizeof(result):
|
|
|
|
raiseIncorrectSize T
|
|
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
2022-01-06 07:38:40 +00:00
|
|
|
|
2023-09-22 21:44:57 +00:00
|
|
|
func fromSszBytes*(
|
|
|
|
T: type JustificationBits, bytes: openArray[byte]
|
|
|
|
): T {.raises: [SszError].} =
|
2022-01-06 07:38:40 +00:00
|
|
|
if bytes.len != sizeof(result):
|
|
|
|
raiseIncorrectSize T
|
|
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
2022-06-20 05:38:56 +00:00
|
|
|
|
2023-09-22 21:44:57 +00:00
|
|
|
func fromSszBytes*(
|
|
|
|
T: type EpochParticipationFlags, bytes: openArray[byte]
|
|
|
|
): T {.raises: [SszError].} =
|
2022-12-19 12:01:49 +00:00
|
|
|
# TODO https://github.com/nim-lang/Nim/issues/21123
|
|
|
|
let tmp = cast[ptr List[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT]](addr result)
|
|
|
|
readSszValue(bytes, tmp[])
|