From cc9a4cdc46131b6d320eb4ac4bbd663db16bf917 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Wed, 16 Dec 2020 17:12:51 -0700 Subject: [PATCH 1/2] add base sanity light client tests --- specs/lightclient/beacon-chain.md | 2 +- specs/lightclient/lightclient-fork.md | 6 +- .../lightclient_patch/sanity/test_blocks.py | 102 ++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 tests/core/pyspec/eth2spec/test/lightclient_patch/sanity/test_blocks.py diff --git a/specs/lightclient/beacon-chain.md b/specs/lightclient/beacon-chain.md index eabcb3a86..3b03ad853 100644 --- a/specs/lightclient/beacon-chain.md +++ b/specs/lightclient/beacon-chain.md @@ -163,7 +163,7 @@ def get_sync_committee(state: BeaconState, epoch: Epoch) -> SyncCommittee: bls.AggregatePKs(pubkeys[i:i + SYNC_COMMITTEE_PUBKEY_AGGREGATES_SIZE]) for i in range(0, len(pubkeys), SYNC_COMMITTEE_PUBKEY_AGGREGATES_SIZE) ] - return SyncCommittee(pubkeys, aggregates) + return SyncCommittee(pubkeys=pubkeys, pubkey_aggregates=aggregates) ``` ### Block processing diff --git a/specs/lightclient/lightclient-fork.md b/specs/lightclient/lightclient-fork.md index bb67fa54a..568a9793b 100644 --- a/specs/lightclient/lightclient-fork.md +++ b/specs/lightclient/lightclient-fork.md @@ -75,9 +75,9 @@ def upgrade_to_lightclient_patch(pre: phase0.BeaconState) -> BeaconState: previous_justified_checkpoint=pre.previous_justified_checkpoint, current_justified_checkpoint=pre.current_justified_checkpoint, finalized_checkpoint=pre.finalized_checkpoint, - # Light-client - current_sync_committee=SyncCommittee(), - next_sync_committee=SyncCommittee(), ) + # Fill in sync committees + post.current_sync_committee = get_sync_committee(post, get_current_epoch(post)) + post.next_sync_committee = get_sync_committee(post, get_current_epoch(post) + EPOCHS_PER_SYNC_COMMITTEE_PERIOD) return post ``` diff --git a/tests/core/pyspec/eth2spec/test/lightclient_patch/sanity/test_blocks.py b/tests/core/pyspec/eth2spec/test/lightclient_patch/sanity/test_blocks.py new file mode 100644 index 000000000..df8e2545c --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/lightclient_patch/sanity/test_blocks.py @@ -0,0 +1,102 @@ +import random +from eth2spec.test.helpers.keys import privkeys, pubkeys +from eth2spec.utils import bls +from eth2spec.test.helpers.state import ( + state_transition_and_sign_block, + next_epoch, +) +from eth2spec.test.helpers.block import ( + build_empty_block_for_next_slot, +) +from eth2spec.test.context import ( + PHASE0, PHASE1, + with_all_phases_except, + spec_state_test, +) + + +def compute_light_client_signature(spec, state, slot, privkey): + domain = spec.get_domain(state, spec.DOMAIN_SYNC_COMMITTEE, spec.compute_epoch_at_slot(slot)) + if slot == state.slot: + block_root = build_empty_block_for_next_slot(spec, state).parent_root + else: + block_root = spec.get_block_root_at_slot(state, slot) + signing_root = spec.compute_signing_root(block_root, domain) + return bls.Sign(privkey, signing_root) + + +def compute_aggregate_light_client_signature(spec, state, slot, participants): + if len(participants) == 0: + return spec.G2_POINT_AT_INFINITY + + signatures = [] + for validator_index in participants: + privkey = privkeys[validator_index] + signatures.append( + compute_light_client_signature( + spec, + state, + slot, + privkey, + ) + ) + return bls.Aggregate(signatures) + + +def run_light_client_sanity_test(spec, state, fraction_full=1.0): + committee = spec.get_sync_committee_indices(state, spec.get_current_epoch(state)) + participants = random.sample(committee, int(len(committee) * fraction_full)) + + yield 'pre', state + + block = build_empty_block_for_next_slot(spec, state) + block.body.sync_committee_bits = [index in participants for index in committee] + block.body.sync_committee_signature = compute_aggregate_light_client_signature( + spec, + state, + block.slot - 1, + participants, + ) + signed_block = state_transition_and_sign_block(spec, state, block) + + yield 'blocks', [signed_block] + yield 'post', state + + +@with_all_phases_except([PHASE0, PHASE1]) +@spec_state_test +def test_full_light_client_committee(spec, state): + next_epoch(spec, state) + yield from run_light_client_sanity_test(spec, state, fraction_full=1.0) + + +@with_all_phases_except([PHASE0, PHASE1]) +@spec_state_test +def test_half_light_client_committee(spec, state): + next_epoch(spec, state) + yield from run_light_client_sanity_test(spec, state, fraction_full=0.5) + + +@with_all_phases_except([PHASE0, PHASE1]) +@spec_state_test +def test_empty_light_client_committee(spec, state): + next_epoch(spec, state) + yield from run_light_client_sanity_test(spec, state, fraction_full=0.0) + + +@with_all_phases_except([PHASE0, PHASE1]) +@spec_state_test +def test_full_light_client_committee_genesis(spec, state): + yield from run_light_client_sanity_test(spec, state, fraction_full=1.0) + + +@with_all_phases_except([PHASE0, PHASE1]) +@spec_state_test +def test_half_light_client_committee_genesis(spec, state): + yield from run_light_client_sanity_test(spec, state, fraction_full=0.5) + + +@with_all_phases_except([PHASE0, PHASE1]) +@spec_state_test +def test_empty_light_client_committee_genesis(spec, state): + yield from run_light_client_sanity_test(spec, state, fraction_full=0.0) From 89c5ca6bcd2fa9a1f57d342038bb08effa969fe7 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Thu, 17 Dec 2020 06:25:58 -0700 Subject: [PATCH 2/2] 'light_client' -> 'sync_committee' --- .../lightclient_patch/sanity/test_blocks.py | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/lightclient_patch/sanity/test_blocks.py b/tests/core/pyspec/eth2spec/test/lightclient_patch/sanity/test_blocks.py index df8e2545c..4fbdfc371 100644 --- a/tests/core/pyspec/eth2spec/test/lightclient_patch/sanity/test_blocks.py +++ b/tests/core/pyspec/eth2spec/test/lightclient_patch/sanity/test_blocks.py @@ -1,5 +1,5 @@ import random -from eth2spec.test.helpers.keys import privkeys, pubkeys +from eth2spec.test.helpers.keys import privkeys from eth2spec.utils import bls from eth2spec.test.helpers.state import ( state_transition_and_sign_block, @@ -15,7 +15,7 @@ from eth2spec.test.context import ( ) -def compute_light_client_signature(spec, state, slot, privkey): +def compute_sync_committee_signature(spec, state, slot, privkey): domain = spec.get_domain(state, spec.DOMAIN_SYNC_COMMITTEE, spec.compute_epoch_at_slot(slot)) if slot == state.slot: block_root = build_empty_block_for_next_slot(spec, state).parent_root @@ -25,7 +25,7 @@ def compute_light_client_signature(spec, state, slot, privkey): return bls.Sign(privkey, signing_root) -def compute_aggregate_light_client_signature(spec, state, slot, participants): +def compute_aggregate_sync_committee_signature(spec, state, slot, participants): if len(participants) == 0: return spec.G2_POINT_AT_INFINITY @@ -33,7 +33,7 @@ def compute_aggregate_light_client_signature(spec, state, slot, participants): for validator_index in participants: privkey = privkeys[validator_index] signatures.append( - compute_light_client_signature( + compute_sync_committee_signature( spec, state, slot, @@ -43,7 +43,7 @@ def compute_aggregate_light_client_signature(spec, state, slot, participants): return bls.Aggregate(signatures) -def run_light_client_sanity_test(spec, state, fraction_full=1.0): +def run_sync_committee_sanity_test(spec, state, fraction_full=1.0): committee = spec.get_sync_committee_indices(state, spec.get_current_epoch(state)) participants = random.sample(committee, int(len(committee) * fraction_full)) @@ -51,7 +51,7 @@ def run_light_client_sanity_test(spec, state, fraction_full=1.0): block = build_empty_block_for_next_slot(spec, state) block.body.sync_committee_bits = [index in participants for index in committee] - block.body.sync_committee_signature = compute_aggregate_light_client_signature( + block.body.sync_committee_signature = compute_aggregate_sync_committee_signature( spec, state, block.slot - 1, @@ -65,38 +65,38 @@ def run_light_client_sanity_test(spec, state, fraction_full=1.0): @with_all_phases_except([PHASE0, PHASE1]) @spec_state_test -def test_full_light_client_committee(spec, state): +def test_full_sync_committee_committee(spec, state): next_epoch(spec, state) - yield from run_light_client_sanity_test(spec, state, fraction_full=1.0) + yield from run_sync_committee_sanity_test(spec, state, fraction_full=1.0) @with_all_phases_except([PHASE0, PHASE1]) @spec_state_test -def test_half_light_client_committee(spec, state): +def test_half_sync_committee_committee(spec, state): next_epoch(spec, state) - yield from run_light_client_sanity_test(spec, state, fraction_full=0.5) + yield from run_sync_committee_sanity_test(spec, state, fraction_full=0.5) @with_all_phases_except([PHASE0, PHASE1]) @spec_state_test -def test_empty_light_client_committee(spec, state): +def test_empty_sync_committee_committee(spec, state): next_epoch(spec, state) - yield from run_light_client_sanity_test(spec, state, fraction_full=0.0) + yield from run_sync_committee_sanity_test(spec, state, fraction_full=0.0) @with_all_phases_except([PHASE0, PHASE1]) @spec_state_test -def test_full_light_client_committee_genesis(spec, state): - yield from run_light_client_sanity_test(spec, state, fraction_full=1.0) +def test_full_sync_committee_committee_genesis(spec, state): + yield from run_sync_committee_sanity_test(spec, state, fraction_full=1.0) @with_all_phases_except([PHASE0, PHASE1]) @spec_state_test -def test_half_light_client_committee_genesis(spec, state): - yield from run_light_client_sanity_test(spec, state, fraction_full=0.5) +def test_half_sync_committee_committee_genesis(spec, state): + yield from run_sync_committee_sanity_test(spec, state, fraction_full=0.5) @with_all_phases_except([PHASE0, PHASE1]) @spec_state_test -def test_empty_light_client_committee_genesis(spec, state): - yield from run_light_client_sanity_test(spec, state, fraction_full=0.0) +def test_empty_sync_committee_committee_genesis(spec, state): + yield from run_sync_committee_sanity_test(spec, state, fraction_full=0.0)