Search in sources :

Example 1 with BeaconStateAccessors

use of tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors in project teku by ConsenSys.

the class SpecLogicPhase0 method create.

public static SpecLogicPhase0 create(final SpecConfig config, final SchemaDefinitions schemaDefinitions) {
    // Helpers
    final Predicates predicates = new Predicates();
    final MiscHelpers miscHelpers = new MiscHelpers(config);
    final BeaconStateAccessors beaconStateAccessors = new BeaconStateAccessorsPhase0(config, predicates, miscHelpers);
    final BeaconStateMutators beaconStateMutators = new BeaconStateMutators(config, miscHelpers, beaconStateAccessors);
    // Operation validaton
    final OperationSignatureVerifier operationSignatureVerifier = new OperationSignatureVerifier(miscHelpers, beaconStateAccessors);
    // Util
    final ValidatorsUtil validatorsUtil = new ValidatorsUtil(config, miscHelpers, beaconStateAccessors);
    final BeaconStateUtil beaconStateUtil = new BeaconStateUtil(config, schemaDefinitions, predicates, miscHelpers, beaconStateAccessors);
    final AttestationUtil attestationUtil = new AttestationUtil(schemaDefinitions, beaconStateAccessors, miscHelpers);
    final OperationValidator operationValidator = OperationValidator.create(config, predicates, miscHelpers, beaconStateAccessors, attestationUtil);
    final ValidatorStatusFactoryPhase0 validatorStatusFactory = new ValidatorStatusFactoryPhase0(config, beaconStateUtil, attestationUtil, beaconStateAccessors, predicates);
    final EpochProcessorPhase0 epochProcessor = new EpochProcessorPhase0(config, miscHelpers, beaconStateAccessors, beaconStateMutators, validatorsUtil, beaconStateUtil, validatorStatusFactory, schemaDefinitions);
    final BlockProcessorPhase0 blockProcessor = new BlockProcessorPhase0(config, predicates, miscHelpers, beaconStateAccessors, beaconStateMutators, operationSignatureVerifier, beaconStateUtil, attestationUtil, validatorsUtil, operationValidator);
    final ForkChoiceUtil forkChoiceUtil = new ForkChoiceUtil(config, beaconStateAccessors, attestationUtil, miscHelpers);
    final BlockProposalUtil blockProposalUtil = new BlockProposalUtil(schemaDefinitions, blockProcessor);
    return new SpecLogicPhase0(predicates, miscHelpers, beaconStateAccessors, beaconStateMutators, operationSignatureVerifier, validatorsUtil, beaconStateUtil, attestationUtil, operationValidator, validatorStatusFactory, epochProcessor, blockProcessor, forkChoiceUtil, blockProposalUtil);
}
Also used : ForkChoiceUtil(tech.pegasys.teku.spec.logic.common.util.ForkChoiceUtil) AttestationUtil(tech.pegasys.teku.spec.logic.common.util.AttestationUtil) EpochProcessorPhase0(tech.pegasys.teku.spec.logic.versions.phase0.statetransition.epoch.EpochProcessorPhase0) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) OperationSignatureVerifier(tech.pegasys.teku.spec.logic.common.operations.OperationSignatureVerifier) ValidatorsUtil(tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil) ValidatorStatusFactoryPhase0(tech.pegasys.teku.spec.logic.versions.phase0.statetransition.epoch.ValidatorStatusFactoryPhase0) OperationValidator(tech.pegasys.teku.spec.logic.common.operations.validation.OperationValidator) BeaconStateUtil(tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil) BeaconStateMutators(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators) BlockProposalUtil(tech.pegasys.teku.spec.logic.common.util.BlockProposalUtil) Predicates(tech.pegasys.teku.spec.logic.common.helpers.Predicates) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) BlockProcessorPhase0(tech.pegasys.teku.spec.logic.versions.phase0.block.BlockProcessorPhase0) BeaconStateAccessorsPhase0(tech.pegasys.teku.spec.logic.versions.phase0.helpers.BeaconStateAccessorsPhase0)

