Search in sources :

Example 1 with ValidationPolicy

use of org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy in project besu by hyperledger.

the class DownloadHeaderSequenceTaskTest method failsWhenPeerReturnsOnlySubsetOfHeaders.

@Test
public void failsWhenPeerReturnsOnlySubsetOfHeaders() {
    final RespondingEthPeer respondingPeer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager);
    // Execute task and wait for response
    final BlockHeader referenceHeader = blockchain.getChainHeadHeader();
    final EthTask<List<BlockHeader>> task = DownloadHeaderSequenceTask.endingAtHeader(protocolSchedule, protocolContext, ethContext, referenceHeader, 10, maxRetries, validationPolicy, metricsSystem);
    final CompletableFuture<List<BlockHeader>> future = task.run();
    // Filter response to include only reference header and previous header
    final RespondingEthPeer.Responder fullResponder = RespondingEthPeer.blockchainResponder(blockchain);
    final RespondingEthPeer.Responder responder = (cap, message) -> {
        final Optional<MessageData> fullResponse = fullResponder.respond(cap, message);
        if (!fullResponse.isPresent() || message.getCode() != EthPV62.GET_BLOCK_HEADERS) {
            return fullResponse;
        }
        final BlockHeadersMessage headersMessage = BlockHeadersMessage.readFrom(fullResponse.get());
        // Filter for a subset of headers
        final List<BlockHeader> headerSubset = headersMessage.getHeaders(protocolSchedule).stream().filter(h -> h.getNumber() >= referenceHeader.getNumber() - 1L).collect(Collectors.toList());
        return Optional.of(BlockHeadersMessage.create(headerSubset));
    };
    respondingPeer.respondTimes(responder, 100);
    assertThat(future.isDone()).isTrue();
    assertThat(future.isCompletedExceptionally()).isTrue();
    assertThatThrownBy(future::get).hasCauseInstanceOf(MaxRetriesReachedException.class);
}
Also used : EthTask(org.hyperledger.besu.ethereum.eth.manager.task.EthTask) EthPV62(org.hyperledger.besu.ethereum.eth.messages.EthPV62) RetryingMessageTaskTest(org.hyperledger.besu.ethereum.eth.manager.ethtaskutils.RetryingMessageTaskTest) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MaxRetriesReachedException(org.hyperledger.besu.ethereum.eth.manager.exceptions.MaxRetriesReachedException) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) EthProtocolManagerTestUtil(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil) Collectors(java.util.stream.Collectors) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) ValidationPolicy(org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy) ArrayList(java.util.ArrayList) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) Collections(java.util.Collections) Optional(java.util.Optional) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) ArrayList(java.util.ArrayList) List(java.util.List) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) RetryingMessageTaskTest(org.hyperledger.besu.ethereum.eth.manager.ethtaskutils.RetryingMessageTaskTest) Test(org.junit.Test)

Example 2 with ValidationPolicy

use of org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy in project besu by hyperledger.

the class DownloadHeaderSequenceTaskTest method failsWhenPeerReturnsOnlyReferenceHeader.

@Test
public void failsWhenPeerReturnsOnlyReferenceHeader() {
    final RespondingEthPeer respondingPeer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager);
    // Execute task and wait for response
    final BlockHeader referenceHeader = blockchain.getChainHeadHeader();
    final EthTask<List<BlockHeader>> task = DownloadHeaderSequenceTask.endingAtHeader(protocolSchedule, protocolContext, ethContext, referenceHeader, 10, maxRetries, validationPolicy, metricsSystem);
    final CompletableFuture<List<BlockHeader>> future = task.run();
    // Respond with only the reference header
    final RespondingEthPeer.Responder responder = (cap, message) -> Optional.of(BlockHeadersMessage.create(Collections.singletonList(referenceHeader)));
    respondingPeer.respondWhile(responder, () -> !future.isDone());
    assertThat(future.isDone()).isTrue();
    assertThat(future.isCompletedExceptionally()).isTrue();
    assertThatThrownBy(future::get).hasCauseInstanceOf(MaxRetriesReachedException.class);
}
Also used : EthTask(org.hyperledger.besu.ethereum.eth.manager.task.EthTask) EthPV62(org.hyperledger.besu.ethereum.eth.messages.EthPV62) RetryingMessageTaskTest(org.hyperledger.besu.ethereum.eth.manager.ethtaskutils.RetryingMessageTaskTest) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MaxRetriesReachedException(org.hyperledger.besu.ethereum.eth.manager.exceptions.MaxRetriesReachedException) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) EthProtocolManagerTestUtil(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil) Collectors(java.util.stream.Collectors) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) ValidationPolicy(org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy) ArrayList(java.util.ArrayList) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) Collections(java.util.Collections) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) ArrayList(java.util.ArrayList) List(java.util.List) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) RetryingMessageTaskTest(org.hyperledger.besu.ethereum.eth.manager.ethtaskutils.RetryingMessageTaskTest) Test(org.junit.Test)

Example 3 with ValidationPolicy

use of org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy in project besu by hyperledger.

the class FastImportBlocksStepTest method shouldImportBlocks.

@Test
public void shouldImportBlocks() {
    final List<Block> blocks = gen.blockSequence(5);
    final List<BlockWithReceipts> blocksWithReceipts = blocks.stream().map(block -> new BlockWithReceipts(block, gen.receipts(block))).collect(toList());
    for (final BlockWithReceipts blockWithReceipts : blocksWithReceipts) {
        when(blockImporter.fastImportBlock(protocolContext, blockWithReceipts.getBlock(), blockWithReceipts.getReceipts(), FULL, LIGHT)).thenReturn(true);
    }
    importBlocksStep.accept(blocksWithReceipts);
    for (final BlockWithReceipts blockWithReceipts : blocksWithReceipts) {
        verify(protocolSchedule).getByBlockNumber(blockWithReceipts.getNumber());
    }
    verify(validationPolicy, times(blocks.size())).getValidationModeForNextBlock();
}
Also used : ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FULL(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.FULL) LIGHT(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.LIGHT) Collections.singletonList(java.util.Collections.singletonList) Mockito.verify(org.mockito.Mockito.verify) ValidationPolicy(org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy) InvalidBlockException(org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Block(org.hyperledger.besu.ethereum.core.Block) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter) Before(org.junit.Before) Block(org.hyperledger.besu.ethereum.core.Block) BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts) Test(org.junit.Test)

Aggregations

List (java.util.List)3 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)3 ValidationPolicy (org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Collectors (java.util.stream.Collectors)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 EthProtocolManagerTestUtil (org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil)2 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)2 RetryingMessageTaskTest (org.hyperledger.besu.ethereum.eth.manager.ethtaskutils.RetryingMessageTaskTest)2 MaxRetriesReachedException (org.hyperledger.besu.ethereum.eth.manager.exceptions.MaxRetriesReachedException)2 EthTask (org.hyperledger.besu.ethereum.eth.manager.task.EthTask)2 BlockHeadersMessage (org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage)2 EthPV62 (org.hyperledger.besu.ethereum.eth.messages.EthPV62)2 HeaderValidationMode (org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode)2 MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)2