/ pushtx / status.js
status.js
  1  /*!
  2   * pushtx/status.js
  3   * Copyright © 2019 – Katana Cryptographic Ltd. All Rights Reserved.
  4   */
  5  
  6  
  7  import util from '../lib/util.js'
  8  import Logger from '../lib/logger.js'
  9  import db from '../lib/db/mysql-db-wrapper.js'
 10  import { createRpcClient } from '../lib/bitcoind-rpc/rpc-client.js'
 11  
 12  
 13  /**
 14   * Default values for status object
 15   */
 16  const DEFAULT_STATUS = {
 17      uptime: 0,
 18      memory: 0,
 19      bitcoind: {
 20          up: false,
 21          conn: -1,
 22          blocks: -1,
 23          version: -1,
 24          protocolversion: -1,
 25          relayfee: 0,
 26          testnet: false
 27      },
 28      push: {
 29          count: 0,
 30          amount: 0
 31      }
 32  }
 33  
 34  /**
 35   * Singleton providing information about the pushtx service
 36   */
 37  class Status {
 38  
 39      constructor() {
 40          this.startTime = Date.now()
 41          this.status = JSON.parse(JSON.stringify(DEFAULT_STATUS))
 42          this.stats = {
 43              amount: 0,
 44              count: 0
 45          }
 46          this.rpcClient = createRpcClient()
 47      }
 48  
 49      /**
 50       * Update the stats
 51       * @param {Number} amount - amount sent (in BTC)
 52       */
 53      updateStats(amount) {
 54          this.stats.count++
 55          this.stats.amount += amount
 56      }
 57  
 58      /**
 59       * Get current status
 60       * @returns {Promise} status object
 61       */
 62      async getCurrent() {
 63          const mem = process.memoryUsage()
 64          this.status.memory = util.toMb(mem.rss)
 65  
 66          this.status.uptime = Number(((Date.now() - this.startTime) / 1000).toFixed(1))
 67  
 68          this.status.push.amount = Number((this.stats.amount / 1e8).toFixed(3))
 69          this.status.push.count = this.stats.count
 70  
 71          try {
 72              await Promise.all([this._refreshNetworkInfo(), this._refreshBlockchainInfo()])
 73          } catch (error) {
 74              Logger.error(error, 'PushTx : Status.getCurrent() : Error')
 75          }
 76  
 77          return this.status
 78      }
 79  
 80      /**
 81       * Get scheduled transactions
 82       */
 83      async getScheduledTransactions() {
 84          const returnValue = {
 85              nbTxs: 0,
 86              txs: []
 87          }
 88  
 89          try {
 90              returnValue.txs = await db.getScheduledTransactions()
 91              returnValue.nbTxs = returnValue.txs.length
 92          } catch {
 93              //
 94          }
 95  
 96          return returnValue
 97      }
 98  
 99      /**
100       * Refresh network info
101       */
102      async _refreshNetworkInfo() {
103          const info = await this.rpcClient.getnetworkinfo()
104          this.status.bitcoind.conn = info.connections
105          this.status.bitcoind.version = info.version
106          this.status.bitcoind.protocolversion = info.protocolversion
107          this.status.bitcoind.relayfee = info.relayfee
108      }
109  
110      /**
111       * Refresh blockchain info
112       */
113      async _refreshBlockchainInfo() {
114          const info = await this.rpcClient.getblockchaininfo()
115          this.status.bitcoind.blocks = info.blocks
116          this.status.bitcoind.testnet = (info.chain !== 'main')
117          this.status.bitcoind.up = true
118      }
119  
120  }
121  
122  export default new Status()