From cba7498504751ba5625c87005bd56b34302fd7ac Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Wed, 9 Jan 2019 23:06:38 +0800 Subject: [PATCH] PR feedback 1. Move `FAR_FUTURE_SLOT` to `eth.beacon.constants` 2. Rename `get_pending_validator` to `create_pending_validator` 3. Add some docstrings --- eth/beacon/constants.py | 7 +++++- eth/beacon/deposit_helpers.py | 7 ++---- eth/beacon/state_machines/configs.py | 1 - .../state_machines/forks/serenity/configs.py | 1 - eth/beacon/types/states.py | 6 +++++ eth/beacon/types/validator_records.py | 25 ++++++++++--------- tests/beacon/conftest.py | 23 ++++++----------- tests/beacon/helpers.py | 10 ++++---- tests/beacon/state_machines/conftest.py | 2 -- .../test_proposer_signature_validation.py | 5 ++-- tests/beacon/test_deposit_helpers.py | 5 +--- tests/beacon/test_helpers.py | 8 +++--- tests/beacon/types/test_states.py | 13 ++++------ tests/beacon/types/test_validator_record.py | 16 ++++++------ 14 files changed, 62 insertions(+), 67 deletions(-) diff --git a/eth/beacon/constants.py b/eth/beacon/constants.py index 8f6ab408b5..81e6479037 100644 --- a/eth/beacon/constants.py +++ b/eth/beacon/constants.py @@ -1,4 +1,8 @@ -from eth.beacon.typing import BLSSignature +from eth.beacon.typing import ( + BLSSignature, + SlotNumber, +) + # # shuffle function @@ -15,3 +19,4 @@ EMPTY_SIGNATURE = BLSSignature((0, 0)) GWEI_PER_ETH = 10**9 +FAR_FUTURE_SLOT = SlotNumber(2**64 - 1) diff --git a/eth/beacon/deposit_helpers.py b/eth/beacon/deposit_helpers.py index 4921c000f0..cd1fb93165 100644 --- a/eth/beacon/deposit_helpers.py +++ b/eth/beacon/deposit_helpers.py @@ -20,7 +20,6 @@ get_domain, ) from eth.beacon.typing import ( - SlotNumber, BLSPubkey, BLSSignature, ValidatorIndex, @@ -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. """ @@ -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 diff --git a/eth/beacon/state_machines/configs.py b/eth/beacon/state_machines/configs.py index a82af255b0..edd7ae87ba 100644 --- a/eth/beacon/state_machines/configs.py +++ b/eth/beacon/state_machines/configs.py @@ -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), diff --git a/eth/beacon/state_machines/forks/serenity/configs.py b/eth/beacon/state_machines/forks/serenity/configs.py index e4f72acbb7..18243aa2ec 100644 --- a/eth/beacon/state_machines/forks/serenity/configs.py +++ b/eth/beacon/state_machines/forks/serenity/configs.py @@ -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 diff --git a/eth/beacon/types/states.py b/eth/beacon/types/states.py index 244308880b..7319000e16 100644 --- a/eth/beacon/types/states.py +++ b/eth/beacon/types/states.py @@ -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") @@ -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 diff --git a/eth/beacon/types/validator_records.py b/eth/beacon/types/validator_records.py index 4fa1ede56d..b9319f7609 100644 --- a/eth/beacon/types/validator_records.py +++ b/eth/beacon/types/validator_records.py @@ -8,6 +8,9 @@ uint384, hash32, ) +from eth.beacon.constants import ( + FAR_FUTURE_SLOT, +) from eth.beacon.typing import ( SlotNumber, BLSPubkey, @@ -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. """ @@ -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, diff --git a/tests/beacon/conftest.py b/tests/beacon/conftest.py index 90687695e3..66eefa94d6 100644 --- a/tests/beacon/conftest.py +++ b/tests/beacon/conftest.py @@ -17,6 +17,7 @@ aggregate_votes, ) from eth.beacon.constants import ( + FAR_FUTURE_SLOT, GWEI_PER_ETH, ) from eth.beacon.enums import ( @@ -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, @@ -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) @@ -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 @@ -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, diff --git a/tests/beacon/helpers.py b/tests/beacon/helpers.py index e1a9cea7be..3a8b9195b8 100644 --- a/tests/beacon/helpers.py +++ b/tests/beacon/helpers.py @@ -7,6 +7,7 @@ ) from eth.beacon.constants import ( EMPTY_SIGNATURE, + FAR_FUTURE_SLOT, ) from eth.beacon.enums import ( SignatureDomain, @@ -21,7 +22,6 @@ def mock_validator_record(pubkey, - far_future_slot, withdrawal_credentials=ZERO_HASH32, randao_commitment=ZERO_HASH32, status_flags=0, @@ -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, diff --git a/tests/beacon/state_machines/conftest.py b/tests/beacon/state_machines/conftest.py index 93f68d8327..50324a5d3b 100644 --- a/tests/beacon/state_machines/conftest.py +++ b/tests/beacon/state_machines/conftest.py @@ -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, @@ -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, diff --git a/tests/beacon/state_machines/test_proposer_signature_validation.py b/tests/beacon/state_machines/test_proposer_signature_validation.py index dad5f5cd1a..3a162106ce 100644 --- a/tests/beacon/state_machines/test_proposer_signature_validation.py +++ b/tests/beacon/state_machines/test_proposer_signature_validation.py @@ -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, diff --git a/tests/beacon/test_deposit_helpers.py b/tests/beacon/test_deposit_helpers.py index a4a7161a41..e7cf19435e 100644 --- a/tests/beacon/test_deposit_helpers.py +++ b/tests/beacon/test_deposit_helpers.py @@ -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=(), @@ -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 @@ -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 diff --git a/tests/beacon/test_helpers.py b/tests/beacon/test_helpers.py index 11840f3f3d..e286a0335f 100644 --- a/tests/beacon/test_helpers.py +++ b/tests/beacon/test_helpers.py @@ -1,7 +1,8 @@ import copy +import random + import itertools import pytest -import random from hypothesis import ( given, @@ -14,6 +15,7 @@ from eth.beacon.constants import ( GWEI_PER_ETH, + FAR_FUTURE_SLOT, ) from eth.beacon.enums import ( SignatureDomain, @@ -483,7 +485,7 @@ 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 = [ @@ -491,7 +493,7 @@ def test_get_active_validator_indices(sample_validator_record_params, far_future **sample_validator_record_params, ).copy( activation_slot=0, - exit_slot=far_future_slot, + exit_slot=FAR_FUTURE_SLOT, ) for i in range(3) ] diff --git a/tests/beacon/types/test_states.py b/tests/beacon/types/test_states.py index fd1875058f..d1e1de537f 100644 --- a/tests/beacon/types/test_states.py +++ b/tests/beacon/types/test_states.py @@ -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) ), ) @@ -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) ), @@ -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( diff --git a/tests/beacon/types/test_validator_record.py b/tests/beacon/types/test_validator_record.py index 1dbe06819a..e9712ff985 100644 --- a/tests/beacon/types/test_validator_record.py +++ b/tests/beacon/types/test_validator_record.py @@ -1,5 +1,8 @@ import pytest +from eth.beacon.constants import ( + FAR_FUTURE_SLOT, +) from eth.beacon.types.validator_records import ( ValidatorRecord, ) @@ -34,26 +37,25 @@ def test_is_active(sample_validator_record_params, assert validator.is_active(slot) == expected -def test_get_pending_validator(far_future_slot): +def test_create_pending_validator(): pubkey = 123 withdrawal_credentials = b'\x11' * 32 randao_commitment = b'\x22' * 32 custody_commitment = b'\x33' * 32 - 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, ) assert validator.pubkey == pubkey assert validator.withdrawal_credentials == withdrawal_credentials assert validator.randao_commitment == randao_commitment assert validator.randao_layers == 0 - assert validator.activation_slot == far_future_slot - assert validator.exit_slot == far_future_slot - assert validator.withdrawal_slot == far_future_slot - assert validator.penalized_slot == far_future_slot + assert validator.activation_slot == FAR_FUTURE_SLOT + assert validator.exit_slot == FAR_FUTURE_SLOT + assert validator.withdrawal_slot == FAR_FUTURE_SLOT + assert validator.penalized_slot == FAR_FUTURE_SLOT assert validator.exit_count == 0