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
17
18
# 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
  Bitcoin2Graphdb::Bitcoin.provider = Bitcoin2Graphdb::Bitcoin::BlockchainProvider.new(config[:bitcoin])
  neo4j_adaptor = create_neo4j_adaptor(config)
  Neo4j::ActiveBase.on_establish_session { Neo4j::Core::CypherSession.new(neo4j_adaptor) }
  @sleep_interval = config[:bitcoin][:sleep_interval].nil? ? 600 : config[:bitcoin][:sleep_interval].to_i

  Graphdb::Model.constants.each {|const_name| Graphdb::Model.const_get(const_name)}
end

Instance Attribute Details

#sleep_intervalObject (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



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

def import_tx(txid)
  Neo4j::ActiveBase.run_transaction 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



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

def remove_block(block_height)
  Neo4j::ActiveBase.run_transaction 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



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

def remove_tx(txid)
  Neo4j::ActiveBase.run_transaction 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



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

def repair_assign_txs(block_height)
  Neo4j::ActiveBase.run_transaction 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

#runObject



20
21
22
23
24
# File 'lib/bitcoin2graphdb/migration.rb', line 20

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

#run_with_height(block_height) ⇒ Object



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

def run_with_height(block_height)
  puts "start migration for block height = #{block_height}. #{Time.now}"
  Neo4j::ActiveBase.run_transaction do |tx|
    begin
      Graphdb::Model::Block.create_from_block_height(block_height)
      @block_height = block_height
    rescue OpenAssets::Provider::ApiError, Bitcoin2Graphdb::Error => 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

#search_invalid_oa_txObject



119
120
121
122
123
124
125
# File 'lib/bitcoin2graphdb/migration.rb', line 119

def search_invalid_oa_tx
  puts "start get_invalid_oa_tx"
  Graphdb::Model::AssetId.all.each{|asset_id|
    asset_id.issuance_txs.each{|issuance_tx| asset_outputs(issuance_tx.txid)}
  }
  puts "get_invalid_oa_tx end."
end