Merkle trie radix abstraction (#5251)

* limit to MerkleTrie refactor

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
Signed-off-by: garyschulte <garyschulte@gmail.com>
pull/5256/head
garyschulte 2 years ago committed by GitHub
parent c955eb290a
commit b88d037f25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/StateBackupService.java
  2. 17
      ethereum/core/src/integration-test/java/org/hyperledger/besu/ethereum/worldstate/PrunerIntegrationTest.java
  3. 4
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiWorldStateProvider.java
  4. 14
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedMerkleTrieLoader.java
  5. 20
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/storage/BonsaiWorldStateKeyValueStorage.java
  6. 6
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/worldview/BonsaiWorldState.java
  7. 2
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/MutableWorldState.java
  8. 14
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java
  9. 6
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateStateRootResolver.java
  10. 23
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/proof/WorldStateProofProvider.java
  11. 16
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/WorldStateKeyValueStorage.java
  12. 27
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/DefaultMutableWorldState.java
  13. 6
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/DefaultWorldStateArchive.java
  14. 10
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/MarkSweepPruner.java
  15. 6
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/WorldStateArchive.java
  16. 21
      ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/TrieGenerator.java
  17. 44
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/BonsaiWorldStateKeyValueStorageTest.java
  18. 8
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/CachedMerkleTrieLoaderTest.java
  19. 6
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/PrivateStateGenesisAllocatorTest.java
  20. 20
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/proof/WorldStateProofProviderTest.java
  21. 18
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/proof/WorldStateRangeProofProviderTest.java
  22. 54
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStorageWorldStateStorageTest.java
  23. 26
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/worldstate/DefaultMutableWorldStateTest.java
  24. 17
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/worldstate/MarkSweepPrunerTest.java
  25. 6
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/worldstate/AccountTrieNodeDataRequest.java
  26. 4
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/worldstate/TrieNodeDataRequest.java
  27. 4
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/RangeManager.java
  28. 11
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/StackTrie.java
  29. 8
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/AccountTrieNodeDataRequest.java
  30. 4
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/TrieNodeDataRequest.java
  31. 10
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/worldstate/FastWorldStateDownloaderTest.java
  32. 8
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/worldstate/PersistDataStepTest.java
  33. 4
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/PersistDataStepTest.java
  34. 8
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/RangeManagerTest.java
  35. 8
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/StackTrieTest.java
  36. 8
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/TaskGenerator.java
  37. 3
      ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/trie/TrieRefTest.java
  38. 7
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/AllNodesVisitor.java
  39. 12
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/CommitVisitor.java
  40. 4
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/CompactEncoding.java
  41. 5
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/InnerNodeDiscoveryManager.java
  42. 8
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/LeafNode.java
  43. 7
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/LocationNodeVisitor.java
  44. 4
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/MerkleStorage.java
  45. 15
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/MerkleTrie.java
  46. 11
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/NodeFactory.java
  47. 7
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/NodeVisitor.java
  48. 8
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/NullNode.java
  49. 7
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/PathNodeVisitor.java
  50. 5
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/PersistVisitor.java
  51. 10
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/ProofVisitor.java
  52. 6
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/RestoreVisitor.java
  53. 39
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/SimpleMerkleTrie.java
  54. 6
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/SnapPutVisitor.java
  55. 80
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/StoredMerkleTrie.java
  56. 8
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/StoredNode.java
  57. 9
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/TrieIterator.java
  58. 47
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/BranchNode.java
  59. 24
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/DefaultNodeFactory.java
  60. 16
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/ExtensionNode.java
  61. 12
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/GetVisitor.java
  62. 11
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/PutVisitor.java
  63. 10
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/RemoveVisitor.java
  64. 53
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/SimpleMerklePatriciaTrie.java
  65. 84
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/StoredMerklePatriciaTrie.java
  66. 34
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/StoredNodeFactory.java
  67. 8
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/TrieNodeDecoder.java
  68. 5
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/AllNodesVisitorTest.java
  69. 11
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/SnapPutVisitorTest.java
  70. 4
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/TrieIteratorTest.java
  71. 14
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/TrieNodeDecoderTest.java
  72. 26
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/patricia/AbstractMerklePatriciaTrieTest.java
  73. 8
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/patricia/SimpleMerklePatriciaTrieTest.java
  74. 9
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/patricia/StoredMerklePatriciaTrieTest.java

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -28,9 +28,9 @@ import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.TrieIterator;
import org.hyperledger.besu.ethereum.trie.TrieIterator.State;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.plugin.data.Hash;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -28,8 +28,8 @@ import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.Pruner.PruningPhase;
import org.hyperledger.besu.evm.worldstate.WorldState;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
@ -197,7 +197,7 @@ public class PrunerIntegrationTest {
private Set<Bytes> collectWorldStateNodes(final Hash stateRootHash, final Set<Bytes> collector) {
final List<Hash> storageRoots = new ArrayList<>();
final MerklePatriciaTrie<Bytes32, Bytes> stateTrie = createStateTrie(stateRootHash);
final MerkleTrie<Bytes32, Bytes> stateTrie = createStateTrie(stateRootHash);
// Collect storage roots and code
stateTrie
@ -216,15 +216,14 @@ public class PrunerIntegrationTest {
collectTrieNodes(stateTrie, collector);
// Collect storage nodes
for (Hash storageRoot : storageRoots) {
final MerklePatriciaTrie<Bytes32, Bytes> storageTrie = createStorageTrie(storageRoot);
final MerkleTrie<Bytes32, Bytes> storageTrie = createStorageTrie(storageRoot);
collectTrieNodes(storageTrie, collector);
}
return collector;
}
private void collectTrieNodes(
final MerklePatriciaTrie<Bytes32, Bytes> trie, final Set<Bytes> collector) {
private void collectTrieNodes(final MerkleTrie<Bytes32, Bytes> trie, final Set<Bytes> collector) {
final Bytes32 rootHash = trie.getRootHash();
trie.visitAll(
(node) -> {
@ -234,7 +233,7 @@ public class PrunerIntegrationTest {
});
}
private MerklePatriciaTrie<Bytes32, Bytes> createStateTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes32, Bytes> createStateTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
worldStateStorage::getAccountStateTrieNode,
rootHash,
@ -242,7 +241,7 @@ public class PrunerIntegrationTest {
Function.identity());
}
private MerklePatriciaTrie<Bytes32, Bytes> createStorageTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes32, Bytes> createStorageTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
(location, hash) -> worldStateStorage.getAccountStorageTrieNode(null, location, hash),
rootHash,

@ -1,5 +1,5 @@
/*
* Copyright Hyperledger Besu contributors.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -34,7 +34,7 @@ import org.hyperledger.besu.ethereum.proof.WorldStateProof;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;

@ -1,5 +1,5 @@
/*
* Copyright Hyperledger Besu contributors.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -19,9 +19,9 @@ import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage.BonsaiStorageSubscriber;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.metrics.ObservableMetricsSystem;
import org.hyperledger.besu.metrics.prometheus.PrometheusMetricsSystem;
@ -135,8 +135,8 @@ public class CachedMerkleTrieLoader implements BonsaiStorageSubscriber {
final BonsaiWorldStateKeyValueStorage worldStateKeyValueStorage,
final Bytes location,
final Bytes32 nodeHash) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerklePatriciaTrie.EMPTY_TRIE_NODE);
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerkleTrie.EMPTY_TRIE_NODE);
} else {
return Optional.ofNullable(accountNodes.getIfPresent(nodeHash))
.or(() -> worldStateKeyValueStorage.getAccountStateTrieNode(location, nodeHash));
@ -148,8 +148,8 @@ public class CachedMerkleTrieLoader implements BonsaiStorageSubscriber {
final Hash accountHash,
final Bytes location,
final Bytes32 nodeHash) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerklePatriciaTrie.EMPTY_TRIE_NODE);
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerkleTrie.EMPTY_TRIE_NODE);
} else {
return Optional.ofNullable(storageNodes.getIfPresent(nodeHash))
.or(

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -17,9 +17,9 @@ package org.hyperledger.besu.ethereum.bonsai.storage;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredNodeFactory;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredNodeFactory;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;
@ -120,8 +120,8 @@ public class BonsaiWorldStateKeyValueStorage implements WorldStateStorage, AutoC
@Override
public Optional<Bytes> getAccountStateTrieNode(final Bytes location, final Bytes32 nodeHash) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerklePatriciaTrie.EMPTY_TRIE_NODE);
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerkleTrie.EMPTY_TRIE_NODE);
} else {
return trieBranchStorage
.get(location.toArrayUnsafe())
@ -133,8 +133,8 @@ public class BonsaiWorldStateKeyValueStorage implements WorldStateStorage, AutoC
@Override
public Optional<Bytes> getAccountStorageTrieNode(
final Hash accountHash, final Bytes location, final Bytes32 nodeHash) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerklePatriciaTrie.EMPTY_TRIE_NODE);
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerkleTrie.EMPTY_TRIE_NODE);
} else {
return trieBranchStorage
.get(Bytes.concatenate(accountHash, location).toArrayUnsafe())
@ -343,7 +343,7 @@ public class BonsaiWorldStateKeyValueStorage implements WorldStateStorage, AutoC
@Override
public BonsaiUpdater putAccountStateTrieNode(
final Bytes location, final Bytes32 nodeHash, final Bytes node) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
// Don't save empty nodes
return this;
}
@ -360,7 +360,7 @@ public class BonsaiWorldStateKeyValueStorage implements WorldStateStorage, AutoC
@Override
public synchronized BonsaiUpdater putAccountStorageTrieNode(
final Hash accountHash, final Bytes location, final Bytes32 nodeHash, final Bytes node) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
// Don't save empty nodes
return this;
}

@ -33,10 +33,10 @@ import org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.bonsai.worldview.BonsaiWorldStateUpdateAccumulator.StorageConsumingMap;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.NodeLoader;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
@ -315,7 +315,7 @@ public class BonsaiWorldState
continue;
}
final Hash addressHash = Hash.hash(address);
final MerklePatriciaTrie<Bytes, Bytes> storageTrie =
final MerkleTrie<Bytes, Bytes> storageTrie =
createTrie(
(location, key) -> getStorageTrieNode(addressHash, location, key),
oldAccount.getStorageRoot());

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -24,8 +24,8 @@ import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.core.encoding.WithdrawalEncoder;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.evm.log.LogsBloomFilter;
import java.util.List;
@ -45,7 +45,7 @@ public final class BodyValidation {
return RLP.encodeOne(UInt256.valueOf(i).trimLeadingZeros());
}
private static MerklePatriciaTrie<Bytes, Bytes> trie() {
private static MerkleTrie<Bytes, Bytes> trie() {
return new SimpleMerklePatriciaTrie<>(b -> b);
}
@ -56,7 +56,7 @@ public final class BodyValidation {
* @return the transaction root
*/
public static Hash transactionsRoot(final List<Transaction> transactions) {
final MerklePatriciaTrie<Bytes, Bytes> trie = trie();
final MerkleTrie<Bytes, Bytes> trie = trie();
IntStream.range(0, transactions.size())
.forEach(
@ -72,7 +72,7 @@ public final class BodyValidation {
* @return the transaction root
*/
public static Hash withdrawalsRoot(final List<Withdrawal> withdrawals) {
final MerklePatriciaTrie<Bytes, Bytes> trie = trie();
final MerkleTrie<Bytes, Bytes> trie = trie();
IntStream.range(0, withdrawals.size())
.forEach(
@ -88,7 +88,7 @@ public final class BodyValidation {
* @return the receipt root
*/
public static Hash receiptsRoot(final List<TransactionReceipt> receipts) {
final MerklePatriciaTrie<Bytes, Bytes> trie = trie();
final MerkleTrie<Bytes, Bytes> trie = trie();
IntStream.range(0, receipts.size())
.forEach(

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -19,14 +19,14 @@ import org.hyperledger.besu.ethereum.privacy.storage.PrivacyGroupHeadBlockMap;
import org.hyperledger.besu.ethereum.privacy.storage.PrivateBlockMetadata;
import org.hyperledger.besu.ethereum.privacy.storage.PrivateMetadataUpdater;
import org.hyperledger.besu.ethereum.privacy.storage.PrivateStateStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
public class PrivateStateRootResolver {
public static final Hash EMPTY_ROOT_HASH = Hash.wrap(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
public static final Hash EMPTY_ROOT_HASH = Hash.wrap(MerkleTrie.EMPTY_TRIE_NODE_HASH);
private final PrivateStateStorage privateStateStorage;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -19,12 +19,12 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.InnerNodeDiscoveryManager;
import org.hyperledger.besu.ethereum.trie.InnerNodeDiscoveryManager.InnerNode;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.Proof;
import org.hyperledger.besu.ethereum.trie.RemoveVisitor;
import org.hyperledger.besu.ethereum.trie.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.RemoveVisitor;
import org.hyperledger.besu.ethereum.trie.patricia.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
@ -79,7 +79,7 @@ public class WorldStateProofProvider {
final Hash accountHash,
final StateTrieAccountValue account,
final List<UInt256> accountStorageKeys) {
final MerklePatriciaTrie<Bytes32, Bytes> storageTrie =
final MerkleTrie<Bytes32, Bytes> storageTrie =
newAccountStorageTrie(accountHash, account.getStorageRoot());
final NavigableMap<UInt256, Proof<Bytes>> storageProofs = new TreeMap<>();
accountStorageKeys.forEach(
@ -94,12 +94,12 @@ public class WorldStateProofProvider {
return accountProof.getProofRelatedNodes();
}
private MerklePatriciaTrie<Bytes, Bytes> newAccountStateTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes, Bytes> newAccountStateTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
worldStateStorage::getAccountStateTrieNode, rootHash, b -> b, b -> b);
}
private MerklePatriciaTrie<Bytes32, Bytes> newAccountStorageTrie(
private MerkleTrie<Bytes32, Bytes> newAccountStorageTrie(
final Hash accountHash, final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
(location, hash) ->
@ -123,8 +123,7 @@ public class WorldStateProofProvider {
// when proof is empty we need to have all the keys to reconstruct the trie
if (proofs.isEmpty()) {
final MerklePatriciaTrie<Bytes, Bytes> trie =
new SimpleMerklePatriciaTrie<>(Function.identity());
final MerkleTrie<Bytes, Bytes> trie = new SimpleMerklePatriciaTrie<>(Function.identity());
// add the received keys in the trie
for (Map.Entry<Bytes32, Bytes> key : keys.entrySet()) {
trie.put(key.getKey(), key.getValue());
@ -140,7 +139,7 @@ public class WorldStateProofProvider {
}
if (keys.isEmpty()) {
final MerklePatriciaTrie<Bytes, Bytes> trie =
final MerkleTrie<Bytes, Bytes> trie =
new StoredMerklePatriciaTrie<>(
new InnerNodeDiscoveryManager<>(
(location, hash) -> Optional.ofNullable(proofsEntries.get(hash)),
@ -170,7 +169,7 @@ public class WorldStateProofProvider {
startKeyHash,
keys.lastKey(),
true);
final MerklePatriciaTrie<Bytes, Bytes> trie =
final MerkleTrie<Bytes, Bytes> trie =
new StoredMerklePatriciaTrie<>(snapStoredNodeFactory, rootHash);
// filling out innerNodes of the InnerNodeDiscoveryManager by walking through the trie
trie.visitAll(node -> {});

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -15,7 +15,7 @@
package org.hyperledger.besu.ethereum.storage.keyvalue;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction;
@ -69,8 +69,8 @@ public class WorldStateKeyValueStorage implements WorldStateStorage {
}
private Optional<Bytes> getTrieNode(final Bytes32 nodeHash) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerklePatriciaTrie.EMPTY_TRIE_NODE);
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerkleTrie.EMPTY_TRIE_NODE);
} else {
return keyValueStorage.get(nodeHash.toArrayUnsafe()).map(Bytes::wrap);
}
@ -78,8 +78,8 @@ public class WorldStateKeyValueStorage implements WorldStateStorage {
@Override
public Optional<Bytes> getNodeData(final Bytes location, final Bytes32 hash) {
if (hash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerklePatriciaTrie.EMPTY_TRIE_NODE);
if (hash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
return Optional.of(MerkleTrie.EMPTY_TRIE_NODE);
} else if (hash.equals(Hash.EMPTY)) {
return Optional.of(Bytes.EMPTY);
} else {
@ -180,7 +180,7 @@ public class WorldStateKeyValueStorage implements WorldStateStorage {
@Override
public Updater putAccountStateTrieNode(
final Bytes location, final Bytes32 nodeHash, final Bytes node) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
// Don't save empty nodes
return this;
}
@ -199,7 +199,7 @@ public class WorldStateKeyValueStorage implements WorldStateStorage {
@Override
public Updater putAccountStorageTrieNode(
final Hash accountHash, final Bytes location, final Bytes32 nodeHash, final Bytes node) {
if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
if (nodeHash.equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
// Don't save empty nodes
return this;
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -22,8 +22,8 @@ import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPException;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.AccountStorageEntry;
import org.hyperledger.besu.evm.worldstate.AbstractWorldUpdater;
@ -53,16 +53,15 @@ public class DefaultMutableWorldState implements MutableWorldState {
private final WorldStateStorage worldStateStorage;
private final WorldStatePreimageStorage preimageStorage;
private final MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie;
private final Map<Address, MerklePatriciaTrie<Bytes32, Bytes>> updatedStorageTries =
new HashMap<>();
private final MerkleTrie<Bytes32, Bytes> accountStateTrie;
private final Map<Address, MerkleTrie<Bytes32, Bytes>> updatedStorageTries = new HashMap<>();
private final Map<Address, Bytes> updatedAccountCode = new HashMap<>();
private final Map<Bytes32, UInt256> newStorageKeyPreimages = new HashMap<>();
private final Map<Bytes32, Address> newAccountKeyPreimages = new HashMap<>();
public DefaultMutableWorldState(
final WorldStateStorage storage, final WorldStatePreimageStorage preimageStorage) {
this(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH, storage, preimageStorage);
this(MerkleTrie.EMPTY_TRIE_NODE_HASH, storage, preimageStorage);
}
public DefaultMutableWorldState(
@ -88,12 +87,12 @@ public class DefaultMutableWorldState implements MutableWorldState {
this.accountStateTrie = newAccountStateTrie(other.accountStateTrie.getRootHash());
}
private MerklePatriciaTrie<Bytes32, Bytes> newAccountStateTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes32, Bytes> newAccountStateTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
worldStateStorage::getAccountStateTrieNode, rootHash, b -> b, b -> b);
}
private MerklePatriciaTrie<Bytes32, Bytes> newAccountStorageTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes32, Bytes> newAccountStorageTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
(location, hash) -> worldStateStorage.getAccountStorageTrieNode(null, location, hash),
rootHash,
@ -175,7 +174,7 @@ public class DefaultMutableWorldState implements MutableWorldState {
stateUpdater.putCode(null, code);
}
// Commit account storage tries
for (final MerklePatriciaTrie<Bytes32, Bytes> updatedStorage : updatedStorageTries.values()) {
for (final MerkleTrie<Bytes32, Bytes> updatedStorage : updatedStorageTries.values()) {
updatedStorage.commit(
(location, hash, value) ->
stateUpdater.putAccountStorageTrieNode(null, location, hash, value));
@ -224,7 +223,7 @@ public class DefaultMutableWorldState implements MutableWorldState {
final StateTrieAccountValue accountValue;
// Lazily initialized since we don't always access storage.
private volatile MerklePatriciaTrie<Bytes32, Bytes> storageTrie;
private volatile MerkleTrie<Bytes32, Bytes> storageTrie;
private WorldStateAccount(
final Address address, final Hash addressHash, final StateTrieAccountValue accountValue) {
@ -234,8 +233,8 @@ public class DefaultMutableWorldState implements MutableWorldState {
this.accountValue = accountValue;
}
private MerklePatriciaTrie<Bytes32, Bytes> storageTrie() {
final MerklePatriciaTrie<Bytes32, Bytes> updatedTrie = updatedStorageTries.get(address);
private MerkleTrie<Bytes32, Bytes> storageTrie() {
final MerkleTrie<Bytes32, Bytes> updatedTrie = updatedStorageTries.get(address);
if (updatedTrie != null) {
storageTrie = updatedTrie;
}
@ -398,7 +397,7 @@ public class DefaultMutableWorldState implements MutableWorldState {
final Map<UInt256, UInt256> updatedStorage = updated.getUpdatedStorage();
if (!updatedStorage.isEmpty()) {
// Apply any storage updates
final MerklePatriciaTrie<Bytes32, Bytes> storageTrie =
final MerkleTrie<Bytes32, Bytes> storageTrie =
freshState
? wrapped.newAccountStorageTrie(Hash.EMPTY_TRIE_HASH)
: origin.storageTrie();

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -21,7 +21,7 @@ import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.proof.WorldStateProof;
import org.hyperledger.besu.ethereum.proof.WorldStateProofProvider;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.evm.worldstate.WorldState;
import java.util.List;
@ -35,7 +35,7 @@ public class DefaultWorldStateArchive implements WorldStateArchive {
private final WorldStatePreimageStorage preimageStorage;
private final WorldStateProofProvider worldStateProof;
private static final Hash EMPTY_ROOT_HASH = Hash.wrap(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
private static final Hash EMPTY_ROOT_HASH = Hash.wrap(MerkleTrie.EMPTY_TRIE_NODE_HASH);
public DefaultWorldStateArchive(
final WorldStateStorage worldStateStorage, final WorldStatePreimageStorage preimageStorage) {

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -18,8 +18,8 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.metrics.ObservableMetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
@ -238,7 +238,7 @@ public class MarkSweepPruner {
return pendingMarks.contains(Bytes32.wrap(key)) || markStorage.containsKey(key);
}
private MerklePatriciaTrie<Bytes32, Bytes> createStateTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes32, Bytes> createStateTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
worldStateStorage::getAccountStateTrieNode,
rootHash,
@ -246,7 +246,7 @@ public class MarkSweepPruner {
Function.identity());
}
private MerklePatriciaTrie<Bytes32, Bytes> createStorageTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes32, Bytes> createStorageTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
(location, hash) -> worldStateStorage.getAccountStorageTrieNode(null, location, hash),
rootHash,

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -19,7 +19,7 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.proof.WorldStateProof;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.evm.worldstate.WorldState;
import java.io.Closeable;
@ -30,7 +30,7 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
public interface WorldStateArchive extends Closeable {
Hash EMPTY_ROOT_HASH = Hash.wrap(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
Hash EMPTY_ROOT_HASH = Hash.wrap(MerkleTrie.EMPTY_TRIE_NODE_HASH);
Optional<WorldState> get(Hash rootHash, Hash blockHash);

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -18,8 +18,8 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
@ -33,7 +33,7 @@ import org.apache.tuweni.units.bigints.UInt256;
public class TrieGenerator {
public static MerklePatriciaTrie<Bytes, Bytes> generateTrie(
public static MerkleTrie<Bytes, Bytes> generateTrie(
final WorldStateStorage worldStateStorage, final int nbAccounts) {
return generateTrie(
worldStateStorage,
@ -42,14 +42,13 @@ public class TrieGenerator {
.collect(Collectors.toList()));
}
public static MerklePatriciaTrie<Bytes, Bytes> generateTrie(
public static MerkleTrie<Bytes, Bytes> generateTrie(
final WorldStateStorage worldStateStorage, final List<Hash> accounts) {
final MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
emptyAccountStateTrie(worldStateStorage);
final MerkleTrie<Bytes, Bytes> accountStateTrie = emptyAccountStateTrie(worldStateStorage);
// Add some storage values
for (int i = 0; i < accounts.size(); i++) {
final WorldStateStorage.Updater updater = worldStateStorage.updater();
final MerklePatriciaTrie<Bytes, Bytes> storageTrie =
final MerkleTrie<Bytes, Bytes> storageTrie =
emptyStorageTrie(worldStateStorage, accounts.get(i));
writeStorageValue(updater, storageTrie, accounts.get(i), UInt256.ONE, UInt256.valueOf(2L));
writeStorageValue(
@ -80,7 +79,7 @@ public class TrieGenerator {
private static void writeStorageValue(
final WorldStateStorage.Updater updater,
final MerklePatriciaTrie<Bytes, Bytes> storageTrie,
final MerkleTrie<Bytes, Bytes> storageTrie,
final Hash hash,
final UInt256 key,
final UInt256 value) {
@ -101,7 +100,7 @@ public class TrieGenerator {
return RLP.encode(out -> out.writeBytes(storageValue.toMinimalBytes()));
}
public static MerklePatriciaTrie<Bytes, Bytes> emptyStorageTrie(
public static MerkleTrie<Bytes, Bytes> emptyStorageTrie(
final WorldStateStorage worldStateStorage, final Hash accountHash) {
return new StoredMerklePatriciaTrie<>(
(location, hash) ->
@ -110,7 +109,7 @@ public class TrieGenerator {
b -> b);
}
public static MerklePatriciaTrie<Bytes, Bytes> emptyAccountStateTrie(
public static MerkleTrie<Bytes, Bytes> emptyAccountStateTrie(
final WorldStateStorage worldStateStorage) {
return new StoredMerklePatriciaTrie<>(
worldStateStorage::getAccountStateTrieNode, b -> b, b -> b);

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -28,9 +28,9 @@ import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStor
import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
import org.hyperledger.besu.ethereum.core.TrieGenerator;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.StorageEntriesCollector;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import java.util.TreeMap;
@ -51,9 +51,8 @@ public class BonsaiWorldStateKeyValueStorageTest {
@Test
public void getAccountStateTrieNode_returnsEmptyNode() {
final BonsaiWorldStateKeyValueStorage storage = emptyStorage();
assertThat(
storage.getAccountStateTrieNode(Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
assertThat(storage.getAccountStateTrieNode(Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
}
@Test
@ -61,8 +60,8 @@ public class BonsaiWorldStateKeyValueStorageTest {
final BonsaiWorldStateKeyValueStorage storage = emptyStorage();
assertThat(
storage.getAccountStorageTrieNode(
Hash.EMPTY, Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
Hash.EMPTY, Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
}
@Test
@ -74,7 +73,7 @@ public class BonsaiWorldStateKeyValueStorageTest {
@Test
public void getNodeData_returnsEmptyNode() {
final BonsaiWorldStateKeyValueStorage storage = emptyStorage();
assertThat(storage.getNodeData(Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)).isEmpty();
assertThat(storage.getNodeData(Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH)).isEmpty();
}
@Test
@ -82,12 +81,12 @@ public class BonsaiWorldStateKeyValueStorageTest {
final BonsaiWorldStateKeyValueStorage storage = emptyStorage();
storage
.updater()
.putCode(Hash.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE)
.putCode(Hash.EMPTY, MerkleTrie.EMPTY_TRIE_NODE)
.putCode(Hash.EMPTY, Bytes.EMPTY)
.commit();
assertThat(storage.getCode(Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE), Hash.EMPTY))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
assertThat(storage.getCode(Hash.hash(MerkleTrie.EMPTY_TRIE_NODE), Hash.EMPTY))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
}
@Test
@ -105,15 +104,12 @@ public class BonsaiWorldStateKeyValueStorageTest {
storage
.updater()
.putAccountStateTrieNode(
Bytes.EMPTY,
Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE),
MerklePatriciaTrie.EMPTY_TRIE_NODE)
Bytes.EMPTY, Hash.hash(MerkleTrie.EMPTY_TRIE_NODE), MerkleTrie.EMPTY_TRIE_NODE)
.putAccountStateTrieNode(Bytes.EMPTY, Hash.hash(Bytes.EMPTY), Bytes.EMPTY)
.commit();
assertThat(
storage.getAccountStateTrieNode(Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
assertThat(storage.getAccountStateTrieNode(Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
assertThat(storage.getAccountStateTrieNode(Bytes.EMPTY, Hash.EMPTY)).contains(Bytes.EMPTY);
}
@ -135,15 +131,15 @@ public class BonsaiWorldStateKeyValueStorageTest {
.putAccountStorageTrieNode(
Hash.EMPTY,
Bytes.EMPTY,
Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE),
MerklePatriciaTrie.EMPTY_TRIE_NODE)
Hash.hash(MerkleTrie.EMPTY_TRIE_NODE),
MerkleTrie.EMPTY_TRIE_NODE)
.putAccountStorageTrieNode(Hash.EMPTY, Bytes.EMPTY, Hash.hash(Bytes.EMPTY), Bytes.EMPTY)
.commit();
assertThat(
storage.getAccountStorageTrieNode(
Hash.EMPTY, Bytes.EMPTY, Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE)))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
Hash.EMPTY, Bytes.EMPTY, Hash.hash(MerkleTrie.EMPTY_TRIE_NODE)))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
assertThat(storage.getAccountStorageTrieNode(Hash.EMPTY, Bytes.EMPTY, Hash.EMPTY))
.contains(Bytes.EMPTY);
}
@ -166,7 +162,7 @@ public class BonsaiWorldStateKeyValueStorageTest {
@Test
public void getAccount_loadFromTrieWhenEmpty() {
final BonsaiWorldStateKeyValueStorage storage = spy(emptyStorage());
MerklePatriciaTrie<Bytes, Bytes> trie = TrieGenerator.generateTrie(storage, 1);
MerkleTrie<Bytes, Bytes> trie = TrieGenerator.generateTrie(storage, 1);
final TreeMap<Bytes32, Bytes> accounts =
(TreeMap<Bytes32, Bytes>)
trie.entriesFrom(root -> StorageEntriesCollector.collectEntries(root, Hash.ZERO, 1));
@ -190,7 +186,7 @@ public class BonsaiWorldStateKeyValueStorageTest {
@Test
public void getStorage_loadFromTrieWhenEmpty() {
final BonsaiWorldStateKeyValueStorage storage = spy(emptyStorage());
final MerklePatriciaTrie<Bytes, Bytes> trie = TrieGenerator.generateTrie(storage, 1);
final MerkleTrie<Bytes, Bytes> trie = TrieGenerator.generateTrie(storage, 1);
final TreeMap<Bytes32, Bytes> accounts =
(TreeMap<Bytes32, Bytes>)
trie.entriesFrom(root -> StorageEntriesCollector.collectEntries(root, Hash.ZERO, 1));

@ -1,5 +1,5 @@
/*
* Copyright Hyperledger Besu contributors.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -25,9 +25,9 @@ import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
import org.hyperledger.besu.ethereum.core.TrieGenerator;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.TrieIterator;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
@ -51,7 +51,7 @@ public class CachedMerkleTrieLoaderTest {
final List<Address> accounts =
List.of(Address.fromHexString("0xdeadbeef"), Address.fromHexString("0xdeadbeee"));
private MerklePatriciaTrie<Bytes, Bytes> trie;
private MerkleTrie<Bytes, Bytes> trie;
@Before
public void setup() {

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -26,7 +26,7 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.data.PrivacyGenesis;
@ -42,7 +42,7 @@ import org.apache.tuweni.units.bigints.UInt256;
import org.junit.Test;
public class PrivateStateGenesisAllocatorTest {
public static final Hash EMPTY_ROOT_HASH = Hash.wrap(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
public static final Hash EMPTY_ROOT_HASH = Hash.wrap(MerkleTrie.EMPTY_TRIE_NODE_HASH);
private final MutableWorldState worldState =
InMemoryKeyValueStorageProvider.createInMemoryWorldState();

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -21,8 +21,8 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;
@ -68,8 +68,8 @@ public class WorldStateProofProviderTest {
@Test
public void getProofWhenWorldStateAvailable() {
final Hash addressHash = Hash.hash(address);
final MerklePatriciaTrie<Bytes32, Bytes> worldStateTrie = emptyWorldStateTrie(addressHash);
final MerklePatriciaTrie<Bytes32, Bytes> storageTrie = emptyStorageTrie();
final MerkleTrie<Bytes32, Bytes> worldStateTrie = emptyWorldStateTrie(addressHash);
final MerkleTrie<Bytes32, Bytes> storageTrie = emptyStorageTrie();
final WorldStateStorage.Updater updater = worldStateStorage.updater();
@ -121,7 +121,7 @@ public class WorldStateProofProviderTest {
@Test
public void getProofWhenStateTrieAccountUnavailable() {
final MerklePatriciaTrie<Bytes32, Bytes> worldStateTrie = emptyWorldStateTrie(null);
final MerkleTrie<Bytes32, Bytes> worldStateTrie = emptyWorldStateTrie(null);
final Optional<WorldStateProof> accountProof =
worldStateProofProvider.getAccountProof(
@ -131,9 +131,7 @@ public class WorldStateProofProviderTest {
}
private void writeStorageValue(
final MerklePatriciaTrie<Bytes32, Bytes> storageTrie,
final UInt256 key,
final UInt256 value) {
final MerkleTrie<Bytes32, Bytes> storageTrie, final UInt256 key, final UInt256 value) {
storageTrie.put(storageKeyHash(key), encodeStorageValue(value));
}
@ -145,12 +143,12 @@ public class WorldStateProofProviderTest {
return RLP.encode(out -> out.writeBytes(storageValue.toMinimalBytes()));
}
private MerklePatriciaTrie<Bytes32, Bytes> emptyStorageTrie() {
private MerkleTrie<Bytes32, Bytes> emptyStorageTrie() {
return new StoredMerklePatriciaTrie<>(
worldStateStorage::getAccountStateTrieNode, b -> b, b -> b);
}
private MerklePatriciaTrie<Bytes32, Bytes> emptyWorldStateTrie(final Hash accountHash) {
private MerkleTrie<Bytes32, Bytes> emptyWorldStateTrie(final Hash accountHash) {
return new StoredMerklePatriciaTrie<>(
(location, hash) ->
worldStateStorage.getAccountStorageTrieNode(accountHash, location, hash),

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -19,7 +19,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.TrieGenerator;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector;
import org.hyperledger.besu.ethereum.trie.TrieIterator;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
@ -53,7 +53,7 @@ public class WorldStateRangeProofProviderTest {
@Test
public void rangeProofValidationNominalCase() {
final MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
final MerkleTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, 15);
// collect accounts in range
final RangeStorageEntriesCollector collector =
@ -82,8 +82,7 @@ public class WorldStateRangeProofProviderTest {
@Test
public void rangeProofValidationMissingAccount() {
MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, 15);
MerkleTrie<Bytes, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
// collect accounts in range
final RangeStorageEntriesCollector collector =
RangeStorageEntriesCollector.createCollector(Hash.ZERO, MAX_RANGE, 10, Integer.MAX_VALUE);
@ -120,8 +119,7 @@ public class WorldStateRangeProofProviderTest {
@Test
public void rangeProofValidationNoMonotonicIncreasing() {
MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, 15);
MerkleTrie<Bytes, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
// generate the invalid proof
final RangeStorageEntriesCollector collector =
@ -157,8 +155,7 @@ public class WorldStateRangeProofProviderTest {
@Test
public void rangeProofValidationEmptyProof() {
MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, 15);
MerkleTrie<Bytes, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
// generate the invalid proof
final RangeStorageEntriesCollector collector =
@ -185,8 +182,7 @@ public class WorldStateRangeProofProviderTest {
@Test
public void rangeProofValidationInvalidEmptyProof() {
MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, 15);
MerkleTrie<Bytes, Bytes> accountStateTrie = TrieGenerator.generateTrie(worldStateStorage, 15);
// generate the invalid proof
final RangeStorageEntriesCollector collector =

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -18,7 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage.Updater;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;
import org.apache.tuweni.bytes.Bytes;
@ -36,18 +36,16 @@ public class KeyValueStorageWorldStateStorageTest {
@Test
public void getAccountStateTrieNode_returnsEmptyNode() {
final WorldStateKeyValueStorage storage = emptyStorage();
assertThat(
storage.getAccountStateTrieNode(Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
assertThat(storage.getAccountStateTrieNode(Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
}
@Test
public void getAccountStorageTrieNode_returnsEmptyNode() {
final WorldStateKeyValueStorage storage = emptyStorage();
assertThat(
storage.getAccountStorageTrieNode(
null, Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
storage.getAccountStorageTrieNode(null, Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
}
@Test
@ -59,21 +57,17 @@ public class KeyValueStorageWorldStateStorageTest {
@Test
public void getNodeData_returnsEmptyNode() {
final WorldStateKeyValueStorage storage = emptyStorage();
assertThat(storage.getNodeData(Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
assertThat(storage.getNodeData(Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
}
@Test
public void getCode_saveAndGetSpecialValues() {
final WorldStateKeyValueStorage storage = emptyStorage();
storage
.updater()
.putCode(null, MerklePatriciaTrie.EMPTY_TRIE_NODE)
.putCode(null, Bytes.EMPTY)
.commit();
storage.updater().putCode(null, MerkleTrie.EMPTY_TRIE_NODE).putCode(null, Bytes.EMPTY).commit();
assertThat(storage.getCode(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH, null))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
assertThat(storage.getCode(MerkleTrie.EMPTY_TRIE_NODE_HASH, null))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
assertThat(storage.getCode(Hash.EMPTY, null)).contains(Bytes.EMPTY);
}
@ -92,13 +86,12 @@ public class KeyValueStorageWorldStateStorageTest {
storage
.updater()
.putAccountStateTrieNode(
null, Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE), MerklePatriciaTrie.EMPTY_TRIE_NODE)
null, Hash.hash(MerkleTrie.EMPTY_TRIE_NODE), MerkleTrie.EMPTY_TRIE_NODE)
.putAccountStateTrieNode(null, Hash.hash(Bytes.EMPTY), Bytes.EMPTY)
.commit();
assertThat(
storage.getAccountStateTrieNode(Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
assertThat(storage.getAccountStateTrieNode(Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
assertThat(storage.getAccountStateTrieNode(Bytes.EMPTY, Hash.EMPTY)).contains(Bytes.EMPTY);
}
@ -117,17 +110,13 @@ public class KeyValueStorageWorldStateStorageTest {
storage
.updater()
.putAccountStorageTrieNode(
null,
null,
Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE),
MerklePatriciaTrie.EMPTY_TRIE_NODE)
null, null, Hash.hash(MerkleTrie.EMPTY_TRIE_NODE), MerkleTrie.EMPTY_TRIE_NODE)
.putAccountStorageTrieNode(null, null, Hash.hash(Bytes.EMPTY), Bytes.EMPTY)
.commit();
assertThat(
storage.getAccountStorageTrieNode(
null, Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
storage.getAccountStorageTrieNode(null, Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
assertThat(storage.getAccountStorageTrieNode(null, Bytes.EMPTY, Hash.EMPTY))
.contains(Bytes.EMPTY);
}
@ -147,15 +136,12 @@ public class KeyValueStorageWorldStateStorageTest {
storage
.updater()
.putAccountStorageTrieNode(
null,
null,
Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE),
MerklePatriciaTrie.EMPTY_TRIE_NODE)
null, null, Hash.hash(MerkleTrie.EMPTY_TRIE_NODE), MerkleTrie.EMPTY_TRIE_NODE)
.putAccountStorageTrieNode(null, null, Hash.hash(Bytes.EMPTY), Bytes.EMPTY)
.commit();
assertThat(storage.getNodeData(Bytes.EMPTY, MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerklePatriciaTrie.EMPTY_TRIE_NODE);
assertThat(storage.getNodeData(Bytes.EMPTY, MerkleTrie.EMPTY_TRIE_NODE_HASH))
.contains(MerkleTrie.EMPTY_TRIE_NODE);
assertThat(storage.getNodeData(Bytes.EMPTY, Hash.EMPTY)).contains(Bytes.EMPTY);
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -23,7 +23,7 @@ import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.evm.account.AccountStorageEntry;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.WorldState;
@ -66,10 +66,10 @@ public class DefaultMutableWorldStateTest {
@Test
public void rootHash_Empty() {
final MutableWorldState worldState = createEmpty();
assertThat(worldState.rootHash()).isEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
worldState.persist(null);
assertThat(worldState.rootHash()).isEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
}
@Test
@ -97,10 +97,10 @@ public class DefaultMutableWorldStateTest {
final WorldUpdater updater = worldState.updater();
updater.deleteAccount(ADDRESS);
updater.commit();
assertThat(worldState.rootHash()).isEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
worldState.persist(null);
assertThat(worldState.rootHash()).isEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
}
@Test
@ -110,10 +110,10 @@ public class DefaultMutableWorldStateTest {
updater.createAccount(ADDRESS).getMutable().setBalance(Wei.of(100000));
updater.deleteAccount(ADDRESS);
updater.commit();
assertThat(worldState.rootHash()).isEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
worldState.persist(null);
assertThat(worldState.rootHash()).isEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
}
@Test
@ -124,7 +124,7 @@ public class DefaultMutableWorldStateTest {
updater.createAccount(ADDRESS).getMutable().setBalance(Wei.of(100000));
updater.commit();
assertThat(worldState.get(ADDRESS)).isNotNull();
assertThat(worldState.rootHash()).isNotEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isNotEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
// Delete account
updater = worldState.updater();
@ -134,7 +134,7 @@ public class DefaultMutableWorldStateTest {
updater.commit();
assertThat(updater.get(ADDRESS)).isNull();
assertThat(worldState.rootHash()).isEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
}
@Test
@ -146,7 +146,7 @@ public class DefaultMutableWorldStateTest {
updater.commit();
worldState.persist(null);
assertThat(worldState.get(ADDRESS)).isNotNull();
assertThat(worldState.rootHash()).isNotEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isNotEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
// Delete account
updater = worldState.updater();
@ -160,7 +160,7 @@ public class DefaultMutableWorldStateTest {
worldState.persist(null);
assertThat(updater.get(ADDRESS)).isNull();
assertThat(worldState.rootHash()).isEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
}
@Test
@ -475,7 +475,7 @@ public class DefaultMutableWorldStateTest {
updater.commit();
worldState.persist(null);
assertThat(worldState.get(ADDRESS)).isNotNull();
assertThat(worldState.rootHash()).isNotEqualTo(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
assertThat(worldState.rootHash()).isNotEqualTo(MerkleTrie.EMPTY_TRIE_NODE_HASH);
// Clear storage
account = updater.getAccount(ADDRESS).getMutable();

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -31,8 +31,8 @@ import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.evm.worldstate.WorldState;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;
@ -220,7 +220,7 @@ public class MarkSweepPrunerTest {
private Set<Bytes> collectWorldStateNodes(final Hash stateRootHash, final Set<Bytes> collector) {
final List<Hash> storageRoots = new ArrayList<>();
final MerklePatriciaTrie<Bytes32, Bytes> stateTrie = createStateTrie(stateRootHash);
final MerkleTrie<Bytes32, Bytes> stateTrie = createStateTrie(stateRootHash);
// Collect storage roots and code
stateTrie
@ -239,15 +239,14 @@ public class MarkSweepPrunerTest {
collectTrieNodes(stateTrie, collector);
// Collect storage nodes
for (Hash storageRoot : storageRoots) {
final MerklePatriciaTrie<Bytes32, Bytes> storageTrie = createStorageTrie(storageRoot);
final MerkleTrie<Bytes32, Bytes> storageTrie = createStorageTrie(storageRoot);
collectTrieNodes(storageTrie, collector);
}
return collector;
}
private void collectTrieNodes(
final MerklePatriciaTrie<Bytes32, Bytes> trie, final Set<Bytes> collector) {
private void collectTrieNodes(final MerkleTrie<Bytes32, Bytes> trie, final Set<Bytes> collector) {
final Bytes32 rootHash = trie.getRootHash();
trie.visitAll(
(node) -> {
@ -257,7 +256,7 @@ public class MarkSweepPrunerTest {
});
}
private MerklePatriciaTrie<Bytes32, Bytes> createStateTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes32, Bytes> createStateTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
worldStateStorage::getAccountStateTrieNode,
rootHash,
@ -265,7 +264,7 @@ public class MarkSweepPrunerTest {
Function.identity());
}
private MerklePatriciaTrie<Bytes32, Bytes> createStorageTrie(final Bytes32 rootHash) {
private MerkleTrie<Bytes32, Bytes> createStorageTrie(final Bytes32 rootHash) {
return new StoredMerklePatriciaTrie<>(
(location, hash) -> worldStateStorage.getAccountStorageTrieNode(null, location, hash),
rootHash,

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -19,7 +19,7 @@ import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStor
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPOutput;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage.Updater;
@ -83,7 +83,7 @@ class AccountTrieNodeDataRequest extends TrieNodeDataRequest {
builder.add(createCodeRequest(accountValue.getCodeHash(), accountHash));
}
// Add storage, if appropriate
if (!accountValue.getStorageRoot().equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
if (!accountValue.getStorageRoot().equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
// If storage is non-empty queue download
final NodeDataRequest storageNode =

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -16,7 +16,7 @@ package org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.TrieNodeDecoder;
import org.hyperledger.besu.ethereum.trie.patricia.TrieNodeDecoder;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import java.util.List;

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -17,7 +17,7 @@ package org.hyperledger.besu.ethereum.eth.sync.snapsync;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.trie.InnerNodeDiscoveryManager;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import java.math.BigInteger;
import java.util.HashMap;

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -17,11 +17,11 @@ package org.hyperledger.besu.ethereum.eth.sync.snapsync;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.trie.CommitVisitor;
import org.hyperledger.besu.ethereum.trie.InnerNodeDiscoveryManager;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NodeUpdater;
import org.hyperledger.besu.ethereum.trie.SnapPutVisitor;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import java.util.ArrayList;
import java.util.HashMap;
@ -108,10 +108,9 @@ public class StackTrie {
keys.lastKey(),
true);
final MerklePatriciaTrie<Bytes, Bytes> trie =
final MerkleTrie<Bytes, Bytes> trie =
new StoredMerklePatriciaTrie<>(
snapStoredNodeFactory,
proofs.isEmpty() ? MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH : rootHash);
snapStoredNodeFactory, proofs.isEmpty() ? MerkleTrie.EMPTY_TRIE_NODE_HASH : rootHash);
for (Map.Entry<Bytes32, Bytes> account : keys.entrySet()) {
trie.put(account.getKey(), new SnapPutVisitor<>(snapStoredNodeFactory, account.getValue()));

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -20,8 +20,8 @@ import org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState;
import org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapWorldDownloadState;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
@ -142,7 +142,7 @@ public class AccountTrieNodeDataRequest extends TrieNodeDataRequest {
builder.add(createBytecodeRequest(accountHash, getRootHash(), accountValue.getCodeHash()));
}
// Add storage, if appropriate
if (!accountValue.getStorageRoot().equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
if (!accountValue.getStorageRoot().equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {
// If we detect an account storage we fill it with snapsync before completing with a heal
final SnapDataRequest storageTrieRequest =
createStorageTrieNodeDataRequest(

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -21,7 +21,7 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState;
import org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapWorldDownloadState;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.TrieNodeDecoder;
import org.hyperledger.besu.ethereum.trie.patricia.TrieNodeDecoder;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.services.tasks.TasksPriorityProvider;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -51,10 +51,10 @@ import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.TrieNodeDecoder;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.TrieNodeDecoder;
import org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
@ -104,7 +104,7 @@ public class FastWorldStateDownloaderTest {
@Rule public Timeout globalTimeout = Timeout.seconds(60); // 1 minute max per test
private static final Hash EMPTY_TRIE_ROOT = Hash.wrap(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH);
private static final Hash EMPTY_TRIE_ROOT = Hash.wrap(MerkleTrie.EMPTY_TRIE_NODE_HASH);
private final BlockDataGenerator dataGen = new BlockDataGenerator(1);
private final ExecutorService persistenceThread =

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -24,8 +24,8 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.patricia.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.services.tasks.Task;
@ -97,7 +97,7 @@ public class PersistDataStepTest {
private Task<NodeDataRequest> createTaskWithData(final Bytes data) {
final StubTask task = createTaskWithoutData(data);
MerklePatriciaTrie<Bytes, String> trie =
MerkleTrie<Bytes, String> trie =
new SimpleMerklePatriciaTrie<>(
value -> (value != null) ? Bytes.wrap(value.getBytes(StandardCharsets.UTF_8)) : null);
trie.put(data, "01");

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -25,7 +25,7 @@ import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataR
import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest;
import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest;
import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.services.tasks.Task;

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -18,7 +18,7 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.TrieGenerator;
import org.hyperledger.besu.ethereum.proof.WorldStateProofProvider;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector;
import org.hyperledger.besu.ethereum.trie.TrieIterator;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
@ -101,7 +101,7 @@ public final class RangeManagerTest {
final WorldStateStorage worldStateStorage =
new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
final MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
final MerkleTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, 15);
final RangeStorageEntriesCollector collector =
@ -140,7 +140,7 @@ public final class RangeManagerTest {
final WorldStateStorage worldStateStorage =
new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
final MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
final MerkleTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, 15);
final RangeStorageEntriesCollector collector =

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -18,7 +18,7 @@ import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.TrieGenerator;
import org.hyperledger.besu.ethereum.proof.WorldStateProofProvider;
import org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector;
import org.hyperledger.besu.ethereum.trie.TrieIterator;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
@ -47,7 +47,7 @@ public class StackTrieTest {
final WorldStateStorage recreatedWorldStateStorage =
new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
final MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
final MerkleTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, nbAccounts);
final StackTrie stackTrie =
@ -99,7 +99,7 @@ public class StackTrieTest {
final WorldStateStorage recreatedWorldStateStorage =
new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
final MerklePatriciaTrie<Bytes, Bytes> accountStateTrie =
final MerkleTrie<Bytes, Bytes> accountStateTrie =
TrieGenerator.generateTrie(worldStateStorage, nbAccounts);
final StackTrie stackTrie =

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -23,10 +23,10 @@ import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest;
import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest;
import org.hyperledger.besu.ethereum.proof.WorldStateProofProvider;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector;
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.TrieIterator;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
@ -49,7 +49,7 @@ public class TaskGenerator {
final WorldStateProofProvider worldStateProofProvider =
new WorldStateProofProvider(worldStateStorage);
final MerklePatriciaTrie<Bytes, Bytes> trie = TrieGenerator.generateTrie(worldStateStorage, 1);
final MerkleTrie<Bytes, Bytes> trie = TrieGenerator.generateTrie(worldStateStorage, 1);
final RangeStorageEntriesCollector collector =
RangeStorageEntriesCollector.createCollector(
Bytes32.ZERO, RangeManager.MAX_RANGE, 1, Integer.MAX_VALUE);

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -17,6 +17,7 @@ package org.hyperledger.besu.ethereum.trie;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.hyperledger.besu.ethereum.trie.patricia.SimpleMerklePatriciaTrie;
import org.hyperledger.besu.testutil.JsonTestParameters;
import java.util.function.Function;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,13 +14,16 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import java.util.function.Consumer;
public class AllNodesVisitor<V> implements NodeVisitor<V> {
private final Consumer<Node<V>> handler;
AllNodesVisitor(final Consumer<Node<V>> handler) {
public AllNodesVisitor(final Consumer<Node<V>> handler) {
this.handler = handler;
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,6 +14,9 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import org.apache.tuweni.bytes.Bytes;
public class CommitVisitor<V> implements LocationNodeVisitor<V> {
@ -44,10 +47,11 @@ public class CommitVisitor<V> implements LocationNodeVisitor<V> {
return;
}
for (byte i = 0; i < BranchNode.RADIX; ++i) {
final Node<V> child = branchNode.child(i);
for (int i = 0; i < branchNode.maxChild(); ++i) {
Bytes index = Bytes.of(i);
final Node<V> child = branchNode.child((byte) i);
if (child.isDirty()) {
child.accept(Bytes.concatenate(location, Bytes.of(i)), this);
child.accept(Bytes.concatenate(location, index), this);
}
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -22,7 +22,7 @@ import org.apache.tuweni.bytes.MutableBytes;
public abstract class CompactEncoding {
private CompactEncoding() {}
static final byte LEAF_TERMINATOR = 0x10;
public static final byte LEAF_TERMINATOR = 0x10;
public static Bytes bytesToPath(final Bytes bytes) {
final MutableBytes path = MutableBytes.create(bytes.size() * 2 + 1);

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -15,6 +15,9 @@
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import org.hyperledger.besu.ethereum.trie.patricia.StoredNodeFactory;
import java.util.ArrayList;
import java.util.Arrays;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -29,7 +29,7 @@ import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
class LeafNode<V> implements Node<V> {
public class LeafNode<V> implements Node<V> {
private final Optional<Bytes> location;
private final Bytes path;
private final V value;
@ -39,7 +39,7 @@ class LeafNode<V> implements Node<V> {
private SoftReference<Bytes32> hash;
private boolean dirty = false;
LeafNode(
public LeafNode(
final Bytes location,
final Bytes path,
final V value,
@ -52,7 +52,7 @@ class LeafNode<V> implements Node<V> {
this.valueSerializer = valueSerializer;
}
LeafNode(
public LeafNode(
final Bytes path,
final V value,
final NodeFactory<V> nodeFactory,

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,9 +14,12 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import org.apache.tuweni.bytes.Bytes;
interface LocationNodeVisitor<V> {
public interface LocationNodeVisitor<V> {
void visit(Bytes location, ExtensionNode<V> extensionNode);

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -17,6 +17,7 @@ package org.hyperledger.besu.ethereum.trie;
import static org.hyperledger.besu.crypto.Hash.keccak256;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.patricia.RemoveVisitor;
import java.util.Map;
import java.util.Optional;
@ -29,7 +30,7 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
/** An Merkle Patricial Trie. */
public interface MerklePatriciaTrie<K, V> {
public interface MerkleTrie<K, V> {
Bytes EMPTY_TRIE_NODE = RLP.NULL;
Bytes32 EMPTY_TRIE_NODE_HASH = keccak256(EMPTY_TRIE_NODE);
@ -67,6 +68,14 @@ public interface MerklePatriciaTrie<K, V> {
*/
void put(K key, V value);
/**
* Updates the value mapped to the specified path, creating the mapping if one does not already
* exist.
*
* @param path path of the node to be updated.
* @param value The value to associate the key with.
*/
void putPath(K path, V value);
/**
* Updates the value mapped to the specified key, creating the mapping if one does not already
* exist.
@ -74,7 +83,7 @@ public interface MerklePatriciaTrie<K, V> {
* @param key The key that corresponds to the value to be updated.
* @param putVisitor custom visitor for the update
*/
void put(K key, PutVisitor<V> putVisitor);
void put(K key, PathNodeVisitor<V> putVisitor);
/**
* Deletes the value mapped to the specified key, if such a value exists (Optional operation).

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,10 +14,11 @@
*/
package org.hyperledger.besu.ethereum.trie;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
public interface NodeFactory<V> {
@ -25,7 +26,11 @@ public interface NodeFactory<V> {
Node<V> createBranch(byte leftIndex, Node<V> left, byte rightIndex, Node<V> right);
Node<V> createBranch(ArrayList<Node<V>> newChildren, Optional<V> value);
Node<V> createBranch(List<Node<V>> newChildren, Optional<V> value);
Node<V> createLeaf(Bytes path, V value);
default Optional<Node<V>> retrieve(final Bytes location, final Bytes32 hash) {
throw new UnsupportedOperationException("retrieve is not implemented");
}
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,7 +14,10 @@
*/
package org.hyperledger.besu.ethereum.trie;
interface NodeVisitor<V> {
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
public interface NodeVisitor<V> {
void visit(ExtensionNode<V> extensionNode);

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -64,17 +64,17 @@ public class NullNode<V> implements Node<V> {
@Override
public Bytes getRlp() {
return MerklePatriciaTrie.EMPTY_TRIE_NODE;
return MerkleTrie.EMPTY_TRIE_NODE;
}
@Override
public Bytes getRlpRef() {
return MerklePatriciaTrie.EMPTY_TRIE_NODE;
return MerkleTrie.EMPTY_TRIE_NODE;
}
@Override
public Bytes32 getHash() {
return MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH;
return MerkleTrie.EMPTY_TRIE_NODE_HASH;
}
@Override

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,9 +14,12 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import org.apache.tuweni.bytes.Bytes;
interface PathNodeVisitor<V> {
public interface PathNodeVisitor<V> {
Node<V> visit(ExtensionNode<V> extensionNode, Bytes path);

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -16,6 +16,9 @@
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import java.util.function.BiConsumer;
import org.apache.tuweni.bytes.Bytes;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,17 +14,21 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import org.hyperledger.besu.ethereum.trie.patricia.GetVisitor;
import java.util.ArrayList;
import java.util.List;
import org.apache.tuweni.bytes.Bytes;
class ProofVisitor<V> extends GetVisitor<V> implements PathNodeVisitor<V> {
public class ProofVisitor<V> extends GetVisitor<V> implements PathNodeVisitor<V> {
private final Node<V> rootNode;
private final List<Node<V>> proof = new ArrayList<>();
ProofVisitor(final Node<V> rootNode) {
public ProofVisitor(final Node<V> rootNode) {
this.rootNode = rootNode;
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,6 +14,10 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.DefaultNodeFactory;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -18,6 +18,9 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.stream.Collectors.toUnmodifiableSet;
import static org.hyperledger.besu.ethereum.trie.CompactEncoding.bytesToPath;
import org.hyperledger.besu.ethereum.trie.patricia.DefaultNodeFactory;
import org.hyperledger.besu.ethereum.trie.patricia.RemoveVisitor;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -32,14 +35,13 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
/**
* An in-memory {@link MerklePatriciaTrie}.
* An in-memory {@link MerkleTrie}.
*
* @param <V> The type of values stored by this trie.
*/
public class SimpleMerklePatriciaTrie<K extends Bytes, V> implements MerklePatriciaTrie<K, V> {
private final PathNodeVisitor<V> getVisitor = new GetVisitor<>();
private final PathNodeVisitor<V> removeVisitor = new RemoveVisitor<>();
private final DefaultNodeFactory<V> nodeFactory;
public abstract class SimpleMerkleTrie<K extends Bytes, V> implements MerkleTrie<K, V> {
protected final DefaultNodeFactory<V> nodeFactory;
private Node<V> root;
@ -48,7 +50,7 @@ public class SimpleMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
*
* @param valueSerializer A function for serializing values to bytes.
*/
public SimpleMerklePatriciaTrie(final Function<V, Bytes> valueSerializer) {
public SimpleMerkleTrie(final Function<V, Bytes> valueSerializer) {
this.nodeFactory = new DefaultNodeFactory<>(valueSerializer);
this.root = NullNode.instance();
}
@ -56,13 +58,13 @@ public class SimpleMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
@Override
public Optional<V> get(final K key) {
checkNotNull(key);
return root.accept(getVisitor, bytesToPath(key)).getValue();
return root.accept(getGetVisitor(), bytesToPath(key)).getValue();
}
@Override
public Optional<V> getPath(final K path) {
checkNotNull(path);
return root.accept(getVisitor, path).getValue();
return root.accept(getGetVisitor(), path).getValue();
}
@Override
@ -79,11 +81,18 @@ public class SimpleMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
public void put(final K key, final V value) {
checkNotNull(key);
checkNotNull(value);
this.root = root.accept(new PutVisitor<>(nodeFactory, value), bytesToPath(key));
this.root = root.accept(getPutVisitor(value), bytesToPath(key));
}
@Override
public void putPath(final K path, final V value) {
checkNotNull(path);
checkNotNull(value);
this.root = root.accept(getPutVisitor(value), path);
}
@Override
public void put(final K key, final PutVisitor<V> putVisitor) {
public void put(final K key, final PathNodeVisitor<V> putVisitor) {
checkNotNull(key);
this.root = root.accept(putVisitor, bytesToPath(key));
}
@ -91,7 +100,7 @@ public class SimpleMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
@Override
public void remove(final K key) {
checkNotNull(key);
this.root = root.accept(removeVisitor, bytesToPath(key));
this.root = root.accept(getRemoveVisitor(), bytesToPath(key));
}
@Override
@ -157,4 +166,10 @@ public class SimpleMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
final TrieIterator<V> visitor = new TrieIterator<>(handler, true);
root.accept(visitor, CompactEncoding.bytesToPath(Bytes32.ZERO));
}
public abstract PathNodeVisitor<V> getGetVisitor();
public abstract PathNodeVisitor<V> getRemoveVisitor();
public abstract PathNodeVisitor<V> getPutVisitor(final V value);
}

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,6 +14,10 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import org.hyperledger.besu.ethereum.trie.patricia.PutVisitor;
import org.apache.tuweni.bytes.Bytes;
public class SnapPutVisitor<V> extends PutVisitor<V> {

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -18,6 +18,9 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.stream.Collectors.toUnmodifiableSet;
import static org.hyperledger.besu.ethereum.trie.CompactEncoding.bytesToPath;
import org.hyperledger.besu.ethereum.trie.patricia.RemoveVisitor;
import org.hyperledger.besu.ethereum.trie.patricia.StoredNodeFactory;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -32,72 +35,42 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
/**
* A {@link MerklePatriciaTrie} that persists trie nodes to a {@link MerkleStorage} key/value store.
* A {@link MerkleTrie} that persists trie nodes to a {@link MerkleStorage} key/value store.
*
* @param <V> The type of values stored by this trie.
*/
public class StoredMerklePatriciaTrie<K extends Bytes, V> implements MerklePatriciaTrie<K, V> {
public abstract class StoredMerkleTrie<K extends Bytes, V> implements MerkleTrie<K, V> {
private final GetVisitor<V> getVisitor = new GetVisitor<>();
private final RemoveVisitor<V> removeVisitor = new RemoveVisitor<>();
private final StoredNodeFactory<V> nodeFactory;
protected final NodeFactory<V> nodeFactory;
private Node<V> root;
/**
* Create a trie.
*
* @param nodeLoader The {@link NodeLoader} to retrieve node data from.
* @param valueSerializer A function for serializing values to bytes.
* @param valueDeserializer A function for deserializing values from bytes.
* @param nodeFactory The {@link StoredNodeFactory} to retrieve node.
*/
public StoredMerklePatriciaTrie(
final NodeLoader nodeLoader,
final Function<V, Bytes> valueSerializer,
final Function<Bytes, V> valueDeserializer) {
this(nodeLoader, EMPTY_TRIE_NODE_HASH, valueSerializer, valueDeserializer);
public StoredMerkleTrie(final NodeFactory<V> nodeFactory) {
this(nodeFactory, EMPTY_TRIE_NODE_HASH);
}
/**
* Create a trie.
*
* @param nodeLoader The {@link NodeLoader} to retrieve node data from.
* @param nodeFactory The {@link StoredNodeFactory} to retrieve node.
* @param rootHash The initial root has for the trie, which should be already present in {@code
* storage}.
* @param rootLocation The initial root location for the trie
* @param valueSerializer A function for serializing values to bytes.
* @param valueDeserializer A function for deserializing values from bytes.
*/
public StoredMerklePatriciaTrie(
final NodeLoader nodeLoader,
final Bytes32 rootHash,
final Bytes rootLocation,
final Function<V, Bytes> valueSerializer,
final Function<Bytes, V> valueDeserializer) {
this.nodeFactory = new StoredNodeFactory<>(nodeLoader, valueSerializer, valueDeserializer);
public StoredMerkleTrie(
final NodeFactory<V> nodeFactory, final Bytes32 rootHash, final Bytes rootLocation) {
this.nodeFactory = nodeFactory;
this.root =
rootHash.equals(EMPTY_TRIE_NODE_HASH)
? NullNode.instance()
: new StoredNode<>(nodeFactory, rootLocation, rootHash);
}
/**
* Create a trie.
*
* @param nodeLoader The {@link NodeLoader} to retrieve node data from.
* @param rootHash The initial root has for the trie, which should be already present in {@code
* storage}.
* @param valueSerializer A function for serializing values to bytes.
* @param valueDeserializer A function for deserializing values from bytes.
*/
public StoredMerklePatriciaTrie(
final NodeLoader nodeLoader,
final Bytes32 rootHash,
final Function<V, Bytes> valueSerializer,
final Function<Bytes, V> valueDeserializer) {
this(nodeLoader, rootHash, Bytes.EMPTY, valueSerializer, valueDeserializer);
}
/**
* Create a trie.
*
@ -105,7 +78,7 @@ public class StoredMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
* @param rootHash The initial root hash for the trie, which should be already present in {@code
* storage}.
*/
public StoredMerklePatriciaTrie(final StoredNodeFactory<V> nodeFactory, final Bytes32 rootHash) {
public StoredMerkleTrie(final NodeFactory<V> nodeFactory, final Bytes32 rootHash) {
this.nodeFactory = nodeFactory;
this.root =
rootHash.equals(EMPTY_TRIE_NODE_HASH)
@ -116,13 +89,13 @@ public class StoredMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
@Override
public Optional<V> get(final K key) {
checkNotNull(key);
return root.accept(getVisitor, bytesToPath(key)).getValue();
return root.accept(getGetVisitor(), bytesToPath(key)).getValue();
}
@Override
public Optional<V> getPath(final K path) {
checkNotNull(path);
return root.accept(getVisitor, path).getValue();
return root.accept(getGetVisitor(), path).getValue();
}
@Override
@ -139,11 +112,18 @@ public class StoredMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
public void put(final K key, final V value) {
checkNotNull(key);
checkNotNull(value);
this.root = root.accept(new PutVisitor<>(nodeFactory, value), bytesToPath(key));
this.root = root.accept(getPutVisitor(value), bytesToPath(key));
}
@Override
public void putPath(final K path, final V value) {
checkNotNull(path);
checkNotNull(value);
this.root = root.accept(getPutVisitor(value), path);
}
@Override
public void put(final K key, final PutVisitor<V> putVisitor) {
public void put(final K key, final PathNodeVisitor<V> putVisitor) {
checkNotNull(key);
this.root = root.accept(putVisitor, bytesToPath(key));
}
@ -151,7 +131,7 @@ public class StoredMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
@Override
public void remove(final K key) {
checkNotNull(key);
this.root = root.accept(removeVisitor, bytesToPath(key));
this.root = root.accept(getRemoveVisitor(), bytesToPath(key));
}
@Override
@ -235,4 +215,10 @@ public class StoredMerklePatriciaTrie<K extends Bytes, V> implements MerklePatri
public String toString() {
return getClass().getSimpleName() + "[" + getRootHash() + "]";
}
public abstract PathNodeVisitor<V> getGetVisitor();
public abstract PathNodeVisitor<V> getRemoveVisitor();
public abstract PathNodeVisitor<V> getPutVisitor(final V value);
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -22,13 +22,13 @@ import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
class StoredNode<V> implements Node<V> {
private final StoredNodeFactory<V> nodeFactory;
public class StoredNode<V> implements Node<V> {
private final NodeFactory<V> nodeFactory;
private final Bytes location;
private final Bytes32 hash;
private Node<V> loaded;
StoredNode(final StoredNodeFactory<V> nodeFactory, final Bytes location, final Bytes32 hash) {
public StoredNode(final NodeFactory<V> nodeFactory, final Bytes location, final Bytes32 hash) {
this.nodeFactory = nodeFactory;
this.location = location;
this.hash = hash;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -14,6 +14,9 @@
*/
package org.hyperledger.besu.ethereum.trie;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
@ -63,9 +66,9 @@ public class TrieIterator<V> implements PathNodeVisitor<V> {
remainingPath = searchPath.slice(1);
}
paths.push(node.getPath());
for (byte i = iterateFrom; i < BranchNode.RADIX && state.continueIterating(); i++) {
for (int i = iterateFrom; i < node.maxChild() && state.continueIterating(); i++) {
paths.push(Bytes.of(i));
final Node<V> child = node.child(i);
final Node<V> child = node.child((byte) i);
child.accept(this, remainingPath);
if (unload) {
child.unload();

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,12 +12,18 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import static org.hyperledger.besu.crypto.Hash.keccak256;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.LocationNodeVisitor;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NodeFactory;
import org.hyperledger.besu.ethereum.trie.NodeVisitor;
import org.hyperledger.besu.ethereum.trie.NullNode;
import org.hyperledger.besu.ethereum.trie.PathNodeVisitor;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
@ -32,29 +38,28 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.bytes.MutableBytes;
class BranchNode<V> implements Node<V> {
public static final byte RADIX = CompactEncoding.LEAF_TERMINATOR;
public class BranchNode<V> implements Node<V> {
@SuppressWarnings("rawtypes")
private static final Node NULL_NODE = NullNode.instance();
protected static final Node NULL_NODE = NullNode.instance();
private final Optional<Bytes> location;
private final ArrayList<Node<V>> children;
private final List<Node<V>> children;
private final Optional<V> value;
private final NodeFactory<V> nodeFactory;
protected final NodeFactory<V> nodeFactory;
private final Function<V, Bytes> valueSerializer;
private WeakReference<Bytes> rlp;
private SoftReference<Bytes32> hash;
private boolean dirty = false;
private boolean needHeal = false;
BranchNode(
public BranchNode(
final Bytes location,
final ArrayList<Node<V>> children,
final Optional<V> value,
final NodeFactory<V> nodeFactory,
final Function<V, Bytes> valueSerializer) {
assert (children.size() == RADIX);
assert (children.size() == maxChild());
this.location = Optional.ofNullable(location);
this.children = children;
this.value = value;
@ -62,12 +67,12 @@ class BranchNode<V> implements Node<V> {
this.valueSerializer = valueSerializer;
}
BranchNode(
final ArrayList<Node<V>> children,
public BranchNode(
final List<Node<V>> children,
final Optional<V> value,
final NodeFactory<V> nodeFactory,
final Function<V, Bytes> valueSerializer) {
assert (children.size() == RADIX);
assert (children.size() == maxChild());
this.location = Optional.empty();
this.children = children;
this.value = value;
@ -124,7 +129,7 @@ class BranchNode<V> implements Node<V> {
}
final BytesValueRLPOutput out = new BytesValueRLPOutput();
out.startList();
for (int i = 0; i < RADIX; ++i) {
for (int i = 0; i < maxChild(); ++i) {
out.writeRaw(children.get(i).getRlpRef());
}
if (value.isPresent()) {
@ -196,7 +201,7 @@ class BranchNode<V> implements Node<V> {
return maybeFlatten(children).orElse(nodeFactory.createBranch(children, Optional.empty()));
}
private boolean hasChildren() {
protected boolean hasChildren() {
for (final Node<V> child : children) {
if (child != NULL_NODE) {
return true;
@ -205,7 +210,7 @@ class BranchNode<V> implements Node<V> {
return false;
}
private static <V> Optional<Node<V>> maybeFlatten(final ArrayList<Node<V>> children) {
protected Optional<Node<V>> maybeFlatten(final List<Node<V>> children) {
final int onlyChildIndex = findOnlyChild(children);
if (onlyChildIndex >= 0) {
// replace the path of the only child and return it
@ -219,10 +224,10 @@ class BranchNode<V> implements Node<V> {
return Optional.empty();
}
private static <V> int findOnlyChild(final ArrayList<Node<V>> children) {
private int findOnlyChild(final List<Node<V>> children) {
int onlyChildIndex = -1;
assert (children.size() == RADIX);
for (int i = 0; i < RADIX; ++i) {
assert (children.size() == maxChild());
for (int i = 0; i < maxChild(); ++i) {
if (children.get(i) != NULL_NODE) {
if (onlyChildIndex >= 0) {
return -1;
@ -238,7 +243,7 @@ class BranchNode<V> implements Node<V> {
final StringBuilder builder = new StringBuilder();
builder.append("Branch:");
builder.append("\n\tRef: ").append(getRlpRef());
for (int i = 0; i < RADIX; i++) {
for (int i = 0; i < maxChild(); i++) {
final Node<V> child = child((byte) i);
if (!Objects.equals(child, NullNode.instance())) {
final String branchLabel = "[" + Integer.toHexString(i) + "] ";
@ -269,4 +274,8 @@ class BranchNode<V> implements Node<V> {
public void markHealNeeded() {
this.needHeal = true;
}
public int maxChild() {
return 16;
}
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,10 +12,16 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import org.hyperledger.besu.ethereum.trie.LeafNode;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NodeFactory;
import org.hyperledger.besu.ethereum.trie.NullNode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
@ -25,6 +31,8 @@ public class DefaultNodeFactory<V> implements NodeFactory<V> {
@SuppressWarnings("rawtypes")
private static final Node NULL_NODE = NullNode.instance();
private static final int NB_CHILD = 16;
private final Function<V, Bytes> valueSerializer;
public DefaultNodeFactory(final Function<V, Bytes> valueSerializer) {
@ -40,16 +48,16 @@ public class DefaultNodeFactory<V> implements NodeFactory<V> {
@Override
public Node<V> createBranch(
final byte leftIndex, final Node<V> left, final byte rightIndex, final Node<V> right) {
assert (leftIndex <= BranchNode.RADIX);
assert (rightIndex <= BranchNode.RADIX);
assert (leftIndex <= NB_CHILD);
assert (rightIndex <= NB_CHILD);
assert (leftIndex != rightIndex);
final ArrayList<Node<V>> children =
new ArrayList<>(Collections.nCopies(BranchNode.RADIX, (Node<V>) NULL_NODE));
if (leftIndex == BranchNode.RADIX) {
new ArrayList<>(Collections.nCopies(NB_CHILD, (Node<V>) NULL_NODE));
if (leftIndex == NB_CHILD) {
children.set(rightIndex, right);
return createBranch(children, left.getValue());
} else if (rightIndex == BranchNode.RADIX) {
} else if (rightIndex == NB_CHILD) {
children.set(leftIndex, left);
return createBranch(children, right.getValue());
} else {
@ -60,7 +68,7 @@ public class DefaultNodeFactory<V> implements NodeFactory<V> {
}
@Override
public Node<V> createBranch(final ArrayList<Node<V>> children, final Optional<V> value) {
public Node<V> createBranch(final List<Node<V>> children, final Optional<V> value) {
return new BranchNode<>(children, value, this, valueSerializer);
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,12 +12,18 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import static org.hyperledger.besu.crypto.Hash.keccak256;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.LocationNodeVisitor;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NodeFactory;
import org.hyperledger.besu.ethereum.trie.NodeVisitor;
import org.hyperledger.besu.ethereum.trie.PathNodeVisitor;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
@ -28,7 +34,7 @@ import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
class ExtensionNode<V> implements Node<V> {
public class ExtensionNode<V> implements Node<V> {
private final Optional<Bytes> location;
private final Bytes path;
@ -39,7 +45,7 @@ class ExtensionNode<V> implements Node<V> {
private boolean dirty = false;
private boolean needHeal = false;
ExtensionNode(
public ExtensionNode(
final Bytes location,
final Bytes path,
final Node<V> child,
@ -53,7 +59,7 @@ class ExtensionNode<V> implements Node<V> {
this.nodeFactory = nodeFactory;
}
ExtensionNode(final Bytes path, final Node<V> child, final NodeFactory<V> nodeFactory) {
public ExtensionNode(final Bytes path, final Node<V> child, final NodeFactory<V> nodeFactory) {
assert (path.size() > 0);
assert (path.get(path.size() - 1) != CompactEncoding.LEAF_TERMINATOR)
: "Extension path ends in a leaf terminator";

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,11 +12,17 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.LeafNode;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NullNode;
import org.hyperledger.besu.ethereum.trie.PathNodeVisitor;
import org.apache.tuweni.bytes.Bytes;
class GetVisitor<V> implements PathNodeVisitor<V> {
public class GetVisitor<V> implements PathNodeVisitor<V> {
private final Node<V> NULL_NODE_RESULT = NullNode.instance();
@Override

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,7 +12,14 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.LeafNode;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NodeFactory;
import org.hyperledger.besu.ethereum.trie.NullNode;
import org.hyperledger.besu.ethereum.trie.PathNodeVisitor;
import org.apache.tuweni.bytes.Bytes;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,7 +12,13 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.LeafNode;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NullNode;
import org.hyperledger.besu.ethereum.trie.PathNodeVisitor;
import org.apache.tuweni.bytes.Bytes;

@ -0,0 +1,53 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie.patricia;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.PathNodeVisitor;
import org.hyperledger.besu.ethereum.trie.SimpleMerkleTrie;
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes;
/**
* An in-memory {@link MerkleTrie}.
*
* @param <V> The type of values stored by this trie.
*/
public class SimpleMerklePatriciaTrie<K extends Bytes, V> extends SimpleMerkleTrie<K, V>
implements MerkleTrie<K, V> {
private final GetVisitor<V> getVisitor = new GetVisitor<>();
private final RemoveVisitor<V> removeVisitor = new RemoveVisitor<>();
public SimpleMerklePatriciaTrie(final Function<V, Bytes> valueSerializer) {
super(valueSerializer);
}
@Override
public GetVisitor<V> getGetVisitor() {
return getVisitor;
}
@Override
public RemoveVisitor<V> getRemoveVisitor() {
return removeVisitor;
}
@Override
public PathNodeVisitor<V> getPutVisitor(final V value) {
return new PutVisitor<>(nodeFactory, value);
}
}

@ -0,0 +1,84 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie.patricia;
import org.hyperledger.besu.ethereum.trie.MerkleStorage;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.NodeLoader;
import org.hyperledger.besu.ethereum.trie.PathNodeVisitor;
import org.hyperledger.besu.ethereum.trie.StoredMerkleTrie;
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
/**
* A {@link MerkleTrie} that persists trie nodes to a {@link MerkleStorage} key/value store.
*
* @param <V> The type of values stored by this trie.
*/
public class StoredMerklePatriciaTrie<K extends Bytes, V> extends StoredMerkleTrie<K, V>
implements MerkleTrie<K, V> {
private final GetVisitor<V> getVisitor = new GetVisitor<>();
private final RemoveVisitor<V> removeVisitor = new RemoveVisitor<>();
public StoredMerklePatriciaTrie(
final NodeLoader nodeLoader,
final Function<V, Bytes> valueSerializer,
final Function<Bytes, V> valueDeserializer) {
super(new StoredNodeFactory<>(nodeLoader, valueSerializer, valueDeserializer));
}
public StoredMerklePatriciaTrie(
final NodeLoader nodeLoader,
final Bytes32 rootHash,
final Bytes rootLocation,
final Function<V, Bytes> valueSerializer,
final Function<Bytes, V> valueDeserializer) {
super(
new StoredNodeFactory<>(nodeLoader, valueSerializer, valueDeserializer),
rootHash,
rootLocation);
}
public StoredMerklePatriciaTrie(
final NodeLoader nodeLoader,
final Bytes32 rootHash,
final Function<V, Bytes> valueSerializer,
final Function<Bytes, V> valueDeserializer) {
super(new StoredNodeFactory<>(nodeLoader, valueSerializer, valueDeserializer), rootHash);
}
public StoredMerklePatriciaTrie(final StoredNodeFactory<V> nodeFactory, final Bytes32 rootHash) {
super(nodeFactory, rootHash);
}
@Override
public PathNodeVisitor<V> getGetVisitor() {
return getVisitor;
}
@Override
public PathNodeVisitor<V> getRemoveVisitor() {
return removeVisitor;
}
@Override
public PathNodeVisitor<V> getPutVisitor(final V value) {
return new PutVisitor<>(nodeFactory, value);
}
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,16 +12,25 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import static java.lang.String.format;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPException;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.LeafNode;
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NodeFactory;
import org.hyperledger.besu.ethereum.trie.NodeLoader;
import org.hyperledger.besu.ethereum.trie.NullNode;
import org.hyperledger.besu.ethereum.trie.StoredNode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
@ -33,6 +42,8 @@ public class StoredNodeFactory<V> implements NodeFactory<V> {
@SuppressWarnings("rawtypes")
private static final NullNode NULL_NODE = NullNode.instance();
private static final int RADIX = 16;
private final NodeLoader nodeLoader;
private final Function<V, Bytes> valueSerializer;
private final Function<Bytes, V> valueDeserializer;
@ -55,17 +66,17 @@ public class StoredNodeFactory<V> implements NodeFactory<V> {
@Override
public Node<V> createBranch(
final byte leftIndex, final Node<V> left, final byte rightIndex, final Node<V> right) {
assert (leftIndex <= BranchNode.RADIX);
assert (rightIndex <= BranchNode.RADIX);
assert (leftIndex <= RADIX);
assert (rightIndex <= RADIX);
assert (leftIndex != rightIndex);
final ArrayList<Node<V>> children =
new ArrayList<>(Collections.nCopies(BranchNode.RADIX, (Node<V>) NULL_NODE));
new ArrayList<>(Collections.nCopies(RADIX, (Node<V>) NULL_NODE));
if (leftIndex == BranchNode.RADIX) {
if (leftIndex == RADIX) {
children.set(rightIndex, right);
return createBranch(children, left.getValue());
} else if (rightIndex == BranchNode.RADIX) {
} else if (rightIndex == RADIX) {
children.set(leftIndex, left);
return createBranch(children, right.getValue());
} else {
@ -76,7 +87,7 @@ public class StoredNodeFactory<V> implements NodeFactory<V> {
}
@Override
public Node<V> createBranch(final ArrayList<Node<V>> children, final Optional<V> value) {
public Node<V> createBranch(final List<Node<V>> children, final Optional<V> value) {
return handleNewNode(new BranchNode<>(children, value, this, valueSerializer));
}
@ -90,6 +101,7 @@ public class StoredNodeFactory<V> implements NodeFactory<V> {
return node;
}
@Override
public Optional<Node<V>> retrieve(final Bytes location, final Bytes32 hash)
throws MerkleTrieException {
return nodeLoader
@ -147,7 +159,7 @@ public class StoredNodeFactory<V> implements NodeFactory<V> {
return extensionNode;
}
case (BranchNode.RADIX + 1):
case (RADIX + 1):
final BranchNode<V> branchNode = decodeBranch(location, nodeRLPs, errMessage);
nodeRLPs.leaveList();
return branchNode;
@ -180,8 +192,8 @@ public class StoredNodeFactory<V> implements NodeFactory<V> {
@SuppressWarnings("unchecked")
protected BranchNode<V> decodeBranch(
final Bytes location, final RLPInput nodeRLPs, final Supplier<String> errMessage) {
final ArrayList<Node<V>> children = new ArrayList<>(BranchNode.RADIX);
for (int i = 0; i < BranchNode.RADIX; ++i) {
final ArrayList<Node<V>> children = new ArrayList<>(RADIX);
for (int i = 0; i < RADIX; ++i) {
if (nodeRLPs.nextIsNull()) {
nodeRLPs.skipNext();
children.add(NULL_NODE);

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,10 +12,14 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import static com.google.common.base.Preconditions.checkArgument;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.NodeLoader;
import org.hyperledger.besu.ethereum.trie.NullNode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -17,6 +17,9 @@ package org.hyperledger.besu.ethereum.trie;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import java.util.Collections;
import org.junit.Test;

@ -1,5 +1,5 @@
/*
* Copyright contributors to Hyperledger Besu.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -19,6 +19,9 @@ import static org.mockito.ArgumentMatchers.anyByte;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.trie.patricia.BranchNode;
import org.hyperledger.besu.ethereum.trie.patricia.ExtensionNode;
import org.hyperledger.besu.ethereum.trie.patricia.StoredNodeFactory;
import org.hyperledger.besu.plugin.data.Hash;
import java.util.ArrayList;
@ -40,7 +43,7 @@ public class SnapPutVisitorTest {
new LeafNode<Bytes>(
Bytes.EMPTY, Bytes.of(0x00), storedNodeFactory, Function.identity()));
final ArrayList<Node<Bytes>> children = new ArrayList<>();
for (int i = 0; i < BranchNode.RADIX; i++) {
for (int i = 0; i < 16; i++) {
children.add(new StoredNode<>(storedNodeFactory, Bytes.EMPTY, Hash.ZERO));
}
final BranchNode<Bytes> invalidBranchNode =
@ -63,7 +66,7 @@ public class SnapPutVisitorTest {
when(storedNodeFactory.createBranch(any(), any()))
.thenReturn(new MissingNode<>(Hash.ZERO, Bytes.EMPTY));
final ArrayList<Node<Bytes>> children = new ArrayList<>();
for (int i = 0; i < BranchNode.RADIX; i++) {
for (int i = 0; i < 16; i++) {
children.add(new StoredNode<>(storedNodeFactory, Bytes.EMPTY, Hash.ZERO));
}
final BranchNode<Bytes> invalidBranchNode =
@ -87,7 +90,7 @@ public class SnapPutVisitorTest {
.thenReturn(
new LeafNode<>(Bytes.EMPTY, Bytes.of(0x00), storedNodeFactory, Function.identity()));
final ArrayList<Node<Bytes>> children = new ArrayList<>();
for (int i = 0; i < BranchNode.RADIX; i++) {
for (int i = 0; i < 16; i++) {
children.add(new StoredNode<>(storedNodeFactory, Bytes.EMPTY, Hash.ZERO));
}
final BranchNode<Bytes> invalidBranchNode =

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -27,6 +27,8 @@ import static org.mockito.Mockito.when;
import org.hyperledger.besu.crypto.Hash;
import org.hyperledger.besu.ethereum.trie.TrieIterator.LeafHandler;
import org.hyperledger.besu.ethereum.trie.TrieIterator.State;
import org.hyperledger.besu.ethereum.trie.patricia.DefaultNodeFactory;
import org.hyperledger.besu.ethereum.trie.patricia.PutVisitor;
import java.nio.charset.StandardCharsets;
import java.util.NavigableSet;

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -16,6 +16,8 @@ package org.hyperledger.besu.ethereum.trie;
import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.ethereum.trie.patricia.StoredMerklePatriciaTrie;
import org.hyperledger.besu.ethereum.trie.patricia.TrieNodeDecoder;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;
@ -38,7 +40,7 @@ public class TrieNodeDecoderTest {
final InMemoryKeyValueStorage storage = new InMemoryKeyValueStorage();
// Build a small trie
final MerklePatriciaTrie<Bytes, Bytes> trie =
final MerkleTrie<Bytes, Bytes> trie =
new StoredMerklePatriciaTrie<>(
new BytesToByteNodeLoader(storage), Function.identity(), Function.identity());
trie.put(Bytes.fromHexString("0x100000"), Bytes.of(1));
@ -82,7 +84,7 @@ public class TrieNodeDecoderTest {
final InMemoryKeyValueStorage storage = new InMemoryKeyValueStorage();
// Build a small trie
final MerklePatriciaTrie<Bytes, Bytes> trie =
final MerkleTrie<Bytes, Bytes> trie =
new StoredMerklePatriciaTrie<>(
new BytesToByteNodeLoader(storage), Function.identity(), Function.identity());
trie.put(Bytes.fromHexString("0x100000"), Bytes.of(1));
@ -153,7 +155,7 @@ public class TrieNodeDecoderTest {
final InMemoryKeyValueStorage partialStorage = new InMemoryKeyValueStorage();
// Build a small trie
final MerklePatriciaTrie<Bytes, Bytes> trie =
final MerkleTrie<Bytes, Bytes> trie =
new StoredMerklePatriciaTrie<>(
new BytesToByteNodeLoader(fullStorage), Function.identity(), Function.identity());
final Random random = new Random(1);
@ -191,7 +193,7 @@ public class TrieNodeDecoderTest {
public void breadthFirstDecode_emptyTrie() {
final List<Node<Bytes>> result =
TrieNodeDecoder.breadthFirstDecoder(
(l, h) -> Optional.empty(), MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)
(l, h) -> Optional.empty(), MerkleTrie.EMPTY_TRIE_NODE_HASH)
.collect(Collectors.toList());
assertThat(result.size()).isEqualTo(0);
}
@ -200,7 +202,7 @@ public class TrieNodeDecoderTest {
public void breadthFirstDecode_singleNodeTrie() {
final InMemoryKeyValueStorage storage = new InMemoryKeyValueStorage();
final MerklePatriciaTrie<Bytes, Bytes> trie =
final MerkleTrie<Bytes, Bytes> trie =
new StoredMerklePatriciaTrie<>(
new BytesToByteNodeLoader(storage), Function.identity(), Function.identity());
trie.put(Bytes.fromHexString("0x100000"), Bytes.of(1));

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,13 +12,19 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import static java.nio.charset.StandardCharsets.UTF_8;
import static junit.framework.TestCase.assertFalse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.KeyValueMerkleStorage;
import org.hyperledger.besu.ethereum.trie.MerkleStorage;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.Node;
import org.hyperledger.besu.ethereum.trie.Proof;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;
@ -31,14 +37,14 @@ import org.junit.Before;
import org.junit.Test;
public abstract class AbstractMerklePatriciaTrieTest {
protected MerklePatriciaTrie<Bytes, String> trie;
protected MerkleTrie<Bytes, String> trie;
@Before
public void setup() {
trie = createTrie();
}
protected abstract MerklePatriciaTrie<Bytes, String> createTrie();
protected abstract MerkleTrie<Bytes, String> createTrie();
@Test
public void emptyTreeReturnsEmpty() {
@ -69,6 +75,18 @@ public abstract class AbstractMerklePatriciaTrieTest {
assertThat(trie.get(key)).isEqualTo(Optional.of(value2));
}
@Test
public void replaceSingleValueWithPath() {
final Bytes key = Bytes.of(1);
final String value1 = "value1";
trie.putPath(CompactEncoding.bytesToPath(key), value1);
assertThat(trie.get(key)).isEqualTo(Optional.of(value1));
final String value2 = "value2";
trie.putPath(CompactEncoding.bytesToPath(key), value2);
assertThat(trie.get(key)).isEqualTo(Optional.of(value2));
}
@Test
public void hashChangesWhenSingleValueReplaced() {
final Bytes key = Bytes.of(1);

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,7 +12,9 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import java.nio.charset.Charset;
@ -20,7 +22,7 @@ import org.apache.tuweni.bytes.Bytes;
public class SimpleMerklePatriciaTrieTest extends AbstractMerklePatriciaTrieTest {
@Override
protected MerklePatriciaTrie<Bytes, String> createTrie() {
protected MerkleTrie<Bytes, String> createTrie() {
return new SimpleMerklePatriciaTrie<>(
value -> (value != null) ? Bytes.wrap(value.getBytes(Charset.forName("UTF-8"))) : null);
}

@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@ -12,10 +12,13 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.trie;
package org.hyperledger.besu.ethereum.trie.patricia;
import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.ethereum.trie.KeyValueMerkleStorage;
import org.hyperledger.besu.ethereum.trie.MerkleStorage;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.plugin.services.storage.KeyValueStorage;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;
@ -34,7 +37,7 @@ public class StoredMerklePatriciaTrieTest extends AbstractMerklePatriciaTrieTest
private Function<Bytes, String> valueDeserializer;
@Override
protected MerklePatriciaTrie<Bytes, String> createTrie() {
protected MerkleTrie<Bytes, String> createTrie() {
keyValueStore = new InMemoryKeyValueStorage();
merkleStorage = new KeyValueMerkleStorage(keyValueStore);
valueSerializer =
Loading…
Cancel
Save