diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 17da7163..07d634be 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -36,6 +36,17 @@ const { VERIFY_COVENANTS_LOCKUP } = rules.nameFlags; +/** @typedef {import('../types').Hash} Hash */ +/** @typedef {import('../types').LockFlags} LockFlags */ +/** @typedef {import('@handshake-org/bfilter').BloomFilter} BloomFilter */ +/** @typedef {import('../primitives/block')} Block */ +/** @typedef {import('../primitives/tx')} TX */ +/** @typedef {import('../primitives/txmeta')} TXMeta */ +/** @typedef {import('../primitives/outpoint')} Outpoint */ +/** @typedef {import('../primitives/coin')} Coin */ +/** @typedef {import('../primitives/address')} Address */ +/** @typedef {import('../coins/coinentry')} CoinEntry */ + /** * Blockchain * @alias module:blockchain.Chain @@ -78,7 +89,7 @@ class Chain extends AsyncEmitter { /** * Open the chain, wait for the database to load. - * @returns {Promise} + * @returns {Promise} */ async open() { @@ -123,7 +134,7 @@ class Chain extends AsyncEmitter { /** * Close the chain, wait for the database to close. - * @returns {Promise} + * @returns {Promise} */ async close() { @@ -243,7 +254,7 @@ class Chain extends AsyncEmitter { * @param {Block} block * @param {ChainEntry} prev * @param {Number} flags - * @returns {Promise} - Returns {@link ContextResult}. + * @returns {Promise} - [CoinView, DeploymentState] */ async verifyContext(block, prev, flags) { @@ -272,7 +283,7 @@ class Chain extends AsyncEmitter { * Perform all necessary contextual verification * on a block, without POW check. * @param {Block} block - * @returns {Promise} + * @returns {Promise} - [CoinView, DeploymentState] */ async verifyBlock(block) { @@ -289,18 +300,18 @@ class Chain extends AsyncEmitter { * on a block, without POW check (no lock). * @private * @param {Block} block - * @returns {Promise} + * @returns {Promise} - [CoinView, DeploymentState] */ async _verifyBlock(block) { - const flags = common.flags.DEFAULT_FLAGS & ~common.flags.VERIFY_POW; + const flags = common.DEFAULT_FLAGS & ~common.flags.VERIFY_POW; return this.verifyContext(block, this.tip, flags); } /** * Test whether the hash is in the main chain. * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ isMainHash(hash) { @@ -310,7 +321,7 @@ class Chain extends AsyncEmitter { /** * Test whether the entry is in the main chain. * @param {ChainEntry} entry - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ isMainChain(entry) { @@ -321,7 +332,7 @@ class Chain extends AsyncEmitter { * Get ancestor by `height`. * @param {ChainEntry} entry * @param {Number} height - * @returns {Promise} - Returns ChainEntry. + * @returns {Promise} */ getAncestor(entry, height) { @@ -331,7 +342,7 @@ class Chain extends AsyncEmitter { /** * Get previous entry. * @param {ChainEntry} entry - * @returns {Promise} - Returns ChainEntry. + * @returns {Promise} */ getPrevious(entry) { @@ -341,7 +352,7 @@ class Chain extends AsyncEmitter { /** * Get previous cached entry. * @param {ChainEntry} entry - * @returns {ChainEntry|null} + * @returns {ChainEntry?} */ getPrevCache(entry) { @@ -351,7 +362,7 @@ class Chain extends AsyncEmitter { /** * Get next entry. * @param {ChainEntry} entry - * @returns {Promise} - Returns ChainEntry. + * @returns {Promise} */ getNext(entry) { @@ -361,7 +372,7 @@ class Chain extends AsyncEmitter { /** * Get next entry. * @param {ChainEntry} entry - * @returns {Promise} - Returns ChainEntry. + * @returns {Promise} */ getNextEntry(entry) { @@ -434,7 +445,7 @@ class Chain extends AsyncEmitter { * @param {Block} block * @param {ChainEntry} prev * @param {Number} flags - * @returns {Promise} - Returns {@link DeploymentState}. + * @returns {Promise} */ async verify(block, prev, flags) { @@ -713,7 +724,7 @@ class Chain extends AsyncEmitter { * Check all deployments on a chain. * @param {Number} time * @param {ChainEntry} prev - * @returns {Promise} - Returns {@link DeploymentState}. + * @returns {Promise} */ async getDeployments(time, prev) { @@ -759,7 +770,8 @@ class Chain extends AsyncEmitter { * @private * @param {Block} block * @param {ChainEntry} prev - * @returns {Promise} - Returns {@link CoinView}. + * @param {DeploymentState} state + * @returns {Promise} */ async updateInputs(block, prev, state) { @@ -801,7 +813,7 @@ class Chain extends AsyncEmitter { * @param {Block} block * @param {ChainEntry} prev * @param {DeploymentState} state - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ async verifyInputs(block, prev, state) { @@ -923,7 +935,7 @@ class Chain extends AsyncEmitter { /** * Get main chain height for hash. * @param {Hash} hash - * @returns {Number} + * @returns {Promise} */ async getMainHeight(hash) { @@ -989,7 +1001,7 @@ class Chain extends AsyncEmitter { * @param {TX} tx * @param {CoinView} view * @param {Number} height - * @param {NameFlags} nameFlags + * @param {Number} nameFlags */ async verifyCovenants(tx, view, height, nameFlags) { @@ -1571,7 +1583,7 @@ class Chain extends AsyncEmitter { * @private * @param {ChainEntry} fork - The current chain. * @param {ChainEntry} longer - The competing chain. - * @returns {Promise} + * @returns {Promise} */ async findFork(fork, longer) { @@ -1600,7 +1612,7 @@ class Chain extends AsyncEmitter { * is received. * @private * @param {ChainEntry} competitor - The competing chain's tip. - * @returns {Promise} + * @returns {Promise} - Fork block. */ async reorganize(competitor) { @@ -1749,7 +1761,7 @@ class Chain extends AsyncEmitter { // to the fork block, causing // us to redownload the blocks // on the new main chain. - await this._reset(fork.hash); + await this._reset(fork.hash, false); // Emit disconnection events now that // the chain has successfully reset. @@ -1808,7 +1820,6 @@ class Chain extends AsyncEmitter { * (necessary because we cannot validate the inputs * in alternate chains when they come in). * @param {ChainEntry} entry - * @param {Number} flags * @returns {Promise} */ @@ -2044,10 +2055,11 @@ class Chain extends AsyncEmitter { * Reset the chain to the desired block without a lock. * @private * @param {Hash|Number} block + * @param {Boolean} silent - don't emit reset. * @returns {Promise} */ - async _reset(block, silent) { + async _reset(block, silent = false) { const tip = await this.db.reset(block); // Reset state. @@ -2092,7 +2104,7 @@ class Chain extends AsyncEmitter { * Reset the chain without a lock. * @private * @param {Hash|Number} block - hash/height - * @param {Boolean?} silent + * @param {Boolean} silent * @returns {Promise} */ @@ -2141,7 +2153,7 @@ class Chain extends AsyncEmitter { /** * Retroactively prune the database. - * @returns {Promise} + * @returns {Promise} */ async prune() { @@ -2252,9 +2264,9 @@ class Chain extends AsyncEmitter { /** * Scan the blockchain for transactions containing specified address hashes. * @param {Hash|Number} start - Block hash or height to start at. - * @param {Bloom} filter - Bloom filter containing tx and address hashes. + * @param {BloomFilter} filter - Bloomfilter containing tx and address hashes. * @param {Function} iter - Iterator. - * @returns {Promise} + * @returns {Promise} */ async scan(start, filter, iter) { @@ -2283,7 +2295,7 @@ class Chain extends AsyncEmitter { * address and name hashes. * @param {ScanInteractiveIterCB} iter - Iterator. * @param {Boolean} [fullLock=false] - * @returns {Promise} + * @returns {Promise} */ async scanInteractive(start, filter, iter, fullLock = false) { @@ -2311,7 +2323,7 @@ class Chain extends AsyncEmitter { * address and name hashes. * @param {ScanInteractiveIterCB} iter - Iterator. * @param {Boolean} [lockPerScan=true] - if we should lock per block scan. - * @returns {Promise} + * @returns {Promise} */ async _scanInteractive(start, filter, iter, lockPerScan = true) { @@ -2386,7 +2398,7 @@ class Chain extends AsyncEmitter { * @param {Block} block * @param {Number?} flags * @param {Number?} id - * @returns {Promise} + * @returns {Promise} */ async add(block, flags, id) { @@ -2405,14 +2417,14 @@ class Chain extends AsyncEmitter { * @param {Block} block * @param {Number?} flags * @param {Number?} id - * @returns {Promise} + * @returns {Promise} */ async _add(block, flags, id) { const hash = block.hash(); if (flags == null) - flags = common.flags.DEFAULT_FLAGS; + flags = common.DEFAULT_FLAGS; if (id == null) id = -1; @@ -2480,7 +2492,7 @@ class Chain extends AsyncEmitter { * @param {ChainEntry} prev * @param {Block} block * @param {Number} flags - * @returns {Promise} + * @returns {Promise} */ async connect(prev, block, flags) { @@ -2916,7 +2928,7 @@ class Chain extends AsyncEmitter { /** * Retrieve a chain entry by height. * @param {Number} height - * @returns {Promise} - Returns {@link ChainEntry}. + * @returns {Promise} */ getEntryByHeight(height) { @@ -2926,7 +2938,7 @@ class Chain extends AsyncEmitter { /** * Retrieve a chain entry by hash. * @param {Hash} hash - * @returns {Promise} - Returns {@link ChainEntry}. + * @returns {Promise} */ getEntryByHash(hash) { @@ -2937,7 +2949,7 @@ class Chain extends AsyncEmitter { * Get the hash of a block by height. Note that this * will only return hashes in the main chain. * @param {Number} height - * @returns {Promise} - Returns {@link Hash}. + * @returns {Promise} */ getHash(height) { @@ -2947,7 +2959,7 @@ class Chain extends AsyncEmitter { /** * Get the height of a block by hash. * @param {Hash} hash - * @returns {Promise} - Returns Number. + * @returns {Promise} */ getHeight(hash) { @@ -2957,7 +2969,7 @@ class Chain extends AsyncEmitter { /** * Test the chain to see if it contains a block. * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ hasEntry(hash) { @@ -2967,7 +2979,7 @@ class Chain extends AsyncEmitter { /** * Get the _next_ block hash (does not work by height). * @param {Hash} hash - * @returns {Promise} - Returns {@link Hash}. + * @returns {Promise} */ getNextHash(hash) { @@ -2977,7 +2989,7 @@ class Chain extends AsyncEmitter { /** * Check whether coins are still unspent. * @param {TX} tx - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ hasCoins(tx) { @@ -2986,7 +2998,7 @@ class Chain extends AsyncEmitter { /** * Get all tip hashes. - * @returns {Promise} - Returns {@link Hash}[]. + * @returns {Promise} */ getTips() { @@ -3017,9 +3029,8 @@ class Chain extends AsyncEmitter { /** * Get a coin (unspents only). - * @private * @param {Outpoint} prevout - * @returns {Promise} - Returns {@link CoinEntry}. + * @returns {Promise} */ readCoin(prevout) { @@ -3030,7 +3041,7 @@ class Chain extends AsyncEmitter { * Get a coin (unspents only). * @param {Hash} hash * @param {Number} index - * @returns {Promise} - Returns {@link Coin}. + * @returns {Promise} */ getCoin(hash, index) { @@ -3040,7 +3051,7 @@ class Chain extends AsyncEmitter { /** * Retrieve a block from the database (not filled with coins). * @param {Hash} hash - * @returns {Promise} - Returns {@link Block}. + * @returns {Promise} */ getBlock(hash) { @@ -3049,18 +3060,18 @@ class Chain extends AsyncEmitter { /** * Retrieve a block from the database (not filled with coins). - * @param {Hash} hash - * @returns {Promise} - Returns {@link Block}. + * @param {Hash|Number} hashHeight + * @returns {Promise} */ - getRawBlock(block) { - return this.db.getRawBlock(block); + getRawBlock(hashHeight) { + return this.db.getRawBlock(hashHeight); } /** * Get a historical block coin viewpoint. - * @param {Block} hash - * @returns {Promise} - Returns {@link CoinView}. + * @param {Block} block + * @returns {Promise} */ getBlockView(block) { @@ -3070,7 +3081,7 @@ class Chain extends AsyncEmitter { /** * Get a transaction with metadata. * @param {Hash} hash - * @returns {Promise} - Returns {@link TXMeta}. + * @returns {Promise} */ getMeta(hash) { @@ -3080,7 +3091,7 @@ class Chain extends AsyncEmitter { /** * Retrieve a transaction. * @param {Hash} hash - * @returns {Promise} - Returns {@link TX}. + * @returns {Promise} */ getTX(hash) { @@ -3089,7 +3100,7 @@ class Chain extends AsyncEmitter { /** * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ hasTX(hash) { @@ -3099,7 +3110,7 @@ class Chain extends AsyncEmitter { /** * Get all coins pertinent to an address. * @param {Address[]} addrs - * @returns {Promise} - Returns {@link Coin}[]. + * @returns {Promise} */ getCoinsByAddress(addrs) { @@ -3109,7 +3120,7 @@ class Chain extends AsyncEmitter { /** * Get all transaction hashes to an address. * @param {Address[]} addrs - * @returns {Promise} - Returns {@link Hash}[]. + * @returns {Promise} */ getHashesByAddress(addrs) { @@ -3119,7 +3130,7 @@ class Chain extends AsyncEmitter { /** * Get all transactions pertinent to an address. * @param {Address[]} addrs - * @returns {Promise} - Returns {@link TX}[]. + * @returns {Promise} */ getTXByAddress(addrs) { @@ -3129,7 +3140,7 @@ class Chain extends AsyncEmitter { /** * Get all transactions pertinent to an address. * @param {Address[]} addrs - * @returns {Promise} - Returns {@link TXMeta}[]. + * @returns {Promise} */ getMetaByAddress(addrs) { @@ -3139,7 +3150,7 @@ class Chain extends AsyncEmitter { /** * Get an orphan block. * @param {Hash} hash - * @returns {Block} + * @returns {Block?} */ getOrphan(hash) { @@ -3149,7 +3160,7 @@ class Chain extends AsyncEmitter { /** * Test the chain to see if it contains an orphan. * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Boolean} */ hasOrphan(hash) { @@ -3159,7 +3170,7 @@ class Chain extends AsyncEmitter { /** * Test the chain to see if it contains a pending block in its queue. * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Boolean} - Returns Boolean. */ hasPending(hash) { @@ -3169,7 +3180,7 @@ class Chain extends AsyncEmitter { /** * Get coin viewpoint. * @param {TX} tx - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ getCoinView(tx) { @@ -3179,7 +3190,7 @@ class Chain extends AsyncEmitter { /** * Get coin viewpoint (spent). * @param {TX} tx - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ async getSpentView(tx) { @@ -3252,7 +3263,7 @@ class Chain extends AsyncEmitter { * @param {Hash?} start - Height or hash to treat as the tip. * The current tip will be used if not present. Note that this can be a * non-existent hash, which is useful for headers-first locators. - * @returns {Promise} - Returns {@link Hash}[]. + * @returns {Promise} */ async getLocator(start) { @@ -3268,7 +3279,7 @@ class Chain extends AsyncEmitter { * Calculate chain locator without a lock. * @private * @param {Hash?} start - * @returns {Promise} + * @returns {Promise} */ async _getLocator(start) { @@ -3386,7 +3397,7 @@ class Chain extends AsyncEmitter { /** * Get median block by timestamp. * @param {ChainEntry} prev - * @returns {Promise} + * @returns {Promise} */ async getSuitableBlock(prev) { @@ -3414,7 +3425,7 @@ class Chain extends AsyncEmitter { * Calculate the next target. * @param {Number} time - Next block timestamp. * @param {ChainEntry} prev - Previous entry. - * @returns {Promise} - returns Number + * @returns {Promise} - returns Number * (target is in compact/mantissa form). */ @@ -3511,8 +3522,7 @@ class Chain extends AsyncEmitter { /** * Find a locator. Analagous to bitcoind's `FindForkInGlobalIndex()`. * @param {Hash[]} locator - Hashes. - * @returns {Promise} - Returns {@link Hash} (the - * hash of the latest known block). + * @returns {Promise} (the hash of the latest known block). */ async findLocator(locator) { @@ -3531,7 +3541,7 @@ class Chain extends AsyncEmitter { * @see https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki * @param {ChainEntry} prev - Previous chain entry. * @param {Object} deployment - Deployment. - * @returns {Promise} - Returns Number. + * @returns {Promise} */ async isActive(prev, deployment) { @@ -3546,7 +3556,7 @@ class Chain extends AsyncEmitter { * @see https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki * @param {ChainEntry} prev - Previous chain entry. * @param {Object} deployment - Deployment. - * @returns {Promise} - Returns Number. + * @returns {Promise} */ async getState(prev, deployment) { @@ -3669,8 +3679,8 @@ class Chain extends AsyncEmitter { /** * Get signalling statistics for BIP9/versionbits soft fork * @param {ChainEntry} prev - Previous chain entry. - * @param {Obejct} deployment - Deployment. - * @returns {Promise} - Returns JSON object. + * @param {Object} deployment - Deployment. + * @returns {Promise} */ async getBIP9Stats(prev, deployment) { @@ -3714,7 +3724,7 @@ class Chain extends AsyncEmitter { * Compute the version for a new block (BIP9: versionbits). * @see https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki * @param {ChainEntry} prev - Previous chain entry (usually the tip). - * @returns {Promise} - Returns Number. + * @returns {Promise} */ async computeBlockVersion(prev) { @@ -3737,7 +3747,7 @@ class Chain extends AsyncEmitter { /** * Get the current deployment state of the chain. Called on load. * @private - * @returns {Promise} - Returns {@link DeploymentState}. + * @returns {Promise} */ async getDeploymentState() { @@ -3747,7 +3757,8 @@ class Chain extends AsyncEmitter { /** * Get deployment state. * @private - * @returns {Promise} - Returns {@link DeploymentState}. + * @param {ChainEntry} tip + * @returns {Promise} */ async readDeploymentState(tip) { @@ -3766,8 +3777,7 @@ class Chain extends AsyncEmitter { /** * Get the next deployment state of the chain. - * @private - * @returns {Promise} - Returns {@link DeploymentState}. + * @returns {Promise} */ async getNextState() { @@ -3876,7 +3886,7 @@ class Chain extends AsyncEmitter { /** * Get safe tree root. - * @returns {Hash} + * @returns {Promise} */ async getSafeRoot() { @@ -4096,14 +4106,15 @@ class ChainOptions { class DeploymentState { /** * Create a deployment state. + * @param {Hash} tip * @constructor */ constructor(tip) { this.tip = tip; this.flags = Script.flags.MANDATORY_VERIFY_FLAGS; - this.lockFlags = common.lockFlags.MANDATORY_LOCKTIME_FLAGS; - this.nameFlags = rules.nameFlags.MANDATORY_VERIFY_COVENANT_FLAGS; + this.lockFlags = common.MANDATORY_LOCKTIME_FLAGS; + this.nameFlags = rules.MANDATORY_VERIFY_COVENANT_FLAGS; } hasHardening() { @@ -4146,4 +4157,6 @@ function cmp(a, b) { * Expose */ +Chain.ChainOptions = ChainOptions; + module.exports = Chain; diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 1e337165..9e70ec78 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -37,6 +37,14 @@ const { ChainFlags } = require('./records'); +/** @typedef {import('urkel').Proof} Proof */ +/** @typedef {ReturnType} Batch */ +/** @typedef {import('@handshake-org/bfilter').BloomFilter} BloomFilter */ +/** @typedef {import('../types').Hash} Hash */ +/** @typedef {import('./chain').ChainOptions} ChainOptions */ +/** @typedef {import('../primitives/tx')} TX */ +/** @typedef {import('../primitives/coin')} Coin */ + /** * ChainDB * @alias module:blockchain.ChainDB @@ -75,13 +83,15 @@ class ChainDB { this.current = null; this.blocksBatch = null; + /** @type {LRU} */ this.cacheHash = new LRU(this.options.entryCache, null, BufferMap); + /** @type {LRU} */ this.cacheHeight = new LRU(this.options.entryCache); } /** * Open and wait for the database to load. - * @returns {Promise} + * @returns {Promise} */ async open() { @@ -174,7 +184,7 @@ class ChainDB { /** * Initialize fresh database. - * @returns {Promise} + * @returns {Promise} */ async _initialize() { @@ -202,8 +212,8 @@ class ChainDB { /** * Verify version - * @param {Number} - * @returns {Promise} + * @param {Number} version + * @returns {Promise} */ async verifyVersion(version) { @@ -225,7 +235,7 @@ class ChainDB { /** * Close and wait for the database to close. - * @returns {Promise} + * @returns {Promise} */ async close() { @@ -258,7 +268,7 @@ class ChainDB { /** * Put key and value to current batch. - * @param {String} key + * @param {Buffer} key * @param {Buffer} value */ @@ -269,7 +279,7 @@ class ChainDB { /** * Delete key from current batch. - * @param {String} key + * @param {Buffer} key */ del(key) { @@ -289,7 +299,6 @@ class ChainDB { /** * Drop current batch. - * @returns {Batch} */ drop() { @@ -318,7 +327,7 @@ class ChainDB { /** * Commit current batch. - * @returns {Promise} + * @returns {Promise} */ async commit() { @@ -398,7 +407,7 @@ class ChainDB { /** * Get the height of a block by hash. * @param {Hash} hash - * @returns {Promise} - Returns Number. + * @returns {Promise} */ async getHeight(hash) { @@ -420,14 +429,14 @@ class ChainDB { if (!height) return -1; - return height.readUInt32LE(0, true); + return height.readUInt32LE(0); } /** * Get the hash of a block by height. Note that this * will only return hashes in the main chain. - * @param {Number} height - * @returns {Promise} - Returns {@link Hash}. + * @param {Hash|Number} height + * @returns {Promise} */ async getHash(height) { @@ -450,7 +459,7 @@ class ChainDB { /** * Retrieve a chain entry by height. * @param {Number} height - * @returns {Promise} - Returns {@link ChainEntry}. + * @returns {Promise} */ async getEntryByHeight(height) { @@ -487,7 +496,7 @@ class ChainDB { /** * Retrieve a chain entry by hash. * @param {Hash} hash - * @returns {Promise} - Returns {@link ChainEntry}. + * @returns {Promise} */ async getEntryByHash(hash) { @@ -506,6 +515,7 @@ class ChainDB { if (!raw) return null; + /** @type {ChainEntry} */ const entry = ChainEntry.decode(raw); // There's no efficient way to check whether @@ -519,7 +529,7 @@ class ChainDB { /** * Retrieve a chain entry. * @param {Number|Hash} block - Height or hash. - * @returns {Promise} - Returns {@link ChainEntry}. + * @returns {Promise} */ getEntry(block) { @@ -531,7 +541,7 @@ class ChainDB { /** * Test whether the chain contains a block. * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ async hasEntry(hash) { @@ -543,7 +553,7 @@ class ChainDB { * Get ancestor by `height`. * @param {ChainEntry} entry * @param {Number} height - * @returns {Promise} - Returns ChainEntry. + * @returns {Promise} */ async getAncestor(entry, height) { @@ -573,7 +583,7 @@ class ChainDB { /** * Get previous entry. * @param {ChainEntry} entry - * @returns {Promise} - Returns ChainEntry. + * @returns {Promise} */ getPrevious(entry) { @@ -583,7 +593,7 @@ class ChainDB { /** * Get previous cached entry. * @param {ChainEntry} entry - * @returns {ChainEntry|null} + * @returns {ChainEntry?} */ getPrevCache(entry) { @@ -593,7 +603,7 @@ class ChainDB { /** * Get next entry. * @param {ChainEntry} entry - * @returns {Promise} - Returns ChainEntry. + * @returns {Promise} */ async getNext(entry) { @@ -608,7 +618,7 @@ class ChainDB { /** * Get next entry. * @param {ChainEntry} entry - * @returns {Promise} - Returns ChainEntry. + * @returns {Promise} */ async getNextEntry(entry) { @@ -628,7 +638,7 @@ class ChainDB { * Lookup a name tree value. * @param {Hash} root * @param {Hash} key - * @returns {Buffer} + * @returns {Promise} */ async lookup(root, key) { @@ -643,7 +653,7 @@ class ChainDB { * Create a name tree proof. * @param {Hash} root * @param {Hash} key - * @returns {Buffer[]} nodes + * @returns {Promise} nodes */ async prove(root, key) { @@ -665,7 +675,7 @@ class ChainDB { /** * Retrieve the tip entry from the tip record. - * @returns {Promise} - Returns {@link ChainEntry}. + * @returns {Promise} */ getTip() { @@ -674,7 +684,7 @@ class ChainDB { /** * Retrieve the tip entry from the tip record. - * @returns {Promise} - Returns {@link ChainState}. + * @returns {Promise} */ async getState() { @@ -688,7 +698,7 @@ class ChainDB { /** * Retrieve tree state from the tree record. - * @returns {Promise} + * @returns {Promise} */ async getTreeState() { @@ -702,7 +712,7 @@ class ChainDB { /** * Write genesis block to database. - * @returns {Promise} + * @returns {Promise} */ async writeGenesis() { @@ -733,7 +743,7 @@ class ChainDB { /** * Retrieve the database flags. - * @returns {Promise} - Returns {@link ChainFlags}. + * @returns {Promise} */ async getFlags() { @@ -747,7 +757,7 @@ class ChainDB { /** * Verify current options against db options. - * @returns {Promise} + * @returns {Promise} */ async verifyFlags() { @@ -787,7 +797,7 @@ class ChainDB { /** * Get state caches. - * @returns {Promise} - Returns {@link StateCache}. + * @returns {Promise} */ async getStateCache() { @@ -795,8 +805,7 @@ class ChainDB { const items = await this.db.range({ gte: layout.v.min(), - lte: layout.v.max(), - values: true + lte: layout.v.max() }); for (const item of items) { @@ -810,7 +819,7 @@ class ChainDB { /** * Save deployment table. - * @returns {Promise} + * @returns {Promise} */ saveDeployments() { @@ -821,7 +830,7 @@ class ChainDB { /** * Save deployment table. - * @returns {Promise} + * @param {Batch} b */ writeDeployments(b) { @@ -843,7 +852,7 @@ class ChainDB { /** * Check for outdated deployments. * @private - * @returns {Promise} + * @returns {Promise} */ async checkDeployments() { @@ -879,7 +888,7 @@ class ChainDB { /** * Potentially invalidate state cache. - * @returns {Promise} + * @returns {Promise} */ async verifyDeployments() { @@ -916,7 +925,9 @@ class ChainDB { /** * Invalidate state cache. * @private - * @returns {Promise} + * @param {Number} bit + * @param {Batch} b + * @returns {Promise} */ async invalidateCache(bit, b) { @@ -931,7 +942,7 @@ class ChainDB { /** * Retroactively prune the database. - * @returns {Promise} + * @returns {Promise} */ async prune() { @@ -991,7 +1002,7 @@ class ChainDB { * Removes all historical state and all data not * linked directly to the provided root node hash. * @param {ChainEntry} entry - * @returns {Promise} + * @returns {Promise} */ async compactTree(entry) { @@ -1014,7 +1025,9 @@ class ChainDB { const tmpTree = new Tree({ hash: blake2b, bits: 256, - prefix: tmpDir + prefix: tmpDir, + cacheOnly: false, + initCacheSize: 0 }); // Make sure to remove the tmp directory first. @@ -1049,7 +1062,7 @@ class ChainDB { /** * Get the _next_ block hash (does not work by height). * @param {Hash} hash - * @returns {Promise} - Returns {@link Hash}. + * @returns {Promise} */ async getNextHash(hash) { @@ -1059,7 +1072,7 @@ class ChainDB { /** * Check to see if a block is on the main chain. * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ async isMainHash(hash) { @@ -1091,7 +1104,7 @@ class ChainDB { /** * Test whether the entry is in the main chain. * @param {ChainEntry} entry - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ async isMainChain(entry) { @@ -1161,7 +1174,7 @@ class ChainDB { /** * Get all tip hashes. - * @returns {Promise} - Returns {@link Hash}[]. + * @returns {Promise} */ async getTips() { @@ -1174,7 +1187,7 @@ class ChainDB { /** * Get bitfield. - * @returns {Promise} + * @returns {Promise} */ async getField() { @@ -1189,7 +1202,7 @@ class ChainDB { /** * Get a coin (unspents only). * @param {Outpoint} prevout - * @returns {Promise} - Returns {@link CoinEntry}. + * @returns {Promise} */ async readCoin(prevout) { @@ -1210,7 +1223,7 @@ class ChainDB { * Get a coin (unspents only). * @param {Hash} hash * @param {Number} index - * @returns {Promise} - Returns {@link Coin}. + * @returns {Promise} */ async getCoin(hash, index) { @@ -1227,7 +1240,7 @@ class ChainDB { * Check whether coins are still unspent. Necessary for bip30. * @see https://bitcointalk.org/index.php?topic=67738.0 * @param {TX} tx - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ async hasCoins(tx) { @@ -1242,7 +1255,7 @@ class ChainDB { /** * Get coin viewpoint. * @param {TX} tx - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ async getCoinView(tx) { @@ -1261,7 +1274,7 @@ class ChainDB { /** * Get coin viewpoint (historical). * @param {TX} tx - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ async getSpentView(tx) { @@ -1289,7 +1302,7 @@ class ChainDB { /** * Get coins necessary to be resurrected during a reorg. * @param {Hash} hash - * @returns {Promise} - Returns {@link Coin}[]. + * @returns {Promise} */ async getUndoCoins(hash) { @@ -1356,7 +1369,7 @@ class ChainDB { /** * Retrieve a block from the database (not filled with coins). * @param {Hash} hash - * @returns {Promise} - Returns {@link Block}. + * @returns {Promise} */ async getBlock(hash) { @@ -1370,15 +1383,15 @@ class ChainDB { /** * Retrieve a block from the database (not filled with coins). - * @param {Hash} hash - * @returns {Promise} - Returns {@link Block}. + * @param {Hash|Number} hashHeight + * @returns {Promise} */ - async getRawBlock(block) { + async getRawBlock(hashHeight) { if (this.options.spv) return null; - const hash = await this.getHash(block); + const hash = await this.getHash(hashHeight); if (!hash) return null; @@ -1388,8 +1401,8 @@ class ChainDB { /** * Get a historical block coin viewpoint. - * @param {Block} hash - * @returns {Promise} - Returns {@link CoinView}. + * @param {Block} block + * @returns {Promise} */ async getBlockView(block) { @@ -1417,7 +1430,7 @@ class ChainDB { /** * Get a transaction with metadata. * @param {Hash} hash - * @returns {Promise} - Returns {@link TXMeta}. + * @returns {Promise} */ async getMeta(hash) { @@ -1435,7 +1448,7 @@ class ChainDB { /** * Retrieve a transaction. * @param {Hash} hash - * @returns {Promise} - Returns {@link TX}. + * @returns {Promise} */ async getTX(hash) { @@ -1449,7 +1462,7 @@ class ChainDB { /** * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ async hasTX(hash) { @@ -1462,7 +1475,7 @@ class ChainDB { /** * Get all coins pertinent to an address. * @param {Address[]} addrs - * @returns {Promise} - Returns {@link Coin}[]. + * @returns {Promise} */ async getCoinsByAddress(addrs) { @@ -1502,7 +1515,7 @@ class ChainDB { /** * Get all transaction hashes to an address. * @param {Address[]} addrs - * @returns {Promise} - Returns {@link Hash}[]. + * @returns {Promise} */ async getHashesByAddress(addrs) { @@ -1530,7 +1543,7 @@ class ChainDB { /** * Get all transactions pertinent to an address. * @param {Address[]} addrs - * @returns {Promise} - Returns {@link TX}[]. + * @returns {Promise} */ async getTXByAddress(addrs) { @@ -1546,7 +1559,7 @@ class ChainDB { /** * Get all transactions pertinent to an address. * @param {Address[]} addrs - * @returns {Promise} - Returns {@link TXMeta}[]. + * @returns {Promise} */ async getMetaByAddress(addrs) { @@ -1570,10 +1583,10 @@ class ChainDB { /** * Scan the blockchain for transactions containing specified address hashes. - * @param {Hash} start - Block hash to start at. - * @param {Bloom} filter - Bloom filter containing tx and address hashes. + * @param {Hash|Number} start - Block hash or height to start at. + * @param {BloomFilter} filter - Bloomfilter containing tx and address hashes. * @param {Function} iter - Iterator. - * @returns {Promise} + * @returns {Promise} */ async scan(start, filter, iter) { @@ -1685,8 +1698,8 @@ class ChainDB { * instead performed in {@link Chain#add}. * @param {ChainEntry} entry * @param {Block} block - * @param {CoinView?} view - Will not connect if null. - * @returns {Promise} + * @param {CoinView?} [view] - Will not connect if null. + * @returns {Promise} */ async save(entry, block, view) { @@ -1705,8 +1718,8 @@ class ChainDB { * @private * @param {ChainEntry} entry * @param {Block} block - * @param {CoinView?} view - * @returns {Promise} + * @param {CoinView?} [view] + * @returns {Promise} */ async _save(entry, block, view) { @@ -1752,7 +1765,7 @@ class ChainDB { * @param {ChainEntry} entry * @param {Block} block * @param {CoinView} view - * @returns {Promise} + * @returns {Promise} */ async reconnect(entry, block, view) { @@ -1772,7 +1785,7 @@ class ChainDB { * @param {ChainEntry} entry * @param {Block} block * @param {CoinView} view - * @returns {Promise} + * @returns {Promise} */ async _reconnect(entry, block, view) { @@ -1804,7 +1817,7 @@ class ChainDB { * Disconnect block from the chain. * @param {ChainEntry} entry * @param {Block} block - * @returns {Promise} + * @returns {Promise} */ async disconnect(entry, block) { @@ -1828,7 +1841,7 @@ class ChainDB { * @private * @param {ChainEntry} entry * @param {Block} block - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ async _disconnect(entry, block) { @@ -1854,6 +1867,7 @@ class ChainDB { /** * Save state cache updates. * @private + * @returns {void} */ saveUpdates() { @@ -1874,7 +1888,7 @@ class ChainDB { * Reset the chain to a height or hash. Useful for replaying * the blockchain download for SPV. * @param {Hash|Number} block - hash/height - * @returns {Promise} + * @returns {Promise} */ async reset(block) { @@ -1952,7 +1966,7 @@ class ChainDB { /** * Remove all alternate chains. - * @returns {Promise} + * @returns {Promise} */ async removeChains() { @@ -1977,7 +1991,7 @@ class ChainDB { * Remove an alternate chain. * @private * @param {Hash} hash - Alternate chain tip. - * @returns {Promise} + * @returns {Promise} */ async _removeChain(hash) { @@ -2016,8 +2030,8 @@ class ChainDB { * database and potentially connect the inputs. * @param {ChainEntry} entry * @param {Block} block - * @param {CoinView?} view - * @returns {Promise} - Returns {@link Block}. + * @param {CoinView?} [view] + * @returns {Promise} */ async saveBlock(entry, block, view) { @@ -2039,7 +2053,7 @@ class ChainDB { * Remove a block (not an entry) to the database. * Disconnect inputs. * @param {ChainEntry} entry - * @returns {Promise} - Returns {@link Block}. + * @returns {Promise} */ async removeBlock(entry) { @@ -2083,7 +2097,6 @@ class ChainDB { /** * Commit names to tree. - * @private * @param {CoinView} view * @param {ChainEntry} entry * @param {Boolean} revert @@ -2106,6 +2119,7 @@ class ChainDB { * @param {CoinView} view * @param {ChainEntry} entry * @param {Boolean} revert + * @returns {Promise} */ async _saveNames(view, entry, revert) { @@ -2173,6 +2187,7 @@ class ChainDB { * @private * @param {CoinView} view * @param {ChainEntry} entry + * @returns {Promise} */ async disconnectNames(view, entry) { @@ -2198,7 +2213,7 @@ class ChainDB { * @param {ChainEntry} entry * @param {Block} block * @param {CoinView} view - * @returns {Promise} - Returns {@link Block}. + * @returns {Promise} */ async connectBlock(entry, block, view) { @@ -2275,7 +2290,7 @@ class ChainDB { * Disconnect block inputs. * @param {ChainEntry} entry * @param {Block} block - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ async disconnectBlock(entry, block) { @@ -2365,7 +2380,7 @@ class ChainDB { * add current block to the prune queue. * @private * @param {ChainEntry} entry - * @returns {Promise} + * @returns {Promise} */ async pruneBlock(entry) { @@ -2391,7 +2406,7 @@ class ChainDB { /** * Save database options. - * @returns {Promise} + * @returns {Promise} */ saveFlags() { @@ -2516,7 +2531,7 @@ class ChainDB { function fromU32(num) { const data = Buffer.allocUnsafe(4); - data.writeUInt32LE(num, 0, true); + data.writeUInt32LE(num, 0); return data; } diff --git a/lib/blockchain/chainentry.js b/lib/blockchain/chainentry.js index 2855a962..e9af6de8 100644 --- a/lib/blockchain/chainentry.js +++ b/lib/blockchain/chainentry.js @@ -14,6 +14,11 @@ const Headers = require('../primitives/headers'); const InvItem = require('../primitives/invitem'); const util = require('../utils/util'); +/** @typedef {import('../types').BufioWriter} BufioWriter */ +/** @typedef {import('../protocol/network')} Network */ +/** @typedef {import('../primitives/block')} Block */ +/** @typedef {import('../primitives/merkleblock')} MerkleBlock */ + /* * Constants */ @@ -69,8 +74,8 @@ class ChainEntry extends bio.Struct { /** * Inject properties from options. - * @private * @param {Object} options + * @returns {this} */ fromOptions(options) { @@ -126,6 +131,7 @@ class ChainEntry extends bio.Struct { /** * Calculate the chainwork by * adding proof to previous chainwork. + * @param {ChainEntry?} [prev] - Previous entry. * @returns {BN} chainwork */ @@ -169,9 +175,9 @@ class ChainEntry extends bio.Struct { /** * Inject properties from block. - * @private * @param {Block|MerkleBlock} block - * @param {ChainEntry} prev - Previous entry. + * @param {ChainEntry?} [prev] - Previous entry. + * @returns {this} */ fromBlock(block, prev) { @@ -203,7 +209,8 @@ class ChainEntry extends bio.Struct { /** * Serialize the entry to internal database format. - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -234,8 +241,7 @@ class ChainEntry extends bio.Struct { /** * Inject properties from serialized data. - * @private - * @param {Buffer} data + * @param {bio.BufferReader} br */ read(br) { @@ -291,8 +297,8 @@ class ChainEntry extends bio.Struct { /** * Inject properties from json object. - * @private * @param {Object} json + * @returns {this} */ fromJSON(json) { @@ -355,7 +361,7 @@ class ChainEntry extends bio.Struct { /** * Instantiate chainentry from block. * @param {Block|MerkleBlock} block - * @param {ChainEntry} prev - Previous entry. + * @param {ChainEntry?} [prev] - Previous entry. * @returns {ChainEntry} */ diff --git a/lib/blockchain/common.js b/lib/blockchain/common.js index 6a9ab833..fae1ed3d 100644 --- a/lib/blockchain/common.js +++ b/lib/blockchain/common.js @@ -6,6 +6,9 @@ 'use strict'; +/** @typedef {import('@handshake-org/bfilter').BloomFilter} BloomFilter */ +/** @typedef {import('../types').LockFlags} LockFlags */ + /** * @module blockchain/common */ @@ -23,7 +26,7 @@ exports.lockFlags = {}; * @default */ -exports.lockFlags.MANDATORY_LOCKTIME_FLAGS = 0; +exports.MANDATORY_LOCKTIME_FLAGS = 0; /** * Standard locktime flags (used for mempool validation). @@ -31,8 +34,8 @@ exports.lockFlags.MANDATORY_LOCKTIME_FLAGS = 0; * @default */ -exports.lockFlags.STANDARD_LOCKTIME_FLAGS = 0 - | exports.lockFlags.MANDATORY_LOCKTIME_FLAGS; +exports.STANDARD_LOCKTIME_FLAGS = 0 + | exports.MANDATORY_LOCKTIME_FLAGS; /** * Threshold states for versionbits @@ -66,7 +69,7 @@ exports.flags = { * @default */ -exports.flags.DEFAULT_FLAGS = 0 +exports.DEFAULT_FLAGS = 0 | exports.flags.VERIFY_POW | exports.flags.VERIFY_BODY; diff --git a/lib/blockchain/migrations.js b/lib/blockchain/migrations.js index aab81dd9..c0835b4e 100644 --- a/lib/blockchain/migrations.js +++ b/lib/blockchain/migrations.js @@ -25,6 +25,8 @@ const { } = require('../migrations/migrator'); /** @typedef {import('../types').Hash} Hash */ +/** @typedef {ReturnType} Batch */ +/** @typedef {import('../migrations/migrator').types} MigrationType */ /** * Switch to new migrations layout. @@ -46,6 +48,10 @@ class MigrateMigrations extends AbstractMigration { this.layout = MigrateMigrations.layout(); } + /** + * @returns {Promise} + */ + async check() { return types.MIGRATE; } @@ -59,8 +65,8 @@ class MigrateMigrations extends AbstractMigration { async migrate(b) { this.logger.info('Migrating migrations..'); - const oldLayout = this.layout.oldLayout.wdb; - const newLayout = this.layout.newLayout.wdb; + const oldLayout = this.layout.oldLayout; + const newLayout = this.layout.newLayout; let nextMigration = 1; const skipped = []; @@ -83,10 +89,17 @@ class MigrateMigrations extends AbstractMigration { } this.db.writeVersion(b, 2); - b.put(newLayout.M.encode(), - this.encodeMigrationState(nextMigration, skipped)); + + const rawState = this.encodeMigrationState(nextMigration, skipped); + b.put(newLayout.M.encode(), rawState); } + /** + * @param {Number} nextMigration + * @param {Number[]} skipped + * @returns {Buffer} + */ + encodeMigrationState(nextMigration, skipped) { let size = 4; size += encoding.sizeVarint(nextMigration); @@ -116,14 +129,10 @@ class MigrateMigrations extends AbstractMigration { static layout() { return { oldLayout: { - wdb: { - M: bdb.key('M', ['uint32']) - } + M: bdb.key('M', ['uint32']) }, newLayout: { - wdb: { - M: bdb.key('M') - } + M: bdb.key('M') } }; } @@ -138,7 +147,7 @@ class MigrateChainState extends AbstractMigration { /** * Create migration chain state * @constructor - * @param {ChainMigrator} options + * @param {ChainMigratorOptions} options */ constructor(options) { @@ -261,7 +270,7 @@ class MigrateChainState extends AbstractMigration { /** * Get Block (old layout) * @param {Hash} hash - * @returns {Promise} - Block + * @returns {Promise} */ async getBlock(hash) { @@ -276,7 +285,7 @@ class MigrateChainState extends AbstractMigration { /** * Get block view (old layout) - * @param {Hash} hash + * @param {Block} block * @returns {Promise} - UndoCoins */ @@ -369,7 +378,6 @@ class MigrateBlockStore extends AbstractMigration { /** * Migrate blocks and undo blocks - * @param {Batch} b * @returns {Promise} */ @@ -480,7 +488,7 @@ class MigrateTreeState extends AbstractMigration { /** * Create tree state migrator * @constructor - * @param {ChainMigrator} options + * @param {ChainMigratorOptions} options */ constructor(options) { @@ -498,6 +506,11 @@ class MigrateTreeState extends AbstractMigration { return types.MIGRATE; } + /** + * @param {Batch} b + * @returns {Promise} + */ + async migrate(b) { if (this.options.spv) { this.db.writeVersion(b, 3); @@ -672,13 +685,13 @@ class ChainMigratorOptions { assert(typeof options.prune === 'boolean'); this.prune = options.prune; } + + return this; } } -exports = ChainMigrator; - // List of the migrations with ids -exports.migrations = { +ChainMigrator.migrations = { 0: MigrateMigrations, 1: MigrateChainState, 2: MigrateBlockStore, @@ -686,9 +699,9 @@ exports.migrations = { }; // Expose migrations -exports.MigrateChainState = MigrateChainState; -exports.MigrateMigrations = MigrateMigrations; -exports.MigrateBlockStore = MigrateBlockStore; -exports.MigrateTreeState = MigrateTreeState; +ChainMigrator.MigrateChainState = MigrateChainState; +ChainMigrator.MigrateMigrations = MigrateMigrations; +ChainMigrator.MigrateBlockStore = MigrateBlockStore; +ChainMigrator.MigrateTreeState = MigrateTreeState; -module.exports = exports; +module.exports = ChainMigrator; diff --git a/lib/covenants/rules.js b/lib/covenants/rules.js index 9156d8d9..dbb763d9 100644 --- a/lib/covenants/rules.js +++ b/lib/covenants/rules.js @@ -20,6 +20,7 @@ const rules = exports; /** @typedef {import('../types').Amount} AmountValue */ /** @typedef {import('../types').Hash} Hash */ +/** @typedef {import('../types').NameFlags} NameFlags */ /** @typedef {import('../protocol/network')} Network */ /** @typedef {import('../primitives/tx')} TX */ /** @typedef {import('../coins/coinview')} CoinView */ @@ -130,11 +131,11 @@ rules.nameFlags = { /** * Standard verify flags for covenants. - * @const {NameFlags} + * @const {rules.NameFlags} * @default */ -rules.nameFlags.MANDATORY_VERIFY_COVENANT_FLAGS = 0; +rules.MANDATORY_VERIFY_COVENANT_FLAGS = 0; /** * Maximum covenant size. diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index afcd7574..8768cb47 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -448,7 +448,7 @@ class Mempool extends EventEmitter { const state = await this.getNextState(); const mtp = await this.chain.getMedianTime(this.chain.tip); const remove = []; - const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS; + const lockFlags = common.STANDARD_LOCKTIME_FLAGS; this.contracts.clear(); @@ -1486,7 +1486,7 @@ class Mempool extends EventEmitter { async insertTX(tx, id) { assert(!tx.mutable, 'Cannot add mutable TX to mempool.'); - const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS; + const lockFlags = common.STANDARD_LOCKTIME_FLAGS; const height = this.chain.height; const hash = tx.hash(); @@ -1604,7 +1604,7 @@ class Mempool extends EventEmitter { const network = this.network; const height = this.chain.height + 1; const state = await this.getNextState(); - const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS; + const lockFlags = common.STANDARD_LOCKTIME_FLAGS; const tx = entry.tx; // Verify sequence locks. diff --git a/lib/migrations/migration.js b/lib/migrations/migration.js index dacde6c2..5f387804 100644 --- a/lib/migrations/migration.js +++ b/lib/migrations/migration.js @@ -5,6 +5,10 @@ 'use strict'; +/** @typedef {import('bdb').DB} DB */ +/** @typedef {ReturnType} Batch */ +/** @typedef {import('./migrator').types} MigrationType */ + /** * Abstract class for single migration. * @alias module:migrations.AbstractMigration @@ -23,7 +27,7 @@ class AbstractMigration { /** * Check if the migration applies to the database - * @returns {Promise} + * @returns {Promise} */ async check() { @@ -32,11 +36,11 @@ class AbstractMigration { /** * Run the actual migration - * @param {Batch} batch + * @param {Batch} b * @returns {Promise} */ - async migrate() { + async migrate(b) { throw new Error('Abstract method.'); } diff --git a/lib/migrations/migrator.js b/lib/migrations/migrator.js index 8f32201c..5a432b91 100644 --- a/lib/migrations/migrator.js +++ b/lib/migrations/migrator.js @@ -10,6 +10,8 @@ const Logger = require('blgr'); const bdb = require('bdb'); const MigrationState = require('../migrations/state'); +/** @typedef {ReturnType} Batch */ + /** * This entry needs to be part of all dbs that support migrations. * V -> DB Version @@ -29,6 +31,12 @@ const oldLayout = { M: bdb.key('M', ['uint32']) }; +/** + * Migration types. + * @enum {Number} + * @default + */ + const types = { MIGRATE: 0, SKIP: 1, @@ -42,14 +50,24 @@ const types = { class MigrationResult { constructor() { + /** @type {Set} */ this.migrated = new Set(); + /** @type {Set} */ this.skipped = new Set(); } + /** + * @param {Number} id + */ + skip(id) { this.skipped.add(id); } + /** + * @param {Number} id + */ + migrate(id) { this.migrated.add(id); } @@ -90,6 +108,7 @@ class Migrator { /** * Recheck options + * @param {Object} options * @private */ diff --git a/lib/migrations/state.js b/lib/migrations/state.js index 667e7bcd..dce7d5be 100644 --- a/lib/migrations/state.js +++ b/lib/migrations/state.js @@ -8,6 +8,8 @@ const assert = require('bsert'); const bio = require('bufio'); const {encoding} = bio; +/** @typedef {import('../types').BufioWriter} BufioWriter */ + /** * State of database migrations. * Because migration IDs are only increasing, we only need @@ -32,6 +34,7 @@ class MigrationState extends bio.Struct { this.inProgress = false; this.nextMigration = 0; + /** @type {Number[]} */ this.skipped = []; } @@ -72,8 +75,8 @@ class MigrationState extends bio.Struct { /** * Serialize migration state. - * @param {BufferWriter} bw - * @returns {BufferWriter} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -94,8 +97,8 @@ class MigrationState extends bio.Struct { /** * Deserialize migration state. - * @param {BufferReader} - * @returns {MigrationState} + * @param {bio.BufferReader} br + * @returns {this} */ read(br) { @@ -108,7 +111,7 @@ class MigrationState extends bio.Struct { const skippedItems = br.readVarint(); for (let i = 0; i < skippedItems; i++) - this.skipped.push(br.readVarint(0)); + this.skipped.push(br.readVarint()); return this; } diff --git a/lib/net/pool.js b/lib/net/pool.js index a0b72cfd..fc1b64bc 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -2353,7 +2353,7 @@ class Pool extends EventEmitter { */ async handleBlock(peer, packet) { - const flags = chainCommon.flags.DEFAULT_FLAGS; + const flags = chainCommon.DEFAULT_FLAGS; if (this.options.spv) { this.logger.warning( @@ -4280,7 +4280,7 @@ class Pool extends EventEmitter { /** * Resolve a name at the "safe" Urkel Tree root. * @param {Buffer} nameHash - * @returns {Buffer} + * @returns {Promise} */ async resolve(nameHash) { @@ -4292,7 +4292,7 @@ class Pool extends EventEmitter { * Resolve a name given any Urkel Tree root. * @param {Buffer} nameHash * @param {Buffer} root - * @returns {Buffer} + * @returns {Promise} */ async resolveAtRoot(nameHash, root) { diff --git a/test/chain-full-test.js b/test/chain-full-test.js index 483096fc..544f47ff 100644 --- a/test/chain-full-test.js +++ b/test/chain-full-test.js @@ -388,7 +388,7 @@ describe('Chain', function() { }); it('should fail to connect bad tx merkle root', async () => { - const flags = common.flags.DEFAULT_FLAGS & ~common.flags.VERIFY_POW; + const flags = common.DEFAULT_FLAGS & ~common.flags.VERIFY_POW; const block = await cpu.mineBlock(); block.merkleRoot = Buffer.from(block.merkleRoot); @@ -614,7 +614,7 @@ describe('Chain', function() { // We are going to modify blocks after mining, // just to get UTXO for the next test. // These blocks don't have to be strictly 100% valid. - const flags = common.flags.DEFAULT_FLAGS & ~common.flags.VERIFY_POW; + const flags = common.DEFAULT_FLAGS & ~common.flags.VERIFY_POW; // Generate 80 blocks where each coinbase transaction // has 10 outputs to the 100-sigop address.