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

handshake-org / hsd / 8452447998

27 Mar 2024 01:17PM UTC coverage: 16.226% (-52.4%) from 68.632%
8452447998

Pull #888

github

web-flow
Merge c40b9e40c into 0a4f24bdb
Pull Request #888: Wallet TX Count and time indexing

1001 of 12966 branches covered (7.72%)

Branch coverage included in aggregate %.

130 of 474 new or added lines in 11 files covered. (27.43%)

17522 existing lines in 124 files now uncovered.

6546 of 33547 relevant lines covered (19.51%)

37.41 hits per line

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

30.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);
4✔
40

41
    this.opened = false;
4✔
42

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

46
    this.chain = new Chain({
4✔
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({
4✔
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);
4✔
80

81
    this.http = new HTTP({
4✔
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')) {
4✔
97
      this.ns = new RootServer({
3✔
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')) {
3!
108
        this.rs = new RecursiveServer({
3✔
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();
4✔
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));
4✔
132
    this.chain.on('abort', err => this.abort(err));
4✔
133

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

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

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

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

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

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

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

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

163
    this.loadPlugins();
4✔
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() {
UNCOV
173
    assert(!this.opened, 'SPVNode is already open.');
×
UNCOV
174
    this.opened = true;
×
175

UNCOV
176
    await this.handlePreopen();
×
UNCOV
177
    await this.chain.open();
×
UNCOV
178
    await this.pool.open();
×
179

UNCOV
180
    await this.openPlugins();
×
181

UNCOV
182
    await this.http.open();
×
183

UNCOV
184
    if (this.ns)
×
UNCOV
185
      await this.ns.open();
×
186

UNCOV
187
    if (this.rs)
×
UNCOV
188
      await this.rs.open();
×
189

UNCOV
190
    await this.handleOpen();
×
191

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

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

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

UNCOV
205
    await this.handlePreclose();
×
UNCOV
206
    await this.http.close();
×
207

UNCOV
208
    if (this.rs)
×
UNCOV
209
      await this.rs.close();
×
210

UNCOV
211
    if (this.ns)
×
UNCOV
212
      await this.ns.close();
×
213

UNCOV
214
    await this.closePlugins();
×
215

UNCOV
216
    await this.pool.close();
×
UNCOV
217
    await this.chain.close();
×
UNCOV
218
    await this.handleClose();
×
219

UNCOV
220
    this.logger.info('Node is closed.');
×
UNCOV
221
    this.emit('closed');
×
UNCOV
222
    this.emit('close');
×
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) {
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) {
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) {
UNCOV
311
    const key = proof.getKey();
×
312

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

UNCOV
318
    if (this.chain.tip.height + 1 >= this.network.goosigStop) {
×
UNCOV
319
      if (key.isGoo()) {
×
UNCOV
320
        this.emit('error', new Error('GooSig disabled.'));
×
UNCOV
321
        return Promise.resolve();
×
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() {
UNCOV
344
    return this.pool.connect();
×
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() {
UNCOV
361
    return this.pool.startSync();
×
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

© 2026 Coveralls, Inc