Reduce Exception Hiding in Trie RLP Processing (#1536)

The current formulation of the decode can hide exceptions if the finally
block does not complete.  Since exceptions are thrown mid-rlp processing
this typically results in the list not being fully consumed and the real
exception is hidden by a "not at end of list" exception.

This more verbose form prevents such exceptions from being overruled by
rlp processing errors.

Signed-off-by: Danno Ferrin <danno.ferrin@gmail.com>
pull/1543/head
Danno Ferrin 4 years ago committed by GitHub
parent 954906720a
commit 88b9c01503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 64
      ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/StoredNodeFactory.java

@ -118,36 +118,40 @@ class StoredNodeFactory<V> implements NodeFactory<V> {
private Node<V> decode(final RLPInput nodeRLPs, final Supplier<String> errMessage) {
final int nodesCount = nodeRLPs.enterList();
try {
switch (nodesCount) {
case 1:
return decodeNull(nodeRLPs, errMessage);
case 2:
final Bytes encodedPath = nodeRLPs.readBytes();
final Bytes path;
try {
path = CompactEncoding.decode(encodedPath);
} catch (final IllegalArgumentException ex) {
throw new MerkleTrieException(errMessage.get() + ": invalid path " + encodedPath, ex);
}
final int size = path.size();
if (size > 0 && path.get(size - 1) == CompactEncoding.LEAF_TERMINATOR) {
return decodeLeaf(path, nodeRLPs, errMessage);
} else {
return decodeExtension(path, nodeRLPs, errMessage);
}
case (BranchNode.RADIX + 1):
return decodeBranch(nodeRLPs, errMessage);
default:
throw new MerkleTrieException(
errMessage.get() + format(": invalid list size %s", nodesCount));
}
} finally {
nodeRLPs.leaveList();
switch (nodesCount) {
case 1:
final NullNode<V> nullNode = decodeNull(nodeRLPs, errMessage);
nodeRLPs.leaveList();
return nullNode;
case 2:
final Bytes encodedPath = nodeRLPs.readBytes();
final Bytes path;
try {
path = CompactEncoding.decode(encodedPath);
} catch (final IllegalArgumentException ex) {
throw new MerkleTrieException(errMessage.get() + ": invalid path " + encodedPath, ex);
}
final int size = path.size();
if (size > 0 && path.get(size - 1) == CompactEncoding.LEAF_TERMINATOR) {
final LeafNode<V> leafNode = decodeLeaf(path, nodeRLPs, errMessage);
nodeRLPs.leaveList();
return leafNode;
} else {
final Node<V> extensionNode = decodeExtension(path, nodeRLPs, errMessage);
nodeRLPs.leaveList();
return extensionNode;
}
case (BranchNode.RADIX + 1):
final BranchNode<V> branchNode = decodeBranch(nodeRLPs, errMessage);
nodeRLPs.leaveList();
return branchNode;
default:
throw new MerkleTrieException(
errMessage.get() + format(": invalid list size %s", nodesCount));
}
}

Loading…
Cancel
Save