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