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);
}
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);
}
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);
}
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);
}
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;
});
}
Aggregations