Search in sources :

Example 1 with Domain

use of tech.pegasys.teku.spec.constants.Domain in project teku by ConsenSys.

the class BlockProcessorAltair method processSyncAggregate.

@Override
public void processSyncAggregate(final MutableBeaconState baseState, final SyncAggregate aggregate, final BLSSignatureVerifier signatureVerifier) throws BlockProcessingException {
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(baseState);
    final List<BLSPublicKey> participantPubkeys = new ArrayList<>();
    final List<BLSPublicKey> idlePubkeys = new ArrayList<>();
    for (int i = 0; i < specConfigAltair.getSyncCommitteeSize(); i++) {
        final BLSPublicKey publicKey = syncCommitteeUtil.getCurrentSyncCommitteeParticipantPubKey(state, i);
        if (aggregate.getSyncCommitteeBits().getBit(i)) {
            participantPubkeys.add(publicKey);
        } else {
            idlePubkeys.add(publicKey);
        }
    }
    final UInt64 previousSlot = state.getSlot().minusMinZero(1);
    final Bytes32 domain = beaconStateAccessors.getDomain(state.getForkInfo(), Domain.SYNC_COMMITTEE, miscHelpers.computeEpochAtSlot(previousSlot));
    final Bytes32 signingRoot = miscHelpersAltair.computeSigningRoot(beaconStateAccessors.getBlockRootAtSlot(state, previousSlot), domain);
    if (!eth2FastAggregateVerify(signatureVerifier, participantPubkeys, signingRoot, aggregate.getSyncCommitteeSignature().getSignature())) {
        throw new BlockProcessingException("Invalid sync committee signature in " + aggregate);
    }
    // Compute participant and proposer rewards
    final UInt64 totalActiveIncrements = beaconStateAccessors.getTotalActiveBalance(state).dividedBy(specConfig.getEffectiveBalanceIncrement());
    final UInt64 totalBaseRewards = beaconStateAccessorsAltair.getBaseRewardPerIncrement(state).times(totalActiveIncrements);
    final UInt64 maxParticipantRewards = totalBaseRewards.times(SYNC_REWARD_WEIGHT).dividedBy(WEIGHT_DENOMINATOR).dividedBy(specConfig.getSlotsPerEpoch());
    final UInt64 participantReward = maxParticipantRewards.dividedBy(specConfigAltair.getSyncCommitteeSize());
    final UInt64 proposerReward = participantReward.times(PROPOSER_WEIGHT).dividedBy(WEIGHT_DENOMINATOR.minus(PROPOSER_WEIGHT));
    // Apply participant and proposer rewards
    participantPubkeys.stream().map(pubkey -> validatorsUtil.getValidatorIndex(state, pubkey).orElseThrow()).forEach(participantIndex -> beaconStateMutators.increaseBalance(state, participantIndex, participantReward));
    UInt64 totalProposerReward = proposerReward.times(participantPubkeys.size());
    beaconStateMutators.increaseBalance(state, beaconStateAccessors.getBeaconProposerIndex(state), totalProposerReward);
    // impose penalties for any idle validators
    idlePubkeys.stream().map(pubkey -> validatorsUtil.getValidatorIndex(state, pubkey).orElseThrow()).forEach(participantIndex -> beaconStateMutators.decreaseBalance(state, participantIndex, participantReward));
}
Also used : BeaconBlockBodyAltair(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.BeaconBlockBodyAltair) MiscHelpersAltair(tech.pegasys.teku.spec.logic.versions.altair.helpers.MiscHelpersAltair) AbstractBlockProcessor(tech.pegasys.teku.spec.logic.common.block.AbstractBlockProcessor) PARTICIPATION_FLAG_WEIGHTS(tech.pegasys.teku.spec.logic.versions.altair.helpers.MiscHelpersAltair.PARTICIPATION_FLAG_WEIGHTS) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) BLSSignatureVerifier(tech.pegasys.teku.bls.BLSSignatureVerifier) ArrayList(java.util.ArrayList) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) ExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload) SyncCommitteeUtil(tech.pegasys.teku.spec.logic.common.util.SyncCommitteeUtil) SYNC_REWARD_WEIGHT(tech.pegasys.teku.spec.constants.IncentivizationWeights.SYNC_REWARD_WEIGHT) WEIGHT_DENOMINATOR(tech.pegasys.teku.spec.constants.IncentivizationWeights.WEIGHT_DENOMINATOR) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconStateUtil(tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) OperationValidator(tech.pegasys.teku.spec.logic.common.operations.validation.OperationValidator) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) Domain(tech.pegasys.teku.spec.constants.Domain) BeaconStateAccessorsAltair(tech.pegasys.teku.spec.logic.versions.altair.helpers.BeaconStateAccessorsAltair) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) OptimisticExecutionPayloadExecutor(tech.pegasys.teku.spec.logic.versions.bellatrix.block.OptimisticExecutionPayloadExecutor) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) ValidatorsUtil(tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) Predicates(tech.pegasys.teku.spec.logic.common.helpers.Predicates) MutableBeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState) List(java.util.List) IndexedAttestationCache(tech.pegasys.teku.spec.cache.IndexedAttestationCache) AttestationUtil(tech.pegasys.teku.spec.logic.common.util.AttestationUtil) SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) BeaconStateMutators(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators) SszMutableList(tech.pegasys.teku.infrastructure.ssz.SszMutableList) SpecConfigAltair(tech.pegasys.teku.spec.config.SpecConfigAltair) PROPOSER_WEIGHT(tech.pegasys.teku.spec.constants.IncentivizationWeights.PROPOSER_WEIGHT) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) SyncAggregate(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.SyncAggregate) OperationSignatureVerifier(tech.pegasys.teku.spec.logic.common.operations.OperationSignatureVerifier) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) ArrayList(java.util.ArrayList) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 2 with Domain

