type annotation clean up

This commit is contained in:
protolambda 2019-06-22 18:12:42 +02:00
parent 16093eb8ce
commit 00aae07d46
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
3 changed files with 46 additions and 48 deletions

View File

@ -12,8 +12,7 @@ from typing import (
PHASE0_IMPORTS = '''from typing import ( PHASE0_IMPORTS = '''from typing import (
Any, Callable, Iterable, Dict, Set, Tuple, NewType, Any, Callable, Iterable, Dict, Set, Sequence, Tuple,
List as TypingList,
) )
from dataclasses import ( from dataclasses import (
@ -38,7 +37,7 @@ from eth2spec.utils.bls import (
from eth2spec.utils.hash_function import hash from eth2spec.utils.hash_function import hash
''' '''
PHASE1_IMPORTS = '''from typing import ( PHASE1_IMPORTS = '''from typing import (
Any, Callable, Dict, Optional, Set, Tuple, Iterable, NewType, Any, Callable, Dict, Iterable, Optional, Set, Sequence, Tuple,
List as TypingList List as TypingList
) )
@ -79,13 +78,13 @@ def hash(x: bytes) -> Hash:
# Monkey patch validator compute committee code # Monkey patch validator compute committee code
_compute_committee = compute_committee _compute_committee = compute_committee
committee_cache: Dict[Tuple[Hash, Hash, int, int], Tuple[ValidatorIndex, ...]] = {} committee_cache: Dict[Tuple[Hash, Hash, int, int], Sequence[ValidatorIndex]] = {}
def compute_committee(indices: Tuple[ValidatorIndex, ...], # type: ignore def compute_committee(indices: Sequence[ValidatorIndex], # type: ignore
seed: Hash, seed: Hash,
index: int, index: int,
count: int) -> Tuple[ValidatorIndex, ...]: count: int) -> Sequence[ValidatorIndex]:
param_hash = (hash(b''.join(index.to_bytes(length=4, byteorder='little') for index in indices)), seed, index, count) param_hash = (hash(b''.join(index.to_bytes(length=4, byteorder='little') for index in indices)), seed, index, count)
if param_hash not in committee_cache: if param_hash not in committee_cache:
@ -154,7 +153,6 @@ def objects_to_spec(functions: Dict[str, str],
spec = ( spec = (
imports imports
+ '\n\n' + new_type_definitions + '\n\n' + new_type_definitions
+ '\n\n' + "Deltas = NewType('Deltas', TypingList[Gwei])"
+ '\n\n' + constants_spec + '\n\n' + constants_spec
+ '\n\n\n' + ssz_objects_instantiation_spec + '\n\n\n' + ssz_objects_instantiation_spec
+ '\n\n' + functions_spec + '\n\n' + functions_spec

View File

@ -651,13 +651,11 @@ def is_slashable_validator(validator: Validator, epoch: Epoch) -> bool:
### `get_active_validator_indices` ### `get_active_validator_indices`
```python ```python
def get_active_validator_indices(state: BeaconState, epoch: Epoch) -> List[ValidatorIndex, VALIDATOR_REGISTRY_SIZE]: def get_active_validator_indices(state: BeaconState, epoch: Epoch) -> Sequence[ValidatorIndex]:
""" """
Get active validator indices at ``epoch``. Get active validator indices at ``epoch``.
""" """
return List[ValidatorIndex, VALIDATOR_REGISTRY_SIZE]( return [ValidatorIndex(i) for i, v in enumerate(state.validators) if is_active_validator(v, epoch)]
i for i, v in enumerate(state.validators) if is_active_validator(v, epoch)
)
``` ```
### `increase_balance` ### `increase_balance`
@ -819,7 +817,7 @@ def get_beacon_proposer_index(state: BeaconState) -> ValidatorIndex:
### `verify_merkle_branch` ### `verify_merkle_branch`
```python ```python
def verify_merkle_branch(leaf: Hash, proof: Tuple[Hash, ...], depth: int, index: int, root: Hash) -> bool: def verify_merkle_branch(leaf: Hash, proof: Sequence[Hash], depth: int, index: int, root: Hash) -> bool:
""" """
Verify that the given ``leaf`` is on the merkle branch ``proof`` Verify that the given ``leaf`` is on the merkle branch ``proof``
starting with the given ``root``. starting with the given ``root``.
@ -863,19 +861,19 @@ def get_shuffled_index(index: ValidatorIndex, index_count: int, seed: Hash) -> V
### `compute_committee` ### `compute_committee`
```python ```python
def compute_committee(indices: Tuple[ValidatorIndex, ...], def compute_committee(indices: Sequence[ValidatorIndex],
seed: Hash, index: int, count: int) -> Tuple[ValidatorIndex, ...]: seed: Hash, index: int, count: int) -> Sequence[ValidatorIndex]:
start = (len(indices) * index) // count start = (len(indices) * index) // count
end = (len(indices) * (index + 1)) // count end = (len(indices) * (index + 1)) // count
return tuple(indices[get_shuffled_index(ValidatorIndex(i), len(indices), seed)] for i in range(start, end)) return [indices[get_shuffled_index(ValidatorIndex(i), len(indices), seed)] for i in range(start, end)]
``` ```
### `get_crosslink_committee` ### `get_crosslink_committee`
```python ```python
def get_crosslink_committee(state: BeaconState, epoch: Epoch, shard: Shard) -> Tuple[ValidatorIndex, ...]: def get_crosslink_committee(state: BeaconState, epoch: Epoch, shard: Shard) -> Sequence[ValidatorIndex]:
return compute_committee( return compute_committee(
indices=tuple(get_active_validator_indices(state, epoch)), indices=get_active_validator_indices(state, epoch),
seed=generate_seed(state, epoch), seed=generate_seed(state, epoch),
index=(shard + SHARD_COUNT - get_epoch_start_shard(state, epoch)) % SHARD_COUNT, index=(shard + SHARD_COUNT - get_epoch_start_shard(state, epoch)) % SHARD_COUNT,
count=get_epoch_committee_count(state, epoch), count=get_epoch_committee_count(state, epoch),
@ -887,13 +885,13 @@ def get_crosslink_committee(state: BeaconState, epoch: Epoch, shard: Shard) -> T
```python ```python
def get_attesting_indices(state: BeaconState, def get_attesting_indices(state: BeaconState,
attestation_data: AttestationData, attestation_data: AttestationData,
bitfield: bytes) -> Tuple[ValidatorIndex, ...]: bitfield: bytes) -> Sequence[ValidatorIndex]:
""" """
Return the sorted attesting indices corresponding to ``attestation_data`` and ``bitfield``. Return the sorted attesting indices corresponding to ``attestation_data`` and ``bitfield``.
""" """
committee = get_crosslink_committee(state, attestation_data.target_epoch, attestation_data.crosslink.shard) committee = get_crosslink_committee(state, attestation_data.target_epoch, attestation_data.crosslink.shard)
assert verify_bitfield(bitfield, len(committee)) assert verify_bitfield(bitfield, len(committee))
return tuple(sorted([index for i, index in enumerate(committee) if get_bitfield_bit(bitfield, i) == 0b1])) return sorted([index for i, index in enumerate(committee) if get_bitfield_bit(bitfield, i) == 0b1])
``` ```
### `int_to_bytes` ### `int_to_bytes`
@ -1139,7 +1137,7 @@ def slash_validator(state: BeaconState,
### Genesis trigger ### Genesis trigger
Before genesis has been triggered and whenever the deposit contract emits a `Deposit` log, call the function `is_genesis_trigger(deposits: Iterable[Deposit], timestamp: uint64) -> bool` where: Before genesis has been triggered and whenever the deposit contract emits a `Deposit` log, call the function `is_genesis_trigger(deposits: Sequence[Deposit], timestamp: uint64) -> bool` where:
* `deposits` is the list of all deposits, ordered chronologically, up to and including the deposit triggering the latest `Deposit` log * `deposits` is the list of all deposits, ordered chronologically, up to and including the deposit triggering the latest `Deposit` log
* `timestamp` is the Unix timestamp in the Ethereum 1.0 block that emitted the latest `Deposit` log * `timestamp` is the Unix timestamp in the Ethereum 1.0 block that emitted the latest `Deposit` log
@ -1156,7 +1154,7 @@ When `is_genesis_trigger(deposits, timestamp) is True` for the first time let:
*Note*: The function `is_genesis_trigger` has yet to be agreed by the community, and can be updated as necessary. We define the following testing placeholder: *Note*: The function `is_genesis_trigger` has yet to be agreed by the community, and can be updated as necessary. We define the following testing placeholder:
```python ```python
def is_genesis_trigger(deposits: Iterable[Deposit], timestamp: uint64) -> bool: def is_genesis_trigger(deposits: Sequence[Deposit], timestamp: uint64) -> bool:
# Process deposits # Process deposits
state = BeaconState() state = BeaconState()
for deposit in deposits: for deposit in deposits:
@ -1178,7 +1176,7 @@ def is_genesis_trigger(deposits: Iterable[Deposit], timestamp: uint64) -> bool:
Let `genesis_state = get_genesis_beacon_state(genesis_deposits, genesis_time, genesis_eth1_data)`. Let `genesis_state = get_genesis_beacon_state(genesis_deposits, genesis_time, genesis_eth1_data)`.
```python ```python
def get_genesis_beacon_state(deposits: Iterable[Deposit], genesis_time: int, eth1_data: Eth1Data) -> BeaconState: def get_genesis_beacon_state(deposits: Sequence[Deposit], genesis_time: int, eth1_data: Eth1Data) -> BeaconState:
state = BeaconState( state = BeaconState(
genesis_time=genesis_time, genesis_time=genesis_time,
eth1_data=eth1_data, eth1_data=eth1_data,
@ -1196,7 +1194,9 @@ def get_genesis_beacon_state(deposits: Iterable[Deposit], genesis_time: int, eth
validator.activation_epoch = GENESIS_EPOCH validator.activation_epoch = GENESIS_EPOCH
# Populate active_index_roots # Populate active_index_roots
genesis_active_index_root = hash_tree_root(get_active_validator_indices(state, GENESIS_EPOCH)) genesis_active_index_root = hash_tree_root(
List[ValidatorIndex, VALIDATOR_REGISTRY_SIZE](get_active_validator_indices(state, GENESIS_EPOCH))
)
for index in range(EPOCHS_PER_HISTORICAL_VECTOR): for index in range(EPOCHS_PER_HISTORICAL_VECTOR):
state.active_index_roots[index] = genesis_active_index_root state.active_index_roots[index] = genesis_active_index_root
@ -1271,17 +1271,17 @@ def process_epoch(state: BeaconState) -> None:
```python ```python
def get_total_active_balance(state: BeaconState) -> Gwei: def get_total_active_balance(state: BeaconState) -> Gwei:
return get_total_balance(state, get_active_validator_indices(state, get_current_epoch(state))) return get_total_balance(state, set(get_active_validator_indices(state, get_current_epoch(state))))
``` ```
```python ```python
def get_matching_source_attestations(state: BeaconState, epoch: Epoch) -> Iterable[PendingAttestation]: def get_matching_source_attestations(state: BeaconState, epoch: Epoch) -> Sequence[PendingAttestation]:
assert epoch in (get_current_epoch(state), get_previous_epoch(state)) assert epoch in (get_current_epoch(state), get_previous_epoch(state))
return state.current_epoch_attestations if epoch == get_current_epoch(state) else state.previous_epoch_attestations return state.current_epoch_attestations if epoch == get_current_epoch(state) else state.previous_epoch_attestations
``` ```
```python ```python
def get_matching_target_attestations(state: BeaconState, epoch: Epoch) -> Iterable[PendingAttestation]: def get_matching_target_attestations(state: BeaconState, epoch: Epoch) -> Sequence[PendingAttestation]:
return [ return [
a for a in get_matching_source_attestations(state, epoch) a for a in get_matching_source_attestations(state, epoch)
if a.data.target_root == get_block_root(state, epoch) if a.data.target_root == get_block_root(state, epoch)
@ -1289,7 +1289,7 @@ def get_matching_target_attestations(state: BeaconState, epoch: Epoch) -> Iterab
``` ```
```python ```python
def get_matching_head_attestations(state: BeaconState, epoch: Epoch) -> Iterable[PendingAttestation]: def get_matching_head_attestations(state: BeaconState, epoch: Epoch) -> Sequence[PendingAttestation]:
return [ return [
a for a in get_matching_source_attestations(state, epoch) a for a in get_matching_source_attestations(state, epoch)
if a.data.beacon_block_root == get_block_root_at_slot(state, get_attestation_data_slot(state, a.data)) if a.data.beacon_block_root == get_block_root_at_slot(state, get_attestation_data_slot(state, a.data))
@ -1298,22 +1298,22 @@ def get_matching_head_attestations(state: BeaconState, epoch: Epoch) -> Iterable
```python ```python
def get_unslashed_attesting_indices(state: BeaconState, def get_unslashed_attesting_indices(state: BeaconState,
attestations: Iterable[PendingAttestation]) -> Iterable[ValidatorIndex]: attestations: Sequence[PendingAttestation]) -> Set[ValidatorIndex]:
output = set() # type: Set[ValidatorIndex] output = set() # type: Set[ValidatorIndex]
for a in attestations: for a in attestations:
output = output.union(get_attesting_indices(state, a.data, a.aggregation_bitfield)) output = output.union(get_attesting_indices(state, a.data, a.aggregation_bitfield))
return sorted(filter(lambda index: not state.validators[index].slashed, list(output))) return set(filter(lambda index: not state.validators[index].slashed, list(output)))
``` ```
```python ```python
def get_attesting_balance(state: BeaconState, attestations: Iterable[PendingAttestation]) -> Gwei: def get_attesting_balance(state: BeaconState, attestations: Sequence[PendingAttestation]) -> Gwei:
return get_total_balance(state, get_unslashed_attesting_indices(state, attestations)) return get_total_balance(state, get_unslashed_attesting_indices(state, attestations))
``` ```
```python ```python
def get_winning_crosslink_and_attesting_indices(state: BeaconState, def get_winning_crosslink_and_attesting_indices(state: BeaconState,
epoch: Epoch, epoch: Epoch,
shard: Shard) -> Tuple[Crosslink, Iterable[ValidatorIndex]]: shard: Shard) -> Tuple[Crosslink, Set[ValidatorIndex]]:
attestations = [a for a in get_matching_source_attestations(state, epoch) if a.data.crosslink.shard == shard] attestations = [a for a in get_matching_source_attestations(state, epoch) if a.data.crosslink.shard == shard]
crosslinks = list(filter( crosslinks = list(filter(
lambda c: hash_tree_root(state.current_crosslinks[shard]) in (c.parent_root, hash_tree_root(c)), lambda c: hash_tree_root(state.current_crosslinks[shard]) in (c.parent_root, hash_tree_root(c)),
@ -1402,11 +1402,11 @@ def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei:
``` ```
```python ```python
def get_attestation_deltas(state: BeaconState) -> Tuple[Deltas, Deltas]: def get_attestation_deltas(state: BeaconState) -> Tuple[Sequence[Gwei], Sequence[Gwei]]:
previous_epoch = get_previous_epoch(state) previous_epoch = get_previous_epoch(state)
total_balance = get_total_active_balance(state) total_balance = get_total_active_balance(state)
rewards = Deltas([Gwei(0) for _ in range(len(state.validators))]) rewards = [Gwei(0) for _ in range(len(state.validators))]
penalties = Deltas([Gwei(0) for _ in range(len(state.validators))]) penalties = [Gwei(0) for _ in range(len(state.validators))]
eligible_validator_indices = [ eligible_validator_indices = [
ValidatorIndex(index) for index, v in enumerate(state.validators) ValidatorIndex(index) for index, v in enumerate(state.validators)
if is_active_validator(v, previous_epoch) or (v.slashed and previous_epoch + 1 < v.withdrawable_epoch) if is_active_validator(v, previous_epoch) or (v.slashed and previous_epoch + 1 < v.withdrawable_epoch)
@ -1453,9 +1453,9 @@ def get_attestation_deltas(state: BeaconState) -> Tuple[Deltas, Deltas]:
``` ```
```python ```python
def get_crosslink_deltas(state: BeaconState) -> Tuple[Deltas, Deltas]: def get_crosslink_deltas(state: BeaconState) -> Tuple[Sequence[Gwei], Sequence[Gwei]]:
rewards = Deltas([Gwei(0) for _ in range(len(state.validators))]) rewards = [Gwei(0) for _ in range(len(state.validators))]
penalties = Deltas([Gwei(0) for _ in range(len(state.validators))]) penalties = [Gwei(0) for _ in range(len(state.validators))]
epoch = get_previous_epoch(state) epoch = get_previous_epoch(state)
for offset in range(get_epoch_committee_count(state, epoch)): for offset in range(get_epoch_committee_count(state, epoch)):
shard = Shard((get_epoch_start_shard(state, epoch) + offset) % SHARD_COUNT) shard = Shard((get_epoch_start_shard(state, epoch) + offset) % SHARD_COUNT)
@ -1642,7 +1642,7 @@ def process_operations(state: BeaconState, body: BeaconBlockBody) -> None:
(body.deposits, process_deposit), (body.deposits, process_deposit),
(body.voluntary_exits, process_voluntary_exit), (body.voluntary_exits, process_voluntary_exit),
(body.transfers, process_transfer), (body.transfers, process_transfer),
) # type: Tuple[Tuple[List, Callable], ...] ) # type: Sequence[Tuple[List, Callable]]
for operations, function in all_operations: for operations, function in all_operations:
for operation in operations: for operation in operations:
function(state, operation) function(state, operation)

View File

@ -133,7 +133,7 @@ def get_period_committee(state: BeaconState,
epoch: Epoch, epoch: Epoch,
shard: Shard, shard: Shard,
index: int, index: int,
count: int) -> Tuple[ValidatorIndex, ...]: count: int) -> Sequence[ValidatorIndex]:
""" """
Return committee for a period. Used to construct persistent committees. Return committee for a period. Used to construct persistent committees.
""" """
@ -159,7 +159,7 @@ def get_switchover_epoch(state: BeaconState, epoch: Epoch, index: ValidatorIndex
```python ```python
def get_persistent_committee(state: BeaconState, def get_persistent_committee(state: BeaconState,
shard: Shard, shard: Shard,
slot: Slot) -> Tuple[ValidatorIndex, ...]: slot: Slot) -> Sequence[ValidatorIndex]:
""" """
Return the persistent committee for the given ``shard`` at the given ``slot``. Return the persistent committee for the given ``shard`` at the given ``slot``.
""" """
@ -193,7 +193,7 @@ def get_shard_proposer_index(state: BeaconState,
shard: Shard, shard: Shard,
slot: Slot) -> Optional[ValidatorIndex]: slot: Slot) -> Optional[ValidatorIndex]:
# Randomly shift persistent committee # Randomly shift persistent committee
persistent_committee = get_persistent_committee(state, shard, slot) persistent_committee = list(get_persistent_committee(state, shard, slot))
seed = hash(state.current_shuffling_seed + int_to_bytes(shard, length=8) + int_to_bytes(slot, length=8)) seed = hash(state.current_shuffling_seed + int_to_bytes(shard, length=8) + int_to_bytes(slot, length=8))
random_index = bytes_to_int(seed[0:8]) % len(persistent_committee) random_index = bytes_to_int(seed[0:8]) % len(persistent_committee)
persistent_committee = persistent_committee[random_index:] + persistent_committee[:random_index] persistent_committee = persistent_committee[random_index:] + persistent_committee[:random_index]
@ -248,7 +248,7 @@ def verify_shard_attestation_signature(state: BeaconState,
### `compute_crosslink_data_root` ### `compute_crosslink_data_root`
```python ```python
def compute_crosslink_data_root(blocks: Iterable[ShardBlock]) -> Bytes32: def compute_crosslink_data_root(blocks: Sequence[ShardBlock]) -> Bytes32:
def is_power_of_two(value: int) -> bool: def is_power_of_two(value: int) -> bool:
return (value > 0) and (value & (value - 1) == 0) return (value > 0) and (value & (value - 1) == 0)
@ -287,9 +287,9 @@ Let:
* `candidate` be a candidate `ShardBlock` for which validity is to be determined by running `is_valid_shard_block` * `candidate` be a candidate `ShardBlock` for which validity is to be determined by running `is_valid_shard_block`
```python ```python
def is_valid_shard_block(beacon_blocks: TypingList[BeaconBlock], def is_valid_shard_block(beacon_blocks: Sequence[BeaconBlock],
beacon_state: BeaconState, beacon_state: BeaconState,
valid_shard_blocks: Iterable[ShardBlock], valid_shard_blocks: Sequence[ShardBlock],
candidate: ShardBlock) -> bool: candidate: ShardBlock) -> bool:
# Check if block is already determined valid # Check if block is already determined valid
for _, block in enumerate(valid_shard_blocks): for _, block in enumerate(valid_shard_blocks):
@ -336,7 +336,7 @@ def is_valid_shard_block(beacon_blocks: TypingList[BeaconBlock],
assert proposer_index is not None assert proposer_index is not None
assert bls_verify( assert bls_verify(
pubkey=beacon_state.validators[proposer_index].pubkey, pubkey=beacon_state.validators[proposer_index].pubkey,
message_hash=signing_root(block), message_hash=signing_root(candidate),
signature=candidate.signature, signature=candidate.signature,
domain=get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, slot_to_epoch(candidate.slot)), domain=get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, slot_to_epoch(candidate.slot)),
) )
@ -353,7 +353,7 @@ Let:
* `candidate` be a candidate `ShardAttestation` for which validity is to be determined by running `is_valid_shard_attestation` * `candidate` be a candidate `ShardAttestation` for which validity is to be determined by running `is_valid_shard_attestation`
```python ```python
def is_valid_shard_attestation(valid_shard_blocks: Iterable[ShardBlock], def is_valid_shard_attestation(valid_shard_blocks: Sequence[ShardBlock],
beacon_state: BeaconState, beacon_state: BeaconState,
candidate: ShardAttestation) -> bool: candidate: ShardAttestation) -> bool:
# Check shard block # Check shard block
@ -383,7 +383,7 @@ Let:
```python ```python
def is_valid_beacon_attestation(shard: Shard, def is_valid_beacon_attestation(shard: Shard,
shard_blocks: TypingList[ShardBlock], shard_blocks: Sequence[ShardBlock],
beacon_state: BeaconState, beacon_state: BeaconState,
valid_attestations: Set[Attestation], valid_attestations: Set[Attestation],
candidate: Attestation) -> bool: candidate: Attestation) -> bool: