• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

handshake-org / hsd / 7221071989

15 Dec 2023 10:45AM UTC coverage: 68.695% (+0.04%) from 68.654%
7221071989

push

github

nodech
Merge PR #856 from 'nodech/interactive-scan'

7544 of 12828 branches covered (0.0%)

Branch coverage included in aggregate %.

80 of 106 new or added lines in 10 files covered. (75.47%)

3 existing lines in 3 files now uncovered.

24071 of 33194 relevant lines covered (72.52%)

34072.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.4
/lib/node/spvnode.js
1
/*!
2
 * spvnode.js - spv node for hsd
3
 * Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
4
 * https://github.com/handshake-org/hsd
5
 */
6

7
'use strict';
8

9
const assert = require('bsert');
1✔
10
const NameState = require('../covenants/namestate');
1✔
11
const Chain = require('../blockchain/chain');
1✔
12
const Pool = require('../net/pool');
1✔
13
const Node = require('./node');
1✔
14
const HTTP = require('./http');
1✔
15
const RPC = require('./rpc');
1✔
16
const pkg = require('../pkg');
1✔
17
const {RootServer, RecursiveServer} = require('../dns/server');
1✔
18

19
/**
20
 * SPV Node
21
 * Create an spv node which only maintains
22
 * a chain, a pool, and an http server.
23
 * @alias module:node.SPVNode
24
 * @extends Node
25
 */
26