Example 2 with BeaconStateAccessors

use of tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors in project teku by ConsenSys.

the class SignedContributionAndProofValidator method validateWithState.

private SafeFuture<InternalValidationResult> validateWithState(final SignedContributionAndProof proof, final ContributionAndProof contributionAndProof, final SyncCommitteeContribution contribution, final SyncCommitteeUtil syncCommitteeUtil, final UniquenessKey uniquenessKey, final BeaconStateAltair state) {
    final BeaconStateAccessors beaconStateAccessors = spec.atSlot(contribution.getSlot()).beaconStateAccessors();
    final Optional<BLSPublicKey> aggregatorPublicKey = beaconStateAccessors.getValidatorPubKey(state, contributionAndProof.getAggregatorIndex());
    if (aggregatorPublicKey.isEmpty()) {
        return futureFailureResult("Rejecting proof because aggregator index %s is an unknown validator", contributionAndProof.getAggregatorIndex());
    }
    final UInt64 contributionEpoch = syncCommitteeUtil.getEpochForDutiesAtSlot(contribution.getSlot());
    // state.current_sync_committee.pubkeys.
    if (!isInSyncSubcommittee(syncCommitteeUtil, contribution, state, contributionEpoch, contributionAndProof.getAggregatorIndex())) {
        return futureFailureResult("Rejecting proof because aggregator index %s is not in the current sync subcommittee", contributionAndProof.getAggregatorIndex());
    }
    // contribution.slot, contribution_and_proof.selection_proof) returns True.
    if (!syncCommitteeUtil.isSyncCommitteeAggregator(contributionAndProof.getSelectionProof())) {
        return futureFailureResult("Rejecting proof because selection proof %s is not an aggregator", contributionAndProof.getSelectionProof());
    }
    final AsyncBatchBLSSignatureVerifier signatureVerifier = new AsyncBatchBLSSignatureVerifier(this.signatureVerifier);
    // [REJECT] The contribution_and_proof.selection_proof is a valid signature of the
    // contribution.slot by the validator with index
    // contribution_and_proof.aggregator_index.
    final Bytes signingRoot = syncCommitteeUtil.getSyncAggregatorSelectionDataSigningRoot(syncCommitteeUtil.createSyncAggregatorSelectionData(contribution.getSlot(), contribution.getSubcommitteeIndex()), state.getForkInfo());
    if (!signatureVerifier.verify(aggregatorPublicKey.get(), signingRoot, contributionAndProof.getSelectionProof())) {
        return futureFailureResult("Rejecting proof at slot %s for subcommittee index %s because selection proof is invalid", contribution.getSlot(), contribution.getSubcommitteeIndex());
    }
    // valid.
    if (!signatureVerifier.verify(aggregatorPublicKey.get(), syncCommitteeUtil.getContributionAndProofSigningRoot(state, contributionAndProof), proof.getSignature())) {
        return futureFailureResult("Rejecting proof %s because aggregator signature is invalid", proof.getSignature());
    }
    final SpecConfigAltair config = SpecConfigAltair.required(spec.getSpecConfig(contributionEpoch));
    final SyncCommittee syncCommittee = syncCommitteeUtil.getSyncCommittee(state, contributionEpoch);
    final int subcommitteeSize = config.getSyncCommitteeSize() / SYNC_COMMITTEE_SUBNET_COUNT;
    // [REJECT] The aggregate signature is valid for the message beacon_block_root and
    // aggregate pubkey derived from the participation info in aggregation_bits for the
    // subcommittee specified by the subcommittee_index.
    final List<BLSPublicKey> contributorPublicKeys = contribution.getAggregationBits().streamAllSetBits().mapToObj(participantIndex -> getParticipantPublicKey(state, syncCommittee, contribution, subcommitteeSize, participantIndex)).collect(Collectors.toList());
    if (!signatureVerifier.verify(contributorPublicKeys, syncCommitteeUtil.getSyncCommitteeMessageSigningRoot(contribution.getBeaconBlockRoot(), contributionEpoch, state.getForkInfo()), contribution.getSignature())) {
        return futureFailureResult("Rejecting proof because aggregate signature %s is invalid", contribution.getSignature());
    }
    return signatureVerifier.batchVerify().thenApply(signatureValid -> {
        if (!signatureValid) {
            return failureResult("Rejecting proof with signature %s because batch signature check failed", contribution.getSignature());
        }
        if (!seenIndices.add(uniquenessKey)) {
            // Got added by another thread while we were validating it
            return IGNORE;
        }
        return ACCEPT;
    });
}
Also used : AsyncBLSSignatureVerifier(tech.pegasys.teku.spec.logic.common.util.AsyncBLSSignatureVerifier) TimeProvider(tech.pegasys.teku.infrastructure.time.TimeProvider) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) IGNORE(tech.pegasys.teku.statetransition.validation.InternalValidationResult.IGNORE) Bytes(org.apache.tuweni.bytes.Bytes) ContributionAndProof(tech.pegasys.teku.spec.datastructures.operations.versions.altair.ContributionAndProof) SYNC_COMMITTEE_SUBNET_COUNT(tech.pegasys.teku.spec.constants.NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT) LimitedSet(tech.pegasys.teku.infrastructure.collections.LimitedSet) FormatMethod(com.google.errorprone.annotations.FormatMethod) SyncCommitteeUtil(tech.pegasys.teku.spec.logic.common.util.SyncCommitteeUtil) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) ACCEPT(tech.pegasys.teku.statetransition.validation.InternalValidationResult.ACCEPT) SyncCommitteeContribution(tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeContribution) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) Set(java.util.Set) ValidationResultCode(tech.pegasys.teku.statetransition.validation.ValidationResultCode) Collectors(java.util.stream.Collectors) SignedContributionAndProof(tech.pegasys.teku.spec.datastructures.operations.versions.altair.SignedContributionAndProof) Objects(java.util.Objects) SyncCommittee(tech.pegasys.teku.spec.datastructures.state.SyncCommittee) List(java.util.List) VALID_CONTRIBUTION_AND_PROOF_SET_SIZE(tech.pegasys.teku.spec.config.Constants.VALID_CONTRIBUTION_AND_PROOF_SET_SIZE) Logger(org.apache.logging.log4j.Logger) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) Optional(java.util.Optional) AsyncBatchBLSSignatureVerifier(tech.pegasys.teku.spec.logic.common.util.AsyncBatchBLSSignatureVerifier) BeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair) LogManager(org.apache.logging.log4j.LogManager) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) SpecConfigAltair(tech.pegasys.teku.spec.config.SpecConfigAltair) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Bytes(org.apache.tuweni.bytes.Bytes) AsyncBatchBLSSignatureVerifier(tech.pegasys.teku.spec.logic.common.util.AsyncBatchBLSSignatureVerifier) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SyncCommittee(tech.pegasys.teku.spec.datastructures.state.SyncCommittee) SpecConfigAltair(tech.pegasys.teku.spec.config.SpecConfigAltair)

Aggregations

BeaconStateAccessors (tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors)2 FormatMethod (com.google.errorprone.annotations.FormatMethod)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Bytes (org.apache.tuweni.bytes.Bytes)1 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)1 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)1 LimitedSet (tech.pegasys.teku.infrastructure.collections.LimitedSet)1 TimeProvider (tech.pegasys.teku.infrastructure.time.TimeProvider)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 Spec (tech.pegasys.teku.spec.Spec)1 VALID_CONTRIBUTION_AND_PROOF_SET_SIZE (tech.pegasys.teku.spec.config.Constants.VALID_CONTRIBUTION_AND_PROOF_SET_SIZE)1 SpecConfigAltair (tech.pegasys.teku.spec.config.SpecConfigAltair)1 SYNC_COMMITTEE_SUBNET_COUNT (tech.pegasys.teku.spec.constants.NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT)1 ContributionAndProof (tech.pegasys.teku.spec.datastructures.operations.versions.altair.ContributionAndProof)1