Search in sources :

Example 1 with BytecodeRequest

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");
        }
    });
}
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 2 with BytecodeRequest

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

Example 3 with BytecodeRequest

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));
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) RLP(org.hyperledger.besu.ethereum.rlp.RLP) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) BytecodeRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest) ArrayDeque(kotlin.collections.ArrayDeque) SnapDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) StateTrieAccountValue(org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue) TreeMap(java.util.TreeMap) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) AccountRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Task(org.hyperledger.besu.services.tasks.Task) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) BytecodeRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest) StateTrieAccountValue(org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue) TreeMap(java.util.TreeMap) Hash(org.hyperledger.besu.datatypes.Hash) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) AccountRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)

Aggregations

BytecodeRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest)3 SnapDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest)3 Task (org.hyperledger.besu.services.tasks.Task)3 List (java.util.List)2 Bytes (org.apache.tuweni.bytes.Bytes)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 Hash (org.hyperledger.besu.datatypes.Hash)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 WorldStateStorage (org.hyperledger.besu.ethereum.worldstate.WorldStateStorage)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ArrayDeque (kotlin.collections.ArrayDeque)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.fail (org.assertj.core.api.Assertions.fail)1 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)1