Class: Bitcoin::MerkleTree::Node
Overview
node of merkle tree
Instance Attribute Summary collapse
-
#flag ⇒ Object
Returns the value of attribute flag.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#left ⇒ Object
Returns the value of attribute left.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#right ⇒ Object
Returns the value of attribute right.
Instance Method Summary collapse
-
#depth ⇒ Object
calculate the depth of this node in the tree.
-
#initialize(hash = nil) ⇒ Node
constructor
A new instance of Node.
- #leaf? ⇒ Boolean
- #next_partial ⇒ Object
- #partial? ⇒ Boolean
- #root? ⇒ Boolean
Constructor Details
#initialize(hash = nil) ⇒ Node
Returns a new instance of Node.
64 65 66 |
# File 'lib/bitcoin/merkle_tree.rb', line 64 def initialize(hash = nil) @hash = hash end |
Instance Attribute Details
#flag ⇒ Object
Returns the value of attribute flag.
58 59 60 |
# File 'lib/bitcoin/merkle_tree.rb', line 58 def flag @flag end |
#hash ⇒ Object
Returns the value of attribute hash.
59 60 61 |
# File 'lib/bitcoin/merkle_tree.rb', line 59 def hash @hash end |
#left ⇒ Object
Returns the value of attribute left.
61 62 63 |
# File 'lib/bitcoin/merkle_tree.rb', line 61 def left @left end |
#parent ⇒ Object
Returns the value of attribute parent.
60 61 62 |
# File 'lib/bitcoin/merkle_tree.rb', line 60 def parent @parent end |
#right ⇒ Object
Returns the value of attribute right.
62 63 64 |
# File 'lib/bitcoin/merkle_tree.rb', line 62 def right @right end |
Instance Method Details
#depth ⇒ Object
calculate the depth of this node in the tree.
105 106 107 108 109 110 111 112 113 |
# File 'lib/bitcoin/merkle_tree.rb', line 105 def depth d = 0 current_node = self until current_node.root? do current_node = current_node.parent d += 1 end d end |
#leaf? ⇒ Boolean
88 89 90 |
# File 'lib/bitcoin/merkle_tree.rb', line 88 def leaf? right.nil? && left.nil? end |
#next_partial ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/bitcoin/merkle_tree.rb', line 96 def next_partial return nil if root? && (flag.zero? || (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
92 93 94 |
# File 'lib/bitcoin/merkle_tree.rb', line 92 def partial? !flag.nil? end |
#root? ⇒ Boolean
84 85 86 |
# File 'lib/bitcoin/merkle_tree.rb', line 84 def root? parent.nil? end |