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

48 lines
1.9 KiB
Python
Raw Normal View History

import copy
2019-05-15 16:36:32 +00:00
from eth2spec.test.helpers.keys import pubkeys
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
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)
)
def create_genesis_state(spec, validator_balances, activation_threshold):
2019-05-15 16:36:32 +00:00
deposit_root = b'\x42' * 32
eth1_block_hash = b'\xda' * 32
2019-05-15 16:36:32 +00:00
state = spec.BeaconState(
genesis_time=0,
eth1_deposit_index=len(validator_balances),
2019-06-09 19:41:21 +00:00
eth1_data=spec.Eth1Data(
2019-05-15 16:36:32 +00:00
deposit_root=deposit_root,
deposit_count=len(validator_balances),
block_hash=eth1_block_hash,
),
latest_block_header=spec.BeaconBlockHeader(body_root=spec.hash_tree_root(spec.BeaconBlockBody())),
randao_mixes=[eth1_block_hash] * spec.EPOCHS_PER_HISTORICAL_VECTOR,
)
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.
state.balances = copy.deepcopy(validator_balances)
state.validators = [build_mock_validator(spec, i, state.balances[i]) for i in range(len(validator_balances))]
2019-05-15 16:36:32 +00:00
# Process genesis activations
2019-06-09 19:41:21 +00:00
for validator in state.validators:
if validator.effective_balance >= activation_threshold:
2019-05-15 16:36:32 +00:00
validator.activation_eligibility_epoch = spec.GENESIS_EPOCH
validator.activation_epoch = spec.GENESIS_EPOCH
return state