module Bitcoin module Protocol class Block # block hash attr_accessor :hash # previous block hash attr_accessor :prev_block # transactions (Array of Tx) attr_accessor :tx # merkle root attr_accessor :mrkl_root # block generation time attr_accessor :time # difficulty target bits attr_accessor :bits # nonce (number counted when searching for block hash matching target) attr_accessor :nonce # version (usually 1) attr_accessor :ver # raw protocol payload attr_accessor :payload alias :transactions :tx # compare to another block def ==(other) @hash == other.hash end # create block from raw binary +data+ def initialize(data) @tx = [] parse_data(data) if data end # parse raw binary data def parse_data(data) @ver, @prev_block, @mrkl_root, @time, @bits, @nonce, payload = data.unpack("Ia32a32IIIa*") recalc_block_hash tx_size, payload = Protocol.unpack_var_int(payload) (0...tx_size).each{ break if payload == true t = Tx.new(nil) payload = t.parse_data(payload) @tx << t } @payload = to_payload payload end # recalculate the block hash def recalc_block_hash @hash = Bitcoin.block_hash(hth(@prev_block), hth(@mrkl_root), @time, @bits, @nonce, @ver) end # get the block header info # [, , ,