2019-05-15 16:36:32 +00:00
|
|
|
from eth2spec.test.helpers.keys import pubkeys
|
2019-05-31 23:51:09 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
|
2019-06-22 17:48:06 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import List
|
2019-05-15 16:36:32 +00:00
|
|
|
|
|
|
|
|
2019-05-30 20:57:18 +00:00
|
|
|
def build_mock_validator(spec, i: int, balance: int):
|
2019-05-15 16:36:32 +00:00
|
|
|
pubkey = pubkeys[i]
|
|
|
|
# insecurely use pubkey as withdrawal key as well
|
2019-06-30 22:36:24 +00:00
|
|
|
withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:]
|
2019-05-15 16:36:32 +00:00
|
|
|
return spec.Validator(
|
|
|
|
pubkey=pubkeys[i],
|
|
|
|
withdrawal_credentials=withdrawal_credentials,
|
|
|
|
activation_eligibility_epoch=spec.FAR_FUTURE_EPOCH,
|
|
|
|
activation_epoch=spec.FAR_FUTURE_EPOCH,
|
|
|
|
exit_epoch=spec.FAR_FUTURE_EPOCH,
|
|
|
|
withdrawable_epoch=spec.FAR_FUTURE_EPOCH,
|
|
|
|
effective_balance=min(balance - balance % spec.EFFECTIVE_BALANCE_INCREMENT, spec.MAX_EFFECTIVE_BALANCE)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-09-26 19:55:58 +00:00
|
|
|
def create_genesis_state(spec, num_validators, validator_balance):
|
2019-05-15 16:36:32 +00:00
|
|
|
deposit_root = b'\x42' * 32
|
|
|
|
|
|
|
|
state = spec.BeaconState(
|
|
|
|
genesis_time=0,
|
2019-06-09 19:41:21 +00:00
|
|
|
eth1_deposit_index=num_validators,
|
|
|
|
eth1_data=spec.Eth1Data(
|
2019-05-15 16:36:32 +00:00
|
|
|
deposit_root=deposit_root,
|
|
|
|
deposit_count=num_validators,
|
2019-06-30 15:10:22 +00:00
|
|
|
block_hash=spec.Hash(),
|
2019-06-25 16:31:39 +00:00
|
|
|
),
|
|
|
|
latest_block_header=spec.BeaconBlockHeader(body_root=spec.hash_tree_root(spec.BeaconBlockBody())),
|
|
|
|
)
|
2019-05-15 16:36:32 +00:00
|
|
|
|
|
|
|
# We "hack" in the initial validators,
|
|
|
|
# as it is much faster than creating and processing genesis deposits for every single test case.
|
2019-09-26 19:55:58 +00:00
|
|
|
state.balances = [validator_balance] * num_validators
|
2019-06-09 19:41:21 +00:00
|
|
|
state.validators = [build_mock_validator(spec, i, state.balances[i]) for i in range(num_validators)]
|
2019-05-15 16:36:32 +00:00
|
|
|
|
|
|
|
# Process genesis activations
|
2019-06-09 19:41:21 +00:00
|
|
|
for validator in state.validators:
|
2019-09-26 19:55:58 +00:00
|
|
|
if validator.effective_balance >= validator_balance:
|
2019-05-15 16:36:32 +00:00
|
|
|
validator.activation_eligibility_epoch = spec.GENESIS_EPOCH
|
|
|
|
validator.activation_epoch = spec.GENESIS_EPOCH
|
|
|
|
|
2019-06-30 20:19:52 +00:00
|
|
|
genesis_active_index_root = hash_tree_root(List[spec.ValidatorIndex, spec.VALIDATOR_REGISTRY_LIMIT](
|
|
|
|
spec.get_active_validator_indices(state, spec.GENESIS_EPOCH)))
|
2019-06-27 07:44:44 +00:00
|
|
|
genesis_compact_committees_root = hash_tree_root(List[spec.ValidatorIndex, spec.VALIDATOR_REGISTRY_LIMIT](
|
2019-06-22 16:34:33 +00:00
|
|
|
spec.get_active_validator_indices(state, spec.GENESIS_EPOCH)))
|
2019-06-19 19:59:44 +00:00
|
|
|
for index in range(spec.EPOCHS_PER_HISTORICAL_VECTOR):
|
2019-06-30 20:19:52 +00:00
|
|
|
state.active_index_roots[index] = genesis_active_index_root
|
2019-06-27 07:44:44 +00:00
|
|
|
state.compact_committees_roots[index] = genesis_compact_committees_root
|
2019-05-15 16:36:32 +00:00
|
|
|
|
|
|
|
return state
|