2021-11-18 12:02:43 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-11-18 12:02:43 +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.
|
|
|
|
|
2024-02-29 13:24:08 +00:00
|
|
|
{.push raises: [].}
|
2021-11-18 12:02:43 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
# Test for spec functions and helpers outside of the EF test vectors - mainly
|
|
|
|
# helpers that extend or make the spec functions usable outside of the state
|
|
|
|
# transition functions
|
|
|
|
|
|
|
|
import
|
|
|
|
unittest2,
|
2023-10-24 01:58:52 +00:00
|
|
|
../beacon_chain/spec/beaconstate,
|
2021-11-18 12:02:43 +00:00
|
|
|
./testutil, ./testblockutil
|
|
|
|
|
2023-10-24 01:58:52 +00:00
|
|
|
from ../beacon_chain/spec/state_transition import process_slots
|
|
|
|
|
2021-11-18 12:02:43 +00:00
|
|
|
suite "Beacon state" & preset():
|
2022-03-15 08:24:55 +00:00
|
|
|
setup:
|
|
|
|
let cfg = defaultRuntimeConfig
|
|
|
|
|
2021-11-18 12:02:43 +00:00
|
|
|
test "Smoke test initialize_beacon_state_from_eth1" & preset():
|
|
|
|
let state = newClone(initialize_beacon_state_from_eth1(
|
2023-10-24 01:58:52 +00:00
|
|
|
cfg, ZERO_HASH, 0, makeInitialDeposits(SLOTS_PER_EPOCH, {}),
|
|
|
|
default(bellatrix.ExecutionPayloadHeader), {}))
|
2021-11-18 12:02:43 +00:00
|
|
|
check: state.validators.lenu64 == SLOTS_PER_EPOCH
|
|
|
|
|
2022-01-17 11:19:58 +00:00
|
|
|
test "process_slots":
|
|
|
|
var
|
|
|
|
state = (ref ForkedHashedBeaconState)(
|
2023-01-28 19:53:41 +00:00
|
|
|
kind: ConsensusFork.Phase0,
|
2022-01-17 11:19:58 +00:00
|
|
|
phase0Data: initialize_hashed_beacon_state_from_eth1(
|
2022-06-18 04:57:37 +00:00
|
|
|
defaultRuntimeConfig, ZERO_HASH, 0,
|
2022-01-17 11:19:58 +00:00
|
|
|
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipBlsValidation}))
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
check:
|
|
|
|
process_slots(cfg, state[], Slot 1, cache, info, {}).isOk()
|
|
|
|
process_slots(cfg, state[], Slot 1, cache, info, {}).isErr()
|
|
|
|
process_slots(cfg, state[], Slot 1, cache, info, {slotProcessed}).isOk()
|
|
|
|
|
2021-11-18 12:02:43 +00:00
|
|
|
test "latest_block_root":
|
|
|
|
var
|
|
|
|
state = (ref ForkedHashedBeaconState)(
|
2023-01-28 19:53:41 +00:00
|
|
|
kind: ConsensusFork.Phase0,
|
2021-11-18 12:02:43 +00:00
|
|
|
phase0Data: initialize_hashed_beacon_state_from_eth1(
|
2022-06-18 04:57:37 +00:00
|
|
|
defaultRuntimeConfig, ZERO_HASH, 0,
|
2021-11-18 12:02:43 +00:00
|
|
|
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipBlsValidation}))
|
|
|
|
genBlock = get_initial_beacon_block(state[])
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
|
|
|
|
check: # Works for genesis block
|
2022-03-16 07:20:40 +00:00
|
|
|
state[].phase0Data.latest_block_root == genBlock.root
|
2022-03-16 15:00:18 +00:00
|
|
|
state[].phase0Data.latest_block_id == genBlock.toBlockId()
|
|
|
|
|
2022-01-17 11:19:58 +00:00
|
|
|
process_slots(cfg, state[], Slot 1, cache, info, {}).isOk()
|
2022-03-16 07:20:40 +00:00
|
|
|
state[].phase0Data.latest_block_root == genBlock.root
|
2021-11-18 12:02:43 +00:00
|
|
|
|
|
|
|
let blck = addTestBlock(
|
|
|
|
state[], cache, nextSlot = false, flags = {skipBlsValidation}).phase0Data
|
|
|
|
|
|
|
|
check: # Works for random blocks
|
2022-03-16 07:20:40 +00:00
|
|
|
state[].phase0Data.latest_block_root == blck.root
|
2022-01-17 11:19:58 +00:00
|
|
|
process_slots(cfg, state[], Slot 2, cache, info, {}).isOk()
|
2022-03-16 07:20:40 +00:00
|
|
|
state[].phase0Data.latest_block_root == blck.root
|
2021-11-18 12:02:43 +00:00
|
|
|
|
|
|
|
test "get_beacon_proposer_index":
|
|
|
|
var
|
|
|
|
state = (ref ForkedHashedBeaconState)(
|
2023-01-28 19:53:41 +00:00
|
|
|
kind: ConsensusFork.Phase0,
|
2021-11-18 12:02:43 +00:00
|
|
|
phase0Data: initialize_hashed_beacon_state_from_eth1(
|
2022-06-18 04:57:37 +00:00
|
|
|
defaultRuntimeConfig, ZERO_HASH, 0,
|
2021-11-18 12:02:43 +00:00
|
|
|
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipBlsValidation}))
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
|
|
|
|
check:
|
|
|
|
get_beacon_proposer_index(state[].phase0Data.data, cache, Slot 1).isSome()
|
|
|
|
get_beacon_proposer_index(
|
2022-01-11 10:01:54 +00:00
|
|
|
state[].phase0Data.data, cache, Epoch(1).start_slot()).isNone()
|
2021-11-18 12:02:43 +00:00
|
|
|
get_beacon_proposer_index(
|
2022-01-11 10:01:54 +00:00
|
|
|
state[].phase0Data.data, cache, Epoch(2).start_slot()).isNone()
|
2021-11-18 12:02:43 +00:00
|
|
|
|
|
|
|
check:
|
2022-01-17 11:19:58 +00:00
|
|
|
process_slots(cfg, state[], Epoch(1).start_slot(), cache, info, {}).isOk()
|
2021-11-18 12:02:43 +00:00
|
|
|
get_beacon_proposer_index(state[].phase0Data.data, cache, Slot 1).isNone()
|
|
|
|
get_beacon_proposer_index(
|
2022-01-11 10:01:54 +00:00
|
|
|
state[].phase0Data.data, cache, Epoch(1).start_slot()).isSome()
|
2021-11-18 12:02:43 +00:00
|
|
|
get_beacon_proposer_index(
|
2022-01-11 10:01:54 +00:00
|
|
|
state[].phase0Data.data, cache, Epoch(2).start_slot()).isNone()
|
2022-03-15 08:24:55 +00:00
|
|
|
|
|
|
|
test "dependent_root":
|
|
|
|
var
|
|
|
|
state = (ref ForkedHashedBeaconState)(
|
2023-01-28 19:53:41 +00:00
|
|
|
kind: ConsensusFork.Phase0,
|
2022-03-15 08:24:55 +00:00
|
|
|
phase0Data: initialize_hashed_beacon_state_from_eth1(
|
2022-06-18 04:57:37 +00:00
|
|
|
defaultRuntimeConfig, ZERO_HASH, 0,
|
2022-03-15 08:24:55 +00:00
|
|
|
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipBlsValidation}))
|
|
|
|
genBlock = get_initial_beacon_block(state[])
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
|
|
|
|
check:
|
|
|
|
state[].phase0Data.dependent_root(Epoch(0)) == genBlock.root
|
|
|
|
|
|
|
|
while getStateField(state[], slot).epoch < Epoch(1):
|
|
|
|
discard addTestBlock(state[], cache)
|
|
|
|
|
|
|
|
check:
|
|
|
|
state[].phase0Data.dependent_root(Epoch(1)) ==
|
|
|
|
state[].phase0Data.data.get_block_root_at_slot(Epoch(1).start_slot - 1)
|
|
|
|
state[].phase0Data.dependent_root(Epoch(0)) == genBlock.root
|
|
|
|
|
|
|
|
while getStateField(state[], slot).epoch < Epoch(2):
|
|
|
|
discard addTestBlock(state[], cache)
|
|
|
|
|
|
|
|
check:
|
|
|
|
state[].phase0Data.dependent_root(Epoch(2)) ==
|
|
|
|
state[].phase0Data.data.get_block_root_at_slot(Epoch(2).start_slot - 1)
|
|
|
|
state[].phase0Data.dependent_root(Epoch(1)) ==
|
|
|
|
state[].phase0Data.data.get_block_root_at_slot(Epoch(1).start_slot - 1)
|
|
|
|
state[].phase0Data.dependent_root(Epoch(0)) == genBlock.root
|
2022-06-29 16:53:59 +00:00
|
|
|
|
|
|
|
test "merklizer state roundtrip":
|
|
|
|
let
|
|
|
|
dcs = DepositContractState()
|
|
|
|
merkleizer = DepositsMerkleizer.init(dcs)
|
|
|
|
|
|
|
|
check:
|
|
|
|
dcs == merkleizer.toDepositContractState()
|
2022-11-10 10:44:47 +00:00
|
|
|
|
|
|
|
test "can_advance_slots":
|
|
|
|
var
|
|
|
|
state = (ref ForkedHashedBeaconState)(
|
2023-01-28 19:53:41 +00:00
|
|
|
kind: ConsensusFork.Phase0,
|
2022-11-10 10:44:47 +00:00
|
|
|
phase0Data: initialize_hashed_beacon_state_from_eth1(
|
|
|
|
defaultRuntimeConfig, ZERO_HASH, 0,
|
|
|
|
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipBlsValidation}))
|
|
|
|
genBlock = get_initial_beacon_block(state[])
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
|
|
|
|
check:
|
|
|
|
state[].can_advance_slots(genBlock.root, Slot(0))
|
|
|
|
state[].can_advance_slots(genBlock.root, Slot(0))
|
|
|
|
state[].can_advance_slots(genBlock.root, Slot(0))
|
|
|
|
|
|
|
|
let blck = addTestBlock(
|
|
|
|
state[], cache, flags = {skipBlsValidation})
|
|
|
|
|
|
|
|
check:
|
|
|
|
not state[].can_advance_slots(genBlock.root, Slot(0))
|
|
|
|
not state[].can_advance_slots(genBlock.root, Slot(0))
|
|
|
|
not state[].can_advance_slots(genBlock.root, Slot(0))
|
|
|
|
not state[].can_advance_slots(blck.root, Slot(0))
|
|
|
|
state[].can_advance_slots(blck.root, Slot(1))
|
|
|
|
state[].can_advance_slots(blck.root, Slot(2))
|