From 330d58b242117a079b8e7d501a4085cda679feed Mon Sep 17 00:00:00 2001 From: "Filip.Gumula" Date: Tue, 14 Nov 2023 14:26:51 +0100 Subject: [PATCH] Modification of toDot method, added showRepeatingEdges flag, to manage possibility of displaying repeating edges, if 'true' show all adges, if 'false' don't display repeating edges --- .../trie/verkle/SimpleVerkleTrie.java | 4 +-- .../ethereum/trie/verkle/node/BranchNode.java | 6 ++-- .../trie/verkle/node/InternalNode.java | 6 ++-- .../ethereum/trie/verkle/node/LeafNode.java | 2 +- .../besu/ethereum/trie/verkle/node/Node.java | 8 +++-- .../trie/verkle/node/NullLeafNode.java | 2 +- .../ethereum/trie/verkle/node/NullNode.java | 2 +- .../ethereum/trie/verkle/node/StemNode.java | 7 ++-- .../ethereum/trie/verkle/node/StoredNode.java | 2 +- .../trie/verkle/SimpleVerkleTrieTest.java | 32 ++++++++++++++++--- 10 files changed, 49 insertions(+), 22 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 03be1f8..b789c37 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 @@ -148,10 +148,10 @@ public void commit(final NodeUpdater nodeUpdater) { * @return The DOT representation of the Verkle Trie. */ - public String toDotTree() { + public String toDotTree(Boolean showRepeatingEdges) { StringBuilder result = new StringBuilder("digraph VerkleTrie {\n"); Node root = getRoot(); - result.append(root.toDot()); + result.append(root.toDot(showRepeatingEdges)); return result.append("}\n").toString(); } } 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 b232483..64b3065 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 @@ -248,7 +248,7 @@ public String print() { * @return DOT representation of the BranchNode. */ @Override - public String toDot() { + public String toDot(Boolean showRepeatingEdges) { StringBuilder result = new StringBuilder() .append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY)) .append("\", location=\"").append(getLocation().orElse(Bytes.EMPTY)) @@ -258,10 +258,10 @@ public String toDot() { String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " + child.getClass().getSimpleName() + child.getLocation().orElse(Bytes.EMPTY) + "\n"; - if(!result.toString().contains(edgeString)) { + if(showRepeatingEdges || !result.toString().contains(edgeString)) { result.append(edgeString); } - result.append(child.toDot()); + result.append(child.toDot(showRepeatingEdges)); } return result.toString(); } 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 9702b25..6e80a8e 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 @@ -165,7 +165,7 @@ public String print() { * @return DOT representation of the InternalNode. */ @Override - public String toDot() { + public String toDot(Boolean showRepeatingEdges) { StringBuilder result = new StringBuilder() .append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY)) .append("[location=\"").append(getLocation().orElse(Bytes.EMPTY)) @@ -175,10 +175,10 @@ public String toDot() { String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " + child.getClass().getSimpleName() + child.getLocation().orElse(Bytes.EMPTY) + "\n"; - if(!result.toString().contains(edgeString)) { + if(showRepeatingEdges || !result.toString().contains(edgeString)) { result.append(edgeString); } - result.append(child.toDot()); + result.append(child.toDot(showRepeatingEdges)); } return result.toString(); } 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 d98b844..e28ff91 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 @@ -166,7 +166,7 @@ public String print() { * @return DOT representation of the LeafNode. */ @Override - public String toDot() { + public String toDot(Boolean showRepeatingEdges) { Bytes locationBytes = getLocation().orElse(Bytes.EMPTY); return new StringBuilder() 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 bc841ec..e6ad5d7 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 @@ -127,10 +127,12 @@ default List> getChildren() { String print(); /** - * Generates DOT representation for the node. + * Generates DOT representation for the Node. * - * @return DOT representation of the node. + * @param showRepeatingEdges If true, prints all edges; if false, prints only unique edges. + * + * @return DOT representation of the Node. */ - String toDot(); + String toDot(Boolean showRepeatingEdges); } 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 36ee56d..95bd33b 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 @@ -110,7 +110,7 @@ public String print() { * @return DOT representation of the NullLeafNode. */ @Override - public String toDot() { + public String toDot(Boolean showRepeatingEdges) { String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " [location=\"" + getLocation().orElse(Bytes.EMPTY) + "\"]\n"; return result; } 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 69e3d47..d50f18b 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 @@ -110,7 +110,7 @@ public String print() { * @return DOT representation of the NullNode. */ @Override - public String toDot() { + public String toDot(Boolean showRepeatingEdges) { String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + "[location=\"" + getLocation().orElse(Bytes.EMPTY) + "\"]\n"; return result; } 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 17b71b9..6527c52 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 @@ -298,8 +298,9 @@ private Bytes extractStem(final Bytes stemValue) { return stemValue.slice(0, 31); } + @Override - public String toDot() { + public String toDot(Boolean showRepeatingEdges) { StringBuilder result = new StringBuilder() .append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY)) .append("[location=\"").append(getLocation().orElse(Bytes.EMPTY)) @@ -311,10 +312,10 @@ public String toDot() { String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " + child.getClass().getSimpleName() + child.getLocation().orElse(Bytes.EMPTY) + "\n"; - if(!result.toString().contains(edgeString)) { + if(showRepeatingEdges || !result.toString().contains(edgeString)) { result.append(edgeString); } - result.append(child.toDot()); + result.append(child.toDot(showRepeatingEdges)); } return result.toString(); } 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 16e0b61..801f95c 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 @@ -174,7 +174,7 @@ public String print() { * @return DOT representation of the StoredNode. */ @Override - public String toDot() { + public String toDot(Boolean showRepeatingEdges) { 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/SimpleVerkleTrieTest.java b/src/test/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleVerkleTrieTest.java index 68bb1ea..dd195ee 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 @@ -27,18 +27,18 @@ public class SimpleVerkleTrieTest { @Test - public void toDotTreeWithOneValue() { + 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(false)); } @Test - public void toDotTreeWithTwoValues() { + public void testToDotTrieTwoValuesNoRepeatingEdges() { SimpleVerkleTrie trie = new SimpleVerkleTrie(); Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"); Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000"); @@ -48,7 +48,31 @@ public void toDotTreeWithTwoValues() { trie.put(key1, value1); trie.put(key2, value2); - System.out.println(trie.toDotTree()); + System.out.println(trie.toDotTree(false)); + } + @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)); + } + + + @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); + + System.out.println(trie.toDotTree(true)); } @Test public void testEmptyTrie() {