Search in sources :

Example 1 with Task

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();
}
Also used : StubTask(org.hyperledger.besu.ethereum.eth.sync.worldstate.StubTask) Task(org.hyperledger.besu.services.tasks.Task) StubTask(org.hyperledger.besu.ethereum.eth.sync.worldstate.StubTask) Test(org.junit.Test)

Example 2 with Task

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

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);
}
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 4 with Task

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");
        }
    });
}
Also used : StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Bytes(org.apache.tuweni.bytes.Bytes) BytecodeRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest) SnapDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) AccountRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Task(org.hyperledger.besu.services.tasks.Task) Mockito.mock(org.mockito.Mockito.mock) Hash(org.hyperledger.besu.datatypes.Hash) Before(org.junit.Before) Bytes(org.apache.tuweni.bytes.Bytes) StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) BytecodeRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest) AccountRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)

Example 5 with Task

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

Task (org.hyperledger.besu.services.tasks.Task)9 List (java.util.List)6 Test (org.junit.Test)6 EthTask (org.hyperledger.besu.ethereum.eth.manager.task.EthTask)5 StubTask (org.hyperledger.besu.ethereum.eth.sync.worldstate.StubTask)5 Arrays.asList (java.util.Arrays.asList)3 Collections.singletonList (java.util.Collections.singletonList)3 BytecodeRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest)3 SnapDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest)3 WorldStateStorage (org.hyperledger.besu.ethereum.worldstate.WorldStateStorage)3 Bytes (org.apache.tuweni.bytes.Bytes)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 Hash (org.hyperledger.besu.datatypes.Hash)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 InMemoryKeyValueStorageProvider (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider)2 AccountRangeDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest)2 StorageRangeDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)2 StoredMerklePatriciaTrie (org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie)2 DataStorageFormat (org.hyperledger.besu.ethereum.worldstate.DataStorageFormat)2 HashMap (java.util.HashMap)1