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

handshake-org / hsd / 7113784955

06 Dec 2023 11:15AM UTC coverage: 68.615% (+0.003%) from 68.612%
7113784955

push

github

nodech
Merge PR #879 from 'nodech/cache-test-dns'

7499 of 12765 branches covered (0.0%)

Branch coverage included in aggregate %.

23952 of 33072 relevant lines covered (72.42%)

34147.95 hits per line

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

75.0
/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
   * @returns {Promise}
230
   */
231

232
  async scan(start) {
233
    throw new Error('Not implemented.');
×
234
  }
235

236
  /**
237
   * Broadcast a transaction.
238
   * @param {TX|Block} item
239
   * @returns {Promise}
240
   */
241

242
  async broadcast(item) {
243
    try {
×
244
      await this.pool.broadcast(item);
×
245
    } catch (e) {
246
      this.emit('error', e);
×
247
    }
248
  }
249

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

256
  sendTX(tx) {
257
    return this.broadcast(tx);
×
258
  }
259

260
  /**
261
   * Broadcast a transaction. Silence errors.
262
   * @param {TX} tx
263
   * @returns {Promise}
264
   */
265

266
  relay(tx) {
267
    return this.broadcast(tx);
×
268
  }
269

270
  /**
271
   * Broadcast a claim.
272
   * @param {Claim} claim
273
   * @returns {Promise}
274
   */
275

276
  sendClaim(claim) {
277
    return this.broadcast(claim);
×
278
  }
279

280
  /**
281
   * Broadcast a claim. Silence errors.
282
   * @param {Claim} claim
283
   * @returns {Promise}
284
   */
285

286
  relayClaim(claim) {
287
    return this.broadcast(claim);
×
288
  }
289

290
  /**
291
   * Broadcast an airdrop proof.
292
   * @param {AirdropProof} proof
293
   * @returns {Promise}
294
   */
295

296
  sendAirdrop(proof) {
297
    const key = proof.getKey();
1✔
298

299
    if (!key) {
1!
300
      this.emit('error', new Error('Invalid Airdrop.'));
×
301
      return Promise.resolve();
×
302
    }
303

304
    if (this.chain.tip.height + 1 >= this.network.goosigStop) {
1!
305
      if (key.isGoo()) {
1!
306
        this.emit('error', new Error('GooSig disabled.'));
1✔
307
        return Promise.resolve();
1✔
308
      }
309
    }
310

311
    return this.broadcast(proof);
×
312
  }
313

314
  /**
315
   * Broadcast an airdrop proof. Silence errors.
316
   * @param {AirdropProof} proof
317
   * @returns {Promise}
318
   */
319

320
  relayAirdrop(proof) {
321
    return this.broadcast(proof);
×
322
  }
323

324
  /**
325
   * Connect to the network.
326
   * @returns {Promise}
327
   */
328

329
  connect() {
330
    return this.pool.connect();
3✔
331
  }
332

333
  /**
334
   * Disconnect from the network.
335
   * @returns {Promise}
336
   */
337

338
  disconnect() {
339
    return this.pool.disconnect();
×
340
  }
341

342
  /**
343
   * Start the blockchain sync.
344
   */
345

346
  startSync() {
347
    return this.pool.startSync();
3✔
348
  }
349

350
  /**
351
   * Stop syncing the blockchain.
352
   */
353

354
  stopSync() {
355
    return this.pool.stopSync();
×
356
  }
357

358
  /**
359
   * Get current name state.
360
   * @param {Buffer} nameHash
361
   * @returns {NameState}
362
   */
363

364
  async getNameStatus(nameHash) {
365
    const network = this.network;
×
366
    const height = this.chain.height + 1;
×
367
    const blob = await this.pool.resolve(nameHash);
×
368

369
    if (!blob) {
×
370
      const state = new NameState();
×
371
      state.reset(height);
×
372
      return state;
×
373
    }
374

375
    const state = NameState.decode(blob);
×
376

377
    state.maybeExpire(height, network);
×
378

379
    return state;
×
380
  }
381
}
382

383
/*
384
 * Expose
385
 */
386

387
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