From a14299bdad34139d15bade770462afc7329b2b1f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 19 Apr 2016 06:06:28 -0700 Subject: [PATCH] move adjusted time around. --- lib/bcoin/abstractblock.js | 8 ++------ lib/bcoin/block.js | 5 ++--- lib/bcoin/chain.js | 9 ++++----- lib/bcoin/env.js | 14 +++++++++++++- lib/bcoin/headers.js | 5 ++--- lib/bcoin/merkleblock.js | 5 ++--- lib/bcoin/pool.js | 8 +++----- lib/bcoin/{adjustedtime.js => timedata.js} | 18 +++++++++--------- 8 files changed, 37 insertions(+), 35 deletions(-) rename lib/bcoin/{adjustedtime.js => timedata.js} (87%) diff --git a/lib/bcoin/abstractblock.js b/lib/bcoin/abstractblock.js index 35c7fb5f..0be5ce46 100644 --- a/lib/bcoin/abstractblock.js +++ b/lib/bcoin/abstractblock.js @@ -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; diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 32df6f42..67c5cc27 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -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 diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 3cfc7b2d..97cef441 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -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()); }; /** diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index 1cf3c1e9..e0e22d38 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -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 */ diff --git a/lib/bcoin/headers.js b/lib/bcoin/headers.js index 27d0550b..03a02e42 100644 --- a/lib/bcoin/headers.js +++ b/lib/bcoin/headers.js @@ -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); }; /** diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index 61b18a50..5d59a9ab 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -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. diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 065888ac..c11e28cf 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -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); }); diff --git a/lib/bcoin/adjustedtime.js b/lib/bcoin/timedata.js similarity index 87% rename from lib/bcoin/adjustedtime.js rename to lib/bcoin/timedata.js index 00ab1dad..1f776f6d 100644 --- a/lib/bcoin/adjustedtime.js +++ b/lib/bcoin/timedata.js @@ -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; };