From 8143ff74158aa396a6e8f25fb7cfcfdc7cecbdcb Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 5 Dec 2019 14:02:36 +0800 Subject: [PATCH] spec v0.9.2 minor updates --- eth2/beacon/tools/builder/aggregator.py | 31 ++++++++++--------- eth2/beacon/types/aggregate_and_proof.py | 16 +++++----- tests/eth2/core/beacon/conftest.py | 4 +-- .../beacon/tools/builder/test_aggregator.py | 6 ++-- .../beacon/types/test_aggregate_and_proof.py | 10 +++--- trinity/components/eth2/beacon/validator.py | 10 +++--- 6 files changed, 43 insertions(+), 34 deletions(-) diff --git a/eth2/beacon/tools/builder/aggregator.py b/eth2/beacon/tools/builder/aggregator.py index ebe90908a5..6085f8969b 100644 --- a/eth2/beacon/tools/builder/aggregator.py +++ b/eth2/beacon/tools/builder/aggregator.py @@ -6,9 +6,14 @@ from eth2._utils.bls import bls from eth2._utils.hash import hash_eth2 -from eth2.beacon.attestation_helpers import validate_indexed_attestation_aggregate_signature +from eth2.beacon.attestation_helpers import ( + validate_indexed_attestation_aggregate_signature, +) from eth2.beacon.committee_helpers import get_beacon_committee -from eth2.beacon.epoch_processing_helpers import get_attesting_indices, get_indexed_attestation +from eth2.beacon.epoch_processing_helpers import ( + get_attesting_indices, + get_indexed_attestation, +) from eth2.beacon.helpers import compute_epoch_at_slot, get_domain from eth2.beacon.signature_domain import SignatureDomain from eth2.beacon.types.aggregate_and_proof import AggregateAndProof @@ -21,7 +26,7 @@ TARGET_AGGREGATORS_PER_COMMITTEE = 16 -def slot_signature( +def get_slot_signature( state: BeaconState, slot: Slot, privkey: int, config: CommitteeConfig ) -> BLSSignature: """ @@ -105,7 +110,8 @@ def validate_aggregate_and_proof( Reference: https://github.com/ethereum/eth2.0-specs/blob/v09x/specs/networking/p2p-interface.md#global-topics # noqa: E501 """ if ( - aggregate_and_proof.aggregate.data.slot + attestation_propagation_slot_range < state.slot + aggregate_and_proof.aggregate.data.slot + attestation_propagation_slot_range + < state.slot or aggregate_and_proof.aggregate.data.slot > state.slot ): raise ValidationError( @@ -121,9 +127,9 @@ def validate_aggregate_and_proof( aggregate_and_proof.aggregate.aggregation_bits, config, ) - if aggregate_and_proof.index not in attesting_indices: + if aggregate_and_proof.aggregator_index not in attesting_indices: raise ValidationError( - f"The aggregator index ({aggregate_and_proof.index}) is not within" + f"The aggregator index ({aggregate_and_proof.aggregator_index}) is not within" f" the aggregate's committee {attesting_indices}" ) @@ -135,7 +141,8 @@ def validate_aggregate_and_proof( config, ): raise ValidationError( - f"The given validator {aggregate_and_proof.index} is not selected aggregator" + f"The given validator {aggregate_and_proof.aggregator_index}" + " is not a selected aggregator" ) validate_aggregator_proof(state, aggregate_and_proof, config) @@ -147,7 +154,7 @@ def validate_aggregator_proof( state: BeaconState, aggregate_and_proof: AggregateAndProof, config: CommitteeConfig ) -> None: slot = aggregate_and_proof.aggregate.data.slot - pubkey = state.validators[aggregate_and_proof.index].pubkey + pubkey = state.validators[aggregate_and_proof.aggregator_index].pubkey domain = get_domain( state, SignatureDomain.DOMAIN_BEACON_ATTESTER, @@ -165,13 +172,9 @@ def validate_aggregator_proof( def validate_aggregate_signature( - state: BeaconState, - attestation: Attestation, - config: CommitteeConfig + state: BeaconState, attestation: Attestation, config: CommitteeConfig ) -> None: indexed_attestation = get_indexed_attestation(state, attestation, config) validate_indexed_attestation_aggregate_signature( - state, - indexed_attestation, - config.SLOTS_PER_EPOCH, + state, indexed_attestation, config.SLOTS_PER_EPOCH ) diff --git a/eth2/beacon/types/aggregate_and_proof.py b/eth2/beacon/types/aggregate_and_proof.py index 93c455b2dd..4e0b6097e0 100644 --- a/eth2/beacon/types/aggregate_and_proof.py +++ b/eth2/beacon/types/aggregate_and_proof.py @@ -12,26 +12,28 @@ class AggregateAndProof(ssz.Serializable): fields = [ - ("index", uint64), - ("selection_proof", bytes96), + ("aggregator_index", uint64), ("aggregate", Attestation), + ("selection_proof", bytes96), ] def __init__( self, - index: ValidatorIndex = default_validator_index, - selection_proof: BLSSignature = EMPTY_SIGNATURE, + aggregator_index: ValidatorIndex = default_validator_index, aggregate: Attestation = default_attestation, + selection_proof: BLSSignature = EMPTY_SIGNATURE, ) -> None: super().__init__( - index=index, selection_proof=selection_proof, aggregate=aggregate + aggregator_index=aggregator_index, + aggregate=aggregate, + selection_proof=selection_proof, ) def __str__(self) -> str: return ( - f"index={self.index}," - f" selection_proof={humanize_hash(self.selection_proof)}," + f"aggregator_index={self.aggregator_index}," f" aggregate={self.aggregate}," + f" selection_proof={humanize_hash(self.selection_proof)}," ) diff --git a/tests/eth2/core/beacon/conftest.py b/tests/eth2/core/beacon/conftest.py index 0db3a9e4fd..a586a3e3b5 100644 --- a/tests/eth2/core/beacon/conftest.py +++ b/tests/eth2/core/beacon/conftest.py @@ -572,9 +572,9 @@ def sample_state(sample_beacon_state_params): @pytest.fixture def sample_aggregate_and_proof_params(sample_attestation_params): return { - "index": 5, - "selection_proof": 1, + "aggregator_index": 5, "aggregate": Attestation(**sample_attestation_params), + "selection_proof": 1, } diff --git a/tests/eth2/core/beacon/tools/builder/test_aggregator.py b/tests/eth2/core/beacon/tools/builder/test_aggregator.py index 5a82d6c551..e5bc3c4d1a 100644 --- a/tests/eth2/core/beacon/tools/builder/test_aggregator.py +++ b/tests/eth2/core/beacon/tools/builder/test_aggregator.py @@ -9,8 +9,8 @@ from eth2.beacon.tools.builder.aggregator import ( TARGET_AGGREGATORS_PER_COMMITTEE, get_aggregate_from_valid_committee_attestations, + get_slot_signature, is_aggregator, - slot_signature, ) from eth2.beacon.tools.builder.validator import sign_transaction from eth2.beacon.types.attestations import Attestation @@ -34,7 +34,9 @@ def test_aggregator_selection(validator_count, privkeys, genesis_state, config): aggregator_count = 0 for index in range(validator_count): if index in committee: - signature = slot_signature(genesis_state, slot, privkeys[index], config) + signature = get_slot_signature( + genesis_state, slot, privkeys[index], config + ) attester_is_aggregator = is_aggregator( state, slot, committee_index, signature, config ) diff --git a/tests/eth2/core/beacon/types/test_aggregate_and_proof.py b/tests/eth2/core/beacon/types/test_aggregate_and_proof.py index bd58e0a6ad..fc2746a8c8 100644 --- a/tests/eth2/core/beacon/types/test_aggregate_and_proof.py +++ b/tests/eth2/core/beacon/types/test_aggregate_and_proof.py @@ -4,11 +4,13 @@ def test_defaults(sample_aggregate_and_proof_params): aggregate_and_proof = AggregateAndProof(**sample_aggregate_and_proof_params) - assert aggregate_and_proof.index == sample_aggregate_and_proof_params["index"] - assert ( - aggregate_and_proof.selection_proof - == sample_aggregate_and_proof_params["selection_proof"] + assert aggregate_and_proof.aggregator_index == ( + sample_aggregate_and_proof_params["aggregator_index"] ) assert ( aggregate_and_proof.aggregate == sample_aggregate_and_proof_params["aggregate"] ) + assert ( + aggregate_and_proof.selection_proof + == sample_aggregate_and_proof_params["selection_proof"] + ) diff --git a/trinity/components/eth2/beacon/validator.py b/trinity/components/eth2/beacon/validator.py index 3958735566..4f82fa5ada 100644 --- a/trinity/components/eth2/beacon/validator.py +++ b/trinity/components/eth2/beacon/validator.py @@ -36,8 +36,8 @@ ) from eth2.beacon.tools.builder.aggregator import ( get_aggregate_from_valid_committee_attestations, + get_slot_signature, is_aggregator, - slot_signature, ) from eth2.beacon.tools.builder.committee_assignment import ( CommitteeAssignment, @@ -377,8 +377,8 @@ def _get_local_attesters_at_assignment( """ for validator_index, (_, assignment) in self.local_validator_epoch_assignment.items(): if ( - assignment.slot == target_assignment.slot - and assignment.committee_index == target_assignment.committee_index + assignment.slot == target_assignment.slot and + assignment.committee_index == target_assignment.committee_index ): yield validator_index @@ -490,7 +490,7 @@ async def aggregate( # 2. For each attester for validator_index, privkey in attesting_validator_privkeys.items(): # Check if the vallidator is one of the aggregators - signature = slot_signature( + signature = get_slot_signature( state, slot, privkey, CommitteeConfig(config), ) is_aggregator_result = is_aggregator( @@ -514,7 +514,7 @@ async def aggregate( # (it's possible with same CommitteeIndex and different AttesatationData) for aggregate in aggregates: aggregate_and_proof = AggregateAndProof( - index=validator_index, + aggregator_index=validator_index, aggregate=aggregate, selection_proof=selected_proofs[validator_index], )