Search in sources :

Example 1 with StorageRangeDataRequest

use of org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest 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 StorageRangeDataRequest

use of org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest in project besu by hyperledger.

the class RequestDataStep method requestStorage.

public CompletableFuture<List<Task<SnapDataRequest>>> requestStorage(final List<Task<SnapDataRequest>> requestTasks) {
    final List<Bytes32> accountHashes = requestTasks.stream().map(Task::getData).map(StorageRangeDataRequest.class::cast).map(StorageRangeDataRequest::getAccountHash).collect(Collectors.toList());
    final BlockHeader blockHeader = fastSyncState.getPivotBlockHeader().get();
    final Bytes32 minRange = requestTasks.size() == 1 ? ((StorageRangeDataRequest) requestTasks.get(0).getData()).getStartKeyHash() : RangeManager.MIN_RANGE;
    final Bytes32 maxRange = requestTasks.size() == 1 ? ((StorageRangeDataRequest) requestTasks.get(0).getData()).getEndKeyHash() : RangeManager.MAX_RANGE;
    final EthTask<StorageRangeMessage.SlotRangeData> getStorageRangeTask = RetryingGetStorageRangeFromPeerTask.forStorageRange(ethContext, accountHashes, minRange, maxRange, blockHeader, metricsSystem);
    downloadState.addOutstandingTask(getStorageRangeTask);
    return getStorageRangeTask.run().handle((response, error) -> {
        if (response != null) {
            downloadState.removeOutstandingTask(getStorageRangeTask);
            for (int i = 0; i < response.slots().size(); i++) {
                final StorageRangeDataRequest request = (StorageRangeDataRequest) requestTasks.get(i).getData();
                request.setRootHash(blockHeader.getStateRoot());
                request.addResponse(downloadState, worldStateProofProvider, response.slots().get(i), i < response.slots().size() - 1 ? new ArrayDeque<>() : response.proofs());
            }
        }
        return requestTasks;
    });
}
Also used : BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Bytes32(org.apache.tuweni.bytes.Bytes32) ArrayDeque(kotlin.collections.ArrayDeque) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)

Example 3 with StorageRangeDataRequest

use of org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest in project besu by hyperledger.

the class TaskGenerator method createStorageRangeDataRequest.

private static StorageRangeDataRequest createStorageRangeDataRequest(final WorldStateProofProvider worldStateProofProvider, final WorldStateStorage worldStateStorage, final Hash rootHash, final Hash accountHash, final Bytes32 storageRoot, final boolean withData) {
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Bytes32.ZERO, RangeManager.MAX_RANGE, 100, Integer.MAX_VALUE);
    final StoredMerklePatriciaTrie<Bytes, Bytes> storageTrie = new StoredMerklePatriciaTrie<>((location, hash) -> worldStateStorage.getAccountStorageTrieNode(accountHash, location, hash), storageRoot, b -> b, b -> b);
    final TrieIterator<Bytes> visitor = RangeStorageEntriesCollector.createVisitor(collector);
    final TreeMap<Bytes32, Bytes> slots = (TreeMap<Bytes32, Bytes>) storageTrie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, visitor, root, Bytes32.ZERO));
    final StorageRangeDataRequest request = SnapDataRequest.createStorageRangeDataRequest(rootHash, accountHash, storageRoot, RangeManager.MIN_RANGE, RangeManager.MAX_RANGE);
    if (withData) {
        request.setProofValid(true);
        request.addResponse(null, worldStateProofProvider, slots, new ArrayDeque<>());
    }
    return request;
}
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) Bytes(org.apache.tuweni.bytes.Bytes) StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)

Example 4 with StorageRangeDataRequest

use of org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest 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

StorageRangeDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)4 List (java.util.List)3 ArrayDeque (kotlin.collections.ArrayDeque)3 Bytes (org.apache.tuweni.bytes.Bytes)3 Bytes32 (org.apache.tuweni.bytes.Bytes32)3 Hash (org.hyperledger.besu.datatypes.Hash)3 InMemoryKeyValueStorageProvider (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider)3 AccountRangeDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest)3 BytecodeRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest)3 SnapDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest)3 StoredMerklePatriciaTrie (org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie)3 DataStorageFormat (org.hyperledger.besu.ethereum.worldstate.DataStorageFormat)3 WorldStateStorage (org.hyperledger.besu.ethereum.worldstate.WorldStateStorage)3 Task (org.hyperledger.besu.services.tasks.Task)3 TreeMap (java.util.TreeMap)2 TrieGenerator (org.hyperledger.besu.ethereum.core.TrieGenerator)2 WorldStateProofProvider (org.hyperledger.besu.ethereum.proof.WorldStateProofProvider)2 RLP (org.hyperledger.besu.ethereum.rlp.RLP)2 MerklePatriciaTrie (org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie)2 RangeStorageEntriesCollector (org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector)2