minor fixes. refactor timestamps.

This commit is contained in:
Christopher Jeffrey 2016-01-04 04:05:08 -08:00
parent a20cd7edbe
commit 0f8819c78e
9 changed files with 31 additions and 27 deletions

View File

@ -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') {

View File

@ -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));
};

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
};