eth2.0-specs/test_libs/pyspec/eth2spec/test/helpers/state.py

87 lines
3.1 KiB
Python
Raw Normal View History

2019-06-30 22:33:10 +00:00
from copy import deepcopy
2019-11-21 22:13:45 +00:00
from eth2spec.test.context import expect_assertion_error
2019-06-30 22:33:10 +00:00
from eth2spec.test.helpers.attestations import get_valid_attestation
2019-11-21 22:13:45 +00:00
from eth2spec.test.helpers.block import sign_block, build_empty_block_for_next_slot, transition_unsigned_block
2019-05-15 16:36:32 +00:00
def get_balance(state, index):
return state.balances[index]
2019-05-30 20:57:18 +00:00
def next_slot(spec, state):
2019-05-15 16:36:32 +00:00
"""
Transition to the next slot.
"""
2019-05-30 20:57:18 +00:00
spec.process_slots(state, state.slot + 1)
2019-05-15 16:36:32 +00:00
def transition_to(spec, state, slot):
"""
Transition to ``slot``.
"""
assert state.slot <= slot
for _ in range(slot - state.slot):
next_slot(spec, state)
assert state.slot == slot
2019-05-30 20:57:18 +00:00
def next_epoch(spec, state):
2019-05-15 16:36:32 +00:00
"""
Transition to the start slot of the next epoch
"""
slot = state.slot + spec.SLOTS_PER_EPOCH - (state.slot % spec.SLOTS_PER_EPOCH)
2019-05-30 20:57:18 +00:00
spec.process_slots(state, slot)
2019-05-15 16:36:32 +00:00
2019-05-30 20:57:18 +00:00
def get_state_root(spec, state, slot) -> bytes:
2019-05-15 16:36:32 +00:00
"""
Return the state root at a recent ``slot``.
"""
assert slot < state.slot <= slot + spec.SLOTS_PER_HISTORICAL_ROOT
2019-06-09 19:41:21 +00:00
return state.state_roots[slot % spec.SLOTS_PER_HISTORICAL_ROOT]
2019-11-21 22:13:45 +00:00
def state_transition_and_sign_block(spec, state, block, expect_fail=False):
"""
State transition via the provided ``block``
2019-11-21 22:13:45 +00:00
then package the block with the correct state root and signature.
"""
2019-11-21 22:13:45 +00:00
if expect_fail:
expect_assertion_error(lambda: transition_unsigned_block(spec, state, block))
else:
transition_unsigned_block(spec, state, block)
block.state_root = state.hash_tree_root()
2019-11-21 22:13:45 +00:00
return sign_block(spec, state, block)
2019-06-30 22:33:10 +00:00
def next_epoch_with_attestations(spec,
state,
fill_cur_epoch,
fill_prev_epoch):
assert state.slot % spec.SLOTS_PER_EPOCH == 0
post_state = deepcopy(state)
2019-11-21 22:13:45 +00:00
signed_blocks = []
2019-06-30 22:33:10 +00:00
for _ in range(spec.SLOTS_PER_EPOCH):
block = build_empty_block_for_next_slot(spec, post_state)
if fill_cur_epoch and post_state.slot >= spec.MIN_ATTESTATION_INCLUSION_DELAY:
slot_to_attest = post_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + 1
2019-10-17 08:47:51 +00:00
committees_per_slot = spec.get_committee_count_at_slot(state, slot_to_attest)
2019-10-23 00:37:15 +00:00
if slot_to_attest >= spec.compute_start_slot_at_epoch(spec.get_current_epoch(post_state)):
2019-10-16 09:47:19 +00:00
for index in range(committees_per_slot):
2019-11-21 22:13:45 +00:00
cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index, signed=True)
block.body.attestations.append(cur_attestation)
2019-06-30 22:33:10 +00:00
if fill_prev_epoch:
slot_to_attest = post_state.slot - spec.SLOTS_PER_EPOCH + 1
2019-10-17 08:47:51 +00:00
committees_per_slot = spec.get_committee_count_at_slot(state, slot_to_attest)
2019-10-16 09:47:19 +00:00
for index in range(committees_per_slot):
2019-11-21 22:13:45 +00:00
prev_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index, signed=True)
block.body.attestations.append(prev_attestation)
2019-06-30 22:33:10 +00:00
2019-11-21 22:13:45 +00:00
signed_block = state_transition_and_sign_block(spec, post_state, block)
signed_blocks.append(signed_block)
2019-06-30 22:33:10 +00:00
2019-11-21 22:13:45 +00:00
return state, signed_blocks, post_state