use of org.hyperledger.besu.services.tasks.Task in project besu by hyperledger.
the class PersistDataStepTest method shouldSkipPersistingTasksWithNoData.
@Test
public void shouldSkipPersistingTasksWithNoData() {
final Task<NodeDataRequest> withData = createTaskWithData(1, 2, 3);
final StubTask withoutData = createTaskWithoutData(4, 5, 6);
final List<Task<NodeDataRequest>> tasks = asList(withData, withoutData);
final List<Task<NodeDataRequest>> result = persistDataStep.persist(tasks, blockHeader, downloadState);
assertThat(result).isSameAs(tasks);
assertThat(worldStateStorage.contains(withData.getData().getHash())).isTrue();
assertThat(worldStateStorage.contains(withoutData.getData().getHash())).isFalse();
}
use of org.hyperledger.besu.services.tasks.Task 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.services.tasks.Task 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.services.tasks.Task in project besu by hyperledger.
the class PersistDataStepTest method assertDataPersisted.
private void assertDataPersisted(final List<Task<SnapDataRequest>> tasks) {
tasks.forEach(task -> {
if (task.getData() instanceof AccountRangeDataRequest) {
final AccountRangeDataRequest data = (AccountRangeDataRequest) task.getData();
StoredMerklePatriciaTrie<Bytes, Bytes> trie = new StoredMerklePatriciaTrie<>(worldStateStorage::getAccountStateTrieNode, data.getRootHash(), b -> b, b -> b);
data.getAccounts().forEach((key, value) -> assertThat(trie.get(key)).isPresent());
} else if (task.getData() instanceof StorageRangeDataRequest) {
final StorageRangeDataRequest data = (StorageRangeDataRequest) task.getData();
final StoredMerklePatriciaTrie<Bytes, Bytes> trie = new StoredMerklePatriciaTrie<>((location, hash) -> worldStateStorage.getAccountStorageTrieNode(Hash.wrap(data.getAccountHash()), location, hash), data.getStorageRoot(), b -> b, b -> b);
data.getSlots().forEach((key, value) -> assertThat(trie.get(key)).isPresent());
} else if (task.getData() instanceof BytecodeRequest) {
final BytecodeRequest data = (BytecodeRequest) task.getData();
assertThat(worldStateStorage.getCode(data.getCodeHash(), Hash.wrap(data.getAccountHash()))).isPresent();
} else {
fail("not expected message");
}
});
}
use of org.hyperledger.besu.services.tasks.Task 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