Skip to content

Commit

Permalink
Fix attestation tools
Browse files Browse the repository at this point in the history
1. Fix `create_mock_signed_attestation`: Address ethereum/consensus-specs#671
2. Fix `test_demo.py`
	1. Use queued `attestations_map: Dict[Slot, Sequence[Attestation]]` to keep the generated attestations, and include them when reache `MIN_ATTESTATION_INCLUSION_DELAY`.
	2. Add justification assertions
  • Loading branch information
hwwhww committed Feb 22, 2019
1 parent 3e78837 commit 5f6849d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 35 deletions.
38 changes: 30 additions & 8 deletions eth2/beacon/tools/builder/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,44 @@ def create_mock_signed_attestations_at_slot(
state: BeaconState,
config: BeaconConfig,
attestation_slot: Slot,
beacon_block_root: Hash32,
keymap: Dict[BLSPubkey, int],
voted_attesters_ratio: float=1.0) -> Iterable[Attestation]:
"""
Create the mocking attestations of the given ``attestation_slot`` slot with ``keymap``.
"""
slots_per_epoch = config.SLOTS_PER_EPOCH

crosslink_committees_at_slot = get_crosslink_committees_at_slot(
# To avoid the epoch boundary cases
state.copy(
slot=state.slot + 1,
),
attestation_slot,
CommitteeConfig(config),
)

# Get `epoch_boundary_root`
epoch_start_slot = get_epoch_start_slot(
slot_to_epoch(state.slot, slots_per_epoch),
slots_per_epoch,
)
if epoch_start_slot == state.slot:
epoch_boundary_root = beacon_block_root
else:
epoch_boundary_root = get_block_root(
state,
epoch_start_slot,
config.LATEST_BLOCK_ROOTS_LENGTH,
)

# Get `justified_block_root`
justified_block_root = get_block_root(
state,
get_epoch_start_slot(state.justified_epoch, slots_per_epoch),
config.LATEST_BLOCK_ROOTS_LENGTH,
)

for crosslink_committee in crosslink_committees_at_slot:
committee, shard = crosslink_committee

Expand All @@ -320,16 +346,12 @@ def create_mock_signed_attestations_at_slot(
attestation_data = AttestationData(
slot=attestation_slot,
shard=shard,
beacon_block_root=ZERO_HASH32,
epoch_boundary_root=ZERO_HASH32,
beacon_block_root=beacon_block_root,
epoch_boundary_root=epoch_boundary_root,
shard_block_root=ZERO_HASH32,
latest_crosslink_root=latest_crosslink_root,
justified_epoch=state.previous_justified_epoch,
justified_block_root=get_block_root(
state,
get_epoch_start_slot(state.previous_justified_epoch, config.SLOTS_PER_EPOCH),
config.LATEST_BLOCK_ROOTS_LENGTH,
),
justified_epoch=state.justified_epoch,
justified_block_root=justified_block_root,
)

yield create_mock_signed_attestation(
Expand Down
1 change: 0 additions & 1 deletion tests/eth2/beacon/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
)

import eth2._utils.bls as bls
from eth2.beacon._utils.hash import hash_eth2
from eth2.beacon.configs import (
BeaconConfig,
CommitteeConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


def test_process_max_attestations(genesis_state,
genesis_block,
sample_beacon_block_params,
sample_beacon_block_body_params,
config,
Expand All @@ -32,11 +33,12 @@ def test_process_max_attestations(genesis_state,
)

attestations = create_mock_signed_attestations_at_slot(
state,
config,
attestation_slot,
keymap,
1.0,
state=state,
config=config,
attestation_slot=attestation_slot,
beacon_block_root=genesis_block.root,
keymap=keymap,
voted_attesters_ratio=1.0,
)

attestations_count = len(attestations)
Expand Down Expand Up @@ -140,6 +142,7 @@ def test_process_proposer_slashings(genesis_state,
]
)
def test_process_attestations(genesis_state,
genesis_block,
sample_beacon_block_params,
sample_beacon_block_body_params,
config,
Expand All @@ -153,11 +156,12 @@ def test_process_attestations(genesis_state,
)

attestations = create_mock_signed_attestations_at_slot(
state,
config,
attestation_slot,
keymap,
1.0,
state=state,
config=config,
attestation_slot=attestation_slot,
beacon_block_root=genesis_block.root,
keymap=keymap,
voted_attesters_ratio=1.0,
)

assert len(attestations) > 0
Expand Down
42 changes: 26 additions & 16 deletions tests/eth2/beacon/state_machines/test_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'shard_count'
),
[
(20, 4, 2, 2, 2)
(40, 8, 2, 3, 2)
]
)
def test_demo(base_db,
Expand All @@ -49,12 +49,17 @@ def test_demo(base_db,
state = genesis_state
block = genesis_block

current_slot = 1
chain_length = 3 * config.SLOTS_PER_EPOCH
attestations = ()
blocks = (block,)
for current_slot in range(chain_length):
# two epochs

attestations_map = {} # Dict[Slot, Sequence[Attestation]]

for current_slot in range(1, chain_length):
if current_slot > config.MIN_ATTESTATION_INCLUSION_DELAY:
attestations = attestations_map[current_slot - config.MIN_ATTESTATION_INCLUSION_DELAY]
else:
attestations = ()

block = create_mock_block(
state=state,
config=config,
Expand Down Expand Up @@ -85,17 +90,22 @@ def test_demo(base_db,
chaindb.persist_block(block, SerenityBeaconBlock)

blocks += (block,)
if current_slot > config.MIN_ATTESTATION_INCLUSION_DELAY:
attestation_slot = current_slot - config.MIN_ATTESTATION_INCLUSION_DELAY
attestations = create_mock_signed_attestations_at_slot(
state,
config,
attestation_slot,
keymap,
1.0,
)
else:
attestations = ()

# Mock attestations
attestation_slot = current_slot
attestations = create_mock_signed_attestations_at_slot(
state=state,
config=config,
attestation_slot=attestation_slot,
beacon_block_root=block.root,
keymap=keymap,
voted_attesters_ratio=1.0,
)
attestations_map[attestation_slot] = attestations

assert state.slot == chain_length - 1
assert isinstance(sm.block, SerenityBeaconBlock)

# Justification assertions
assert state.justified_epoch == 2
assert state.finalized_epoch == 1

0 comments on commit 5f6849d

Please sign in to comment.