type annotation clean up
This commit is contained in:
parent
16093eb8ce
commit
00aae07d46
|
@ -12,8 +12,7 @@ from typing import (
|
|||
|
||||
|
||||
PHASE0_IMPORTS = '''from typing import (
|
||||
Any, Callable, Iterable, Dict, Set, Tuple, NewType,
|
||||
List as TypingList,
|
||||
Any, Callable, Iterable, Dict, Set, Sequence, Tuple,
|
||||
)
|
||||
|
||||
from dataclasses import (
|
||||
|
@ -38,7 +37,7 @@ from eth2spec.utils.bls import (
|
|||
from eth2spec.utils.hash_function import hash
|
||||
'''
|
||||
PHASE1_IMPORTS = '''from typing import (
|
||||
Any, Callable, Dict, Optional, Set, Tuple, Iterable, NewType,
|
||||
Any, Callable, Dict, Iterable, Optional, Set, Sequence, Tuple,
|
||||
List as TypingList
|
||||
)
|
||||
|
||||
|
@ -79,13 +78,13 @@ def hash(x: bytes) -> Hash:
|
|||
|
||||
# Monkey patch validator compute committee code
|
||||
_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,
|
||||
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)
|
||||
|
||||
if param_hash not in committee_cache:
|
||||
|
@ -154,7 +153,6 @@ def objects_to_spec(functions: Dict[str, str],
|
|||
spec = (
|
||||
imports
|
||||
+ '\n\n' + new_type_definitions
|
||||
+ '\n\n' + "Deltas = NewType('Deltas', TypingList[Gwei])"
|
||||
+ '\n\n' + constants_spec
|
||||
+ '\n\n\n' + ssz_objects_instantiation_spec
|
||||
+ '\n\n' + functions_spec
|
||||
|
|
|
@ -651,13 +651,11 @@ def is_slashable_validator(validator: Validator, epoch: Epoch) -> bool:
|
|||
### `get_active_validator_indices`
|
||||
|
||||
```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``.
|
||||
"""
|
||||
return List[ValidatorIndex, VALIDATOR_REGISTRY_SIZE](
|
||||
i for i, v in enumerate(state.validators) if is_active_validator(v, epoch)
|
||||
)
|
||||
return [ValidatorIndex(i) for i, v in enumerate(state.validators) if is_active_validator(v, epoch)]
|
||||
```
|
||||
|
||||
### `increase_balance`
|
||||
|
@ -819,7 +817,7 @@ def get_beacon_proposer_index(state: BeaconState) -> ValidatorIndex:
|
|||
### `verify_merkle_branch`
|
||||
|
||||
```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``
|
||||
starting with the given ``root``.
|
||||
|
@ -863,19 +861,19 @@ def get_shuffled_index(index: ValidatorIndex, index_count: int, seed: Hash) -> V
|
|||
### `compute_committee`
|
||||
|
||||
```python
|
||||
def compute_committee(indices: Tuple[ValidatorIndex, ...],
|
||||
seed: Hash, index: int, count: int) -> Tuple[ValidatorIndex, ...]:
|
||||
def compute_committee(indices: Sequence[ValidatorIndex],
|
||||
seed: Hash, index: int, count: int) -> Sequence[ValidatorIndex]:
|
||||
start = (len(indices) * index) // 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`
|
||||
|
||||
```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(
|
||||
indices=tuple(get_active_validator_indices(state, epoch)),
|
||||
indices=get_active_validator_indices(state, epoch),
|
||||
seed=generate_seed(state, epoch),
|
||||
index=(shard + SHARD_COUNT - get_epoch_start_shard(state, epoch)) % SHARD_COUNT,
|
||||
count=get_epoch_committee_count(state, epoch),
|
||||
|
@ -887,13 +885,13 @@ def get_crosslink_committee(state: BeaconState, epoch: Epoch, shard: Shard) -> T
|
|||
```python
|
||||
def get_attesting_indices(state: BeaconState,
|
||||
attestation_data: AttestationData,
|
||||
bitfield: bytes) -> Tuple[ValidatorIndex, ...]:
|
||||
bitfield: bytes) -> Sequence[ValidatorIndex]:
|
||||
"""
|
||||
Return the sorted attesting indices corresponding to ``attestation_data`` and ``bitfield``.
|
||||
"""
|
||||
committee = get_crosslink_committee(state, attestation_data.target_epoch, attestation_data.crosslink.shard)
|
||||
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`
|
||||
|
@ -1139,7 +1137,7 @@ def slash_validator(state: BeaconState,
|
|||
|
||||
### 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
|
||||
* `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:
|
||||
|
||||
```python
|
||||
def is_genesis_trigger(deposits: Iterable[Deposit], timestamp: uint64) -> bool:
|
||||
def is_genesis_trigger(deposits: Sequence[Deposit], timestamp: uint64) -> bool:
|
||||
# Process deposits
|
||||
state = BeaconState()
|
||||
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)`.
|
||||
|
||||
```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(
|
||||
genesis_time=genesis_time,
|
||||
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
|
||||
|
||||
# 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):
|
||||
state.active_index_roots[index] = genesis_active_index_root
|
||||
|
||||
|
@ -1271,17 +1271,17 @@ def process_epoch(state: BeaconState) -> None:
|
|||
|
||||
```python
|
||||
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
|
||||
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))
|
||||
return state.current_epoch_attestations if epoch == get_current_epoch(state) else state.previous_epoch_attestations
|
||||
```
|
||||
|
||||
```python
|
||||
def get_matching_target_attestations(state: BeaconState, epoch: Epoch) -> Iterable[PendingAttestation]:
|
||||
def get_matching_target_attestations(state: BeaconState, epoch: Epoch) -> Sequence[PendingAttestation]:
|
||||
return [
|
||||
a for a in get_matching_source_attestations(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
|
||||
def get_matching_head_attestations(state: BeaconState, epoch: Epoch) -> Iterable[PendingAttestation]:
|
||||
def get_matching_head_attestations(state: BeaconState, epoch: Epoch) -> Sequence[PendingAttestation]:
|
||||
return [
|
||||
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))
|
||||
|
@ -1298,22 +1298,22 @@ def get_matching_head_attestations(state: BeaconState, epoch: Epoch) -> Iterable
|
|||
|
||||
```python
|
||||
def get_unslashed_attesting_indices(state: BeaconState,
|
||||
attestations: Iterable[PendingAttestation]) -> Iterable[ValidatorIndex]:
|
||||
attestations: Sequence[PendingAttestation]) -> Set[ValidatorIndex]:
|
||||
output = set() # type: Set[ValidatorIndex]
|
||||
for a in attestations:
|
||||
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
|
||||
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))
|
||||
```
|
||||
|
||||
```python
|
||||
def get_winning_crosslink_and_attesting_indices(state: BeaconState,
|
||||
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]
|
||||
crosslinks = list(filter(
|
||||
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
|
||||
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)
|
||||
total_balance = get_total_active_balance(state)
|
||||
rewards = Deltas([Gwei(0) for _ in range(len(state.validators))])
|
||||
penalties = Deltas([Gwei(0) for _ in range(len(state.validators))])
|
||||
rewards = [Gwei(0) for _ in range(len(state.validators))]
|
||||
penalties = [Gwei(0) for _ in range(len(state.validators))]
|
||||
eligible_validator_indices = [
|
||||
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)
|
||||
|
@ -1453,9 +1453,9 @@ def get_attestation_deltas(state: BeaconState) -> Tuple[Deltas, Deltas]:
|
|||
```
|
||||
|
||||
```python
|
||||
def get_crosslink_deltas(state: BeaconState) -> Tuple[Deltas, Deltas]:
|
||||
rewards = Deltas([Gwei(0) for _ in range(len(state.validators))])
|
||||
penalties = Deltas([Gwei(0) for _ in range(len(state.validators))])
|
||||
def get_crosslink_deltas(state: BeaconState) -> Tuple[Sequence[Gwei], Sequence[Gwei]]:
|
||||
rewards = [Gwei(0) for _ in range(len(state.validators))]
|
||||
penalties = [Gwei(0) for _ in range(len(state.validators))]
|
||||
epoch = get_previous_epoch(state)
|
||||
for offset in range(get_epoch_committee_count(state, epoch)):
|
||||
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.voluntary_exits, process_voluntary_exit),
|
||||
(body.transfers, process_transfer),
|
||||
) # type: Tuple[Tuple[List, Callable], ...]
|
||||
) # type: Sequence[Tuple[List, Callable]]
|
||||
for operations, function in all_operations:
|
||||
for operation in operations:
|
||||
function(state, operation)
|
||||
|
|
|
@ -133,7 +133,7 @@ def get_period_committee(state: BeaconState,
|
|||
epoch: Epoch,
|
||||
shard: Shard,
|
||||
index: int,
|
||||
count: int) -> Tuple[ValidatorIndex, ...]:
|
||||
count: int) -> Sequence[ValidatorIndex]:
|
||||
"""
|
||||
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
|
||||
def get_persistent_committee(state: BeaconState,
|
||||
shard: Shard,
|
||||
slot: Slot) -> Tuple[ValidatorIndex, ...]:
|
||||
slot: Slot) -> Sequence[ValidatorIndex]:
|
||||
"""
|
||||
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,
|
||||
slot: Slot) -> Optional[ValidatorIndex]:
|
||||
# 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))
|
||||
random_index = bytes_to_int(seed[0:8]) % len(persistent_committee)
|
||||
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`
|
||||
|
||||
```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:
|
||||
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`
|
||||
|
||||
```python
|
||||
def is_valid_shard_block(beacon_blocks: TypingList[BeaconBlock],
|
||||
def is_valid_shard_block(beacon_blocks: Sequence[BeaconBlock],
|
||||
beacon_state: BeaconState,
|
||||
valid_shard_blocks: Iterable[ShardBlock],
|
||||
valid_shard_blocks: Sequence[ShardBlock],
|
||||
candidate: ShardBlock) -> bool:
|
||||
# Check if block is already determined valid
|
||||
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 bls_verify(
|
||||
pubkey=beacon_state.validators[proposer_index].pubkey,
|
||||
message_hash=signing_root(block),
|
||||
message_hash=signing_root(candidate),
|
||||
signature=candidate.signature,
|
||||
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`
|
||||
|
||||
```python
|
||||
def is_valid_shard_attestation(valid_shard_blocks: Iterable[ShardBlock],
|
||||
def is_valid_shard_attestation(valid_shard_blocks: Sequence[ShardBlock],
|
||||
beacon_state: BeaconState,
|
||||
candidate: ShardAttestation) -> bool:
|
||||
# Check shard block
|
||||
|
@ -383,7 +383,7 @@ Let:
|
|||
|
||||
```python
|
||||
def is_valid_beacon_attestation(shard: Shard,
|
||||
shard_blocks: TypingList[ShardBlock],
|
||||
shard_blocks: Sequence[ShardBlock],
|
||||
beacon_state: BeaconState,
|
||||
valid_attestations: Set[Attestation],
|
||||
candidate: Attestation) -> bool:
|
||||
|
|
Loading…
Reference in New Issue