Skip to content

Commit 64925be

Browse files
committed
Sync ethereum/consensus-specs#374 part1: types, constants, deposit_helpers
1 parent f12a207 commit 64925be

20 files changed

+522
-555
lines changed

eth/beacon/deposit_helpers.py

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import (
2-
Sequence,
32
Tuple,
43
)
54

@@ -18,9 +17,6 @@
1817
from eth.beacon.enums import (
1918
SignatureDomain,
2019
)
21-
from eth.beacon.exceptions import (
22-
MinEmptyValidatorIndexNotFound,
23-
)
2420
from eth.beacon.types.deposit_input import DepositInput
2521
from eth.beacon.types.states import BeaconState
2622
from eth.beacon.types.validator_records import ValidatorRecord
@@ -36,20 +32,6 @@
3632
)
3733

3834

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-
5335
def validate_proof_of_possession(state: BeaconState,
5436
pubkey: BLSPubkey,
5537
proof_of_possession: BLSSignature,
@@ -84,55 +66,40 @@ def validate_proof_of_possession(state: BeaconState,
8466

8567
def add_pending_validator(state: BeaconState,
8668
validator: ValidatorRecord,
87-
deposit: Gwei,
88-
zero_balance_validator_ttl: int) -> Tuple[BeaconState, ValidatorIndex]:
69+
amount: Gwei) -> Tuple[BeaconState, int]:
8970
"""
90-
Add a validator to the existing minimum empty validator index or
91-
append to ``validator_registry``.
71+
Add a validator to ``state``.
9272
"""
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)
73+
validator_registry = state.validator_registry + (validator,)
74+
state = state.copy(
75+
validator_registry=validator_registry,
76+
validator_balances=state.validator_balances + (amount, )
77+
)
78+
79+
index = len(state.validator_registry) - 1
11380

11481
return state, index
11582

11683

11784
def process_deposit(*,
11885
state: BeaconState,
11986
pubkey: BLSPubkey,
120-
deposit: Gwei,
87+
amount: Gwei,
12188
proof_of_possession: BLSSignature,
12289
withdrawal_credentials: Hash32,
12390
randao_commitment: Hash32,
12491
custody_commitment: Hash32,
125-
zero_balance_validator_ttl: int) -> Tuple[BeaconState, ValidatorIndex]:
92+
far_future_slot: SlotNumber) -> BeaconState:
12693
"""
12794
Process a deposit from Ethereum 1.0.
12895
"""
12996
validate_proof_of_possession(
130-
state,
131-
pubkey,
132-
proof_of_possession,
133-
withdrawal_credentials,
134-
randao_commitment,
135-
custody_commitment,
97+
state=state,
98+
pubkey=pubkey,
99+
proof_of_possession=proof_of_possession,
100+
withdrawal_credentials=withdrawal_credentials,
101+
randao_commitment=randao_commitment,
102+
custody_commitment=custody_commitment,
136103
)
137104

138105
validator_pubkeys = tuple(v.pubkey for v in state.validator_registry)
@@ -141,15 +108,16 @@ def process_deposit(*,
141108
pubkey=pubkey,
142109
withdrawal_credentials=withdrawal_credentials,
143110
randao_commitment=randao_commitment,
144-
latest_status_change_slot=state.slot,
145111
custody_commitment=custody_commitment,
112+
far_future_slot=far_future_slot,
146113
)
147114

115+
# Note: In phase 2 registry indices that has been withdrawn for a long time
116+
# will be recycled.
148117
state, index = add_pending_validator(
149118
state,
150119
validator,
151-
deposit,
152-
zero_balance_validator_ttl,
120+
amount,
153121
)
154122
else:
155123
# Top-up - increase balance by deposit
@@ -166,10 +134,9 @@ def process_deposit(*,
166134
)
167135

168136
# Update validator's balance and state
169-
state = state.update_validator(
137+
state = state.update_validator_balance(
170138
validator_index=index,
171-
validator=validator,
172-
balance=state.validator_balances[index] + deposit,
139+
balance=state.validator_balances[index] + amount,
173140
)
174141

175-
return state, index
142+
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
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
from eth.exceptions import (
2-
PyEVMError,
3-
)
4-
5-
6-
class MinEmptyValidatorIndexNotFound(PyEVMError):
7-
"""
8-
No empty slot in the validator registry
9-
"""
10-
pass

eth/beacon/helpers.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@
2424
clamp,
2525
)
2626

