Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
1. Move `FAR_FUTURE_SLOT` to `eth.beacon.constants`
2. Rename `get_pending_validator` to `create_pending_validator`
3. Add some docstrings
  • Loading branch information
hwwhww committed Jan 9, 2019
1 parent 144bda1 commit cba7498
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 67 deletions.
7 changes: 6 additions & 1 deletion eth/beacon/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from eth.beacon.typing import BLSSignature
from eth.beacon.typing import (
BLSSignature,
SlotNumber,
)


#
# shuffle function
Expand All @@ -15,3 +19,4 @@

EMPTY_SIGNATURE = BLSSignature((0, 0))
GWEI_PER_ETH = 10**9
FAR_FUTURE_SLOT = SlotNumber(2**64 - 1)
7 changes: 2 additions & 5 deletions eth/beacon/deposit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
get_domain,
)
from eth.beacon.typing import (
SlotNumber,
BLSPubkey,
BLSSignature,
ValidatorIndex,
Expand Down Expand Up @@ -81,8 +80,7 @@ def process_deposit(*,
proof_of_possession: BLSSignature,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
custody_commitment: Hash32,
far_future_slot: SlotNumber) -> BeaconState:
custody_commitment: Hash32) -> BeaconState:
"""
Process a deposit from Ethereum 1.0.
"""
Expand All @@ -97,12 +95,11 @@ def process_deposit(*,

validator_pubkeys = tuple(v.pubkey for v in state.validator_registry)
if pubkey not in validator_pubkeys:
validator = ValidatorRecord.get_pending_validator(
validator = ValidatorRecord.create_pending_validator(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
far_future_slot=far_future_slot,
)

# Note: In phase 2 registry indices that has been withdrawn for a long time
Expand Down
1 change: 0 additions & 1 deletion eth/beacon/state_machines/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
# Initial values
('GENESIS_FORK_VERSION', int),
('GENESIS_SLOT', SlotNumber),
('FAR_FUTURE_SLOT', SlotNumber),
('BLS_WITHDRAWAL_PREFIX_BYTE', bytes),
# Time parameters
('SLOT_DURATION', Second),
Expand Down
1 change: 0 additions & 1 deletion eth/beacon/state_machines/forks/serenity/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
# Initial values
GENESIS_FORK_VERSION=0,
GENESIS_SLOT=SlotNumber(0),
FAR_FUTURE_SLOT=SlotNumber(2**64 - 1),
BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
# Time parameters
SLOT_DURATION=Second(6), # seconds
Expand Down
6 changes: 6 additions & 0 deletions eth/beacon/types/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ def update_validator_registry(self,
def update_validator_balance(self,
validator_index: ValidatorIndex,
balance: Gwei) -> 'BeaconState':
"""
Update the balance of validator of the given ``validator_index``.
"""
if validator_index >= self.num_validators or validator_index < 0:
raise IndexError("Incorrect validator index")

Expand All @@ -216,6 +219,9 @@ def update_validator(self,
validator_index: ValidatorIndex,
validator: ValidatorRecord,
balance: Gwei) -> 'BeaconState':
"""
Update the ``ValidatorRecord`` and balance of validator of the given ``validator_index``.
"""
state = self.update_validator_registry(validator_index, validator)
state = state.update_validator_balance(validator_index, balance)
return state
25 changes: 13 additions & 12 deletions eth/beacon/types/validator_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
uint384,
hash32,
)
from eth.beacon.constants import (
FAR_FUTURE_SLOT,
)
from eth.beacon.typing import (
SlotNumber,
BLSPubkey,
Expand Down Expand Up @@ -79,18 +82,16 @@ def __init__(self,

def is_active(self, slot: int) -> bool:
"""
Return ``True`` if the validator is active.Return ``True``
if the validator is active during the slot, ``slot``.
Return ``True`` if the validator is active during the slot, ``slot``.
"""
return self.activation_slot <= slot < self.exit_slot

@classmethod
def get_pending_validator(cls,
pubkey: BLSPubkey,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
custody_commitment: Hash32,
far_future_slot: SlotNumber) -> 'ValidatorRecord':
def create_pending_validator(cls,
pubkey: BLSPubkey,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
custody_commitment: Hash32) -> 'ValidatorRecord':
"""
Return a new pending ``ValidatorRecord`` with the given fields.
"""
Expand All @@ -99,10 +100,10 @@ def get_pending_validator(cls,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
randao_layers=0,
activation_slot=far_future_slot,
exit_slot=far_future_slot,
withdrawal_slot=far_future_slot,
penalized_slot=far_future_slot,
activation_slot=FAR_FUTURE_SLOT,
exit_slot=FAR_FUTURE_SLOT,
withdrawal_slot=FAR_FUTURE_SLOT,
penalized_slot=FAR_FUTURE_SLOT,
exit_count=0,
status_flags=0,
custody_commitment=custody_commitment,
Expand Down
23 changes: 8 additions & 15 deletions tests/beacon/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
aggregate_votes,
)
from eth.beacon.constants import (
FAR_FUTURE_SLOT,
GWEI_PER_ETH,
)
from eth.beacon.enums import (
Expand Down Expand Up @@ -306,16 +307,16 @@ def sample_casper_slashing_params(sample_slashable_vote_data_params):


@pytest.fixture
def sample_validator_record_params(far_future_slot):
def sample_validator_record_params():
return {
'pubkey': 123,
'withdrawal_credentials': b'\x01' * 32,
'randao_commitment': b'\x01' * 32,
'randao_layers': 1,
'activation_slot': far_future_slot,
'exit_slot': far_future_slot,
'withdrawal_slot': far_future_slot,
'penalized_slot': far_future_slot,
'activation_slot': FAR_FUTURE_SLOT,
'exit_slot': FAR_FUTURE_SLOT,
'withdrawal_slot': FAR_FUTURE_SLOT,
'penalized_slot': FAR_FUTURE_SLOT,
'exit_count': 0,
'status_flags': 0,
'custody_commitment': ZERO_HASH32,
Expand Down Expand Up @@ -371,13 +372,12 @@ def empty_beacon_state(latest_block_roots_length,


@pytest.fixture()
def ten_validators_state(empty_beacon_state, max_deposit, far_future_slot):
def ten_validators_state(empty_beacon_state, max_deposit):
validator_count = 10
return empty_beacon_state.copy(
validator_registry=tuple(
mock_validator_record(
pubkey=pubkey,
far_future_slot=far_future_slot,
is_active=True,
)
for pubkey in range(validator_count)
Expand Down Expand Up @@ -492,11 +492,6 @@ def genesis_slot():
return SERENITY_CONFIG.GENESIS_SLOT


@pytest.fixture
def far_future_slot():
return SERENITY_CONFIG.FAR_FUTURE_SLOT


@pytest.fixture
def bls_withdrawal_prefix_byte():
return SERENITY_CONFIG.BLS_WITHDRAWAL_PREFIX_BYTE
Expand Down Expand Up @@ -621,15 +616,13 @@ def genesis_state(sample_beacon_state_params,
@pytest.fixture
def initial_validators(init_validator_pubkeys,
init_randao,
max_deposit,
far_future_slot):
max_deposit):
"""
Inactive
"""
return tuple(
mock_validator_record(
pubkey=pubkey,
far_future_slot=far_future_slot,
withdrawal_credentials=ZERO_HASH32,
randao_commitment=init_randao,
status_flags=0,
Expand Down
10 changes: 5 additions & 5 deletions tests/beacon/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
from eth.beacon.constants import (
EMPTY_SIGNATURE,
FAR_FUTURE_SLOT,
)
from eth.beacon.enums import (
SignatureDomain,
Expand All @@ -21,7 +22,6 @@


def mock_validator_record(pubkey,
far_future_slot,
withdrawal_credentials=ZERO_HASH32,
randao_commitment=ZERO_HASH32,
status_flags=0,
Expand All @@ -31,10 +31,10 @@ def mock_validator_record(pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
randao_layers=0,
activation_slot=0 if is_active else far_future_slot,
exit_slot=far_future_slot,
withdrawal_slot=far_future_slot,
penalized_slot=far_future_slot,
activation_slot=0 if is_active else FAR_FUTURE_SLOT,
exit_slot=FAR_FUTURE_SLOT,
withdrawal_slot=FAR_FUTURE_SLOT,
penalized_slot=FAR_FUTURE_SLOT,
exit_count=0,
status_flags=status_flags,
custody_commitment=b'\x55' * 32,
Expand Down
2 changes: 0 additions & 2 deletions tests/beacon/state_machines/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def config(
max_deposit,
genesis_fork_version,
genesis_slot,
far_future_slot,
bls_withdrawal_prefix_byte,
slot_duration,
min_attestation_inclusion_delay,
Expand Down Expand Up @@ -58,7 +57,6 @@ def config(
MAX_DEPOSIT=max_deposit,
GENESIS_FORK_VERSION=genesis_fork_version,
GENESIS_SLOT=genesis_slot,
FAR_FUTURE_SLOT=far_future_slot,
BLS_WITHDRAWAL_PREFIX_BYTE=bls_withdrawal_prefix_byte,
SLOT_DURATION=slot_duration,
MIN_ATTESTATION_INCLUSION_DELAY=min_attestation_inclusion_delay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ def test_validate_serenity_proposer_signature(
sample_shard_committee_params,
beacon_chain_shard_number,
epoch_length,
max_deposit,
far_future_slot):
max_deposit):

state = BeaconState(**sample_beacon_state_params).copy(
validator_registry=tuple(
mock_validator_record(proposer_pubkey, far_future_slot)
mock_validator_record(proposer_pubkey)
for _ in range(10)
),
validator_balances=(max_deposit * GWEI_PER_ETH,) * 10,
Expand Down
5 changes: 1 addition & 4 deletions tests/beacon/test_deposit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ def test_validate_proof_of_possession(sample_beacon_state_params, pubkeys, privk
def test_process_deposit(sample_beacon_state_params,
privkeys,
pubkeys,
max_deposit,
far_future_slot):
max_deposit):
state = BeaconState(**sample_beacon_state_params).copy(
slot=1,
validator_registry=(),
Expand Down Expand Up @@ -133,7 +132,6 @@ def test_process_deposit(sample_beacon_state_params,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
far_future_slot=far_future_slot,
)

assert len(result_state.validator_registry) == 1
Expand Down Expand Up @@ -168,7 +166,6 @@ def test_process_deposit(sample_beacon_state_params,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
far_future_slot=far_future_slot,
)
assert len(result_state.validator_registry) == 2
assert result_state.validator_registry[1].pubkey == pubkey_2
8 changes: 5 additions & 3 deletions tests/beacon/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import copy
import random

import itertools
import pytest
import random

from hypothesis import (
given,
Expand All @@ -14,6 +15,7 @@

from eth.beacon.constants import (
GWEI_PER_ETH,
FAR_FUTURE_SLOT,
)
from eth.beacon.enums import (
SignatureDomain,
Expand Down Expand Up @@ -483,15 +485,15 @@ def mock_get_shard_committees_at_slot(state,
)


def test_get_active_validator_indices(sample_validator_record_params, far_future_slot):
def test_get_active_validator_indices(sample_validator_record_params):
current_slot = 1
# 3 validators are ACTIVE
validators = [
ValidatorRecord(
**sample_validator_record_params,
).copy(
activation_slot=0,
exit_slot=far_future_slot,
exit_slot=FAR_FUTURE_SLOT,
)
for i in range(3)
]
Expand Down
13 changes: 5 additions & 8 deletions tests/beacon/types/test_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def test_defaults(sample_beacon_state_params):
assert state.validator_registry_latest_change_slot == sample_beacon_state_params['validator_registry_latest_change_slot'] # noqa: E501


def test_validator_registry_and_balances_length(sample_beacon_state_params, far_future_slot):
def test_validator_registry_and_balances_length(sample_beacon_state_params):
# When len(BeaconState.validator_registry) != len(BeaconState.validtor_balances)
with pytest.raises(ValueError):
BeaconState(**sample_beacon_state_params).copy(
validator_registry=tuple(
mock_validator_record(pubkey, far_future_slot)
mock_validator_record(pubkey)
for pubkey in range(10)
),
)
Expand All @@ -42,13 +42,11 @@ def test_validator_registry_and_balances_length(sample_beacon_state_params, far_
)
def test_num_validators(expected,
max_deposit,
empty_beacon_state,
far_future_slot):
empty_beacon_state):
state = empty_beacon_state.copy(
validator_registry=tuple(
mock_validator_record(
pubkey,
far_future_slot,
)
for pubkey in range(expected)
),
Expand Down Expand Up @@ -90,10 +88,9 @@ def test_hash(sample_beacon_state_params):
def test_update_validator(ten_validators_state,
validator_index,
new_pubkey,
new_balance,
far_future_slot):
new_balance):
state = ten_validators_state
validator = mock_validator_record(new_pubkey, far_future_slot)
validator = mock_validator_record(new_pubkey)

if validator_index < state.num_validators:
result_state = state.update_validator(
Expand Down
Loading

0 comments on commit cba7498

Please sign in to comment.