Class Node


public class Node extends AbstractNode
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. 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 Type
    Method
    Description
    byte[]
    Returns a copy of the node's data.
    getLeaf(int index)
    Returns the leaf node at the given index.
    Returns the root of the tree.
    final boolean
    Determines if the node is a carry.
    final boolean
    Determines whether this node is the right child of its parent.
    final boolean
     
    final int
    Returns the total number of leaves in the tree this node belongs to.
    Returns this node's left child, or null if this node is a leaf.
    Returns this node's parent, or null if this node is root.
    Returns this node's right child, or null if this node is a leaf.
    final int
    Returns the node's serial index.
    Returns the sibling that makes this node's parent, or null if this node is root.
    Returns the tree this node belongs to.
    boolean
    Verifies the hash of this node against its children, if it has any.

    Methods inherited from class io.crums.util.mrkl.index.AbstractNode

    equals, hashCode, index, isLeaf, isLeft, level, toString

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • isRight

      public final boolean isRight()
      Description copied from class: AbstractNode
      Determines whether this node is the right child of its parent.

      Implementations should be marked final.

      Specified by:
      isRight in class AbstractNode
    • 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

      public boolean verify(MessageDigest digest)
      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

      public Node sibling()
      Returns the sibling that makes this node's parent, or null if this node is root.
    • parent

      public Node parent()
      Returns this node's parent, or null if this node is root.
    • leftChild

      public Node leftChild()
      Returns this node's left child, or null if this node is a leaf.
      See Also:
    • rightChild

      public Node rightChild()
      Returns this node's right child, or null if this node is a leaf.
      See Also:
    • getRoot

      public Node getRoot()
      Returns the root of the tree.
    • getLeaf

      public Node getLeaf(int index) throws IndexOutOfBoundsException
      Returns the leaf node at the given index.
      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

      public Tree tree()
      Returns the tree this node belongs to.