class Bitcoin::MerkleTree
merkle tree
Attributes
root[RW]
Public Class Methods
build_from_leaf(txids)
click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 16 def self.build_from_leaf(txids) if txids.size == 1 nodes = [Node.new(txids.first)] else nodes = txids.each_slice(2).map{ |m| left = Node.new(m[0]) right = Node.new(m[1] ? m[1] : m[0]) [left, right] }.flatten end new(build_initial_tree(nodes)) end
build_initial_tree(nodes)
click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 52 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
build_partial(tx_count, hashes, flags)
click to toggle source
bitcoin.org/en/developer-reference#creating-a-merkleblock-message
# File lib/bitcoin/merkle_tree.rb, line 30 def self.build_partial(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
new(root = nil)
click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 8 def initialize(root = nil) @root = root end
Public Instance Methods
find_node(value)
click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 64 def find_node(value) root.find_node(value) end
merkle_root()
click to toggle source
# File lib/bitcoin/merkle_tree.rb, line 12 def merkle_root root.value end