2021-11-10 11:39:08 +00:00
|
|
|
import
|
|
|
|
unittest2,
|
|
|
|
stew/byteutils,
|
|
|
|
../beacon_chain/spec/[forks, helpers],
|
2022-01-18 13:36:52 +00:00
|
|
|
../beacon_chain/spec/datatypes/[phase0, altair, bellatrix]
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
template testHashedBeaconState(T: type, s: Slot) =
|
|
|
|
let
|
|
|
|
state = (ref T)()
|
|
|
|
state[].slot = s
|
|
|
|
let
|
|
|
|
bytes = SSZ.encode(state[])
|
|
|
|
forked = (ref ForkedHashedBeaconState)()
|
|
|
|
|
|
|
|
forked[] = readSszForkedHashedBeaconState(cfg, bytes)
|
|
|
|
|
|
|
|
check:
|
|
|
|
forked.kind == T.toFork()
|
|
|
|
|
|
|
|
template testTrustedSignedBeaconBlock(T: type, s: Slot) =
|
|
|
|
let
|
|
|
|
blck = (ref T)()
|
|
|
|
|
|
|
|
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:
|
|
|
|
forked.kind == T.toFork()
|
|
|
|
|
2023-01-28 19:53:41 +00:00
|
|
|
suite "Type helpers":
|
|
|
|
test "BeaconBlockType":
|
|
|
|
check:
|
|
|
|
BeaconBlockType(ConsensusFork.Phase0) is phase0.BeaconBlock
|
|
|
|
BeaconBlockType(ConsensusFork.Bellatrix) is bellatrix.BeaconBlock
|
|
|
|
BeaconBlockBodyType(ConsensusFork.Altair) is altair.BeaconBlockBody
|
|
|
|
BeaconBlockBodyType(ConsensusFork.Bellatrix) is bellatrix.BeaconBlockBody
|
|
|
|
|
2021-11-10 11:39:08 +00:00
|
|
|
suite "Forked SSZ readers":
|
|
|
|
var
|
|
|
|
cfg = defaultRuntimeConfig
|
|
|
|
|
|
|
|
cfg.ALTAIR_FORK_EPOCH = Epoch(1)
|
2022-02-02 13:06:55 +00:00
|
|
|
cfg.BELLATRIX_FORK_EPOCH = Epoch(2)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
test "load phase0 state":
|
|
|
|
testHashedBeaconState(phase0.BeaconState, 0.Slot)
|
|
|
|
|
|
|
|
expect(SszError):
|
|
|
|
testHashedBeaconState(altair.BeaconState, 0.Slot)
|
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testHashedBeaconState(bellatrix.BeaconState, 0.Slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
test "load altair state":
|
2022-01-11 10:01:54 +00:00
|
|
|
testHashedBeaconState(altair.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
expect(SszError):
|
2022-01-11 10:01:54 +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)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
2022-01-26 12:21:29 +00:00
|
|
|
test "load bellatrix state":
|
2022-02-02 13:06:55 +00:00
|
|
|
testHashedBeaconState(bellatrix.BeaconState, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
expect(SszError):
|
2022-02-02 13:06:55 +00:00
|
|
|
testHashedBeaconState(phase0.BeaconState, cfg.BELLATRIX_FORK_EPOCH.start_slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
expect(SszError):
|
2022-02-02 13:06:55 +00:00
|
|
|
testHashedBeaconState(altair.BeaconState, cfg.BELLATRIX_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":
|
|
|
|
testTrustedSignedBeaconBlock(phase0.TrustedSignedBeaconBlock, 0.Slot)
|
|
|
|
expect(SszError):
|
|
|
|
testTrustedSignedBeaconBlock(altair.TrustedSignedBeaconBlock, 0.Slot)
|
|
|
|
expect(SszError):
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(bellatrix.TrustedSignedBeaconBlock, 0.Slot)
|
2021-11-10 11:39:08 +00:00
|
|
|
|
|
|
|
test "load altair block":
|
2022-01-18 13:36:52 +00:00
|
|
|
testTrustedSignedBeaconBlock(
|
|
|
|
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(
|
|
|
|
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)
|
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(
|
2022-02-02 13:06:55 +00:00
|
|
|
phase0.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(
|
2022-02-02 13:06:55 +00:00
|
|
|
altair.TrustedSignedBeaconBlock, cfg.BELLATRIX_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))
|