Class: Bitcoin::Storage::Backends::Activerecord

Inherits:
Base
  • Object
show all
Includes:
ActiverecordStore
Defined in:
lib/bitcoin/storage/backends/activerecord.rb

Instance Method Summary (collapse)

Methods inherited from Base

#get_block, #get_locator, #inject_genesis, #log

Constructor Details

- (Activerecord) initialize(config)

A new instance of Activerecord



18
19
20
21
22
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 18

def initialize config
  @config = config
  connect
  super @config
end

Instance Method Details

- (Object) connect



24
25
26
27
28
29
30
31
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 24

def connect
  # TODO: load schema if not already there
  ActiveRecord::Base.establish_connection @config
  if defined?(Log4r)
    ActiveRecord::Base.logger = Bitcoin::Logger.create(:database)
    ActiveRecord::Base.logger.level = 2
  end
end

- (Object) get_balance

TODO



50
51
52
53
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 50

def get_balance
 s = "41" + pubkey_hash + "ac"
 Output.where("script = decode('#{s}', 'hex')")
end

- (Object) get_block_by_depth(depth)



85
86
87
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 85

def get_block_by_depth(depth)
  Block.where(:depth => depth).first.to_protocol rescue nil
end

- (Object) get_block_by_hash(blk_hash)



81
82
83
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 81

def get_block_by_hash(blk_hash)
  Block.where("block_hash = decode(?, 'hex')", blk_hash).first.to_protocol rescue nil
end

- (Object) get_block_depth(blk_hash)



89
90
91
92
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 89

def get_block_depth(blk_hash)
  block = Block.where("block_hash = decode(?, 'hex')", blk_hash).first
  block.depth
end

- (Object) get_depth



39
40
41
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 39

def get_depth
  Block.order("depth DESC").limit(1).first.depth rescue -1
end

- (Object) get_head



43
44
45
46
47
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 43

def get_head
  Bitcoin::hth(Block.order("depth DESC").limit(1).first.block_hash)
rescue
  Bitcoin::network[:genesis_hash]
end

- (Object) get_tx(tx_hash)



118
119
120
121
122
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 118

def get_tx(tx_hash)
  tx = Transaction.where("transaction_hash = decode('#{tx_hash}', 'hex')").first
  return nil unless tx
  tx.to_protocol
end

- (Object) reset



33
34
35
36
37
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 33

def reset
  [:blocks, :transactions_parents, :transactions, :inputs, :outputs, :chains].each do |t|
    ActiveRecord::Base.connection.query("DELETE from #{t};")
  end
end

- (Object) store_block(blk)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 55

def store_block(blk)
  return nil  unless blk

  if block = get_block_by_hash(blk.hash)
    log.debug { "Block #{blk.hash} already stored" }
  end

  block = Block.from_protocol(blk)

  return nil  unless block

  begin
    if block.save
      log.info { "NEW HEAD: #{blk.hash} (#{blk.payload.size} bytes) - DEPTH: #{block.depth}" }
      return block.depth
    end
  rescue
    log.error { "ERROR SAVING BLOCK: #{$!.inspect}" }
    p $@.first
    puts *$@
    binding.pry
    exit
  end

end

- (Object) store_tx(tx)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bitcoin/storage/backends/activerecord.rb', line 94

def store_tx(tx)
  return nil  unless tx
  
  if transaction = get_tx(tx.hash)
    log.debug { "Tx #{tx.hash} already stored"}
    return false
  end

  transaction = Transaction.from_protocol(tx)

  begin
    if transaction.save
      log.info { "Tx #{tx.hash} saved" }
      return true
    else
      log.warn { "Error saving tx #{tx.hash}" }
      return false
    end
  rescue
    log.error { "Exception trying to save tx: #{$!.message}" }
    return false
  end
end