From 5000dbf789bf591b363f023001b09404d2038a7d Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Mon, 15 Jul 2019 16:41:15 -0400 Subject: [PATCH] change getChildren return type (#1674) Signed-off-by: Adrian Sutton --- .../tech/pegasys/pantheon/ethereum/trie/BranchNode.java | 4 ++-- .../tech/pegasys/pantheon/ethereum/trie/ExtensionNode.java | 4 ++-- .../java/tech/pegasys/pantheon/ethereum/trie/LeafNode.java | 5 +++-- .../main/java/tech/pegasys/pantheon/ethereum/trie/Node.java | 2 +- .../java/tech/pegasys/pantheon/ethereum/trie/NullNode.java | 5 +++-- .../tech/pegasys/pantheon/ethereum/trie/StoredNode.java | 2 +- .../pegasys/pantheon/ethereum/trie/TrieNodeDecoder.java | 6 +++--- .../pegasys/pantheon/ethereum/trie/TrieNodeDecoderTest.java | 2 +- 8 files changed, 16 insertions(+), 14 deletions(-) diff --git a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/BranchNode.java b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/BranchNode.java index 2899d4a14d..371941f697 100644 --- a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/BranchNode.java +++ b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/BranchNode.java @@ -76,8 +76,8 @@ class BranchNode implements Node { } @Override - public Optional>> getChildren() { - return Optional.of(Collections.unmodifiableList(children)); + public List> getChildren() { + return Collections.unmodifiableList(children); } public Node child(final byte index) { diff --git a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/ExtensionNode.java b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/ExtensionNode.java index 41c0909581..32566cb473 100644 --- a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/ExtensionNode.java +++ b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/ExtensionNode.java @@ -64,8 +64,8 @@ class ExtensionNode implements Node { } @Override - public Optional>> getChildren() { - return Optional.of(Collections.singletonList(child)); + public List> getChildren() { + return Collections.singletonList(child); } public Node getChild() { diff --git a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/LeafNode.java b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/LeafNode.java index 45e72e6923..c91a21ac04 100644 --- a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/LeafNode.java +++ b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/LeafNode.java @@ -21,6 +21,7 @@ import tech.pegasys.pantheon.util.bytes.BytesValue; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.function.Function; @@ -66,8 +67,8 @@ class LeafNode implements Node { } @Override - public Optional>> getChildren() { - return Optional.empty(); + public List> getChildren() { + return Collections.emptyList(); } @Override diff --git a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/Node.java b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/Node.java index 8f35692d20..e24eaaa4b5 100644 --- a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/Node.java +++ b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/Node.java @@ -28,7 +28,7 @@ public interface Node { Optional getValue(); - Optional>> getChildren(); + List> getChildren(); BytesValue getRlp(); diff --git a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/NullNode.java b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/NullNode.java index e6996facab..15f388d16d 100644 --- a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/NullNode.java +++ b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/NullNode.java @@ -15,6 +15,7 @@ package tech.pegasys.pantheon.ethereum.trie; import tech.pegasys.pantheon.util.bytes.Bytes32; import tech.pegasys.pantheon.util.bytes.BytesValue; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -50,8 +51,8 @@ class NullNode implements Node { } @Override - public Optional>> getChildren() { - return Optional.empty(); + public List> getChildren() { + return Collections.emptyList(); } @Override diff --git a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/StoredNode.java b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/StoredNode.java index a9b0759829..6b409aba23 100644 --- a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/StoredNode.java +++ b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/StoredNode.java @@ -65,7 +65,7 @@ class StoredNode implements Node { } @Override - public Optional>> getChildren() { + public List> getChildren() { return load().getChildren(); } diff --git a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/TrieNodeDecoder.java b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/TrieNodeDecoder.java index 92e9525a66..5212e90df0 100644 --- a/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/TrieNodeDecoder.java +++ b/ethereum/trie/src/main/java/tech/pegasys/pantheon/ethereum/trie/TrieNodeDecoder.java @@ -57,7 +57,7 @@ public class TrieNodeDecoder { nodes.add(node); final List> toProcess = new ArrayList<>(); - node.getChildren().ifPresent(toProcess::addAll); + toProcess.addAll(node.getChildren()); while (!toProcess.isEmpty()) { final Node currentNode = toProcess.remove(0); if (Objects.equals(NullNode.instance(), currentNode)) { @@ -68,7 +68,7 @@ public class TrieNodeDecoder { if (!currentNode.isReferencedByHash()) { // If current node is inlined, that means we can process its children - currentNode.getChildren().ifPresent(toProcess::addAll); + toProcess.addAll(currentNode.getChildren()); } } @@ -140,7 +140,7 @@ public class TrieNodeDecoder { final Node nextNode = currentNodes.remove(0); final List> children = new ArrayList<>(); - nextNode.getChildren().ifPresent(children::addAll); + children.addAll(nextNode.getChildren()); while (!children.isEmpty()) { Node child = children.remove(0); if (Objects.equals(child, NullNode.instance())) { diff --git a/ethereum/trie/src/test/java/tech/pegasys/pantheon/ethereum/trie/TrieNodeDecoderTest.java b/ethereum/trie/src/test/java/tech/pegasys/pantheon/ethereum/trie/TrieNodeDecoderTest.java index 4feb18eb03..1361722cdb 100644 --- a/ethereum/trie/src/test/java/tech/pegasys/pantheon/ethereum/trie/TrieNodeDecoderTest.java +++ b/ethereum/trie/src/test/java/tech/pegasys/pantheon/ethereum/trie/TrieNodeDecoderTest.java @@ -114,7 +114,7 @@ public class TrieNodeDecoderTest { assertThat(depth0And1Nodes.get(0).getHash()).isEqualTo(rootNode.getHash()); // Subsequent nodes should be children of root node List expectedNodesHashes = - rootNode.getChildren().get().stream() + rootNode.getChildren().stream() .filter(n -> !Objects.equals(n, NullNode.instance())) .map(Node::getHash) .collect(Collectors.toList());