27-
from eth.beacon.types.validator_registry_delta_block import (
28-
ValidatorRegistryDeltaBlock,
29-
)
3027
from eth.beacon.block_committees_info import (
3128
BlockCommitteesInfo,
3229
)
3330
from eth.beacon.enums import (
3431
SignatureDomain,
35-
ValidatorRegistryDeltaFlag,
3632
)
3733
from eth.beacon.types.shard_committees import (
3834
ShardCommittee,
@@ -157,14 +153,14 @@ def get_shard_committees_at_slot(state: 'BeaconState',
157153
)
158154

159155

160-
def get_active_validator_indices(
161-
validators: Sequence['ValidatorRecord']) -> Tuple[ValidatorIndex, ...]:
156+
def get_active_validator_indices(validators: Sequence['ValidatorRecord'],
157+
slot: SlotNumber) -> Tuple[int, ...]:
162158
"""
163159
Get indices of active validators from ``validators``.
164160
"""
165161
return tuple(
166-
ValidatorIndex(i) for i, v in enumerate(validators)
167-
if v.is_active
162+
i for i, v in enumerate(validators)
163+
if v.is_active(slot)
168164
)
169165

170166

@@ -189,13 +185,14 @@ def _get_shards_committees_for_shard_indices(
189185

190186

191187
@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]]:
188+
def get_shuffling(*,
189+
seed: Hash32,
190+
validators: Sequence['ValidatorRecord'],
191+
crosslinking_start_shard: ShardNumber,
192+
slot: SlotNumber,
193+
epoch_length: int,
194+
target_committee_size: int,
195+
shard_count: int) -> Iterable[Iterable[ShardCommittee]]:
199196
"""
200197
Return shuffled ``shard_committee_for_slots`` (``[[ShardCommittee]]``) of
201198
the given active ``validators`` using ``seed`` as entropy.
@@ -238,7 +235,7 @@ def get_new_shuffling(*,
238235
],
239236
]
240237
"""
241-
active_validators = get_active_validator_indices(validators)
238+
active_validators = get_active_validator_indices(validators, slot)
242239
active_validators_size = len(active_validators)
243240
committees_per_slot = clamp(
244241
1,
@@ -393,22 +390,6 @@ def get_effective_balance(
393390
return min(validator_balances[index], max_deposit * denoms.gwei)
394391

395392

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
410-
411-
412393
def get_fork_version(fork_data: 'ForkData',
413394
slot: SlotNumber) -> int:
414395
"""

eth/beacon/state_machines/configs.py

Lines changed: 9 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,19 @@
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+
('FAR_FUTURE_SLOT', SlotNumber),
40+
('BLS_WITHDRAWAL_PREFIX_BYTE', bytes),
3941
# Time parameters
4042
('SLOT_DURATION', Second),
4143
('MIN_ATTESTATION_INCLUSION_DELAY', int),
4244
('EPOCH_LENGTH', int),
45+
('MIN_VALIDATOR_REGISTRY_CHANGE_INTERVAL', int),
46+
('SEED_LOOKAHEAD', int),
47+
('ENTRY_EXIT_DELAY', int),
4348
('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),
49+
('MIN_VALIDATOR_WITHDRAWAL_TIME', int),
4750
# Reward and penalty quotients
4851
('BASE_REWARD_QUOTIENT', int),
4952
('WHISTLEBLOWER_REWARD_QUOTIENT', int),

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,29 @@
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+
FAR_FUTURE_SLOT=SlotNumber(2**63),
33+
BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
3234
# Time parameters
3335
SLOT_DURATION=Second(6), # seconds
3436
MIN_ATTESTATION_INCLUSION_DELAY=2**2, # (= 4) slots
3537
EPOCH_LENGTH=2**6, # (= 64) slots
38+
MIN_VALIDATOR_REGISTRY_CHANGE_INTERVAL=2**8, # (= 256) slots
39+
SEED_LOOKAHEAD=2**6, # (= 64) slots
40+
ENTRY_EXIT_DELAY=2**8, # (= 256) slots
3641
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
42+
MIN_VALIDATOR_WITHDRAWAL_TIME=2**14, # (= 16,384) slots
4043
# Reward and penalty quotients
4144
BASE_REWARD_QUOTIENT=2**10, # (= 1,024)
4245
WHISTLEBLOWER_REWARD_QUOTIENT=2**9, # (= 512)

eth/beacon/types/deposit_data.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ class DepositData(rlp.Serializable):
1414
"""
1515
fields = [
1616
('deposit_input', DepositInput),
17-
# Value in Gwei
18-
('value', uint64),
17+
# Amount in Gwei
18+
('amount', uint64),
1919
# Timestamp from deposit contract
2020
('timestamp', uint64),
2121
]
2222

2323
def __init__(self,
2424
deposit_input: DepositInput,
25-
value: Gwei,
25+
amount: Gwei,
2626
timestamp: Timestamp) -> None:
2727

2828
super().__init__(
2929
deposit_input,
30-
value,
30+
amount,
3131
timestamp,
3232
)

0 commit comments

Comments
 (0)