Class: Bitcoin::PartialTree
- Inherits:
-
Object
- Object
- Bitcoin::PartialTree
- Defined in:
- lib/bitcoin/partial_tree.rb
Overview
A class for recovering partial from merkleblock message. For a complete Merkle tree implementation, migrate to the merkle gem.
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #find_node(value) ⇒ Object
-
#initialize(root = nil) ⇒ PartialTree
constructor
A new instance of PartialTree.
- #merkle_root ⇒ Object
Constructor Details
#initialize(root = nil) ⇒ PartialTree
Returns a new instance of PartialTree.
9 10 11 |
# File 'lib/bitcoin/partial_tree.rb', line 9 def initialize(root = nil) @root = root end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
7 8 9 |
# File 'lib/bitcoin/partial_tree.rb', line 7 def root @root end |
Class Method Details
.build(tx_count, hashes, flags) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bitcoin/partial_tree.rb', line 18 def self.build(tx_count, hashes, flags) flags = flags.each_char.map(&:to_i) root = build_initial_tree( Array.new(tx_count) { Node.new }) current_node = root hash_index = 0 flags.each do |f| current_node.flag = f if f.zero? || current_node.leaf? current_node.value = hashes[hash_index] hash_index += 1 end current_node = current_node.next_partial if hash_index == hashes.size if current_node&.leaf? current_node.value = hashes.last end break end end new(root) end |
.build_initial_tree(nodes) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bitcoin/partial_tree.rb', line 40 def self.build_initial_tree(nodes) while nodes.size != 1 nodes = nodes.each_slice(2).map { |m| parent = Node.new parent.left = m[0] parent.right = m[1] ? m[1] : m[0].dup parent } end nodes.first end |
Instance Method Details
#find_node(value) ⇒ Object
52 53 54 |
# File 'lib/bitcoin/partial_tree.rb', line 52 def find_node(value) root.find_node(value) end |
#merkle_root ⇒ Object
13 14 15 |
# File 'lib/bitcoin/partial_tree.rb', line 13 def merkle_root root.value end |