27
class SPVNode extends Node {
28
  /**
29
   * Create SPV node.
30
   * @constructor
31
   * @param {Object?} options
32
   * @param {Buffer?} options.sslKey
33
   * @param {Buffer?} options.sslCert
34
   * @param {Number?} options.httpPort
35
   * @param {String?} options.httpHost
36
   */
37

38
  constructor(options) {
39
    super(pkg.core, pkg.cfg, 'debug.log', options);
12✔
40

41
    this.opened = false;
12✔
42

43
    // SPV flag.
44
    this.spv = true;
12✔
45

46
    this.chain = new Chain({
12✔
47
      network: this.network,
48
      logger: this.logger,
49
      prefix: this.config.prefix,
50
      memory: this.config.bool('memory'),
51
      maxFiles: this.config.uint('max-files'),
52
      cacheSize: this.config.mb('cache-size'),
53
      entryCache: this.config.uint('entry-cache'),
54
      checkpoints: this.config.bool('checkpoints'),
55
      chainMigrate: this.config.uint('chain-migrate'),
56
      spv: true
57
    });
58

59
    this.pool = new Pool({
12✔
60
      network: this.network,
61
      logger: this.logger,
62
      chain: this.chain,
63
      prefix: this.config.prefix,
64
      proxy: this.config.str('proxy'),
65
      onion: this.config.bool('onion'),
66
      brontideOnly: this.config.bool('brontide-only'),
67
      upnp: this.config.bool('upnp'),
68
      seeds: this.config.array('seeds'),
69
      nodes: this.config.array('nodes'),
70
      only: this.config.array('only'),
71
      identityKey: this.identityKey,
72
      maxOutbound: this.config.uint('max-outbound'),
73
      createSocket: this.config.func('create-socket'),
74
      memory: this.config.bool('memory'),
75
      agent: this.config.str('agent'),
76
      listen: false
77
    });
78

79
    this.rpc = new RPC(this);
12✔
80

81
    this.http = new HTTP({
12✔
82
      network: this.network,
83
      logger: this.logger,
84
      node: this,
85
      prefix: this.config.prefix,
86
      ssl: this.config.bool('ssl'),
87
      keyFile: this.config.path('ssl-key'),
88
      certFile: this.config.path('ssl-cert'),
89
      host: this.config.str('http-host'),
90
      port: this.config.uint('http-port'),
91
      apiKey: this.config.str('api-key'),
92
      noAuth: this.config.bool('no-auth'),
93
      cors: this.config.bool('cors')
94
    });
95

96
    if (!this.config.bool('no-dns')) {
12✔
97
      this.ns = new RootServer({
10✔
98
        logger: this.logger,
99
        key: this.identityKey,
100
        host: this.config.str('ns-host'),
101
        port: this.config.uint('ns-port', this.network.nsPort),
102
        lookup: key => this.pool.resolve(key),
×
103
        publicHost: this.config.str('public-host'),
104
        noSig0: this.config.bool('no-sig0')
105
      });
106

107
      if (!this.config.bool('no-rs')) {
10✔
108
        this.rs = new RecursiveServer({
9✔
109
          logger: this.logger,
110
          key: this.identityKey,
111
          host: this.config.str('rs-host'),
112
          port: this.config.uint('rs-port', this.network.rsPort),
113
          stubHost: this.ns.host,
114
          stubPort: this.ns.port,
115
          noUnbound: this.config.bool('rs-no-unbound'),
116
          noSig0: this.config.bool('no-sig0')
117
        });
118
      }
119
    }
120

121
    this.init();
12✔
122
  }
123

124
  /**
125
   * Initialize the node.
126
   * @private
127
   */
128

129
  init() {
130
    // Bind to errors
131
    this.chain.on('error', err => this.error(err));
12✔
132
    this.chain.on('abort', err => this.abort(err));
12✔
133

134
    this.pool.on('error', err => this.error(err));
12✔
135

136
    if (this.http)
12!
137
      this.http.on('error', err => this.error(err));
12✔
138

139
    this.pool.on('tx', (tx) => {
12✔
140
      this.emit('tx', tx);
1✔
141
    });
142

143
    this.chain.on('block', (block) => {
12✔
144
      this.emit('block', block);
510✔
145
    });
146

147
    this.chain.on('connect', async (entry, block) => {
12✔
148
      this.emit('connect', entry, block);
510✔
149
    });
150

151
    this.chain.on('disconnect', (entry, block) => {
12✔
152
      this.emit('disconnect', entry, block);
11✔
153
    });
154

155
    this.chain.on('reorganize', (tip, competitor, fork) => {
12✔
156
      this.emit('reorganize', tip, competitor, fork);
1✔
157
    });
158

159
    this.chain.on('reset', (tip) => {
12✔
160
      this.emit('reset', tip);
6✔
161
    });
162

163
    this.loadPlugins();
12✔
164
  }
165

166
  /**
167
   * Open the node and all its child objects,
168
   * wait for the database to load.
169
   * @returns {Promise}
170
   */
171

172
  async open() {
173
    assert(!this.opened, 'SPVNode is already open.');
12✔
174
    this.opened = true;
12✔
175

176
    await this.handlePreopen();
12✔
177
    await this.chain.open();
12✔
178
    await this.pool.open();
12✔
179

180
    await this.openPlugins();
12✔
181

182
    await this.http.open();
12✔
183

184
    if (this.ns)
12✔
185
      await this.ns.open();
10✔
186

187
    if (this.rs)
12✔
188
      await this.rs.open();
9✔
189

190
    await this.handleOpen();
12✔
191

192
    this.logger.info('Node is loaded.');
12✔
193
    this.emit('open');
12✔
194
  }
195

196
  /**
197
   * Close the node, wait for the database to close.
198
   * @returns {Promise}
199
   */
200

201
  async close() {
202
    assert(this.opened, 'SPVNode is not open.');
12✔
203
    this.opened = false;
12✔
204

205
    await this.handlePreclose();
12✔
206
    await this.http.close();
12✔
207

208
    if (this.rs)
12✔
209
      await this.rs.close();
9✔
210

211
    if (this.ns)
12✔
212
      await this.ns.close();
10✔
213

214
    await this.closePlugins();
12✔
215

216
    await this.pool.close();
12✔
217
    await this.chain.close();
12✔
218
    await this.handleClose();
12✔
219

220
    this.logger.info('Node is closed.');
12✔
221
    this.emit('closed');
12✔
222
    this.emit('close');
12✔
223
  }
224

225
  /**
226
   * Scan for any missed transactions.
227
   * Note that this will replay the blockchain sync.
228
   * @param {Number|Hash} start - Start block.
229
   * @param {BloomFilter} filter
230
   * @param {Function} iter
231
   * @returns {Promise}
232
   */
233

234
  async scan(start, filter, iter) {
NEW
235
    throw new Error('Not implemented.');
×
236
  }
237

238
  /**
239
   * Interactive scan for any missed transactions.
240
   * @param {Number|Hash} start
241
   * @param {BloomFilter} filter
242
   * @param {Function} iter
243
   * @returns {Promise}
244
   */
245

246
  scanInteractive(start, filter, iter) {
UNCOV
247
    throw new Error('Not implemented.');
×
248
  }
249

250
  /**
251
   * Broadcast a transaction.
252
   * @param {TX|Block} item
253
   * @returns {Promise}
254
   */
255

256
  async broadcast(item) {
257
    try {
×
258
      await this.pool.broadcast(item);
×
259
    } catch (e) {
260
      this.emit('error', e);
×
261
    }
262
  }
263

264
  /**
265
   * Broadcast a transaction.
266
   * @param {TX} tx
267
   * @returns {Promise}
268
   */
269

270
  sendTX(tx) {
271
    return this.broadcast(tx);
×
272
  }
273

274
  /**
275
   * Broadcast a transaction. Silence errors.
276
   * @param {TX} tx
277
   * @returns {Promise}
278
   */
279

280
  relay(tx) {
281
    return this.broadcast(tx);
×
282
  }
283

284
  /**
285
   * Broadcast a claim.
286
   * @param {Claim} claim
287
   * @returns {Promise}
288
   */
289

290
  sendClaim(claim) {
291
    return this.broadcast(claim);
×
292
  }
293

294
  /**
295
   * Broadcast a claim. Silence errors.
296
   * @param {Claim} claim
297
   * @returns {Promise}
298
   */
299

300
  relayClaim(claim) {
301
    return this.broadcast(claim);
×
302
  }
303

304
  /**
305
   * Broadcast an airdrop proof.
306
   * @param {AirdropProof} proof
307
   * @returns {Promise}
308
   */
309

310
  sendAirdrop(proof) {
311
    const key = proof.getKey();
1✔
312

313
    if (!key) {
1!
314
      this.emit('error', new Error('Invalid Airdrop.'));
×
315
      return Promise.resolve();
×
316
    }
317

318
    if (this.chain.tip.height + 1 >= this.network.goosigStop) {
1!
319
      if (key.isGoo()) {
1!
320
        this.emit('error', new Error('GooSig disabled.'));
1✔
321
        return Promise.resolve();
1✔
322
      }
323
    }
324

325
    return this.broadcast(proof);
×
326
  }
327

328
  /**
329
   * Broadcast an airdrop proof. Silence errors.
330
   * @param {AirdropProof} proof
331
   * @returns {Promise}
332
   */
333

334
  relayAirdrop(proof) {
335
    return this.broadcast(proof);
×
336
  }
337

338
  /**
339
   * Connect to the network.
340
   * @returns {Promise}
341
   */
342

343
  connect() {
344
    return this.pool.connect();
4✔
345
  }
346

347
  /**
348
   * Disconnect from the network.
349
   * @returns {Promise}
350
   */
351

352
  disconnect() {
353
    return this.pool.disconnect();
×
354
  }
355

356
  /**
357
   * Start the blockchain sync.
358
   */
359

360
  startSync() {
361
    return this.pool.startSync();
4✔
362
  }
363

364
  /**
365
   * Stop syncing the blockchain.
366
   */
367

368
  stopSync() {
369
    return this.pool.stopSync();
×
370
  }
371

372
  /**
373
   * Get current name state.
374
   * @param {Buffer} nameHash
375
   * @returns {NameState}
376
   */
377

378
  async getNameStatus(nameHash) {
379
    const network = this.network;
×
380
    const height = this.chain.height + 1;
×
381
    const blob = await this.pool.resolve(nameHash);
×
382

383
    if (!blob) {
×
384
      const state = new NameState();
×
385
      state.reset(height);
×
386
      return state;
×
387
    }
388

389
    const state = NameState.decode(blob);
×
390

391
    state.maybeExpire(height, network);
×
392

393
    return state;
×
394
  }
395
}
396

397
/*
398
 * Expose
399
 */
400

401
module.exports = SPVNode;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc