Class Node
java.lang.Object
io.crums.util.mrkl.index.AbstractNode
io.crums.util.mrkl.Node
Merkle tree node. Besides its coordinates (level, index), each node holds
data, which for internal nodes is a fixed-width signature.
(The leaf nodes can be anything--including another signature.) Instances are
immutable, so they're safe to pass around.
Navigation
Supports navigating to parent, siblings, and children--as well as random access. Note these access methods return a new object every time they're invoked. (This is to minimize the memory footprint of large trees.) Equality and hashCode semantics are properly implemented, so as long as you don't compare instances by reference, you'll be OK.-
Field Summary
Fields inherited from class io.crums.util.mrkl.index.AbstractNode
FACTORY -
Method Summary
Modifier and TypeMethodDescriptionbyte[]data()Returns a copy of the node's data.getLeaf(int index) Returns the leaf node at the givenindex.getRoot()Returns the root of the tree.final booleanisCarry()Determines if the node is a carry.final booleanisRight()Determines whether this node is the right child of its parent.final booleanisRoot()final intReturns the total number of leaves in the tree this node belongs to.Returns this node's left child, ornullif this node is a leaf.parent()Returns this node's parent, ornullif this node is root.Returns this node's right child, ornullif this node is a leaf.final intReturns the node's serial index.sibling()Returns the sibling that makes this node's parent, ornullif this node is root.tree()Returns the tree this node belongs to.booleanverify(MessageDigest digest) Verifies the hash of this node against its children, if it has any.
-
Method Details
-
isRight
public final boolean isRight()Description copied from class:AbstractNodeDetermines whether this node is the right child of its parent.Implementations should be marked final.
- Specified by:
isRightin classAbstractNode
-
serialIndex
public final int serialIndex()Returns the node's serial index. This is equal to the number of nodes preceding this node in a breadth-first traversal of the tree. The root node, therefore, returns 0; the last leaf returns 2 x leafCount - 2. -
data
public byte[] data()Returns a copy of the node's data. For internal nodes, this is always the hash of the node itself (computed from the hash of its child nodes); for leaf nodes, depending on the tree type, this can be the hash of an item, or an item itself.- See Also:
-
verify
Verifies the hash of this node against its children, if it has any. The verification is not recursive.Mostly only for testing. Reason why: in a Merkle tree, you construct proofs from the bottom up, from leaf to root.
-
isRoot
public final boolean isRoot() -
sibling
Returns the sibling that makes this node's parent, ornullif this node is root. -
parent
Returns this node's parent, ornullif this node is root. -
leftChild
-
rightChild
-
getRoot
Returns the root of the tree. -
getLeaf
Returns the leaf node at the givenindex.- Parameters:
index- zero-based index into leaf count- Throws:
IndexOutOfBoundsException- See Also:
-
isCarry
public final boolean isCarry()Determines if the node is a carry. A node is a carry if it is an internal node and its right child is from a lower level than its left child. -
leafCount
public final int leafCount()Returns the total number of leaves in the tree this node belongs to. -
tree
Returns the tree this node belongs to.
-