def get_epoch_boundary_attesting_balances( current_epoch: Epoch, previous_epoch: Epoch, state: 'BeaconState', config: 'BeaconConfig') -> Tuple[Gwei, Gwei]: current_epoch_attestations = get_current_epoch_attestations( state, config.SLOTS_PER_EPOCH) previous_epoch_attestations = get_previous_epoch_attestations( state, config.SLOTS_PER_EPOCH, config.GENESIS_EPOCH, ) previous_epoch_boundary_root = get_block_root( state, get_epoch_start_slot(previous_epoch, config.SLOTS_PER_EPOCH), config.LATEST_BLOCK_ROOTS_LENGTH, ) previous_epoch_boundary_attester_indices = get_epoch_boundary_attester_indices( state, current_epoch_attestations + previous_epoch_attestations, state.previous_justified_epoch, previous_epoch_boundary_root, CommitteeConfig(config), ) previous_epoch_boundary_attesting_balance = get_total_balance( state.validator_balances, previous_epoch_boundary_attester_indices, config.MAX_DEPOSIT_AMOUNT, ) current_epoch_boundary_root = get_block_root( state, get_epoch_start_slot(current_epoch, config.SLOTS_PER_EPOCH), config.LATEST_BLOCK_ROOTS_LENGTH, ) current_epoch_boundary_attester_indices = get_epoch_boundary_attester_indices( state, current_epoch_attestations, state.justified_epoch, current_epoch_boundary_root, CommitteeConfig(config), ) current_epoch_boundary_attesting_balance = get_total_balance( state.validator_balances, current_epoch_boundary_attester_indices, config.MAX_DEPOSIT_AMOUNT, ) return previous_epoch_boundary_attesting_balance, current_epoch_boundary_attesting_balance
def get_winning_root(*, state: 'BeaconState', shard: Shard, attestations: Sequence[PendingAttestationRecord], max_deposit_amount: Gwei, committee_config: CommitteeConfig) -> Tuple[Hash32, Gwei]: winning_root = None winning_root_balance: Gwei = Gwei(0) crosslink_data_roots = set([ a.data.crosslink_data_root for a in attestations if a.data.shard == shard ]) for crosslink_data_root in crosslink_data_roots: attesting_validator_indices = get_attester_indices_from_attesttion( state=state, attestations=[ a for a in attestations if a.data.shard == shard and a.data.crosslink_data_root == crosslink_data_root ], committee_config=committee_config, ) total_attesting_balance = get_total_balance( state.validator_balances, attesting_validator_indices, max_deposit_amount, ) if total_attesting_balance > winning_root_balance: winning_root = crosslink_data_root winning_root_balance = total_attesting_balance elif total_attesting_balance == winning_root_balance and winning_root_balance > 0: if crosslink_data_root < winning_root: winning_root = crosslink_data_root if winning_root is None: raise NoWinningRootError return (winning_root, winning_root_balance)
def test_get_total_balance(genesis_state, balances, validator_indices, expected): state = genesis_state for i, index in enumerate(validator_indices): state = state.transform(["balances", index], balances[i]) total_balance = get_total_balance(state, validator_indices) assert total_balance == expected
def test_get_total_balance(genesis_state, balances, validator_indices, expected): state = genesis_state for i, index in enumerate(validator_indices): state = state._update_validator_balance(index, balances[i]) total_balance = get_total_balance(state, validator_indices) assert total_balance == expected
def get_attesting_balance(state: BeaconState, attestations: Sequence[PendingAttestation], config: Eth2Config) -> Gwei: return get_total_balance( state, get_unslashed_attesting_indices(state, attestations, CommitteeConfig(config)))
def get_epoch_boundary_attesting_balance( state: 'BeaconState', attestations: Sequence[PendingAttestation], epoch: Epoch, config: Eth2Config) -> Gwei: attesting_indices = _get_epoch_boundary_attesting_indices( state, attestations, epoch, config) return get_total_balance( state.validator_balances, attesting_indices, config.MAX_DEPOSIT_AMOUNT, )
def get_total_active_balance(state: BeaconState, config: Eth2Config) -> Gwei: current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH) active_validator_indices = get_active_validator_indices( state.validators, current_epoch) return get_total_balance(state, set(active_validator_indices))
def test_get_total_balance(validator_balances, validator_indices, max_deposit_amount, expected): total_balance = get_total_balance(validator_balances, validator_indices, max_deposit_amount) assert total_balance == expected