Class: Bitcoin::MerkleTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/merkle_tree.rb

Overview

node of merkle tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Node

Returns a new instance of Node.



77
78
79
# File 'lib/bitcoin/merkle_tree.rb', line 77

def initialize(value = nil)
  @value = value
end

Instance Attribute Details

#flagObject

Returns the value of attribute flag.



71
72
73
# File 'lib/bitcoin/merkle_tree.rb', line 71

def flag
  @flag
end

#leftObject

Returns the value of attribute left.



74
75
76
# File 'lib/bitcoin/merkle_tree.rb', line 74

def left
  @left
end

#parentObject

Returns the value of attribute parent.



73
74
75
# File 'lib/bitcoin/merkle_tree.rb', line 73

def parent
  @parent
end

#rightObject

Returns the value of attribute right.



75
76
77
# File 'lib/bitcoin/merkle_tree.rb', line 75

def right
  @right
end

#valueObject

Returns the value of attribute value.



72
73
74
# File 'lib/bitcoin/merkle_tree.rb', line 72

def value
  @value
end

Instance Method Details

#depthObject

calculate the depth of this node in the tree.



118
119
120
121
122
123
124
125
126
# 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) ⇒ Object

Returns node which has same value as target. nil if this node and any children don’t have same value.

Parameters:

  • target

    value to be found

Returns:

  • node which has same value as target. nil if this node and any children don’t have same value.



130
131
132
133
134
# 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

#indexObject



136
137
138
139
140
141
142
143
144
145
146
# 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?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/bitcoin/merkle_tree.rb', line 101

def leaf?
  right.nil? && left.nil?
end

#next_partialObject



109
110
111
112
113
114
115
# 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?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/bitcoin/merkle_tree.rb', line 105

def partial?
  !flag.nil?
end

#root?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/bitcoin/merkle_tree.rb', line 97

def root?
  parent.nil?
end