class XRBP::SHAMap::NodeID

Encapsulates node key to allow for tree traversal.

Provides branch extraction/generation logic. Since branch is between 0-15, only a nibble (4bits) are needed to store. Thus each char (8bits) can describe 2 tree branches

Constants

MASK_SIZE

Attributes

depth[R]
key[R]

Public Class Methods

masks() click to toggle source

Masks corresponding to each tree level. Used to calculate inner node hash for tree level:

inner node = lookup key & mask
# File lib/xrbp/nodestore/shamap/node_id.rb, line 23
def self.masks
  @masks ||= begin
    masks = Array.new(MASK_SIZE)

    i = 0
    selector = NodeStore.uint256
    while(i < MASK_SIZE-1)
      masks[i] = String.new(selector)
      selector[i / 2] = 0xF0.chr
      masks[i+1] = String.new(selector)
      selector[i / 2] = 0xFF.chr
      i += 2
    end
    masks[MASK_SIZE-1] = selector

    masks
  end
end
new(args={}) click to toggle source
# File lib/xrbp/nodestore/shamap/node_id.rb, line 12
def initialize(args={})
  @depth ||= args[:depth] || 0
  @key   ||= args[:key]   || NodeStore.uint256
end

Public Instance Methods

child_node_id(branch) click to toggle source

Return NodeID for specified branch under this one.

# File lib/xrbp/nodestore/shamap/node_id.rb, line 69
def child_node_id(branch)
  raise unless branch >= 0 && branch < 16
  raise unless depth < 64

  # Copy local key and assign branch number to
  # nibble in byte at local depth
  child = key.unpack("C*")
  child[depth/2] |= ((depth & 1) == 1) ? branch : (branch << 4)

  NodeID.new :depth => (depth + 1),
             :key   => child.pack("C*")
end
mask() click to toggle source

Return mask for current tree depth

# File lib/xrbp/nodestore/shamap/node_id.rb, line 43
def mask
  @mask ||= self.class.masks[depth]
end
select_branch(hash) click to toggle source

Return branch number of specified hash.

# File lib/xrbp/nodestore/shamap/node_id.rb, line 48
def select_branch(hash)
  #if RIPPLE_VERIFY_NODEOBJECT_KEYS
  raise if depth >= 64
  raise if (hash.to_bn & mask.to_bn) != key.to_bn
  #end

  # Extract hash byte at local node depth
  br = hash[depth / 2].ord

  # Reduce to relevant nibble
  if (depth & 1) == 1
    br &= 0xf
  else
    br >>= 4
  end

  raise unless (br >= 0) && (br < 16)
  br
end