/ tracker / tracker.js
tracker.js
 1  /*!
 2   * tracker/tracker.js
 3   * Copyright © 2019 – Katana Cryptographic Ltd. All Rights Reserved.
 4   */
 5  
 6  
 7  import zmq from 'zeromq/v5-compat.js'
 8  
 9  import network from '../lib/bitcoin/network.js'
10  import keysFile from '../keys/index.js'
11  import BlockchainProcessor from './blockchain-processor.js'
12  import MempoolProcessor from './mempool-processor.js'
13  import util from '../lib/util.js'
14  
15  const keys = keysFile[network.key]
16  
17  /**
18   * A class implementing a process tracking the blockchain
19   */
20  class Tracker {
21  
22      /**
23       * Constructor
24       */
25      constructor() {
26          // Notification socket for client events
27          this.notifSock = zmq.socket('pub')
28          this.notifSock.bind(`tcp://127.0.0.1:${keys.ports.tracker}`, () => {
29              // Initialize the blockchain processor
30              // and the mempool buffer
31              this.initialized = true
32              this.blockchainProcessor = new BlockchainProcessor(this.notifSock)
33              this.mempoolProcessor = new MempoolProcessor(this.notifSock)
34          })
35      }
36  
37      /**
38       * Start the tracker
39       * @returns {Promise<void>}
40       */
41      async start() {
42          if (!this.initialized) {
43              await util.delay(1000)
44  
45              return this.start()
46          }
47  
48          await this.blockchainProcessor.start()
49          await this.mempoolProcessor.start()
50      }
51  
52      /**
53       * Stop the tracker
54       */
55      async stop() {
56          clearTimeout(this.startupTimeout)
57          await this.blockchainProcessor.stop()
58          await this.mempoolProcessor.stop()
59      }
60  
61  }
62  
63  export default Tracker