Search in sources :

Example 1 with MerklePatriciaTrie

use of org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie 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 MerklePatriciaTrie

use of org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie 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 MerklePatriciaTrie

use of org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie in project besu by hyperledger.

the class WorldStateRangeProofProviderTest method rangeProofValidationInvalidEmptyProof.

@Test
public void rangeProofValidationInvalidEmptyProof() {
    MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
    // generate the invalid proof
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Hash.ZERO, MAX_RANGE, 9, Integer.MAX_VALUE);
    final TrieIterator<Bytes> invalidVisitor = RangeStorageEntriesCollector.createVisitor(collector);
    final TreeMap<Bytes32, Bytes> accounts = (TreeMap<Bytes32, Bytes>) accountStateTrie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, invalidVisitor, root, Hash.ZERO));
    // validate the range proof
    boolean isValidRangeProof = worldStateProofProvider.isValidRangeProof(Bytes32.ZERO, accounts.lastKey(), accountStateTrie.getRootHash(), new ArrayList<>(), accounts);
    assertThat(isValidRangeProof).isFalse();
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) Iterator(java.util.Iterator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) ArrayList(java.util.ArrayList) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) Map(java.util.Map) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) Before(org.junit.Before) Bytes(org.apache.tuweni.bytes.Bytes) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.Test)

Example 4 with MerklePatriciaTrie

use of org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie in project besu by hyperledger.

the class WorldStateRangeProofProviderTest method rangeProofValidationMissingAccount.

@Test
public void rangeProofValidationMissingAccount() {
    MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
    // collect accounts in range
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Hash.ZERO, 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));
    // 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()));
    // remove an account
    final Iterator<Map.Entry<Bytes32, Bytes>> iterator = accounts.entrySet().iterator();
    int i = 0;
    while (iterator.hasNext()) {
        iterator.next();
        i++;
        if (i == 7)
            iterator.remove();
    }
    // validate the range proof
    boolean isValidRangeProof = worldStateProofProvider.isValidRangeProof(Bytes32.ZERO, accounts.lastKey(), accountStateTrie.getRootHash(), proofs, accounts);
    assertThat(isValidRangeProof).isFalse();
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) Iterator(java.util.Iterator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) ArrayList(java.util.ArrayList) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) Map(java.util.Map) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) Before(org.junit.Before) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) Test(org.junit.Test)

Example 5 with MerklePatriciaTrie

use of org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie in project besu by hyperledger.

the class WorldStateRangeProofProviderTest method rangeProofValidationEmptyProof.

@Test
public void rangeProofValidationEmptyProof() {
    MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
    // generate the invalid proof
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Hash.ZERO, MAX_RANGE, 15, Integer.MAX_VALUE);
    final TrieIterator<Bytes> invalidVisitor = RangeStorageEntriesCollector.createVisitor(collector);
    final TreeMap<Bytes32, Bytes> accounts = (TreeMap<Bytes32, Bytes>) accountStateTrie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, invalidVisitor, root, Hash.ZERO));
    // validate the range proof
    boolean isValidRangeProof = worldStateProofProvider.isValidRangeProof(Bytes32.ZERO, accounts.lastKey(), accountStateTrie.getRootHash(), new ArrayList<>(), accounts);
    assertThat(isValidRangeProof).isTrue();
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) Iterator(java.util.Iterator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) ArrayList(java.util.ArrayList) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) Map(java.util.Map) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) Before(org.junit.Before) Bytes(org.apache.tuweni.bytes.Bytes) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.Test)

Aggregations

TreeMap (java.util.TreeMap)12 Bytes (org.apache.tuweni.bytes.Bytes)12 Bytes32 (org.apache.tuweni.bytes.Bytes32)12 Hash (org.hyperledger.besu.datatypes.Hash)12 MerklePatriciaTrie (org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie)12 List (java.util.List)11 TrieGenerator (org.hyperledger.besu.ethereum.core.TrieGenerator)11 WorldStateStorage (org.hyperledger.besu.ethereum.worldstate.WorldStateStorage)11 RangeStorageEntriesCollector (org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector)10 TrieIterator (org.hyperledger.besu.ethereum.trie.TrieIterator)10 Test (org.junit.Test)10 WorldStateKeyValueStorage (org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage)9 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)9 Map (java.util.Map)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 ArrayList (java.util.ArrayList)5 Iterator (java.util.Iterator)5 WorldStateProofProvider (org.hyperledger.besu.ethereum.proof.WorldStateProofProvider)5 Before (org.junit.Before)5 Assertions (org.assertj.core.api.Assertions)4