lib/bitcoin/protocol/aux_pow.rb in bitcoin-ruby-0.0.18 vs lib/bitcoin/protocol/aux_pow.rb in bitcoin-ruby-0.0.19

- old
+ new

@@ -1,10 +1,9 @@ # encoding: ascii-8bit module Bitcoin module Protocol - # Auxiliary Proof-of-Work for merge-mined blockchains # See https://en.bitcoin.it/wiki/Merged_mining_specification. # # The AuxPow contains all data needed to verify that the child # block was included in the parents coinbase transaction, and @@ -18,11 +17,10 @@ # to the merkle root contained in the +coinbase_tx+. (So there can be # more than one merge-mined chain) # # TODO: decode merged-mining data from +coinbase_tx+ class AuxPow - # Coinbase transaction of the parent block, linking to the child block attr_accessor :coinbase_tx # Hash of the parent block header attr_accessor :block_hash @@ -41,11 +39,11 @@ # Parent block header attr_accessor :parent_block def initialize(data) - parse_data (data) if data + parse_data data if data end def parse_data(data) buf = StringIO.new(data) parse_data_from_io(buf) @@ -56,46 +54,47 @@ @coinbase_tx = P::Tx.new(nil) @coinbase_tx.parse_data_from_io(data) @block_hash = data.read(32) coinbase_branch_count = P.unpack_var_int_from_io(data) + @coinbase_branch = [] - coinbase_branch_count.times{ + coinbase_branch_count.times do break if data.eof? @coinbase_branch << data.read(32).reverse.hth - } - @coinbase_index = data.read(4).unpack("I")[0] + end + @coinbase_index = data.read(4).unpack('I')[0] + @chain_branch = [] chain_branch_count = P.unpack_var_int_from_io(data) - chain_branch_count.times{ + chain_branch_count.times do break if data.eof? @chain_branch << data.read(32).reverse.hth - } + end - @chain_index = data.read(4).unpack("I")[0] + @chain_index = data.read(4).unpack('I')[0] block = data.read(80) @parent_block = P::Block.new(block) data end - def to_payload payload = @coinbase_tx.to_payload payload << @block_hash payload << P.pack_var_int(@coinbase_branch.count) payload << @coinbase_branch.map(&:htb).map(&:reverse).join - payload << [@coinbase_index].pack("I") + payload << [@coinbase_index].pack('I') payload << P.pack_var_int(@chain_branch.count) payload << @chain_branch.map(&:htb).map(&:reverse).join - payload << [@chain_index].pack("I") + payload << [@chain_index].pack('I') payload << @parent_block.to_payload payload end - def self.from_hash h + def self.from_hash(h) aux_pow = new(nil) aux_pow.instance_eval do @coinbase_tx = P::Tx.from_hash(h['coinbase_tx']) @block_hash = h['block_hash'].htb @coinbase_branch = h['coinbase_branch'] @@ -114,10 +113,8 @@ 'coinbase_index' => @coinbase_index, 'chain_branch' => @chain_branch, 'chain_index' => @chain_index, 'parent_block' => @parent_block.to_hash } end - end - end end