lib/bitcoin/protocol/block.rb in bitcoin-ruby-0.0.5 vs lib/bitcoin/protocol/block.rb in bitcoin-ruby-0.0.6
- old
+ new
@@ -71,11 +71,11 @@
def parse_data_from_io(buf, header_only=false)
buf = buf.is_a?(String) ? StringIO.new(buf) : buf
@ver, @prev_block, @mrkl_root, @time, @bits, @nonce = buf.read(80).unpack("Va32a32VVV")
recalc_block_hash
- if (@ver & BLOCK_VERSION_AUXPOW) > 0
+ if Bitcoin.network[:project] == :namecoin && (@ver & BLOCK_VERSION_AUXPOW) > 0
@aux_pow = AuxPow.new(nil)
@aux_pow.parse_data_from_io(buf)
end
return buf if buf.eof?
@@ -97,10 +97,14 @@
# recalculate the block hash
def recalc_block_hash
@hash = Bitcoin.block_hash(@prev_block.reverse_hth, @mrkl_root.reverse_hth, @time, @bits, @nonce, @ver)
end
+ def recalc_block_scrypt_hash
+ @scrypt_hash = Bitcoin.block_scrypt_hash(@prev_block.reverse_hth, @mrkl_root.reverse_hth, @time, @bits, @nonce, @ver)
+ end
+
def recalc_mrkl_root
@mrkl_root = Bitcoin.hash_mrkl_tree( @tx.map(&:hash) ).last.htb_reverse
end
# verify mrkl tree
@@ -214,23 +218,31 @@
h = to_hash
%w[tx mrkl_tree].each{|k| h.delete(k) }
JSON.pretty_generate( h, options )
end
+ # block header binary output
+ def block_header
+ [@ver, @prev_block, @mrkl_root, @time, @bits, @nonce, Protocol.pack_var_int(0)].pack("Va32a32VVVa*")
+ end
+
# read binary block from a file
def self.from_file(path); new( Bitcoin::Protocol.read_binary_file(path) ); end
# read json block from a file
def self.from_json_file(path); from_json( Bitcoin::Protocol.read_binary_file(path) ); end
+ # Get a Bitcoin::Validation object to validate this block. It needs a +store+
+ # to validate against, and optionally takes the +prev_block+ for optimization.
def validator(store, prev_block = nil)
@validator ||= Bitcoin::Validation::Block.new(self, store, prev_block)
end
# get the (statistical) amount of work that was needed to generate this block.
def block_work
- target = Bitcoin.decode_compact_bits(@bits)
- (2**256) / (target.to_i(16) + 1)
+ target = Bitcoin.decode_compact_bits(@bits).to_i(16)
+ return 0 if target <= 0
+ (2**256) / (target + 1)
end
end
end