Skip to content

Commit 73533cd

Browse files
committed
Merge PR #923 from 'nodech/type-cleanup'
2 parents 0da3a98 + efb0029 commit 73533cd

17 files changed

+361
-270
lines changed

lib/blockchain/chain.js

+95-82
Large diffs are not rendered by default.

lib/blockchain/chaindb.js

+104-89
Large diffs are not rendered by default.

lib/blockchain/chainentry.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const Headers = require('../primitives/headers');
1414
const InvItem = require('../primitives/invitem');
1515
const util = require('../utils/util');
1616

17+
/** @typedef {import('../types').BufioWriter} BufioWriter */
18+
/** @typedef {import('../protocol/network')} Network */
19+
/** @typedef {import('../primitives/block')} Block */
20+
/** @typedef {import('../primitives/merkleblock')} MerkleBlock */
21+
1722
/*
1823
* Constants
1924
*/
@@ -69,8 +74,8 @@ class ChainEntry extends bio.Struct {
6974

7075
/**
7176
* Inject properties from options.
72-
* @private
7377
* @param {Object} options
78+
* @returns {this}
7479
*/
7580

7681
fromOptions(options) {
@@ -126,6 +131,7 @@ class ChainEntry extends bio.Struct {
126131
/**
127132
* Calculate the chainwork by
128133
* adding proof to previous chainwork.
134+
* @param {ChainEntry?} [prev] - Previous entry.
129135
* @returns {BN} chainwork
130136
*/
131137

@@ -169,9 +175,9 @@ class ChainEntry extends bio.Struct {
169175

170176
/**
171177
* Inject properties from block.
172-
* @private
173178
* @param {Block|MerkleBlock} block
174-
* @param {ChainEntry} prev - Previous entry.
179+
* @param {ChainEntry?} [prev] - Previous entry.
180+
* @returns {this}
175181
*/
176182

177183
fromBlock(block, prev) {
@@ -203,7 +209,8 @@ class ChainEntry extends bio.Struct {
203209

204210
/**
205211
* Serialize the entry to internal database format.
206-
* @returns {Buffer}
212+
* @param {BufioWriter} bw
213+
* @returns {BufioWriter}
207214
*/
208215

209216
write(bw) {
@@ -234,8 +241,7 @@ class ChainEntry extends bio.Struct {
234241

235242
/**
236243
* Inject properties from serialized data.
237-
* @private
238-
* @param {Buffer} data
244+
* @param {bio.BufferReader} br
239245
*/
240246

241247
read(br) {
@@ -291,8 +297,8 @@ class ChainEntry extends bio.Struct {
291297

292298
/**
293299
* Inject properties from json object.
294-
* @private
295300
* @param {Object} json
301+
* @returns {this}
296302
*/
297303

298304
fromJSON(json) {
@@ -355,7 +361,7 @@ class ChainEntry extends bio.Struct {
355361
/**
356362
* Instantiate chainentry from block.
357363
* @param {Block|MerkleBlock} block
358-
* @param {ChainEntry} prev - Previous entry.
364+
* @param {ChainEntry?} [prev] - Previous entry.
359365
* @returns {ChainEntry}
360366
*/
361367

lib/blockchain/common.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
'use strict';
88

9+
/** @typedef {import('@handshake-org/bfilter').BloomFilter} BloomFilter */
10+
/** @typedef {import('../types').LockFlags} LockFlags */
11+
912
/**
1013
* @module blockchain/common
1114
*/
@@ -23,16 +26,16 @@ exports.lockFlags = {};
2326
* @default
2427
*/
2528

26-
exports.lockFlags.MANDATORY_LOCKTIME_FLAGS = 0;
29+
exports.MANDATORY_LOCKTIME_FLAGS = 0;
2730

2831
/**
2932
* Standard locktime flags (used for mempool validation).
3033
* @const {LockFlags}
3134
* @default
3235
*/
3336

34-
exports.lockFlags.STANDARD_LOCKTIME_FLAGS = 0
35-
| exports.lockFlags.MANDATORY_LOCKTIME_FLAGS;
37+
exports.STANDARD_LOCKTIME_FLAGS = 0
38+
| exports.MANDATORY_LOCKTIME_FLAGS;
3639

3740
/**
3841
* Threshold states for versionbits
@@ -66,7 +69,7 @@ exports.flags = {
6669
* @default
6770
*/
6871

69-
exports.flags.DEFAULT_FLAGS = 0
72+
exports.DEFAULT_FLAGS = 0
7073
| exports.flags.VERIFY_POW
7174
| exports.flags.VERIFY_BODY;
7275

lib/blockchain/migrations.js

+37-24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const {
2525
} = require('../migrations/migrator');
2626

2727
/** @typedef {import('../types').Hash} Hash */
28+
/** @typedef {ReturnType<bdb.DB['batch']>} Batch */
29+
/** @typedef {import('../migrations/migrator').types} MigrationType */
2830

2931
/**
3032
* Switch to new migrations layout.
@@ -46,6 +48,10 @@ class MigrateMigrations extends AbstractMigration {
4648
this.layout = MigrateMigrations.layout();
4749
}
4850

51+
/**
52+
* @returns {Promise<MigrationType>}
53+
*/
54+
4955
async check() {
5056
return types.MIGRATE;
5157
}
@@ -59,8 +65,8 @@ class MigrateMigrations extends AbstractMigration {
5965
async migrate(b) {
6066
this.logger.info('Migrating migrations..');
6167

62-
const oldLayout = this.layout.oldLayout.wdb;
63-
const newLayout = this.layout.newLayout.wdb;
68+
const oldLayout = this.layout.oldLayout;
69+
const newLayout = this.layout.newLayout;
6470
let nextMigration = 1;
6571
const skipped = [];
6672

@@ -83,10 +89,17 @@ class MigrateMigrations extends AbstractMigration {
8389
}
8490

8591
this.db.writeVersion(b, 2);
86-
b.put(newLayout.M.encode(),
87-
this.encodeMigrationState(nextMigration, skipped));
92+
93+
const rawState = this.encodeMigrationState(nextMigration, skipped);
94+
b.put(newLayout.M.encode(), rawState);
8895
}
8996

97+
/**
98+
* @param {Number} nextMigration
99+
* @param {Number[]} skipped
100+
* @returns {Buffer}
101+
*/
102+
90103
encodeMigrationState(nextMigration, skipped) {
91104
let size = 4;
92105
size += encoding.sizeVarint(nextMigration);
@@ -116,14 +129,10 @@ class MigrateMigrations extends AbstractMigration {
116129
static layout() {
117130
return {
118131
oldLayout: {
119-
wdb: {
120-
M: bdb.key('M', ['uint32'])
121-
}
132+
M: bdb.key('M', ['uint32'])
122133
},
123134
newLayout: {
124-
wdb: {
125-
M: bdb.key('M')
126-
}
135+
M: bdb.key('M')
127136
}
128137
};
129138
}
@@ -138,7 +147,7 @@ class MigrateChainState extends AbstractMigration {
138147
/**
139148
* Create migration chain state
140149
* @constructor
141-
* @param {ChainMigrator} options
150+
* @param {ChainMigratorOptions} options
142151
*/
143152

144153
constructor(options) {
@@ -261,7 +270,7 @@ class MigrateChainState extends AbstractMigration {
261270
/**
262271
* Get Block (old layout)
263272
* @param {Hash} hash
264-
* @returns {Promise} - Block
273+
* @returns {Promise<Block>}
265274
*/
266275

267276
async getBlock(hash) {
@@ -276,7 +285,7 @@ class MigrateChainState extends AbstractMigration {
276285

277286
/**
278287
* Get block view (old layout)
279-
* @param {Hash} hash
288+
* @param {Block} block
280289
* @returns {Promise} - UndoCoins
281290
*/
282291

@@ -369,7 +378,6 @@ class MigrateBlockStore extends AbstractMigration {
369378

370379
/**
371380
* Migrate blocks and undo blocks
372-
* @param {Batch} b
373381
* @returns {Promise}
374382
*/
375383

@@ -480,7 +488,7 @@ class MigrateTreeState extends AbstractMigration {
480488
/**
481489
* Create tree state migrator
482490
* @constructor
483-
* @param {ChainMigrator} options
491+
* @param {ChainMigratorOptions} options
484492
*/
485493

486494
constructor(options) {
@@ -498,6 +506,11 @@ class MigrateTreeState extends AbstractMigration {
498506
return types.MIGRATE;
499507
}
500508

509+
/**
510+
* @param {Batch} b
511+
* @returns {Promise}
512+
*/
513+
501514
async migrate(b) {
502515
if (this.options.spv) {
503516
this.db.writeVersion(b, 3);
@@ -612,7 +625,7 @@ class ChainMigratorOptions {
612625
this.network = Network.primary;
613626
this.logger = Logger.global;
614627

615-
this.migrations = exports.migrations;
628+
this.migrations = ChainMigrator.migrations;
616629
this.migrateFlag = -1;
617630

618631
this.dbVersion = 0;
@@ -672,23 +685,23 @@ class ChainMigratorOptions {
672685
assert(typeof options.prune === 'boolean');
673686
this.prune = options.prune;
674687
}
688+
689+
return this;
675690
}
676691
}
677692

678-
exports = ChainMigrator;
679-
680693
// List of the migrations with ids
681-
exports.migrations = {
694+
ChainMigrator.migrations = {
682695
0: MigrateMigrations,
683696
1: MigrateChainState,
684697
2: MigrateBlockStore,
685698
3: MigrateTreeState
686699
};
687700

688701
// Expose migrations
689-
exports.MigrateChainState = MigrateChainState;
690-
exports.MigrateMigrations = MigrateMigrations;
691-
exports.MigrateBlockStore = MigrateBlockStore;
692-
exports.MigrateTreeState = MigrateTreeState;
702+
ChainMigrator.MigrateChainState = MigrateChainState;
703+
ChainMigrator.MigrateMigrations = MigrateMigrations;
704+
ChainMigrator.MigrateBlockStore = MigrateBlockStore;
705+
ChainMigrator.MigrateTreeState = MigrateTreeState;
693706

694-
module.exports = exports;
707+
module.exports = ChainMigrator;

lib/covenants/rules.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const rules = exports;
2020

2121
/** @typedef {import('../types').Amount} AmountValue */
2222
/** @typedef {import('../types').Hash} Hash */
23+
/** @typedef {import('../types').NameFlags} NameFlags */
2324
/** @typedef {import('../protocol/network')} Network */
2425
/** @typedef {import('../primitives/tx')} TX */
2526
/** @typedef {import('../coins/coinview')} CoinView */
@@ -130,11 +131,11 @@ rules.nameFlags = {
130131

131132
/**
132133
* Standard verify flags for covenants.
133-
* @const {NameFlags}
134+
* @const {rules.NameFlags}
134135
* @default
135136
*/
136137

137-
rules.nameFlags.MANDATORY_VERIFY_COVENANT_FLAGS = 0;
138+
rules.MANDATORY_VERIFY_COVENANT_FLAGS = 0;
138139

139140
/**
140141
* Maximum covenant size.

lib/mempool/mempool.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ class Mempool extends EventEmitter {
448448
const state = await this.getNextState();
449449
const mtp = await this.chain.getMedianTime(this.chain.tip);
450450
const remove = [];
451-
const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS;
451+
const lockFlags = common.STANDARD_LOCKTIME_FLAGS;
452452

453453
this.contracts.clear();
454454

@@ -1486,7 +1486,7 @@ class Mempool extends EventEmitter {
14861486
async insertTX(tx, id) {
14871487
assert(!tx.mutable, 'Cannot add mutable TX to mempool.');
14881488

1489-
const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS;
1489+
const lockFlags = common.STANDARD_LOCKTIME_FLAGS;
14901490
const height = this.chain.height;
14911491
const hash = tx.hash();
14921492

@@ -1604,7 +1604,7 @@ class Mempool extends EventEmitter {
16041604
const network = this.network;
16051605
const height = this.chain.height + 1;
16061606
const state = await this.getNextState();
1607-
const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS;
1607+
const lockFlags = common.STANDARD_LOCKTIME_FLAGS;
16081608
const tx = entry.tx;
16091609

16101610
// Verify sequence locks.

lib/migrations/migration.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
'use strict';
77

8+
/** @typedef {import('bdb').DB} DB */
9+
/** @typedef {ReturnType<DB['batch']>} Batch */
10+
/** @typedef {import('./migrator').types} MigrationType */
11+
812
/**
913
* Abstract class for single migration.
1014
* @alias module:migrations.AbstractMigration
@@ -23,7 +27,7 @@ class AbstractMigration {
2327

2428
/**
2529
* Check if the migration applies to the database
26-
* @returns {Promise}
30+
* @returns {Promise<MigrationType>}
2731
*/
2832

2933
async check() {
@@ -32,11 +36,11 @@ class AbstractMigration {
3236

3337
/**
3438
* Run the actual migration
35-
* @param {Batch} batch
39+
* @param {Batch} b
3640
* @returns {Promise}
3741
*/
3842

39-
async migrate() {
43+
async migrate(b) {
4044
throw new Error('Abstract method.');
4145
}
4246

0 commit comments

Comments
 (0)