From 148e2c6eb7a69cd6e147f83b3822d378858309a1 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 17 Feb 2019 17:55:56 +0800 Subject: [PATCH] Add BeaconState.deposit_index (ethereum/eth2.0-specs#594) --- eth2/beacon/on_startup.py | 1 + eth2/beacon/types/states.py | 6 +++++- tests/eth2/beacon/conftest.py | 1 + tests/eth2/beacon/test_on_startup.py | 3 ++- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/eth2/beacon/on_startup.py b/eth2/beacon/on_startup.py index ebb1c624d8..aa112f681a 100644 --- a/eth2/beacon/on_startup.py +++ b/eth2/beacon/on_startup.py @@ -119,6 +119,7 @@ def get_genesis_beacon_state(*, # Ethereum 1.0 chain data latest_eth1_data=latest_eth1_data, eth1_data_votes=(), + deposit_index=len(genesis_validator_deposits), ) # Process initial deposits diff --git a/eth2/beacon/types/states.py b/eth2/beacon/types/states.py index a32c2ae6e7..9162c40f7a 100644 --- a/eth2/beacon/types/states.py +++ b/eth2/beacon/types/states.py @@ -87,6 +87,7 @@ class BeaconState(rlp.Serializable): # Ethereum 1.0 chain ('latest_eth1_data', Eth1Data), ('eth1_data_votes', CountableList(Eth1DataVote)), + ('deposit_index', uint64), ] def __init__( @@ -122,7 +123,8 @@ def __init__( latest_attestations: Sequence[PendingAttestationRecord], # Ethereum 1.0 chain latest_eth1_data: Eth1Data, - eth1_data_votes: Sequence[Eth1DataVote]) -> None: + eth1_data_votes: Sequence[Eth1DataVote], + deposit_index: int) -> None: if len(validator_registry) != len(validator_balances): raise ValueError( "The length of validator_registry and validator_balances should be the same." @@ -159,6 +161,7 @@ def __init__( # Ethereum 1.0 chain latest_eth1_data=latest_eth1_data, eth1_data_votes=eth1_data_votes, + deposit_index=deposit_index, ) def __repr__(self) -> str: @@ -244,6 +247,7 @@ def create_filled_state(cls, # Ethereum 1.0 chain data latest_eth1_data=Eth1Data.create_empty_data(), eth1_data_votes=(), + deposit_index=len(activated_genesis_validators), ) def update_validator_registry(self, diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index 3f9fa3d782..9f379adfd8 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -169,6 +169,7 @@ def sample_beacon_state_params(sample_fork_params, sample_eth1_data_params): 'batched_block_roots': (), 'latest_eth1_data': Eth1Data(**sample_eth1_data_params), 'eth1_data_votes': (), + 'deposit_index': 0, } diff --git a/tests/eth2/beacon/test_on_startup.py b/tests/eth2/beacon/test_on_startup.py index 7984313a2a..90b8fcc73e 100644 --- a/tests/eth2/beacon/test_on_startup.py +++ b/tests/eth2/beacon/test_on_startup.py @@ -76,7 +76,7 @@ def test_get_genesis_beacon_state( validator_count = 5 - genesis_validator_deposits = ( + genesis_validator_deposits = tuple( Deposit( branch=( b'\x11' * 32 @@ -171,5 +171,6 @@ def test_get_genesis_beacon_state( # Ethereum 1.0 chain data assert state.latest_eth1_data == latest_eth1_data assert len(state.eth1_data_votes) == 0 + assert state.deposit_index == len(genesis_validator_deposits) assert state.validator_registry[0].is_active(genesis_epoch)