chain: cleanup some functions.

This commit is contained in:
Christopher Jeffrey 2017-02-02 11:28:25 -08:00
parent 0f1a62b0de
commit a443b88f92
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -220,7 +220,7 @@ Chain.prototype._close = function close() {
/** /**
* Perform all necessary contextual verification on a block. * Perform all necessary contextual verification on a block.
* @private * @private
* @param {Block|MerkleBlock} block * @param {Block} block
* @param {ChainEntry} prev * @param {ChainEntry} prev
* @returns {Promise} - Returns {@link ContextResult}. * @returns {Promise} - Returns {@link ContextResult}.
*/ */
@ -509,7 +509,7 @@ Chain.prototype.setDeploymentState = function setDeploymentState(state) {
* can skip this. * can skip this.
* @private * @private
* @see https://github.com/bitcoin/bips/blob/master/bip-0030.mediawiki * @see https://github.com/bitcoin/bips/blob/master/bip-0030.mediawiki
* @param {Block|MerkleBlock} block * @param {Block} block
* @param {ChainEntry} prev * @param {ChainEntry} prev
* @returns {Promise} * @returns {Promise}
*/ */
@ -709,7 +709,7 @@ Chain.prototype.findFork = co(function* findFork(fork, longer) {
* is received. * is received.
* @private * @private
* @param {ChainEntry} competitor - The competing chain's tip. * @param {ChainEntry} competitor - The competing chain's tip.
* @param {Block|MerkleBlock} block - The being being added. * @param {Block} block - The being being added.
* @returns {Promise} * @returns {Promise}
*/ */
@ -760,7 +760,7 @@ Chain.prototype.reorganize = co(function* reorganize(competitor, block) {
* will reset the chain to the fork block. * will reset the chain to the fork block.
* @private * @private
* @param {ChainEntry} competitor - The competing chain's tip. * @param {ChainEntry} competitor - The competing chain's tip.
* @param {Block|MerkleBlock} block - The being being added. * @param {Block} block - The being being added.
* @returns {Promise} * @returns {Promise}
*/ */
@ -877,7 +877,7 @@ Chain.prototype.reconnect = co(function* reconnect(entry) {
* reorganize the chain (a higher fork). * reorganize the chain (a higher fork).
* @private * @private
* @param {ChainEntry} entry * @param {ChainEntry} entry
* @param {Block|MerkleBlock} block * @param {Block} block
* @param {ChainEntry} prev * @param {ChainEntry} prev
* @returns {Promise} * @returns {Promise}
*/ */
@ -937,7 +937,7 @@ Chain.prototype.setBestChain = co(function* setBestChain(entry, block, prev) {
* Save block on an alternate chain. * Save block on an alternate chain.
* @private * @private
* @param {ChainEntry} entry * @param {ChainEntry} entry
* @param {Block|MerkleBlock} block * @param {Block} block
* @param {ChainEntry} prev * @param {ChainEntry} prev
* @returns {Promise} * @returns {Promise}
*/ */
@ -1575,22 +1575,13 @@ Chain.prototype.has = co(function* has(hash) {
}); });
/** /**
* Test the chain to see if it has recently seen a block. * Find the corresponding block entry by hash or height.
* @param {Hash} hash * @param {Hash|Number} hash/height
* @returns {Boolean} * @returns {Promise} - Returns {@link ChainEntry}.
*/ */
Chain.prototype.seen = function seen(hash) { Chain.prototype.getEntry = function getEntry(hash) {
if (this.hasOrphan(hash)) return this.db.getEntry(hash);
return true;
if (this.locker.has(hash))
return true;
if (this.invalid.has(hash))
return true;
return this.db.hasCache(hash);
}; };
/** /**
@ -1603,6 +1594,16 @@ Chain.prototype.hasEntry = function hasEntry(hash) {
return this.db.hasEntry(hash); return this.db.hasEntry(hash);
}; };
/**
* Get an orphan block.
* @param {Hash} hash
* @returns {Block}
*/
Chain.prototype.getOrphan = function getOrphan(hash) {
return this.orphanMap[hash] || null;
};
/** /**
* Test the chain to see if it contains an orphan. * Test the chain to see if it contains an orphan.
* @param {Hash} hash * @param {Hash} hash
@ -1610,7 +1611,7 @@ Chain.prototype.hasEntry = function hasEntry(hash) {
*/ */
Chain.prototype.hasOrphan = function hasOrphan(hash) { Chain.prototype.hasOrphan = function hasOrphan(hash) {
return !!this.getOrphan(hash); return this.orphanMap[hash] != null;
}; };
/** /**
@ -1623,26 +1624,6 @@ Chain.prototype.hasPending = function hasPending(hash) {
return this.locker.hasPending(hash); return this.locker.hasPending(hash);
}; };
/**
* Find the corresponding block entry by hash or height.
* @param {Hash|Number} hash/height
* @returns {Promise} - Returns {@link ChainEntry}.
*/
Chain.prototype.getEntry = function getEntry(hash) {
return this.db.getEntry(hash);
};
/**
* Get an orphan block.
* @param {Hash} hash
* @returns {Block|MerkleBlock|MemBlock}
*/
Chain.prototype.getOrphan = function getOrphan(hash) {
return this.orphanMap[hash] || null;
};
/** /**
* Get coin viewpoint. * Get coin viewpoint.
* @param {TX} tx * @param {TX} tx
@ -1719,7 +1700,7 @@ Chain.prototype.getProgress = function getProgress() {
/** /**
* Calculate chain locator (an array of hashes). * Calculate chain locator (an array of hashes).
* @param {(Number|Hash)?} start - Height or hash to treat as the tip. * @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 * 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. * non-existent hash, which is useful for headers-first locators.
* @returns {Promise} - Returns {@link Hash}[]. * @returns {Promise} - Returns {@link Hash}[].
@ -1737,7 +1718,7 @@ Chain.prototype.getLocator = co(function* getLocator(start) {
/** /**
* Calculate chain locator without a lock. * Calculate chain locator without a lock.
* @private * @private
* @param {(Number|Hash)?} start * @param {Hash} start
* @returns {Promise} * @returns {Promise}
*/ */
@ -1749,16 +1730,12 @@ Chain.prototype._getLocator = co(function* getLocator(start) {
if (start == null) if (start == null)
start = this.tip.hash; start = this.tip.hash;
assert(typeof start === 'string');
entry = yield this.db.getEntry(start); entry = yield this.db.getEntry(start);
if (!entry) { if (!entry) {
// We could simply return `start` here, hashes.push(start);
// but there is no required "spacing"
// for locator hashes. Pretend this hash
// is our tip. This is useful for
// getheaders.
if (typeof start === 'string')
hashes.push(start);
entry = this.tip; entry = this.tip;
} }
@ -1860,7 +1837,7 @@ Chain.prototype.getCurrentTarget = co(function* getCurrentTarget() {
/** /**
* Calculate the target based on the passed-in chain entry. * Calculate the target based on the passed-in chain entry.
* @param {ChainEntry} prev - Previous entry. * @param {ChainEntry} prev - Previous entry.
* @param {Block|MerkleBlock|null} - Current block. * @param {Block} - Current block.
* @returns {Promise} - returns Number * @returns {Promise} - returns Number
* (target is in compact/mantissa form). * (target is in compact/mantissa form).
*/ */
@ -1882,8 +1859,9 @@ Chain.prototype.getTargetAsync = co(function* getTargetAsync(block, prev) {
/** /**
* Calculate the target synchronously. _Must_ * Calculate the target synchronously. _Must_
* have ancestors pre-allocated. * have ancestors pre-allocated.
* @param {Block|MerkleBlock|null} - Current block. * @param {Block} - Current block.
* @param {ChainEntry} prev - Previous entry. * @param {ChainEntry} prev - Previous entry.
* @param {ChainEntry[]} ancestors
* @returns {Promise} - returns Number * @returns {Promise} - returns Number
* (target is in compact/mantissa form). * (target is in compact/mantissa form).
*/ */