Class Nibbles

java.lang.Object
com.norswap.nanoeth.trees.patricia.Nibbles

public final class Nibbles
extends Object
Represent a sequence of nibbles (half-bytes, 4-bit values), which are used as key fragments in Patricia tree lookups.

The hexPrefix(boolean) method implements the hex-prefix encoding for sequences of nibbles, as specified in appendix C of the yellowpaper.

Note that when represented as an array of bytes, the spec specifies big-endian ordering (i.e. the first nibble in a byte is the high-order nibble).

  • Field Summary

    Fields
    Modifier and Type Field Description
    static Nibbles EMPTY  
  • Constructor Summary

    Constructors
    Constructor Description
    Nibbles​(byte nibble)
    Construct a nibble sequence containing a single nibble.
    Nibbles​(byte[] key)  
    Nibbles​(byte[] key, int start, int end)  
  • Method Summary

    Modifier and Type Method Description
    byte[] bytes()
    Returns a copy of this nibble sequence as an array of bytes.
    Nibbles concat​(Nibbles other)  
    Nibbles dropFirst​(int n)
    Return a suffix of this nibble sequence, shaving off the first n nibbles.
    Nibbles dropLast​(int n)
    Return a prefix of this nibble sequence, dropping the last n nibbles.
    boolean equals​(Object o)  
    static Nibbles fromHexPrefix​(byte[] hexPrefixEncoded)
    Returns a Nibbles object that represent the hex-prefix encoded nibble sequence, as per appendix C of the yellowpaper.
    byte get​(int i)
    Returns the nibble at the given index.
    int hashCode()  
    byte[] hexPrefix​(boolean leaf)
    Returns the hex-prefix encoding of the nibble sequence, as per appendix C of the yellowpaper.
    int length()
    Return the number of nibbles represented by this object.
    Nibbles prefix​(int n)
    Return a prefix of this nibble sequence, keeping the first n nibbles.
    int sharedPrefix​(Nibbles o)
    Return the amount of nibbles that this object and o share as prefix.
    Nibbles suffix​(int n)
    Return a suffix of this nibble sequence, keeping the last n nibbles.
    String toString()
    Returns a string representation of this nibble sequence, as an hex string (e.g.

    Methods inherited from class java.lang.Object

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

    • EMPTY

      public static final Nibbles EMPTY
  • Constructor Details

    • Nibbles

      public Nibbles​(@Retained byte[] key)
    • Nibbles

      public Nibbles​(byte nibble)
      Construct a nibble sequence containing a single nibble.
    • Nibbles

      public Nibbles​(@Retained byte[] key, int start, int end)
  • Method Details

    • fromHexPrefix

      public static Nibbles fromHexPrefix​(@Retained byte[] hexPrefixEncoded)
      Returns a Nibbles object that represent the hex-prefix encoded nibble sequence, as per appendix C of the yellowpaper. See hexPrefix(boolean) for detail on the encoding.

      The returned object will be backed by the passed array.

    • get

      public byte get​(int i)
      Returns the nibble at the given index.
    • length

      public int length()
      Return the number of nibbles represented by this object.
    • sharedPrefix

      public int sharedPrefix​(Nibbles o)
      Return the amount of nibbles that this object and o share as prefix.
    • prefix

      public Nibbles prefix​(int n)
      Return a prefix of this nibble sequence, keeping the first n nibbles.
    • dropLast

      public Nibbles dropLast​(int n)
      Return a prefix of this nibble sequence, dropping the last n nibbles.
    • suffix

      public Nibbles suffix​(int n)
      Return a suffix of this nibble sequence, keeping the last n nibbles.
    • dropFirst

      public Nibbles dropFirst​(int n)
      Return a suffix of this nibble sequence, shaving off the first n nibbles.
    • concat

      public Nibbles concat​(Nibbles other)
    • hexPrefix

      public byte[] hexPrefix​(boolean leaf)
      Returns the hex-prefix encoding of the nibble sequence, as per appendix C of the yellowpaper.

      This encoding sets two flags in the first (highest-order) nibble: whether the number of nibble is odd (lowest-order bit), and whether the nibble sequence is the key suffix associated with a PatriciaLeafNode (indicated by the leaf parameter, stored in the second lowest-order bit).

      If the number of nibbles is odd, the second highest-order nibble is the first nibble, otherwise it is zero and the third highest-order nibble is the first nibble.

    • bytes

      public byte[] bytes()
      Returns a copy of this nibble sequence as an array of bytes. If the number of nibbles in the sequence is odd, there is an extra nibble in the byte array, which is set to 0.
    • toString

      public String toString()
      Returns a string representation of this nibble sequence, as an hex string (e.g. 0x123) containing as many characters after "0x" as there are nibbles in the sequence.
      Overrides:
      toString in class Object
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object