Search in sources :

Example 1 with WorldStateProofProvider

use of org.hyperledger.besu.ethereum.proof.WorldStateProofProvider in project besu by hyperledger.

the class StackTrieTest method shouldNotSaveTheRootWhenIncomplete.

@Test
public void shouldNotSaveTheRootWhenIncomplete() {
    final int nbAccounts = 15;
    final WorldStateStorage worldStateStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final WorldStateStorage recreatedWorldStateStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, nbAccounts);
    final StackTrie stackTrie = new StackTrie(Hash.wrap(accountStateTrie.getRootHash()), 0, 256, lastAccount);
    stackTrie.addSegment();
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(lastAccount, RangeManager.MAX_RANGE, 5, Integer.MAX_VALUE);
    final TrieIterator<Bytes> visitor = RangeStorageEntriesCollector.createVisitor(collector);
    final TreeMap<Bytes32, Bytes> accounts = (TreeMap<Bytes32, Bytes>) accountStateTrie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, visitor, root, lastAccount));
    final WorldStateProofProvider worldStateProofProvider = new WorldStateProofProvider(worldStateStorage);
    // generate the proof
    final List<Bytes> proofs = worldStateProofProvider.getAccountProofRelatedNodes(Hash.wrap(accountStateTrie.getRootHash()), lastAccount);
    proofs.addAll(worldStateProofProvider.getAccountProofRelatedNodes(Hash.wrap(accountStateTrie.getRootHash()), accounts.lastKey()));
    stackTrie.addElement(Bytes32.random(), proofs, accounts);
    final WorldStateStorage.Updater updater = recreatedWorldStateStorage.updater();
    stackTrie.commit(updater::putAccountStateTrieNode);
    updater.commit();
    Assertions.assertThat(recreatedWorldStateStorage.getAccountStateTrieNode(Bytes.EMPTY, accountStateTrie.getRootHash())).isEmpty();
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) Assertions(org.assertj.core.api.Assertions) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) Test(org.junit.Test)

Example 2 with WorldStateProofProvider

use of org.hyperledger.besu.ethereum.proof.WorldStateProofProvider in project besu by hyperledger.

the class StackTrieTest method shouldSaveTheRootWhenComplete.

@Test
public void shouldSaveTheRootWhenComplete() {
    final int nbAccounts = 15;
    final WorldStateStorage worldStateStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final WorldStateStorage recreatedWorldStateStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, nbAccounts);
    final StackTrie stackTrie = new StackTrie(Hash.wrap(accountStateTrie.getRootHash()), 0, 256, lastAccount);
    for (int i = 0; i < nbAccounts; i += 5) {
        stackTrie.addSegment();
        final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(lastAccount, RangeManager.MAX_RANGE, 5, Integer.MAX_VALUE);
        final TrieIterator<Bytes> visitor = RangeStorageEntriesCollector.createVisitor(collector);
        final TreeMap<Bytes32, Bytes> accounts = (TreeMap<Bytes32, Bytes>) accountStateTrie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, visitor, root, lastAccount));
        final WorldStateProofProvider worldStateProofProvider = new WorldStateProofProvider(worldStateStorage);
        // generate the proof
        final List<Bytes> proofs = worldStateProofProvider.getAccountProofRelatedNodes(Hash.wrap(accountStateTrie.getRootHash()), lastAccount);
        proofs.addAll(worldStateProofProvider.getAccountProofRelatedNodes(Hash.wrap(accountStateTrie.getRootHash()), accounts.lastKey()));
        stackTrie.addElement(Bytes32.random(), proofs, accounts);
        final WorldStateStorage.Updater updater = recreatedWorldStateStorage.updater();
        stackTrie.commit(updater::putAccountStateTrieNode);
        updater.commit();
    }
    Assertions.assertThat(worldStateStorage.getAccountStateTrieNode(Bytes.EMPTY, accountStateTrie.getRootHash())).isPresent();
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) Assertions(org.assertj.core.api.Assertions) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) Test(org.junit.Test)

Example 3 with WorldStateProofProvider

use of org.hyperledger.besu.ethereum.proof.WorldStateProofProvider in project besu by hyperledger.

the class RangeManagerTest method testFindNewBeginElementWhenNothingIsMissing.

