From deee991487ace7dcc4de9e783c0fb57c84773056 Mon Sep 17 00:00:00 2001 From: Uacias Date: Mon, 20 Nov 2023 16:21:02 +0000 Subject: [PATCH] Fix newline issue, tests pass, format Signed-off-by: Neo --- .../trie/verkle/SimpleVerkleTrie.java | 23 +- .../trie/verkle/StoredVerkleTrie.java | 1 - .../besu/ethereum/trie/verkle/VerkleTrie.java | 14 +- .../ethereum/trie/verkle/node/BranchNode.java | 45 +- .../trie/verkle/node/InternalNode.java | 45 +- .../ethereum/trie/verkle/node/LeafNode.java | 25 +- .../besu/ethereum/trie/verkle/node/Node.java | 9 +- .../trie/verkle/node/NullLeafNode.java | 26 +- .../ethereum/trie/verkle/node/NullNode.java | 26 +- .../ethereum/trie/verkle/node/StemNode.java | 96 ++-- .../ethereum/trie/verkle/node/StoredNode.java | 15 +- .../ethereum/trie/verkle/DotDisplayTest.java | 1 - .../trie/verkle/SimpleVerkleTrieTest.java | 504 ++++++++++-------- 13 files changed, 452 insertions(+), 378 deletions(-) diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrie.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrie.java index 94ab974..31970ab 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrie.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrie.java @@ -78,8 +78,7 @@ public Node getRoot() { * Gets the value associated with the specified key from the Verkle Trie. * * @param key The key to retrieve the value for. - * @return An optional containing the value if found, or an empty optional if - * not found. + * @return An optional containing the value if found, or an empty optional if not found. */ @Override public Optional get(final K key) { @@ -90,7 +89,7 @@ public Optional get(final K key) { /** * Inserts a key-value pair into the Verkle Trie. * - * @param key The key to insert. + * @param key The key to insert. * @param value The value to associate with the key. */ @Override @@ -137,8 +136,7 @@ public String toString() { /** * Commits the Verkle Trie using the provided node updater. * - * @param nodeUpdater The node updater for storing the changes in the Verkle - * Trie. + * @param nodeUpdater The node updater for storing the changes in the Verkle Trie. */ @Override public void commit(final NodeUpdater nodeUpdater) { @@ -149,9 +147,7 @@ public void commit(final NodeUpdater nodeUpdater) { /** * Returns the DOT representation of the entire Verkle Trie. * - * @param showRepeatingEdges if true displays repeating edges; if false does - * not. - * + * @param showRepeatingEdges if true displays repeating edges; if false does not. * @return The DOT representation of the Verkle Trie. */ public String toDotTree(Boolean showRepeatingEdges) { @@ -163,8 +159,7 @@ public String toDotTree(Boolean showRepeatingEdges) { /** * Returns the DOT representation of the entire Verkle Trie. * - *

- * The representation does not contain repeating edges. + *

The representation does not contain repeating edges. * * @return The DOT representation of the Verkle Trie. */ @@ -172,12 +167,11 @@ public String toDotTree() { StringBuilder result = new StringBuilder("digraph VerkleTrie {\n"); Node root = getRoot(); result.append(root.toDot()); - return result.append("}\n").toString(); + return result.append("}").toString(); } /** - * Exports the Verkle Trie DOT representation to a '.gv' file located in the - * current directory. + * Exports the Verkle Trie DOT representation to a '.gv' file located in the current directory. * The default file name is "VerkleTree.gv". * * @throws IOException if an I/O error occurs. @@ -187,8 +181,7 @@ public void dotTreeToFile() throws IOException { } /** - * /** Exports the Verkle Trie DOT representation to a '.gv' file located at the - * specified path. + * /** Exports the Verkle Trie DOT representation to a '.gv' file located at the specified path. * * @param path The location where the DOT file will be saved. * @throws IOException if ann I/O error occurs. diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/StoredVerkleTrie.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/StoredVerkleTrie.java index b663212..7fbde05 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/StoredVerkleTrie.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/StoredVerkleTrie.java @@ -38,5 +38,4 @@ public StoredVerkleTrie(final NodeFactory nodeFactory) { super(nodeFactory.retrieve(Bytes.EMPTY, null)); this.nodeFactory = nodeFactory; } - } diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/VerkleTrie.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/VerkleTrie.java index b307ce8..7652994 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/VerkleTrie.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/VerkleTrie.java @@ -25,29 +25,25 @@ public interface VerkleTrie { /** - * Returns an {@code Optional} of value mapped to the hash if it exists; - * otherwise empty. + * Returns an {@code Optional} of value mapped to the hash if it exists; otherwise empty. * * @param key The key for the value. - * @return an {@code Optional} of value mapped to the hash if it exists; - * otherwise empty + * @return an {@code Optional} of value mapped to the hash if it exists; otherwise empty */ Optional get(K key); /** - * Updates the value mapped to the specified key, creating the mapping if one - * does not already + * Updates the value mapped to the specified key, creating the mapping if one does not already * exist. * - * @param key The key that corresponds to the value to be updated. + * @param key The key that corresponds to the value to be updated. * @param value The value to associate the key with. * @return Optional previous value before replacement if it exists. */ Optional put(K key, V value); /** - * Deletes the value mapped to the specified key, if such a value exists - * (Optional operation). + * Deletes the value mapped to the specified key, if such a value exists (Optional operation). * * @param key The key of the value to be deleted. */ diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/BranchNode.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/BranchNode.java index 9f8ffb8..95af72d 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/BranchNode.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/BranchNode.java @@ -42,10 +42,10 @@ public abstract class BranchNode implements Node { /** * Constructs a new BranchNode with location, hash, path, and children. * - * @param location The location in the tree. - * @param hash Node's vector commitment's hash. + * @param location The location in the tree. + * @param hash Node's vector commitment's hash. * @param commitment Node's vector commitment. - * @param children The list of children nodes. + * @param children The list of children nodes. */ public BranchNode( final Bytes location, @@ -60,14 +60,13 @@ public BranchNode( } /** - * Constructs a new BranchNode with optional location, optional hash, optional - * commitment and + * Constructs a new BranchNode with optional location, optional hash, optional commitment and * children. * - * @param location The optional location in the tree. - * @param hash The optional vector commitment of children's commitments. + * @param location The optional location in the tree. + * @param hash The optional vector commitment of children's commitments. * @param commitment Node's optional vector commitment. - * @param children The list of children nodes. + * @param children The list of children nodes. */ public BranchNode( final Optional location, @@ -96,8 +95,7 @@ public BranchNode(final Optional location, final List> children) } /** - * Constructs a new BranchNode with optional location and path, initializing - * children to + * Constructs a new BranchNode with optional location and path, initializing children to * NullNodes. * * @param location The optional location in the tree. @@ -125,7 +123,7 @@ public static int maxChild() { * Accepts a visitor for path-based operations on the node. * * @param visitor The path node visitor. - * @param path The path associated with a node. + * @param path The path associated with a node. * @return The result of the visitor's operation. */ @Override @@ -153,7 +151,7 @@ public Node child(final byte childIndex) { /** * Replaces the child node at a specified index with a new node. * - * @param index The index of the child node to replace. + * @param index The index of the child node to replace. * @param childNode The new child node. */ public void replaceChild(final byte index, final Node childNode) { @@ -251,15 +249,24 @@ public String print() { */ @Override public String toDot(Boolean showRepeatingEdges) { - StringBuilder result = new StringBuilder() - .append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY)) - .append("\", location=\"").append(getLocation().orElse(Bytes.EMPTY)) - .append("\", commitment=\"").append(getHash().orElse(Bytes32.ZERO)).append("\"]\n"); + StringBuilder result = + new StringBuilder() + .append(getClass().getSimpleName()) + .append(getLocation().orElse(Bytes.EMPTY)) + .append("\", location=\"") + .append(getLocation().orElse(Bytes.EMPTY)) + .append("\", commitment=\"") + .append(getHash().orElse(Bytes32.ZERO)) + .append("\"]\n"); for (Node child : getChildren()) { - String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " - + child.getClass().getSimpleName() - + child.getLocation().orElse(Bytes.EMPTY) + "\n"; + String edgeString = + getClass().getSimpleName() + + getLocation().orElse(Bytes.EMPTY) + + " -> " + + child.getClass().getSimpleName() + + child.getLocation().orElse(Bytes.EMPTY) + + "\n"; if (showRepeatingEdges || !result.toString().contains(edgeString)) { result.append(edgeString); diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/InternalNode.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/InternalNode.java index 0bf50fa..24b6084 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/InternalNode.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/InternalNode.java @@ -38,10 +38,10 @@ public class InternalNode extends BranchNode { /** * Constructs a new InternalNode with location, hash, path, and children. * - * @param location The location in the tree. - * @param hash Node's vector commitment's hash. + * @param location The location in the tree. + * @param hash Node's vector commitment's hash. * @param commitment Node's vector commitment. - * @param children The list of children nodes. + * @param children The list of children nodes. */ public InternalNode( final Bytes location, @@ -52,14 +52,13 @@ public InternalNode( } /** - * Constructs a new InternalNode with optional location, optional hash, optional - * commitment and + * Constructs a new InternalNode with optional location, optional hash, optional commitment and * children. * - * @param location The optional location in the tree. - * @param hash The optional vector commitment of children's commitments. + * @param location The optional location in the tree. + * @param hash The optional vector commitment of children's commitments. * @param commitment Node's optional vector commitment. - * @param children The list of children nodes. + * @param children The list of children nodes. */ public InternalNode( final Optional location, @@ -80,8 +79,7 @@ public InternalNode(final Optional location, final List> children } /** - * Constructs a new InternalNode with optional location and path, initializing - * children to + * Constructs a new InternalNode with optional location and path, initializing children to * NullNodes. * * @param location The optional location in the tree. @@ -94,7 +92,7 @@ public InternalNode(final Bytes location) { * Accepts a visitor for path-based operations on the node. * * @param visitor The path node visitor. - * @param path The path associated with a node. + * @param path The path associated with a node. * @return The result of the visitor's operation. */ @Override @@ -116,7 +114,7 @@ public Node accept(final NodeVisitor visitor) { /** * Replace the vector commitment with a new one. * - * @param hash The new vector commitment's hash to set. + * @param hash The new vector commitment's hash to set. * @param commitment The new vector commitment to set. * @return A new InternalNode with the updated vector commitment. */ @@ -168,15 +166,24 @@ public String print() { */ @Override public String toDot(Boolean showRepeatingEdges) { - StringBuilder result = new StringBuilder() - .append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY)) - .append("[location=\"").append(getLocation().orElse(Bytes.EMPTY)) - .append("\", commitment=\"").append(getHash().orElse(Bytes32.ZERO)).append("\"]\n"); + StringBuilder result = + new StringBuilder() + .append(getClass().getSimpleName()) + .append(getLocation().orElse(Bytes.EMPTY)) + .append("[location=\"") + .append(getLocation().orElse(Bytes.EMPTY)) + .append("\", commitment=\"") + .append(getHash().orElse(Bytes32.ZERO)) + .append("\"]\n"); for (Node child : getChildren()) { - String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " - + child.getClass().getSimpleName() - + child.getLocation().orElse(Bytes.EMPTY) + "\n"; + String edgeString = + getClass().getSimpleName() + + getLocation().orElse(Bytes.EMPTY) + + " -> " + + child.getClass().getSimpleName() + + child.getLocation().orElse(Bytes.EMPTY) + + "\n"; if (showRepeatingEdges || !result.toString().contains(edgeString)) { result.append(edgeString); diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/LeafNode.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/LeafNode.java index 2c9ff2b..fe45916 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/LeafNode.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/LeafNode.java @@ -43,7 +43,7 @@ public class LeafNode implements Node { * Constructs a new LeafNode with location, value. * * @param location The location of the node in the tree. - * @param value The value associated with the node. + * @param value The value associated with the node. */ public LeafNode(final Bytes location, final V value) { this.location = Optional.of(location); @@ -55,7 +55,7 @@ public LeafNode(final Bytes location, final V value) { * Constructs a new LeafNode with optional location, value. * * @param location The location of the node in the tree (Optional). - * @param value The value associated with the node. + * @param value The value associated with the node. */ public LeafNode(final Optional location, final V value) { this.location = location; @@ -67,7 +67,7 @@ public LeafNode(final Optional location, final V value) { * Accepts a visitor for path-based operations on the node. * * @param visitor The path node visitor. - * @param path The path associated with a node. + * @param path The path associated with a node. * @return The result of the visitor's operation. */ @Override @@ -107,8 +107,7 @@ public Optional getLocation() { } /** - * Get the children of the node. A leaf node does not have children, so this - * method throws an + * Get the children of the node. A leaf node does not have children, so this method throws an * UnsupportedOperationException. * * @return The list of children nodes (unsupported operation). @@ -129,7 +128,8 @@ public Bytes getEncodedValue() { if (encodedValue.isPresent()) { return encodedValue.get(); } - Bytes encodedVal = getValue().isPresent() ? valueSerializer.apply(getValue().get()) : Bytes.EMPTY; + Bytes encodedVal = + getValue().isPresent() ? valueSerializer.apply(getValue().get()) : Bytes.EMPTY; List values = Arrays.asList(encodedVal); Bytes result = RLP.encodeList(values, RLPWriter::writeValue); this.encodedValue = Optional.of(result); @@ -172,10 +172,15 @@ public String toDot(Boolean showRepeatingEdges) { Bytes locationBytes = getLocation().orElse(Bytes.EMPTY); return new StringBuilder() - .append(getClass().getSimpleName()).append(locationBytes) - .append("[location=\"").append(locationBytes) - .append("\", suffix=\"").append(locationBytes.get(locationBytes.size() - 1)) - .append("\", value=\"").append(getValue().orElse(null)).append("\"]\n") + .append(getClass().getSimpleName()) + .append(locationBytes) + .append("[location=\"") + .append(locationBytes) + .append("\", suffix=\"") + .append(locationBytes.get(locationBytes.size() - 1)) + .append("\", value=\"") + .append(getValue().orElse(null)) + .append("\"]\n") .toString(); } } diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/Node.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/Node.java index aabae25..5a9a18c 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/Node.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/Node.java @@ -42,7 +42,7 @@ public interface Node { * Accept a visitor to perform operations on the node based on a provided path. * * @param visitor The visitor to accept. - * @param path The path associated with a node. + * @param path The path associated with a node. * @return The result of visitor's operation. */ Node accept(PathNodeVisitor visitor, Bytes path); @@ -129,9 +129,7 @@ default List> getChildren() { /** * Generates DOT representation for the Node. * - * @param showRepeatingEdges If true, prints all edges; if false, prints only - * unique edges. - * + * @param showRepeatingEdges If true, prints all edges; if false, prints only unique edges. * @return DOT representation of the Node. */ String toDot(Boolean showRepeatingEdges); @@ -139,8 +137,7 @@ default List> getChildren() { /** * Generates DOT representation for the Node. * - *

- * Representation does not contain repeating edges. + *

Representation does not contain repeating edges. * * @return DOT representation of the Node. */ diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/NullLeafNode.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/NullLeafNode.java index 463f843..d411136 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/NullLeafNode.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/NullLeafNode.java @@ -26,11 +26,8 @@ /** * A special node representing a null or empty node in the Verkle Trie. * - *

- * The `NullNode` class serves as a placeholder for non-existent nodes in the - * Verkle Trie - * structure. It implements the Node interface and represents a node that - * contains no information or + *

The `NullNode` class serves as a placeholder for non-existent nodes in the Verkle Trie + * structure. It implements the Node interface and represents a node that contains no information or * value. */ public class NullLeafNode implements Node { @@ -38,12 +35,10 @@ public class NullLeafNode implements Node { private static final NullLeafNode instance = new NullLeafNode(); /** - * Constructs a new `NullNode`. This constructor is protected to ensure that - * `NullNode` instances + * Constructs a new `NullNode`. This constructor is protected to ensure that `NullNode` instances * are only created as singletons. */ - protected NullLeafNode() { - } + protected NullLeafNode() {} /** * Gets the shared instance of the `NullNode`. @@ -60,7 +55,7 @@ public static NullLeafNode instance() { * Accepts a visitor for path-based operations on the node. * * @param visitor The path node visitor. - * @param path The path associated with a node. + * @param path The path associated with a node. * @return The result of the visitor's operation. */ @Override @@ -116,8 +111,12 @@ public String print() { */ @Override public String toDot(Boolean showRepeatingEdges) { - String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " [location=\"" - + getLocation().orElse(Bytes.EMPTY) + "\"]\n"; + String result = + getClass().getSimpleName() + + getLocation().orElse(Bytes.EMPTY) + + " [location=\"" + + getLocation().orElse(Bytes.EMPTY) + + "\"]\n"; return result; } @@ -134,8 +133,7 @@ public boolean isDirty() { /** * Mark the `NullNode` as dirty (not used, no operation). * - *

- * This method intentionally does nothing. + *

This method intentionally does nothing. */ @Override public void markDirty() { diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/NullNode.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/NullNode.java index 844a2a3..e4c0733 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/NullNode.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/NullNode.java @@ -26,11 +26,8 @@ /** * A special node representing a null or empty node in the Verkle Trie. * - *

- * The `NullNode` class serves as a placeholder for non-existent nodes in the - * Verkle Trie - * structure. It implements the Node interface and represents a node that - * contains no information or + *

The `NullNode` class serves as a placeholder for non-existent nodes in the Verkle Trie + * structure. It implements the Node interface and represents a node that contains no information or * value. */ public class NullNode implements Node { @@ -38,12 +35,10 @@ public class NullNode implements Node { private static final NullNode instance = new NullNode(); /** - * Constructs a new `NullNode`. This constructor is protected to ensure that - * `NullNode` instances + * Constructs a new `NullNode`. This constructor is protected to ensure that `NullNode` instances * are only created as singletons. */ - protected NullNode() { - } + protected NullNode() {} /** * Gets the shared instance of the `NullNode`. @@ -60,7 +55,7 @@ public static NullNode instance() { * Accepts a visitor for path-based operations on the node. * * @param visitor The path node visitor. - * @param path The path associated with a node. + * @param path The path associated with a node. * @return The result of the visitor's operation. */ @Override @@ -116,8 +111,12 @@ public String print() { */ @Override public String toDot(Boolean showRepeatingEdges) { - String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + "[location=\"" - + getLocation().orElse(Bytes.EMPTY) + "\"]\n"; + String result = + getClass().getSimpleName() + + getLocation().orElse(Bytes.EMPTY) + + "[location=\"" + + getLocation().orElse(Bytes.EMPTY) + + "\"]\n"; return result; } @@ -134,8 +133,7 @@ public boolean isDirty() { /** * Mark the `NullNode` as dirty (not used, no operation). * - *

- * This method intentionally does nothing. + *

This method intentionally does nothing. */ @Override public void markDirty() { diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/StemNode.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/StemNode.java index 4373e37..4ecde3a 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/StemNode.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/StemNode.java @@ -30,9 +30,7 @@ /** * Represents a stem node in the Verkle Trie. * - *

- * StemNodes are nodes storing the stem of the key, and is the root of the - * suffix to value trie. + *

StemNodes are nodes storing the stem of the key, and is the root of the suffix to value trie. * * @param The type of the node's value. */ @@ -49,15 +47,15 @@ public class StemNode extends BranchNode { /** * Constructs a new StemNode with non-optional parameters. * - * @param location The location in the tree. - * @param stem Node's stem. - * @param hash Node's vector commitment's hash. - * @param commitment Node's vector commitment. - * @param leftHash Hash of vector commitment to left values. - * @param leftCommitment Vector commitment to left values. - * @param rightHash Hash of vector commitment to right values. + * @param location The location in the tree. + * @param stem Node's stem. + * @param hash Node's vector commitment's hash. + * @param commitment Node's vector commitment. + * @param leftHash Hash of vector commitment to left values. + * @param leftCommitment Vector commitment to left values. + * @param rightHash Hash of vector commitment to right values. * @param rightCommitment Vector commitment to right values. - * @param children The list of children nodes. + * @param children The list of children nodes. */ public StemNode( final Bytes location, @@ -80,15 +78,15 @@ public StemNode( /** * Constructs a new StemNode with optional parameters. * - * @param location Optional location in the tree. - * @param stem Node's stem. - * @param hash Optional node's vector commitment's hash. - * @param commitment Optional node's vector commitment. - * @param leftHash Optional hash of vector commitment to left values. - * @param leftCommitment Optional vector commitment to left values. - * @param rightHash Optional hash of vector commitment to right values. + * @param location Optional location in the tree. + * @param stem Node's stem. + * @param hash Optional node's vector commitment's hash. + * @param commitment Optional node's vector commitment. + * @param leftHash Optional hash of vector commitment to left values. + * @param leftCommitment Optional vector commitment to left values. + * @param rightHash Optional hash of vector commitment to right values. * @param rightCommitment Optional vector commitment to right values. - * @param children The list of children nodes. + * @param children The list of children nodes. */ public StemNode( final Optional location, @@ -112,7 +110,7 @@ public StemNode( * Constructs a new BranchNode with non-optional parameters. * * @param location The location in the tree. - * @param stem Node's stem. + * @param stem Node's stem. */ public StemNode(final Bytes location, final Bytes stem) { super(location); @@ -130,7 +128,7 @@ public StemNode(final Bytes location, final Bytes stem) { * Accepts a visitor for path-based operations on the node. * * @param visitor The path node visitor. - * @param path The path associated with a node. + * @param path The path associated with a node. * @return The result of the visitor's operation. */ @Override @@ -225,11 +223,11 @@ public StemNode replaceLocation(final Bytes location) { /** * Creates a new node by replacing all its commitments * - * @param hash Node's vector commitment hash - * @param commitment Node's vector commitment - * @param leftHash Node's left vector commitment hash - * @param leftCommitment Node's left vector commitment - * @param rightHash Node's right vector commitment hash + * @param hash Node's vector commitment hash + * @param commitment Node's vector commitment + * @param leftHash Node's left vector commitment hash + * @param leftCommitment Node's left vector commitment + * @param rightHash Node's right vector commitment hash * @param rightCommitment Node's right vector commitment * @return StemNode with new commitments. */ @@ -262,14 +260,15 @@ public Bytes getEncodedValue() { if (encodedValue.isPresent()) { return encodedValue.get(); } - List values = Arrays.asList( - getStem(), - (Bytes) getHash().get(), - (Bytes) getCommitment().get(), - (Bytes) getLeftHash().get(), - (Bytes) getLeftCommitment().get(), - (Bytes) getRightHash().get(), - (Bytes) getRightCommitment().get()); + List values = + Arrays.asList( + getStem(), + (Bytes) getHash().get(), + (Bytes) getCommitment().get(), + (Bytes) getLeftHash().get(), + (Bytes) getLeftCommitment().get(), + (Bytes) getRightHash().get(), + (Bytes) getRightCommitment().get()); Bytes result = RLP.encodeList(values, RLPWriter::writeValue); this.encodedValue = Optional.of(result); return result; @@ -301,17 +300,28 @@ private Bytes extractStem(final Bytes stemValue) { @Override public String toDot(Boolean showRepeatingEdges) { - StringBuilder result = new StringBuilder() - .append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY)) - .append("[location=\"").append(getLocation().orElse(Bytes.EMPTY)) - .append("\", stem=\"").append(getStem()) - .append("\", leftCommitment=\"").append(getLeftCommitment().orElse(Bytes32.ZERO)) - .append("\", rightCommitment=\"").append(getRightCommitment().orElse(Bytes32.ZERO)).append("\"]\n"); + StringBuilder result = + new StringBuilder() + .append(getClass().getSimpleName()) + .append(getLocation().orElse(Bytes.EMPTY)) + .append("[location=\"") + .append(getLocation().orElse(Bytes.EMPTY)) + .append("\", stem=\"") + .append(getStem()) + .append("\", leftCommitment=\"") + .append(getLeftCommitment().orElse(Bytes32.ZERO)) + .append("\", rightCommitment=\"") + .append(getRightCommitment().orElse(Bytes32.ZERO)) + .append("\"]\n"); for (Node child : getChildren()) { - String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " - + child.getClass().getSimpleName() - + child.getLocation().orElse(Bytes.EMPTY) + "\n"; + String edgeString = + getClass().getSimpleName() + + getLocation().orElse(Bytes.EMPTY) + + " -> " + + child.getClass().getSimpleName() + + child.getLocation().orElse(Bytes.EMPTY) + + "\n"; if (showRepeatingEdges || !result.toString().contains(edgeString)) { result.append(edgeString); diff --git a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/StoredNode.java b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/StoredNode.java index deaa547..a5b78bc 100644 --- a/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/StoredNode.java +++ b/src/main/java/org/hyperledger/besu/ethereum/trie/verkle/node/StoredNode.java @@ -28,8 +28,7 @@ /** * Represents a regular node that can possibly be stored in storage. * - *

- * StoredNodes wrap regular nodes and loads them lazily from storage as needed. + *

StoredNodes wrap regular nodes and loads them lazily from storage as needed. * * @param The type of the node's value. */ @@ -44,7 +43,7 @@ public class StoredNode implements Node { * Constructs a new StoredNode at location. * * @param nodeFactory The node factory for creating nodes from storage. - * @param location The location in the tree. + * @param location The location in the tree. */ public StoredNode(final NodeFactory nodeFactory, final Bytes location) { this.location = location; @@ -56,7 +55,7 @@ public StoredNode(final NodeFactory nodeFactory, final Bytes location) { * Accept a visitor to perform operations on the node based on a provided path. * * @param visitor The visitor to accept. - * @param path The path associated with a node. + * @param path The path associated with a node. * @return The result of visitor's operation. */ @Override @@ -176,8 +175,12 @@ public String print() { */ @Override public String toDot(Boolean showRepeatingEdges) { - String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + "[location=\"" - + getLocation().orElse(Bytes.EMPTY) + "\"]\n"; + String result = + getClass().getSimpleName() + + getLocation().orElse(Bytes.EMPTY) + + "[location=\"" + + getLocation().orElse(Bytes.EMPTY) + + "\"]\n"; return result; } diff --git a/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/DotDisplayTest.java b/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/DotDisplayTest.java index caaa675..4d862cf 100644 --- a/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/DotDisplayTest.java +++ b/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/DotDisplayTest.java @@ -58,7 +58,6 @@ public void testToDotTrieOneValueNoRepeatingEdgesExport() throws IOException { final String fileName = "expectedTreeOneValueNoRepeatingEdges.txt"; final String expectedTree = getResources(fileName); final String actualTree = trie.toDotTree(); - System.out.println(actualTree); assertEquals(expectedTree, actualTree); } diff --git a/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrieTest.java b/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrieTest.java index 784b2c1..8bbc538 100644 --- a/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrieTest.java +++ b/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrieTest.java @@ -25,246 +25,308 @@ public class SimpleVerkleTrieTest { - @Test - public void testToDotTrieOneValueNoRepeatingEdges() { - SimpleVerkleTrie trie = new SimpleVerkleTrie<>(); - Bytes32 key = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - trie.put(key, value); + @Test + public void testToDotTrieOneValueNoRepeatingEdges() { + SimpleVerkleTrie trie = new SimpleVerkleTrie<>(); + Bytes32 key = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + trie.put(key, value); - System.out.println(trie.toDotTree()); - } + System.out.println(trie.toDotTree()); + } - @Test - public void testToDotTrieTwoValuesNoRepeatingEdges() { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); - Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); + @Test + public void testToDotTrieTwoValuesNoRepeatingEdges() { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); + Bytes32 value2 = + Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); - trie.put(key1, value1); - trie.put(key2, value2); + trie.put(key1, value1); + trie.put(key2, value2); - System.out.println(trie.toDotTree()); - } + System.out.println(trie.toDotTree()); + } - @Test - public void testToDotTrieOneValueRepeatingEdges() { - SimpleVerkleTrie trie = new SimpleVerkleTrie<>(); - Bytes32 key = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - trie.put(key, value); + @Test + public void testToDotTrieOneValueRepeatingEdges() { + SimpleVerkleTrie trie = new SimpleVerkleTrie<>(); + Bytes32 key = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + trie.put(key, value); - System.out.println(trie.toDotTree(true)); - } + System.out.println(trie.toDotTree(true)); + } - @Test - public void testToDotTrieTwoValuesRepeatingEdges() { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); - Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); + @Test + public void testToDotTrieTwoValuesRepeatingEdges() { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); + Bytes32 value2 = + Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); - trie.put(key1, value1); - trie.put(key2, value2); + trie.put(key1, value1); + trie.put(key2, value2); - System.out.println(trie.toDotTree(true)); - } + System.out.println(trie.toDotTree(true)); + } - @Test - public void testEmptyTrie() { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(Bytes32.ZERO); - } + @Test + public void testEmptyTrie() { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(Bytes32.ZERO); + } - @Test - public void testOneValue() { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - trie.put(key, value); - assertThat(trie.get(key)) - .as("Get one value should be the inserted value") - .isEqualTo(Optional.of(value)); - Bytes32 expectedRootHash = Bytes32 - .fromHexString("afceaacfd8f1d62ceff7d2bbfc733e42fdb40cef6f7c3c870a5bdd9203c30a16"); - assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); - } + @Test + public void testOneValue() { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + trie.put(key, value); + assertThat(trie.get(key)) + .as("Get one value should be the inserted value") + .isEqualTo(Optional.of(value)); + Bytes32 expectedRootHash = + Bytes32.fromHexString("afceaacfd8f1d62ceff7d2bbfc733e42fdb40cef6f7c3c870a5bdd9203c30a16"); + assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); + } - @Test - public void testTwoValuesAtSameStem() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); - Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key3 = Bytes32.fromHexString("0xde112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - trie.put(key1, value1); - trie.put(key2, value2); - assertThat(trie.get(key1).get()).as("Get first value").isEqualByComparingTo(value1); - assertThat(trie.get(key2).get()).as("Get second value").isEqualByComparingTo(value2); - assertThat(trie.get(key3)).as("Get non-key returns empty").isEmpty(); + @Test + public void testTwoValuesAtSameStem() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); + Bytes32 value2 = + Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key3 = + Bytes32.fromHexString("0xde112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + trie.put(key1, value1); + trie.put(key2, value2); + assertThat(trie.get(key1).get()).as("Get first value").isEqualByComparingTo(value1); + assertThat(trie.get(key2).get()).as("Get second value").isEqualByComparingTo(value2); + assertThat(trie.get(key3)).as("Get non-key returns empty").isEmpty(); - Bytes32 expectedRootHash = Bytes32 - .fromHexString("1defb89c793eb6cf89a90fe7e9bff4b96b5c9774ad21433adb959466a7669602"); - assertThat(trie.getRootHash()).as("Get root hash").isEqualByComparingTo(expectedRootHash); - } + Bytes32 expectedRootHash = + Bytes32.fromHexString("1defb89c793eb6cf89a90fe7e9bff4b96b5c9774ad21433adb959466a7669602"); + assertThat(trie.getRootHash()).as("Get root hash").isEqualByComparingTo(expectedRootHash); + } - @Test - public void testTwoValuesAtDifferentIndex() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0xff112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); - Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); - trie.put(key1, value1); - trie.put(key2, value2); - assertThat(trie.get(key1).get()).as("Get first value").isEqualByComparingTo(value1); - assertThat(trie.get(key2).get()).as("Get second value").isEqualByComparingTo(value2); - Bytes32 expectedRootHash = Bytes32 - .fromHexString("1758925a729ae085d4a2e32139f47c647f70495a6a38053bc0056996dd34b60e"); - assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); - } + @Test + public void testTwoValuesAtDifferentIndex() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0xff112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); + Bytes32 value2 = + Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); + trie.put(key1, value1); + trie.put(key2, value2); + assertThat(trie.get(key1).get()).as("Get first value").isEqualByComparingTo(value1); + assertThat(trie.get(key2).get()).as("Get second value").isEqualByComparingTo(value2); + Bytes32 expectedRootHash = + Bytes32.fromHexString("1758925a729ae085d4a2e32139f47c647f70495a6a38053bc0056996dd34b60e"); + assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); + } - @Test - public void testTwoValuesWithDivergentStemsAtDepth2() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddee"); - Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); - trie.put(key1, value1); - trie.put(key2, value2); - assertThat(trie.get(key1)).as("Retrieve first value").isEqualTo(Optional.of(value1)); - assertThat(trie.get(key2)).as("Retrieve second value").isEqualTo(Optional.of(value2)); - Bytes32 expectedRootHash = Bytes32 - .fromHexString("88028cbafb20137dba8b42d243cfcac81f6ac635cf984c7a89e54ef006bf750d"); - assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); - } + @Test + public void testTwoValuesWithDivergentStemsAtDepth2() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddee"); + Bytes32 value2 = + Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); + trie.put(key1, value1); + trie.put(key2, value2); + assertThat(trie.get(key1)).as("Retrieve first value").isEqualTo(Optional.of(value1)); + assertThat(trie.get(key2)).as("Retrieve second value").isEqualTo(Optional.of(value2)); + Bytes32 expectedRootHash = + Bytes32.fromHexString("88028cbafb20137dba8b42d243cfcac81f6ac635cf984c7a89e54ef006bf750d"); + assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); + } - @Test - public void testDeleteTwoValuesAtSameStem() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000001"); - Bytes32 key2 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); - Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000002"); - trie.put(key1, value1); - trie.put(key2, value2); - trie.remove(key1); - assertThat(trie.get(key1)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - trie.remove(key2); - assertThat(trie.get(key2)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - } + @Test + public void testDeleteTwoValuesAtSameStem() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000001"); + Bytes32 key2 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); + Bytes32 value2 = + Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000002"); + trie.put(key1, value1); + trie.put(key2, value2); + trie.remove(key1); + assertThat(trie.get(key1)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + trie.remove(key2); + assertThat(trie.get(key2)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + } - @Test - public void testDeleteTwoValuesAtDifferentIndex() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0xff112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); - Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); - trie.put(key1, value1); - trie.put(key2, value2); - trie.remove(key1); - assertThat(trie.get(key1)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - trie.remove(key2); - assertThat(trie.get(key2)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - } + @Test + public void testDeleteTwoValuesAtDifferentIndex() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0xff112233445566778899aabbccddeeff00112233445566778899aabbccddee00"); + Bytes32 value2 = + Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); + trie.put(key1, value1); + trie.put(key2, value2); + trie.remove(key1); + assertThat(trie.get(key1)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + trie.remove(key2); + assertThat(trie.get(key2)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + } - @Test - public void testDeleteTwoValuesWithDivergentStemsAtDepth2() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddee"); - Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); - trie.put(key1, value1); - trie.put(key2, value2); - trie.remove(key1); - assertThat(trie.get(key1)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - trie.remove(key2); - assertThat(trie.get(key2)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - } + @Test + public void testDeleteTwoValuesWithDivergentStemsAtDepth2() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddee"); + Bytes32 value2 = + Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000"); + trie.put(key1, value1); + trie.put(key2, value2); + trie.remove(key1); + assertThat(trie.get(key1)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + trie.remove(key2); + assertThat(trie.get(key2)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + } - @Test - public void testDeleteThreeValues() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddee"); - Bytes32 value2 = Bytes32.fromHexString("0x0200000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key3 = Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddff"); - Bytes32 value3 = Bytes32.fromHexString("0x0300000000000000000000000000000000000000000000000000000000000000"); - trie.put(key1, value1); - trie.put(key2, value2); - trie.put(key3, value3); - trie.remove(key3); - assertThat(trie.get(key3)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - assertThat(trie.get(key2)).as("Retrieve second value").isEqualTo(Optional.of(value2)); - trie.remove(key2); - assertThat(trie.get(key2)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - assertThat(trie.get(key1)).as("Retrieve first value").isEqualTo(Optional.of(value1)); - trie.remove(key1); - assertThat(trie.get(key1)).as("Make sure value is deleted").isEqualTo(Optional.empty()); - } + @Test + public void testDeleteThreeValues() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddee"); + Bytes32 value2 = + Bytes32.fromHexString("0x0200000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key3 = + Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddff"); + Bytes32 value3 = + Bytes32.fromHexString("0x0300000000000000000000000000000000000000000000000000000000000000"); + trie.put(key1, value1); + trie.put(key2, value2); + trie.put(key3, value3); + trie.remove(key3); + assertThat(trie.get(key3)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + assertThat(trie.get(key2)).as("Retrieve second value").isEqualTo(Optional.of(value2)); + trie.remove(key2); + assertThat(trie.get(key2)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + assertThat(trie.get(key1)).as("Retrieve first value").isEqualTo(Optional.of(value1)); + trie.remove(key1); + assertThat(trie.get(key1)).as("Make sure value is deleted").isEqualTo(Optional.empty()); + } - @Test - public void testDeleteThreeValuesWithFlattening() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie(); - Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); - Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key2 = Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddee"); - Bytes32 value2 = Bytes32.fromHexString("0x0200000000000000000000000000000000000000000000000000000000000000"); - Bytes32 key3 = Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddff"); - Bytes32 value3 = Bytes32.fromHexString("0x0300000000000000000000000000000000000000000000000000000000000000"); - trie.put(key1, value1); - trie.put(key2, value2); - trie.put(key3, value3); - trie.remove(key1); - assertThat(trie.get(key1)).as("First value has been deleted").isEqualTo(Optional.empty()); - assertThat(trie.get(key2)).as("Second value").isEqualTo(Optional.of(value2)); - trie.remove(key2); - assertThat(trie.get(key2)).as("Second value has been deleted").isEqualTo(Optional.empty()); - assertThat(trie.get(key3)).as("Third value").isEqualTo(Optional.of(value3)); - trie.remove(key3); - assertThat(trie.get(key3)).as("Third value has been deleted").isEqualTo(Optional.empty()); - } + @Test + public void testDeleteThreeValuesWithFlattening() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie(); + Bytes32 key1 = + Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); + Bytes32 value1 = + Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key2 = + Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddee"); + Bytes32 value2 = + Bytes32.fromHexString("0x0200000000000000000000000000000000000000000000000000000000000000"); + Bytes32 key3 = + Bytes32.fromHexString("0x00ff112233445566778899aabbccddeeff00112233445566778899aabbccddff"); + Bytes32 value3 = + Bytes32.fromHexString("0x0300000000000000000000000000000000000000000000000000000000000000"); + trie.put(key1, value1); + trie.put(key2, value2); + trie.put(key3, value3); + trie.remove(key1); + assertThat(trie.get(key1)).as("First value has been deleted").isEqualTo(Optional.empty()); + assertThat(trie.get(key2)).as("Second value").isEqualTo(Optional.of(value2)); + trie.remove(key2); + assertThat(trie.get(key2)).as("Second value has been deleted").isEqualTo(Optional.empty()); + assertThat(trie.get(key3)).as("Third value").isEqualTo(Optional.of(value3)); + trie.remove(key3); + assertThat(trie.get(key3)).as("Third value has been deleted").isEqualTo(Optional.empty()); + } - @Test - public void testDeleteManyValuesWithDivergentStemsAtDepth2() throws Exception { - SimpleVerkleTrie trie = new SimpleVerkleTrie<>(); - assertThat(trie.getRootHash()).isEqualTo(Bytes32.ZERO); - Bytes32 key0 = Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45641"); - Bytes32 value0 = Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000001"); - Bytes32 key1 = Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45601"); - Bytes32 value1 = Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000001"); - Bytes32 key2 = Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45602"); - Bytes32 value2 = Bytes32.fromHexString("0x01"); - Bytes32 key3 = Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45600"); - Bytes32 value3 = Bytes32.fromHexString("0x00"); - Bytes32 key4 = Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45603"); - Bytes32 value4 = Bytes32.fromHexString("0xf84a97f1f0a956e738abd85c2e0a5026f8874e3ec09c8f012159dfeeaab2b156"); - Bytes32 key5 = Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45604"); - Bytes32 value5 = Bytes32.fromHexString("0x03"); - Bytes32 key6 = Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45680"); - Bytes32 value6 = Bytes32.fromHexString("0x0000010200000000000000000000000000000000000000000000000000000000"); - trie.put(key0, value0); - trie.put(key1, value1); - trie.put(key2, value2); - trie.put(key3, value3); - trie.put(key4, value4); - trie.put(key5, value5); - trie.put(key6, value6); - trie.remove(key0); - trie.remove(key4); - trie.remove(key5); - trie.remove(key6); - trie.remove(key3); - trie.remove(key1); - trie.remove(key2); - assertThat(trie.getRootHash()).isEqualTo(Bytes32.ZERO); - } + @Test + public void testDeleteManyValuesWithDivergentStemsAtDepth2() throws Exception { + SimpleVerkleTrie trie = new SimpleVerkleTrie<>(); + assertThat(trie.getRootHash()).isEqualTo(Bytes32.ZERO); + Bytes32 key0 = + Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45641"); + Bytes32 value0 = + Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000001"); + Bytes32 key1 = + Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45601"); + Bytes32 value1 = + Bytes32.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000001"); + Bytes32 key2 = + Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45602"); + Bytes32 value2 = Bytes32.fromHexString("0x01"); + Bytes32 key3 = + Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45600"); + Bytes32 value3 = Bytes32.fromHexString("0x00"); + Bytes32 key4 = + Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45603"); + Bytes32 value4 = + Bytes32.fromHexString("0xf84a97f1f0a956e738abd85c2e0a5026f8874e3ec09c8f012159dfeeaab2b156"); + Bytes32 key5 = + Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45604"); + Bytes32 value5 = Bytes32.fromHexString("0x03"); + Bytes32 key6 = + Bytes32.fromHexString("0x1e4abaeaa58259f4784e086ddbaa74a9d3975efb2e4380595f0eed5692c45680"); + Bytes32 value6 = + Bytes32.fromHexString("0x0000010200000000000000000000000000000000000000000000000000000000"); + trie.put(key0, value0); + trie.put(key1, value1); + trie.put(key2, value2); + trie.put(key3, value3); + trie.put(key4, value4); + trie.put(key5, value5); + trie.put(key6, value6); + trie.remove(key0); + trie.remove(key4); + trie.remove(key5); + trie.remove(key6); + trie.remove(key3); + trie.remove(key1); + trie.remove(key2); + assertThat(trie.getRootHash()).isEqualTo(Bytes32.ZERO); + } }