Class: Bitcoin2Graphdb::Migration

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Migration

Returns a new instance of Migration.



5
6
7
8
9
10
11
12
13
# File 'lib/bitcoin2graphdb/migration.rb', line 5

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: {request: {timeout: 600, open_timeout: 5}}}
  Bitcoin2Graphdb::Bitcoin.provider = Bitcoin2Graphdb::Bitcoin::BlockchainProvider.new(config[:bitcoin])
  Neo4j::Session.open(:server_db, config[:neo4j][:server], neo4j_config)
end

Instance Method Details

#import_tx(txid) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bitcoin2graphdb/migration.rb', line 57

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bitcoin2graphdb/migration.rb', line 40

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bitcoin2graphdb/migration.rb', line 74

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

#runObject



15
16
17
18
19
# File 'lib/bitcoin2graphdb/migration.rb', line 15

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

#run_with_height(block_height) ⇒ Object



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

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 10 min."
        sleep 600
      else
        tx.failure
        raise e
      end
    end
  end
  puts "end migration for block height #{block_height}. #{Time.now}"
end