Class Nibbles
public final class Nibbles extends Object
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
-
Constructor Summary
-
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 firstn
nibbles.Nibbles
dropLast(int n)
Return a prefix of this nibble sequence, dropping the lastn
nibbles.boolean
equals(Object o)
static Nibbles
fromHexPrefix(byte[] hexPrefixEncoded)
Returns aNibbles
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 firstn
nibbles.int
sharedPrefix(Nibbles o)
Return the amount of nibbles that this object ando
share as prefix.Nibbles
suffix(int n)
Return a suffix of this nibble sequence, keeping the lastn
nibbles.String
toString()
Returns a string representation of this nibble sequence, as an hex string (e.g.
-
Field Details
-
EMPTY
-
-
Constructor Details
-
Nibbles
-
Nibbles
public Nibbles(byte nibble)Construct a nibble sequence containing a single nibble. -
Nibbles
-
-
Method Details
-
fromHexPrefix
Returns aNibbles
object that represent the hex-prefix encoded nibble sequence, as per appendix C of the yellowpaper. SeehexPrefix(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. -
prefix
Return a prefix of this nibble sequence, keeping the firstn
nibbles. -
dropLast
Return a prefix of this nibble sequence, dropping the lastn
nibbles. -
suffix
Return a suffix of this nibble sequence, keeping the lastn
nibbles. -
dropFirst
Return a suffix of this nibble sequence, shaving off the firstn
nibbles. -
concat
-
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 theleaf
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
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. -
equals
-
hashCode
public int hashCode()
-