2021-06-11 17:51:46 +00:00
|
|
|
# beacon_chain
|
2022-01-05 14:24:15 +00:00
|
|
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
2021-06-11 17:51:46 +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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2022-01-05 14:24:15 +00:00
|
|
|
stew/assign2,
|
2021-06-11 17:51:46 +00:00
|
|
|
chronicles,
|
|
|
|
../extras,
|
2022-03-16 15:00:18 +00:00
|
|
|
"."/[block_id, eth2_merkleization, eth2_ssz_serialization, presets],
|
2022-01-06 11:25:35 +00:00
|
|
|
./datatypes/[phase0, altair, bellatrix]
|
2021-06-11 17:51:46 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
export
|
2022-03-16 15:00:18 +00:00
|
|
|
extras, block_id, phase0, altair, bellatrix, eth2_merkleization,
|
|
|
|
eth2_ssz_serialization, presets
|
2021-11-05 07:34:34 +00:00
|
|
|
|
|
|
|
# This file contains helpers for dealing with forks - we have two ways we can
|
|
|
|
# deal with forks:
|
|
|
|
# * generics - this means using the static typing and differentiating forks
|
|
|
|
# at compile time - this is preferred in fork-specific code where the fork
|
|
|
|
# is known up-front, for example spec functions.
|
|
|
|
# * variants - this means using a variant object and determining the fork at
|
|
|
|
# runtime - this carries the obvious risk and complexity of dealing with
|
|
|
|
# runtime checking, but is of course needed for external data that may be
|
|
|
|
# of any fork kind.
|
|
|
|
#
|
|
|
|
# For generics, we define `Forky*` type classes that cover "similar" objects
|
|
|
|
# across forks - for variants, they're called `Forked*` instead.
|
|
|
|
# See withXxx and `init` for convenient ways of moving between these two worlds.
|
|
|
|
# A clever programmer would use templates, macros and dark magic to create all
|
|
|
|
# these types and converters :)
|
2021-08-12 13:08:20 +00:00
|
|
|
|
2021-06-11 17:51:46 +00:00
|
|
|
type
|
2021-10-18 16:37:27 +00:00
|
|
|
BeaconStateFork* {.pure.} = enum
|
|
|
|
Phase0,
|
|
|
|
Altair,
|
2022-01-04 09:45:38 +00:00
|
|
|
Bellatrix
|
2021-06-11 17:51:46 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
ForkyBeaconState* =
|
|
|
|
phase0.BeaconState |
|
|
|
|
altair.BeaconState |
|
2022-01-06 11:25:35 +00:00
|
|
|
bellatrix.BeaconState
|
2021-11-05 07:34:34 +00:00
|
|
|
|
|
|
|
ForkyHashedBeaconState* =
|
|
|
|
phase0.HashedBeaconState |
|
|
|
|
altair.HashedBeaconState |
|
2022-01-06 11:25:35 +00:00
|
|
|
bellatrix.HashedBeaconState
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2021-06-11 17:51:46 +00:00
|
|
|
ForkedHashedBeaconState* = object
|
2021-10-18 16:37:27 +00:00
|
|
|
case kind*: BeaconStateFork
|
2022-01-24 16:23:13 +00:00
|
|
|
of BeaconStateFork.Phase0: phase0Data*: phase0.HashedBeaconState
|
|
|
|
of BeaconStateFork.Altair: altairData*: altair.HashedBeaconState
|
|
|
|
of BeaconStateFork.Bellatrix: bellatrixData*: bellatrix.HashedBeaconState
|
2021-06-11 17:51:46 +00:00
|
|
|
|
2021-07-07 09:09:47 +00:00
|
|
|
BeaconBlockFork* {.pure.} = enum
|
|
|
|
Phase0
|
|
|
|
Altair
|
2022-01-04 09:45:38 +00:00
|
|
|
Bellatrix
|
2021-07-07 09:09:47 +00:00
|
|
|
|
2022-02-11 15:58:33 +00:00
|
|
|
ForkyBeaconBlockBody* =
|
|
|
|
phase0.BeaconBlockBody |
|
|
|
|
altair.BeaconBlockBody |
|
|
|
|
bellatrix.BeaconBlockBody
|
|
|
|
|
|
|
|
ForkySigVerifiedBeaconBlockBody* =
|
|
|
|
phase0.SigVerifiedBeaconBlockBody |
|
|
|
|
altair.SigVerifiedBeaconBlockBody |
|
|
|
|
bellatrix.SigVerifiedBeaconBlockBody
|
|
|
|
|
|
|
|
ForkyTrustedBeaconBlockBody* =
|
|
|
|
phase0.TrustedBeaconBlockBody |
|
|
|
|
altair.TrustedBeaconBlockBody |
|
|
|
|
bellatrix.TrustedBeaconBlockBody
|
|
|
|
|
|
|
|
SomeForkyBeaconBlockBody* =
|
|
|
|
ForkyBeaconBlockBody |
|
|
|
|
ForkySigVerifiedBeaconBlockBody |
|
|
|
|
ForkyTrustedBeaconBlockBody
|
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
ForkyBeaconBlock* =
|
|
|
|
phase0.BeaconBlock |
|
|
|
|
altair.BeaconBlock |
|
2022-01-06 11:25:35 +00:00
|
|
|
bellatrix.BeaconBlock
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-01-17 11:19:58 +00:00
|
|
|
ForkySigVerifiedBeaconBlock* =
|
|
|
|
phase0.SigVerifiedBeaconBlock |
|
|
|
|
altair.SigVerifiedBeaconBlock |
|
|
|
|
bellatrix.SigVerifiedBeaconBlock
|
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
ForkyTrustedBeaconBlock* =
|
|
|
|
phase0.TrustedBeaconBlock |
|
|
|
|
altair.TrustedBeaconBlock |
|
2022-01-06 11:25:35 +00:00
|
|
|
bellatrix.TrustedBeaconBlock
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-01-30 16:51:04 +00:00
|
|
|
SomeForkyBeaconBlock* =
|
|
|
|
ForkyBeaconBlock |
|
|
|
|
ForkySigVerifiedBeaconBlock |
|
|
|
|
ForkyTrustedBeaconBlock
|
|
|
|
|
2021-08-27 09:00:06 +00:00
|
|
|
ForkedBeaconBlock* = object
|
|
|
|
case kind*: BeaconBlockFork
|
2022-01-24 16:23:13 +00:00
|
|
|
of BeaconBlockFork.Phase0: phase0Data*: phase0.BeaconBlock
|
|
|
|
of BeaconBlockFork.Altair: altairData*: altair.BeaconBlock
|
|
|
|
of BeaconBlockFork.Bellatrix: bellatrixData*: bellatrix.BeaconBlock
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-05-17 16:55:03 +00:00
|
|
|
Web3SignerForkedBeaconBlock* {.borrow: `.`} = distinct ForkedBeaconBlock
|
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
ForkedTrustedBeaconBlock* = object
|
|
|
|
case kind*: BeaconBlockFork
|
2022-01-24 16:23:13 +00:00
|
|
|
of BeaconBlockFork.Phase0: phase0Data*: phase0.TrustedBeaconBlock
|
|
|
|
of BeaconBlockFork.Altair: altairData*: altair.TrustedBeaconBlock
|
|
|
|
of BeaconBlockFork.Bellatrix: bellatrixData*: bellatrix.TrustedBeaconBlock
|
2021-11-05 07:34:34 +00:00
|
|
|
|
|
|
|
ForkySignedBeaconBlock* =
|
|
|
|
phase0.SignedBeaconBlock |
|
|
|
|
altair.SignedBeaconBlock |
|
2022-01-06 11:25:35 +00:00
|
|
|
bellatrix.SignedBeaconBlock
|
2021-08-27 09:00:06 +00:00
|
|
|
|
2021-07-07 09:09:47 +00:00
|
|
|
ForkedSignedBeaconBlock* = object
|
|
|
|
case kind*: BeaconBlockFork
|
2022-01-24 16:23:13 +00:00
|
|
|
of BeaconBlockFork.Phase0: phase0Data*: phase0.SignedBeaconBlock
|
|
|
|
of BeaconBlockFork.Altair: altairData*: altair.SignedBeaconBlock
|
|
|
|
of BeaconBlockFork.Bellatrix: bellatrixData*: bellatrix.SignedBeaconBlock
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
ForkySigVerifiedSignedBeaconBlock* =
|
|
|
|
phase0.SigVerifiedSignedBeaconBlock |
|
|
|
|
altair.SigVerifiedSignedBeaconBlock |
|
|
|
|
bellatrix.SigVerifiedSignedBeaconBlock
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
ForkyMsgTrustedSignedBeaconBlock* =
|
|
|
|
phase0.MsgTrustedSignedBeaconBlock |
|
|
|
|
altair.MsgTrustedSignedBeaconBlock |
|
|
|
|
bellatrix.MsgTrustedSignedBeaconBlock
|
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
ForkyTrustedSignedBeaconBlock* =
|
|
|
|
phase0.TrustedSignedBeaconBlock |
|
|
|
|
altair.TrustedSignedBeaconBlock |
|
2022-01-06 11:25:35 +00:00
|
|
|
bellatrix.TrustedSignedBeaconBlock
|
2021-07-07 09:09:47 +00:00
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
ForkedMsgTrustedSignedBeaconBlock* = object
|
|
|
|
case kind*: BeaconBlockFork
|
|
|
|
of BeaconBlockFork.Phase0: phase0Data*: phase0.MsgTrustedSignedBeaconBlock
|
|
|
|
of BeaconBlockFork.Altair: altairData*: altair.MsgTrustedSignedBeaconBlock
|
|
|
|
of BeaconBlockFork.Bellatrix: bellatrixData*: bellatrix.MsgTrustedSignedBeaconBlock
|
|
|
|
|
2021-07-07 09:09:47 +00:00
|
|
|
ForkedTrustedSignedBeaconBlock* = object
|
|
|
|
case kind*: BeaconBlockFork
|
2022-01-24 16:23:13 +00:00
|
|
|
of BeaconBlockFork.Phase0: phase0Data*: phase0.TrustedSignedBeaconBlock
|
|
|
|
of BeaconBlockFork.Altair: altairData*: altair.TrustedSignedBeaconBlock
|
|
|
|
of BeaconBlockFork.Bellatrix: bellatrixData*: bellatrix.TrustedSignedBeaconBlock
|
2021-07-07 09:09:47 +00:00
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
SomeForkySignedBeaconBlock* =
|
|
|
|
ForkySignedBeaconBlock |
|
|
|
|
ForkySigVerifiedSignedBeaconBlock |
|
2022-06-10 14:16:37 +00:00
|
|
|
ForkyMsgTrustedSignedBeaconBlock |
|
2022-01-11 10:01:54 +00:00
|
|
|
ForkyTrustedSignedBeaconBlock
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
EpochInfoFork* {.pure.} = enum
|
|
|
|
Phase0
|
|
|
|
Altair
|
|
|
|
|
|
|
|
ForkedEpochInfo* = object
|
|
|
|
case kind*: EpochInfoFork
|
2021-11-05 07:34:34 +00:00
|
|
|
of EpochInfoFork.Phase0: phase0Data*: phase0.EpochInfo
|
|
|
|
of EpochInfoFork.Altair: altairData*: altair.EpochInfo
|
2021-10-13 14:24:36 +00:00
|
|
|
|
|
|
|
ForkyEpochInfo* = phase0.EpochInfo | altair.EpochInfo
|
|
|
|
|
2021-10-07 13:19:47 +00:00
|
|
|
ForkDigests* = object
|
2022-01-05 14:24:15 +00:00
|
|
|
phase0*: ForkDigest
|
|
|
|
altair*: ForkDigest
|
|
|
|
bellatrix*: ForkDigest
|
2022-06-03 14:42:40 +00:00
|
|
|
capella*: ForkDigest
|
2022-01-05 14:24:15 +00:00
|
|
|
sharding*: ForkDigest
|
2021-07-07 09:09:47 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
template toFork*[T: phase0.BeaconState | phase0.HashedBeaconState](
|
|
|
|
t: type T): BeaconStateFork =
|
|
|
|
BeaconStateFork.Phase0
|
|
|
|
template toFork*[T: altair.BeaconState | altair.HashedBeaconState](
|
|
|
|
t: type T): BeaconStateFork =
|
|
|
|
BeaconStateFork.Altair
|
2022-01-06 11:25:35 +00:00
|
|
|
template toFork*[T: bellatrix.BeaconState | bellatrix.HashedBeaconState](
|
2021-11-05 07:34:34 +00:00
|
|
|
t: type T): BeaconStateFork =
|
2022-01-04 09:45:38 +00:00
|
|
|
BeaconStateFork.Bellatrix
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-04-08 20:11:37 +00:00
|
|
|
# TODO these cause stack overflows due to large temporaries getting allocated
|
|
|
|
# template init*(T: type ForkedHashedBeaconState, data: phase0.HashedBeaconState): T =
|
|
|
|
# T(kind: BeaconStateFork.Phase0, phase0Data: data)
|
|
|
|
# template init*(T: type ForkedHashedBeaconState, data: altair.HashedBeaconState): T =
|
|
|
|
# T(kind: BeaconStateFork.Altair, altairData: data)
|
|
|
|
# template init*(T: type ForkedHashedBeaconState, data: bellatrix.HashedBeaconState): T =
|
|
|
|
# T(kind: BeaconStateFork.Bellatrix, bellatrixData: data)
|
2021-07-07 09:09:47 +00:00
|
|
|
|
2021-08-27 09:00:06 +00:00
|
|
|
template init*(T: type ForkedBeaconBlock, blck: phase0.BeaconBlock): T =
|
2021-10-18 16:37:27 +00:00
|
|
|
T(kind: BeaconBlockFork.Phase0, phase0Data: blck)
|
2021-08-27 09:00:06 +00:00
|
|
|
template init*(T: type ForkedBeaconBlock, blck: altair.BeaconBlock): T =
|
2021-10-18 16:37:27 +00:00
|
|
|
T(kind: BeaconBlockFork.Altair, altairData: blck)
|
2022-01-06 11:25:35 +00:00
|
|
|
template init*(T: type ForkedBeaconBlock, blck: bellatrix.BeaconBlock): T =
|
2022-01-24 16:23:13 +00:00
|
|
|
T(kind: BeaconBlockFork.Bellatrix, bellatrixData: blck)
|
2021-08-27 09:00:06 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
template init*(T: type ForkedTrustedBeaconBlock, blck: phase0.TrustedBeaconBlock): T =
|
|
|
|
T(kind: BeaconBlockFork.Phase0, phase0Data: blck)
|
|
|
|
template init*(T: type ForkedTrustedBeaconBlock, blck: altair.TrustedBeaconBlock): T =
|
|
|
|
T(kind: BeaconBlockFork.Altair, altairData: blck)
|
2022-01-06 11:25:35 +00:00
|
|
|
template init*(T: type ForkedTrustedBeaconBlock, blck: bellatrix.TrustedBeaconBlock): T =
|
2022-01-24 16:23:13 +00:00
|
|
|
T(kind: BeaconBlockFork.Bellatrix, bellatrixData: blck)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2021-07-14 12:18:52 +00:00
|
|
|
template init*(T: type ForkedSignedBeaconBlock, blck: phase0.SignedBeaconBlock): T =
|
2021-10-18 16:37:27 +00:00
|
|
|
T(kind: BeaconBlockFork.Phase0, phase0Data: blck)
|
2021-07-14 12:18:52 +00:00
|
|
|
template init*(T: type ForkedSignedBeaconBlock, blck: altair.SignedBeaconBlock): T =
|
2021-10-18 16:37:27 +00:00
|
|
|
T(kind: BeaconBlockFork.Altair, altairData: blck)
|
2022-01-06 11:25:35 +00:00
|
|
|
template init*(T: type ForkedSignedBeaconBlock, blck: bellatrix.SignedBeaconBlock): T =
|
2022-01-24 16:23:13 +00:00
|
|
|
T(kind: BeaconBlockFork.Bellatrix, bellatrixData: blck)
|
2021-07-14 12:18:52 +00:00
|
|
|
|
2021-08-27 09:00:06 +00:00
|
|
|
template init*(T: type ForkedSignedBeaconBlock, forked: ForkedBeaconBlock,
|
|
|
|
blockRoot: Eth2Digest, signature: ValidatorSig): T =
|
|
|
|
case forked.kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
|
|
|
T(kind: BeaconBlockFork.Phase0,
|
2021-10-18 16:37:27 +00:00
|
|
|
phase0Data: phase0.SignedBeaconBlock(message: forked.phase0Data,
|
2021-11-05 07:34:34 +00:00
|
|
|
root: blockRoot,
|
|
|
|
signature: signature))
|
2021-08-27 09:00:06 +00:00
|
|
|
of BeaconBlockFork.Altair:
|
|
|
|
T(kind: BeaconBlockFork.Altair,
|
2021-10-18 16:37:27 +00:00
|
|
|
altairData: altair.SignedBeaconBlock(message: forked.altairData,
|
2021-11-05 07:34:34 +00:00
|
|
|
root: blockRoot,
|
|
|
|
signature: signature))
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconBlockFork.Bellatrix:
|
|
|
|
T(kind: BeaconBlockFork.Bellatrix,
|
2022-01-24 16:23:13 +00:00
|
|
|
bellatrixData: bellatrix.SignedBeaconBlock(message: forked.bellatrixData,
|
|
|
|
root: blockRoot,
|
|
|
|
signature: signature))
|
2021-08-27 09:00:06 +00:00
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
template init*(T: type ForkedMsgTrustedSignedBeaconBlock, blck: phase0.MsgTrustedSignedBeaconBlock): T =
|
|
|
|
T(kind: BeaconBlockFork.Phase0, phase0Data: blck)
|
|
|
|
template init*(T: type ForkedMsgTrustedSignedBeaconBlock, blck: altair.MsgTrustedSignedBeaconBlock): T =
|
|
|
|
T(kind: BeaconBlockFork.Altair, altairData: blck)
|
|
|
|
template init*(T: type ForkedMsgTrustedSignedBeaconBlock, blck: bellatrix.MsgTrustedSignedBeaconBlock): T =
|
|
|
|
T(kind: BeaconBlockFork.Bellatrix, bellatrixData: blck)
|
|
|
|
|
2021-07-14 12:18:52 +00:00
|
|
|
template init*(T: type ForkedTrustedSignedBeaconBlock, blck: phase0.TrustedSignedBeaconBlock): T =
|
2021-10-18 16:37:27 +00:00
|
|
|
T(kind: BeaconBlockFork.Phase0, phase0Data: blck)
|
2021-07-14 12:18:52 +00:00
|
|
|
template init*(T: type ForkedTrustedSignedBeaconBlock, blck: altair.TrustedSignedBeaconBlock): T =
|
2021-10-18 16:37:27 +00:00
|
|
|
T(kind: BeaconBlockFork.Altair, altairData: blck)
|
2022-01-06 11:25:35 +00:00
|
|
|
template init*(T: type ForkedTrustedSignedBeaconBlock, blck: bellatrix.TrustedSignedBeaconBlock): T =
|
2022-01-24 16:23:13 +00:00
|
|
|
T(kind: BeaconBlockFork.Bellatrix, bellatrixData: blck)
|
2021-07-14 12:18:52 +00:00
|
|
|
|
2022-06-20 05:53:39 +00:00
|
|
|
template toString*(kind: BeaconBlockFork): string =
|
|
|
|
case kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
|
|
|
"phase0"
|
|
|
|
of BeaconBlockFork.Altair:
|
|
|
|
"altair"
|
|
|
|
of BeaconBlockFork.Bellatrix:
|
|
|
|
"bellatrix"
|
|
|
|
|
|
|
|
template toString*(kind: BeaconStateFork): string =
|
|
|
|
case kind
|
|
|
|
of BeaconStateFork.Phase0:
|
|
|
|
"phase0"
|
|
|
|
of BeaconStateFork.Altair:
|
|
|
|
"altair"
|
|
|
|
of BeaconStateFork.Bellatrix:
|
|
|
|
"bellatrix"
|
|
|
|
|
2022-02-28 12:58:34 +00:00
|
|
|
template toFork*[T:
|
2022-07-04 20:35:33 +00:00
|
|
|
phase0.BeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
phase0.SignedBeaconBlock |
|
2022-07-04 20:35:33 +00:00
|
|
|
phase0.TrustedBeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
phase0.SigVerifiedSignedBeaconBlock |
|
2022-06-10 14:16:37 +00:00
|
|
|
phase0.MsgTrustedSignedBeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
phase0.TrustedSignedBeaconBlock](
|
2021-11-05 07:34:34 +00:00
|
|
|
t: type T): BeaconBlockFork =
|
|
|
|
BeaconBlockFork.Phase0
|
2022-02-28 12:58:34 +00:00
|
|
|
template toFork*[T:
|
2022-07-04 20:35:33 +00:00
|
|
|
altair.BeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
altair.SignedBeaconBlock |
|
2022-07-04 20:35:33 +00:00
|
|
|
altair.TrustedBeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
altair.SigVerifiedSignedBeaconBlock |
|
2022-06-10 14:16:37 +00:00
|
|
|
altair.MsgTrustedSignedBeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
altair.TrustedSignedBeaconBlock](
|
2021-11-05 07:34:34 +00:00
|
|
|
t: type T): BeaconBlockFork =
|
|
|
|
BeaconBlockFork.Altair
|
2022-02-28 12:58:34 +00:00
|
|
|
template toFork*[T:
|
2022-07-04 20:35:33 +00:00
|
|
|
bellatrix.BeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
bellatrix.SignedBeaconBlock |
|
2022-07-04 20:35:33 +00:00
|
|
|
bellatrix.TrustedBeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
bellatrix.SigVerifiedSignedBeaconBlock |
|
2022-06-10 14:16:37 +00:00
|
|
|
bellatrix.MsgTrustedSignedBeaconBlock |
|
2022-02-28 12:58:34 +00:00
|
|
|
bellatrix.TrustedSignedBeaconBlock](
|
2021-11-05 07:34:34 +00:00
|
|
|
t: type T): BeaconBlockFork =
|
2022-01-04 09:45:38 +00:00
|
|
|
BeaconBlockFork.Bellatrix
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
template init*(T: type ForkedEpochInfo, info: phase0.EpochInfo): T =
|
2021-10-18 16:37:27 +00:00
|
|
|
T(kind: EpochInfoFork.Phase0, phase0Data: info)
|
2021-10-13 14:24:36 +00:00
|
|
|
template init*(T: type ForkedEpochInfo, info: altair.EpochInfo): T =
|
2021-10-18 16:37:27 +00:00
|
|
|
T(kind: EpochInfoFork.Altair, altairData: info)
|
2021-10-13 14:24:36 +00:00
|
|
|
|
2021-09-28 18:08:03 +00:00
|
|
|
template withState*(x: ForkedHashedBeaconState, body: untyped): untyped =
|
2021-10-18 16:37:27 +00:00
|
|
|
case x.kind
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Bellatrix:
|
|
|
|
const stateFork {.inject, used.} = BeaconStateFork.Bellatrix
|
2022-01-24 16:23:13 +00:00
|
|
|
template state: untyped {.inject, used.} = x.bellatrixData
|
2021-09-28 18:08:03 +00:00
|
|
|
body
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Altair:
|
2021-12-05 17:32:41 +00:00
|
|
|
const stateFork {.inject, used.} = BeaconStateFork.Altair
|
|
|
|
template state: untyped {.inject, used.} = x.altairData
|
2021-09-28 18:08:03 +00:00
|
|
|
body
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Phase0:
|
2021-12-05 17:32:41 +00:00
|
|
|
const stateFork {.inject, used.} = BeaconStateFork.Phase0
|
|
|
|
template state: untyped {.inject, used.} = x.phase0Data
|
2021-09-28 18:08:03 +00:00
|
|
|
body
|
2021-06-11 17:51:46 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
template withEpochInfo*(x: ForkedEpochInfo, body: untyped): untyped =
|
|
|
|
case x.kind
|
|
|
|
of EpochInfoFork.Phase0:
|
2021-12-29 02:50:49 +00:00
|
|
|
const infoFork {.inject.} = EpochInfoFork.Phase0
|
2021-10-18 16:37:27 +00:00
|
|
|
template info: untyped {.inject.} = x.phase0Data
|
2021-10-13 14:24:36 +00:00
|
|
|
body
|
|
|
|
of EpochInfoFork.Altair:
|
2021-12-29 02:50:49 +00:00
|
|
|
const infoFork {.inject.} = EpochInfoFork.Altair
|
2021-10-18 16:37:27 +00:00
|
|
|
template info: untyped {.inject.} = x.altairData
|
2021-10-13 14:24:36 +00:00
|
|
|
body
|
|
|
|
|
|
|
|
template withEpochInfo*(
|
|
|
|
state: phase0.BeaconState, x: var ForkedEpochInfo, body: untyped): untyped =
|
|
|
|
x.kind = EpochInfoFork.Phase0
|
2021-10-18 16:37:27 +00:00
|
|
|
template info: untyped {.inject.} = x.phase0Data
|
2021-10-13 14:24:36 +00:00
|
|
|
body
|
|
|
|
|
|
|
|
template withEpochInfo*(
|
2022-01-06 11:25:35 +00:00
|
|
|
state: altair.BeaconState | bellatrix.BeaconState, x: var ForkedEpochInfo,
|
2021-10-13 14:24:36 +00:00
|
|
|
body: untyped): untyped =
|
|
|
|
x.kind = EpochInfoFork.Altair
|
2021-10-18 16:37:27 +00:00
|
|
|
template info: untyped {.inject.} = x.altairData
|
2021-10-13 14:24:36 +00:00
|
|
|
body
|
|
|
|
|
2021-06-11 17:51:46 +00:00
|
|
|
func assign*(tgt: var ForkedHashedBeaconState, src: ForkedHashedBeaconState) =
|
2021-10-18 16:37:27 +00:00
|
|
|
if tgt.kind == src.kind:
|
|
|
|
case tgt.kind
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Bellatrix:
|
2022-01-24 16:23:13 +00:00
|
|
|
assign(tgt.bellatrixData, src.bellatrixData):
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Altair:
|
2022-01-24 16:23:13 +00:00
|
|
|
assign(tgt.altairData, src.altairData):
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Phase0:
|
2022-01-24 16:23:13 +00:00
|
|
|
assign(tgt.phase0Data, src.phase0Data):
|
2021-06-11 17:51:46 +00:00
|
|
|
else:
|
|
|
|
# Ensure case object and discriminator get updated simultaneously, even
|
|
|
|
# with nimOldCaseObjects. This is infrequent.
|
|
|
|
tgt = src
|
|
|
|
|
2021-09-28 18:08:03 +00:00
|
|
|
template getStateField*(x: ForkedHashedBeaconState, y: untyped): untyped =
|
2021-09-22 20:06:50 +00:00
|
|
|
# The use of `unsafeAddr` avoids excessive copying in certain situations, e.g.,
|
|
|
|
# ```
|
2022-05-10 10:03:40 +00:00
|
|
|
# for index, validator in getStateField(stateData.data, validators):
|
2021-09-22 20:06:50 +00:00
|
|
|
# ```
|
|
|
|
# Without `unsafeAddr`, the `validators` list would be copied to a temporary variable.
|
2021-10-18 16:37:27 +00:00
|
|
|
(case x.kind
|
2022-01-24 16:23:13 +00:00
|
|
|
of BeaconStateFork.Bellatrix: unsafeAddr x.bellatrixData.data.y
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Altair: unsafeAddr x.altairData.data.y
|
|
|
|
of BeaconStateFork.Phase0: unsafeAddr x.phase0Data.data.y)[]
|
2021-06-11 17:51:46 +00:00
|
|
|
|
2021-09-28 18:08:03 +00:00
|
|
|
func getStateRoot*(x: ForkedHashedBeaconState): Eth2Digest =
|
|
|
|
withState(x): state.root
|
2021-06-17 17:13:14 +00:00
|
|
|
|
|
|
|
func setStateRoot*(x: var ForkedHashedBeaconState, root: Eth2Digest) =
|
2021-09-28 18:08:03 +00:00
|
|
|
withState(x): state.root = root
|
2021-08-27 09:00:06 +00:00
|
|
|
|
2021-10-04 08:31:21 +00:00
|
|
|
func stateForkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): BeaconStateFork =
|
|
|
|
## Return the current fork for the given epoch.
|
|
|
|
static:
|
2022-01-04 09:45:38 +00:00
|
|
|
doAssert BeaconStateFork.Bellatrix > BeaconStateFork.Altair
|
|
|
|
doAssert BeaconStateFork.Altair > BeaconStateFork.Phase0
|
2021-10-04 08:31:21 +00:00
|
|
|
doAssert GENESIS_EPOCH == 0
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
if epoch >= cfg.BELLATRIX_FORK_EPOCH: BeaconStateFork.Bellatrix
|
|
|
|
elif epoch >= cfg.ALTAIR_FORK_EPOCH: BeaconStateFork.Altair
|
|
|
|
else: BeaconStateFork.Phase0
|
2021-10-04 08:31:21 +00:00
|
|
|
|
2021-10-18 12:32:54 +00:00
|
|
|
func blockForkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): BeaconBlockFork =
|
|
|
|
## Return the current fork for the given epoch.
|
2022-05-23 12:02:54 +00:00
|
|
|
if epoch >= cfg.BELLATRIX_FORK_EPOCH: BeaconBlockFork.Bellatrix
|
|
|
|
elif epoch >= cfg.ALTAIR_FORK_EPOCH: BeaconBlockFork.Altair
|
|
|
|
else: BeaconBlockFork.Phase0
|
|
|
|
|
|
|
|
func stateForkForDigest*(
|
|
|
|
forkDigests: ForkDigests, forkDigest: ForkDigest): Opt[BeaconStateFork] =
|
|
|
|
if forkDigest == forkDigests.bellatrix:
|
|
|
|
ok BeaconStateFork.Bellatrix
|
|
|
|
elif forkDigest == forkDigests.altair:
|
|
|
|
ok BeaconStateFork.Altair
|
|
|
|
elif forkDigest == forkDigests.phase0:
|
|
|
|
ok BeaconStateFork.Phase0
|
|
|
|
else:
|
|
|
|
err()
|
2021-10-18 12:32:54 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
func atStateFork*(
|
|
|
|
forkDigests: ForkDigests, stateFork: BeaconStateFork): ForkDigest =
|
|
|
|
case stateFork
|
|
|
|
of BeaconStateFork.Bellatrix:
|
|
|
|
forkDigests.bellatrix
|
|
|
|
of BeaconStateFork.Altair:
|
|
|
|
forkDigests.altair
|
|
|
|
of BeaconStateFork.Phase0:
|
|
|
|
forkDigests.phase0
|
|
|
|
|
|
|
|
template atEpoch*(
|
|
|
|
forkDigests: ForkDigests, epoch: Epoch, cfg: RuntimeConfig): ForkDigest =
|
|
|
|
forkDigests.atStateFork(cfg.stateForkAtEpoch(epoch))
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
template asSigned*(
|
|
|
|
x: ForkedMsgTrustedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock): ForkedSignedBeaconBlock =
|
2021-07-14 12:18:52 +00:00
|
|
|
isomorphicCast[ForkedSignedBeaconBlock](x)
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
template asMsgTrusted*(
|
|
|
|
x: ForkedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock): ForkedMsgTrustedSignedBeaconBlock =
|
|
|
|
isomorphicCast[ForkedMsgTrustedSignedBeaconBlock](x)
|
|
|
|
|
|
|
|
template asTrusted*(
|
|
|
|
x: ForkedSignedBeaconBlock |
|
|
|
|
ForkedMsgTrustedSignedBeaconBlock): ForkedTrustedSignedBeaconBlock =
|
2021-07-14 12:18:52 +00:00
|
|
|
isomorphicCast[ForkedTrustedSignedBeaconBlock](x)
|
|
|
|
|
2021-10-06 17:05:06 +00:00
|
|
|
template withBlck*(
|
2022-05-17 16:55:03 +00:00
|
|
|
x: ForkedBeaconBlock | Web3SignerForkedBeaconBlock |
|
2022-06-10 14:16:37 +00:00
|
|
|
ForkedSignedBeaconBlock | ForkedMsgTrustedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock,
|
2021-10-06 17:05:06 +00:00
|
|
|
body: untyped): untyped =
|
2021-07-14 12:18:52 +00:00
|
|
|
case x.kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
2021-12-05 17:32:41 +00:00
|
|
|
const stateFork {.inject, used.} = BeaconStateFork.Phase0
|
2021-10-18 16:37:27 +00:00
|
|
|
template blck: untyped {.inject.} = x.phase0Data
|
2021-07-14 12:18:52 +00:00
|
|
|
body
|
|
|
|
of BeaconBlockFork.Altair:
|
2021-12-05 17:32:41 +00:00
|
|
|
const stateFork {.inject, used.} = BeaconStateFork.Altair
|
2021-10-18 16:37:27 +00:00
|
|
|
template blck: untyped {.inject.} = x.altairData
|
2021-07-14 12:18:52 +00:00
|
|
|
body
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconBlockFork.Bellatrix:
|
|
|
|
const stateFork {.inject, used.} = BeaconStateFork.Bellatrix
|
2022-01-24 16:23:13 +00:00
|
|
|
template blck: untyped {.inject.} = x.bellatrixData
|
2021-09-27 14:22:58 +00:00
|
|
|
body
|
2021-07-14 12:18:52 +00:00
|
|
|
|
2021-09-28 18:08:03 +00:00
|
|
|
func proposer_index*(x: ForkedBeaconBlock): uint64 =
|
|
|
|
withBlck(x): blck.proposer_index
|
|
|
|
|
|
|
|
func hash_tree_root*(x: ForkedBeaconBlock): Eth2Digest =
|
|
|
|
withBlck(x): hash_tree_root(blck)
|
|
|
|
|
2022-05-17 16:55:03 +00:00
|
|
|
func hash_tree_root*(x: Web3SignerForkedBeaconBlock): Eth2Digest {.borrow.}
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
template getForkedBlockField*(
|
|
|
|
x: ForkedSignedBeaconBlock |
|
|
|
|
ForkedMsgTrustedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock,
|
|
|
|
y: untyped): untyped =
|
2021-09-28 18:08:03 +00:00
|
|
|
# unsafeAddr avoids a copy of the field in some cases
|
|
|
|
(case x.kind
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconBlockFork.Phase0: unsafeAddr x.phase0Data.message.y
|
|
|
|
of BeaconBlockFork.Altair: unsafeAddr x.altairData.message.y
|
2022-01-24 16:23:13 +00:00
|
|
|
of BeaconBlockFork.Bellatrix: unsafeAddr x.bellatrixData.message.y)[]
|
2021-07-14 12:18:52 +00:00
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
template signature*(x: ForkedSignedBeaconBlock |
|
|
|
|
ForkedMsgTrustedSignedBeaconBlock): ValidatorSig =
|
2021-07-14 12:18:52 +00:00
|
|
|
withBlck(x): blck.signature
|
|
|
|
|
|
|
|
template signature*(x: ForkedTrustedSignedBeaconBlock): TrustedSig =
|
|
|
|
withBlck(x): blck.signature
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
template root*(x: ForkedSignedBeaconBlock |
|
|
|
|
ForkedMsgTrustedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock): Eth2Digest =
|
2021-07-14 12:18:52 +00:00
|
|
|
withBlck(x): blck.root
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
template slot*(x: ForkedSignedBeaconBlock |
|
|
|
|
ForkedMsgTrustedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock): Slot =
|
2021-09-28 18:08:03 +00:00
|
|
|
withBlck(x): blck.message.slot
|
2021-07-07 09:09:47 +00:00
|
|
|
|
2021-08-27 09:00:06 +00:00
|
|
|
template shortLog*(x: ForkedBeaconBlock): auto =
|
|
|
|
withBlck(x): shortLog(blck)
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
template shortLog*(x: ForkedSignedBeaconBlock |
|
|
|
|
ForkedMsgTrustedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock): auto =
|
2021-08-27 09:00:06 +00:00
|
|
|
withBlck(x): shortLog(blck)
|
2021-07-07 09:09:47 +00:00
|
|
|
|
2021-08-27 09:00:06 +00:00
|
|
|
chronicles.formatIt ForkedBeaconBlock: it.shortLog
|
2021-07-14 12:18:52 +00:00
|
|
|
chronicles.formatIt ForkedSignedBeaconBlock: it.shortLog
|
2022-06-10 14:16:37 +00:00
|
|
|
chronicles.formatIt ForkedMsgTrustedSignedBeaconBlock: it.shortLog
|
2021-07-14 12:18:52 +00:00
|
|
|
chronicles.formatIt ForkedTrustedSignedBeaconBlock: it.shortLog
|
2021-08-09 12:54:45 +00:00
|
|
|
|
2021-10-06 17:05:06 +00:00
|
|
|
template withStateAndBlck*(
|
|
|
|
s: ForkedHashedBeaconState,
|
|
|
|
b: ForkedBeaconBlock | ForkedSignedBeaconBlock |
|
2022-06-10 14:16:37 +00:00
|
|
|
ForkedMsgTrustedSignedBeaconBlock |
|
2021-10-06 17:05:06 +00:00
|
|
|
ForkedTrustedSignedBeaconBlock,
|
|
|
|
body: untyped): untyped =
|
2021-10-18 16:37:27 +00:00
|
|
|
case s.kind
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Bellatrix:
|
|
|
|
const stateFork {.inject.} = BeaconStateFork.Bellatrix
|
2022-01-24 16:23:13 +00:00
|
|
|
template state: untyped {.inject.} = s.bellatrixData
|
|
|
|
template blck: untyped {.inject.} = b.bellatrixData
|
2021-10-06 17:05:06 +00:00
|
|
|
body
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Altair:
|
|
|
|
const stateFork {.inject.} = BeaconStateFork.Altair
|
|
|
|
template state: untyped {.inject.} = s.altairData
|
|
|
|
template blck: untyped {.inject.} = b.altairData
|
2021-10-06 17:05:06 +00:00
|
|
|
body
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Phase0:
|
|
|
|
const stateFork {.inject.} = BeaconStateFork.Phase0
|
|
|
|
template state: untyped {.inject.} = s.phase0Data
|
|
|
|
template blck: untyped {.inject.} = b.phase0Data
|
2021-10-06 17:05:06 +00:00
|
|
|
body
|
|
|
|
|
2022-03-07 13:56:43 +00:00
|
|
|
func toBeaconBlockHeader*(
|
|
|
|
blck: SomeForkyBeaconBlock): BeaconBlockHeader =
|
|
|
|
## Reduce a given `BeaconBlock` to its `BeaconBlockHeader`.
|
|
|
|
BeaconBlockHeader(
|
|
|
|
slot: blck.slot,
|
|
|
|
proposer_index: blck.proposer_index,
|
|
|
|
parent_root: blck.parent_root,
|
|
|
|
state_root: blck.state_root,
|
|
|
|
body_root: blck.body.hash_tree_root())
|
|
|
|
|
|
|
|
template toBeaconBlockHeader*(
|
|
|
|
blck: SomeForkySignedBeaconBlock): BeaconBlockHeader =
|
|
|
|
## Reduce a given `SignedBeaconBlock` to its `BeaconBlockHeader`.
|
|
|
|
blck.message.toBeaconBlockHeader
|
|
|
|
|
|
|
|
template toBeaconBlockHeader*(
|
2022-06-10 14:16:37 +00:00
|
|
|
blckParam: ForkedMsgTrustedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock): BeaconBlockHeader =
|
|
|
|
## Reduce a given signed beacon block to its `BeaconBlockHeader`.
|
|
|
|
withBlck(blckParam): blck.toBeaconBlockHeader()
|
2022-03-07 13:56:43 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
func genesisFork*(cfg: RuntimeConfig): Fork =
|
|
|
|
Fork(
|
|
|
|
previous_version: cfg.GENESIS_FORK_VERSION,
|
|
|
|
current_version: cfg.GENESIS_FORK_VERSION,
|
|
|
|
epoch: GENESIS_EPOCH)
|
|
|
|
|
|
|
|
func altairFork*(cfg: RuntimeConfig): Fork =
|
|
|
|
Fork(
|
|
|
|
previous_version: cfg.GENESIS_FORK_VERSION,
|
|
|
|
current_version: cfg.ALTAIR_FORK_VERSION,
|
|
|
|
epoch: cfg.ALTAIR_FORK_EPOCH)
|
|
|
|
|
2022-01-24 16:23:13 +00:00
|
|
|
func bellatrixFork*(cfg: RuntimeConfig): Fork =
|
2021-11-05 07:34:34 +00:00
|
|
|
# TODO in theory, the altair + merge forks could be in same epoch, so the
|
|
|
|
# previous fork version would be the GENESIS_FORK_VERSION
|
|
|
|
Fork(
|
|
|
|
previous_version: cfg.ALTAIR_FORK_VERSION,
|
2022-01-20 08:30:33 +00:00
|
|
|
current_version: cfg.BELLATRIX_FORK_VERSION,
|
2022-02-02 13:06:55 +00:00
|
|
|
epoch: cfg.BELLATRIX_FORK_EPOCH)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-02-17 11:53:55 +00:00
|
|
|
func forkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Fork =
|
2021-10-04 08:31:21 +00:00
|
|
|
case cfg.stateForkAtEpoch(epoch)
|
2022-01-24 16:23:13 +00:00
|
|
|
of BeaconStateFork.Bellatrix: cfg.bellatrixFork
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Altair: cfg.altairFork
|
|
|
|
of BeaconStateFork.Phase0: cfg.genesisFork
|
2021-08-09 12:54:45 +00:00
|
|
|
|
2022-02-17 11:53:55 +00:00
|
|
|
func forkVersionAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Version =
|
2021-10-04 08:31:21 +00:00
|
|
|
case cfg.stateForkAtEpoch(epoch)
|
2022-01-20 08:30:33 +00:00
|
|
|
of BeaconStateFork.Bellatrix: cfg.BELLATRIX_FORK_VERSION
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Altair: cfg.ALTAIR_FORK_VERSION
|
|
|
|
of BeaconStateFork.Phase0: cfg.GENESIS_FORK_VERSION
|
2021-08-09 12:54:45 +00:00
|
|
|
|
2022-02-17 11:53:55 +00:00
|
|
|
func nextForkEpochAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Epoch =
|
2021-10-04 08:31:21 +00:00
|
|
|
case cfg.stateForkAtEpoch(epoch)
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Bellatrix: FAR_FUTURE_EPOCH
|
2022-02-02 13:06:55 +00:00
|
|
|
of BeaconStateFork.Altair: cfg.BELLATRIX_FORK_EPOCH
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Phase0: cfg.ALTAIR_FORK_EPOCH
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2021-12-21 14:24:23 +00:00
|
|
|
func getForkSchedule*(cfg: RuntimeConfig): array[3, Fork] =
|
2021-08-23 10:41:48 +00:00
|
|
|
## This procedure returns list of known and/or scheduled forks.
|
|
|
|
##
|
|
|
|
## This procedure is used by HTTP REST framework and validator client.
|
|
|
|
##
|
|
|
|
## NOTE: Update this procedure when new fork will be scheduled.
|
2022-01-24 16:23:13 +00:00
|
|
|
[cfg.genesisFork(), cfg.altairFork(), cfg.bellatrixFork()]
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2021-11-10 11:39:08 +00:00
|
|
|
type
|
|
|
|
# The first few fields of a state, shared across all forks
|
|
|
|
BeaconStateHeader = object
|
|
|
|
genesis_time: uint64
|
|
|
|
genesis_validators_root: Eth2Digest
|
2022-01-11 10:01:54 +00:00
|
|
|
slot: Slot
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
func readSszForkedHashedBeaconState*(cfg: RuntimeConfig, data: openArray[byte]):
|
2021-11-05 07:34:34 +00:00
|
|
|
ForkedHashedBeaconState {.raises: [Defect, SszError].} =
|
2021-11-10 11:39:08 +00:00
|
|
|
## Helper to read a header from bytes when it's not certain what kind of state
|
2021-11-05 07:34:34 +00:00
|
|
|
## it is - this happens for example when loading an SSZ state from command
|
2021-11-10 11:39:08 +00:00
|
|
|
## line
|
|
|
|
if data.len() < sizeof(BeaconStateHeader):
|
|
|
|
raise (ref MalformedSszError)(msg: "Not enough data for BeaconState header")
|
|
|
|
let header = SSZ.decode(
|
|
|
|
data.toOpenArray(0, sizeof(BeaconStateHeader) - 1),
|
|
|
|
BeaconStateHeader)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-02-16 15:44:04 +00:00
|
|
|
# TODO https://github.com/nim-lang/Nim/issues/19357
|
2021-11-10 11:39:08 +00:00
|
|
|
result = ForkedHashedBeaconState(
|
2022-01-11 10:01:54 +00:00
|
|
|
kind: cfg.stateForkAtEpoch(header.slot.epoch()))
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2021-11-10 11:39:08 +00:00
|
|
|
withState(result):
|
|
|
|
readSszBytes(data, state.data)
|
|
|
|
state.root = hash_tree_root(state.data)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2021-11-10 11:39:08 +00:00
|
|
|
type
|
|
|
|
ForkedBeaconBlockHeader = object
|
|
|
|
message*: uint32 # message offset
|
|
|
|
signature*: ValidatorSig
|
|
|
|
slot: Slot # start of BeaconBlock
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
func readSszForkedSignedBeaconBlock*(
|
2021-11-10 11:39:08 +00:00
|
|
|
cfg: RuntimeConfig, data: openArray[byte]):
|
2022-01-06 07:38:40 +00:00
|
|
|
ForkedSignedBeaconBlock {.raises: [Defect, SszError].} =
|
2021-11-10 11:39:08 +00:00
|
|
|
## Helper to read a header from bytes when it's not certain what kind of block
|
|
|
|
## it is
|
2021-12-10 02:41:57 +00:00
|
|
|
if data.len() < sizeof(ForkedBeaconBlockHeader):
|
2021-11-10 11:39:08 +00:00
|
|
|
raise (ref MalformedSszError)(msg: "Not enough data for SignedBeaconBlock header")
|
|
|
|
let header = SSZ.decode(
|
|
|
|
data.toOpenArray(0, sizeof(ForkedBeaconBlockHeader) - 1),
|
|
|
|
ForkedBeaconBlockHeader)
|
2021-12-10 02:41:57 +00:00
|
|
|
|
2022-02-16 15:44:04 +00:00
|
|
|
# TODO https://github.com/nim-lang/Nim/issues/19357
|
2022-01-06 07:38:40 +00:00
|
|
|
result = ForkedSignedBeaconBlock(
|
2022-01-11 10:01:54 +00:00
|
|
|
kind: cfg.blockForkAtEpoch(header.slot.epoch()))
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
withBlck(result):
|
|
|
|
readSszBytes(data, blck)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
|
|
|
func toBeaconBlockFork*(fork: BeaconStateFork): BeaconBlockFork =
|
|
|
|
case fork
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Phase0: BeaconBlockFork.Phase0
|
|
|
|
of BeaconStateFork.Altair: BeaconBlockFork.Altair
|
|
|
|
of BeaconStateFork.Bellatrix: BeaconBlockFork.Bellatrix
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#compute_fork_data_root
|
2021-11-05 07:34:34 +00:00
|
|
|
func compute_fork_data_root*(current_version: Version,
|
|
|
|
genesis_validators_root: Eth2Digest): Eth2Digest =
|
|
|
|
## Return the 32-byte fork data root for the ``current_version`` and
|
|
|
|
## ``genesis_validators_root``.
|
|
|
|
## This is used primarily in signature domains to avoid collisions across
|
|
|
|
## forks/chains.
|
|
|
|
hash_tree_root(ForkData(
|
|
|
|
current_version: current_version,
|
|
|
|
genesis_validators_root: genesis_validators_root
|
|
|
|
))
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#compute_fork_digest
|
2021-11-05 07:34:34 +00:00
|
|
|
func compute_fork_digest*(current_version: Version,
|
|
|
|
genesis_validators_root: Eth2Digest): ForkDigest =
|
|
|
|
## Return the 4-byte fork digest for the ``current_version`` and
|
|
|
|
## ``genesis_validators_root``.
|
|
|
|
## This is a digest primarily used for domain separation on the p2p layer.
|
|
|
|
## 4-bytes suffices for practical separation of forks/chains.
|
|
|
|
array[4, byte](result)[0..3] =
|
|
|
|
compute_fork_data_root(
|
|
|
|
current_version, genesis_validators_root).data.toOpenArray(0, 3)
|
|
|
|
|
|
|
|
func init*(T: type ForkDigests,
|
|
|
|
cfg: RuntimeConfig,
|
2022-04-08 16:22:49 +00:00
|
|
|
genesis_validators_root: Eth2Digest): T =
|
2021-11-05 07:34:34 +00:00
|
|
|
T(
|
|
|
|
phase0:
|
2022-04-08 16:22:49 +00:00
|
|
|
compute_fork_digest(cfg.GENESIS_FORK_VERSION, genesis_validators_root),
|
2021-11-05 07:34:34 +00:00
|
|
|
altair:
|
2022-04-08 16:22:49 +00:00
|
|
|
compute_fork_digest(cfg.ALTAIR_FORK_VERSION, genesis_validators_root),
|
2022-01-05 14:24:15 +00:00
|
|
|
bellatrix:
|
2022-04-08 16:22:49 +00:00
|
|
|
compute_fork_digest(cfg.BELLATRIX_FORK_VERSION, genesis_validators_root),
|
2022-06-03 14:42:40 +00:00
|
|
|
capella:
|
|
|
|
compute_fork_digest(cfg.CAPELLA_FORK_VERSION, genesis_validators_root),
|
2022-01-05 14:24:15 +00:00
|
|
|
sharding:
|
2022-04-08 16:22:49 +00:00
|
|
|
compute_fork_digest(cfg.SHARDING_FORK_VERSION, genesis_validators_root),
|
2021-11-05 07:34:34 +00:00
|
|
|
)
|
2022-03-16 15:00:18 +00:00
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
func toBlockId*(header: BeaconBlockHeader): BlockId =
|
|
|
|
BlockId(root: header.hash_tree_root(), slot: header.slot)
|
|
|
|
|
2022-03-16 15:00:18 +00:00
|
|
|
func toBlockId*(blck: SomeForkySignedBeaconBlock): BlockId =
|
|
|
|
BlockId(root: blck.root, slot: blck.message.slot)
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
func toBlockId*(blck: ForkedSignedBeaconBlock |
|
|
|
|
ForkedMsgTrustedSignedBeaconBlock |
|
|
|
|
ForkedTrustedSignedBeaconBlock): BlockId =
|
2022-03-16 15:00:18 +00:00
|
|
|
withBlck(blck): BlockId(root: blck.root, slot: blck.message.slot)
|