2023-01-12 17:11:38 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# 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: [].}
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
import
|
2023-02-20 08:45:49 +00:00
|
|
|
./datatypes/[phase0, altair, bellatrix, capella, deneb],
|
2023-01-18 14:32:57 +00:00
|
|
|
./eth2_merkleization
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
LightClientDataFork* {.pure.} = enum # Append only, used in DB data!
|
|
|
|
None = 0, # only use non-0 in DB to detect accidentally uninitialized data
|
2023-01-18 14:32:57 +00:00
|
|
|
Altair = 1,
|
2023-01-19 22:53:37 +00:00
|
|
|
Capella = 2,
|
2023-03-10 17:16:26 +00:00
|
|
|
Deneb = 3
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
ForkyLightClientHeader* =
|
2023-01-18 14:32:57 +00:00
|
|
|
altair.LightClientHeader |
|
2023-01-19 22:53:37 +00:00
|
|
|
capella.LightClientHeader |
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientHeader
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
ForkyLightClientBootstrap* =
|
2023-01-18 14:32:57 +00:00
|
|
|
altair.LightClientBootstrap |
|
2023-01-19 22:53:37 +00:00
|
|
|
capella.LightClientBootstrap |
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientBootstrap
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
ForkyLightClientUpdate* =
|
2023-01-18 14:32:57 +00:00
|
|
|
altair.LightClientUpdate |
|
2023-01-19 22:53:37 +00:00
|
|
|
capella.LightClientUpdate |
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientUpdate
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
ForkyLightClientFinalityUpdate* =
|
2023-01-18 14:32:57 +00:00
|
|
|
altair.LightClientFinalityUpdate |
|
2023-01-19 22:53:37 +00:00
|
|
|
capella.LightClientFinalityUpdate |
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientFinalityUpdate
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
ForkyLightClientOptimisticUpdate* =
|
2023-01-18 14:32:57 +00:00
|
|
|
altair.LightClientOptimisticUpdate |
|
2023-01-19 22:53:37 +00:00
|
|
|
capella.LightClientOptimisticUpdate |
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientOptimisticUpdate
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
SomeForkyLightClientUpdateWithSyncCommittee* =
|
|
|
|
ForkyLightClientUpdate
|
|
|
|
|
|
|
|
SomeForkyLightClientUpdateWithFinality* =
|
|
|
|
ForkyLightClientUpdate |
|
|
|
|
ForkyLightClientFinalityUpdate
|
|
|
|
|
|
|
|
SomeForkyLightClientUpdate* =
|
|
|
|
ForkyLightClientUpdate |
|
|
|
|
ForkyLightClientFinalityUpdate |
|
|
|
|
ForkyLightClientOptimisticUpdate
|
|
|
|
|
|
|
|
SomeForkyLightClientObject* =
|
|
|
|
ForkyLightClientBootstrap |
|
|
|
|
SomeForkyLightClientUpdate
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
ForkyLightClientStore* =
|
2023-01-18 14:32:57 +00:00
|
|
|
altair.LightClientStore |
|
2023-01-19 22:53:37 +00:00
|
|
|
capella.LightClientStore |
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientStore
|
2023-01-14 21:19:50 +00:00
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
ForkedLightClientHeader* = object
|
|
|
|
case kind*: LightClientDataFork
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
discard
|
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
altairData*: altair.LightClientHeader
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
capellaData*: capella.LightClientHeader
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
denebData*: deneb.LightClientHeader
|
2023-01-16 15:53:45 +00:00
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
ForkedLightClientBootstrap* = object
|
|
|
|
case kind*: LightClientDataFork
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
discard
|
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
altairData*: altair.LightClientBootstrap
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
capellaData*: capella.LightClientBootstrap
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
denebData*: deneb.LightClientBootstrap
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
ForkedLightClientUpdate* = object
|
|
|
|
case kind*: LightClientDataFork
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
discard
|
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
altairData*: altair.LightClientUpdate
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
capellaData*: capella.LightClientUpdate
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
denebData*: deneb.LightClientUpdate
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
ForkedLightClientFinalityUpdate* = object
|
|
|
|
case kind*: LightClientDataFork
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
discard
|
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
altairData*: altair.LightClientFinalityUpdate
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
capellaData*: capella.LightClientFinalityUpdate
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
denebData*: deneb.LightClientFinalityUpdate
|
2023-01-12 17:11:38 +00:00
|
|
|
|
|
|
|
ForkedLightClientOptimisticUpdate* = object
|
|
|
|
case kind*: LightClientDataFork
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
discard
|
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
altairData*: altair.LightClientOptimisticUpdate
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
capellaData*: capella.LightClientOptimisticUpdate
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
denebData*: deneb.LightClientOptimisticUpdate
|
2023-01-12 17:11:38 +00:00
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
SomeForkedLightClientUpdateWithSyncCommittee* =
|
|
|
|
ForkedLightClientUpdate
|
|
|
|
|
|
|
|
SomeForkedLightClientUpdateWithFinality* =
|
|
|
|
ForkedLightClientUpdate |
|
|
|
|
ForkedLightClientFinalityUpdate
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
SomeForkedLightClientUpdate* =
|
|
|
|
ForkedLightClientUpdate |
|
|
|
|
ForkedLightClientFinalityUpdate |
|
|
|
|
ForkedLightClientOptimisticUpdate
|
|
|
|
|
|
|
|
SomeForkedLightClientObject* =
|
|
|
|
ForkedLightClientBootstrap |
|
|
|
|
SomeForkedLightClientUpdate
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
ForkedLightClientStore* = object
|
|
|
|
case kind*: LightClientDataFork
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
discard
|
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
altairData*: altair.LightClientStore
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
capellaData*: capella.LightClientStore
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
denebData*: deneb.LightClientStore
|
2023-01-14 21:19:50 +00:00
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
func lcDataForkAtEpoch*(
|
|
|
|
cfg: RuntimeConfig, epoch: Epoch): LightClientDataFork =
|
2023-03-10 17:16:26 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Deneb
|
2023-02-15 14:44:09 +00:00
|
|
|
if epoch >= cfg.DENEB_FORK_EPOCH:
|
2023-03-10 17:16:26 +00:00
|
|
|
LightClientDataFork.Deneb
|
2023-01-19 22:53:37 +00:00
|
|
|
elif epoch >= cfg.CAPELLA_FORK_EPOCH:
|
2023-01-18 14:32:57 +00:00
|
|
|
LightClientDataFork.Capella
|
|
|
|
elif epoch >= cfg.ALTAIR_FORK_EPOCH:
|
2023-01-12 17:11:38 +00:00
|
|
|
LightClientDataFork.Altair
|
|
|
|
else:
|
|
|
|
LightClientDataFork.None
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template kind*(
|
|
|
|
x: typedesc[ # `SomeLightClientObject` doesn't work here (Nim 1.6)
|
|
|
|
altair.LightClientHeader |
|
|
|
|
altair.LightClientBootstrap |
|
|
|
|
altair.LightClientUpdate |
|
|
|
|
altair.LightClientFinalityUpdate |
|
|
|
|
altair.LightClientOptimisticUpdate |
|
|
|
|
altair.LightClientStore]): LightClientDataFork =
|
2023-01-12 17:11:38 +00:00
|
|
|
LightClientDataFork.Altair
|
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
template kind*(
|
|
|
|
x: typedesc[ # `SomeLightClientObject` doesn't work here (Nim 1.6)
|
|
|
|
capella.LightClientHeader |
|
|
|
|
capella.LightClientBootstrap |
|
|
|
|
capella.LightClientUpdate |
|
|
|
|
capella.LightClientFinalityUpdate |
|
|
|
|
capella.LightClientOptimisticUpdate |
|
|
|
|
capella.LightClientStore]): LightClientDataFork =
|
|
|
|
LightClientDataFork.Capella
|
|
|
|
|
2023-01-19 22:53:37 +00:00
|
|
|
template kind*(
|
|
|
|
x: typedesc[ # `SomeLightClientObject` doesn't work here (Nim 1.6)
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientHeader |
|
|
|
|
deneb.LightClientBootstrap |
|
|
|
|
deneb.LightClientUpdate |
|
|
|
|
deneb.LightClientFinalityUpdate |
|
|
|
|
deneb.LightClientOptimisticUpdate |
|
|
|
|
deneb.LightClientStore]): LightClientDataFork =
|
2023-03-10 17:16:26 +00:00
|
|
|
LightClientDataFork.Deneb
|
2023-01-19 22:53:37 +00:00
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template LightClientHeader*(kind: static LightClientDataFork): auto =
|
2023-03-10 17:16:26 +00:00
|
|
|
when kind == LightClientDataFork.Deneb:
|
2023-02-25 01:03:34 +00:00
|
|
|
typedesc[deneb.LightClientHeader]
|
2023-01-19 22:53:37 +00:00
|
|
|
elif kind == LightClientDataFork.Capella:
|
2023-01-18 14:32:57 +00:00
|
|
|
typedesc[capella.LightClientHeader]
|
|
|
|
elif kind == LightClientDataFork.Altair:
|
2023-01-13 15:46:35 +00:00
|
|
|
typedesc[altair.LightClientHeader]
|
2023-01-12 17:11:38 +00:00
|
|
|
else:
|
|
|
|
static: raiseAssert "Unreachable"
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template LightClientBootstrap*(kind: static LightClientDataFork): auto =
|
2023-03-10 17:16:26 +00:00
|
|
|
when kind == LightClientDataFork.Deneb:
|
2023-03-02 20:09:24 +00:00
|
|
|
typedesc[deneb.LightClientBootstrap]
|
2023-01-19 22:53:37 +00:00
|
|
|
elif kind == LightClientDataFork.Capella:
|
2023-01-18 14:32:57 +00:00
|
|
|
typedesc[capella.LightClientBootstrap]
|
|
|
|
elif kind == LightClientDataFork.Altair:
|
2023-01-12 17:11:38 +00:00
|
|
|
typedesc[altair.LightClientBootstrap]
|
|
|
|
else:
|
|
|
|
static: raiseAssert "Unreachable"
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template LightClientUpdate*(kind: static LightClientDataFork): auto =
|
2023-03-10 17:16:26 +00:00
|
|
|
when kind == LightClientDataFork.Deneb:
|
2023-02-25 01:03:34 +00:00
|
|
|
typedesc[deneb.LightClientUpdate]
|
2023-01-19 22:53:37 +00:00
|
|
|
elif kind == LightClientDataFork.Capella:
|
2023-01-18 14:32:57 +00:00
|
|
|
typedesc[capella.LightClientUpdate]
|
|
|
|
elif kind == LightClientDataFork.Altair:
|
2023-01-12 17:11:38 +00:00
|
|
|
typedesc[altair.LightClientUpdate]
|
|
|
|
else:
|
|
|
|
static: raiseAssert "Unreachable"
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template LightClientFinalityUpdate*(kind: static LightClientDataFork): auto =
|
2023-03-10 17:16:26 +00:00
|
|
|
when kind == LightClientDataFork.Deneb:
|
2023-03-09 00:34:17 +00:00
|
|
|
typedesc[deneb.LightClientFinalityUpdate]
|
2023-01-19 22:53:37 +00:00
|
|
|
elif kind == LightClientDataFork.Capella:
|
2023-01-18 14:32:57 +00:00
|
|
|
typedesc[capella.LightClientFinalityUpdate]
|
|
|
|
elif kind == LightClientDataFork.Altair:
|
2023-01-12 17:11:38 +00:00
|
|
|
typedesc[altair.LightClientFinalityUpdate]
|
|
|
|
else:
|
|
|
|
static: raiseAssert "Unreachable"
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template LightClientOptimisticUpdate*(kind: static LightClientDataFork): auto =
|
2023-03-10 17:16:26 +00:00
|
|
|
when kind == LightClientDataFork.Deneb:
|
2023-03-02 20:09:24 +00:00
|
|
|
typedesc[deneb.LightClientOptimisticUpdate]
|
2023-01-19 22:53:37 +00:00
|
|
|
elif kind == LightClientDataFork.Capella:
|
2023-01-18 14:32:57 +00:00
|
|
|
typedesc[capella.LightClientOptimisticUpdate]
|
|
|
|
elif kind == LightClientDataFork.Altair:
|
2023-01-12 17:11:38 +00:00
|
|
|
typedesc[altair.LightClientOptimisticUpdate]
|
|
|
|
else:
|
|
|
|
static: raiseAssert "Unreachable"
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template LightClientStore*(kind: static LightClientDataFork): auto =
|
2023-03-10 17:16:26 +00:00
|
|
|
when kind == LightClientDataFork.Deneb:
|
2023-02-25 01:03:34 +00:00
|
|
|
typedesc[deneb.LightClientStore]
|
2023-01-19 22:53:37 +00:00
|
|
|
elif kind == LightClientDataFork.Capella:
|
2023-01-18 14:32:57 +00:00
|
|
|
typedesc[capella.LightClientStore]
|
|
|
|
elif kind == LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
typedesc[altair.LightClientStore]
|
|
|
|
else:
|
|
|
|
static: raiseAssert "Unreachable"
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
template Forky*(
|
|
|
|
x: typedesc[ForkedLightClientHeader],
|
|
|
|
kind: static LightClientDataFork): auto =
|
|
|
|
kind.LightClientHeader
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template Forky*(
|
|
|
|
x: typedesc[ForkedLightClientBootstrap],
|
|
|
|
kind: static LightClientDataFork): auto =
|
|
|
|
kind.LightClientBootstrap
|
|
|
|
|
|
|
|
template Forky*(
|
|
|
|
x: typedesc[ForkedLightClientUpdate],
|
|
|
|
kind: static LightClientDataFork): auto =
|
|
|
|
kind.LightClientUpdate
|
|
|
|
|
|
|
|
template Forky*(
|
|
|
|
x: typedesc[ForkedLightClientFinalityUpdate],
|
|
|
|
kind: static LightClientDataFork): auto =
|
|
|
|
kind.LightClientFinalityUpdate
|
|
|
|
|
|
|
|
template Forky*(
|
|
|
|
x: typedesc[ForkedLightClientOptimisticUpdate],
|
|
|
|
kind: static LightClientDataFork): auto =
|
|
|
|
kind.LightClientOptimisticUpdate
|
|
|
|
|
|
|
|
template Forky*(
|
|
|
|
x: typedesc[ForkedLightClientStore],
|
|
|
|
kind: static LightClientDataFork): auto =
|
|
|
|
kind.LightClientStore
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
template Forked*(x: typedesc[ForkyLightClientHeader]): auto =
|
|
|
|
typedesc[ForkedLightClientHeader]
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template Forked*(x: typedesc[ForkyLightClientBootstrap]): auto =
|
2023-01-12 17:11:38 +00:00
|
|
|
typedesc[ForkedLightClientBootstrap]
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template Forked*(x: typedesc[ForkyLightClientUpdate]): auto =
|
2023-01-12 17:11:38 +00:00
|
|
|
typedesc[ForkedLightClientUpdate]
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template Forked*(x: typedesc[ForkyLightClientFinalityUpdate]): auto =
|
2023-01-12 17:11:38 +00:00
|
|
|
typedesc[ForkedLightClientFinalityUpdate]
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template Forked*(x: typedesc[ForkyLightClientOptimisticUpdate]): auto =
|
2023-01-12 17:11:38 +00:00
|
|
|
typedesc[ForkedLightClientOptimisticUpdate]
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
template Forked*(x: typedesc[ForkyLightClientStore]): auto =
|
|
|
|
typedesc[ForkedLightClientStore]
|
|
|
|
|
|
|
|
template withAll*(
|
|
|
|
x: typedesc[LightClientDataFork], body: untyped): untyped =
|
2023-03-10 17:16:26 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Deneb
|
2023-01-19 22:53:37 +00:00
|
|
|
block:
|
2023-03-10 17:16:26 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
block:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
body
|
2023-01-14 21:19:50 +00:00
|
|
|
block:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
body
|
|
|
|
block:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
|
|
|
body
|
|
|
|
|
|
|
|
template withLcDataFork*(
|
|
|
|
x: LightClientDataFork, body: untyped): untyped =
|
|
|
|
case x
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
body
|
2023-01-14 21:19:50 +00:00
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
body
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
|
|
|
body
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
template withForkyHeader*(
|
|
|
|
x: ForkedLightClientHeader, body: untyped): untyped =
|
|
|
|
case x.kind
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
|
|
|
template forkyHeader: untyped {.inject, used.} = x.denebData
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
template forkyHeader: untyped {.inject, used.} = x.capellaData
|
|
|
|
body
|
2023-01-16 15:53:45 +00:00
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
template forkyHeader: untyped {.inject, used.} = x.altairData
|
|
|
|
body
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
|
|
|
body
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
template withForkyBootstrap*(
|
|
|
|
x: ForkedLightClientBootstrap, body: untyped): untyped =
|
|
|
|
case x.kind
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
|
|
|
template forkyBootstrap: untyped {.inject, used.} = x.denebData
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
template forkyBootstrap: untyped {.inject, used.} = x.capellaData
|
|
|
|
body
|
2023-01-12 17:11:38 +00:00
|
|
|
of LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
template forkyBootstrap: untyped {.inject, used.} = x.altairData
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
of LightClientDataFork.None:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
|
|
|
|
template withForkyUpdate*(
|
|
|
|
x: ForkedLightClientUpdate, body: untyped): untyped =
|
|
|
|
case x.kind
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
|
|
|
template forkyUpdate: untyped {.inject, used.} = x.denebData
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
template forkyUpdate: untyped {.inject, used.} = x.capellaData
|
|
|
|
body
|
2023-01-12 17:11:38 +00:00
|
|
|
of LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
template forkyUpdate: untyped {.inject, used.} = x.altairData
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
of LightClientDataFork.None:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
|
|
|
|
template withForkyFinalityUpdate*(
|
|
|
|
x: ForkedLightClientFinalityUpdate, body: untyped): untyped =
|
|
|
|
case x.kind
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
|
|
|
template forkyFinalityUpdate: untyped {.inject, used.} = x.denebData
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
template forkyFinalityUpdate: untyped {.inject, used.} = x.capellaData
|
|
|
|
body
|
2023-01-12 17:11:38 +00:00
|
|
|
of LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
template forkyFinalityUpdate: untyped {.inject, used.} = x.altairData
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
of LightClientDataFork.None:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
|
|
|
|
template withForkyOptimisticUpdate*(
|
|
|
|
x: ForkedLightClientOptimisticUpdate, body: untyped): untyped =
|
|
|
|
case x.kind
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
|
|
|
template forkyOptimisticUpdate: untyped {.inject, used.} = x.denebData
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
template forkyOptimisticUpdate: untyped {.inject, used.} = x.capellaData
|
|
|
|
body
|
2023-01-12 17:11:38 +00:00
|
|
|
of LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
template forkyOptimisticUpdate: untyped {.inject, used.} = x.altairData
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
of LightClientDataFork.None:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
|
|
|
|
template withForkyObject*(
|
|
|
|
x: SomeForkedLightClientObject, body: untyped): untyped =
|
|
|
|
case x.kind
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
|
|
|
template forkyObject: untyped {.inject, used.} = x.denebData
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
template forkyObject: untyped {.inject, used.} = x.capellaData
|
|
|
|
body
|
2023-01-12 17:11:38 +00:00
|
|
|
of LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
template forkyObject: untyped {.inject, used.} = x.altairData
|
|
|
|
body
|
|
|
|
of LightClientDataFork.None:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
|
|
|
body
|
|
|
|
|
|
|
|
template withForkyStore*(
|
|
|
|
x: ForkedLightClientStore, body: untyped): untyped =
|
|
|
|
case x.kind
|
2023-03-10 17:16:26 +00:00
|
|
|
of LightClientDataFork.Deneb:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Deneb
|
|
|
|
template forkyStore: untyped {.inject, used.} = x.denebData
|
2023-01-19 22:53:37 +00:00
|
|
|
body
|
2023-01-18 14:32:57 +00:00
|
|
|
of LightClientDataFork.Capella:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Capella
|
|
|
|
template forkyStore: untyped {.inject, used.} = x.capellaData
|
|
|
|
body
|
2023-01-14 21:19:50 +00:00
|
|
|
of LightClientDataFork.Altair:
|
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.Altair
|
|
|
|
template forkyStore: untyped {.inject, used.} = x.altairData
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
of LightClientDataFork.None:
|
2023-01-14 21:19:50 +00:00
|
|
|
const lcDataFork {.inject, used.} = LightClientDataFork.None
|
2023-01-12 17:11:38 +00:00
|
|
|
body
|
|
|
|
|
2023-01-23 19:38:03 +00:00
|
|
|
func toFull*(
|
|
|
|
update: SomeForkyLightClientUpdate): auto =
|
|
|
|
type ResultType = typeof(update).kind.LightClientUpdate
|
|
|
|
when update is ForkyLightClientUpdate:
|
|
|
|
update
|
|
|
|
elif update is SomeForkyLightClientUpdateWithFinality:
|
|
|
|
ResultType(
|
|
|
|
attested_header: update.attested_header,
|
|
|
|
finalized_header: update.finalized_header,
|
|
|
|
finality_branch: update.finality_branch,
|
|
|
|
sync_aggregate: update.sync_aggregate,
|
|
|
|
signature_slot: update.signature_slot)
|
|
|
|
else:
|
|
|
|
ResultType(
|
|
|
|
attested_header: update.attested_header,
|
|
|
|
sync_aggregate: update.sync_aggregate,
|
|
|
|
signature_slot: update.signature_slot)
|
|
|
|
|
|
|
|
func toFull*(
|
2023-01-14 21:19:50 +00:00
|
|
|
update: SomeForkedLightClientUpdate): ForkedLightClientUpdate =
|
|
|
|
when update is ForkyLightClientUpdate:
|
|
|
|
update
|
|
|
|
else:
|
|
|
|
withForkyObject(update):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var res = ForkedLightClientUpdate(kind: lcDataFork)
|
|
|
|
template forkyRes: untyped = res.forky(lcDataFork)
|
|
|
|
forkyRes = forkyObject.toFull()
|
|
|
|
res
|
|
|
|
else:
|
|
|
|
default(ForkedLightClientUpdate)
|
|
|
|
|
2023-01-23 19:38:03 +00:00
|
|
|
func toFinality*(
|
|
|
|
update: SomeForkyLightClientUpdate): auto =
|
|
|
|
type ResultType = typeof(update).kind.LightClientFinalityUpdate
|
|
|
|
when update is ForkyLightClientFinalityUpdate:
|
|
|
|
update
|
|
|
|
elif update is SomeForkyLightClientUpdateWithFinality:
|
|
|
|
ResultType(
|
|
|
|
attested_header: update.attested_header,
|
|
|
|
finalized_header: update.finalized_header,
|
|
|
|
finality_branch: update.finality_branch,
|
|
|
|
sync_aggregate: update.sync_aggregate,
|
|
|
|
signature_slot: update.signature_slot)
|
|
|
|
else:
|
|
|
|
ResultType(
|
|
|
|
attested_header: update.attested_header,
|
|
|
|
sync_aggregate: update.sync_aggregate,
|
|
|
|
signature_slot: update.signature_slot)
|
|
|
|
|
|
|
|
func toFinality*(
|
2023-01-14 21:19:50 +00:00
|
|
|
update: SomeForkedLightClientUpdate): ForkedLightClientFinalityUpdate =
|
|
|
|
when update is ForkyLightClientFinalityUpdate:
|
|
|
|
update
|
|
|
|
else:
|
|
|
|
withForkyObject(update):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var res = ForkedLightClientFinalityUpdate(kind: lcDataFork)
|
|
|
|
template forkyRes: untyped = res.forky(lcDataFork)
|
|
|
|
forkyRes = forkyObject.toFinality()
|
|
|
|
res
|
|
|
|
else:
|
|
|
|
default(ForkedLightClientFinalityUpdate)
|
|
|
|
|
2023-01-23 19:38:03 +00:00
|
|
|
func toOptimistic*(
|
|
|
|
update: SomeForkyLightClientUpdate): auto =
|
|
|
|
type ResultType = typeof(update).kind.LightClientOptimisticUpdate
|
|
|
|
when update is ForkyLightClientOptimisticUpdate:
|
|
|
|
update
|
|
|
|
else:
|
|
|
|
ResultType(
|
|
|
|
attested_header: update.attested_header,
|
|
|
|
sync_aggregate: update.sync_aggregate,
|
|
|
|
signature_slot: update.signature_slot)
|
|
|
|
|
|
|
|
func toOptimistic*(
|
2023-01-12 17:11:38 +00:00
|
|
|
update: SomeForkedLightClientUpdate): ForkedLightClientOptimisticUpdate =
|
|
|
|
when update is ForkyLightClientOptimisticUpdate:
|
|
|
|
update
|
|
|
|
else:
|
|
|
|
withForkyObject(update):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var res = ForkedLightClientOptimisticUpdate(kind: lcDataFork)
|
|
|
|
template forkyRes: untyped = res.forky(lcDataFork)
|
|
|
|
forkyRes = forkyObject.toOptimistic()
|
|
|
|
res
|
2023-01-12 17:11:38 +00:00
|
|
|
else:
|
|
|
|
default(ForkedLightClientOptimisticUpdate)
|
|
|
|
|
2023-01-23 19:38:03 +00:00
|
|
|
func matches*[A, B: SomeForkyLightClientUpdate](a: A, b: B): bool =
|
|
|
|
static: doAssert typeof(A).kind == typeof(B).kind
|
|
|
|
if a.attested_header != b.attested_header:
|
|
|
|
return false
|
|
|
|
when a is SomeForkyLightClientUpdateWithSyncCommittee and
|
|
|
|
b is SomeForkyLightClientUpdateWithSyncCommittee:
|
|
|
|
if a.next_sync_committee != b.next_sync_committee:
|
|
|
|
return false
|
|
|
|
if a.next_sync_committee_branch != b.next_sync_committee_branch:
|
|
|
|
return false
|
|
|
|
when a is SomeForkyLightClientUpdateWithFinality and
|
|
|
|
b is SomeForkyLightClientUpdateWithFinality:
|
|
|
|
if a.finalized_header != b.finalized_header:
|
|
|
|
return false
|
|
|
|
if a.finality_branch != b.finality_branch:
|
|
|
|
return false
|
|
|
|
if a.sync_aggregate != b.sync_aggregate:
|
|
|
|
return false
|
|
|
|
if a.signature_slot != b.signature_slot:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
func matches*[A, B: SomeForkedLightClientUpdate](a: A, b: B): bool =
|
|
|
|
if a.kind != b.kind:
|
|
|
|
return false
|
|
|
|
withForkyObject(a):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
forkyObject.matches(b.forky(lcDataFork))
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
|
|
|
|
template forky*(
|
2023-01-16 15:53:45 +00:00
|
|
|
x:
|
|
|
|
ForkedLightClientHeader |
|
|
|
|
SomeForkedLightClientObject |
|
|
|
|
ForkedLightClientStore,
|
2023-01-14 21:19:50 +00:00
|
|
|
kind: static LightClientDataFork): untyped =
|
2023-03-10 17:16:26 +00:00
|
|
|
when kind == LightClientDataFork.Deneb:
|
|
|
|
x.denebData
|
2023-01-19 22:53:37 +00:00
|
|
|
elif kind == LightClientDataFork.Capella:
|
2023-01-18 14:32:57 +00:00
|
|
|
x.capellaData
|
|
|
|
elif kind == LightClientDataFork.Altair:
|
2023-01-12 17:11:38 +00:00
|
|
|
x.altairData
|
|
|
|
else:
|
2023-01-14 21:19:50 +00:00
|
|
|
static: raiseAssert "Unreachable"
|
2023-01-12 17:11:38 +00:00
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
func migrateToDataFork*(
|
|
|
|
x: var ForkedLightClientHeader,
|
|
|
|
newKind: static LightClientDataFork) =
|
|
|
|
if newKind == x.kind:
|
|
|
|
# Already at correct kind
|
|
|
|
discard
|
|
|
|
elif newKind < x.kind:
|
|
|
|
# Downgrade not supported, re-initialize
|
|
|
|
x = ForkedLightClientHeader(kind: newKind)
|
|
|
|
else:
|
|
|
|
# Upgrade to Altair
|
|
|
|
when newKind >= LightClientDataFork.Altair:
|
|
|
|
if x.kind == LightClientDataFork.None:
|
|
|
|
x = ForkedLightClientHeader(
|
|
|
|
kind: LightClientDataFork.Altair)
|
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
# Upgrade to Capella
|
|
|
|
when newKind >= LightClientDataFork.Capella:
|
|
|
|
if x.kind == LightClientDataFork.Altair:
|
|
|
|
x = ForkedLightClientHeader(
|
|
|
|
kind: LightClientDataFork.Capella,
|
|
|
|
capellaData: upgrade_lc_header_to_capella(
|
|
|
|
x.forky(LightClientDataFork.Altair)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
# Upgrade to Deneb
|
|
|
|
when newKind >= LightClientDataFork.Deneb:
|
2023-01-19 22:53:37 +00:00
|
|
|
if x.kind == LightClientDataFork.Capella:
|
|
|
|
x = ForkedLightClientHeader(
|
2023-03-10 17:16:26 +00:00
|
|
|
kind: LightClientDataFork.Deneb,
|
|
|
|
denebData: upgrade_lc_header_to_deneb(
|
2023-01-19 22:53:37 +00:00
|
|
|
x.forky(LightClientDataFork.Capella)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Deneb
|
2023-01-16 15:53:45 +00:00
|
|
|
doAssert x.kind == newKind
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
func migrateToDataFork*(
|
|
|
|
x: var ForkedLightClientBootstrap,
|
|
|
|
newKind: static LightClientDataFork) =
|
|
|
|
if newKind == x.kind:
|
|
|
|
# Already at correct kind
|
|
|
|
discard
|
|
|
|
elif newKind < x.kind:
|
|
|
|
# Downgrade not supported, re-initialize
|
|
|
|
x = ForkedLightClientBootstrap(kind: newKind)
|
|
|
|
else:
|
|
|
|
# Upgrade to Altair
|
|
|
|
when newKind >= LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
if x.kind == LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
x = ForkedLightClientBootstrap(
|
|
|
|
kind: LightClientDataFork.Altair)
|
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
# Upgrade to Capella
|
|
|
|
when newKind >= LightClientDataFork.Capella:
|
|
|
|
if x.kind == LightClientDataFork.Altair:
|
|
|
|
x = ForkedLightClientBootstrap(
|
|
|
|
kind: LightClientDataFork.Capella,
|
|
|
|
capellaData: upgrade_lc_bootstrap_to_capella(
|
|
|
|
x.forky(LightClientDataFork.Altair)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
# Upgrade to Deneb
|
|
|
|
when newKind >= LightClientDataFork.Deneb:
|
2023-01-19 22:53:37 +00:00
|
|
|
if x.kind == LightClientDataFork.Capella:
|
|
|
|
x = ForkedLightClientBootstrap(
|
2023-03-10 17:16:26 +00:00
|
|
|
kind: LightClientDataFork.Deneb,
|
|
|
|
denebData: upgrade_lc_bootstrap_to_deneb(
|
2023-01-19 22:53:37 +00:00
|
|
|
x.forky(LightClientDataFork.Capella)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Deneb
|
2023-01-12 17:11:38 +00:00
|
|
|
doAssert x.kind == newKind
|
|
|
|
|
|
|
|
func migrateToDataFork*(
|
|
|
|
x: var ForkedLightClientUpdate,
|
|
|
|
newKind: static LightClientDataFork) =
|
|
|
|
if newKind == x.kind:
|
|
|
|
# Already at correct kind
|
|
|
|
discard
|
|
|
|
elif newKind < x.kind:
|
|
|
|
# Downgrade not supported, re-initialize
|
|
|
|
x = ForkedLightClientUpdate(kind: newKind)
|
|
|
|
else:
|
|
|
|
# Upgrade to Altair
|
|
|
|
when newKind >= LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
if x.kind == LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
x = ForkedLightClientUpdate(
|
|
|
|
kind: LightClientDataFork.Altair)
|
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
# Upgrade to Capella
|
|
|
|
when newKind >= LightClientDataFork.Capella:
|
|
|
|
if x.kind == LightClientDataFork.Altair:
|
|
|
|
x = ForkedLightClientUpdate(
|
|
|
|
kind: LightClientDataFork.Capella,
|
|
|
|
capellaData: upgrade_lc_update_to_capella(
|
|
|
|
x.forky(LightClientDataFork.Altair)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
# Upgrade to Deneb
|
|
|
|
when newKind >= LightClientDataFork.Deneb:
|
2023-01-19 22:53:37 +00:00
|
|
|
if x.kind == LightClientDataFork.Capella:
|
|
|
|
x = ForkedLightClientUpdate(
|
2023-03-10 17:16:26 +00:00
|
|
|
kind: LightClientDataFork.Deneb,
|
|
|
|
denebData: upgrade_lc_update_to_deneb(
|
2023-01-19 22:53:37 +00:00
|
|
|
x.forky(LightClientDataFork.Capella)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Deneb
|
2023-01-12 17:11:38 +00:00
|
|
|
doAssert x.kind == newKind
|
|
|
|
|
|
|
|
func migrateToDataFork*(
|
|
|
|
x: var ForkedLightClientFinalityUpdate,
|
|
|
|
newKind: static LightClientDataFork) =
|
|
|
|
if newKind == x.kind:
|
|
|
|
# Already at correct kind
|
|
|
|
discard
|
|
|
|
elif newKind < x.kind:
|
|
|
|
# Downgrade not supported, re-initialize
|
|
|
|
x = ForkedLightClientFinalityUpdate(kind: newKind)
|
|
|
|
else:
|
|
|
|
# Upgrade to Altair
|
|
|
|
when newKind >= LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
if x.kind == LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
x = ForkedLightClientFinalityUpdate(
|
|
|
|
kind: LightClientDataFork.Altair)
|
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
# Upgrade to Capella
|
|
|
|
when newKind >= LightClientDataFork.Capella:
|
|
|
|
if x.kind == LightClientDataFork.Altair:
|
|
|
|
x = ForkedLightClientFinalityUpdate(
|
|
|
|
kind: LightClientDataFork.Capella,
|
|
|
|
capellaData: upgrade_lc_finality_update_to_capella(
|
|
|
|
x.forky(LightClientDataFork.Altair)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
# Upgrade to Deneb
|
|
|
|
when newKind >= LightClientDataFork.Deneb:
|
2023-01-19 22:53:37 +00:00
|
|
|
if x.kind == LightClientDataFork.Capella:
|
|
|
|
x = ForkedLightClientFinalityUpdate(
|
2023-03-10 17:16:26 +00:00
|
|
|
kind: LightClientDataFork.Deneb,
|
|
|
|
denebData: upgrade_lc_finality_update_to_deneb(
|
2023-01-19 22:53:37 +00:00
|
|
|
x.forky(LightClientDataFork.Capella)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Deneb
|
2023-01-12 17:11:38 +00:00
|
|
|
doAssert x.kind == newKind
|
|
|
|
|
|
|
|
func migrateToDataFork*(
|
|
|
|
x: var ForkedLightClientOptimisticUpdate,
|
|
|
|
newKind: static LightClientDataFork) =
|
|
|
|
if newKind == x.kind:
|
|
|
|
# Already at correct kind
|
|
|
|
discard
|
|
|
|
elif newKind < x.kind:
|
|
|
|
# Downgrade not supported, re-initialize
|
|
|
|
x = ForkedLightClientOptimisticUpdate(kind: newKind)
|
|
|
|
else:
|
|
|
|
# Upgrade to Altair
|
|
|
|
when newKind >= LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
if x.kind == LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
x = ForkedLightClientOptimisticUpdate(
|
|
|
|
kind: LightClientDataFork.Altair)
|
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
# Upgrade to Capella
|
|
|
|
when newKind >= LightClientDataFork.Capella:
|
|
|
|
if x.kind == LightClientDataFork.Altair:
|
|
|
|
x = ForkedLightClientOptimisticUpdate(
|
|
|
|
kind: LightClientDataFork.Capella,
|
|
|
|
capellaData: upgrade_lc_optimistic_update_to_capella(
|
|
|
|
x.forky(LightClientDataFork.Altair)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
# Upgrade to Deneb
|
|
|
|
when newKind >= LightClientDataFork.Deneb:
|
2023-01-19 22:53:37 +00:00
|
|
|
if x.kind == LightClientDataFork.Capella:
|
|
|
|
x = ForkedLightClientOptimisticUpdate(
|
2023-03-10 17:16:26 +00:00
|
|
|
kind: LightClientDataFork.Deneb,
|
|
|
|
denebData: upgrade_lc_optimistic_update_to_deneb(
|
2023-01-19 22:53:37 +00:00
|
|
|
x.forky(LightClientDataFork.Capella)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Deneb
|
2023-01-12 17:11:38 +00:00
|
|
|
doAssert x.kind == newKind
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
func migrateToDataFork*(
|
|
|
|
x: var ForkedLightClientStore,
|
|
|
|
newKind: static LightClientDataFork) =
|
|
|
|
if newKind == x.kind:
|
|
|
|
# Already at correct kind
|
|
|
|
discard
|
|
|
|
elif newKind < x.kind:
|
|
|
|
# Downgrade not supported, re-initialize
|
|
|
|
x = ForkedLightClientStore(kind: newKind)
|
|
|
|
else:
|
|
|
|
# Upgrade to Altair
|
|
|
|
when newKind >= LightClientDataFork.Altair:
|
|
|
|
if x.kind == LightClientDataFork.None:
|
|
|
|
x = ForkedLightClientStore(
|
|
|
|
kind: LightClientDataFork.Altair)
|
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
# Upgrade to Capella
|
|
|
|
when newKind >= LightClientDataFork.Capella:
|
|
|
|
if x.kind == LightClientDataFork.Altair:
|
|
|
|
x = ForkedLightClientStore(
|
|
|
|
kind: LightClientDataFork.Capella,
|
|
|
|
capellaData: upgrade_lc_store_to_capella(
|
|
|
|
x.forky(LightClientDataFork.Altair)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
# Upgrade to Deneb
|
|
|
|
when newKind >= LightClientDataFork.Deneb:
|
2023-01-19 22:53:37 +00:00
|
|
|
if x.kind == LightClientDataFork.Capella:
|
|
|
|
x = ForkedLightClientStore(
|
2023-03-10 17:16:26 +00:00
|
|
|
kind: LightClientDataFork.Deneb,
|
|
|
|
denebData: upgrade_lc_store_to_deneb(
|
2023-01-19 22:53:37 +00:00
|
|
|
x.forky(LightClientDataFork.Capella)))
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Deneb
|
2023-01-14 21:19:50 +00:00
|
|
|
doAssert x.kind == newKind
|
|
|
|
|
|
|
|
func migratingToDataFork*[
|
2023-01-16 15:53:45 +00:00
|
|
|
T:
|
|
|
|
ForkedLightClientHeader |
|
|
|
|
SomeForkedLightClientObject |
|
|
|
|
ForkedLightClientStore](
|
2023-01-12 17:11:38 +00:00
|
|
|
x: T, newKind: static LightClientDataFork): T =
|
|
|
|
var upgradedObject = x
|
|
|
|
upgradedObject.migrateToDataFork(newKind)
|
|
|
|
upgradedObject
|
2023-01-13 15:46:35 +00:00
|
|
|
|
2023-03-21 00:42:22 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.5/specs/altair/light-client/full-node.md#block_to_light_client_header
|
2023-01-14 21:19:50 +00:00
|
|
|
func toAltairLightClientHeader(
|
|
|
|
blck: # `SomeSignedBeaconBlock` doesn't work here (Nim 1.6)
|
|
|
|
phase0.SignedBeaconBlock | phase0.TrustedSignedBeaconBlock |
|
|
|
|
altair.SignedBeaconBlock | altair.TrustedSignedBeaconBlock |
|
2023-01-18 14:32:57 +00:00
|
|
|
bellatrix.SignedBeaconBlock | bellatrix.TrustedSignedBeaconBlock
|
2023-01-14 21:19:50 +00:00
|
|
|
): altair.LightClientHeader =
|
|
|
|
altair.LightClientHeader(
|
|
|
|
beacon: blck.message.toBeaconBlockHeader())
|
|
|
|
|
2023-03-21 00:42:22 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.5/specs/capella/light-client/full-node.md#modified-block_to_light_client_header
|
2023-01-18 14:32:57 +00:00
|
|
|
func toCapellaLightClientHeader(
|
|
|
|
blck: # `SomeSignedBeaconBlock` doesn't work here (Nim 1.6)
|
|
|
|
phase0.SignedBeaconBlock | phase0.TrustedSignedBeaconBlock |
|
|
|
|
altair.SignedBeaconBlock | altair.TrustedSignedBeaconBlock |
|
|
|
|
bellatrix.SignedBeaconBlock | bellatrix.TrustedSignedBeaconBlock
|
|
|
|
): capella.LightClientHeader =
|
|
|
|
# Note that during fork transitions, `finalized_header` may still
|
|
|
|
# point to earlier forks. While Bellatrix blocks also contain an
|
|
|
|
# `ExecutionPayload` (minus `withdrawals_root`), it was not included
|
|
|
|
# in the corresponding light client data. To ensure compatibility
|
|
|
|
# with legacy data going through `upgrade_lc_header_to_capella`,
|
|
|
|
# leave out execution data.
|
|
|
|
capella.LightClientHeader(
|
|
|
|
beacon: blck.message.toBeaconBlockHeader())
|
|
|
|
|
|
|
|
func toCapellaLightClientHeader(
|
|
|
|
blck: # `SomeSignedBeaconBlock` doesn't work here (Nim 1.6)
|
2023-01-19 22:53:37 +00:00
|
|
|
capella.SignedBeaconBlock | capella.TrustedSignedBeaconBlock
|
2023-01-18 14:32:57 +00:00
|
|
|
): capella.LightClientHeader =
|
|
|
|
template payload: untyped = blck.message.body.execution_payload
|
|
|
|
capella.LightClientHeader(
|
|
|
|
beacon: blck.message.toBeaconBlockHeader(),
|
|
|
|
execution: capella.ExecutionPayloadHeader(
|
|
|
|
parent_hash: payload.parent_hash,
|
|
|
|
fee_recipient: payload.fee_recipient,
|
|
|
|
state_root: payload.state_root,
|
|
|
|
receipts_root: payload.receipts_root,
|
|
|
|
logs_bloom: payload.logs_bloom,
|
|
|
|
prev_randao: payload.prev_randao,
|
|
|
|
block_number: payload.block_number,
|
|
|
|
gas_limit: payload.gas_limit,
|
|
|
|
gas_used: payload.gas_used,
|
|
|
|
timestamp: payload.timestamp,
|
|
|
|
extra_data: payload.extra_data,
|
|
|
|
base_fee_per_gas: payload.base_fee_per_gas,
|
|
|
|
block_hash: payload.block_hash,
|
|
|
|
transactions_root: hash_tree_root(payload.transactions),
|
|
|
|
withdrawals_root: hash_tree_root(payload.withdrawals)),
|
|
|
|
execution_branch: blck.message.body.build_proof(
|
|
|
|
capella.EXECUTION_PAYLOAD_INDEX).get)
|
|
|
|
|
2023-03-21 00:42:22 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.5/specs/deneb/light-client/full-node.md#modified-block_to_light_client_header
|
2023-03-10 17:16:26 +00:00
|
|
|
func toDenebLightClientHeader(
|
2023-01-19 22:53:37 +00:00
|
|
|
blck: # `SomeSignedBeaconBlock` doesn't work here (Nim 1.6)
|
|
|
|
phase0.SignedBeaconBlock | phase0.TrustedSignedBeaconBlock |
|
|
|
|
altair.SignedBeaconBlock | altair.TrustedSignedBeaconBlock |
|
|
|
|
bellatrix.SignedBeaconBlock | bellatrix.TrustedSignedBeaconBlock
|
2023-02-20 08:45:49 +00:00
|
|
|
): deneb.LightClientHeader =
|
2023-01-19 22:53:37 +00:00
|
|
|
# Note that during fork transitions, `finalized_header` may still
|
|
|
|
# point to earlier forks. While Bellatrix blocks also contain an
|
|
|
|
# `ExecutionPayload` (minus `withdrawals_root`), it was not included
|
|
|
|
# in the corresponding light client data. To ensure compatibility
|
|
|
|
# with legacy data going through `upgrade_lc_header_to_capella`,
|
|
|
|
# leave out execution data.
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientHeader(
|
2023-01-19 22:53:37 +00:00
|
|
|
beacon: blck.message.toBeaconBlockHeader())
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
func toDenebLightClientHeader(
|
2023-01-19 22:53:37 +00:00
|
|
|
blck: # `SomeSignedBeaconBlock` doesn't work here (Nim 1.6)
|
|
|
|
capella.SignedBeaconBlock | capella.TrustedSignedBeaconBlock
|
2023-02-20 08:45:49 +00:00
|
|
|
): deneb.LightClientHeader =
|
2023-01-19 22:53:37 +00:00
|
|
|
template payload: untyped = blck.message.body.execution_payload
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientHeader(
|
2023-01-19 22:53:37 +00:00
|
|
|
beacon: blck.message.toBeaconBlockHeader(),
|
2023-02-20 08:45:49 +00:00
|
|
|
execution: deneb.ExecutionPayloadHeader(
|
2023-01-19 22:53:37 +00:00
|
|
|
parent_hash: payload.parent_hash,
|
|
|
|
fee_recipient: payload.fee_recipient,
|
|
|
|
state_root: payload.state_root,
|
|
|
|
receipts_root: payload.receipts_root,
|
|
|
|
logs_bloom: payload.logs_bloom,
|
|
|
|
prev_randao: payload.prev_randao,
|
|
|
|
block_number: payload.block_number,
|
|
|
|
gas_limit: payload.gas_limit,
|
|
|
|
gas_used: payload.gas_used,
|
|
|
|
timestamp: payload.timestamp,
|
|
|
|
extra_data: payload.extra_data,
|
|
|
|
base_fee_per_gas: payload.base_fee_per_gas,
|
|
|
|
block_hash: payload.block_hash,
|
|
|
|
transactions_root: hash_tree_root(payload.transactions),
|
|
|
|
withdrawals_root: hash_tree_root(payload.withdrawals)),
|
|
|
|
execution_branch: blck.message.body.build_proof(
|
|
|
|
capella.EXECUTION_PAYLOAD_INDEX).get)
|
|
|
|
|
2023-03-10 17:16:26 +00:00
|
|
|
func toDenebLightClientHeader(
|
2023-01-19 22:53:37 +00:00
|
|
|
blck: # `SomeSignedBeaconBlock` doesn't work here (Nim 1.6)
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.SignedBeaconBlock | deneb.TrustedSignedBeaconBlock
|
|
|
|
): deneb.LightClientHeader =
|
2023-01-19 22:53:37 +00:00
|
|
|
template payload: untyped = blck.message.body.execution_payload
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.LightClientHeader(
|
2023-01-19 22:53:37 +00:00
|
|
|
beacon: blck.message.toBeaconBlockHeader(),
|
2023-02-20 08:45:49 +00:00
|
|
|
execution: deneb.ExecutionPayloadHeader(
|
2023-01-19 22:53:37 +00:00
|
|
|
parent_hash: payload.parent_hash,
|
|
|
|
fee_recipient: payload.fee_recipient,
|
|
|
|
state_root: payload.state_root,
|
|
|
|
receipts_root: payload.receipts_root,
|
|
|
|
logs_bloom: payload.logs_bloom,
|
|
|
|
prev_randao: payload.prev_randao,
|
|
|
|
block_number: payload.block_number,
|
|
|
|
gas_limit: payload.gas_limit,
|
|
|
|
gas_used: payload.gas_used,
|
|
|
|
timestamp: payload.timestamp,
|
|
|
|
extra_data: payload.extra_data,
|
|
|
|
base_fee_per_gas: payload.base_fee_per_gas,
|
|
|
|
excess_data_gas: payload.excess_data_gas,
|
|
|
|
block_hash: payload.block_hash,
|
|
|
|
transactions_root: hash_tree_root(payload.transactions),
|
|
|
|
withdrawals_root: hash_tree_root(payload.withdrawals)),
|
|
|
|
execution_branch: blck.message.body.build_proof(
|
|
|
|
capella.EXECUTION_PAYLOAD_INDEX).get)
|
|
|
|
|
2023-01-13 15:46:35 +00:00
|
|
|
func toLightClientHeader*(
|
|
|
|
blck: # `SomeSignedBeaconBlock` doesn't work here (Nim 1.6)
|
|
|
|
phase0.SignedBeaconBlock | phase0.TrustedSignedBeaconBlock |
|
|
|
|
altair.SignedBeaconBlock | altair.TrustedSignedBeaconBlock |
|
|
|
|
bellatrix.SignedBeaconBlock | bellatrix.TrustedSignedBeaconBlock |
|
|
|
|
capella.SignedBeaconBlock | capella.TrustedSignedBeaconBlock |
|
2023-02-20 08:45:49 +00:00
|
|
|
deneb.SignedBeaconBlock | deneb.TrustedSignedBeaconBlock,
|
2023-01-13 15:46:35 +00:00
|
|
|
kind: static LightClientDataFork): auto =
|
2023-03-10 17:16:26 +00:00
|
|
|
when kind == LightClientDataFork.Deneb:
|
|
|
|
blck.toDenebLightClientHeader()
|
2023-01-19 22:53:37 +00:00
|
|
|
elif kind == LightClientDataFork.Capella:
|
2023-01-18 14:32:57 +00:00
|
|
|
blck.toCapellaLightClientHeader()
|
|
|
|
elif kind == LightClientDataFork.Altair:
|
2023-01-14 21:19:50 +00:00
|
|
|
blck.toAltairLightClientHeader()
|
|
|
|
else:
|
|
|
|
static: raiseAssert "Unreachable"
|