Skip to content

Commit 583a193

Browse files
authored
Merge pull request #1700 from hwwhww/rework_deposit_helpers
Sync ethereum/consensus-specs#374 part1: types, constants, deposit_helpers
2 parents f12a207 + d2f76bd commit 583a193

21 files changed

+535
-623
lines changed

eth/beacon/constants.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from eth.beacon.typing import BLSSignature
1+
from eth.beacon.typing import (
2+
BLSSignature,
3+
SlotNumber,
4+
)
5+
26

37
#
48
# shuffle function
@@ -14,3 +18,5 @@
1418
RAND_MAX = 2 ** (RAND_BYTES * 8) - 1
1519

1620
EMPTY_SIGNATURE = BLSSignature((0, 0))
21+
GWEI_PER_ETH = 10**9
22+
FAR_FUTURE_SLOT = SlotNumber(2**64 - 1)

eth/beacon/deposit_helpers.py

Lines changed: 23 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
from typing import (
2-
Sequence,
3-
Tuple,
4-
)
5-
61
from eth_typing import (
72
Hash32,
83
)
@@ -18,38 +13,20 @@
1813
from eth.beacon.enums import (
1914
SignatureDomain,
2015
)
21-
from eth.beacon.exceptions import (
22-
MinEmptyValidatorIndexNotFound,
23-
)
2416
from eth.beacon.types.deposit_input import DepositInput
2517
from eth.beacon.types.states import BeaconState
2618
from eth.beacon.types.validator_records import ValidatorRecord
2719
from eth.beacon.helpers import (
2820
get_domain,
2921
)
3022
from eth.beacon.typing import (
31-
SlotNumber,
3223
BLSPubkey,
3324
BLSSignature,
3425
ValidatorIndex,
3526
Gwei,
3627
)
3728

3829

39-
def get_min_empty_validator_index(validators: Sequence[ValidatorRecord],
40-
validator_balances: Sequence[Gwei],
41-
current_slot: SlotNumber,
42-
zero_balance_validator_ttl: int) -> ValidatorIndex:
43-
for index, (validator, balance) in enumerate(zip(validators, validator_balances)):
44-
is_empty = (
45-
balance == 0 and
46-
validator.latest_status_change_slot + zero_balance_validator_ttl <= current_slot
47-
)
48-
if is_empty:
49-
return ValidatorIndex(index)
50-
raise MinEmptyValidatorIndexNotFound()
51-
52-
5330
def validate_proof_of_possession(state: BeaconState,
5431
pubkey: BLSPubkey,
5532
proof_of_possession: BLSSignature,
@@ -84,72 +61,53 @@ def validate_proof_of_possession(state: BeaconState,
8461

8562
def add_pending_validator(state: BeaconState,
8663
validator: ValidatorRecord,
87-
deposit: Gwei,
88-
zero_balance_validator_ttl: int) -> Tuple[BeaconState, ValidatorIndex]:
64+
amount: Gwei) -> BeaconState:
8965
"""
90-
Add a validator to the existing minimum empty validator index or
91-
append to ``validator_registry``.
66+
Add a validator to ``state``.
9267
"""
93-
# Check if there's empty validator index in `validator_registry`
94-
try:
95-
index = get_min_empty_validator_index(
96-
state.validator_registry,
97-
state.validator_balances,
98-
state.slot,
99-
zero_balance_validator_ttl,
100-
)
101-
except MinEmptyValidatorIndexNotFound:
102-
index = None
103-
# Append to the validator_registry
104-
validator_registry = state.validator_registry + (validator,)
105-
state = state.copy(
106-
validator_registry=validator_registry,
107-
validator_balances=state.validator_balances + (deposit, )
108-
)
109-
index = ValidatorIndex(len(state.validator_registry) - 1)
110-
else:
111-
# Use the empty validator index
112-
state = state.update_validator(index, validator, deposit)
68+
state = state.copy(
69+
validator_registry=state.validator_registry + (validator,),
70+
validator_balances=state.validator_balances + (amount, ),
71+
)
11372

114-
return state, index
73+
return state
11574

11675

11776
def process_deposit(*,
11877
state: BeaconState,
11978
pubkey: BLSPubkey,
120-
deposit: Gwei,
79+
amount: Gwei,
12180
proof_of_possession: BLSSignature,
12281
withdrawal_credentials: Hash32,
12382
randao_commitment: Hash32,
124-
custody_commitment: Hash32,
125-
zero_balance_validator_ttl: int) -> Tuple[BeaconState, ValidatorIndex]:
83+
custody_commitment: Hash32) -> BeaconState:
12684
"""
12785
Process a deposit from Ethereum 1.0.
12886
"""
12987
validate_proof_of_possession(
130-
state,
131-
pubkey,
132-
proof_of_possession,
133-
withdrawal_credentials,
134-
randao_commitment,
135-
custody_commitment,
88+
state=state,
89+
pubkey=pubkey,
90+
proof_of_possession=proof_of_possession,
91+
withdrawal_credentials=withdrawal_credentials,
92+
randao_commitment=randao_commitment,
93+
custody_commitment=custody_commitment,
13694
)
13795

