Class: Bitcoin2Graphdb::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin2graphdb/migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Migration

Returns a new instance of Migration.



7
8
9
10
11
12
13
14
15
16
# File 'lib/bitcoin2graphdb/migration.rb', line 7

def initialize(config)
  Graphdb.configure do |c|
    c.neo4j_server = config[:neo4j][:server]
    c.extensions = config[:extensions] unless config[:extensions].nil?
  end
  neo4j_config = {basic_auth: config[:neo4j][:basic_auth], initialize: neo4j_timeout_ops(config)}
  Bitcoin2Graphdb::Bitcoin.provider = Bitcoin2Graphdb::Bitcoin::BlockchainProvider.new(config[:bitcoin])
  Neo4j::Session.open(:server_db, config[:neo4j][:server], neo4j_config)
  @sleep_interval = config[:bitcoin][:sleep_interval].nil? ? 600 : config[:bitcoin][:sleep_interval].to_i
end

Instance Attribute Details

#sleep_interval ⇒ Object (readonly)

Returns the value of attribute sleep_interval.



5
6
7
# File 'lib/bitcoin2graphdb/migration.rb', line 5

def sleep_interval
  @sleep_interval
end

Instance Method Details

#import_tx(txid) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bitcoin2graphdb/migration.rb', line 83

def import_tx(txid)
  Neo4j::Transaction.run do |tx|
    begin
      tx = Graphdb::Model::Transaction.with_txid(txid).first
      if tx.nil?
        Graphdb::Model::Transaction.create_from_txid(txid)
        puts "import #{txid} tx."
      else
        puts "txid #{txid} is already exist."
      end
    rescue => e
      puts e.message
      tx.failure
    end
  end
end

#remove_block(block_height) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bitcoin2graphdb/migration.rb', line 66

def remove_block(block_height)
  Neo4j::Transaction.run do |tx|
    begin
      block = Graphdb::Model::Block.with_height(block_height).first
      if block.nil?
        puts "block height #{block_height} does not exist."
      else
        block.destroy
        puts "block height #{block_height} is deleted."
      end
    rescue => e
      puts e.message
      tx.failure
    end
  end
end

#remove_tx(txid) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bitcoin2graphdb/migration.rb', line 100

def remove_tx(txid)
  Neo4j::Transaction.run do |tx|
    begin
      tx = Graphdb::Model::Transaction.with_txid(txid).first
      if tx.nil?
        puts "txid #{txid} does not exist."
      else
        tx.destroy
        puts "tixd #{txid} is deleted."
      end
    rescue => e
      puts e.message
      tx.failure
    end
  end
end

#repair_assign_txs(block_height) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bitcoin2graphdb/migration.rb', line 43

def repair_assign_txs(block_height)
  Neo4j::Transaction.run do |tx|
    begin
      block = Graphdb::Model::Block.with_height(block_height).first
      if block
        hash = Bitcoin2Graphdb::Bitcoin.provider.block(block.block_hash)
        tx_list = block.transactions.to_a
        hash['tx'].each do |txid|
          target = tx_list.find{|t|t.txid == txid}
          if target.nil?
            block.transactions << Graphdb::Model::Transaction.with_txid(txid).first
            puts "repair tx = #{txid}"
          end
        end
        block.save!
      end
    rescue => e
      tx.failure
      raise e
    end
  end
end

#run ⇒ Object



18
19
20
21
22
# File 'lib/bitcoin2graphdb/migration.rb', line 18

def run
  loop {
    run_with_height(current_block_height + 1)
  }
end

#run_with_height(block_height) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bitcoin2graphdb/migration.rb', line 24

def run_with_height(block_height)
  puts "start migration for block height = #{block_height}. #{Time.now}"
  Neo4j::Transaction.run do |tx|
    begin
      Graphdb::Model::Block.create_from_block_height(block_height)
      @block_height = block_height
    rescue OpenAssets::Provider::ApiError => e
      if e.message == '{"code"=>-8, "message"=>"Block height out of range"}'
        puts "Block height out of range. sleep #{@sleep_interval} seconds."
        sleep @sleep_interval
      else
        tx.failure
        raise e
      end
    end
  end
  puts "end migration for block height #{block_height}. #{Time.now}"
end