Class: Graphdb::Model::Block

Inherits:
ActiveNodeBase show all
Defined in:
lib/graphdb/model/block.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveNodeBase

remove_module

Class Method Details

.create_from_block_height(block_height) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

Instance Method Details

#genesis_block?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/graphdb/model/block.rb', line 70

def genesis_block?
  Bitcoin.network[:genesis_hash] == block_hash
end