13896
validator_pubkeys = tuple(v.pubkey for v in state.validator_registry)
13997
if pubkey not in validator_pubkeys:
140-
validator = ValidatorRecord.get_pending_validator(
98+
validator = ValidatorRecord.create_pending_validator(
14199
pubkey=pubkey,
142100
withdrawal_credentials=withdrawal_credentials,
143101
randao_commitment=randao_commitment,
144-
latest_status_change_slot=state.slot,
145102
custody_commitment=custody_commitment,
146103
)
147104

148-
state, index = add_pending_validator(
105+
# Note: In phase 2 registry indices that has been withdrawn for a long time
106+
# will be recycled.
107+
state = add_pending_validator(
149108
state,
150109
validator,
151-
deposit,
152-
zero_balance_validator_ttl,
110+
amount,
153111
)
154112
else:
155113
# Top-up - increase balance by deposit
@@ -166,10 +124,9 @@ def process_deposit(*,
166124
)
167125

168126
# Update validator's balance and state
169-
state = state.update_validator(
127+
state = state.update_validator_balance(
170128
validator_index=index,
171-
validator=validator,
172-
balance=state.validator_balances[index] + deposit,
129+
balance=state.validator_balances[index] + amount,
173130
)
174131

175-
return state, index
132+
return state

eth/beacon/enums.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
from enum import IntEnum
22

33

4-
class ValidatorStatusCode(IntEnum):
5-
PENDING_ACTIVATION = 0
6-
ACTIVE = 1
7-
ACTIVE_PENDING_EXIT = 2
8-
EXITED_WITHOUT_PENALTY = 3
9-
EXITED_WITH_PENALTY = 4
4+
class ValidatorStatusFlags(IntEnum):
5+
INITIATED_EXIT = 1
6+
WITHDRAWABLE = 2
107

118

129
class ValidatorRegistryDeltaFlag(IntEnum):

eth/beacon/exceptions.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

eth/beacon/helpers.py

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
TYPE_CHECKING,
66
)
77

8+
import functools
9+
810
from eth_utils import (
9-
denoms,
1011
to_tuple,
1112
ValidationError,
1213
)
13-
1414
from eth_typing import (
1515
Hash32,
1616
)
@@ -23,26 +23,23 @@
2323
from eth._utils.numeric import (
2424
clamp,
2525
)
26-
27-
from eth.beacon.types.validator_registry_delta_block import (
28-
ValidatorRegistryDeltaBlock,
26+
from eth.beacon._utils.random import (
27+
shuffle,
28+
split,
2929
)
30+
3031
from eth.beacon.block_committees_info import (
3132
BlockCommitteesInfo,
3233
)
34+
from eth.beacon.constants import (
35+
GWEI_PER_ETH,
36+
)
3337
from eth.beacon.enums import (
3438
SignatureDomain,
35-
ValidatorRegistryDeltaFlag,
3639
)
3740
from eth.beacon.types.shard_committees import (
3841
ShardCommittee,
3942
)
40-
from eth.beacon._utils.random import (
41-
shuffle,
42-
split,
43-
)
44-
import functools
45-
4643
from eth.beacon.typing import (
4744
Bitfield,
4845
BLSPubkey,
@@ -157,14 +154,14 @@ def get_shard_committees_at_slot(state: 'BeaconState',
157154
)
158155

159156

160-
def get_active_validator_indices(
161-
validators: Sequence['ValidatorRecord']) -> Tuple[ValidatorIndex, ...]:
157+
def get_active_validator_indices(validators: Sequence['ValidatorRecord'],
158+
slot: SlotNumber) -> Tuple[ValidatorIndex, ...]:
162159
"""
163160
Get indices of active validators from ``validators``.
164161
"""
165162
return tuple(
166-
ValidatorIndex(i) for i, v in enumerate(validators)
167-
if v.is_active
163+
ValidatorIndex(index) for index, validator in enumerate(validators)
164+
if validator.is_active(slot)
168165
)
169166

170167

@@ -189,13 +186,14 @@ def _get_shards_committees_for_shard_indices(
189186

190187

191188
@to_tuple
192-
def get_new_shuffling(*,
193-
seed: Hash32,
194-
validators: Sequence['ValidatorRecord'],
195-
crosslinking_start_shard: ShardNumber,
196-
epoch_length: int,
197-
target_committee_size: int,
198-
shard_count: int) -> Iterable[Iterable[ShardCommittee]]:
189+
def get_shuffling(*,
190+
seed: Hash32,
191+
validators: Sequence['ValidatorRecord'],
192+
crosslinking_start_shard: ShardNumber,
193+
slot: SlotNumber,
194+
epoch_length: int,
195+
target_committee_size: int,
196+
shard_count: int) -> Iterable[Tuple[ShardCommittee]]:
199197
"""
200198
Return shuffled ``shard_committee_for_slots`` (``[[ShardCommittee]]``) of
201199
the given active ``validators`` using ``seed`` as entropy.
@@ -238,7 +236,7 @@ def get_new_shuffling(*,
238236
],
239237
]
240238
"""
241-
active_validators = get_active_validator_indices(validators)
239+
active_validators = get_active_validator_indices(validators, slot)
242240
active_validators_size = len(active_validators)
243241
committees_per_slot = clamp(
244242
1,
@@ -390,23 +388,7 @@ def get_effective_balance(
390388
Return the effective balance (also known as "balance at stake") for a
391389
``validator`` with the given ``index``.
392390
"""
393-
return min(validator_balances[index], max_deposit * denoms.gwei)
394-
395-
396-
def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_chain_tip: Hash32,
397-
validator_index: ValidatorIndex,
398-
pubkey: BLSPubkey,
399-
flag: ValidatorRegistryDeltaFlag) -> Hash32:
400-
"""
401-
Compute the next hash in the validator registry delta hash chain.
402-
"""
403-
# TODO: switch to SSZ tree hashing
404-
return ValidatorRegistryDeltaBlock(
405-
latest_registry_delta_root=current_validator_registry_delta_chain_tip,
406-
validator_index=validator_index,
407-
pubkey=pubkey,
408-
flag=flag,
409-
).root
391+
return min(validator_balances[index], Gwei(max_deposit * GWEI_PER_ETH))
410392

411393

412394
def get_fork_version(fork_data: 'ForkData',

eth/beacon/state_machines/configs.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
('EJECTION_BALANCE', Ether),
2323
('MAX_BALANCE_CHURN_QUOTIENT', int),
2424
('BEACON_CHAIN_SHARD_NUMBER', ShardNumber),
25-
('BLS_WITHDRAWAL_PREFIX_BYTE', bytes),
2625
('MAX_CASPER_VOTES', int),
2726
('LATEST_BLOCK_ROOTS_LENGTH', int),
2827
('LATEST_RANDAO_MIXES_LENGTH', int),
28+
('LATEST_PENALIZED_EXIT_LENGTH', int),
2929
# EMPTY_SIGNATURE is defined in constants.py
3030
# Deposit contract
3131
('DEPOSIT_CONTRACT_ADDRESS', Address),
@@ -34,16 +34,17 @@
3434
('MAX_DEPOSIT', Ether),
3535
# ZERO_HASH (ZERO_HASH32) is defined in constants.py
3636
# Initial values
37-
('INITIAL_FORK_VERSION', int),
38-
('INITIAL_SLOT_NUMBER', SlotNumber),
37+
('GENESIS_FORK_VERSION', int),
38+
('GENESIS_SLOT', SlotNumber),
39+
('BLS_WITHDRAWAL_PREFIX_BYTE', bytes),
3940
# Time parameters
4041
('SLOT_DURATION', Second),
4142
('MIN_ATTESTATION_INCLUSION_DELAY', int),
4243
('EPOCH_LENGTH', int),
44+
('SEED_LOOKAHEAD', int),
45+
('ENTRY_EXIT_DELAY', int),
4346
('POW_RECEIPT_ROOT_VOTING_PERIOD', int),
44-
('SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD', int),
45-
('COLLECTIVE_PENALTY_CALCULATION_PERIOD', int),
46-
('ZERO_BALANCE_VALIDATOR_TTL', int),
47+
('MIN_VALIDATOR_WITHDRAWAL_TIME', int),
4748
# Reward and penalty quotients
4849
('BASE_REWARD_QUOTIENT', int),
4950
('WHISTLEBLOWER_REWARD_QUOTIENT', int),

eth/beacon/state_machines/forks/serenity/configs.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@
1717
EJECTION_BALANCE=Ether(2**4), # (= 16) ETH
1818
MAX_BALANCE_CHURN_QUOTIENT=2**5, # (= 32)
1919
BEACON_CHAIN_SHARD_NUMBER=ShardNumber(2**64 - 1),
20-
BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
2120
MAX_CASPER_VOTES=2**10, # (= 1,024) votes
2221
LATEST_BLOCK_ROOTS_LENGTH=2**13, # (= 8,192) block roots
2322
LATEST_RANDAO_MIXES_LENGTH=2**13, # (= 8,192) randao mixes
23+
LATEST_PENALIZED_EXIT_LENGTH=2**13, # (= 8,192) randao mixes
2424
# Deposit contract
2525
DEPOSIT_CONTRACT_ADDRESS=ZERO_ADDRESS, # TBD
2626
DEPOSIT_CONTRACT_TREE_DEPTH=2**5, # (= 32)
2727
MIN_DEPOSIT=Ether(2**0), # (= 1) ETH
2828
MAX_DEPOSIT=Ether(2**5), # (= 32) ETH
2929
# Initial values
30-
INITIAL_FORK_VERSION=0,
31-
INITIAL_SLOT_NUMBER=SlotNumber(0),
30+
GENESIS_FORK_VERSION=0,
31+
GENESIS_SLOT=SlotNumber(0),
32+
BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
3233
# Time parameters
3334
SLOT_DURATION=Second(6), # seconds
3435
MIN_ATTESTATION_INCLUSION_DELAY=2**2, # (= 4) slots
3536
EPOCH_LENGTH=2**6, # (= 64) slots
37+
SEED_LOOKAHEAD=2**6, # (= 64) slots
38+
ENTRY_EXIT_DELAY=2**8, # (= 256) slots
3639
POW_RECEIPT_ROOT_VOTING_PERIOD=2**10, # (= 1,024) slots
37-
SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD=2**17, # (= 131,072) slots
38-
COLLECTIVE_PENALTY_CALCULATION_PERIOD=2**20, # (= 1,048,576) slots
39-
ZERO_BALANCE_VALIDATOR_TTL=2**22, # (= 4,194,304) slots
40+
MIN_VALIDATOR_WITHDRAWAL_TIME=2**14, # (= 16,384) slots
4041
# Reward and penalty quotients
4142
BASE_REWARD_QUOTIENT=2**10, # (= 1,024)
4243
WHISTLEBLOWER_REWARD_QUOTIENT=2**9, # (= 512)

0 commit comments

Comments
 (0)