class Tapyrus::MerkleTree
merkle tree
Attributes
root[RW]
Public Class Methods
build_from_leaf(txids)
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 14 def self.build_from_leaf(txids) if txids.size == 1 nodes = [Node.new(txids.first)] else nodes = txids.each_slice(2).map do |m| left = Node.new(m[0]) right = Node.new(m[1] ? m[1] : m[0]) [left, right] end.flatten end new(build_initial_tree(nodes)) end
build_initial_tree(nodes)
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 49 def self.build_initial_tree(nodes) while nodes.size != 1 nodes = nodes .each_slice(2) .map do |m| parent = Node.new parent.left = m[0] parent.right = m[1] ? m[1] : m[0].dup parent end 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/tapyrus/merkle_tree.rb, line 29 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 current_node.value = hashes.last if current_node&.leaf? break end end new(root) end
new(root = nil)
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 6 def initialize(root = nil) @root = root end
Public Instance Methods
find_node(value)
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 64 def find_node(value) root.find_node(value) end
merkle_root()
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 10 def merkle_root root.value end