2019-07-26 21:50:11 +00:00
|
|
|
from eth2spec.test.context import spec_test, with_phases
|
2019-06-29 22:38:30 +00:00
|
|
|
from eth2spec.test.helpers.deposits import (
|
|
|
|
prepare_genesis_deposits,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def create_valid_beacon_state(spec):
|
|
|
|
deposit_count = spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
|
2019-06-30 00:10:18 +00:00
|
|
|
deposits, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True)
|
2019-06-29 22:38:30 +00:00
|
|
|
|
|
|
|
eth1_block_hash = b'\x12' * 32
|
|
|
|
eth1_timestamp = spec.MIN_GENESIS_TIME
|
|
|
|
return spec.initialize_beacon_state_from_eth1(eth1_block_hash, eth1_timestamp, deposits)
|
|
|
|
|
|
|
|
|
|
|
|
def run_is_valid_genesis_state(spec, state, valid=True):
|
|
|
|
"""
|
|
|
|
Run ``is_valid_genesis_state``, yielding:
|
2019-06-30 12:58:53 +00:00
|
|
|
- genesis ('state')
|
2019-06-29 22:38:30 +00:00
|
|
|
- is_valid ('is_valid')
|
|
|
|
"""
|
2019-06-30 12:58:53 +00:00
|
|
|
yield 'genesis', state
|
2019-06-29 22:38:30 +00:00
|
|
|
is_valid = spec.is_valid_genesis_state(state)
|
|
|
|
yield 'is_valid', is_valid
|
2019-06-30 12:58:53 +00:00
|
|
|
assert is_valid == valid
|
2019-06-29 22:38:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@with_phases(['phase0'])
|
2019-07-26 21:50:11 +00:00
|
|
|
@spec_test
|
2019-06-29 22:38:30 +00:00
|
|
|
def test_is_valid_genesis_state_true(spec):
|
|
|
|
state = create_valid_beacon_state(spec)
|
|
|
|
|
|
|
|
yield from run_is_valid_genesis_state(spec, state, valid=True)
|
|
|
|
|
|
|
|
|
|
|
|
@with_phases(['phase0'])
|
2019-07-26 21:50:11 +00:00
|
|
|
@spec_test
|
2019-06-29 22:38:30 +00:00
|
|
|
def test_is_valid_genesis_state_false_invalid_timestamp(spec):
|
|
|
|
state = create_valid_beacon_state(spec)
|
|
|
|
state.genesis_time = spec.MIN_GENESIS_TIME - 1
|
|
|
|
|
2019-06-30 13:01:30 +00:00
|
|
|
yield from run_is_valid_genesis_state(spec, state, valid=False)
|
2019-06-29 22:38:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@with_phases(['phase0'])
|
2019-07-26 21:50:11 +00:00
|
|
|
@spec_test
|
2019-06-29 22:38:30 +00:00
|
|
|
def test_is_valid_genesis_state_true_more_balance(spec):
|
|
|
|
state = create_valid_beacon_state(spec)
|
|
|
|
state.validators[0].effective_balance = spec.MAX_EFFECTIVE_BALANCE + 1
|
|
|
|
|
|
|
|
yield from run_is_valid_genesis_state(spec, state, valid=True)
|
|
|
|
|
|
|
|
|
2019-06-30 13:01:30 +00:00
|
|
|
# TODO: not part of the genesis function yet. Erroneously merged.
|
|
|
|
# @with_phases(['phase0'])
|
2019-07-26 21:50:11 +00:00
|
|
|
# @spec_test
|
2019-06-30 13:01:30 +00:00
|
|
|
# def test_is_valid_genesis_state_false_not_enough_balance(spec):
|
|
|
|
# state = create_valid_beacon_state(spec)
|
|
|
|
# state.validators[0].effective_balance = spec.MAX_EFFECTIVE_BALANCE - 1
|
|
|
|
#
|
|
|
|
# yield from run_is_valid_genesis_state(spec, state, valid=False)
|
2019-06-29 22:38:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@with_phases(['phase0'])
|
2019-07-26 21:50:11 +00:00
|
|
|
@spec_test
|
2019-06-29 22:38:30 +00:00
|
|
|
def test_is_valid_genesis_state_true_one_more_validator(spec):
|
|
|
|
deposit_count = spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT + 1
|
2019-06-30 00:10:18 +00:00
|
|
|
deposits, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True)
|
2019-06-29 22:38:30 +00:00
|
|
|
|
|
|
|
eth1_block_hash = b'\x12' * 32
|
|
|
|
eth1_timestamp = spec.MIN_GENESIS_TIME
|
|
|
|
state = spec.initialize_beacon_state_from_eth1(eth1_block_hash, eth1_timestamp, deposits)
|
|
|
|
|
|
|
|
yield from run_is_valid_genesis_state(spec, state, valid=True)
|
|
|
|
|
|
|
|
|
|
|
|
@with_phases(['phase0'])
|
2019-07-26 21:50:11 +00:00
|
|
|
@spec_test
|
2019-06-30 04:25:51 +00:00
|
|
|
def test_is_valid_genesis_state_false_not_enough_validator(spec):
|
2019-06-29 22:38:30 +00:00
|
|
|
deposit_count = spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT - 1
|
2019-06-30 00:10:18 +00:00
|
|
|
deposits, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True)
|
2019-06-29 22:38:30 +00:00
|
|
|
|
|
|
|
eth1_block_hash = b'\x12' * 32
|
|
|
|
eth1_timestamp = spec.MIN_GENESIS_TIME
|
|
|
|
state = spec.initialize_beacon_state_from_eth1(eth1_block_hash, eth1_timestamp, deposits)
|
|
|
|
|
|
|
|
yield from run_is_valid_genesis_state(spec, state, valid=False)
|