Class: Tapyrus::Block

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/tapyrus/block.rb

Constant Summary

Constants included from Util

Util::DIGEST_NAME_SHA256

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#byte_to_bit, #calc_checksum, #decode_base58_address, #double_sha256, #encode_base58_address, #hash160, #hmac_sha256, #pack_boolean, #pack_var_int, #pack_var_string, #padding_zero, #sha256, #unpack_boolean, #unpack_var_int, #unpack_var_int_from_io, #unpack_var_string, #valid_address?

Constructor Details

#initialize(header, transactions = []) ⇒ Block

Returns a new instance of Block.



8
9
10
11
# File 'lib/tapyrus/block.rb', line 8

def initialize(header, transactions = [])
  @header = header
  @transactions = transactions
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



5
6
7
# File 'lib/tapyrus/block.rb', line 5

def header
  @header
end

#transactionsObject

Returns the value of attribute transactions.



6
7
8
# File 'lib/tapyrus/block.rb', line 6

def transactions
  @transactions
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



13
14
15
# File 'lib/tapyrus/block.rb', line 13

def self.parse_from_payload(payload)
  Tapyrus::Message::Block.parse_from_payload(payload).to_block
end

Instance Method Details

#block_hashObject



21
22
23
# File 'lib/tapyrus/block.rb', line 21

def block_hash
  header.block_hash
end

#calculate_merkle_rootObject

calculate merkle root from tx list.



31
32
33
# File 'lib/tapyrus/block.rb', line 31

def calculate_merkle_root
  Tapyrus::MerkleTree.build_from_leaf(transactions.map(&:tx_hash)).merkle_root
end

#hashObject



17
18
19
# File 'lib/tapyrus/block.rb', line 17

def hash
  header.hash
end

#heightInteger

return this block height. block height is included in coinbase.

Returns:



37
38
39
# File 'lib/tapyrus/block.rb', line 37

def height
  transactions.first.in.first.out_point.index
end

#valid_merkle_root?Boolean

check the merkle root in the block header matches merkle root calculated from tx list.

Returns:

  • (Boolean)


26
27
28
# File 'lib/tapyrus/block.rb', line 26

def valid_merkle_root?
  calculate_merkle_root == header.merkle_root
end