Fix + refactor `is_valid_beacon_attestation` and add basic test

This commit is contained in:
Hsiao-Wei Wang 2019-08-11 22:21:58 +08:00
parent 095cfe6633
commit 5290b62465
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
2 changed files with 59 additions and 10 deletions

View File

@ -555,16 +555,14 @@ def shard_block_transition(state: ShardState,
Let:
- `shard` be a valid `Shard`
- `pre_state` is the `ShardState` before processing any blocks
- `shard_blocks_or_state_roots` be the `Union[ShardBlock, Hash]` list such that `shard_blocks[slot]` is the canonical `ShardBlock` for shard `shard` at slot `slot` if a block exists, or the post-state-root of processing state up to and including that slot if a block does not exist.
- `shard_blocks_or_state_roots` be the `Union[ShardBlock, Hash]` list such that `shard_blocks[slot]` is the canonical `ShardBlock` for shard `pre_state.shard` at slot `slot` if a block exists, or the post-state-root of processing state up to and including that slot if a block does not exist.
- `beacon_state` be the canonical `BeaconState`
- `valid_attestations` be the set of valid `Attestation` objects, recursively defined
- `candidate` be a candidate `Attestation` which is valid under Phase 0 rules, and for which validity is to be determined under Phase 1 rules by running `is_valid_beacon_attestation`
```python
def is_valid_beacon_attestation(shard: Shard,
pre_state: ShardState,
def is_valid_beacon_attestation(pre_state: ShardState,
shard_blocks_or_state_roots: Sequence[Union[ShardBlock, Hash]],
beacon_state: BeaconState,
valid_attestations: Set[Attestation],
@ -587,7 +585,7 @@ def is_valid_beacon_attestation(shard: Shard,
assert candidate.data.previous_attestation.epoch < compute_epoch_of_slot(candidate.data.slot)
# Check crosslink data root
start_epoch = beacon_state.crosslinks[shard].epoch
start_epoch = beacon_state.crosslinks[pre_state.shard].epoch
end_epoch = min(compute_epoch_of_slot(candidate.data.slot) - CROSSLINK_LOOKBACK,
start_epoch + MAX_EPOCHS_PER_CROSSLINK)
blocks = []
@ -595,11 +593,14 @@ def is_valid_beacon_attestation(shard: Shard,
if isinstance(shard_blocks_or_state_roots[slot], ShardBlock):
blocks.append(shard_blocks_or_state_roots[slot])
else:
blocks.append(ShardBlockHeader(ShardBlockCore(
slot=slot,
state_root=shard_blocks_or_state_roots[slot],
total_bytes=pre_state.total_bytes
), ShardBlockSignatures()))
blocks.append(ShardBlock(
core=ExtendedShardBlockCore(
slot=slot,
state_root=shard_blocks_or_state_roots[slot],
total_bytes=pre_state.total_bytes,
),
signatures=ShardBlockSignatures(),
))
assert candidate.data.crosslink.data_root == compute_crosslink_data_root(blocks)
return True

View File

@ -0,0 +1,48 @@
from eth2spec.test.context import (
with_all_phases_except,
spec_state_test,
always_bls,
)
from eth2spec.test.helpers.phase1.shard_block import (
build_empty_shard_block,
)
from eth2spec.test.helpers.attestations import get_valid_attestation
@with_all_phases_except(['phase0'])
@always_bls
@spec_state_test
def test_process_empty_shard_block(spec, state):
beacon_state = state
shard_slot = spec.PHASE_1_FORK_SLOT
beacon_state.slot = spec.Slot(spec.PHASE_1_FORK_EPOCH * spec.SLOTS_PER_EPOCH)
shard_state = spec.get_default_shard_state(beacon_state, shard=spec.Shard(0))
shard_state.slot = shard_slot
block = build_empty_shard_block(
spec,
shard_state,
beacon_state,
slot=shard_slot + 1,
parent_root=spec.Hash(),
signed=True,
full_attestation=True,
)
yield 'pre', shard_state
yield 'beacon_state', beacon_state
yield 'block', block
beacon_attestation = get_valid_attestation(spec, beacon_state, signed=True)
yield 'beacon_attestation', beacon_attestation
is_valid_beacon_attestation = spec.is_valid_beacon_attestation(
pre_state=shard_state,
shard_blocks_or_state_roots=(block,),
beacon_state=beacon_state,
valid_attestations=set([beacon_attestation]),
candidate=beacon_attestation,
)
assert is_valid_beacon_attestation
yield 'is_valid_beacon_attestation', is_valid_beacon_attestation