Пример #1
0
    async def aggregate(self, slot: Slot) -> Tuple[AggregateAndProof, ...]:
        """
        Aggregate the attestations at ``slot`` and broadcast them.
        """
        # Check the aggregators selection
        aggregate_and_proofs: Tuple[AggregateAndProof, ...] = ()
        state_machine = self.chain.get_state_machine()
        state = self.chain.get_head_state()
        config = state_machine.config

        attesting_committee_assignments_at_slot = self._get_attesting_assignments_at_slot(
            slot)
        # 1. For each committee_assignment at the given slot
        for committee_assignment in attesting_committee_assignments_at_slot:
            committee_index = committee_assignment.committee_index

            local_attesters = self._get_local_attesters_at_assignment(
                committee_assignment)
            # Get the validator_index -> privkey map of the attesting validators
            attesting_validator_privkeys = {
                index: self.validator_privkeys[index]
                for index in local_attesters
            }

            selected_proofs: Dict[ValidatorIndex, BLSSignature] = {}
            # 2. For each attester
            for validator_index, privkey in attesting_validator_privkeys.items(
            ):
                # Check if the vallidator is one of the aggregators
                signature = get_slot_signature(state, slot, privkey,
                                               CommitteeConfig(config))
                is_aggregator_result = is_aggregator(state, slot,
                                                     committee_index,
                                                     signature,
                                                     CommitteeConfig(config))
                if is_aggregator_result:
                    self.logger.debug(
                        f"validator ({validator_index}) is aggregator of"
                        f" committee_index={committee_index} at slot={slot}")
                    selected_proofs[validator_index] = signature
                else:
                    continue

                aggregates = self._get_aggregates(slot, committee_index,
                                                  config)
                # 3. For each aggregate
                # (it's possible with same CommitteeIndex and different AttesatationData)
                for aggregate in aggregates:
                    aggregate_and_proof = AggregateAndProof.create(
                        aggregator_index=validator_index,
                        aggregate=aggregate,
                        selection_proof=selected_proofs[validator_index],
                    )
                    self.import_attestation(aggregate_and_proof.aggregate,
                                            True)
                    await self.p2p_node.broadcast_beacon_aggregate_and_proof(
                        aggregate_and_proof)
                    aggregate_and_proofs += (aggregate_and_proof, )

        return aggregate_and_proofs
Пример #2
0
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.aggregate ==
            sample_aggregate_and_proof_params["aggregate"])