eth2.0-specs/test_libs/pyspec/eth2spec/test/test_sanity.py

414 lines
12 KiB
Python
Raw Normal View History

2019-03-18 18:51:52 +00:00
from copy import deepcopy
2019-03-18 18:51:52 +00:00
from py_ecc import bls
2019-04-03 03:18:17 +00:00
import eth2spec.phase0.spec as spec
2019-03-18 18:51:52 +00:00
from eth2spec.utils.minimal_ssz import signing_root
2019-04-03 03:18:17 +00:00
from eth2spec.phase0.spec import (
2019-03-18 18:51:52 +00:00
# SSZ
VoluntaryExit,
# functions
get_active_validator_indices,
2019-04-03 00:04:12 +00:00
get_beacon_proposer_index,
2019-04-24 05:17:25 +00:00
get_block_root_at_slot,
2019-03-18 18:51:52 +00:00
get_current_epoch,
get_domain,
advance_slot,
cache_state,
)
2019-04-03 03:18:17 +00:00
from eth2spec.phase0.state_transition import (
2019-03-18 18:51:52 +00:00
state_transition,
)
2019-03-27 18:30:47 +00:00
from .helpers import (
2019-04-20 05:17:33 +00:00
get_balance,
build_empty_block_for_next_slot,
get_state_root,
get_valid_attestation,
get_valid_attester_slashing,
2019-03-21 23:08:54 +00:00
get_valid_proposer_slashing,
get_valid_transfer,
prepare_state_and_deposit,
2019-03-25 17:25:33 +00:00
privkeys,
pubkeys,
)
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
from .context import spec_state_test
2019-03-18 18:51:52 +00:00
2019-03-19 03:39:19 +00:00
2019-05-06 15:43:02 +00:00
@spec_state_test
2019-03-18 18:51:52 +00:00
def test_slot_transition(state):
2019-05-06 15:43:02 +00:00
pre_slot = state.slot
pre_root = state.hash_tree_root()
yield 'pre', state
cache_state(state)
advance_slot(state)
yield 'post', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
assert state.slot == pre_slot + 1
assert get_state_root(state, pre_slot) == pre_root
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
@spec_state_test
2019-03-18 18:51:52 +00:00
def test_empty_block_transition(state):
2019-05-06 15:43:02 +00:00
pre_slot = state.slot
pre_eth1_votes = len(state.eth1_data_votes)
yield 'pre', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
block = build_empty_block_for_next_slot(state)
yield 'blocks', [block], [spec.BeaconBlock]
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
state_transition(state, block)
yield 'post', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
assert len(state.eth1_data_votes) == pre_eth1_votes + 1
assert get_block_root_at_slot(state, pre_slot) == block.previous_block_root
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
@spec_state_test
2019-03-18 18:51:52 +00:00
def test_skipped_slots(state):
2019-05-06 15:43:02 +00:00
pre_slot = state.slot
yield 'pre', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
block = build_empty_block_for_next_slot(state)
block.slot += 3
yield 'blocks', [block], [spec.BeaconBlock]
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
state_transition(state, block)
yield 'post', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
assert state.slot == block.slot
for slot in range(pre_slot, state.slot):
assert get_block_root_at_slot(state, slot) == block.previous_block_root
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
@spec_state_test
2019-03-18 18:51:52 +00:00
def test_empty_epoch_transition(state):
2019-05-06 15:43:02 +00:00
pre_slot = state.slot
yield 'pre', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
block = build_empty_block_for_next_slot(state)
block.slot += spec.SLOTS_PER_EPOCH
yield 'blocks', [block], [spec.BeaconBlock]
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
state_transition(state, block)
yield 'post', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
assert state.slot == block.slot
for slot in range(pre_slot, state.slot):
assert get_block_root_at_slot(state, slot) == block.previous_block_root
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
@spec_state_test
2019-03-18 18:51:52 +00:00
def test_empty_epoch_transition_not_finalizing(state):
2019-05-06 15:43:02 +00:00
# copy for later balance lookups.
pre_state = deepcopy(state)
yield 'pre', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
block = build_empty_block_for_next_slot(state)
block.slot += spec.SLOTS_PER_EPOCH * 5
yield 'blocks', [block], [spec.BeaconBlock]
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
state_transition(state, block)
yield 'post', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
assert state.slot == block.slot
assert state.finalized_epoch < get_current_epoch(state) - 4
for index in range(len(state.validator_registry)):
assert get_balance(state, index) < get_balance(pre_state, index)
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
@spec_state_test
2019-03-25 17:25:33 +00:00
def test_proposer_slashing(state):
2019-05-06 15:43:02 +00:00
# copy for later balance lookups.
pre_state = deepcopy(state)
2019-03-21 23:08:54 +00:00
proposer_slashing = get_valid_proposer_slashing(state)
validator_index = proposer_slashing.proposer_index
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
assert not state.validator_registry[validator_index].slashed
yield 'pre', state
2019-03-18 18:51:52 +00:00
#
# Add to state via block transition
#
2019-05-06 15:43:02 +00:00
block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
block.body.proposer_slashings.append(proposer_slashing)
2019-05-06 15:43:02 +00:00
yield 'blocks', [block], [spec.BeaconBlock]
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
state_transition(state, block)
yield 'post', state
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
# check if slashed
slashed_validator = state.validator_registry[validator_index]
2019-03-18 18:51:52 +00:00
assert slashed_validator.slashed
assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH
assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH
# lost whistleblower reward
2019-05-06 15:43:02 +00:00
assert get_balance(state, validator_index) < get_balance(pre_state, validator_index)
2019-03-18 18:51:52 +00:00
2019-05-06 15:43:02 +00:00
@spec_state_test
def test_attester_slashing(state):
2019-05-06 15:43:02 +00:00
# copy for later balance lookups.
pre_state = deepcopy(state)
attester_slashing = get_valid_attester_slashing(state)
validator_index = attester_slashing.attestation_1.custody_bit_0_indices[0]
2019-05-06 15:43:02 +00:00
assert not state.validator_registry[validator_index].slashed
yield 'pre', state
#
# Add to state via block transition
#
2019-05-06 15:43:02 +00:00
block = build_empty_block_for_next_slot(state)
block.body.attester_slashings.append(attester_slashing)
2019-05-06 15:43:02 +00:00
yield 'blocks', [block], [spec.BeaconBlock]
2019-05-06 15:43:02 +00:00
state_transition(state, block)
yield 'post', state
2019-05-06 15:43:02 +00:00
slashed_validator = state.validator_registry[validator_index]
assert slashed_validator.slashed
assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH
assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH
# lost whistleblower reward
2019-05-06 15:43:02 +00:00
assert get_balance(state, validator_index) < get_balance(pre_state, validator_index)
2019-05-06 15:43:02 +00:00
proposer_index = get_beacon_proposer_index(state)
2019-04-03 00:04:12 +00:00
# gained whistleblower reward
assert (
2019-05-06 15:43:02 +00:00
get_balance(state, proposer_index) >
get_balance(pre_state, proposer_index)
2019-04-03 00:04:12 +00:00
)
2019-05-06 15:43:02 +00:00
# TODO update functions below to be like above, i.e. with @spec_state_test and yielding data to put into the test vector
@spec_state_test
2019-03-25 17:25:33 +00:00
def test_deposit_in_block(state):
initial_registry_len = len(state.validator_registry)
initial_balances_len = len(state.balances)
validator_index = len(state.validator_registry)
amount = spec.MAX_EFFECTIVE_BALANCE
deposit = prepare_state_and_deposit(state, validator_index, amount)
yield 'pre', state
2019-03-18 18:51:52 +00:00
block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
block.body.deposits.append(deposit)
yield 'blocks', [block], [spec.BeaconBlock]
2019-03-18 18:51:52 +00:00
state_transition(state, block)
yield 'post', state
2019-03-18 18:51:52 +00:00
assert len(state.validator_registry) == initial_registry_len + 1
assert len(state.balances) == initial_balances_len + 1
assert get_balance(state, validator_index) == spec.MAX_EFFECTIVE_BALANCE
assert state.validator_registry[validator_index].pubkey == pubkeys[validator_index]
2019-03-18 18:51:52 +00:00
@spec_state_test
def test_deposit_top_up(state):
2019-03-18 18:51:52 +00:00
validator_index = 0
2019-04-16 06:16:13 +00:00
amount = spec.MAX_EFFECTIVE_BALANCE // 4
deposit = prepare_state_and_deposit(state, validator_index, amount)
initial_registry_len = len(state.validator_registry)
initial_balances_len = len(state.balances)
validator_pre_balance = get_balance(state, validator_index)
2019-03-18 18:51:52 +00:00
yield 'pre', state
block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
block.body.deposits.append(deposit)
yield 'blocks', [block], [spec.BeaconBlock]
state_transition(state, block)
yield 'post', state
2019-03-18 18:51:52 +00:00
assert len(state.validator_registry) == initial_registry_len
assert len(state.balances) == initial_balances_len
assert get_balance(state, validator_index) == validator_pre_balance + amount
2019-03-18 18:51:52 +00:00
@spec_state_test
2019-03-25 17:25:33 +00:00
def test_attestation(state):
2019-04-20 05:17:33 +00:00
state.slot = spec.SLOTS_PER_EPOCH
yield 'pre', state
attestation = get_valid_attestation(state)
2019-03-18 18:51:52 +00:00
# Add to state via block transition
pre_current_attestations_len = len(state.current_epoch_attestations)
attestation_block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
attestation_block.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation_block.body.attestations.append(attestation)
state_transition(state, attestation_block)
2019-03-18 18:51:52 +00:00
assert len(state.current_epoch_attestations) == pre_current_attestations_len + 1
2019-03-18 18:51:52 +00:00
2019-03-18 18:51:52 +00:00
# Epoch transition should move to previous_epoch_attestations
pre_current_attestations_root = spec.hash_tree_root(state.current_epoch_attestations)
2019-03-18 18:51:52 +00:00
epoch_block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
epoch_block.slot += spec.SLOTS_PER_EPOCH
state_transition(state, epoch_block)
2019-03-18 18:51:52 +00:00
yield 'blocks', [attestation_block, epoch_block], [spec.BeaconBlock]
yield 'post', state
2019-03-18 18:51:52 +00:00
assert len(state.current_epoch_attestations) == 0
assert spec.hash_tree_root(state.previous_epoch_attestations) == pre_current_attestations_root
2019-03-18 18:51:52 +00:00
@spec_state_test
2019-03-25 17:25:33 +00:00
def test_voluntary_exit(state):
2019-03-19 19:25:34 +00:00
validator_index = get_active_validator_indices(
state,
get_current_epoch(state)
2019-03-19 19:25:34 +00:00
)[-1]
2019-03-18 18:51:52 +00:00
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit
state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
2019-03-18 18:51:52 +00:00
yield 'pre', state
2019-03-18 18:51:52 +00:00
voluntary_exit = VoluntaryExit(
epoch=get_current_epoch(state),
2019-03-18 18:51:52 +00:00
validator_index=validator_index,
)
voluntary_exit.signature = bls.sign(
2019-04-08 01:51:13 +00:00
message_hash=signing_root(voluntary_exit),
2019-03-18 18:51:52 +00:00
privkey=privkeys[validator_index],
domain=get_domain(
state=state,
2019-03-18 18:51:52 +00:00
domain_type=spec.DOMAIN_VOLUNTARY_EXIT,
)
)
# Add to state via block transition
initiate_exit_block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
initiate_exit_block.body.voluntary_exits.append(voluntary_exit)
state_transition(state, initiate_exit_block)
2019-03-18 18:51:52 +00:00
assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH
2019-03-18 18:51:52 +00:00
# Process within epoch transition
exit_block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
exit_block.slot += spec.SLOTS_PER_EPOCH
state_transition(state, exit_block)
2019-03-18 18:51:52 +00:00
yield 'blocks', [initiate_exit_block, exit_block], [spec.BeaconBlock]
yield 'post', state
2019-03-18 18:51:52 +00:00
assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH
2019-03-18 18:51:52 +00:00
@spec_state_test
2019-03-25 17:25:33 +00:00
def test_transfer(state):
# overwrite default 0 to test
spec.MAX_TRANSFERS = 1
sender_index = get_active_validator_indices(state, get_current_epoch(state))[-1]
amount = get_balance(state, sender_index)
transfer = get_valid_transfer(state, state.slot + 1, sender_index, amount)
recipient_index = transfer.recipient
pre_transfer_recipient_balance = get_balance(state, recipient_index)
2019-03-18 18:51:52 +00:00
# un-activate so validator can transfer
state.validator_registry[sender_index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH
yield 'pre', state
2019-03-18 18:51:52 +00:00
# Add to state via block transition
block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
block.body.transfers.append(transfer)
yield 'blocks', [block], [spec.BeaconBlock]
state_transition(state, block)
yield 'post', state
sender_balance = get_balance(state, sender_index)
recipient_balance = get_balance(state, recipient_index)
2019-03-18 18:51:52 +00:00
assert sender_balance == 0
assert recipient_balance == pre_transfer_recipient_balance + amount
@spec_state_test
def test_balance_driven_status_transitions(state):
current_epoch = get_current_epoch(state)
validator_index = get_active_validator_indices(state, current_epoch)[-1]
2019-03-18 18:51:52 +00:00
assert state.validator_registry[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH
2019-03-18 18:51:52 +00:00
# set validator balance to below ejection threshold
state.validator_registry[validator_index].effective_balance = spec.EJECTION_BALANCE
2019-03-18 18:51:52 +00:00
yield 'pre', state
2019-03-18 18:51:52 +00:00
# trigger epoch transition
block = build_empty_block_for_next_slot(state)
2019-03-18 18:51:52 +00:00
block.slot += spec.SLOTS_PER_EPOCH
state_transition(state, block)
2019-03-18 18:51:52 +00:00
yield 'blocks', [block], [spec.BeaconBlock]
yield 'post', state
2019-03-18 18:51:52 +00:00
assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH
2019-03-18 18:51:52 +00:00
@spec_state_test
2019-03-18 18:51:52 +00:00
def test_historical_batch(state):
state.slot += spec.SLOTS_PER_HISTORICAL_ROOT - (state.slot % spec.SLOTS_PER_HISTORICAL_ROOT) - 1
pre_historical_roots_len = len(state.historical_roots)
2019-03-18 18:51:52 +00:00
yield 'pre', state
2019-03-18 18:51:52 +00:00
block = build_empty_block_for_next_slot(state)
state_transition(state, block)
2019-03-18 18:51:52 +00:00
yield 'blocks', [block], [spec.BeaconBlock]
yield 'post', state
2019-03-18 18:51:52 +00:00
assert state.slot == block.slot
assert get_current_epoch(state) % (spec.SLOTS_PER_HISTORICAL_ROOT // spec.SLOTS_PER_EPOCH) == 0
assert len(state.historical_roots) == pre_historical_roots_len + 1
@spec_state_test
def test_eth1_data_votes(state):
yield 'pre', state
expected_votes = 0
assert len(state.eth1_data_votes) == expected_votes
blocks = []
for _ in range(spec.SLOTS_PER_ETH1_VOTING_PERIOD - 1):
block = build_empty_block_for_next_slot(state)
state_transition(state, block)
expected_votes += 1
assert len(state.eth1_data_votes) == expected_votes
blocks.append(block)
block = build_empty_block_for_next_slot(state)
2019-04-24 19:20:55 +00:00
blocks.append(block)
state_transition(state, block)
yield 'blocks', [block], [spec.BeaconBlock]
yield 'post', state
assert state.slot % spec.SLOTS_PER_ETH1_VOTING_PERIOD == 0
assert len(state.eth1_data_votes) == 1