lib: jshint

This commit is contained in:
Fedor Indutny 2014-05-10 22:43:15 +04:00
parent ed8d886bf9
commit ee9037e19a
8 changed files with 54 additions and 40 deletions

View File

@ -322,8 +322,11 @@ Chain.prototype.get = function get(hash, cb) {
Chain.prototype.isFull = function isFull() { Chain.prototype.isFull = function isFull() {
// < 40m since last block // < 40m since last block
return !this.request.count && if (!this.request.count)
(+new Date / 1000) - this.index.ts[this.index.ts.length - 1] < 40 * 60; return false;
var delta = (+new Date() / 1000) - this.index.ts[this.index.ts.length - 1];
return delta < 40 * 60;
}; };
Chain.prototype.hashesInRange = function hashesInRange(start, end, cb) { Chain.prototype.hashesInRange = function hashesInRange(start, end, cb) {

View File

@ -105,7 +105,7 @@ Peer.prototype.broadcast = function broadcast(items) {
var inv = this.framer.inv([{ var inv = this.framer.inv([{
type: item.type, type: item.type,
hash: item.hash() hash: item.hash()
}]) }]);
// Auto-cleanup broadcast map after timeout // Auto-cleanup broadcast map after timeout
var entry = { var entry = {
@ -198,7 +198,7 @@ Peer.prototype._req = function _req(cmd, cb) {
}, },
timer: null timer: null
}; };
entry.timer = setTimeout(entry.ontimeout, this._request.timeout) entry.timer = setTimeout(entry.ontimeout, this._request.timeout);
this._request.queue.push(entry); this._request.queue.push(entry);
return entry; return entry;
@ -216,7 +216,7 @@ Peer.prototype._res = function _res(cmd, payload) {
assert(!entry.cmd); assert(!entry.cmd);
// Restart timer // Restart timer
entry.timer = setTimeout(entry.ontimeout, this._request.timeout) entry.timer = setTimeout(entry.ontimeout, this._request.timeout);
return true; return true;
} else if (res !== this._request.skip) { } else if (res !== this._request.skip) {
this._request.queue.shift(); this._request.queue.shift();

View File

@ -35,7 +35,7 @@ function Pool(options) {
this.watchMap = {}; this.watchMap = {};
this.bloom = new bcoin.bloom(8 * 1024, this.bloom = new bcoin.bloom(8 * 1024,
10, 10,
(Math.random() * 0xffffffff) | 0), (Math.random() * 0xffffffff) | 0);
this.peers = { this.peers = {
// Peers that are loading blocks themselves // Peers that are loading blocks themselves
block: [], block: [],
@ -114,7 +114,7 @@ Pool.prototype._addLoader = function _addLoader() {
if (self.destroyed) if (self.destroyed)
return; return;
self._addLoader(); self._addLoader();
}; }
var interval = setInterval(function() { var interval = setInterval(function() {
self._load(); self._load();
@ -186,7 +186,7 @@ Pool.prototype._loadRange = function _loadRange(hashes, force) {
return; return;
// Limit number of requests // Limit number of requests
var now = +new Date; var now = +new Date();
if (!force && now - this.load.lastRange < this.load.rangeWindow) if (!force && now - this.load.lastRange < this.load.rangeWindow)
return; return;
this.load.lastRange = now; this.load.lastRange = now;
@ -368,9 +368,9 @@ Pool.prototype.search = function search(id, range) {
// Last 5 days by default, this covers 1000 blocks that we have in the // Last 5 days by default, this covers 1000 blocks that we have in the
// chain by default // chain by default
if (!range.end) if (!range.end)
range.end = +new Date / 1000; range.end = +new Date() / 1000;
if (!range.start) if (!range.start)
range.start = +new Date / 1000 - 432000; range.start = +new Date() / 1000 - 432000;
var self = this; var self = this;
var e = new EventEmitter(); var e = new EventEmitter();
@ -428,7 +428,7 @@ Pool.prototype._request = function _request(type, hash, options, cb) {
// Block should be not in chain, or be requested // Block should be not in chain, or be requested
if (type === 'block') if (type === 'block')
return this.chain.has(hash, true, next) return this.chain.has(hash, true, next);
else else
return next(false); return next(false);
@ -493,14 +493,16 @@ Pool.prototype._doRequests = function _doRequests() {
if (above && below && this.load.hiReached) if (above && below && this.load.hiReached)
this._load(); this._load();
function mapReq(item) {
return item.start(this.peers.block[i]);
}
// Split list between peers // Split list between peers
var red = this.redundancy; var red = this.redundancy;
var count = this.peers.block.length; var count = this.peers.block.length;
var split = Math.ceil(items.length * red / count); var split = Math.ceil(items.length * red / count);
for (var i = 0, off = 0; i < count; i += red, off += split) { for (var i = 0, off = 0; i < count; i += red, off += split) {
var req = items.slice(off, off + split).map(function(item) { var req = items.slice(off, off + split).map(mapReq, this);
return item.start(this.peers.block[i]);
}, this);
for (var j = 0; j < red && i + j < count; j++) { for (var j = 0; j < red && i + j < count; j++) {
this.peers.block[i + j].getData(req); this.peers.block[i + j].getData(req);
@ -537,7 +539,7 @@ Pool.prototype.getTX = function getTX(hash, range, cb) {
if (range) if (range)
range = { start: range.start, end: range.end }; range = { start: range.start, end: range.end };
else else
range = { start: (+new Date / 1000) - delta, end: 0 }; range = { start: (+new Date() / 1000) - delta, end: 0 };
var self = this; var self = this;
doSearch(); doSearch();
@ -628,13 +630,13 @@ Pool.prototype.fromJSON = function fromJSON(json) {
}; };
function LoadRequest(pool, type, hash, cb) { function LoadRequest(pool, type, hash, cb) {
this.pool = pool this.pool = pool;
this.type = type; this.type = type;
this.hash = hash; this.hash = hash;
this.cbs = cb ? [ cb ] : []; this.cbs = cb ? [ cb ] : [];
this.timer = null; this.timer = null;
this.peer = null; this.peer = null;
this.ts = +new Date; this.ts = +new Date();
this.active = false; this.active = false;
this.noQueue = false; this.noQueue = false;

View File

@ -66,7 +66,7 @@ Framer.prototype.version = function version(packet) {
writeU32(p, 0, 8); writeU32(p, 0, 8);
// Timestamp // Timestamp
var ts = ((+new Date) / 1000) | 0; var ts = ((+new Date()) / 1000) | 0;
writeU32(p, ts, 12); writeU32(p, ts, 12);
writeU32(p, 0, 16); writeU32(p, 0, 16);
@ -162,7 +162,7 @@ Framer.prototype.pong = function pong(nonce) {
Framer.prototype.filterLoad = function filterLoad(bloom, update) { Framer.prototype.filterLoad = function filterLoad(bloom, update) {
var filter = bloom.toArray(); var filter = bloom.toArray();
var before = []; var before = [];
varint(before, filter.length, 0) varint(before, filter.length, 0);
var after = new Array(9); var after = new Array(9);

View File

@ -68,13 +68,13 @@ script.encode = function encode(s) {
} else if (1 <= instr.length && instr.length <= 0x4b) { } else if (1 <= instr.length && instr.length <= 0x4b) {
res = res.concat(instr.length, instr); res = res.concat(instr.length, instr);
} else if (instr.length <= 0xff) { } else if (instr.length <= 0xff) {
res = res.concat(opcodes['pushdata1'], instr.length, instr); res = res.concat(opcodes.pushdata1, instr.length, instr);
} else if (instr.length <= 0xffff) { } else if (instr.length <= 0xffff) {
res.push(opcodes['pushdata2']); res.push(opcodes.pushdata2);
utils.writeU16(res, instr.length, res.length); utils.writeU16(res, instr.length, res.length);
res = res.concat(instr); res = res.concat(instr);
} else { } else {
res.push(opcodes['pushdata4']); res.push(opcodes.pushdata4);
utils.writeU32(res, instr.length, res.length); utils.writeU32(res, instr.length, res.length);
res = res.concat(instr); res = res.concat(instr);
} }

View File

@ -33,7 +33,7 @@ TXPool.prototype._init = function init() {
keys: false, keys: false,
start: this._prefix, start: this._prefix,
end: this._prefix + 'z' end: this._prefix + 'z'
}) });
s.on('data', function(data) { s.on('data', function(data) {
self.add(bcoin.tx.fromJSON(data), true); self.add(bcoin.tx.fromJSON(data), true);
}); });
@ -49,7 +49,7 @@ TXPool.prototype.add = function add(tx, noWrite) {
var hash = tx.hash('hex'); var hash = tx.hash('hex');
// Ignore stale pending transactions // 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 < +new Date() / 1000) {
this._removeTX(tx); this._removeTX(tx);
return; return;
} }
@ -108,6 +108,18 @@ TXPool.prototype.add = function add(tx, noWrite) {
return; return;
} }
function checkOrphan(orphan) {
var index = orphan.tx._input(tx, orphan.index);
// Verify that input script is correct, if not - add output to unspent
// and remove orphan from storage
if (!orphan.tx.verify(index)) {
this._removeTX(orphan.tx);
return false;
}
return true;
}
// Add unspent outputs or fullfill orphans // Add unspent outputs or fullfill orphans
for (var i = 0; i < tx.outputs.length; i++) { for (var i = 0; i < tx.outputs.length; i++) {
var out = tx.outputs[i]; var out = tx.outputs[i];
@ -117,17 +129,7 @@ TXPool.prototype.add = function add(tx, noWrite) {
// Add input to orphan // Add input to orphan
if (orphans) { if (orphans) {
var some = orphans.some(function(orphan) { var some = orphans.some(checkOrphan, this);
var index = orphan.tx._input(tx, orphan.index);
// Verify that input script is correct, if not - add output to unspent
// and remove orphan from storage
if (!orphan.tx.verify(index)) {
this._removeTX(orphan.tx);
return false;
}
return true;
}, this);
if (!some) if (!some)
orphans = null; orphans = null;
} }

View File

@ -40,7 +40,7 @@ function TX(data, block) {
} }
// ps = Pending Since // ps = Pending Since
this.ps = this.ts === 0 ? +new Date / 1000 : 0; this.ps = this.ts === 0 ? +new Date() / 1000 : 0;
} }
module.exports = TX; module.exports = TX;

View File

@ -23,7 +23,7 @@ function toArray(msg, enc) {
} }
} else if (enc === 'hex') { } else if (enc === 'hex') {
msg = msg.replace(/[^a-z0-9]+/ig, ''); msg = msg.replace(/[^a-z0-9]+/ig, '');
if (msg.length % 2 != 0) if (msg.length % 2 !== 0)
msg = '0' + msg; msg = '0' + msg;
for (var i = 0; i < msg.length; i += 8) { for (var i = 0; i < msg.length; i += 8) {
var slice = msg.slice(i, i + 8); var slice = msg.slice(i, i + 8);
@ -292,7 +292,7 @@ utils.nextTick = function nextTick(fn) {
function RequestCache() { function RequestCache() {
this.map = {}; this.map = {};
this.count = 0; this.count = 0;
}; }
utils.RequestCache = RequestCache; utils.RequestCache = RequestCache;
RequestCache.prototype.add = function add(id, cb) { RequestCache.prototype.add = function add(id, cb) {
@ -323,5 +323,12 @@ utils.asyncify = function asyncify(fn) {
utils.nextTick(function() { utils.nextTick(function() {
fn(err, data1, data2); fn(err, data1, data2);
}); });
} };
} };
utils.revHex = function revHex() {
var r = '';
for (var i = 0; i < s.length; i += 2)
r = s.slice(i, i + 2) + r;
return r;
};