From eb7466c7a8d1c5480acbadef68562c7c1adc0781 Mon Sep 17 00:00:00 2001 From: Jannik Luhn Date: Sat, 2 Feb 2019 11:44:17 +0100 Subject: [PATCH 1/2] Implement eth1 data processing --- .../forks/serenity/block_processing.py | 39 ++++++++++++++++ .../forks/test_serenity_block_processing.py | 45 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 eth2/beacon/state_machines/forks/serenity/block_processing.py create mode 100644 tests/eth2/beacon/state_machines/forks/test_serenity_block_processing.py diff --git a/eth2/beacon/state_machines/forks/serenity/block_processing.py b/eth2/beacon/state_machines/forks/serenity/block_processing.py new file mode 100644 index 0000000000..e4960db7cb --- /dev/null +++ b/eth2/beacon/state_machines/forks/serenity/block_processing.py @@ -0,0 +1,39 @@ +from eth_utils.toolz import ( + first, +) + +from eth2._utils.tuple import update_tuple_item + +from eth2.beacon.types.states import BeaconState +from eth2.beacon.types.blocks import BaseBeaconBlock +from eth2.beacon.types.eth1_data_vote import Eth1DataVote + +from eth2.beacon.state_machines.configs import BeaconConfig + + +def process_eth1_data(state: BeaconState, + block: BaseBeaconBlock, + config: BeaconConfig) -> BeaconState: + try: + vote_index, original_vote = first( + (index, eth1_data_vote) + for index, eth1_data_vote in enumerate(state.eth1_data_votes) + if block.eth1_data == eth1_data_vote.eth1_data + ) + except StopIteration: + new_vote = Eth1DataVote( + eth1_data=block.eth1_data, + vote_count=1, + ) + state = state.copy( + eth1_data_votes=state.eth1_data_votes + (new_vote,) + ) + else: + updated_vote = original_vote.copy( + vote_count=original_vote.vote_count + 1 + ) + state = state.copy( + eth1_data_votes=update_tuple_item(state.eth1_data_votes, vote_index, updated_vote) + ) + + return state diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_block_processing.py b/tests/eth2/beacon/state_machines/forks/test_serenity_block_processing.py new file mode 100644 index 0000000000..89c2a5da6b --- /dev/null +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_block_processing.py @@ -0,0 +1,45 @@ +import pytest + +from eth2.beacon.types.eth1_data_vote import Eth1DataVote +from eth2.beacon.types.states import BeaconState +from eth2.beacon.types.blocks import BeaconBlock + +from eth2.beacon.state_machines.forks.serenity.block_processing import ( + process_eth1_data, +) + +HASH1 = b"\x11" * 32 +HASH2 = b"\x22" * 32 + + +@pytest.mark.parametrize(("original_votes", "block_data", "expected_votes"), ( + ((), HASH1, ((HASH1, 1),)), + (((HASH1, 5),), HASH1, ((HASH1, 6),)), + (((HASH2, 5),), HASH1, ((HASH2, 5), (HASH1, 1))), + (((HASH1, 10), (HASH2, 2)), HASH2, ((HASH1, 10), (HASH2, 3))), +)) +def test_process_eth1_data(original_votes, + block_data, + expected_votes, + sample_beacon_state_params, + sample_beacon_block_params, + config): + eth1_data_votes = tuple( + Eth1DataVote(data, vote_count) + for data, vote_count in original_votes + ) + state = BeaconState(**sample_beacon_state_params).copy( + eth1_data_votes=eth1_data_votes, + ) + + block = BeaconBlock(**sample_beacon_block_params).copy( + eth1_data=block_data, + ) + + updated_state = process_eth1_data(state, block, config) + updated_votes = tuple( + (vote.eth1_data, vote.vote_count) + for vote in updated_state.eth1_data_votes + ) + assert updated_votes == expected_votes + From 9e576b8b29b100f499e2dcfd8ca5745ab96ba006 Mon Sep 17 00:00:00 2001 From: Jannik Luhn Date: Sat, 2 Feb 2019 12:30:39 +0100 Subject: [PATCH 2/2] flake8 --- .../state_machines/forks/test_serenity_block_processing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_block_processing.py b/tests/eth2/beacon/state_machines/forks/test_serenity_block_processing.py index 89c2a5da6b..8c680f5bae 100644 --- a/tests/eth2/beacon/state_machines/forks/test_serenity_block_processing.py +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_block_processing.py @@ -42,4 +42,3 @@ def test_process_eth1_data(original_votes, for vote in updated_state.eth1_data_votes ) assert updated_votes == expected_votes -