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(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

#flagObject

Returns the value of attribute flag.



58
59
60
# File 'lib/bitcoin/merkle_tree.rb', line 58

def flag
  @flag
end

#hashObject

Returns the value of attribute hash.



59
60
61
# File 'lib/bitcoin/merkle_tree.rb', line 59

def hash
  @hash
end

#leftObject

Returns the value of attribute left.



61
62
63
# File 'lib/bitcoin/merkle_tree.rb', line 61

def left
  @left
end

#parentObject

Returns the value of attribute parent.



60
61
62
# File 'lib/bitcoin/merkle_tree.rb', line 60

def parent
  @parent
end

#rightObject

Returns the value of attribute right.



62
63
64
# File 'lib/bitcoin/merkle_tree.rb', line 62

def right
  @right
end

Instance Method Details

#depthObject

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/bitcoin/merkle_tree.rb', line 88

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

#next_partialObject



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

Returns:

  • (Boolean)


92
93
94
# File 'lib/bitcoin/merkle_tree.rb', line 92

def partial?
  !flag.nil?
end

#root?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/bitcoin/merkle_tree.rb', line 84

def root?
  parent.nil?
end