2023-09-27 15:10:28 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2021-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-03-29 13:22:19 +00:00
|
|
|
{.used.}
|
|
|
|
|
2021-11-10 11:39:08 +00:00
|
|
|
import
|
|
|
|
unittest2,
|
2023-03-29 13:22:19 +00:00
|
|
|
../beacon_chain/spec/forks
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
template testHashedBeaconState(T: type, s: Slot) =
|
2023-03-29 13:22:19 +00:00
|
|
|
let state = (ref T)()
|
2021-11-10 11:39:08 +00:00
|
|
|
state[].slot = s
|
|
|
|
let
|
|
|
|
bytes = SSZ.encode(state[])
|
|
|
|
forked = (ref ForkedHashedBeaconState)()
|
|
|
|
|
|
|
|
forked[] = readSszForkedHashedBeaconState(cfg, bytes)
|
|
|
|
|
|
|
|
check:
|
2023-09-27 15:10:28 +00:00
|
|
|
forked.kind == T.kind
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
template testTrustedSignedBeaconBlock(T: type, s: Slot) =
|
2023-03-29 13:22:19 +00:00
|
|
|
let blck = (ref T)()
|
2021-11-10 11:39:08 +00:00
|
|
|
blck[].message.slot = s
|
|
|
|
let
|
|
|
|
bytes = SSZ.encode(blck[])
|
2022-01-06 07:38:40 +00:00
|
|
|
forked = (ref ForkedSignedBeaconBlock)()
|
2021-11-10 11:39:08 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
forked[] = readSszForkedSignedBeaconBlock(cfg, bytes)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
check:
|
2023-09-27 15:10:28 +00:00
|
|
|
forked.kind == T.kind
|
2021-11-10 11:39:08 +00:00
|
|
|
|
2023-01-28 19:53:41 +00:00
|
|
|
suite "Type helpers":
|
2023-10-05 12:01:40 +00:00
|
|
|
test "BeaconBlock":
|
2023-01-28 19:53:41 +00:00
|
|
|
check:
|
2023-10-05 12:01:40 +00:00
|
|
|
ConsensusFork.Phase0.BeaconBlock is phase0.BeaconBlock
|
|
|
|
ConsensusFork.Altair.BeaconBlock is altair.BeaconBlock
|
|
|
|
ConsensusFork.Bellatrix.BeaconBlock is bellatrix.BeaconBlock
|
|
|
|
ConsensusFork.Capella.BeaconBlock is capella.BeaconBlock
|
|
|
|
ConsensusFork.Deneb.BeaconBlock is deneb.BeaconBlock
|
|
|
|
ConsensusFork.Phase0.BeaconBlockBody is phase0.BeaconBlockBody
|
|
|
|
ConsensusFork.Altair.BeaconBlockBody is altair.BeaconBlockBody
|
|
|
|
ConsensusFork.Bellatrix.BeaconBlockBody is bellatrix.BeaconBlockBody
|
|
|
|
ConsensusFork.Capella.BeaconBlockBody is capella.BeaconBlockBody
|
|
|
|
ConsensusFork.Deneb.BeaconBlockBody is deneb.BeaconBlockBody
|
2023-01-28 19:53:41 +00:00
|
|
|
|
2021-11-10 11:39:08 +00:00
|
|
|
suite "Forked SSZ readers":
|
2023-03-29 13:22:19 +00:00
|
|
|
let cfg = block:
|
|
|
|
var cfg = defaultRuntimeConfig
|
|
|
|
cfg.ALTAIR_FORK_EPOCH = Epoch(1)
|
|
|
|
cfg.BELLATRIX_FORK_EPOCH = Epoch(2)
|
|
|
|
cfg.CAPELLA_FORK_EPOCH = Epoch(3)
|
|
|
|
cfg.DENEB_FORK_EPOCH = Epoch(4)
|
|
|
|
cfg
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
test "load phase0 state":
|
2023-03-29 13:22:19 +00:00
|
|
|
testHashedBeaconState(phase0.BeaconState, 0.Slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
expect(SszError):
|
2023-03-29 13:22:19 +00:00
|
|
|
testHashedBeaconState(altair.BeaconState, 0.Slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testHashedBeaconState(bellatrix.BeaconState, 0.Slot)
|
2023-03-29 13:22:19 +00:00
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(capella.BeaconState, 0.Slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(deneb.BeaconState, 0.Slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
test "load altair state":
|
2023-03-29 13:22:19 +00:00
|
|
|
testHashedBeaconState(altair.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
expect(SszError):
|
2023-03-29 13:22:19 +00:00
|
|
|
testHashedBeaconState(phase0.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testHashedBeaconState(bellatrix.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2023-03-29 13:22:19 +00:00
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(capella.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(deneb.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
2022-01-26 12:21:29 +00:00
|
|
|
test "load bellatrix state":
|
2023-03-29 13:22:19 +00:00
|
|
|
testHashedBeaconState(bellatrix.BeaconState, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(phase0.BeaconState, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(altair.BeaconState, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(capella.BeaconState, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(deneb.BeaconState, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
|
|
|
|
test "load capella state":
|
|
|
|
testHashedBeaconState(capella.BeaconState, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
expect(SszError):
|
2023-03-29 13:22:19 +00:00
|
|
|
testHashedBeaconState(phase0.BeaconState, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2023-03-29 13:22:19 +00:00
|
|
|
testHashedBeaconState(altair.BeaconState, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(bellatrix.BeaconState, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(deneb.BeaconState, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
|
|
|
|
|
|
|
test "load deneb state":
|
|
|
|
testHashedBeaconState(deneb.BeaconState, cfg.DENEB_FORK_EPOCH.start_slot)
|
|
|
|
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(phase0.BeaconState, cfg.DENEB_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(altair.BeaconState, cfg.DENEB_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(bellatrix.BeaconState, cfg.DENEB_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(capella.BeaconState, cfg.DENEB_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
test "should raise on unknown data":
|
|
|
|
let
|
|
|
|
bytes = SSZ.encode(AttestationData())
|
|
|
|
expect(SszError):
|
|
|
|
discard newClone(readSszForkedHashedBeaconState(cfg, bytes))
|
|
|
|
|
|
|
|
test "load phase0 block":
|
2023-03-29 13:22:19 +00:00
|
|
|
testTrustedSignedBeaconBlock(phase0.TrustedSignedBeaconBlock, 0.Slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2023-03-29 13:22:19 +00:00
|
|
|
testTrustedSignedBeaconBlock(altair.TrustedSignedBeaconBlock, 0.Slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(bellatrix.TrustedSignedBeaconBlock, 0.Slot)
|
2023-03-29 13:22:19 +00:00
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(capella.TrustedSignedBeaconBlock, 0.Slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(deneb.TrustedSignedBeaconBlock, 0.Slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
test "load altair block":
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(
|
2023-03-29 13:22:19 +00:00
|
|
|
altair.TrustedSignedBeaconBlock, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(
|
2023-03-29 13:22:19 +00:00
|
|
|
phase0.TrustedSignedBeaconBlock, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
bellatrix.TrustedSignedBeaconBlock, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2023-03-29 13:22:19 +00:00
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
capella.TrustedSignedBeaconBlock, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
deneb.TrustedSignedBeaconBlock, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
2022-01-26 12:21:29 +00:00
|
|
|
test "load bellatrix block":
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(
|
2022-02-02 13:06:55 +00:00
|
|
|
bellatrix.TrustedSignedBeaconBlock, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(
|
2023-03-29 13:22:19 +00:00
|
|
|
phase0.TrustedSignedBeaconBlock, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
altair.TrustedSignedBeaconBlock, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
capella.TrustedSignedBeaconBlock, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
deneb.TrustedSignedBeaconBlock, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
|
|
|
|
|
|
|
test "load capella block":
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
capella.TrustedSignedBeaconBlock, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
|
|
|
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
phase0.TrustedSignedBeaconBlock, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
altair.TrustedSignedBeaconBlock, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
bellatrix.TrustedSignedBeaconBlock, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
deneb.TrustedSignedBeaconBlock, cfg.CAPELLA_FORK_EPOCH.start_slot)
|
|
|
|
|
|
|
|
test "load deneb block":
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
deneb.TrustedSignedBeaconBlock, cfg.DENEB_FORK_EPOCH.start_slot)
|
|
|
|
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
phase0.TrustedSignedBeaconBlock, cfg.DENEB_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
altair.TrustedSignedBeaconBlock, cfg.DENEB_FORK_EPOCH.start_slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
bellatrix.TrustedSignedBeaconBlock, cfg.DENEB_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(
|
2023-03-29 13:22:19 +00:00
|
|
|
capella.TrustedSignedBeaconBlock, cfg.DENEB_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
test "should raise on unknown data":
|
|
|
|
let
|
|
|
|
bytes = SSZ.encode(AttestationData())
|
|
|
|
expect(SszError):
|
2022-01-06 07:38:40 +00:00
|
|
|
discard newClone(readSszForkedSignedBeaconBlock(cfg, bytes))
|