Skip to content

Commit

Permalink
Sync ethereum/consensus-specs#374 part1: types, constants, deposit_he…
Browse files Browse the repository at this point in the history
…lpers
  • Loading branch information
hwwhww committed Jan 9, 2019
1 parent ee8f72d commit 43aec53
Show file tree
Hide file tree
Showing 20 changed files with 522 additions and 539 deletions.
81 changes: 24 additions & 57 deletions eth/beacon/deposit_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import (
Sequence,
Tuple,
)

Expand All @@ -18,9 +17,6 @@
from eth.beacon.enums import (
SignatureDomain,
)
from eth.beacon.exceptions import (
MinEmptyValidatorIndexNotFound,
)
from eth.beacon.types.deposit_input import DepositInput
from eth.beacon.types.states import BeaconState
from eth.beacon.types.validator_records import ValidatorRecord
Expand All @@ -36,20 +32,6 @@
)


def get_min_empty_validator_index(validators: Sequence[ValidatorRecord],
validator_balances: Sequence[Gwei],
current_slot: SlotNumber,
zero_balance_validator_ttl: int) -> ValidatorIndex:
for index, (validator, balance) in enumerate(zip(validators, validator_balances)):
is_empty = (
balance == 0 and
validator.latest_status_change_slot + zero_balance_validator_ttl <= current_slot
)
if is_empty:
return ValidatorIndex(index)
raise MinEmptyValidatorIndexNotFound()


def validate_proof_of_possession(state: BeaconState,
pubkey: BLSPubkey,
proof_of_possession: BLSSignature,
Expand Down Expand Up @@ -84,55 +66,40 @@ def validate_proof_of_possession(state: BeaconState,

def add_pending_validator(state: BeaconState,
validator: ValidatorRecord,
deposit: Gwei,
zero_balance_validator_ttl: int) -> Tuple[BeaconState, ValidatorIndex]:
amount: Gwei) -> Tuple[BeaconState, int]:
"""
Add a validator to the existing minimum empty validator index or
append to ``validator_registry``.
Add a validator to ``state``.
"""
# Check if there's empty validator index in `validator_registry`
try:
index = get_min_empty_validator_index(
state.validator_registry,
state.validator_balances,
state.slot,
zero_balance_validator_ttl,
)
except MinEmptyValidatorIndexNotFound:
index = None
# Append to the validator_registry
validator_registry = state.validator_registry + (validator,)
state = state.copy(
validator_registry=validator_registry,
validator_balances=state.validator_balances + (deposit, )
)
index = ValidatorIndex(len(state.validator_registry) - 1)
else:
# Use the empty validator index
state = state.update_validator(index, validator, deposit)
validator_registry = state.validator_registry + (validator,)
state = state.copy(
validator_registry=validator_registry,
validator_balances=state.validator_balances + (amount, )
)

index = len(state.validator_registry) - 1

return state, index


def process_deposit(*,
state: BeaconState,
pubkey: BLSPubkey,
deposit: Gwei,
amount: Gwei,
proof_of_possession: BLSSignature,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
custody_commitment: Hash32,
zero_balance_validator_ttl: int) -> Tuple[BeaconState, ValidatorIndex]:
far_future_slot: SlotNumber) -> BeaconState:
"""
Process a deposit from Ethereum 1.0.
"""
validate_proof_of_possession(
state,
pubkey,
proof_of_possession,
withdrawal_credentials,
randao_commitment,
custody_commitment,
state=state,
pubkey=pubkey,
proof_of_possession=proof_of_possession,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
)

validator_pubkeys = tuple(v.pubkey for v in state.validator_registry)
Expand All @@ -141,15 +108,16 @@ def process_deposit(*,
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
latest_status_change_slot=state.slot,
custody_commitment=custody_commitment,
far_future_slot=far_future_slot,
)

# Note: In phase 2 registry indices that has been withdrawn for a long time
# will be recycled.
state, index = add_pending_validator(
state,
validator,
deposit,
zero_balance_validator_ttl,
amount,
)
else:
# Top-up - increase balance by deposit
Expand All @@ -166,10 +134,9 @@ def process_deposit(*,
)

