diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 272bf4af..24d883e8 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -224,7 +224,7 @@ Block.prototype._verify = function _verify() { return false; // Check timestamp - if (this.ts > (Date.now() / 1000) + 2 * 60 * 60) + if (this.ts > utils.now() + 2 * 60 * 60) return false; if (this.subtype === 'merkleblock') { diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index bb3e624a..41b38fc0 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -550,13 +550,13 @@ Chain.prototype.isFull = function isFull() { if (this.request.count) return false; - var delta = (+new Date() / 1000) - this.index.ts[this.index.ts.length - 1]; + var delta = utils.now() - this.index.ts[this.index.ts.length - 1]; return delta < 40 * 60; }; Chain.prototype.fillPercent = function fillPercent() { - var total = (+new Date() / 1000 - 40 * 60) - this.index.ts[0]; + var total = (utils.now() - 40 * 60) - this.index.ts[0]; var current = this.index.ts[this.index.ts.length - 1] - this.index.ts[0]; return Math.max(0, Math.min(current / total, 1)); }; diff --git a/lib/bcoin/fullchain.js b/lib/bcoin/fullchain.js index a7eaf636..0cc896da 100644 --- a/lib/bcoin/fullchain.js +++ b/lib/bcoin/fullchain.js @@ -383,12 +383,12 @@ Chain.prototype.getTip = function getTip() { Chain.prototype.isFull = function isFull() { var last = this.index.entries[this.index.entries.length - 1].ts; - var delta = (+new Date() / 1000) - last; + var delta = utils.now() - last; return delta < 40 * 60; }; Chain.prototype.fillPercent = function fillPercent() { - var total = (+new Date() / 1000 - 40 * 60) - this.index.lastTs; + var total = (utils.now() - 40 * 60) - this.index.lastTs; var current = this.getTip().ts - this.index.lastTs; return Math.max(0, Math.min(current / total, 1)); }; @@ -512,7 +512,7 @@ Chain.prototype.target = function target(last) { if ((last.height + 1) % adjustmentInterval) { if (network.powAllowMinDifficultyBlocks) { // Special behavior for testnet: - newBlockTs = Date.now() / 1000 | 0; + newBlockTs = utils.now(); if (newBlockTs > last.ts + network.powTargetSpacing * 2) return proofOfWorkLimit; diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 230bbc49..aa2d664c 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -86,7 +86,7 @@ Peer.prototype._init = function init() { var self = this; this.socket.once('connect', function() { - self.ts = Date.now() / 1000 | 0; + self.ts = utils.now(); self.address = self.socket.remoteAddress; self.port = self.socket.remotePort; }); @@ -137,7 +137,7 @@ Peer.prototype._init = function init() { return self._error(err); self.ack = true; self.emit('ack'); - self.ts = Date.now() / 1000 | 0; + self.ts = utils.now(); }); }; @@ -400,7 +400,7 @@ Peer.prototype._handleGetData = function handleGetData(items) { }; Peer.prototype._handleAddr = function handleAddr(addrs) { - var now = Date.now() / 1000 | 0; + var now = utils.now(); addrs.forEach(function(addr) { if (addr.ts <= 100000000 || addr.ts > now + 10 * 60) addr.ts = now - 5 * 24 * 60 * 60; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 66e564c0..f210310e 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -48,8 +48,6 @@ function Pool(options) { }; this.load = { - // timeout: options.loadTimeout || 3000, - // interval: options.loadInterval || 5000, timeout: options.loadTimeout || 30000, interval: options.loadInterval || 5000, window: options.loadWindow || 250, @@ -588,7 +586,9 @@ Pool.prototype._addPeer = function _addPeer(backoff) { self.tx.list.forEach(function(entry) { var result = peer.broadcast(entry.tx); - if (!result) return; + if (!result) + return; + result[0].once('request', function() { entry.e.emit('ack', peer); }); @@ -816,7 +816,7 @@ Pool.prototype.addWallet = function addWallet(w, defaultTs) { // Search for last week by default if (!ts) - ts = defaultTs || ((+new Date / 1000) - 7 * 24 * 3600); + ts = defaultTs || (utils.now() - 7 * 24 * 3600); // self.search(false, ts, e); self.searchWallet(ts); @@ -885,16 +885,18 @@ Pool.prototype.searchWallet = function(w) { } else { if (!w.loaded) { w.once('load', function() { - self.searchWallet(w.lastTs); + self.searchWallet(w); }); return; } ts = w.lastTs; if (!ts) - ts = (+new Date / 1000) - 7 * 24 * 3600; + ts = utils.now() - 7 * 24 * 3600; } - this.emit('debug', 'Wallet time: %s', new Date(ts * 1000)); + utils.nextTick(function() { + self.emit('debug', 'Wallet time: %s', new Date(ts * 1000)); + }); this.chain.resetTime(ts); }; @@ -930,9 +932,9 @@ Pool.prototype.search = function search(id, range, e) { // Last 5 days by default, this covers 1000 blocks that we have in the // chain by default if (!range.end) - range.end = +new Date() / 1000 | 0; + range.end = utils.now(); if (!range.start) - range.start = (+new Date() / 1000 | 0) - 432000; + range.start = utils.now() - 432000; if (range.start < this.chain.lastTs) { if (id) @@ -1160,7 +1162,7 @@ Pool.prototype.getTX = function getTX(hash, range, cb) { if (range) range = { start: range.start, end: range.end }; else - range = { start: (+new Date() / 1000) - delta, end: 0 }; + range = { start: utils.now() - delta, end: 0 }; function doSearch() { var e = self.search(hash, range); diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 55045d3c..c536be9c 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -86,7 +86,7 @@ Framer.prototype.version = function version(packet) { off += writeU32(p, 0, off); // Timestamp - ts = ((+new Date()) / 1000) | 0; + ts = utils.now(); off += writeU32(p, ts, off); off += writeU32(p, 0, off); @@ -359,16 +359,14 @@ Framer.prototype.merkleBlock = function merkleBlock(block) { Framer.prototype.addr = function addr(peers) { var p = []; - var i = 0; var off = 0; - var peer; - var start = (Date.now() / 1000 | 0) - process.uptime(); - var i; + var start = utils.now() - (process.uptime() | 0); + var i, peer; // count off += utils.writeIntv(p, peers.length, off); - for (; i < peers.length; i++) { + for (i = 0; i < peers.length; i++) { peer = peers[i]; // timestamp diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index db279efb..1ec28b78 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -70,7 +70,7 @@ TXPool.prototype.add = function add(tx, noWrite) { var out, key, orphans, some; // Ignore stale pending transactions - if (tx.ts === 0 && tx.ps + 2 * 24 * 3600 < +new Date() / 1000) { + if (tx.ts === 0 && tx.ps + 2 * 24 * 3600 < utils.now()) { this._removeTX(tx, noWrite); return; } diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 5c4dd31a..7a993df7 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -62,7 +62,7 @@ function TX(data, block) { this.changeOutput = data.changeOutput || null; // ps = Pending Since - this.ps = this.ts === 0 ? +new Date() / 1000 : 0; + this.ps = this.ts === 0 ? utils.now() : 0; } TX.fee = 10000; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index b83e4482..038e8685 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -826,3 +826,7 @@ utils.testTarget = function testTarget(target, hash) { return new bn(hash.slice().reverse()).cmp(target) < 0; }; + +utils.now = function now() { + return +new Date() / 1000 | 0; +};