Skip to content

Commit 9a951dc

Browse files
fix helpers typing
1 parent 5a3bb4f commit 9a951dc

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

eth/beacon/deposit_helpers.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,25 @@
2828
get_domain,
2929
)
3030
from eth.beacon.typing import (
31+
SlotNumber,
3132
BLSPubkey,
3233
BLSSignatureBytes,
34+
ValidatorIndex,
3335
Gwei,
3436
)
3537

3638

3739
def get_min_empty_validator_index(validators: Sequence[ValidatorRecord],
38-
validator_balances: Sequence[int],
39-
current_slot: int,
40-
zero_balance_validator_ttl: int) -> int:
40+
validator_balances: Sequence[Gwei],
41+
current_slot: SlotNumber,
42+
zero_balance_validator_ttl: int) -> ValidatorIndex:
4143
for index, (validator, balance) in enumerate(zip(validators, validator_balances)):
4244
is_empty = (
4345
balance == 0 and
4446
validator.latest_status_change_slot + zero_balance_validator_ttl <= current_slot
4547
)
4648
if is_empty:
47-
return index
49+
return ValidatorIndex(index)
4850
raise MinEmptyValidatorIndexNotFound()
4951

5052

@@ -81,7 +83,7 @@ def validate_proof_of_possession(state: BeaconState,
8183
def add_pending_validator(state: BeaconState,
8284
validator: ValidatorRecord,
8385
deposit: Gwei,
84-
zero_balance_validator_ttl: int) -> Tuple[BeaconState, int]:
86+
zero_balance_validator_ttl: int) -> Tuple[BeaconState, ValidatorIndex]:
8587
"""
8688
Add a validator to the existing minimum empty validator index or
8789
append to ``validator_registry``.
@@ -96,14 +98,13 @@ def add_pending_validator(state: BeaconState,
9698
)
9799
except MinEmptyValidatorIndexNotFound:
98100
index = None
99-
100101
# Append to the validator_registry
101102
validator_registry = state.validator_registry + (validator,)
102103
state = state.copy(
103104
validator_registry=validator_registry,
104105
validator_balances=state.validator_balances + (deposit, )
105106
)
106-
index = len(state.validator_registry) - 1
107+
index = ValidatorIndex(len(state.validator_registry) - 1)
107108
else:
108109
# Use the empty validator index
109110
state = state.update_validator(index, validator, deposit)
@@ -118,7 +119,7 @@ def process_deposit(*,
118119
proof_of_possession: BLSSignatureBytes,
119120
withdrawal_credentials: Hash32,
120121
randao_commitment: Hash32,
121-
zero_balance_validator_ttl: int) -> Tuple[BeaconState, int]:
122+
zero_balance_validator_ttl: int) -> Tuple[BeaconState, ValidatorIndex]:
122123
"""
123124
Process a deposit from Ethereum 1.0.
124125
"""
@@ -147,7 +148,7 @@ def process_deposit(*,
147148
)
148149
else:
149150
# Top-up - increase balance by deposit
150-
index = validator_pubkeys.index(pubkey)
151+
index = ValidatorIndex(validator_pubkeys.index(pubkey))
151152
validator = state.validator_registry[index]
152153

153154
if validator.withdrawal_credentials != withdrawal_credentials:

eth/beacon/helpers.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
from eth.beacon.typing import (
4747
ShardNumber,
4848
BLSPubkey,
49+
SlotNumber,
50+
ValidatorIndex,
51+
Bitfield,
52+
Gwei,
53+
Ether,
4954
)
5055

5156
if TYPE_CHECKING:
@@ -59,8 +64,8 @@
5964

6065
def _get_element_from_recent_list(
6166
target_list: Sequence[Any],
62-
target_slot: int,
63-
slot_relative_position: int) -> Any:
67+
target_slot: SlotNumber,
68+
slot_relative_position: SlotNumber) -> Any:
6469
"""
6570
Return the element from ``target_list`` by the ``target_slot`` number,
6671
where the the element should be at ``target_slot - slot_relative_position``th
@@ -88,12 +93,12 @@ def _get_element_from_recent_list(
8893
#
8994
def get_block_root(
9095
latest_block_roots: Sequence[Hash32],
91-
current_slot: int,
92-
slot: int) -> Hash32:
96+
current_slot: SlotNumber,
97+
slot: SlotNumber) -> Hash32:
9398
"""
9499
Returns the block root at a recent ``slot``.
95100
"""
96-
slot_relative_position = current_slot - len(latest_block_roots)
101+
slot_relative_position = SlotNumber(current_slot - len(latest_block_roots))
97102
return _get_element_from_recent_list(
98103
latest_block_roots,
99104
slot,
@@ -106,9 +111,9 @@ def get_block_root(
106111
#
107112
@to_tuple
108113
def _get_shard_committees_at_slot(
109-
state_slot: int,
114+
state_slot: SlotNumber,
110115
shard_committees_at_slots: Sequence[Sequence[ShardCommittee]],
111-
slot: int,
116+
slot: SlotNumber,
112117
epoch_length: int) -> Iterable[ShardCommittee]:
113118
if len(shard_committees_at_slots) != epoch_length * 2:
114119
raise ValueError(
@@ -118,7 +123,7 @@ def _get_shard_committees_at_slot(
118123
)
119124
)
120125

121-
slot_relative_position = state_slot - epoch_length
126+
slot_relative_position = SlotNumber(state_slot - epoch_length)
122127

123128
yield from _get_element_from_recent_list(
124129
shard_committees_at_slots,
@@ -128,7 +133,7 @@ def _get_shard_committees_at_slot(
128133

129134

130135
def get_shard_committees_at_slot(state: 'BeaconState',
131-
slot: int,
136+
slot: SlotNumber,
132137
epoch_length: int) -> Tuple[ShardCommittee]:
133138
"""
134139
Return the ``ShardCommittee`` for the ``slot``.
@@ -141,12 +146,13 @@ def get_shard_committees_at_slot(state: 'BeaconState',
141146
)
142147

143148

144-
def get_active_validator_indices(validators: Sequence['ValidatorRecord']) -> Tuple[int, ...]:
149+
def get_active_validator_indices(
150+
validators: Sequence['ValidatorRecord']) -> Tuple[ValidatorIndex, ...]:
145151
"""
146152
Get indices of active validators from ``validators``.
147153
"""
148154
return tuple(
149-
i for i, v in enumerate(validators)
155+
ValidatorIndex(i) for i, v in enumerate(validators)
150156
if v.is_active
151157
)
152158

@@ -156,8 +162,8 @@ def get_active_validator_indices(validators: Sequence['ValidatorRecord']) -> Tup
156162
#
157163
@to_tuple
158164
def _get_shards_committees_for_shard_indices(
159-
shard_indices: Sequence[Sequence[int]],
160-
start_shard: int,
165+
shard_indices: Sequence[Sequence[ShardNumber]],
166+
start_shard: ShardNumber,
161167
total_validator_count: int,
162168
shard_count: int) -> Iterable[ShardCommittee]:
163169
"""
@@ -175,7 +181,7 @@ def _get_shards_committees_for_shard_indices(
175181
def get_new_shuffling(*,
176182
seed: Hash32,
177183
validators: Sequence['ValidatorRecord'],
178-
crosslinking_start_shard: int,
184+
crosslinking_start_shard: ShardNumber,
179185
epoch_length: int,
180186
target_committee_size: int,
181187
shard_count: int) -> Iterable[Iterable[ShardCommittee]]:
@@ -288,7 +294,7 @@ def get_block_committees_info(parent_block: 'BaseBeaconBlock',
288294

289295

290296
def get_beacon_proposer_index(state: 'BeaconState',
291-
slot: int,
297+
slot: SlotNumber,
292298
epoch_length: int) -> int:
293299
"""
294300
Return the beacon proposer index for the ``slot``.
@@ -318,10 +324,10 @@ def get_beacon_proposer_index(state: 'BeaconState',
318324
#
319325
@to_tuple
320326
def get_attestation_participants(state: 'BeaconState',
321-
slot: int,
322-
shard: int,
323-
participation_bitfield: bytes,
324-
epoch_length: int) -> Iterable[int]:
327+
slot: SlotNumber,
328+
shard: ShardNumber,
329+
participation_bitfield: Bitfield,
330+
epoch_length: int) -> Iterable[ValidatorIndex]:
325331
"""
326332
Return the participants' indices at the ``slot`` of shard ``shard``
327333
from ``participation_bitfield``.
@@ -365,7 +371,10 @@ def get_attestation_participants(state: 'BeaconState',
365371
#
366372
# Misc
367373
#
368-
def get_effective_balance(validator_balances: Sequence[int], index: int, max_deposit: int) -> int:
374+
def get_effective_balance(
375+
validator_balances: Sequence[Gwei],
376+
index: ValidatorIndex,
377+
max_deposit: Ether) -> Gwei:
369378
"""
370379
Return the effective balance (also known as "balance at stake") for a
371380
``validator`` with the given ``index``.
@@ -374,7 +383,7 @@ def get_effective_balance(validator_balances: Sequence[int], index: int, max_dep
374383

375384

376385
def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_chain_tip: Hash32,
377-
validator_index: int,
386+
validator_index: ValidatorIndex,
378387
pubkey: BLSPubkey,
379388
flag: int) -> Hash32:
380389
"""
@@ -390,7 +399,7 @@ def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_
390399

391400

392401
def get_fork_version(fork_data: 'ForkData',
393-
slot: int) -> int:
402+
slot: SlotNumber) -> int:
394403
"""
395404
Return the current ``fork_version`` from the given ``fork_data`` and ``slot``.
396405
"""
@@ -401,7 +410,7 @@ def get_fork_version(fork_data: 'ForkData',
401410

402411

403412
def get_domain(fork_data: 'ForkData',
404-
slot: int,
413+
slot: SlotNumber,
405414
domain_type: SignatureDomain) -> int:
406415
"""
407416
Return the domain number of the current fork and ``domain_type``.
@@ -415,14 +424,14 @@ def get_domain(fork_data: 'ForkData',
415424

416425
@to_tuple
417426
def get_pubkey_for_indices(validators: Sequence['ValidatorRecord'],
418-
indices: Sequence[int]) -> Iterable[int]:
427+
indices: Sequence[ValidatorIndex]) -> Iterable[BLSPubkey]:
419428
for index in indices:
420429
yield validators[index].pubkey
421430

422431

423432
@to_tuple
424433
def generate_aggregate_pubkeys(validators: Sequence['ValidatorRecord'],
425-
vote_data: 'SlashableVoteData') -> Iterable[int]:
434+
vote_data: 'SlashableVoteData') -> Iterable[BLSPubkey]:
426435
"""
427436
Compute the aggregate pubkey we expect based on
428437
the proof-of-custody indices found in the ``vote_data``.

eth/beacon/types/states.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
Bitfield,
2828
Timestamp,
2929
Gwei,
30+
ValidatorIndex,
3031
)
3132

3233
from .pending_attestation_records import PendingAttestationRecord
@@ -168,7 +169,7 @@ def num_crosslinks(self) -> int:
168169
return len(self.latest_crosslinks)
169170

170171
def update_validator(self,
171-
validator_index: int,
172+
validator_index: ValidatorIndex,
172173
validator: ValidatorRecord,
173174
balance: Gwei) -> 'BeaconState':
174175
validator_registry = list(self.validator_registry)

0 commit comments

Comments
 (0)