Skip to content

Commit

Permalink
Fix newline issue, tests pass, format
Browse files Browse the repository at this point in the history
Signed-off-by: Neo <[email protected]>
  • Loading branch information
Uacias authored and neotheprogramist committed Nov 21, 2023
1 parent bbe9931 commit deee991
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public Node<V> 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<V> get(final K key) {
Expand All @@ -90,7 +89,7 @@ public Optional<V> 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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -163,21 +159,19 @@ public String toDotTree(Boolean showRepeatingEdges) {
/**
* Returns the DOT representation of the entire Verkle Trie.
*
* <p>
* The representation does not contain repeating edges.
* <p>The representation does not contain repeating edges.
*
* @return The DOT representation of the Verkle Trie.
*/
public String toDotTree() {
StringBuilder result = new StringBuilder("digraph VerkleTrie {\n");
Node<V> 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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ public StoredVerkleTrie(final NodeFactory<V> nodeFactory) {
super(nodeFactory.retrieve(Bytes.EMPTY, null));
this.nodeFactory = nodeFactory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,25 @@
public interface VerkleTrie<K, V> {

/**
* 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<V> 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<V> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public abstract class BranchNode<V> implements Node<V> {
/**
* 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,
Expand All @@ -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<Bytes> location,
Expand Down Expand Up @@ -96,8 +95,7 @@ public BranchNode(final Optional<Bytes> location, final List<Node<V>> 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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -153,7 +151,7 @@ public Node<V> 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<V> childNode) {
Expand Down Expand Up @@ -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<V> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public class InternalNode<V> extends BranchNode<V> {
/**
* 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,
Expand All @@ -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<Bytes> location,
Expand All @@ -80,8 +79,7 @@ public InternalNode(final Optional<Bytes> location, final List<Node<V>> 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.
Expand All @@ -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
Expand All @@ -116,7 +114,7 @@ public Node<V> accept(final NodeVisitor<V> 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.
*/
Expand Down Expand Up @@ -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<V> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class LeafNode<V> implements Node<V> {
* 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);
Expand All @@ -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<Bytes> location, final V value) {
this.location = location;
Expand All @@ -67,7 +67,7 @@ public LeafNode(final Optional<Bytes> 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
Expand Down Expand Up @@ -107,8 +107,7 @@ public Optional<Bytes> 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).
Expand All @@ -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<Bytes> values = Arrays.asList(encodedVal);
Bytes result = RLP.encodeList(values, RLPWriter::writeValue);
this.encodedValue = Optional.of(result);
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface Node<V> {
* 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<V> accept(PathNodeVisitor<V> visitor, Bytes path);
Expand Down Expand Up @@ -129,18 +129,15 @@ default List<Node<V>> 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);

/**
* Generates DOT representation for the Node.
*
* <p>
* Representation does not contain repeating edges.
* <p>Representation does not contain repeating edges.
*
* @return DOT representation of the Node.
*/
Expand Down
Loading

0 comments on commit deee991

Please sign in to comment.