|
| 1 | +const ffi = require('ffi-napi') |
| 2 | + , ref = require('ref-napi') |
| 3 | + , path = require('path') |
| 4 | + , EventEmitter = require('events') |
| 5 | + , debug = require('debug')('bwt-daemon') |
| 6 | + |
| 7 | +const LIB_PATH = process.env.BWT_LIB || path.join(__dirname, 'libbwt') |
| 8 | + |
| 9 | +// Low-level private API |
| 10 | + |
| 11 | +const OK = 0 |
| 12 | + |
| 13 | +const shutdownPtr = ref.refType('void') |
| 14 | + |
| 15 | +const libbwt = ffi.Library(LIB_PATH, { |
| 16 | + bwt_start: [ 'int', [ 'string', 'pointer', 'pointer' ] ] |
| 17 | +, bwt_shutdown: [ 'int', [ shutdownPtr ] ] |
| 18 | +}) |
| 19 | + |
| 20 | +function bwt_start(options, init_cb, notify_cb, done) { |
| 21 | + const opt_json = JSON.stringify(options) |
| 22 | + , init_cb_ffi = ffi.Callback('void', [ shutdownPtr ], init_cb) |
| 23 | + , notify_cb_ffi = ffi.Callback('void', [ 'string', 'float', 'uint32', 'string' ], notify_cb) |
| 24 | + |
| 25 | + libbwt.bwt_start.async(opt_json, init_cb_ffi, notify_cb_ffi, function(err, code) { |
| 26 | + debug('stopped with', { err, code }) |
| 27 | + if (err) return done(err) |
| 28 | + if (code != OK) return done(new Error(`bwt failed with code ${code}`)) |
| 29 | + done(null) |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +function bwt_shutdown(shutdown_ptr) { |
| 34 | + const code = libbwt.bwt_shutdown(shutdown_ptr) |
| 35 | + shutdown_ptr.deref() |
| 36 | + if (code != OK) throw new Error(`bwt shutdown failed with code ${code}`) |
| 37 | +} |
| 38 | + |
| 39 | +// High-level public API |
| 40 | + |
| 41 | +class BwtDaemon extends EventEmitter { |
| 42 | + constructor(options) { |
| 43 | + super() |
| 44 | + |
| 45 | + if (options.progress) this.on('progress', take_prop(options, 'progress')) |
| 46 | + this.options = normalize_options(options) |
| 47 | + |
| 48 | + this.ready = new Promise((resolve, reject) => { |
| 49 | + this.on('ready', _ => resolve(this)) |
| 50 | + this.on('error', reject) |
| 51 | + this.on('exit', err => { |
| 52 | + if (err) return this.emit('error', err) |
| 53 | + // this `reject` will be ignored if the daemon already started up successfully |
| 54 | + // and the promise was resolved, which can happen following a shutdown() call. |
| 55 | + // imagine this is wrapped in an `if (!promise.resolved)`. |
| 56 | + reject(new Error('daemon stopped while starting up')) |
| 57 | + }) |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + start() { |
| 62 | + if (this._started) throw new Error('daemon already started') |
| 63 | + this._started = true |
| 64 | + |
| 65 | + debug('starting with %O', { ...this.options, bitcoind_auth: '**SCRUBBED**' }); |
| 66 | + bwt_start(this.options, this._init.bind(this), this._notify.bind(this) |
| 67 | + , err => this.emit('exit', err /*null for successful exit*/)) |
| 68 | + return this.ready |
| 69 | + } |
| 70 | + |
| 71 | + shutdown() { |
| 72 | + debug('shutdown', this._shutdown_ptr != null) |
| 73 | + // we cannot shut down yet, mark for later termination |
| 74 | + if (!this._shutdown_ptr) return this._terminate = true |
| 75 | + |
| 76 | + bwt_shutdown(take_prop(this, '_shutdown_ptr')) |
| 77 | + } |
| 78 | + |
| 79 | + _init(shutdown_ptr) { |
| 80 | + this._shutdown_ptr = shutdown_ptr |
| 81 | + if (take_prop(this, '_terminate')) this.shutdown() |
| 82 | + } |
| 83 | + |
| 84 | + _notify(msg_type, progress_n, detail_n, detail_s) { |
| 85 | + debug('notify %s %s %s', msg_type, progress_n, detail_n, detail_s) |
| 86 | + switch (msg_type) { |
| 87 | + case 'error': |
| 88 | + this.emit('error', detail_s) |
| 89 | + break |
| 90 | + case 'progress:sync': |
| 91 | + this.emit('progress', 'sync', progress_n, { tip_time: detail_n }) |
| 92 | + break |
| 93 | + case 'progress:scan': |
| 94 | + this.emit('progress', 'scan', progress_n, { eta: detail_n }) |
| 95 | + break |
| 96 | + case 'ready:http': |
| 97 | + this.http_addr = detail_s |
| 98 | + this.http_url = `http://${this.http_addr}/` |
| 99 | + break |
| 100 | + case 'ready:electrum': |
| 101 | + this.electrum_addr = detail_s |
| 102 | + break |
| 103 | + case 'ready': |
| 104 | + this.emit('ready') |
| 105 | + break |
| 106 | + default: debug('unknown msg', msg_type) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// optional 'new' |
| 112 | +exports = module.exports = function(opt) { return new BwtDaemon(opt) } |
| 113 | +exports.BwtDaemon = BwtDaemon |
| 114 | +exports.start = BwtDaemon.start = opt => new BwtDaemon(opt).start() |
| 115 | + |
| 116 | +// Utility |
| 117 | + |
| 118 | +function normalize_options(options) { |
| 119 | + if (options.rescan_since) { |
| 120 | + options.rescan_since = parse_timestamp(options.rescan_since) |
| 121 | + } |
| 122 | + if (take_prop(options, 'electrum') && !options.electrum_addr) { |
| 123 | + options.electrum_addr = '127.0.0.1:0' |
| 124 | + } |
| 125 | + if (take_prop(options, 'http') && !options.http_addr) { |
| 126 | + options.http_addr = '127.0.0.1:0' |
| 127 | + } |
| 128 | + |
| 129 | + if (!options.electrum_addr && !options.http_addr) { |
| 130 | + throw new Error('None of the bwt services are enabled') |
| 131 | + } |
| 132 | + |
| 133 | + // Delete nully options so that they get their default value |
| 134 | + Object.entries(options) |
| 135 | + .filter(([ _, val ]) => val == null) |
| 136 | + .forEach(([ key, _ ]) => delete options[key]) |
| 137 | + |
| 138 | + return options |
| 139 | +} |
| 140 | + |
| 141 | +function take_prop(obj, key) { |
| 142 | + const val = obj[key] |
| 143 | + delete obj[key] |
| 144 | + return val |
| 145 | +} |
| 146 | + |
| 147 | +function parse_timestamp(ts) { |
| 148 | + // Pass 'now' as is |
| 149 | + if (ts == 'now') return ts |
| 150 | + // Date objects |
| 151 | + if (ts.getTime) return ts.getTime()/1000|0 |
| 152 | + // Unix timestamp |
| 153 | + if (!isNaN(ts)) return +ts |
| 154 | + // Date string (e.g. YYYY-MM-DD) |
| 155 | + const dt = new Date(ts) |
| 156 | + if (!isNaN(dt.getTime())) return dt.getTime()/1000|0 |
| 157 | + |
| 158 | + throw new Error(`Invalid rescan since value: ${ts}`) |
| 159 | +} |
0 commit comments