class Bitcoin::MerkleTree::Node

node of merkle tree

Attributes

flag[RW]
left[RW]
parent[RW]
right[RW]
value[RW]

Public Class Methods

new(value = nil) click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 77
def initialize(value = nil)
  @value = value
end

Public Instance Methods

depth() click to toggle source

calculate the depth of this node in the tree.

# File lib/bitcoin/merkle_tree.rb, line 118
def depth
  d = 0
  current_node = self
  until current_node.root? do
    current_node = current_node.parent
    d += 1
  end
  d
end
find_node(target) click to toggle source

@param target value to be found @return node which has same value as target. nil if this node and any children don't have same value.

# File lib/bitcoin/merkle_tree.rb, line 130
def find_node(target)
  return self if value == target
  return nil if flag && flag.zero?
  return left&.find_node(target) || right&.find_node(target)
end
index() click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 136
def index
  i = 0
  d = 1
  current_node = self
  until current_node.root? do
    i += d if current_node.parent.right == current_node
    current_node = current_node.parent
    d *= 2
  end
  i
end
leaf?() click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 101
def leaf?
  right.nil? && left.nil?
end
left=(node) click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 81
def left=(node)
  node.parent = self
  @left = node
end
next_partial() click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 109
def next_partial
  return nil if root? && (flag.zero? || (left.nil? && right.nil?) || (left.partial? && right.partial?))
  return parent.next_partial if flag.zero? || leaf?
  return left unless left.partial?
  self.right = left.dup unless right
  right.partial? ? parent.next_partial : right
end
partial?() click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 105
def partial?
  !flag.nil?
end
right=(node) click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 86
def right=(node)
  node.parent = self
  @right = node
end
root?() click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 97
def root?
  parent.nil?
end