move adjusted time around.
This commit is contained in:
parent
42ede54e94
commit
a14299bdad
@ -106,11 +106,10 @@ AbstractBlock.prototype.verify = function verify(ret) {
|
||||
* all objects which inherit from AbstractBlock).
|
||||
* @param {Object?} ret - Return object, may be
|
||||
* set with properties `reason` and `score`.
|
||||
* @param {Number?} now - Hopefully the adjusted time.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
AbstractBlock.prototype.verifyHeaders = function verifyHeaders(ret, now) {
|
||||
AbstractBlock.prototype.verifyHeaders = function verifyHeaders(ret) {
|
||||
if (!ret)
|
||||
ret = {};
|
||||
|
||||
@ -121,11 +120,8 @@ AbstractBlock.prototype.verifyHeaders = function verifyHeaders(ret, now) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (now == null)
|
||||
now = utils.now();
|
||||
|
||||
// Check timestamp against now + 2 hours
|
||||
if (this.ts > now + 2 * 60 * 60) {
|
||||
if (this.ts > bcoin.now() + 2 * 60 * 60) {
|
||||
ret.reason = 'time-too-new';
|
||||
ret.score = 0;
|
||||
return false;
|
||||
|
||||
@ -322,18 +322,17 @@ Block.prototype.__defineGetter__('commitmentHash', function() {
|
||||
* @alias verify
|
||||
* @param {Object?} ret - Return object, may be
|
||||
* set with properties `reason` and `score`.
|
||||
* @param {Number?} now - Hopefully the adjusted time.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
Block.prototype._verify = function _verify(ret, now) {
|
||||
Block.prototype._verify = function _verify(ret) {
|
||||
var sigops = 0;
|
||||
var i, tx, merkle;
|
||||
|
||||
if (!ret)
|
||||
ret = {};
|
||||
|
||||
if (!this.verifyHeaders(ret, now))
|
||||
if (!this.verifyHeaders(ret))
|
||||
return false;
|
||||
|
||||
// Size can't be bigger than MAX_BLOCK_SIZE
|
||||
|
||||
@ -67,7 +67,6 @@ function Chain(options) {
|
||||
this.options = options;
|
||||
|
||||
this.loaded = false;
|
||||
this.time = options.time || new bcoin.adjustedtime();
|
||||
this.db = new bcoin.chaindb(this, options);
|
||||
this.total = 0;
|
||||
this.orphanLimit = options.orphanLimit || (20 << 20);
|
||||
@ -480,7 +479,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
|
||||
callback(err, result);
|
||||
}
|
||||
|
||||
if (!block.verify(ret, this.time.now()))
|
||||
if (!block.verify(ret))
|
||||
return done(new VerifyError(block, 'invalid', ret.reason, ret.score));
|
||||
|
||||
if (this.options.spv || block.type !== 'block')
|
||||
@ -1398,7 +1397,7 @@ Chain.prototype.add = function add(block, callback, force) {
|
||||
// This is only necessary for new
|
||||
// blocks coming in, not the resolving
|
||||
// orphans.
|
||||
if (initial && !block.verify(ret, self.time.now())) {
|
||||
if (initial && !block.verify(ret)) {
|
||||
self.invalid[hash] = true;
|
||||
self.emit('invalid', block, {
|
||||
height: block.getCoinbaseHeight(),
|
||||
@ -2070,7 +2069,7 @@ Chain.prototype.getTarget = function getTarget(last, block, ancestors) {
|
||||
if ((last.height + 1) % network.pow.retargetInterval !== 0) {
|
||||
if (network.pow.allowMinDifficultyBlocks) {
|
||||
// Special behavior for testnet:
|
||||
ts = block ? (block.ts || block) : utils.now();
|
||||
ts = block ? (block.ts || block) : bcoin.now();
|
||||
if (ts > last.ts + network.pow.targetSpacing * 2)
|
||||
return powLimit;
|
||||
|
||||
@ -2427,7 +2426,7 @@ Chain.prototype.checkFinal = function checkFinal(prev, tx, flags, callback) {
|
||||
if (flags & constants.flags.MEDIAN_TIME_PAST)
|
||||
return prev.getMedianTimeAsync(check);
|
||||
|
||||
utils.asyncify(check)(null, utils.now());
|
||||
utils.asyncify(check)(null, bcoin.now());
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -100,6 +100,7 @@ try {
|
||||
* @property {Function} http.request - See {@link request}.
|
||||
* @property {Function} http.server - {@link HTTPServer} constructor.
|
||||
* @property {Object} workers - See {@link module:workers}.
|
||||
* @property {TimeData} time - For adjusted time.
|
||||
*/
|
||||
|
||||
function Environment(options) {
|
||||
@ -187,7 +188,7 @@ function Environment(options) {
|
||||
this.protocol = require('./protocol')(this);
|
||||
this.profiler = require('./profiler')(this);
|
||||
this.ldb = require('./ldb')(this);
|
||||
this.adjustedtime = require('./adjustedtime')(this);
|
||||
this.timedata = require('./timedata')(this);
|
||||
this.script = require('./script')(this);
|
||||
this.stack = this.script.stack;
|
||||
this.witness = this.script.witness;
|
||||
@ -228,6 +229,8 @@ function Environment(options) {
|
||||
this.workers = this.useWorkers && !this.isBrowser
|
||||
? require('./work' + 'ers')(this)
|
||||
: null;
|
||||
|
||||
this.time = new this.timedata();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -288,6 +291,15 @@ Environment.prototype.debug = function debug() {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the adjusted time.
|
||||
* @returns {Number} Adjusted time.
|
||||
*/
|
||||
|
||||
Environment.prototype.now = function now() {
|
||||
return this.time.now();
|
||||
};
|
||||
|
||||
/**
|
||||
* Expose
|
||||
*/
|
||||
|
||||
@ -52,12 +52,11 @@ Headers.prototype.render = function render() {
|
||||
* @alias verify
|
||||
* @param {Object?} ret - Return object, may be
|
||||
* set with properties `reason` and `score`.
|
||||
* @param {Number?} now - Hopefully the adjusted time.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
Headers.prototype._verify = function _verify(ret, now) {
|
||||
return this.verifyHeaders(ret, now);
|
||||
Headers.prototype._verify = function _verify(ret) {
|
||||
return this.verifyHeaders(ret);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -204,15 +204,14 @@ MerkleBlock.prototype.verifyPartial = function verifyPartial() {
|
||||
* @alias verify
|
||||
* @param {Object?} ret - Return object, may be
|
||||
* set with properties `reason` and `score`.
|
||||
* @param {Number?} now - Hopefully the adjusted time.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
MerkleBlock.prototype._verify = function _verify(ret, now) {
|
||||
MerkleBlock.prototype._verify = function _verify(ret) {
|
||||
if (!ret)
|
||||
ret = {};
|
||||
|
||||
if (!this.verifyHeaders(ret, now))
|
||||
if (!this.verifyHeaders(ret))
|
||||
return false;
|
||||
|
||||
// Verify the partial merkle tree if we are a merkleblock.
|
||||
|
||||
@ -81,9 +81,6 @@ function Pool(options) {
|
||||
this.chain = options.chain;
|
||||
this.mempool = options.mempool;
|
||||
|
||||
this.time = options.time || new bcoin.adjustedtime();
|
||||
this.chain.time = this.time;
|
||||
|
||||
assert(this.chain, 'Pool requires a blockchain.');
|
||||
|
||||
if (options.relay == null) {
|
||||
@ -682,7 +679,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer, callback)
|
||||
if (last && header.prevBlock !== last)
|
||||
return next(new Error('Bad header chain.'));
|
||||
|
||||
if (!header.verify(ret, self.time.now()))
|
||||
if (!header.verify(ret))
|
||||
return next(new VerifyError(header, 'invalid', ret.reason, ret.score));
|
||||
|
||||
last = hash;
|
||||
@ -1006,7 +1003,8 @@ Pool.prototype._createPeer = function _createPeer(options) {
|
||||
'Received version from %s: version=%d height=%d agent=%s',
|
||||
peer.host, version.version, version.height, version.agent);
|
||||
|
||||
self.time.add(peer.host, version.ts);
|
||||
bcoin.time.add(peer.host, version.ts);
|
||||
|
||||
self.emit('version', version, peer);
|
||||
});
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ var assert = utils.assert;
|
||||
* look it, but this is actually a semi-consensus-critical
|
||||
* piece of code. It handles version packets from peers
|
||||
* and calculates what to offset our system clock's time by.
|
||||
* @exports AdjustedTime
|
||||
* @exports TimeData
|
||||
* @constructor
|
||||
* @param {Number} [limit=200]
|
||||
* @property {Array} samples
|
||||
@ -25,9 +25,9 @@ var assert = utils.assert;
|
||||
* @property {Number} offset
|
||||
*/
|
||||
|
||||
function AdjustedTime(limit) {
|
||||
if (!(this instanceof AdjustedTime))
|
||||
return new AdjustedTime(limit);
|
||||
function TimeData(limit) {
|
||||
if (!(this instanceof TimeData))
|
||||
return new TimeData(limit);
|
||||
|
||||
EventEmitter.call(this);
|
||||
|
||||
@ -41,7 +41,7 @@ function AdjustedTime(limit) {
|
||||
this._checked = false;
|
||||
}
|
||||
|
||||
utils.inherits(AdjustedTime, EventEmitter);
|
||||
utils.inherits(TimeData, EventEmitter);
|
||||
|
||||
/**
|
||||
* Add time data.
|
||||
@ -49,7 +49,7 @@ utils.inherits(AdjustedTime, EventEmitter);
|
||||
* @param {Number} time
|
||||
*/
|
||||
|
||||
AdjustedTime.prototype.add = function add(host, time) {
|
||||
TimeData.prototype.add = function add(host, time) {
|
||||
var sample = time - utils.now();
|
||||
var i, median, match, offset;
|
||||
|
||||
@ -97,10 +97,10 @@ AdjustedTime.prototype.add = function add(host, time) {
|
||||
|
||||
/**
|
||||
* Get the current adjusted time.
|
||||
* @returns {Number} Time.
|
||||
* @returns {Number} Adjusted Time.
|
||||
*/
|
||||
|
||||
AdjustedTime.prototype.now = function now() {
|
||||
TimeData.prototype.now = function now() {
|
||||
return utils.now() + this.offset;
|
||||
};
|
||||
|
||||
@ -108,5 +108,5 @@ function compare(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
return AdjustedTime;
|
||||
return TimeData;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user