Top Level Namespace

Defined Under Namespace

Modules: TxCatcher Classes: BitcoinRPC, Hash

Constant Summary collapse

LOGGER =
TxCatcher::Logger.new(log_file: "txcatcher_monitor.log", error_file: "txcatcher_monitor_errors.log", error_log_delimiter: "")

Constants included from TxCatcher::Initializer

TxCatcher::Initializer::GEM_ROOT, TxCatcher::Initializer::MIGRATIONS_ROOT

Instance Method Summary collapse

Methods included from TxCatcher::Initializer

#add_route, #connect_to_db, #connect_to_rpc_node, #create_config_files, #initialize_sentry, #migrations_pending?, #prepare, #read_config_file, #run_migrations, #set_goliath_args

Instance Method Details

#fetch_txcatcher_for_addr(addr) ⇒ Object



131
132
133
134
135
136
137
138
# File 'bin/txcatcher-monitor', line 131

def fetch_txcatcher_for_addr(addr)
  response = Faraday.get do |req|
    req.url TxCatcher::Config["monitor"]["txcatcher_url"] + "/addr/#{addr}/utxo"
    req.options.timeout = 5
    req.options.open_timeout = 5
  end
  JSON.parse(response.body)
end

#fetch_txcatcher_response(addr, sleep_time: 3) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'bin/txcatcher-monitor', line 121

def fetch_txcatcher_response(addr, sleep_time: 3)
  response = {}
  3.times do
    sleep sleep_time # let's wait, perhaps txcatcher hasn't caught up
    response = fetch_txcatcher_for_addr(addr)
    break unless response.empty?
  end
  return response
end

#latest_output_addr(i = 0) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'bin/txcatcher-monitor', line 79

def latest_output_addr(i=0)

  txs      = nil
  attempts = 0
  until txs || attempts > 10
    attempts += 1
    begin
      response = Faraday.get do |req|
        req.url TxCatcher::Config["monitor"]["blockchain_source_url"]
        req.options.timeout = 5
        req.options.open_timeout = 5
      end
    rescue StandardError => e
      LOGGER.report(e, :error, data: { attempt: attempts })
      if attempts >= 10
        send_alert(
          "TxCatcher monitor error",
          "Tried fetching new txs from #{TxCatcher::Config["monitor"]["blockchain_source_url"]}, got the following error\n\n" +
          "#{e.to_s}\n\n#{e.backtrace.join("\n")}"
        )
        return nil
      else
        sleep 15
      end
    end
    txs = JSON.parse(response.body) if response
  end

  txs.each do |tx|
    if tx["outputs"] && tx["outputs"][i] && tx["outputs"][i]["addresses"]
      tx["outputs"][i]["addresses"].each do |addr|
        # Blockcypher returns old Litecoin P2SH addresses which start with 3, which
        # txcatcher doesn't support. Let's just ignore them.
        if addr[0] != "3" || !TxCatcher::Config["monitor"]["ignore_3_litecoin_addr"]
          return addr
        end
      end
    end
  end

end

#send_alert(subject, text) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'bin/txcatcher-monitor', line 50

def send_alert(subject, text)

  return unless TxCatcher::Config["monitor"]["alert_mail"]

  # Do not send emails more often than once in 1 hour
  email_time_file = TxCatcher::Config.config_dir + "/txcatcher_monitor_last_email_time.txt"
  t = Time.parse(File.read(email_time_file)) if File.exists?(email_time_file)
  return if t && t > Time.now - 3600

  ses_config = TxCatcher::Config["monitor"]["aws_ses"]
  ses = Aws::SES::Client.new(
    region: ses_config["region"],
    access_key_id: ses_config["access_key"],
    secret_access_key: ses_config["secret_access_key"]
  )

  resp = ses.send_email({
    destination: { to_addresses: TxCatcher::Config["monitor"]["alert_mail"]["to"] },
    source: TxCatcher::Config["monitor"]["alert_mail"]["from"],
    message: {
      body: { text: { charset: "utf-8", data: text }},
      subject: { charset: "utf-8", data: subject   }
    }
  })

  File.open(email_time_file, 'w') { |f| f.write Time.now.to_s }

end