missed merkle refactoring (#5257)

Signed-off-by: garyschulte <garyschulte@gmail.com>
pull/5267/head
garyschulte 2 years ago committed by GitHub
parent 0e36f661e8
commit 16779eae21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      ethereum/core/src/integration-test/java/org/hyperledger/besu/ethereum/worldstate/PrunerIntegrationTest.java
  2. 8
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/CachedMerkleTrieLoaderTest.java
  3. 2
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/worldstate/MarkSweepPrunerTest.java
  4. 2
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/worldstate/FastWorldStateDownloaderTest.java
  5. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/AllNodesVisitor.java
  6. 5
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/CommitVisitor.java
  7. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/InnerNodeDiscoveryManager.java
  8. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/LocationNodeVisitor.java
  9. 3
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/MerkleTrie.java
  10. 6
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/Node.java
  11. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/NodeVisitor.java
  12. 4
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/NullNode.java
  13. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/PathNodeVisitor.java
  14. 7
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/PersistVisitor.java
  15. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/ProofVisitor.java
  16. 7
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/RestoreVisitor.java
  17. 5
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/SimpleMerkleTrie.java
  18. 19
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/StoredMerkleTrie.java
  19. 8
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/StoredNode.java
  20. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/TrieIterator.java
  21. 20
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/BranchNode.java
  22. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/DefaultNodeFactory.java
  23. 12
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/ExtensionNode.java
  24. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/GetVisitor.java
  25. 30
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/LeafNode.java
  26. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/PutVisitor.java
  27. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/RemoveVisitor.java
  28. 1
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/patricia/StoredNodeFactory.java
  29. 1
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/SnapPutVisitorTest.java
  30. 2
      ethereum/trie/src/test/java/org/hyperledger/besu/ethereum/trie/TrieNodeDecoderTest.java

@ -228,7 +228,7 @@ public class PrunerIntegrationTest {
trie.visitAll(
(node) -> {
if (node.isReferencedByHash() || node.getHash().equals(rootHash)) {
collector.add(node.getRlp());
collector.add(node.getEncodedBytes());
}
});
}

@ -97,7 +97,7 @@ public class CachedMerkleTrieLoaderTest {
(keyHash, node) -> {
merkleTrieLoader.cacheStorageNodes(
inMemoryWorldState, accounts.get(0), Hash.wrap(keyHash));
originalSlots.add(node.getRlp());
originalSlots.add(node.getEncodedBytes());
return TrieIterator.State.CONTINUE;
});
@ -114,7 +114,7 @@ public class CachedMerkleTrieLoaderTest {
Function.identity());
cachedTrie.visitLeafs(
(keyHash, node) -> {
cachedSlots.add(node.getRlp());
cachedSlots.add(node.getEncodedBytes());
return TrieIterator.State.CONTINUE;
});
assertThat(originalSlots).isNotEmpty();
@ -149,7 +149,7 @@ public class CachedMerkleTrieLoaderTest {
final List<Bytes> originalSlots = new ArrayList<>();
storageTrie.visitLeafs(
(keyHash, node) -> {
originalSlots.add(node.getRlp());
originalSlots.add(node.getEncodedBytes());
return TrieIterator.State.CONTINUE;
});
@ -164,7 +164,7 @@ public class CachedMerkleTrieLoaderTest {
Function.identity());
cachedTrie.visitLeafs(
(keyHash, node) -> {
cachedSlots.add(node.getRlp());
cachedSlots.add(node.getEncodedBytes());
return TrieIterator.State.CONTINUE;
});
assertThat(originalSlots).isNotEmpty();

@ -251,7 +251,7 @@ public class MarkSweepPrunerTest {
trie.visitAll(
(node) -> {
if (node.isReferencedByHash() || node.getHash().equals(rootHash)) {
collector.add(node.getRlp());
collector.add(node.getEncodedBytes());
}
});
}

@ -782,7 +782,7 @@ public class FastWorldStateDownloaderTest {
.filter(n -> !Objects.equals(n.getHash(), rootHash))
.filter(Node::isReferencedByHash)
.limit(maxNodes)
.forEach((n) -> trieNodes.put(n.getHash(), n.getRlp()));
.forEach((n) -> trieNodes.put(n.getHash(), n.getEncodedBytes()));
return trieNodes;
}

@ -16,6 +16,7 @@ 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.LeafNode;
import java.util.function.Consumer;

@ -16,12 +16,13 @@ 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.LeafNode;
import org.apache.tuweni.bytes.Bytes;
public class CommitVisitor<V> implements LocationNodeVisitor<V> {
private final NodeUpdater nodeUpdater;
protected final NodeUpdater nodeUpdater;
public CommitVisitor(final NodeUpdater nodeUpdater) {
this.nodeUpdater = nodeUpdater;
@ -71,7 +72,7 @@ public class CommitVisitor<V> implements LocationNodeVisitor<V> {
public void visit(final Bytes location, final NullNode<V> nullNode) {}
public void maybeStoreNode(final Bytes location, final Node<V> node) {
final Bytes nodeRLP = node.getRlp();
final Bytes nodeRLP = node.getEncodedBytes();
if (nodeRLP.size() >= 32) {
this.nodeUpdater.store(location, node.getHash(), nodeRLP);
}

@ -17,6 +17,7 @@ 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.LeafNode;
import org.hyperledger.besu.ethereum.trie.patricia.StoredNodeFactory;
import java.util.ArrayList;

@ -16,6 +16,7 @@ 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.LeafNode;
import org.apache.tuweni.bytes.Bytes;

@ -17,7 +17,6 @@ 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;
@ -98,7 +97,7 @@ public interface MerkleTrie<K, V> {
* @param path of the node to be deleted.
* @param removeVisitor custom visitor for the deletion
*/
void removePath(K path, RemoveVisitor<V> removeVisitor);
void removePath(K path, PathNodeVisitor<V> removeVisitor);
/**
* Returns the KECCAK256 hash of the root node of the trie.

@ -38,9 +38,9 @@ public interface Node<V> {
List<Node<V>> getChildren();
Bytes getRlp();
Bytes getEncodedBytes();
Bytes getRlpRef();
Bytes getEncodedBytesRef();
/**
* Whether a reference to this node should be represented as a hash of the rlp, or the node rlp
@ -50,7 +50,7 @@ public interface Node<V> {
* @return true if this node should be referenced by hash
*/
default boolean isReferencedByHash() {
return getRlp().size() >= 32;
return getEncodedBytes().size() >= 32;
}
Bytes32 getHash();

@ -16,6 +16,7 @@ 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.LeafNode;
public interface NodeVisitor<V> {

@ -63,12 +63,12 @@ public class NullNode<V> implements Node<V> {
}
@Override
public Bytes getRlp() {
public Bytes getEncodedBytes() {
return MerkleTrie.EMPTY_TRIE_NODE;
}
@Override
public Bytes getRlpRef() {
public Bytes getEncodedBytesRef() {
return MerkleTrie.EMPTY_TRIE_NODE;
}

@ -16,6 +16,7 @@ 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.LeafNode;
import org.apache.tuweni.bytes.Bytes;

@ -18,6 +18,7 @@ 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.LeafNode;
import java.util.function.BiConsumer;
@ -54,21 +55,21 @@ public class PersistVisitor<V> implements NodeVisitor<V> {
@Override
public void visit(final BranchNode<V> branchNode) {
writer.accept(branchNode.getHash(), branchNode.getRlp());
writer.accept(branchNode.getHash(), branchNode.getEncodedBytes());
branchNodeCount++;
branchNode.getChildren().forEach(node -> node.accept(this));
}
@Override
public void visit(final ExtensionNode<V> extensionNode) {
writer.accept(extensionNode.getHash(), extensionNode.getRlp());
writer.accept(extensionNode.getHash(), extensionNode.getEncodedBytes());
extensionNodeCount++;
extensionNode.getChild().accept(this);
}
@Override
public void visit(final LeafNode<V> leafNode) {
writer.accept(leafNode.getHash(), leafNode.getRlp());
writer.accept(leafNode.getHash(), leafNode.getEncodedBytes());
leafNodeCount++;
}

@ -17,6 +17,7 @@ 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 org.hyperledger.besu.ethereum.trie.patricia.LeafNode;
import java.util.ArrayList;
import java.util.List;

@ -17,6 +17,7 @@ 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 org.hyperledger.besu.ethereum.trie.patricia.LeafNode;
import java.util.List;
import java.util.Optional;
@ -95,7 +96,7 @@ public class RestoreVisitor<V> implements PathNodeVisitor<V> {
if (!(child instanceof StoredNode)) {
child.accept(persistVisitor);
final PersistedNode<V> persistedNode =
new PersistedNode<>(null, child.getHash(), child.getRlpRef());
new PersistedNode<>(null, child.getHash(), child.getEncodedBytesRef());
return (BranchNode<V>) parent.replaceChild(index, persistedNode);
} else {
return parent;
@ -208,13 +209,13 @@ public class RestoreVisitor<V> implements PathNodeVisitor<V> {
}
@Override
public Bytes getRlp() {
public Bytes getEncodedBytes() {
throw new UnsupportedOperationException(
"A persisted node cannot have rlp, as it's already been restored.");
}
@Override
public Bytes getRlpRef() {
public Bytes getEncodedBytesRef() {
return refRlp;
}

@ -19,7 +19,6 @@ 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;
@ -73,7 +72,7 @@ public abstract class SimpleMerkleTrie<K extends Bytes, V> implements MerkleTrie
final ProofVisitor<V> proofVisitor = new ProofVisitor<>(root);
final Optional<V> value = root.accept(proofVisitor, bytesToPath(key)).getValue();
final List<Bytes> proof =
proofVisitor.getProof().stream().map(Node::getRlp).collect(Collectors.toList());
proofVisitor.getProof().stream().map(Node::getEncodedBytes).collect(Collectors.toList());
return new Proof<>(value, proof);
}
@ -104,7 +103,7 @@ public abstract class SimpleMerkleTrie<K extends Bytes, V> implements MerkleTrie
}
@Override
public void removePath(final K path, final RemoveVisitor<V> removeVisitor) {
public void removePath(final K path, final PathNodeVisitor<V> removeVisitor) {
checkNotNull(path);
this.root = root.accept(removeVisitor, path);
}

@ -18,7 +18,6 @@ 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;
@ -43,7 +42,7 @@ public abstract class StoredMerkleTrie<K extends Bytes, V> implements MerkleTrie
protected final NodeFactory<V> nodeFactory;
private Node<V> root;
protected Node<V> root;
/**
* Create a trie.
@ -104,7 +103,7 @@ public abstract class StoredMerkleTrie<K extends Bytes, V> implements MerkleTrie
final ProofVisitor<V> proofVisitor = new ProofVisitor<>(root);
final Optional<V> value = root.accept(proofVisitor, bytesToPath(key)).getValue();
final List<Bytes> proof =
proofVisitor.getProof().stream().map(Node::getRlp).collect(Collectors.toList());
proofVisitor.getProof().stream().map(Node::getEncodedBytes).collect(Collectors.toList());
return new Proof<>(value, proof);
}
@ -135,7 +134,7 @@ public abstract class StoredMerkleTrie<K extends Bytes, V> implements MerkleTrie
}
@Override
public void removePath(final K path, final RemoveVisitor<V> removeVisitor) {
public void removePath(final K path, final PathNodeVisitor<V> removeVisitor) {
checkNotNull(path);
this.root = root.accept(removeVisitor, path);
}
@ -149,8 +148,8 @@ public abstract class StoredMerkleTrie<K extends Bytes, V> implements MerkleTrie
public void commit(final NodeUpdater nodeUpdater, final CommitVisitor<V> commitVisitor) {
root.accept(Bytes.EMPTY, commitVisitor);
// Make sure root node was stored
if (root.isDirty() && root.getRlpRef().size() < 32) {
nodeUpdater.store(Bytes.EMPTY, root.getHash(), root.getRlpRef());
if (root.isDirty() && root.getEncodedBytesRef().size() < 32) {
nodeUpdater.store(Bytes.EMPTY, root.getHash(), root.getEncodedBytesRef());
}
// Reset root so dirty nodes can be garbage collected
final Bytes32 rootHash = root.getHash();
@ -160,14 +159,6 @@ public abstract class StoredMerkleTrie<K extends Bytes, V> implements MerkleTrie
: new StoredNode<>(nodeFactory, Bytes.EMPTY, rootHash);
}
public void acceptAtRoot(final NodeVisitor<V> visitor) {
root.accept(visitor);
}
public void acceptAtRoot(final PathNodeVisitor<V> visitor, final Bytes path) {
root.accept(visitor, path);
}
@Override
public Map<Bytes32, V> entriesFrom(final Bytes32 startKeyHash, final int limit) {
return StorageEntriesCollector.collectEntries(root, startKeyHash, limit);

@ -99,12 +99,12 @@ public class StoredNode<V> implements Node<V> {
}
@Override
public Bytes getRlp() {
return load().getRlp();
public Bytes getEncodedBytes() {
return load().getEncodedBytes();
}
@Override
public Bytes getRlpRef() {
public Bytes getEncodedBytesRef() {
// If this node was stored, then it must have a rlp larger than a hash
return RLP.encodeOne(hash);
}
@ -152,7 +152,7 @@ public class StoredNode<V> implements Node<V> {
@Override
public String print() {
if (loaded == null) {
return "StoredNode:" + "\n\tRef: " + getRlpRef();
return "StoredNode:" + "\n\tRef: " + getEncodedBytesRef();
} else {
return load().print();
}

@ -16,6 +16,7 @@ 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.LeafNode;
import java.util.ArrayDeque;
import java.util.Deque;

@ -48,7 +48,7 @@ public class BranchNode<V> implements Node<V> {
private final Optional<V> value;
protected final NodeFactory<V> nodeFactory;
private final Function<V, Bytes> valueSerializer;
private WeakReference<Bytes> rlp;
protected WeakReference<Bytes> encodedBytes;
private SoftReference<Bytes32> hash;
private boolean dirty = false;
private boolean needHeal = false;
@ -120,9 +120,9 @@ public class BranchNode<V> implements Node<V> {
}
@Override
public Bytes getRlp() {
if (rlp != null) {
final Bytes encoded = rlp.get();
public Bytes getEncodedBytes() {
if (encodedBytes != null) {
final Bytes encoded = encodedBytes.get();
if (encoded != null) {
return encoded;
}
@ -130,7 +130,7 @@ public class BranchNode<V> implements Node<V> {
final BytesValueRLPOutput out = new BytesValueRLPOutput();
out.startList();
for (int i = 0; i < maxChild(); ++i) {
out.writeRaw(children.get(i).getRlpRef());
out.writeRaw(children.get(i).getEncodedBytesRef());
}
if (value.isPresent()) {
out.writeBytes(valueSerializer.apply(value.get()));
@ -139,16 +139,16 @@ public class BranchNode<V> implements Node<V> {
}
out.endList();
final Bytes encoded = out.encoded();
rlp = new WeakReference<>(encoded);
encodedBytes = new WeakReference<>(encoded);
return encoded;
}
@Override
public Bytes getRlpRef() {
public Bytes getEncodedBytesRef() {
if (isReferencedByHash()) {
return RLP.encodeOne(getHash());
} else {
return getRlp();
return getEncodedBytes();
}
}
@ -160,7 +160,7 @@ public class BranchNode<V> implements Node<V> {
return hashed;
}
}
final Bytes32 hashed = keccak256(getRlp());
final Bytes32 hashed = keccak256(getEncodedBytes());
hash = new SoftReference<>(hashed);
return hashed;
}
@ -242,7 +242,7 @@ public class BranchNode<V> implements Node<V> {
public String print() {
final StringBuilder builder = new StringBuilder();
builder.append("Branch:");
builder.append("\n\tRef: ").append(getRlpRef());
builder.append("\n\tRef: ").append(getEncodedBytesRef());
for (int i = 0; i < maxChild(); i++) {
final Node<V> child = child((byte) i);
if (!Objects.equals(child, NullNode.instance())) {

@ -14,7 +14,6 @@
*/
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;

@ -109,7 +109,7 @@ public class ExtensionNode<V> implements Node<V> {
}
@Override
public Bytes getRlp() {
public Bytes getEncodedBytes() {
if (rlp != null) {
final Bytes encoded = rlp.get();
if (encoded != null) {
@ -119,7 +119,7 @@ public class ExtensionNode<V> implements Node<V> {
final BytesValueRLPOutput out = new BytesValueRLPOutput();
out.startList();
out.writeBytes(CompactEncoding.encode(path));
out.writeRaw(child.getRlpRef());
out.writeRaw(child.getEncodedBytesRef());
out.endList();
final Bytes encoded = out.encoded();
rlp = new WeakReference<>(encoded);
@ -127,11 +127,11 @@ public class ExtensionNode<V> implements Node<V> {
}
@Override
public Bytes getRlpRef() {
public Bytes getEncodedBytesRef() {
if (isReferencedByHash()) {
return RLP.encodeOne(getHash());
} else {
return getRlp();
return getEncodedBytes();
}
}
@ -143,7 +143,7 @@ public class ExtensionNode<V> implements Node<V> {
return hashed;
}
}
final Bytes rlp = getRlp();
final Bytes rlp = getEncodedBytes();
final Bytes32 hashed = keccak256(rlp);
hash = new SoftReference<>(hashed);
return hashed;
@ -169,7 +169,7 @@ public class ExtensionNode<V> implements Node<V> {
builder
.append("Extension:")
.append("\n\tRef: ")
.append(getRlpRef())
.append(getEncodedBytesRef())
.append("\n\tPath: ")
.append(CompactEncoding.encode(path))
.append("\n\t")

@ -15,7 +15,6 @@
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;

@ -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;
@ -32,10 +38,10 @@ import org.apache.tuweni.bytes.Bytes32;
public class LeafNode<V> implements Node<V> {
private final Optional<Bytes> location;
private final Bytes path;
private final V value;
protected final V value;
private final NodeFactory<V> nodeFactory;
private final Function<V, Bytes> valueSerializer;
private WeakReference<Bytes> rlp;
protected final Function<V, Bytes> valueSerializer;
protected WeakReference<Bytes> encodedBytes;
private SoftReference<Bytes32> hash;
private boolean dirty = false;
@ -100,9 +106,9 @@ public class LeafNode<V> implements Node<V> {
}
@Override
public Bytes getRlp() {
if (rlp != null) {
final Bytes encoded = rlp.get();
public Bytes getEncodedBytes() {
if (encodedBytes != null) {
final Bytes encoded = encodedBytes.get();
if (encoded != null) {
return encoded;
}
@ -114,16 +120,16 @@ public class LeafNode<V> implements Node<V> {
out.writeBytes(valueSerializer.apply(value));
out.endList();
final Bytes encoded = out.encoded();
rlp = new WeakReference<>(encoded);
encodedBytes = new WeakReference<>(encoded);
return encoded;
}
@Override
public Bytes getRlpRef() {
public Bytes getEncodedBytesRef() {
if (isReferencedByHash()) {
return RLP.encodeOne(getHash());
} else {
return getRlp();
return getEncodedBytes();
}
}
@ -135,7 +141,7 @@ public class LeafNode<V> implements Node<V> {
return hashed;
}
}
final Bytes32 hashed = keccak256(getRlp());
final Bytes32 hashed = keccak256(getEncodedBytes());
hash = new SoftReference<>(hashed);
return hashed;
}
@ -149,7 +155,7 @@ public class LeafNode<V> implements Node<V> {
public String print() {
return "Leaf:"
+ "\n\tRef: "
+ getRlpRef()
+ getEncodedBytesRef()
+ "\n\tPath: "
+ CompactEncoding.encode(path)
+ "\n\tValue: "

@ -15,7 +15,6 @@
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;

@ -15,7 +15,6 @@
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;

@ -20,7 +20,6 @@ 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;

@ -21,6 +21,7 @@ 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.LeafNode;
import org.hyperledger.besu.ethereum.trie.patricia.StoredNodeFactory;
import org.hyperledger.besu.plugin.data.Hash;

@ -179,7 +179,7 @@ public class TrieNodeDecoderTest {
// Decode partially available trie
final KeyValueStorageTransaction partialTx = partialStorage.startTransaction();
partialTx.put(trie.getRootHash().toArrayUnsafe(), rootNode.getRlp().toArrayUnsafe());
partialTx.put(trie.getRootHash().toArrayUnsafe(), rootNode.getEncodedBytes().toArrayUnsafe());
partialTx.commit();
final List<Node<Bytes>> allDecodableNodes =
TrieNodeDecoder.breadthFirstDecoder(

Loading…
Cancel
Save