Skip to content

Commit

Permalink
Added the oDot method to VerkleTrie for a human-readable string repre…
Browse files Browse the repository at this point in the history
…sentation in Dot format.
  • Loading branch information
Uacias authored and neotheprogramist committed Nov 14, 2023
1 parent 26921d9 commit dc37f4b
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,17 @@ public void commit(final NodeUpdater nodeUpdater) {
root = root.accept(new HashVisitor<V>(), Bytes.EMPTY);
root = root.accept(new CommitVisitor<V>(nodeUpdater), Bytes.EMPTY);
}

/**
* Returns the DOT representation of the entire Verkle Trie.
*
* @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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ public interface VerkleTrie<K, V> {
* @param nodeUpdater used to store the node values
*/
void commit(NodeUpdater nodeUpdater);

/**
* Returns the DOT representation of the entire Verkle Trie.
*
* @return The DOT representation of the Verkle Trie.
*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,29 @@ public String print() {
}
return builder.toString();
}

/**
* Generates DOT representation for the BranchNode.
*
* @return DOT representation of the BranchNode.
*/
@Override
public String toDot() {
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";

if(!result.toString().contains(edgeString)) {
result.append(edgeString);
}
result.append(child.toDot());
}
return result.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,29 @@ public String print() {
}
return builder.toString();
}

/**
* Generates DOT representation for the InternalNode.
*
* @return DOT representation of the InternalNode.
*/
@Override
public String toDot() {
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";

if(!result.toString().contains(edgeString)) {
result.append(edgeString);
}
result.append(child.toDot());
}
return result.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,22 @@ public boolean isDirty() {
public String print() {
return "Leaf:" + getValue().map(Object::toString).orElse("empty");
}

/**
* Generates DOT representation for the LeafNode.
*
* @return DOT representation of the LeafNode.
*/
@Override
public String toDot() {
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")
.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,12 @@ default List<Node<V>> getChildren() {
* @return A string representation of the node.
*/
String print();

/**
* Generates DOT representation for the node.
*
* @return DOT representation of the node.
*/
String toDot();

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ public String print() {
return "[NULL-LEAF]";
}

/**
* Generates DOT representation for the NullLeafNode.
*
* @return DOT representation of the NullLeafNode.
*/
@Override
public String toDot() {
String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " [location=\"" + getLocation().orElse(Bytes.EMPTY) + "\"]\n";
return result;
}

/**
* Check if the `NullNode` is marked as dirty (needing to be persisted).
*
Expand All @@ -123,4 +134,5 @@ public boolean isDirty() {
public void markDirty() {
// do nothing
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ public String print() {
return "[NULL]";
}

/**
* Generates DOT representation for the NullNode.
*
* @return DOT representation of the NullNode.
*/
@Override
public String toDot() {
String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + "[location=\"" + getLocation().orElse(Bytes.EMPTY) + "\"]\n";
return result;
}

/**
* Check if the `NullNode` is marked as dirty (needing to be persisted).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,26 @@ public String print() {
private Bytes extractStem(final Bytes stemValue) {
return stemValue.slice(0, 31);
}

@Override
public String toDot() {
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<V> child : getChildren()) {
String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " + child.getClass().getSimpleName()
+ child.getLocation().orElse(Bytes.EMPTY) + "\n";

if(!result.toString().contains(edgeString)) {
result.append(edgeString);
}
result.append(child.toDot());
}
return result.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ public String print() {
return String.format("(stored) %s", node.print());
}

/**
* Generates DOT representation for the StoredNode.
*
* @return DOT representation of the StoredNode.
*/
@Override
public String toDot() {
String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + "[location=\"" + getLocation().orElse(Bytes.EMPTY) + "\"]\n";
return result;
}

private Node<V> load() {
if (loadedNode.isEmpty()) {
loadedNode = nodeFactory.retrieve(location, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@

public class SimpleVerkleTrieTest {


@Test
public void toDotTreeWithOneValue() {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<>();
Bytes32 key = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
trie.put(key, value);

System.out.println(trie.toDotTree());
}


@Test
public void toDotTreeWithTwoValues() {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<Bytes32, Bytes32>();
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);

System.out.println(trie.toDotTree());
}
@Test
public void testEmptyTrie() {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<Bytes32, Bytes32>();
Expand Down

0 comments on commit dc37f4b

Please sign in to comment.