Search in sources :

Example 1 with EthTask

use of org.hyperledger.besu.ethereum.eth.manager.task.EthTask in project besu by hyperledger.

the class RequestDataStepTest method shouldReportNoProgressWhenRequestCompletesWithNoData.

@Test
public void shouldReportNoProgressWhenRequestCompletesWithNoData() {
    final StubTask task1 = StubTask.forHash(HASH1);
    final List<Task<NodeDataRequest>> tasks = singletonList(task1);
    when(getNodeDataTaskFactory.apply(singletonList(HASH1), BLOCK_NUMBER)).thenReturn(ethTask);
    final CompletableFuture<List<Task<NodeDataRequest>>> result = requestDataStep.requestData(tasks, blockHeader, downloadState);
    assertThat(result).isNotDone();
    getDataFuture.complete(emptyMap());
    assertThat(result).isDone();
    assertThat(result).isCompletedWithValue(tasks);
    verify(downloadState).requestComplete(false);
}
Also used : EthTask(org.hyperledger.besu.ethereum.eth.manager.task.EthTask) StubTask(org.hyperledger.besu.ethereum.eth.sync.worldstate.StubTask) Task(org.hyperledger.besu.services.tasks.Task) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) StubTask(org.hyperledger.besu.ethereum.eth.sync.worldstate.StubTask) Test(org.junit.Test)

Example 2 with EthTask

use of org.hyperledger.besu.ethereum.eth.manager.task.EthTask in project besu by hyperledger.

the class RequestDataStepTest method shouldRequestDistinctHashesForTasks.

@Test
public void shouldRequestDistinctHashesForTasks() {
    final StubTask task1 = StubTask.forHash(HASH1);
    final StubTask task2 = StubTask.forHash(HASH2);
    final StubTask task3 = StubTask.forHash(HASH1);
    final List<Task<NodeDataRequest>> tasks = asList(task1, task2, task3);
    when(getNodeDataTaskFactory.apply(asList(HASH1, HASH2), BLOCK_NUMBER)).thenReturn(ethTask);
    final CompletableFuture<List<Task<NodeDataRequest>>> result = requestDataStep.requestData(tasks, blockHeader, downloadState);
    assertThat(result).isNotDone();
    getDataFuture.complete(ImmutableMap.of(HASH1, DATA1, HASH2, DATA2));
    assertThat(result).isDone();
    assertThat(result).isCompletedWithValue(tasks);
    assertThat(task1.getData().getData()).isEqualTo(DATA1);
    assertThat(task2.getData().getData()).isEqualTo(DATA2);
    assertThat(task3.getData().getData()).isEqualTo(DATA1);
    verify(downloadState).requestComplete(true);
}
Also used : EthTask(org.hyperledger.besu.ethereum.eth.manager.task.EthTask) StubTask(org.hyperledger.besu.ethereum.eth.sync.worldstate.StubTask) Task(org.hyperledger.besu.services.tasks.Task) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) StubTask(org.hyperledger.besu.ethereum.eth.sync.worldstate.StubTask) Test(org.junit.Test)

Example 3 with EthTask

use of org.hyperledger.besu.ethereum.eth.manager.task.EthTask 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 4 with EthTask

use of org.hyperledger.besu.ethereum.eth.manager.task.EthTask 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 5 with EthTask

use of org.hyperledger.besu.ethereum.eth.manager.task.EthTask in project besu by hyperledger.

the class RequestDataStep method requestCode.

public CompletableFuture<List<Task<SnapDataRequest>>> requestCode(final List<Task<SnapDataRequest>> requestTasks) {
    final List<Bytes32> codeHashes = requestTasks.stream().map(Task::getData).map(BytecodeRequest.class::cast).map(BytecodeRequest::getCodeHash).distinct().collect(Collectors.toList());
    final BlockHeader blockHeader = fastSyncState.getPivotBlockHeader().get();
    final EthTask<Map<Bytes32, Bytes>> getByteCodeTask = RetryingGetBytecodeFromPeerTask.forByteCode(ethContext, codeHashes, blockHeader, metricsSystem);
    downloadState.addOutstandingTask(getByteCodeTask);
    return getByteCodeTask.run().handle((response, error) -> {
        if (response != null) {
            downloadState.removeOutstandingTask(getByteCodeTask);
            for (Task<SnapDataRequest> requestTask : requestTasks) {
                final BytecodeRequest request = (BytecodeRequest) requestTask.getData();
                request.setRootHash(blockHeader.getStateRoot());
                if (response.containsKey(request.getCodeHash())) {
                    request.setCode(response.get(request.getCodeHash()));
                }
            }
        }
        return requestTasks;
    });
}
Also used : RetryingGetBytecodeFromPeerTask(org.hyperledger.besu.ethereum.eth.manager.snap.RetryingGetBytecodeFromPeerTask) EthTask(org.hyperledger.besu.ethereum.eth.manager.task.EthTask) RetryingGetTrieNodeFromPeerTask(org.hyperledger.besu.ethereum.eth.manager.snap.RetryingGetTrieNodeFromPeerTask) RetryingGetAccountRangeFromPeerTask(org.hyperledger.besu.ethereum.eth.manager.snap.RetryingGetAccountRangeFromPeerTask) Task(org.hyperledger.besu.services.tasks.Task) RetryingGetStorageRangeFromPeerTask(org.hyperledger.besu.ethereum.eth.manager.snap.RetryingGetStorageRangeFromPeerTask) SnapDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest) BytecodeRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Bytes32(org.apache.tuweni.bytes.Bytes32) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EthTask (org.hyperledger.besu.ethereum.eth.manager.task.EthTask)7 Test (org.junit.Test)6 List (java.util.List)5 Task (org.hyperledger.besu.services.tasks.Task)5 StubTask (org.hyperledger.besu.ethereum.eth.sync.worldstate.StubTask)4 Arrays.asList (java.util.Arrays.asList)3 Collections.singletonList (java.util.Collections.singletonList)3 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)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 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)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 BlockHeadersMessage (org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage)2