style. fix addr timestamp.

This commit is contained in:
Christopher Jeffrey 2015-12-19 02:10:56 -08:00
parent d3811acc28
commit a96f27682f
3 changed files with 36 additions and 21 deletions

View File

@ -13,6 +13,10 @@ var network = bcoin.protocol.network;
var utils = bcoin.utils;
var assert = utils.assert;
/**
* Chain
*/
function Chain(options) {
var preload = network.preload;

View File

@ -332,19 +332,26 @@ Peer.prototype._onPacket = function onPacket(packet) {
if (cmd === 'version')
return this._handleVersion(payload);
else if (cmd === 'inv')
if (cmd === 'inv')
return this._handleInv(payload);
else if (cmd === 'headers')
if (cmd === 'headers')
return this._handleHeaders(payload);
else if (cmd === 'getdata')
if (cmd === 'getdata')
return this._handleGetData(payload);
else if (cmd === 'addr')
if (cmd === 'addr')
return this._handleAddr(payload);
else if (cmd === 'ping')
if (cmd === 'ping')
return this._handlePing(payload);
else if (cmd === 'pong')
if (cmd === 'pong')
return this._handlePong(payload);
else if (cmd === 'getaddr')
if (cmd === 'getaddr')
return this._handleGetAddr();
if (cmd === 'merkleblock' || cmd === 'block') {
@ -355,10 +362,11 @@ Peer.prototype._onPacket = function onPacket(packet) {
payload._network = true;
payload = bcoin.tx(payload, this.lastBlock);
}
if (this._res(cmd, payload))
return;
else
this.emit(cmd, payload);
this.emit(cmd, payload);
};
Peer.prototype._handleVersion = function handleVersion(payload) {
@ -385,11 +393,11 @@ Peer.prototype._handleGetData = function handleGetData(items) {
};
Peer.prototype._handleAddr = function handleAddr(addrs) {
var now = Date.now();
var now = Date.now() / 1000 | 0;
addrs.forEach(function(addr) {
// bitcoind does this for some reason:
if (addr.ts <= 100000000 || addr.ts > now + 10 * 60)
addr.ts = now - 5 * 24 * 60 * 60;
this.emit('addr', {
date: new Date(addr.ts * 1000),
ts: addr.ts,
@ -415,12 +423,13 @@ Peer.prototype._handlePong = function handlePong() {
Peer.prototype._handleGetAddr = function handleGetAddr() {
var used = [];
var peers = [].concat(
var peers, addrs;
peers = [].concat(
this.pool.peers.pending,
this.pool.peers.block,
this.pool.peers.load
).filter(Boolean);
var addrs;
// NOTE: For IPv6 BTC uses:
// '0000:0000:0000:0000:0000:xxxx:xxxx:ffff'
@ -471,6 +480,8 @@ Peer.prototype._handleGetAddr = function handleGetAddr() {
};
Peer.prototype._handleInv = function handleInv(items) {
var req, i, block, hash;
// Always request advertised TXs
var txs = items.filter(function(item) {
return item.type === 'tx';
@ -483,8 +494,6 @@ Peer.prototype._handleInv = function handleInv(items) {
return item.hash;
});
var req, i, block, hash;
if (blocks.length === 1)
this.bestBlock = utils.toHex(blocks[0]);

View File

@ -587,6 +587,7 @@ Pool.prototype.unwatch = function unwatch(id) {
// Resend it to peers
if (this.peers.load)
this.peers.load.updateWatch();
for (i = 0; i < this.peers.block.length; i++)
this.peers.block[i].updateWatch();
};
@ -618,6 +619,7 @@ Pool.prototype.addWallet = function addWallet(w, defaultTs) {
// Search for last week by default
if (!ts)
ts = defaultTs || ((+new Date / 1000) - 7 * 24 * 3600);
self.search(false, ts, e);
}
@ -760,12 +762,6 @@ Pool.prototype._request = function _request(type, hash, options, cb) {
if (this.request.map[hash])
return this.request.map[hash].addCallback(cb);
// Block should be not in chain, or be requested
if (!options.force && type === 'block')
return this.chain.has(hash, true, next);
else
return next(false);
function next(has) {
if (has)
return;
@ -776,6 +772,12 @@ Pool.prototype._request = function _request(type, hash, options, cb) {
var req = new LoadRequest(self, type, hash, cb);
req.add(options.noQueue);
}
// Block should be not in chain, or be requested
if (!options.force && type === 'block')
return this.chain.has(hash, true, next);
return next(false);
};
Pool.prototype._response = function _response(entity) {