Add simple tests for `shard_state_transition`

This commit is contained in:
Hsiao-Wei Wang 2019-08-01 14:19:08 +08:00
parent ce3df38028
commit 3aba05e252
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
3 changed files with 97 additions and 11 deletions

View File

@ -129,3 +129,13 @@ DOMAIN_TRANSFER: 0x05000000
DOMAIN_CUSTODY_BIT_CHALLENGE: 0x06000000
DOMAIN_SHARD_PROPOSER: 0x80000000
DOMAIN_SHARD_ATTESTER: 0x81000000
# Phase 1
# ---------------------------------------------------------------
SHARD_SLOTS_PER_BEACON_SLOT: 2
EPOCHS_PER_SHARD_PERIOD: 4
# PHASE_1_FORK_EPOCH >= EPOCHS_PER_SHARD_PERIOD * 2
PHASE_1_FORK_EPOCH: 8
# PHASE_1_FORK_SLOT = PHASE_1_FORK_EPOCH * SHARD_SLOTS_PER_BEACON_SLOT * SLOTS_PER_EPOCH
PHASE_1_FORK_SLOT: 128

View File

@ -7,6 +7,10 @@ from eth2spec.utils.ssz.ssz_impl import (
signing_root,
)
from .attestations import (
sign_shard_attestation,
)
@only_with_bls()
def sign_shard_block(spec, state, block, shard, proposer_index=None):
@ -26,22 +30,52 @@ def sign_shard_block(spec, state, block, shard, proposer_index=None):
)
def build_empty_shard_block(spec, state, slot, shard, parent_root, signed=False):
def build_empty_shard_block(spec,
shard_state,
beacon_state,
slot,
parent_root,
signed=False,
full_attestation=False):
if slot is None:
slot = state.slot
slot = shard_state.slot
block = spec.ShardBlock(
core=spec.ExtendedShardBlockCore(
slot=slot,
beacon_chain_root=state.block_roots[state.slot % spec.SLOTS_PER_HISTORICAL_ROOT],
beacon_chain_root=beacon_state.block_roots[beacon_state.slot % spec.SLOTS_PER_HISTORICAL_ROOT],
parent_root=parent_root,
),
signatures=spec.ShardBlockSignatures(
attestation_signature=b'\x12' * 96,
attestation_signature=b'\x00' * 96,
proposer_signature=b'\x25' * 96,
)
)
# attestation
if full_attestation:
attester_committee = spec.get_persistent_committee(beacon_state, shard_state.shard, block.core.slot)
block.core.attester_bitfield = list(
(True,) * len(attester_committee) +
(False,) * (spec.MAX_PERSISTENT_COMMITTEE_SIZE * 2 - len(attester_committee))
)
block.signatures.attestation_signature = sign_shard_attestation(
spec,
shard_state,
beacon_state,
block,
participants=attester_committee,
)
else:
block.signatures.attestation_signature = sign_shard_attestation(
spec,
shard_state,
beacon_state,
block,
participants=(),
)
if signed:
sign_shard_block(spec, state, block, shard)
sign_shard_block(spec, beacon_state, block, shard_state.shard)
return block

View File

@ -11,16 +11,58 @@ from eth2spec.test.context import (
@with_all_phases_except(['phase0'])
@always_bls
@spec_state_test
def test_is_valid_shard_block(spec, state):
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,
state,
slot=spec.Slot(spec.PERSISTENT_COMMITTEE_PERIOD * 100),
shard=spec.Shard(1),
shard_state,
beacon_state,
slot=shard_slot + 1,
parent_root=spec.Hash(),
signed=True,
full_attestation=False,
)
# TODO: test `is_valid_shard_block`
yield 'pre', shard_state
yield 'beacon_state', beacon_state
yield 'block', block
yield 'blocks', (block,)
spec.shard_state_transition(shard_state, beacon_state, block)
yield 'post', shard_state
@with_all_phases_except(['phase0'])
@always_bls
@spec_state_test
def test_process_full_attestation_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
spec.shard_state_transition(shard_state, beacon_state, block)
yield 'post', shard_state