Search in sources :

Example 1 with AttestationProcessingResult

use of tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult in project teku by ConsenSys.

the class ForkChoiceUtil method validateOnAttestation.

public AttestationProcessingResult validateOnAttestation(final ReadOnlyForkChoiceStrategy forkChoiceStrategy, final UInt64 currentEpoch, final AttestationData attestationData) {
    final Checkpoint target = attestationData.getTarget();
    // Use GENESIS_EPOCH for previous when genesis to avoid underflow
    final UInt64 previousEpoch = currentEpoch.isGreaterThan(SpecConfig.GENESIS_EPOCH) ? currentEpoch.minus(UInt64.ONE) : SpecConfig.GENESIS_EPOCH;
    if (!target.getEpoch().equals(previousEpoch) && !target.getEpoch().equals(currentEpoch)) {
        return AttestationProcessingResult.invalid("Attestations must be from the current or previous epoch");
    }
    if (!target.getEpoch().equals(miscHelpers.computeEpochAtSlot(attestationData.getSlot()))) {
        return AttestationProcessingResult.invalid("Attestation slot must be within specified epoch");
    }
    if (!forkChoiceStrategy.contains(target.getRoot())) {
        // consideration until the block is found
        return AttestationProcessingResult.UNKNOWN_BLOCK;
    }
    final Optional<UInt64> blockSlot = forkChoiceStrategy.blockSlot(attestationData.getBeacon_block_root());
    if (blockSlot.isEmpty()) {
        // block is found
        return AttestationProcessingResult.UNKNOWN_BLOCK;
    }
    if (blockSlot.get().compareTo(attestationData.getSlot()) > 0) {
        return AttestationProcessingResult.invalid("Attestations must not be for blocks in the future. If not, the attestation should not be considered");
    }
    // LMD vote must be consistent with FFG vote target
    final UInt64 targetSlot = miscHelpers.computeStartSlotAtEpoch(target.getEpoch());
    if (getAncestor(forkChoiceStrategy, attestationData.getBeacon_block_root(), targetSlot).map(ancestorRoot -> !ancestorRoot.equals(target.getRoot())).orElse(true)) {
        return AttestationProcessingResult.invalid("LMD vote must be consistent with FFG vote target");
    }
    return AttestationProcessingResult.SUCCESSFUL;
}
Also used : AttestationProcessingResult(tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult) BeaconBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary) MutableStore(tech.pegasys.teku.spec.datastructures.forkchoice.MutableStore) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) Instant(java.time.Instant) NavigableMap(java.util.NavigableMap) CheckReturnValue(javax.annotation.CheckReturnValue) ReadOnlyStore(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyStore) TreeMap(java.util.TreeMap) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Optional(java.util.Optional) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) BlockImportResult(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 2 with AttestationProcessingResult

use of tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult 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)

Example 3 with AttestationProcessingResult

use of tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult in project teku by ConsenSys.

the class ForkChoice method onAttestation.

