Class: Bitcoin::Block
Instance Attribute Summary collapse
-
#header ⇒ Object
Returns the value of attribute header.
-
#transactions ⇒ Object
Returns the value of attribute transactions.
Class Method Summary collapse
Instance Method Summary collapse
-
#calculate_merkle_root ⇒ Object
calculate merkle root from tx list.
-
#calculate_witness_commitment ⇒ Object
calculate witness commitment from tx list.
- #hash ⇒ Object
-
#height ⇒ Object
return this block height.
-
#initialize(header, transactions = []) ⇒ Block
constructor
A new instance of Block.
-
#size ⇒ Object
calculate total size (include witness data.).
-
#stripped_size ⇒ Object
calculate base size (not include witness data.).
-
#valid_merkle_root? ⇒ Boolean
check the merkle root in the block header matches merkle root calculated from tx list.
-
#valid_witness_commitment? ⇒ Boolean
check the witness commitment in coinbase tx matches witness commitment calculated from tx list.
-
#weight ⇒ Object
calculate block weight.
Constructor Details
#initialize(header, transactions = []) ⇒ Block
Returns a new instance of Block.
7 8 9 10 |
# File 'lib/bitcoin/block.rb', line 7 def initialize(header, transactions = []) @header = header @transactions = transactions end |
Instance Attribute Details
#header ⇒ Object
Returns the value of attribute header.
4 5 6 |
# File 'lib/bitcoin/block.rb', line 4 def header @header end |
#transactions ⇒ Object
Returns the value of attribute transactions.
5 6 7 |
# File 'lib/bitcoin/block.rb', line 5 def transactions @transactions end |
Class Method Details
.parse_from_payload(payload) ⇒ Object
12 13 14 |
# File 'lib/bitcoin/block.rb', line 12 def self.parse_from_payload(payload) Bitcoin::Message::Block.parse_from_payload(payload).to_block end |
Instance Method Details
#calculate_merkle_root ⇒ Object
calculate merkle root from tx list.
43 44 45 |
# File 'lib/bitcoin/block.rb', line 43 def calculate_merkle_root Bitcoin::MerkleTree.build_from_leaf(transactions.map(&:hash)).merkle_root end |
#calculate_witness_commitment ⇒ Object
calculate witness commitment from tx list.
53 54 55 56 57 58 59 |
# File 'lib/bitcoin/block.rb', line 53 def calculate_witness_commitment witness_hashes = [COINBASE_WTXID] witness_hashes += (transactions[1..-1].map(&:witness_hash)) reserved_value = transactions[0].inputs[0].script_witness.stack.map(&:bth).join root_hash = Bitcoin::MerkleTree.build_from_leaf(witness_hashes).merkle_root Bitcoin.double_sha256([root_hash + reserved_value].pack('H*')).bth end |
#hash ⇒ Object
16 17 18 |
# File 'lib/bitcoin/block.rb', line 16 def hash header.hash end |
#height ⇒ Object
return this block height. block height is included in coinbase. if block version under 1, height does not include in coinbase, so return nil.
63 64 65 66 67 68 69 70 |
# File 'lib/bitcoin/block.rb', line 63 def height return nil if header.version < 2 coinbase_tx = transactions[0] return nil unless coinbase_tx.coinbase_tx? buf = StringIO.new(coinbase_tx.inputs[0].script_sig.to_payload) len = Bitcoin.unpack_var_int_from_io(buf) buf.read(len).reverse.bth.to_i(16) end |
#size ⇒ Object
calculate total size (include witness data.)
26 27 28 29 |
# File 'lib/bitcoin/block.rb', line 26 def size 80 + Bitcoin.pack_var_int(transactions.size).bytesize + transactions.inject(0){|sum, tx| sum + (tx.witness? ? tx.serialize_witness_format.bytesize : tx.serialize_old_format.bytesize)} end |
#stripped_size ⇒ Object
calculate base size (not include witness data.)
32 33 34 35 |
# File 'lib/bitcoin/block.rb', line 32 def stripped_size 80 + Bitcoin.pack_var_int(transactions.size).bytesize + transactions.inject(0){|sum, tx| sum + tx.serialize_old_format.bytesize} end |
#valid_merkle_root? ⇒ Boolean
check the merkle root in the block header matches merkle root calculated from tx list.
38 39 40 |
# File 'lib/bitcoin/block.rb', line 38 def valid_merkle_root? calculate_merkle_root == header.merkle_root end |
#valid_witness_commitment? ⇒ Boolean
check the witness commitment in coinbase tx matches witness commitment calculated from tx list.
48 49 50 |
# File 'lib/bitcoin/block.rb', line 48 def valid_witness_commitment? transactions[0].witness_commitment == calculate_witness_commitment end |
#weight ⇒ Object
calculate block weight
21 22 23 |
# File 'lib/bitcoin/block.rb', line 21 def weight stripped_size * (WITNESS_SCALE_FACTOR - 1) + size end |