use of tech.pegasys.teku.spec.constants.Domain in project teku by ConsenSys.

the class BlockValidator method blockSignatureIsValidWithRespectToProposerIndex.

private boolean blockSignatureIsValidWithRespectToProposerIndex(SignedBeaconBlock block, BeaconState postState) {
    final Bytes32 domain = spec.getDomain(Domain.BEACON_PROPOSER, spec.getCurrentEpoch(postState), postState.getFork(), postState.getGenesis_validators_root());
    final Bytes signing_root = spec.computeSigningRoot(block.getMessage(), domain);
    final BLSSignature signature = block.getSignature();
    boolean signatureValid = spec.getValidatorPubKey(postState, block.getMessage().getProposerIndex()).map(publicKey -> BLS.verify(publicKey, signing_root, signature)).orElse(false);
    return signatureValid && receivedValidBlockInfoSet.add(new SlotAndProposer(block));
}
Also used : MAXIMUM_GOSSIP_CLOCK_DISPARITY(tech.pegasys.teku.spec.config.Constants.MAXIMUM_GOSSIP_CLOCK_DISPARITY) BLS(tech.pegasys.teku.bls.BLS) BLSSignature(tech.pegasys.teku.bls.BLSSignature) VALID_BLOCK_SET_SIZE(tech.pegasys.teku.spec.config.Constants.VALID_BLOCK_SET_SIZE) Set(java.util.Set) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) Bytes(org.apache.tuweni.bytes.Bytes) SafeFuture.completedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture) LimitedSet(tech.pegasys.teku.infrastructure.collections.LimitedSet) ReadOnlyStore(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyStore) InternalValidationResult.reject(tech.pegasys.teku.statetransition.validation.InternalValidationResult.reject) Logger(org.apache.logging.log4j.Logger) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) ExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload) Optional(java.util.Optional) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Objects(com.google.common.base.Objects) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SlotAndBlockRoot(tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot) LogManager(org.apache.logging.log4j.LogManager) Bytes32(org.apache.tuweni.bytes.Bytes32) Domain(tech.pegasys.teku.spec.constants.Domain) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Bytes(org.apache.tuweni.bytes.Bytes) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.bls.BLSSignature)

Example 3 with Domain

use of tech.pegasys.teku.spec.constants.Domain in project teku by ConsenSys.

the class AttestationUtil method isValidIndexedAttestationAsync.

public SafeFuture<AttestationProcessingResult> isValidIndexedAttestationAsync(Fork fork, BeaconState state, IndexedAttestation indexed_attestation, AsyncBLSSignatureVerifier signatureVerifier) {
    SszUInt64List indices = indexed_attestation.getAttesting_indices();
    if (indices.isEmpty() || !Comparators.isInStrictOrder(indices.asListUnboxed(), Comparator.naturalOrder())) {
        return completedFuture(AttestationProcessingResult.invalid("Attesting indices are not sorted"));
    }
    List<BLSPublicKey> pubkeys = indices.streamUnboxed().flatMap(i -> beaconStateAccessors.getValidatorPubKey(state, i).stream()).collect(toList());
    if (pubkeys.size() < indices.size()) {
        return completedFuture(AttestationProcessingResult.invalid("Attesting indices include non-existent validator"));
    }
    BLSSignature signature = indexed_attestation.getSignature();
    Bytes32 domain = beaconStateAccessors.getDomain(Domain.BEACON_ATTESTER, indexed_attestation.getData().getTarget().getEpoch(), fork, state.getGenesis_validators_root());
    Bytes signing_root = miscHelpers.computeSigningRoot(indexed_attestation.getData(), domain);
    return signatureVerifier.verify(pubkeys, signing_root, signature).thenApply(isValidSignature -> {
        if (isValidSignature) {
            return AttestationProcessingResult.SUCCESSFUL;
        } else {
            LOG.debug("AttestationUtil.is_valid_indexed_attestation: Verify aggregate signature");
            return AttestationProcessingResult.invalid("Signature is invalid");
        }
    });
}
Also used : IntStream(java.util.stream.IntStream) AttestationProcessingResult(tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult) Comparators(com.google.common.collect.Comparators) BeaconBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary) SszBitlist(tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist) IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) Bytes(org.apache.tuweni.bytes.Bytes) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) SafeFuture.completedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture) BLSSignatureVerifier(tech.pegasys.teku.bls.BLSSignatureVerifier) IndexedAttestationSchema(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation.IndexedAttestationSchema) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Domain(tech.pegasys.teku.spec.constants.Domain) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) SchemaDefinitions(tech.pegasys.teku.spec.schemas.SchemaDefinitions) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) IntList(it.unimi.dsi.fastutil.ints.IntList) Logger(org.apache.logging.log4j.Logger) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Bytes(org.apache.tuweni.bytes.Bytes) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.bls.BLSSignature)

Aggregations

Bytes32 (org.apache.tuweni.bytes.Bytes32)3 BLSSignature (tech.pegasys.teku.bls.BLSSignature)3 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 Domain (tech.pegasys.teku.spec.constants.Domain)3 List (java.util.List)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Bytes (org.apache.tuweni.bytes.Bytes)2 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)2 BLSSignatureVerifier (tech.pegasys.teku.bls.BLSSignatureVerifier)2 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)2 SafeFuture.completedFuture (tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture)2 SszUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List)2 ExecutionPayload (tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload)2 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)2 AttestationData (tech.pegasys.teku.spec.datastructures.operations.AttestationData)2 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)2 Objects (com.google.common.base.Objects)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Comparators (com.google.common.collect.Comparators)1