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