Search in sources :

Example 1 with SendSignedBlockResult

use of tech.pegasys.teku.validator.api.SendSignedBlockResult 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)

Example 2 with SendSignedBlockResult

use of tech.pegasys.teku.validator.api.SendSignedBlockResult in project teku by ConsenSys.

the class ValidatorDataProviderTest method submitSignedBlock_shouldReturn500ForInternalError.

@TestTemplate
public void submitSignedBlock_shouldReturn500ForInternalError() throws ExecutionException, InterruptedException {
    final SignedBeaconBlock internalSignedBeaconBlock = dataStructureUtil.randomSignedBeaconBlock(1);
    final tech.pegasys.teku.api.schema.SignedBeaconBlock signedBeaconBlock = tech.pegasys.teku.api.schema.SignedBeaconBlock.create(internalSignedBeaconBlock);
    final SafeFuture<SendSignedBlockResult> failImportResult = completedFuture(SendSignedBlockResult.rejected(FailureReason.INTERNAL_ERROR.name()));
    when(validatorApiChannel.sendSignedBlock(any())).thenReturn(failImportResult);
    final SafeFuture<ValidatorBlockResult> validatorBlockResultSafeFuture = provider.submitSignedBlock(signedBeaconBlock);
    assertThat(validatorBlockResultSafeFuture.get().getResponseCode()).isEqualTo(500);
}
Also used : SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 3 with SendSignedBlockResult

use of tech.pegasys.teku.validator.api.SendSignedBlockResult in project teku by ConsenSys.

the class ValidatorDataProviderTest method submitSignedBlock_shouldReturn200ForSuccess.

@TestTemplate
public void submitSignedBlock_shouldReturn200ForSuccess() throws ExecutionException, InterruptedException {
    final SignedBeaconBlock internalSignedBeaconBlock = dataStructureUtil.randomSignedBeaconBlock(1);
    final tech.pegasys.teku.api.schema.SignedBeaconBlock signedBeaconBlock = tech.pegasys.teku.api.schema.SignedBeaconBlock.create(internalSignedBeaconBlock);
    final SafeFuture<SendSignedBlockResult> successImportResult = completedFuture(SendSignedBlockResult.success(internalSignedBeaconBlock.getRoot()));
    when(validatorApiChannel.sendSignedBlock(any())).thenReturn(successImportResult);
    final SafeFuture<ValidatorBlockResult> validatorBlockResultSafeFuture = provider.submitSignedBlock(signedBeaconBlock);
    assertThat(validatorBlockResultSafeFuture.get().getResponseCode()).isEqualTo(200);
}
Also used : SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 4 with SendSignedBlockResult

use of tech.pegasys.teku.validator.api.SendSignedBlockResult in project teku by ConsenSys.

the class ValidatorApiHandlerTest method sendSignedBlock_shouldConvertKnownBlockResult.

@Test
public void sendSignedBlock_shouldConvertKnownBlockResult() {
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(5);
    when(blockImportChannel.importBlock(block)).thenReturn(SafeFuture.completedFuture(BlockImportResult.knownBlock(block)));
    final SafeFuture<SendSignedBlockResult> result = validatorApiHandler.sendSignedBlock(block);
    verify(blockGossipChannel).publishBlock(block);
    verify(blockImportChannel).importBlock(block);
    assertThat(result).isCompletedWithValue(SendSignedBlockResult.success(block.getRoot()));
}
Also used : SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Test(org.junit.jupiter.api.Test)

Example 5 with SendSignedBlockResult

use of tech.pegasys.teku.validator.api.SendSignedBlockResult in project teku by ConsenSys.

the class ValidatorApiHandlerTest method sendSignedBlock_shouldConvertSuccessfulResult.

@Test
public void sendSignedBlock_shouldConvertSuccessfulResult() {
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(5);
    when(blockImportChannel.importBlock(block)).thenReturn(SafeFuture.completedFuture(BlockImportResult.successful(block)));
    final SafeFuture<SendSignedBlockResult> result = validatorApiHandler.sendSignedBlock(block);
    verify(blockGossipChannel).publishBlock(block);
    verify(blockImportChannel).importBlock(block);
    assertThat(result).isCompletedWithValue(SendSignedBlockResult.success(block.getRoot()));
}
Also used : SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Test(org.junit.jupiter.api.Test)

Aggregations

SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)7 SendSignedBlockResult (tech.pegasys.teku.validator.api.SendSignedBlockResult)7 Test (org.junit.jupiter.api.Test)4 TestTemplate (org.junit.jupiter.api.TestTemplate)3 ValidatorBlockResult (tech.pegasys.teku.api.schema.ValidatorBlockResult)3 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 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 ArgumentCaptor (org.mockito.ArgumentCaptor)1