Sha256: 771ce2ade7be1e0ba216225980322ceb17054453c88c37a27e3bfcbca057e96d

Contents?: true

Size: 971 Bytes

Versions: 11

Compression:

Stored size: 971 Bytes

Contents

module Tapyrus
  class Block
    include Tapyrus::Util

    attr_accessor :header
    attr_accessor :transactions

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

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

    def hash
      header.hash
    end

    def block_hash
      header.block_hash
    end

    # check the merkle root in the block header matches merkle root calculated from tx list.
    def valid_merkle_root?
      calculate_merkle_root == header.merkle_root
    end

    # calculate merkle root from tx list.
    def calculate_merkle_root
      Tapyrus::MerkleTree.build_from_leaf(transactions.map(&:tx_hash)).merkle_root
    end

    # return this block height. block height is included in coinbase.
    # @return [Integer] block height.
    def height
      transactions.first.in.first.out_point.index
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tapyrus-0.3.5 lib/tapyrus/block.rb
tapyrus-0.3.4 lib/tapyrus/block.rb
tapyrus-0.3.3 lib/tapyrus/block.rb
tapyrus-0.3.2 lib/tapyrus/block.rb
tapyrus-0.3.1 lib/tapyrus/block.rb
tapyrus-0.3.0 lib/tapyrus/block.rb
tapyrus-0.2.13 lib/tapyrus/block.rb
tapyrus-0.2.12 lib/tapyrus/block.rb
tapyrus-0.2.10 lib/tapyrus/block.rb
tapyrus-0.2.9 lib/tapyrus/block.rb
tapyrus-0.2.8 lib/tapyrus/block.rb