def test_get_state_root_at_slot( sample_beacon_state_params, current_slot, target_slot, success, slots_per_epoch, slots_per_historical_root, ): blocks, _, state_roots = generate_mock_latest_historical_roots( SignedBeaconBlock.create(), current_slot, slots_per_epoch, slots_per_historical_root, ) state = BeaconState.create(**sample_beacon_state_params).mset( "slot", current_slot, "state_roots", state_roots ) blocks[current_slot].message.state_root = state.hash_tree_root if success: state_root = get_state_root_at_slot( state, target_slot, slots_per_historical_root ) assert state_root == blocks[target_slot].message.state_root else: with pytest.raises(ValidationError): get_state_root_at_slot(state, target_slot, slots_per_historical_root)
def test_process_eth1_data( original_votes, block_data, expected_votes, sample_beacon_state_params, sample_beacon_block_params, sample_beacon_block_body_params, config, ): eth1_data_votes = tuple(mapcat(_expand_eth1_votes, original_votes)) state = BeaconState.create(**sample_beacon_state_params).set( "eth1_data_votes", eth1_data_votes) block_body = BeaconBlockBody.create( **sample_beacon_block_body_params).mset( "eth1_data", Eth1Data.create(block_hash=block_data)) block = BeaconBlock.create(**sample_beacon_block_params).set( "body", block_body) updated_state = process_eth1_data(state, block, config) updated_votes = updated_state.eth1_data_votes expanded_expected_votes = tuple(mapcat(_expand_eth1_votes, expected_votes)) assert tuple(updated_votes) == expanded_expected_votes
def genesis_state(eth2_config, genesis_time, genesis_validators): balances = tuple(validator.effective_balance for validator in genesis_validators) state = BeaconState.create( latest_block_header=BeaconBlockHeader.create( body_root=BeaconBlockBody.create().hash_tree_root ), validators=genesis_validators, balances=balances, config=eth2_config, ) state.genesis_time = genesis_time return state
def initialize_beacon_state_from_eth1(*, eth1_block_hash: Hash32, eth1_timestamp: Timestamp, deposits: Sequence[Deposit], config: Eth2Config) -> BeaconState: fork = Fork.create( previous_version=config.GENESIS_FORK_VERSION, current_version=config.GENESIS_FORK_VERSION, epoch=GENESIS_EPOCH, ) state = BeaconState.create( genesis_time=_genesis_time_from_eth1_timestamp(eth1_timestamp, config.GENESIS_DELAY), fork=fork, eth1_data=Eth1Data.create(block_hash=eth1_block_hash, deposit_count=len(deposits)), latest_block_header=BeaconBlockHeader.create( body_root=BeaconBlockBody.create().hash_tree_root), block_roots=(ZERO_ROOT, ) * config.SLOTS_PER_HISTORICAL_ROOT, state_roots=(ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT, randao_mixes=(eth1_block_hash, ) * config.EPOCHS_PER_HISTORICAL_VECTOR, slashings=(Gwei(0), ) * config.EPOCHS_PER_SLASHINGS_VECTOR, config=config, ) # Process genesis deposits for index, deposit in enumerate(deposits): deposit_data_list = tuple(deposit.data for deposit in deposits[:index + 1]) deposit_root = ssz.get_hash_tree_root( deposit_data_list, ssz.List(DepositData, 2**DEPOSIT_CONTRACT_TREE_DEPTH)) state = state.transform(("eth1_data", "deposit_root"), deposit_root) state = process_deposit(state=state, deposit=deposit, config=config) # Process genesis activations for validator_index in range(len(state.validators)): validator_index = ValidatorIndex(validator_index) balance = state.balances[validator_index] effective_balance = calculate_effective_balance(balance, config) state = state.transform( ("validators", validator_index, "effective_balance"), effective_balance) if effective_balance == config.MAX_EFFECTIVE_BALANCE: activated_validator = activate_validator( state.validators[validator_index], GENESIS_EPOCH) state = state.transform(("validators", validator_index), activated_validator) return state.set("genesis_validators_root", state.validators.hash_tree_root)
def is_genesis_trigger(deposits: Sequence[Deposit], timestamp: int, config: Eth2Config) -> bool: state = BeaconState.create(config=config) for deposit in deposits: state = process_deposit(state, deposit, config) active_validator_count = 0 for validator in state.validators: if validator.effective_balance == config.MAX_EFFECTIVE_BALANCE: active_validator_count += 1 return active_validator_count == config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
def genesis_config_with_default_state( config_profile: Literal["minimal", "mainnet"], genesis_time: Timestamp = None) -> Dict[str, Any]: eth2_config = _get_eth2_config(config_profile) override_lengths(eth2_config) genesis_state = BeaconState.create(config=eth2_config) if genesis_time: genesis_state = genesis_state.set("genesis_time", genesis_time) return _create_genesis_config(config_profile, eth2_config, genesis_state, ())
def test_validate_block_slot( sample_beacon_state_params, sample_beacon_block_params, state_slot, block_slot, expected, ): state = BeaconState.create(**sample_beacon_state_params).set( "slot", state_slot) block = BeaconBlock.create(**sample_beacon_block_params).set( "slot", block_slot) if isinstance(expected, Exception): with pytest.raises(ValidationError): validate_block_slot(state, block) else: validate_block_slot(state, block)
def test_validate_proposer_signature( slots_per_epoch, max_committees_per_slot, proposer_privkey, proposer_pubkey, is_valid_signature, sample_beacon_block_params, sample_beacon_state_params, target_committee_size, max_effective_balance, config, ): state = BeaconState.create(**sample_beacon_state_params).mset( "validators", tuple( create_mock_validator(proposer_pubkey, config) for _ in range(10)), "balances", (max_effective_balance, ) * 10, ) block = BeaconBlock.create(**sample_beacon_block_params) header = block.header proposed_block = block.set( "signature", bls.sign( message_hash=header.signing_root, privkey=proposer_privkey, domain=get_domain(state, SignatureDomain.DOMAIN_BEACON_PROPOSER, slots_per_epoch), ), ) if is_valid_signature: validate_proposer_signature(state, proposed_block, CommitteeConfig(config)) else: with pytest.raises(ValidationError): validate_proposer_signature(state, proposed_block, CommitteeConfig(config))
def test_get_block_root_at_slot( sample_beacon_state_params, current_slot, target_slot, success, slots_per_epoch, slots_per_historical_root, sample_block, ): blocks, block_roots = generate_mock_latest_historical_roots( sample_block, current_slot, slots_per_epoch, slots_per_historical_root) state = BeaconState.create(**sample_beacon_state_params).mset( "slot", current_slot, "block_roots", block_roots) if success: block_root = get_block_root_at_slot(state, target_slot, slots_per_historical_root) assert block_root == blocks[target_slot].signing_root else: with pytest.raises(ValidationError): get_block_root_at_slot(state, target_slot, slots_per_historical_root)
def state(sample_beacon_state_params): return BeaconState.create(**sample_beacon_state_params)
def genesis_state(eth2_config, genesis_time): state = BeaconState.create(config=eth2_config) state.genesis_time = genesis_time return state
def test_defaults(sample_beacon_state_params): state = BeaconState.create(**sample_beacon_state_params) assert tuple(state.validators) == sample_beacon_state_params["validators"] assert ssz.encode(state)