From 7509ecb742173e0df6768d18fdc4d07686ae5184 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 28 May 2020 22:39:52 +0800 Subject: [PATCH 1/5] Add comments, minor refactoring --- specs/phase1/beacon-chain.md | 75 ++++++++++++++++++-------------- specs/phase1/shard-transition.md | 6 +-- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 0ae4bae2b..89dd6aaac 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -64,7 +64,7 @@ - [`apply_shard_transition`](#apply_shard_transition) - [`process_crosslink_for_shard`](#process_crosslink_for_shard) - [`process_crosslinks`](#process_crosslinks) - - [`process_attestation`](#process_attestation) + - [Updated `process_attestation`](#updated-process_attestation) - [New Attester slashing processing](#new-attester-slashing-processing) - [Shard transition false positives](#shard-transition-false-positives) - [Light client processing](#light-client-processing) @@ -153,6 +153,7 @@ class PendingAttestation(Container): data: AttestationData inclusion_delay: Slot proposer_index: ValidatorIndex + # Phase 1 crosslink_success: boolean ``` @@ -445,13 +446,13 @@ def compute_offset_slots(start_slot: Slot, end_slot: Slot) -> Sequence[Slot]: #### `compute_updated_gasprice` ```python -def compute_updated_gasprice(prev_gasprice: Gwei, length: uint8) -> Gwei: - if length > TARGET_SHARD_BLOCK_SIZE: - delta = (prev_gasprice * (length - TARGET_SHARD_BLOCK_SIZE) +def compute_updated_gasprice(prev_gasprice: Gwei, shard_block_length: uint8) -> Gwei: + if shard_block_length > TARGET_SHARD_BLOCK_SIZE: + delta = (prev_gasprice * (shard_block_length - TARGET_SHARD_BLOCK_SIZE) // TARGET_SHARD_BLOCK_SIZE // GASPRICE_ADJUSTMENT_COEFFICIENT) return min(prev_gasprice + delta, MAX_GASPRICE) else: - delta = (prev_gasprice * (TARGET_SHARD_BLOCK_SIZE - length) + delta = (prev_gasprice * (TARGET_SHARD_BLOCK_SIZE - shard_block_length) // TARGET_SHARD_BLOCK_SIZE // GASPRICE_ADJUSTMENT_COEFFICIENT) return max(prev_gasprice, MIN_GASPRICE + delta) - delta ``` @@ -477,9 +478,12 @@ def get_online_validator_indices(state: BeaconState) -> Set[ValidatorIndex]: ```python def get_shard_committee(beacon_state: BeaconState, epoch: Epoch, shard: Shard) -> Sequence[ValidatorIndex]: + """ + Return the shard committee of the given ``epoch`` of the given ``shard``. + """ source_epoch = epoch - epoch % SHARD_COMMITTEE_PERIOD if source_epoch >= SHARD_COMMITTEE_PERIOD: - source_epoch -= SHARD_COMMITTEE_PERIOD + source_epoch -= SHARD_COMMITTEE_PERIOD # `SHARD_COMMITTEE_PERIOD` epochs lookahead active_validator_indices = get_active_validator_indices(beacon_state, source_epoch) seed = get_seed(beacon_state, source_epoch, DOMAIN_SHARD_COMMITTEE) active_shard_count = get_active_shard_count(beacon_state) @@ -495,9 +499,12 @@ def get_shard_committee(beacon_state: BeaconState, epoch: Epoch, shard: Shard) - ```python def get_light_client_committee(beacon_state: BeaconState, epoch: Epoch) -> Sequence[ValidatorIndex]: + """ + Return the light client committee that no more than ``TARGET_COMMITTEE_SIZE`` validators. + """ source_epoch = epoch - epoch % LIGHT_CLIENT_COMMITTEE_PERIOD if source_epoch >= LIGHT_CLIENT_COMMITTEE_PERIOD: - source_epoch -= LIGHT_CLIENT_COMMITTEE_PERIOD + source_epoch -= LIGHT_CLIENT_COMMITTEE_PERIOD # `LIGHT_CLIENT_COMMITTEE_PERIOD` epochs lookahead active_validator_indices = get_active_validator_indices(beacon_state, source_epoch) seed = get_seed(beacon_state, source_epoch, DOMAIN_LIGHT_CLIENT) return compute_committee( @@ -554,7 +561,10 @@ def get_latest_slot_for_shard(state: BeaconState, shard: Shard) -> Slot: ```python def get_offset_slots(state: BeaconState, shard: Shard) -> Sequence[Slot]: - return compute_offset_slots(state.shard_states[shard].slot, state.slot) + """ + Return the offset slots of the given ``shard`` between that latest included slot and current slot. + """ + return compute_offset_slots(get_latest_slot_for_shard(state, shard), state.slot) ``` ### Predicates @@ -569,36 +579,38 @@ def is_valid_indexed_attestation(state: BeaconState, indexed_attestation: Indexe Check if ``indexed_attestation`` has valid indices and signature. """ # Verify aggregate signature - all_pubkeys = [] - all_signing_roots = [] attestation = indexed_attestation.attestation - domain = get_domain(state, DOMAIN_BEACON_ATTESTER, attestation.data.target.epoch) aggregation_bits = attestation.aggregation_bits if not any(aggregation_bits) or len(aggregation_bits) != len(indexed_attestation.committee): return False + domain = get_domain(state, DOMAIN_BEACON_ATTESTER, attestation.data.target.epoch) + all_pubkeys = [] + all_signing_roots = [] if len(attestation.custody_bits_blocks) == 0: # fall back on phase0 behavior if there is no shard data. - for participant, abit in zip(indexed_attestation.committee, aggregation_bits): - if abit: + for participant, aggregation_bit in zip(indexed_attestation.committee, aggregation_bits): + if aggregation_bit: all_pubkeys.append(state.validators[participant].pubkey) signing_root = compute_signing_root(indexed_attestation.attestation.data, domain) return bls.FastAggregateVerify(all_pubkeys, signing_root, signature=attestation.signature) else: - for i, custody_bits in enumerate(attestation.custody_bits_blocks): + for block_index, custody_bits in enumerate(attestation.custody_bits_blocks): assert len(custody_bits) == len(indexed_attestation.committee) - for participant, abit, cbit in zip(indexed_attestation.committee, aggregation_bits, custody_bits): - if abit: + for participant, aggregation_bit, custody_bit in zip( + indexed_attestation.committee, aggregation_bits, custody_bits + ): + if aggregation_bit: all_pubkeys.append(state.validators[participant].pubkey) # Note: only 2N distinct message hashes attestation_wrapper = AttestationCustodyBitWrapper( attestation_data_root=hash_tree_root(attestation.data), - block_index=i, - bit=cbit + block_index=block_index, + bit=custody_bit ) all_signing_roots.append(compute_signing_root(attestation_wrapper, domain)) else: - assert not cbit + assert not custody_bit return bls.AggregateVerify(all_pubkeys, all_signing_roots, signature=attestation.signature) ``` @@ -767,22 +779,22 @@ def apply_shard_transition(state: BeaconState, shard: Shard, transition: ShardTr proposers = [] prev_gasprice = state.shard_states[shard].gasprice shard_parent_root = state.shard_states[shard].latest_block_root - for i in range(len(offset_slots)): + for i, offset_slot in enumerate(offset_slots): shard_block_length = transition.shard_block_lengths[i] shard_state = transition.shard_states[i] # Verify correct calculation of gas prices and slots assert shard_state.gasprice == compute_updated_gasprice(prev_gasprice, shard_block_length) - assert shard_state.slot == offset_slots[i] + assert shard_state.slot == offset_slot # Collect the non-empty proposals result is_empty_proposal = shard_block_length == 0 if not is_empty_proposal: - proposal_index = get_shard_proposer_index(state, offset_slots[i], shard) + proposal_index = get_shard_proposer_index(state, offset_slot, shard) # Reconstruct shard headers header = ShardBlockHeader( shard_parent_root=shard_parent_root, - beacon_parent_root=get_block_root_at_slot(state, offset_slots[i]), + beacon_parent_root=get_block_root_at_slot(state, offset_slot), proposer_index=proposal_index, - slot=offset_slots[i], + slot=offset_slot, body_root=transition.shard_data_roots[i] ) shard_parent_root = hash_tree_root(header) @@ -872,13 +884,12 @@ def process_crosslinks(state: BeaconState, for committee_index in map(CommitteeIndex, range(committee_count)): shard = compute_shard_from_committee_index(state, committee_index, state.slot) # All attestations in the block for this committee/shard and current slot - shard_transition = shard_transitions[shard] shard_attestations = [ attestation for attestation in attestations if is_shard_attestation(state, attestation, committee_index) ] - winning_root = process_crosslink_for_shard(state, committee_index, shard_transition, shard_attestations) + winning_root = process_crosslink_for_shard(state, committee_index, shard_transitions[shard], shard_attestations) if winning_root != Root(): # Mark relevant pending attestations as creating a successful crosslink for pending_attestation in state.current_epoch_attestations: @@ -886,7 +897,7 @@ def process_crosslinks(state: BeaconState, pending_attestation.crosslink_success = True ``` -###### `process_attestation` +###### Updated `process_attestation` ```python def process_attestation(state: BeaconState, attestation: Attestation) -> None: @@ -910,11 +921,9 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None: ```python def get_indices_from_committee( committee: List[ValidatorIndex, MAX_VALIDATORS_PER_COMMITTEE], - bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE]) -> List[ValidatorIndex, MAX_VALIDATORS_PER_COMMITTEE]: + bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE]) -> Sequence[ValidatorIndex]: assert len(bits) == len(committee) - return List[ValidatorIndex, MAX_VALIDATORS_PER_COMMITTEE]( - [validator_index for i, validator_index in enumerate(committee) if bits[i]] - ) + return ([validator_index for i, validator_index in enumerate(committee) if bits[i]]) ``` ```python @@ -1018,7 +1027,9 @@ def process_online_tracking(state: BeaconState) -> None: ```python def process_light_client_committee_updates(state: BeaconState) -> None: - # Update light client committees + """ + Update light client committees. + """ if get_current_epoch(state) % LIGHT_CLIENT_COMMITTEE_PERIOD == 0: state.current_light_committee = state.next_light_committee new_committee = get_light_client_committee(state, get_current_epoch(state) + LIGHT_CLIENT_COMMITTEE_PERIOD) diff --git a/specs/phase1/shard-transition.md b/specs/phase1/shard-transition.md index da3aa648c..1a8277066 100644 --- a/specs/phase1/shard-transition.md +++ b/specs/phase1/shard-transition.md @@ -268,12 +268,8 @@ def get_shard_transition(beacon_state: BeaconState, shard: Shard, shard_blocks: Sequence[SignedShardBlock]) -> ShardTransition: offset_slots = get_offset_slots(beacon_state, shard) - start_slot = offset_slots[0] proposals, shard_states, shard_data_roots = get_shard_state_transition_result(beacon_state, shard, shard_blocks) - assert len(proposals) > 0 - assert len(shard_data_roots) > 0 - shard_block_lengths = [] proposer_signatures = [] for proposal in proposals: @@ -287,7 +283,7 @@ def get_shard_transition(beacon_state: BeaconState, proposer_signature_aggregate = NO_SIGNATURE return ShardTransition( - start_slot=start_slot, + start_slot=offset_slots[0], shard_block_lengths=shard_block_lengths, shard_data_roots=shard_data_roots, shard_states=shard_states, From 8ae7f5b6fa46c6423798ba56dee3c7192db8b232 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 29 May 2020 01:24:17 +0800 Subject: [PATCH 2/5] Refactor `is_valid_indexed_attestation`: extract `verify_attestation_custody` --- specs/phase1/beacon-chain.md | 57 +++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 89dd6aaac..ee282f203 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -52,6 +52,7 @@ - [`get_latest_slot_for_shard`](#get_latest_slot_for_shard) - [`get_offset_slots`](#get_offset_slots) - [Predicates](#predicates) + - [`verify_attestation_custody`](#verify_attestation_custody) - [Updated `is_valid_indexed_attestation`](#updated-is_valid_indexed_attestation) - [`is_shard_attestation`](#is_shard_attestation) - [`is_winning_attestation`](#is_winning_attestation) @@ -414,7 +415,7 @@ def unpack_compact_validator(compact_validator: uint64) -> Tuple[ValidatorIndex, ```python def committee_to_compact_committee(state: BeaconState, committee: Sequence[ValidatorIndex]) -> CompactCommittee: """ - Given a state and a list of validator indices, outputs the CompactCommittee representing them. + Given a state and a list of validator indices, outputs the ``CompactCommittee`` representing them. """ validators = [state.validators[i] for i in committee] compact_validators = [ @@ -569,6 +570,37 @@ def get_offset_slots(state: BeaconState, shard: Shard) -> Sequence[Slot]: ### Predicates +#### `verify_attestation_custody` + +```python +def verify_attestation_custody(state: BeaconState, indexed_attestation: IndexedAttestation) -> bool: + """ + Check if ``indexed_attestation`` has valid signature against non-empty custody bits. + """ + attestation = indexed_attestation.attestation + aggregation_bits = attestation.aggregation_bits + domain = get_domain(state, DOMAIN_BEACON_ATTESTER, attestation.data.target.epoch) + all_pubkeys = [] + all_signing_roots = [] + for block_index, custody_bits in enumerate(attestation.custody_bits_blocks): + assert len(custody_bits) == len(indexed_attestation.committee) + for participant, aggregation_bit, custody_bit in zip( + indexed_attestation.committee, aggregation_bits, custody_bits + ): + if aggregation_bit: + all_pubkeys.append(state.validators[participant].pubkey) + # Note: only 2N distinct message hashes + attestation_wrapper = AttestationCustodyBitWrapper( + attestation_data_root=hash_tree_root(attestation.data), + block_index=block_index, + bit=custody_bit, + ) + all_signing_roots.append(compute_signing_root(attestation_wrapper, domain)) + else: + assert not custody_bit + return bls.AggregateVerify(all_pubkeys, all_signing_roots, signature=attestation.signature) +``` + #### Updated `is_valid_indexed_attestation` Note that this replaces the Phase 0 `is_valid_indexed_attestation`. @@ -584,34 +616,17 @@ def is_valid_indexed_attestation(state: BeaconState, indexed_attestation: Indexe if not any(aggregation_bits) or len(aggregation_bits) != len(indexed_attestation.committee): return False - domain = get_domain(state, DOMAIN_BEACON_ATTESTER, attestation.data.target.epoch) - all_pubkeys = [] - all_signing_roots = [] if len(attestation.custody_bits_blocks) == 0: # fall back on phase0 behavior if there is no shard data. + domain = get_domain(state, DOMAIN_BEACON_ATTESTER, attestation.data.target.epoch) + all_pubkeys = [] for participant, aggregation_bit in zip(indexed_attestation.committee, aggregation_bits): if aggregation_bit: all_pubkeys.append(state.validators[participant].pubkey) signing_root = compute_signing_root(indexed_attestation.attestation.data, domain) return bls.FastAggregateVerify(all_pubkeys, signing_root, signature=attestation.signature) else: - for block_index, custody_bits in enumerate(attestation.custody_bits_blocks): - assert len(custody_bits) == len(indexed_attestation.committee) - for participant, aggregation_bit, custody_bit in zip( - indexed_attestation.committee, aggregation_bits, custody_bits - ): - if aggregation_bit: - all_pubkeys.append(state.validators[participant].pubkey) - # Note: only 2N distinct message hashes - attestation_wrapper = AttestationCustodyBitWrapper( - attestation_data_root=hash_tree_root(attestation.data), - block_index=block_index, - bit=custody_bit - ) - all_signing_roots.append(compute_signing_root(attestation_wrapper, domain)) - else: - assert not custody_bit - return bls.AggregateVerify(all_pubkeys, all_signing_roots, signature=attestation.signature) + return verify_attestation_custody(state, indexed_attestation) ``` #### `is_shard_attestation` From bd9f983eeae34b11ebb84df2d000ab7f22ab3d34 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 29 May 2020 02:20:38 +0800 Subject: [PATCH 3/5] Minor fix --- specs/phase1/beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index ee282f203..ce47ef585 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -938,7 +938,7 @@ def get_indices_from_committee( committee: List[ValidatorIndex, MAX_VALIDATORS_PER_COMMITTEE], bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE]) -> Sequence[ValidatorIndex]: assert len(bits) == len(committee) - return ([validator_index for i, validator_index in enumerate(committee) if bits[i]]) + return [validator_index for i, validator_index in enumerate(committee) if bits[i]] ``` ```python From f70224b84e0e2eebf7e89ff36fc218ea217d3569 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sat, 30 May 2020 03:09:42 +0800 Subject: [PATCH 4/5] Extract `compute_committee_source_epoch` --- specs/phase1/beacon-chain.md | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index ce47ef585..054868847 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -40,6 +40,7 @@ - [`compute_shard_from_committee_index`](#compute_shard_from_committee_index) - [`compute_offset_slots`](#compute_offset_slots) - [`compute_updated_gasprice`](#compute_updated_gasprice) + - [`compute_committee_source_epoch`](#compute_committee_source_epoch) - [Beacon state accessors](#beacon-state-accessors) - [`get_active_shard_count`](#get_active_shard_count) - [`get_online_validator_indices`](#get_online_validator_indices) @@ -458,6 +459,19 @@ def compute_updated_gasprice(prev_gasprice: Gwei, shard_block_length: uint8) -> return max(prev_gasprice, MIN_GASPRICE + delta) - delta ``` +#### `compute_committee_source_epoch` + +```python +def compute_committee_source_epoch(epoch: Epoch, period: uint64) -> Epoch: + """ + Return the source epoch for computing the committee. + """ + source_epoch = epoch - epoch % period + if source_epoch >= period: + source_epoch -= period # `period` epochs lookahead + return source_epoch +``` + ### Beacon state accessors #### `get_active_shard_count` @@ -482,9 +496,7 @@ def get_shard_committee(beacon_state: BeaconState, epoch: Epoch, shard: Shard) - """ Return the shard committee of the given ``epoch`` of the given ``shard``. """ - source_epoch = epoch - epoch % SHARD_COMMITTEE_PERIOD - if source_epoch >= SHARD_COMMITTEE_PERIOD: - source_epoch -= SHARD_COMMITTEE_PERIOD # `SHARD_COMMITTEE_PERIOD` epochs lookahead + source_epoch = compute_committee_source_epoch(epoch, SHARD_COMMITTEE_PERIOD) active_validator_indices = get_active_validator_indices(beacon_state, source_epoch) seed = get_seed(beacon_state, source_epoch, DOMAIN_SHARD_COMMITTEE) active_shard_count = get_active_shard_count(beacon_state) @@ -503,9 +515,7 @@ def get_light_client_committee(beacon_state: BeaconState, epoch: Epoch) -> Seque """ Return the light client committee that no more than ``TARGET_COMMITTEE_SIZE`` validators. """ - source_epoch = epoch - epoch % LIGHT_CLIENT_COMMITTEE_PERIOD - if source_epoch >= LIGHT_CLIENT_COMMITTEE_PERIOD: - source_epoch -= LIGHT_CLIENT_COMMITTEE_PERIOD # `LIGHT_CLIENT_COMMITTEE_PERIOD` epochs lookahead + source_epoch = compute_committee_source_epoch(epoch, LIGHT_CLIENT_COMMITTEE_PERIOD) active_validator_indices = get_active_validator_indices(beacon_state, source_epoch) seed = get_seed(beacon_state, source_epoch, DOMAIN_LIGHT_CLIENT) return compute_committee( From 6317bd68aa7476d82064daf70d7dbd71236ae3e1 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 1 Jun 2020 17:47:47 +0800 Subject: [PATCH 5/5] PR feedback from Danny Co-authored-by: Danny Ryan --- specs/phase1/beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 6c792432f..308af4ee3 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -515,7 +515,7 @@ def get_shard_committee(beacon_state: BeaconState, epoch: Epoch, shard: Shard) - ```python def get_light_client_committee(beacon_state: BeaconState, epoch: Epoch) -> Sequence[ValidatorIndex]: """ - Return the light client committee that no more than ``TARGET_COMMITTEE_SIZE`` validators. + Return the light client committee of no more than ``TARGET_COMMITTEE_SIZE`` validators. """ source_epoch = compute_committee_source_epoch(epoch, LIGHT_CLIENT_COMMITTEE_PERIOD) active_validator_indices = get_active_validator_indices(beacon_state, source_epoch)