Search in sources :

Example 1 with FailureReason

use of tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason in project teku by ConsenSys.

the class PeerSync method handleFailedRequestToPeer.

private PeerSyncResult handleFailedRequestToPeer(Eth2Peer peer, final PeerStatus peerStatus, Throwable err) {
    Throwable rootException = Throwables.getRootCause(err);
    if (rootException instanceof FailedBlockImportException) {
        final FailedBlockImportException importException = (FailedBlockImportException) rootException;
        final FailureReason reason = importException.getResult().getFailureReason();
        final SignedBeaconBlock block = importException.getBlock();
        if (reason.equals(FailureReason.UNKNOWN_PARENT) && !hasPeerFinalizedBlock(block, peerStatus)) {
            // We received a block that doesn't connect to our chain.
            // This can happen if our peer is sending us blocks from the non-final portion of their
            // chain. They may be sending us blocks from a stale fork that we have already pruned out of
            // our Store.
            LOG.debug("Failed to import non-final block from peer (err: {}) {}: {}", reason, block, peer);
            return PeerSyncResult.IMPORT_FAILED;
        } else if (BAD_BLOCK_FAILURE_REASONS.contains(reason)) {
            LOG.warn("Failed to import block from peer (err: {}) {}: {}", reason, block, peer);
            LOG.debug("Disconnecting from peer ({}) who sent invalid block ({}): {}", peer, reason.name(), block);
            disconnectFromPeer(peer);
            return PeerSyncResult.BAD_BLOCK;
        } else {
            LOG.warn("Failed to import block from peer (err: {}) {}: {}", reason, block, peer);
            return PeerSyncResult.IMPORT_FAILED;
        }
    }
    if (rootException instanceof CancellationException) {
        return PeerSyncResult.CANCELLED;
    }
    if (rootException instanceof BlocksByRangeResponseInvalidResponseException || rootException instanceof RpcException) {
        disconnectFromPeer(peer);
        return PeerSyncResult.INVALID_RESPONSE;
    }
    if (err instanceof RuntimeException) {
        throw (RuntimeException) err;
    } else {
        throw new RuntimeException("Unhandled error while syncing", err);
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) FailureReason(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) BlocksByRangeResponseInvalidResponseException(tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BlocksByRangeResponseInvalidResponseException) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)

Example 2 with FailureReason

use of tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason in project teku by ConsenSys.

the class ValidatorDataProviderTest method submitSignedBlock_shouldReturn202ForInvalidBlock.

@TestTemplate
public void submitSignedBlock_shouldReturn202ForInvalidBlock() {
    final SignedBeaconBlock internalSignedBeaconBlock = dataStructureUtil.randomSignedBeaconBlock(1);
    final tech.pegasys.teku.api.schema.SignedBeaconBlock signedBeaconBlock = tech.pegasys.teku.api.schema.SignedBeaconBlock.create(internalSignedBeaconBlock);
    final AtomicInteger failReasonCount = new AtomicInteger();
    Stream.of(FailureReason.values()).filter(failureReason -> !failureReason.equals(FailureReason.INTERNAL_ERROR)).forEach(failureReason -> {
        failReasonCount.getAndIncrement();
        final SafeFuture<SendSignedBlockResult> failImportResult = completedFuture(SendSignedBlockResult.notImported(failureReason.name()));
        when(validatorApiChannel.sendSignedBlock(any())).thenReturn(failImportResult);
        final SafeFuture<ValidatorBlockResult> validatorBlockResultSafeFuture = provider.submitSignedBlock(signedBeaconBlock);
        try {
            assertThat(validatorBlockResultSafeFuture.get().getResponseCode()).isEqualTo(202);
        } catch (final Exception e) {
            fail("Exception while executing test.");
        }
    });
    // Assert that the check has run over each FailureReason except the 500.
    assertThat(failReasonCount.get()).isEqualTo(FailureReason.values().length - 1);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SignedBeaconBlockAltair(tech.pegasys.teku.api.schema.altair.SignedBeaconBlockAltair) SignedBeaconBlockBellatrix(tech.pegasys.teku.api.schema.bellatrix.SignedBeaconBlockBellatrix) CombinedChainDataClient(tech.pegasys.teku.storage.client.CombinedChainDataClient) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestSpecContext(tech.pegasys.teku.spec.TestSpecContext) Attestation(tech.pegasys.teku.api.schema.Attestation) ONE(tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE) SafeFutureAssert.assertThatSafeFuture(tech.pegasys.teku.infrastructure.async.SafeFutureAssert.assertThatSafeFuture) AttesterDuties(tech.pegasys.teku.validator.api.AttesterDuties) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SszDataAssert.assertThatSszData(tech.pegasys.teku.infrastructure.ssz.SszDataAssert.assertThatSszData) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) JsonProvider(tech.pegasys.teku.provider.JsonProvider) BLSPubKey(tech.pegasys.teku.api.schema.BLSPubKey) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.api.schema.BLSSignature) SC_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST) SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) Collections.emptyList(java.util.Collections.emptyList) PostDataFailure(tech.pegasys.teku.api.response.v1.beacon.PostDataFailure) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) Stream(java.util.stream.Stream) ValidatorApiChannel(tech.pegasys.teku.validator.api.ValidatorApiChannel) PostDataFailureResponse(tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Optional(java.util.Optional) SpecContext(tech.pegasys.teku.spec.TestSpecInvocationContextProvider.SpecContext) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SubmitDataError(tech.pegasys.teku.validator.api.SubmitDataError) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FailureReason(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) SafeFuture.completedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture) TestTemplate(org.junit.jupiter.api.TestTemplate) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PostAttesterDutiesResponse(tech.pegasys.teku.api.response.v1.validator.PostAttesterDutiesResponse) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ChainDataUnavailableException(tech.pegasys.teku.storage.client.ChainDataUnavailableException) ZERO(tech.pegasys.teku.infrastructure.unsigned.UInt64.ZERO) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SafeFutureAssert(tech.pegasys.teku.infrastructure.async.SafeFutureAssert) BeaconBlock(tech.pegasys.teku.api.schema.BeaconBlock) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Mockito.when(org.mockito.Mockito.when) SignedBeaconBlockPhase0(tech.pegasys.teku.api.schema.phase0.SignedBeaconBlockPhase0) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) IntList(it.unimi.dsi.fastutil.ints.IntList) BLSTestUtil(tech.pegasys.teku.bls.BLSTestUtil) AttesterDuty(tech.pegasys.teku.validator.api.AttesterDuty) SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) ChainDataUnavailableException(tech.pegasys.teku.storage.client.ChainDataUnavailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)2 FailureReason (tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Optional (java.util.Optional)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Stream (java.util.stream.Stream)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 Assertions.fail (org.assertj.core.api.Assertions.fail)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 TestTemplate (org.junit.jupiter.api.TestTemplate)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1