# Update validator's balance and state
state = state.update_validator(
state = state.update_validator_balance(
validator_index=index,
validator=validator,
balance=state.validator_balances[index] + deposit,
balance=state.validator_balances[index] + amount,
)

return state, index
return state
9 changes: 3 additions & 6 deletions eth/beacon/enums.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from enum import IntEnum


class ValidatorStatusCode(IntEnum):
PENDING_ACTIVATION = 0
ACTIVE = 1
ACTIVE_PENDING_EXIT = 2
EXITED_WITHOUT_PENALTY = 3
EXITED_WITH_PENALTY = 4
class ValidatorStatusFlags(IntEnum):
INITIATED_EXIT = 1
WITHDRAWABLE = 2


class ValidatorRegistryDeltaFlag(IntEnum):
Expand Down
10 changes: 0 additions & 10 deletions eth/beacon/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
from eth.exceptions import (
PyEVMError,
)


class MinEmptyValidatorIndexNotFound(PyEVMError):
"""
No empty slot in the validator registry
"""
pass
45 changes: 13 additions & 32 deletions eth/beacon/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@
clamp,
)

from eth.beacon.types.validator_registry_delta_block import (
ValidatorRegistryDeltaBlock,
)
from eth.beacon.block_committees_info import (
BlockCommitteesInfo,
)
from eth.beacon.enums import (
SignatureDomain,
ValidatorRegistryDeltaFlag,
)
from eth.beacon.types.shard_committees import (
ShardCommittee,
Expand Down Expand Up @@ -157,14 +153,14 @@ def get_shard_committees_at_slot(state: 'BeaconState',
)


def get_active_validator_indices(
validators: Sequence['ValidatorRecord']) -> Tuple[ValidatorIndex, ...]:
def get_active_validator_indices(validators: Sequence['ValidatorRecord'],
slot: SlotNumber) -> Tuple[int, ...]:
"""
Get indices of active validators from ``validators``.
"""
return tuple(
ValidatorIndex(i) for i, v in enumerate(validators)
if v.is_active
i for i, v in enumerate(validators)
if v.is_active(slot)
)


Expand All @@ -189,13 +185,14 @@ def _get_shards_committees_for_shard_indices(


@to_tuple
def get_new_shuffling(*,
seed: Hash32,
validators: Sequence['ValidatorRecord'],
crosslinking_start_shard: ShardNumber,
epoch_length: int,
target_committee_size: int,
shard_count: int) -> Iterable[Iterable[ShardCommittee]]:
def get_shuffling(*,
seed: Hash32,
validators: Sequence['ValidatorRecord'],
crosslinking_start_shard: ShardNumber,
slot: SlotNumber,
epoch_length: int,
target_committee_size: int,
shard_count: int) -> Iterable[Iterable[ShardCommittee]]:
"""
Return shuffled ``shard_committee_for_slots`` (``[[ShardCommittee]]``) of
the given active ``validators`` using ``seed`` as entropy.
Expand Down Expand Up @@ -238,7 +235,7 @@ def get_new_shuffling(*,
],
]
"""
active_validators = get_active_validator_indices(validators)
active_validators = get_active_validator_indices(validators, slot)
active_validators_size = len(active_validators)
committees_per_slot = clamp(
1,
Expand Down Expand Up @@ -393,22 +390,6 @@ def get_effective_balance(
return min(validator_balances[index], max_deposit * denoms.gwei)


def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_chain_tip: Hash32,
validator_index: ValidatorIndex,
pubkey: BLSPubkey,
flag: ValidatorRegistryDeltaFlag) -> Hash32:
"""
Compute the next hash in the validator registry delta hash chain.
"""
# TODO: switch to SSZ tree hashing
return ValidatorRegistryDeltaBlock(
latest_registry_delta_root=current_validator_registry_delta_chain_tip,
validator_index=validator_index,
pubkey=pubkey,
flag=flag,
).root


def get_fork_version(fork_data: 'ForkData',
slot: SlotNumber) -> int:
"""
Expand Down
15 changes: 9 additions & 6 deletions eth/beacon/state_machines/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
('EJECTION_BALANCE', Ether),
('MAX_BALANCE_CHURN_QUOTIENT', int),
('BEACON_CHAIN_SHARD_NUMBER', ShardNumber),
('BLS_WITHDRAWAL_PREFIX_BYTE', bytes),
('MAX_CASPER_VOTES', int),
('LATEST_BLOCK_ROOTS_LENGTH', int),
('LATEST_RANDAO_MIXES_LENGTH', int),
('LATEST_PENALIZED_EXIT_LENGTH', int),
# EMPTY_SIGNATURE is defined in constants.py
# Deposit contract
('DEPOSIT_CONTRACT_ADDRESS', Address),
Expand All @@ -34,16 +34,19 @@
('MAX_DEPOSIT', Ether),
# ZERO_HASH (ZERO_HASH32) is defined in constants.py
# Initial values
('INITIAL_FORK_VERSION', int),
('INITIAL_SLOT_NUMBER', SlotNumber),
('GENESIS_FORK_VERSION', int),
('GENESIS_SLOT', SlotNumber),
('FAR_FUTURE_SLOT', SlotNumber),
('BLS_WITHDRAWAL_PREFIX_BYTE', bytes),
# Time parameters
('SLOT_DURATION', Second),
('MIN_ATTESTATION_INCLUSION_DELAY', int),
('EPOCH_LENGTH', int),
('MIN_VALIDATOR_REGISTRY_CHANGE_INTERVAL', int),
('SEED_LOOKAHEAD', int),
('ENTRY_EXIT_DELAY', int),
('POW_RECEIPT_ROOT_VOTING_PERIOD', int),
('SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD', int),
('COLLECTIVE_PENALTY_CALCULATION_PERIOD', int),
('ZERO_BALANCE_VALIDATOR_TTL', int),
('MIN_VALIDATOR_WITHDRAWAL_TIME', int),
# Reward and penalty quotients
('BASE_REWARD_QUOTIENT', int),
('WHISTLEBLOWER_REWARD_QUOTIENT', int),
Expand Down
15 changes: 9 additions & 6 deletions eth/beacon/state_machines/forks/serenity/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@
EJECTION_BALANCE=Ether(2**4), # (= 16) ETH
MAX_BALANCE_CHURN_QUOTIENT=2**5, # (= 32)
BEACON_CHAIN_SHARD_NUMBER=ShardNumber(2**64 - 1),
BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
MAX_CASPER_VOTES=2**10, # (= 1,024) votes
LATEST_BLOCK_ROOTS_LENGTH=2**13, # (= 8,192) block roots
LATEST_RANDAO_MIXES_LENGTH=2**13, # (= 8,192) randao mixes
LATEST_PENALIZED_EXIT_LENGTH=2**13, # (= 8,192) randao mixes
# Deposit contract
DEPOSIT_CONTRACT_ADDRESS=ZERO_ADDRESS, # TBD
DEPOSIT_CONTRACT_TREE_DEPTH=2**5, # (= 32)
MIN_DEPOSIT=Ether(2**0), # (= 1) ETH
MAX_DEPOSIT=Ether(2**5), # (= 32) ETH
# Initial values
INITIAL_FORK_VERSION=0,
INITIAL_SLOT_NUMBER=SlotNumber(0),
GENESIS_FORK_VERSION=0,
GENESIS_SLOT=SlotNumber(0),
FAR_FUTURE_SLOT=SlotNumber(2**63),
BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
# Time parameters
SLOT_DURATION=Second(6), # seconds
MIN_ATTESTATION_INCLUSION_DELAY=2**2, # (= 4) slots
EPOCH_LENGTH=2**6, # (= 64) slots
MIN_VALIDATOR_REGISTRY_CHANGE_INTERVAL=2**8, # (= 256) slots
SEED_LOOKAHEAD=2**6, # (= 64) slots
ENTRY_EXIT_DELAY=2**8, # (= 256) slots
POW_RECEIPT_ROOT_VOTING_PERIOD=2**10, # (= 1,024) slots
SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD=2**17, # (= 131,072) slots
COLLECTIVE_PENALTY_CALCULATION_PERIOD=2**20, # (= 1,048,576) slots
ZERO_BALANCE_VALIDATOR_TTL=2**22, # (= 4,194,304) slots
MIN_VALIDATOR_WITHDRAWAL_TIME=2**14, # (= 16,384) slots
# Reward and penalty quotients
BASE_REWARD_QUOTIENT=2**10, # (= 1,024)
WHISTLEBLOWER_REWARD_QUOTIENT=2**9, # (= 512)
Expand Down
8 changes: 4 additions & 4 deletions eth/beacon/types/deposit_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ class DepositData(rlp.Serializable):
"""
fields = [
('deposit_input', DepositInput),
# Value in Gwei
('value', uint64),
# Amount in Gwei
('amount', uint64),
# Timestamp from deposit contract
('timestamp', uint64),
]

def __init__(self,
deposit_input: DepositInput,
value: Gwei,
amount: Gwei,
timestamp: Timestamp) -> None:

super().__init__(
deposit_input,
value,
amount,
timestamp,
)

0 comments on commit 43aec53

Please sign in to comment.