class Graphdb::Model::Block

Public Class Methods

create_from_block_height(block_height) click to toggle source
# File lib/graphdb/model/block.rb, line 38
def self.create_from_block_height(block_height)
  block = new
  block.block_hash = Bitcoin2Graphdb::Bitcoin.provider.block_hash(block_height)
  hash = Bitcoin2Graphdb::Bitcoin.provider.block(block.block_hash)
  block.size = hash['size']
  block.height = hash['height']
  block.version = hash['version']
  block.merkle_root = hash['merkleroot']
  block.time = Time.at(hash['time'])
  block.nonce = hash['nonce']
  block.bits = hash['bits']
  block.difficulty = hash['difficulty']
  block.chain_work = hash['chainwork']
  block.previous_block_hash = hash['previousblockhash']
  # check previous block exist?
  begin
    Bitcoin2Graphdb::Bitcoin.provider.block(block.previous_block_hash) if block.previous_block_hash # except genesis block
  rescue OpenAssets::Provider::ApiError
    raise Bitcoin2Graphdb::Error, 'previous block not found. maybe re-org occured.'
  end
  block.next_block_hash = hash['nextblockhash']
  block.confirmations = hash['confirmations']
  block.save!
  unless block.genesis_block?
    hash['tx'].each do |txid|
      block.transactions << Graphdb::Model::Transaction.create_from_txid(txid)
    end
  end
  block.save!
  block
end

Public Instance Methods

genesis_block?() click to toggle source
# File lib/graphdb/model/block.rb, line 70
def genesis_block?
  Bitcoin.network[:genesis_hash] == block_hash
end

Private Instance Methods

chain_previous_block() click to toggle source
# File lib/graphdb/model/block.rb, line 76
def chain_previous_block
  unless self.previous_block_hash.nil?
    self.previous_block = Block.with_block_hash(self.previous_block_hash).first
    save!
  end
end