Fix issue around on_attestation validation by skipping epoch scope

validation if attestation is from a block message
This commit is contained in:
Hsiao-Wei Wang 2021-11-13 18:23:21 +08:00
parent cd3d2ce692
commit f643554aa5
No known key found for this signature in database
GPG Key ID: 1111A8A81778319E
3 changed files with 42 additions and 55 deletions

View File

@ -22,6 +22,7 @@
- [`get_head`](#get_head)
- [`should_update_justified_checkpoint`](#should_update_justified_checkpoint)
- [`on_attestation` helpers](#on_attestation-helpers)
- [`validate_target_epoch_scope`](#validate_target_epoch_scope)
- [`validate_on_attestation`](#validate_on_attestation)
- [`store_target_checkpoint_state`](#store_target_checkpoint_state)
- [`update_latest_messages`](#update_latest_messages)
@ -257,10 +258,11 @@ def should_update_justified_checkpoint(store: Store, new_justified_checkpoint: C
#### `on_attestation` helpers
##### `validate_on_attestation`
##### `validate_target_epoch_scope`
```python
def validate_on_attestation(store: Store, attestation: Attestation) -> None:
def validate_target_epoch_scope(store: Store, attestation: Attestation) -> None:
target = attestation.data.target
# Attestations must be from the current or previous epoch
@ -269,6 +271,15 @@ def validate_on_attestation(store: Store, attestation: Attestation) -> None:
previous_epoch = current_epoch - 1 if current_epoch > GENESIS_EPOCH else GENESIS_EPOCH
# If attestation target is from a future epoch, delay consideration until the epoch arrives
assert target.epoch in [current_epoch, previous_epoch]
```
##### `validate_on_attestation`
```python
def validate_on_attestation(store: Store, attestation: Attestation) -> None:
target = attestation.data.target
# Check that the epoch number and slot number are matching
assert target.epoch == compute_epoch_at_slot(attestation.data.slot)
# Attestations target be for a known block. If target block is unknown, delay consideration until the block is found
@ -378,7 +389,7 @@ def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
#### `on_attestation`
```python
def on_attestation(store: Store, attestation: Attestation) -> None:
def on_attestation(store: Store, attestation: Attestation, is_from_block: bool=False) -> None:
"""
Run ``on_attestation`` upon receiving a new ``attestation`` from either within a block or directly on the wire.
@ -386,6 +397,10 @@ def on_attestation(store: Store, attestation: Attestation) -> None:
consider scheduling it for later processing in such case.
"""
validate_on_attestation(store, attestation)
if not is_from_block:
# If the given attestation is not from a beacon block message, we have to check the target epoch scope.
validate_target_epoch_scope(store, attestation)
store_target_checkpoint_state(store, attestation.data.target)
# Get state at the `target` to fully validate attestation

View File

@ -23,7 +23,7 @@ def add_block_to_store(spec, store, signed_block):
spec.on_block(store, signed_block)
def tick_and_add_block(spec, store, signed_block, test_steps, valid=True, allow_invalid_attestations=False,
def tick_and_add_block(spec, store, signed_block, test_steps, valid=True,
merge_block=False, block_not_found=False):
pre_state = store.block_states[signed_block.message.parent_root]
block_time = pre_state.genesis_time + signed_block.message.slot * spec.config.SECONDS_PER_SLOT
@ -36,14 +36,13 @@ def tick_and_add_block(spec, store, signed_block, test_steps, valid=True, allow_
post_state = yield from add_block(
spec, store, signed_block, test_steps,
valid=valid,
allow_invalid_attestations=allow_invalid_attestations,
block_not_found=block_not_found,
)
return post_state
def tick_and_run_on_attestation(spec, store, attestation, test_steps):
def tick_and_run_on_attestation(spec, store, attestation, test_steps, is_from_block=False):
parent_block = store.blocks[attestation.data.beacon_block_root]
pre_state = store.block_states[spec.hash_tree_root(parent_block)]
block_time = pre_state.genesis_time + parent_block.slot * spec.config.SECONDS_PER_SLOT
@ -53,40 +52,21 @@ def tick_and_run_on_attestation(spec, store, attestation, test_steps):
spec.on_tick(store, next_epoch_time)
test_steps.append({'tick': int(next_epoch_time)})
spec.on_attestation(store, attestation)
spec.on_attestation(store, attestation, is_from_block=is_from_block)
yield get_attestation_file_name(attestation), attestation
test_steps.append({'attestation': get_attestation_file_name(attestation)})
def add_attestation(spec, store, attestation, test_steps, valid=True):
yield get_attestation_file_name(attestation), attestation
def run_on_attestation(spec, store, attestation, is_from_block=False, valid=True):
if not valid:
try:
run_on_attestation(spec, store, attestation, valid=True)
except AssertionError:
test_steps.append({
'attestation': get_attestation_file_name(attestation),
'valid': False,
})
return
else:
assert False
run_on_attestation(spec, store, attestation, valid=True)
test_steps.append({'attestation': get_attestation_file_name(attestation)})
def run_on_attestation(spec, store, attestation, valid=True):
if not valid:
try:
spec.on_attestation(store, attestation)
spec.on_attestation(store, attestation, is_from_block)
except AssertionError:
return
else:
assert False
spec.on_attestation(store, attestation)
spec.on_attestation(store, attestation, is_from_block)
def get_genesis_forkchoice_store(spec, genesis_state):
@ -131,7 +111,6 @@ def add_block(spec,
signed_block,
test_steps,
valid=True,
allow_invalid_attestations=False,
block_not_found=False):
"""
Run on_block and on_attestation
@ -156,14 +135,8 @@ def add_block(spec,
test_steps.append({'block': get_block_file_name(signed_block)})
# An on_block step implies receiving block's attestations
try:
for attestation in signed_block.message.body.attestations:
run_on_attestation(spec, store, attestation, valid=True)
except AssertionError:
if allow_invalid_attestations:
pass
else:
raise
for attestation in signed_block.message.body.attestations:
run_on_attestation(spec, store, attestation, is_from_block=True, valid=True)
block_root = signed_block.message.hash_tree_root()
print(encode_hex(block_root))

View File

@ -543,21 +543,19 @@ def test_new_justified_is_later_than_store_justified(spec, state):
assert fork_3_state.finalized_checkpoint.epoch == 3
assert fork_3_state.current_justified_checkpoint.epoch == 4
# FIXME: pending on the `on_block`, `on_attestation` fix
# # Apply blocks of `fork_3_state` to `store`
# for block in all_blocks:
# if store.time < spec.compute_time_at_slot(fork_2_state, block.message.slot):
# time = store.genesis_time + block.message.slot * spec.config.SECONDS_PER_SLOT
# on_tick_and_append_step(spec, store, time, test_steps)
# # valid_attestations=False because the attestations are outdated (older than previous epoch)
# yield from add_block(spec, store, block, test_steps, allow_invalid_attestations=False)
# Apply blocks of `fork_3_state` to `store`
for block in all_blocks:
if store.time < spec.compute_time_at_slot(fork_2_state, block.message.slot):
time = store.genesis_time + block.message.slot * spec.config.SECONDS_PER_SLOT
on_tick_and_append_step(spec, store, time, test_steps)
yield from add_block(spec, store, block, test_steps)
# assert store.finalized_checkpoint == fork_3_state.finalized_checkpoint
# assert (store.justified_checkpoint
# == fork_3_state.current_justified_checkpoint
# != store.best_justified_checkpoint)
# assert (store.best_justified_checkpoint
# == fork_2_state.current_justified_checkpoint)
assert store.finalized_checkpoint == fork_3_state.finalized_checkpoint
assert (store.justified_checkpoint
== fork_3_state.current_justified_checkpoint
!= store.best_justified_checkpoint)
assert (store.best_justified_checkpoint
== fork_2_state.current_justified_checkpoint)
yield 'steps', test_steps
@ -628,7 +626,7 @@ def test_new_finalized_slot_is_not_justified_checkpoint_ancestor(spec, state):
# # Apply blocks of `another_state` to `store`
# for block in all_blocks:
# # NOTE: Do not call `on_tick` here
# yield from add_block(spec, store, block, test_steps, allow_invalid_attestations=True)
# yield from add_block(spec, store, block, test_steps)
# finalized_slot = spec.compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
# ancestor_at_finalized_slot = spec.get_ancestor(store, pre_store_justified_checkpoint_root, finalized_slot)
@ -704,7 +702,7 @@ def test_new_finalized_slot_is_justified_checkpoint_ancestor(spec, state):
for block in all_blocks:
# FIXME: Once `on_block` and `on_attestation` logic is fixed,
# fix test case and remove allow_invalid_attestations flag
yield from tick_and_add_block(spec, store, block, test_steps, allow_invalid_attestations=True)
yield from tick_and_add_block(spec, store, block, test_steps)
finalized_slot = spec.compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
ancestor_at_finalized_slot = spec.get_ancestor(store, pre_store_justified_checkpoint_root, finalized_slot)
@ -712,8 +710,9 @@ def test_new_finalized_slot_is_justified_checkpoint_ancestor(spec, state):
assert store.finalized_checkpoint == another_state.finalized_checkpoint
# Thus should fail with the fix. Once show fail, swap to ==
assert store.justified_checkpoint != another_state.current_justified_checkpoint
# assert store.justified_checkpoint != another_state.current_justified_checkpoint
print(store.finalized_checkpoint)
print(store.justified_checkpoint)
print('spec.get_head(store)', spec.get_head(store))
yield 'steps', test_steps