use of org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest 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.ethereum.eth.sync.snapsync.request.BytecodeRequest 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;
});
}
use of org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest in project besu by hyperledger.
the class TaskGenerator method createAccountRequest.
public static List<Task<SnapDataRequest>> createAccountRequest(final boolean withData) {
final WorldStateStorage worldStateStorage = new InMemoryKeyValueStorageProvider().createWorldStateStorage(DataStorageFormat.FOREST);
final WorldStateProofProvider worldStateProofProvider = new WorldStateProofProvider(worldStateStorage);
final MerklePatriciaTrie<Bytes32, Bytes> trie = TrieGenerator.generateTrie(worldStateStorage, 1);
final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Bytes32.ZERO, RangeManager.MAX_RANGE, 1, Integer.MAX_VALUE);
final TrieIterator<Bytes> visitor = RangeStorageEntriesCollector.createVisitor(collector);
final TreeMap<Bytes32, Bytes> accounts = (TreeMap<Bytes32, Bytes>) trie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, visitor, root, Bytes32.ZERO));
final Hash rootHash = Hash.wrap(trie.getRootHash());
final AccountRangeDataRequest accountRangeDataRequest = SnapDataRequest.createAccountRangeDataRequest(rootHash, RangeManager.MIN_RANGE, RangeManager.MAX_RANGE);
if (withData) {
accountRangeDataRequest.addResponse(worldStateProofProvider, accounts, new ArrayDeque<>());
}
final StateTrieAccountValue stateTrieAccountValue = StateTrieAccountValue.readFrom(RLP.input(accounts.firstEntry().getValue()));
final Hash accountHash = Hash.wrap(accounts.firstKey());
final StorageRangeDataRequest storageRangeDataRequest = createStorageRangeDataRequest(worldStateProofProvider, worldStateStorage, rootHash, accountHash, stateTrieAccountValue.getStorageRoot(), withData);
final BytecodeRequest bytecodeRequest = createBytecodeDataRequest(worldStateStorage, rootHash, accountHash, stateTrieAccountValue.getCodeHash(), withData);
return List.of(new StubTask(accountRangeDataRequest), new StubTask(storageRangeDataRequest), new StubTask(bytecodeRequest));
}
Aggregations