eth2.0-specs/tests/phase0/block_processing/test_process_block_header.py

72 lines
1.9 KiB
Python
Raw Normal View History

2019-03-19 21:00:29 +00:00
from copy import deepcopy
import pytest
from build.phase0.spec import (
get_beacon_proposer_index,
2019-03-20 14:47:41 +00:00
cache_state,
advance_slot,
2019-03-19 21:00:29 +00:00
process_block_header,
)
from tests.phase0.helpers import (
build_empty_block_for_next_slot,
)
2019-03-19 21:49:01 +00:00
# mark entire file as 'header'
pytestmark = pytest.mark.header
2019-03-19 21:00:29 +00:00
def prepare_state_for_header_processing(state):
cache_state(state)
advance_slot(state)
2019-03-20 14:47:41 +00:00
def run_block_header_processing(state, block, valid=True):
"""
Run ``process_block_header`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
"""
prepare_state_for_header_processing(state)
post_state = deepcopy(state)
2019-03-20 14:47:41 +00:00
if not valid:
with pytest.raises(AssertionError):
process_block_header(post_state, block)
return state, None
2019-03-20 14:47:41 +00:00
process_block_header(post_state, block)
return state, post_state
2019-03-20 14:47:41 +00:00
def test_success(state):
block = build_empty_block_for_next_slot(state)
pre_state, post_state = run_block_header_processing(state, block)
return state, block, post_state
2019-03-20 14:47:41 +00:00
def test_invalid_slot(state):
block = build_empty_block_for_next_slot(state)
block.slot = state.slot + 2 # invalid slot
2019-03-20 14:47:41 +00:00
pre_state, post_state = run_block_header_processing(state, block, valid=False)
return pre_state, block, None
2019-03-20 14:47:41 +00:00
def test_invalid_previous_block_root(state):
block = build_empty_block_for_next_slot(state)
block.previous_block_root = b'\12'*32 # invalid prev root
2019-03-20 14:47:41 +00:00
pre_state, post_state = run_block_header_processing(state, block, valid=False)
return pre_state, block, None
2019-03-20 14:47:41 +00:00
def test_proposer_slashed(state):
# set proposer to slashed
proposer_index = get_beacon_proposer_index(state, state.slot + 1)
state.validator_registry[proposer_index].slashed = True
2019-03-20 14:47:41 +00:00
block = build_empty_block_for_next_slot(state)
2019-03-20 14:47:41 +00:00
pre_state, post_state = run_block_header_processing(state, block, valid=False)
return pre_state, block, None