From 8f45a0d94e917d40264d3bde1c0360728bffb334 Mon Sep 17 00:00:00 2001 From: Chih Cheng Liang Date: Mon, 8 Jul 2019 19:08:58 +0800 Subject: [PATCH] fix get_ready_attestations and tests remove attestation.slot --- .../eth2/beacon/test_receive_server.py | 45 ++++++++++++------- tests/plugins/eth2/beacon/test_validator.py | 4 +- trinity/plugins/eth2/beacon/validator.py | 2 +- trinity/protocol/bcc/servers.py | 2 +- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/tests/plugins/eth2/beacon/test_receive_server.py b/tests/plugins/eth2/beacon/test_receive_server.py index ba07fe5e97..4821a7618a 100644 --- a/tests/plugins/eth2/beacon/test_receive_server.py +++ b/tests/plugins/eth2/beacon/test_receive_server.py @@ -30,6 +30,7 @@ from eth2.beacon.chains.testnet import TestnetChain as _TestnetChain from eth2.beacon.fork_choice import higher_slot_scoring from eth2.beacon.types.attestations import Attestation +from eth2.beacon.types.attestation_data import AttestationData from eth2.beacon.types.blocks import ( BaseBeaconBlock, ) @@ -592,43 +593,53 @@ def test_attestation_pool(): assert len(pool._pool) == 0 -@pytest.mark.xfail @pytest.mark.asyncio async def test_bcc_receive_server_get_ready_attestations( request, event_loop, - event_bus): + event_bus, + mocker): async with get_peer_and_receive_server( request, event_loop, event_bus, ) as (alice, _, bob_recv_server, _): + class MockState: + slot = XIAO_LONG_BAO_CONFIG.GENESIS_SLOT + state = MockState() + + def mock_get_attestation_data_slot(state, data, config): + return data.slot + mocker.patch("eth2.beacon.state_machines.base.BeaconStateMachine.state", state) + mocker.patch( + "trinity.protocol.bcc.servers.get_attestation_data_slot", + mock_get_attestation_data_slot, + ) attesting_slot = XIAO_LONG_BAO_CONFIG.GENESIS_SLOT - a1 = Attestation() - a2 = a1 # TODO: Make it same attesting slot with a1 - a3 = a1 # TODO: Make it a1's attesting slot + 1 + a1 = Attestation(data=AttestationData()) + a1.data.slot = attesting_slot + a2 = Attestation(signature=b'\x56' * 96, data=AttestationData()) + a2.data.slot = attesting_slot + a3 = Attestation(signature=b'\x78' * 96, data=AttestationData()) + a3.data.slot = attesting_slot + 1 bob_recv_server.attestation_pool.batch_add([a1, a2, a3]) # Workaround: add a fake head state slot # so `get_state_machine` wont's trigger `HeadStateSlotNotFound` exception bob_recv_server.chain.chaindb._add_head_state_slot_lookup(XIAO_LONG_BAO_CONFIG.GENESIS_SLOT) - ready_attestations = bob_recv_server.get_ready_attestations( - attesting_slot + XIAO_LONG_BAO_CONFIG.MIN_ATTESTATION_INCLUSION_DELAY - 1, - ) + state.slot = attesting_slot + XIAO_LONG_BAO_CONFIG.MIN_ATTESTATION_INCLUSION_DELAY - 1 + ready_attestations = bob_recv_server.get_ready_attestations() assert len(ready_attestations) == 0 - ready_attestations = bob_recv_server.get_ready_attestations( - attesting_slot + XIAO_LONG_BAO_CONFIG.MIN_ATTESTATION_INCLUSION_DELAY, - ) + state.slot = attesting_slot + XIAO_LONG_BAO_CONFIG.MIN_ATTESTATION_INCLUSION_DELAY + ready_attestations = bob_recv_server.get_ready_attestations() assert set([a1, a2]) == set(ready_attestations) - ready_attestations = bob_recv_server.get_ready_attestations( - attesting_slot + XIAO_LONG_BAO_CONFIG.MIN_ATTESTATION_INCLUSION_DELAY + 1, - ) + state.slot = attesting_slot + XIAO_LONG_BAO_CONFIG.MIN_ATTESTATION_INCLUSION_DELAY + 1 + ready_attestations = bob_recv_server.get_ready_attestations() assert set([a1, a2, a3]) == set(ready_attestations) - ready_attestations = bob_recv_server.get_ready_attestations( - attesting_slot + XIAO_LONG_BAO_CONFIG.SLOTS_PER_EPOCH + 1, - ) + state.slot = attesting_slot + XIAO_LONG_BAO_CONFIG.SLOTS_PER_EPOCH + 1 + ready_attestations = bob_recv_server.get_ready_attestations() assert set([a3]) == set(ready_attestations) diff --git a/tests/plugins/eth2/beacon/test_validator.py b/tests/plugins/eth2/beacon/test_validator.py index b3065f1c60..c40bec0a5e 100644 --- a/tests/plugins/eth2/beacon/test_validator.py +++ b/tests/plugins/eth2/beacon/test_validator.py @@ -110,7 +110,7 @@ async def get_validator(event_loop, event_bus, indices) -> Validator: for index in indices } - def get_ready_attestations_fn(slot): + def get_ready_attestations_fn(): return () v = Validator( @@ -408,7 +408,7 @@ async def test_validator_include_ready_attestations(event_loop, event_bus, monke # Mock `get_ready_attestations_fn` so it returns the attestation alice # attested to. - def get_ready_attestations_fn(slog): + def get_ready_attestations_fn(): return attestations monkeypatch.setattr(alice, 'get_ready_attestations', get_ready_attestations_fn) diff --git a/trinity/plugins/eth2/beacon/validator.py b/trinity/plugins/eth2/beacon/validator.py index 4bf2e01ac2..9fa0921ed2 100644 --- a/trinity/plugins/eth2/beacon/validator.py +++ b/trinity/plugins/eth2/beacon/validator.py @@ -234,7 +234,7 @@ def propose_block(self, state: BeaconState, state_machine: BaseBeaconStateMachine, head_block: BaseBeaconBlock) -> BaseBeaconBlock: - ready_attestations = self.get_ready_attestations(slot) + ready_attestations = self.get_ready_attestations() block = self._make_proposing_block( proposer_index=proposer_index, slot=slot, diff --git a/trinity/protocol/bcc/servers.py b/trinity/protocol/bcc/servers.py index c17faccc29..45455debb5 100644 --- a/trinity/protocol/bcc/servers.py +++ b/trinity/protocol/bcc/servers.py @@ -573,7 +573,7 @@ def _is_block_seen(self, block: BaseBeaconBlock) -> bool: return self._is_block_root_seen(block_root=block.signing_root) @to_tuple - def get_ready_attestations(self, inclusion_slot: Slot) -> Iterable[Attestation]: + def get_ready_attestations(self) -> Iterable[Attestation]: state_machine = self.chain.get_state_machine() config = state_machine.config state = state_machine.state