public SafeFuture<AttestationProcessingResult> onAttestation(final ValidateableAttestation attestation) {
    return recentChainData.retrieveCheckpointState(attestation.getData().getTarget()).thenCompose(maybeTargetState -> {
        final UpdatableStore store = recentChainData.getStore();
        final AttestationProcessingResult validationResult = spec.validateAttestation(store, attestation, maybeTargetState);
        if (!validationResult.isSuccessful()) {
            return SafeFuture.completedFuture(validationResult);
        }
        return onForkChoiceThread(() -> {
            final VoteUpdater transaction = recentChainData.startVoteUpdate();
            getForkChoiceStrategy().onAttestation(transaction, getIndexedAttestation(attestation));
            transaction.commit();
        }).thenApply(__ -> validationResult);
    }).exceptionallyCompose(error -> {
        final Throwable rootCause = Throwables.getRootCause(error);
        if (rootCause instanceof InvalidCheckpointException) {
            return SafeFuture.completedFuture(AttestationProcessingResult.invalid(rootCause.getMessage()));
        }
        return SafeFuture.failedFuture(error);
    });
}
Also used : StateRootCollector.addParentStateRoots(tech.pegasys.teku.statetransition.forkchoice.StateRootCollector.addParentStateRoots) AttestationProcessingResult(tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult) FailureReason(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason) ExceptionThrowingRunnable(tech.pegasys.teku.infrastructure.async.ExceptionThrowingRunnable) ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) UpdatableStore(tech.pegasys.teku.storage.store.UpdatableStore) IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) CapturingIndexedAttestationCache(tech.pegasys.teku.spec.cache.CapturingIndexedAttestationCache) ForkChoiceUtil(tech.pegasys.teku.spec.logic.common.util.ForkChoiceUtil) Subscribers(tech.pegasys.teku.infrastructure.subscribers.Subscribers) VoteUpdater(tech.pegasys.teku.spec.datastructures.forkchoice.VoteUpdater) InvalidCheckpointException(tech.pegasys.teku.spec.datastructures.forkchoice.InvalidCheckpointException) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ExecutionPayloadStatus(tech.pegasys.teku.spec.executionengine.ExecutionPayloadStatus) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SlotAndBlockRoot(tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot) Bytes32(org.apache.tuweni.bytes.Bytes32) FatalServiceFailureException(tech.pegasys.teku.infrastructure.exceptions.FatalServiceFailureException) ForkChoiceState(tech.pegasys.teku.spec.executionengine.ForkChoiceState) EventThread(tech.pegasys.teku.infrastructure.async.eventthread.EventThread) Throwables(com.google.common.base.Throwables) ExecutionEngineChannel(tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel) INTERVALS_PER_SLOT(tech.pegasys.teku.spec.constants.NetworkConstants.INTERVALS_PER_SLOT) P2P_LOG(tech.pegasys.teku.infrastructure.logging.P2PLogger.P2P_LOG) StoreTransaction(tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction) StateTransitionException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException) List(java.util.List) ForkChoiceStrategy(tech.pegasys.teku.ethereum.forkchoice.ForkChoiceStrategy) ExceptionUtil(tech.pegasys.teku.infrastructure.exceptions.ExceptionUtil) Logger(org.apache.logging.log4j.Logger) IndexedAttestationCache(tech.pegasys.teku.spec.cache.IndexedAttestationCache) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Optional(java.util.Optional) ExceptionThrowingSupplier(tech.pegasys.teku.infrastructure.async.ExceptionThrowingSupplier) LogManager(org.apache.logging.log4j.LogManager) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) PayloadStatus(tech.pegasys.teku.spec.executionengine.PayloadStatus) BlockImportResult(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) AttestationProcessingResult(tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult) UpdatableStore(tech.pegasys.teku.storage.store.UpdatableStore) VoteUpdater(tech.pegasys.teku.spec.datastructures.forkchoice.VoteUpdater) InvalidCheckpointException(tech.pegasys.teku.spec.datastructures.forkchoice.InvalidCheckpointException)

Example 4 with AttestationProcessingResult

use of tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult in project teku by ConsenSys.

the class ForkChoiceTest method onAttestation_shouldBeInvalidWhenInvalidCheckpointThrown.

@Test
void onAttestation_shouldBeInvalidWhenInvalidCheckpointThrown() {
    final SignedBlockAndState targetBlock = chainBuilder.generateBlockAtSlot(5);
    importBlock(targetBlock);
    // Attestation where the target checkpoint has a slot prior to the block it references
    final Checkpoint targetCheckpoint = new Checkpoint(ZERO, targetBlock.getRoot());
    final Attestation attestation = attestationSchema.create(attestationSchema.getAggregationBitsSchema().ofBits(5), new AttestationData(targetBlock.getSlot(), spec.computeEpochAtSlot(targetBlock.getSlot()), targetBlock.getRoot(), targetBlock.getState().getCurrent_justified_checkpoint(), targetCheckpoint), BLSSignature.empty());
    final SafeFuture<AttestationProcessingResult> result = forkChoice.onAttestation(ValidateableAttestation.from(spec, attestation));
    assertThat(result).isCompletedWithValue(AttestationProcessingResult.invalid(String.format("Checkpoint state (%s) must be at or prior to checkpoint slot boundary (%s)", targetBlock.getSlot(), targetCheckpoint.getEpochStartSlot(spec))));
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) AttestationProcessingResult(tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Test(org.junit.jupiter.api.Test)

Aggregations

ValidateableAttestation (tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)4 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)4 AttestationProcessingResult (tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult)4 Bytes32 (org.apache.tuweni.bytes.Bytes32)3 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)3 AttestationData (tech.pegasys.teku.spec.datastructures.operations.AttestationData)3 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 List (java.util.List)2 Optional (java.util.Optional)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)2 BeaconBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary)2 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)2 ReadOnlyForkChoiceStrategy (tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy)2 IndexedAttestation (tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation)2 Fork (tech.pegasys.teku.spec.datastructures.state.Fork)2 BeaconStateAccessors (tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors)2