@Test
public void testFindNewBeginElementWhenNothingIsMissing() {
    final WorldStateStorage worldStateStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Hash.ZERO, RangeManager.MAX_RANGE, 15, Integer.MAX_VALUE);
    final TrieIterator<Bytes> visitor = RangeStorageEntriesCollector.createVisitor(collector);
    final TreeMap<Bytes32, Bytes> accounts = (TreeMap<Bytes32, Bytes>) accountStateTrie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, visitor, root, Hash.ZERO));
    final WorldStateProofProvider worldStateProofProvider = new WorldStateProofProvider(worldStateStorage);
    // generate the proof
    final List<Bytes> proofs = worldStateProofProvider.getAccountProofRelatedNodes(Hash.wrap(accountStateTrie.getRootHash()), Hash.ZERO);
    proofs.addAll(worldStateProofProvider.getAccountProofRelatedNodes(Hash.wrap(accountStateTrie.getRootHash()), accounts.lastKey()));
    final Optional<Bytes32> newBeginElementInRange = RangeManager.findNewBeginElementInRange(accountStateTrie.getRootHash(), proofs, accounts, RangeManager.MAX_RANGE);
    Assertions.assertThat(newBeginElementInRange).isEmpty();
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) HashMap(java.util.HashMap) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) Map(java.util.Map) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) Optional(java.util.Optional) Assertions(org.assertj.core.api.Assertions) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) Test(org.junit.Test)

Example 4 with WorldStateProofProvider

use of org.hyperledger.besu.ethereum.proof.WorldStateProofProvider in project besu by hyperledger.

the class RangeManagerTest method testFindNewBeginElement.

@Test
public void testFindNewBeginElement() {
    final WorldStateStorage worldStateStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Hash.ZERO, RangeManager.MAX_RANGE, 10, Integer.MAX_VALUE);
    final TrieIterator<Bytes> visitor = RangeStorageEntriesCollector.createVisitor(collector);
    final TreeMap<Bytes32, Bytes> accounts = (TreeMap<Bytes32, Bytes>) accountStateTrie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, visitor, root, Hash.ZERO));
    final WorldStateProofProvider worldStateProofProvider = new WorldStateProofProvider(worldStateStorage);
    // generate the proof
    final List<Bytes> proofs = worldStateProofProvider.getAccountProofRelatedNodes(Hash.wrap(accountStateTrie.getRootHash()), Hash.ZERO);
    proofs.addAll(worldStateProofProvider.getAccountProofRelatedNodes(Hash.wrap(accountStateTrie.getRootHash()), accounts.lastKey()));
    final Optional<Bytes32> newBeginElementInRange = RangeManager.findNewBeginElementInRange(accountStateTrie.getRootHash(), proofs, accounts, RangeManager.MAX_RANGE);
    Assertions.assertThat(newBeginElementInRange).contains(Bytes32.leftPad(Bytes.wrap(Bytes.ofUnsignedShort(0x0b))));
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) HashMap(java.util.HashMap) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) Map(java.util.Map) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) Optional(java.util.Optional) Assertions(org.assertj.core.api.Assertions) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) Test(org.junit.Test)

Example 5 with WorldStateProofProvider

use of org.hyperledger.besu.ethereum.proof.WorldStateProofProvider 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)

Aggregations

List (java.util.List)6 TreeMap (java.util.TreeMap)6 Bytes (org.apache.tuweni.bytes.Bytes)6 Bytes32 (org.apache.tuweni.bytes.Bytes32)6 Hash (org.hyperledger.besu.datatypes.Hash)6 TrieGenerator (org.hyperledger.besu.ethereum.core.TrieGenerator)6 WorldStateProofProvider (org.hyperledger.besu.ethereum.proof.WorldStateProofProvider)6 MerklePatriciaTrie (org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie)6 RangeStorageEntriesCollector (org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector)6 TrieIterator (org.hyperledger.besu.ethereum.trie.TrieIterator)6 WorldStateStorage (org.hyperledger.besu.ethereum.worldstate.WorldStateStorage)6 Assertions (org.assertj.core.api.Assertions)4 WorldStateKeyValueStorage (org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage)4 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ArrayDeque (kotlin.collections.ArrayDeque)2 InMemoryKeyValueStorageProvider (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider)2