refactor chain args.

This commit is contained in:
Christopher Jeffrey 2016-05-07 03:35:52 -07:00
parent d1aaaefda1
commit 8860eb5894
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 40 additions and 40 deletions

View File

@ -504,7 +504,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
0)); 0));
} }
if (block.bits !== self.getTarget(prev, block, ancestors)) { if (block.bits !== self.getTarget(block, prev, ancestors)) {
return callback(new VerifyError(block, return callback(new VerifyError(block,
'invalid', 'invalid',
'bad-diffbits', 'bad-diffbits',
@ -846,7 +846,7 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, state, callbac
} }
// Verify sequence locks. // Verify sequence locks.
self.checkLocks(tx, state.lockFlags, prev, function(err, valid) { self.checkLocks(prev, tx, state.lockFlags, function(err, valid) {
if (err) if (err)
return next(err); return next(err);
@ -1165,12 +1165,12 @@ Chain.prototype.connect = function connect(entry, callback) {
* reorganize the chain (a higher fork). * reorganize the chain (a higher fork).
* @private * @private
* @param {ChainBlock} entry * @param {ChainBlock} entry
* @param {ChainBlock} prev
* @param {Block|MerkleBlock} block * @param {Block|MerkleBlock} block
* @param {ChainBlock} prev
* @param {Function} callback - Returns [{@link VerifyError}]. * @param {Function} callback - Returns [{@link VerifyError}].
*/ */
Chain.prototype._setBestChain = function _setBestChain(entry, prev, block, callback) { Chain.prototype._setBestChain = function _setBestChain(entry, block, prev, callback) {
var self = this; var self = this;
function done(err) { function done(err) {
@ -1565,7 +1565,7 @@ Chain.prototype.add = function add(block, callback, force) {
} }
// Attempt to add block to the chain index. // Attempt to add block to the chain index.
self._setBestChain(entry, prev, block, function(err) { self._setBestChain(entry, block, prev, function(err) {
if (err) if (err)
return done(err); return done(err);
@ -1973,66 +1973,66 @@ Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) {
Chain.prototype.getCurrentTarget = function getCurrentTarget(callback) { Chain.prototype.getCurrentTarget = function getCurrentTarget(callback) {
if (!this.tip) if (!this.tip)
return callback(null, utils.toCompact(network.pow.limit)); return callback(null, utils.toCompact(network.pow.limit));
return this.getTargetAsync(this.tip, null, callback); return this.getTargetAsync(null, this.tip, callback);
}; };
/** /**
* Calculate the target based on the passed-in chain entry. * Calculate the target based on the passed-in chain entry.
* @param {ChainBlock} last - Previous entry. * @param {ChainBlock} prev - Previous entry.
* @param {Block|MerkleBlock|null} - Current block. * @param {Block|MerkleBlock|null} - Current block.
* @param {Function} callback - returns [Error, Number] * @param {Function} callback - returns [Error, Number]
* (target is in compact/mantissa form). * (target is in compact/mantissa form).
*/ */
Chain.prototype.getTargetAsync = function getTargetAsync(last, block, callback) { Chain.prototype.getTargetAsync = function getTargetAsync(block, prev, callback) {
var self = this; var self = this;
if ((last.height + 1) % network.pow.retargetInterval !== 0) { if ((prev.height + 1) % network.pow.retargetInterval !== 0) {
if (!network.pow.allowMinDifficultyBlocks) if (!network.pow.allowMinDifficultyBlocks)
return utils.asyncify(callback)(null, this.getTarget(last, block)); return utils.asyncify(callback)(null, this.getTarget(block, prev));
} }
return last.getAncestors(network.pow.retargetInterval, function(err, ancestors) { return prev.getAncestors(network.pow.retargetInterval, function(err, ancestors) {
if (err) if (err)
return callback(err); return callback(err);
return callback(null, self.getTarget(last, block, ancestors)); return callback(null, self.getTarget(block, prev, ancestors));
}); });
}; };
/** /**
* Calculate the target synchronously. _Must_ * Calculate the target synchronously. _Must_
* have ancestors pre-allocated. * have ancestors pre-allocated.
* @param {ChainBlock} last - Previous entry.
* @param {Block|MerkleBlock|null} - Current block. * @param {Block|MerkleBlock|null} - Current block.
* @param {ChainBlock} prev - Previous entry.
* @param {Function} callback - returns [Error, Number] * @param {Function} callback - returns [Error, Number]
* (target is in compact/mantissa form). * (target is in compact/mantissa form).
*/ */
Chain.prototype.getTarget = function getTarget(last, block, ancestors) { Chain.prototype.getTarget = function getTarget(block, prev, ancestors) {
var powLimit = utils.toCompact(network.pow.limit); var powLimit = utils.toCompact(network.pow.limit);
var ts, first, i; var ts, first, i;
// Genesis // Genesis
if (!last) if (!prev)
return powLimit; return powLimit;
// Do not retarget // Do not retarget
if ((last.height + 1) % network.pow.retargetInterval !== 0) { if ((prev.height + 1) % network.pow.retargetInterval !== 0) {
if (network.pow.allowMinDifficultyBlocks) { if (network.pow.allowMinDifficultyBlocks) {
// Special behavior for testnet: // Special behavior for testnet:
ts = block ? (block.ts || block) : bcoin.now(); ts = block ? (block.ts || block) : bcoin.now();
if (ts > last.ts + network.pow.targetSpacing * 2) if (ts > prev.ts + network.pow.targetSpacing * 2)
return powLimit; return powLimit;
i = 1; i = 1;
while (ancestors[i] while (ancestors[i]
&& last.height % network.pow.retargetInterval !== 0 && prev.height % network.pow.retargetInterval !== 0
&& last.bits === powLimit) { && prev.bits === powLimit) {
last = ancestors[i++]; prev = ancestors[i++];
} }
} }
return last.bits; return prev.bits;
} }
// Back 2 weeks // Back 2 weeks
@ -2040,26 +2040,26 @@ Chain.prototype.getTarget = function getTarget(last, block, ancestors) {
assert(first); assert(first);
return this.retarget(last, first); return this.retarget(prev, first);
}; };
/** /**
* Retarget. This is called when the chain height * Retarget. This is called when the chain height
* hits a retarget diff interval. * hits a retarget diff interval.
* @param {ChainBlock} last - Previous entry. * @param {ChainBlock} prev - Previous entry.
* @param {ChainBlock} first - Chain entry from 2 weeks prior. * @param {ChainBlock} first - Chain entry from 2 weeks prior.
* @returns {Number} target - Target in compact/mantissa form. * @returns {Number} target - Target in compact/mantissa form.
*/ */
Chain.prototype.retarget = function retarget(last, first) { Chain.prototype.retarget = function retarget(prev, first) {
var powTargetTimespan = new bn(network.pow.targetTimespan); var powTargetTimespan = new bn(network.pow.targetTimespan);
var actualTimespan, target; var actualTimespan, target;
if (network.pow.noRetargeting) if (network.pow.noRetargeting)
return last.bits; return prev.bits;
actualTimespan = new bn(last.ts - first.ts); actualTimespan = new bn(prev.ts - first.ts);
target = utils.fromCompact(last.bits); target = utils.fromCompact(prev.bits);
if (actualTimespan.cmp(powTargetTimespan.divn(4)) < 0) if (actualTimespan.cmp(powTargetTimespan.divn(4)) < 0)
actualTimespan = powTargetTimespan.divn(4); actualTimespan = powTargetTimespan.divn(4);
@ -2390,12 +2390,12 @@ Chain.prototype.checkFinal = function checkFinal(prev, tx, flags, callback) {
* Get the necessary minimum time and height sequence locks for a transaction. * Get the necessary minimum time and height sequence locks for a transaction.
* @param {TX} tx * @param {TX} tx
* @param {LockFlags} flags * @param {LockFlags} flags
* @param {ChainBlock} entry * @param {ChainBlock} prev
* @param {Function} callback - Returns * @param {Function} callback - Returns
* [Error, Number(minTime), Number(minHeight)]. * [Error, Number(minTime), Number(minHeight)].
*/ */
Chain.prototype.getLocks = function getLocks(tx, flags, entry, callback) { Chain.prototype.getLocks = function getLocks(prev, tx, flags, callback) {
var self = this; var self = this;
var mask = constants.sequence.MASK; var mask = constants.sequence.MASK;
var granularity = constants.sequence.GRANULARITY; var granularity = constants.sequence.GRANULARITY;
@ -2423,7 +2423,7 @@ Chain.prototype.getLocks = function getLocks(tx, flags, entry, callback) {
return next(); return next();
} }
entry.getAncestorByHeight(Math.max(coinHeight - 1, 0), function(err, entry) { prev.getAncestorByHeight(Math.max(coinHeight - 1, 0), function(err, entry) {
if (err) if (err)
return next(err); return next(err);
@ -2448,20 +2448,20 @@ Chain.prototype.getLocks = function getLocks(tx, flags, entry, callback) {
/** /**
* Evaluate sequence locks. * Evaluate sequence locks.
* @param {ChainBlock} entry * @param {ChainBlock} prev
* @param {Number} minHeight * @param {Number} minHeight
* @param {Number} minTime * @param {Number} minTime
* @param {Function} callback - Returns [Error, Boolean]. * @param {Function} callback - Returns [Error, Boolean].
*/ */
Chain.prototype.evalLocks = function evalLocks(entry, minHeight, minTime, callback) { Chain.prototype.evalLocks = function evalLocks(prev, minHeight, minTime, callback) {
if (minHeight >= entry.height + 1) if (minHeight >= prev.height + 1)
return utils.asyncify(callback)(null, false); return utils.asyncify(callback)(null, false);
if (minTime === -1) if (minTime === -1)
return utils.asyncify(callback)(null, true); return utils.asyncify(callback)(null, true);
entry.getMedianTimeAsync(function(err, medianTime) { prev.getMedianTimeAsync(function(err, medianTime) {
if (err) if (err)
return callback(err); return callback(err);
@ -2476,18 +2476,18 @@ Chain.prototype.evalLocks = function evalLocks(entry, minHeight, minTime, callba
* Verify sequence locks. * Verify sequence locks.
* @param {TX} tx * @param {TX} tx
* @param {LockFlags} flags * @param {LockFlags} flags
* @param {ChainBlock} entry * @param {ChainBlock} prev
* @param {Function} callback - Returns [Error, Boolean]. * @param {Function} callback - Returns [Error, Boolean].
*/ */
Chain.prototype.checkLocks = function checkLocks(tx, flags, entry, callback) { Chain.prototype.checkLocks = function checkLocks(prev, tx, flags, callback) {
var self = this; var self = this;
this.getLocks(tx, flags, entry, function(err, minHeight, minTime) { this.getLocks(prev, tx, flags, function(err, minHeight, minTime) {
if (err) if (err)
return callback(err); return callback(err);
self.evalLocks(entry, minHeight, minTime, callback); self.evalLocks(prev, minHeight, minTime, callback);
}); });
}; };

View File

@ -1292,7 +1292,7 @@ Mempool.prototype.getSnapshot = function getSnapshot(callback) {
*/ */
Mempool.prototype.checkLocks = function checkLocks(tx, flags, callback) { Mempool.prototype.checkLocks = function checkLocks(tx, flags, callback) {
return this.chain.checkLocks(tx, flags, this.chain.tip, callback); return this.chain.checkLocks(this.chain.tip, tx, flags, callback);
}; };
/** /**

View File

@ -254,7 +254,7 @@ Miner.prototype.createBlock = function createBlock(version, callback) {
assert(this.chain.tip); assert(this.chain.tip);
// Find target // Find target
this.chain.getTargetAsync(this.chain.tip, ts, function(err, target) { this.chain.getTargetAsync(ts, this.chain.tip, function(err, target) {
if (err) if (err)
return callback(err); return callback(err);