Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
fix get_ready_attestations and tests
Browse files Browse the repository at this point in the history
remove attestation.slot
  • Loading branch information
ChihChengLiang committed Jul 9, 2019
1 parent c4019c2 commit 6400927
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
45 changes: 28 additions & 17 deletions tests/plugins/eth2/beacon/test_receive_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tests/plugins/eth2/beacon/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion trinity/plugins/eth2/beacon/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion trinity/protocol/bcc/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6400927

Please sign in to comment.