From ee9037e19a9a7dba54f7d824046c6ea5c980e1e3 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sat, 10 May 2014 22:43:15 +0400 Subject: [PATCH] lib: jshint --- lib/bcoin/chain.js | 7 +++++-- lib/bcoin/peer.js | 6 +++--- lib/bcoin/pool.js | 26 ++++++++++++++------------ lib/bcoin/protocol/framer.js | 4 ++-- lib/bcoin/script.js | 6 +++--- lib/bcoin/tx-pool.js | 28 +++++++++++++++------------- lib/bcoin/tx.js | 2 +- lib/bcoin/utils.js | 15 +++++++++++---- 8 files changed, 54 insertions(+), 40 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index edcf3522..284d5180 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -322,8 +322,11 @@ Chain.prototype.get = function get(hash, cb) { Chain.prototype.isFull = function isFull() { // < 40m since last block - return !this.request.count && - (+new Date / 1000) - this.index.ts[this.index.ts.length - 1] < 40 * 60; + if (!this.request.count) + 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) { diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 32b5f514..5b57a1bc 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -105,7 +105,7 @@ Peer.prototype.broadcast = function broadcast(items) { var inv = this.framer.inv([{ type: item.type, hash: item.hash() - }]) + }]); // Auto-cleanup broadcast map after timeout var entry = { @@ -198,7 +198,7 @@ Peer.prototype._req = function _req(cmd, cb) { }, timer: null }; - entry.timer = setTimeout(entry.ontimeout, this._request.timeout) + entry.timer = setTimeout(entry.ontimeout, this._request.timeout); this._request.queue.push(entry); return entry; @@ -216,7 +216,7 @@ Peer.prototype._res = function _res(cmd, payload) { assert(!entry.cmd); // Restart timer - entry.timer = setTimeout(entry.ontimeout, this._request.timeout) + entry.timer = setTimeout(entry.ontimeout, this._request.timeout); return true; } else if (res !== this._request.skip) { this._request.queue.shift(); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 1f8e6a52..101b15b8 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -35,7 +35,7 @@ function Pool(options) { this.watchMap = {}; this.bloom = new bcoin.bloom(8 * 1024, 10, - (Math.random() * 0xffffffff) | 0), + (Math.random() * 0xffffffff) | 0); this.peers = { // Peers that are loading blocks themselves block: [], @@ -114,7 +114,7 @@ Pool.prototype._addLoader = function _addLoader() { if (self.destroyed) return; self._addLoader(); - }; + } var interval = setInterval(function() { self._load(); @@ -186,7 +186,7 @@ Pool.prototype._loadRange = function _loadRange(hashes, force) { return; // Limit number of requests - var now = +new Date; + var now = +new Date(); if (!force && now - this.load.lastRange < this.load.rangeWindow) return; 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 // chain by default if (!range.end) - range.end = +new Date / 1000; + range.end = +new Date() / 1000; if (!range.start) - range.start = +new Date / 1000 - 432000; + range.start = +new Date() / 1000 - 432000; var self = this; 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 if (type === 'block') - return this.chain.has(hash, true, next) + return this.chain.has(hash, true, next); else return next(false); @@ -493,14 +493,16 @@ Pool.prototype._doRequests = function _doRequests() { if (above && below && this.load.hiReached) this._load(); + function mapReq(item) { + return item.start(this.peers.block[i]); + } + // Split list between peers var red = this.redundancy; var count = this.peers.block.length; var split = Math.ceil(items.length * red / count); for (var i = 0, off = 0; i < count; i += red, off += split) { - var req = items.slice(off, off + split).map(function(item) { - return item.start(this.peers.block[i]); - }, this); + var req = items.slice(off, off + split).map(mapReq, this); for (var j = 0; j < red && i + j < count; j++) { this.peers.block[i + j].getData(req); @@ -537,7 +539,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: (+new Date() / 1000) - delta, end: 0 }; var self = this; doSearch(); @@ -628,13 +630,13 @@ Pool.prototype.fromJSON = function fromJSON(json) { }; function LoadRequest(pool, type, hash, cb) { - this.pool = pool + this.pool = pool; this.type = type; this.hash = hash; this.cbs = cb ? [ cb ] : []; this.timer = null; this.peer = null; - this.ts = +new Date; + this.ts = +new Date(); this.active = false; this.noQueue = false; diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index fea82073..4fe703ac 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -66,7 +66,7 @@ Framer.prototype.version = function version(packet) { writeU32(p, 0, 8); // Timestamp - var ts = ((+new Date) / 1000) | 0; + var ts = ((+new Date()) / 1000) | 0; writeU32(p, ts, 12); writeU32(p, 0, 16); @@ -162,7 +162,7 @@ Framer.prototype.pong = function pong(nonce) { Framer.prototype.filterLoad = function filterLoad(bloom, update) { var filter = bloom.toArray(); var before = []; - varint(before, filter.length, 0) + varint(before, filter.length, 0); var after = new Array(9); diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 8bda003a..0949670d 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -68,13 +68,13 @@ script.encode = function encode(s) { } else if (1 <= instr.length && instr.length <= 0x4b) { res = res.concat(instr.length, instr); } 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) { - res.push(opcodes['pushdata2']); + res.push(opcodes.pushdata2); utils.writeU16(res, instr.length, res.length); res = res.concat(instr); } else { - res.push(opcodes['pushdata4']); + res.push(opcodes.pushdata4); utils.writeU32(res, instr.length, res.length); res = res.concat(instr); } diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index e7f3af87..a4b11fa8 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -33,7 +33,7 @@ TXPool.prototype._init = function init() { keys: false, start: this._prefix, end: this._prefix + 'z' - }) + }); s.on('data', function(data) { self.add(bcoin.tx.fromJSON(data), true); }); @@ -49,7 +49,7 @@ TXPool.prototype.add = function add(tx, noWrite) { var hash = tx.hash('hex'); // 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); return; } @@ -108,6 +108,18 @@ TXPool.prototype.add = function add(tx, noWrite) { 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 for (var i = 0; i < tx.outputs.length; i++) { var out = tx.outputs[i]; @@ -117,17 +129,7 @@ TXPool.prototype.add = function add(tx, noWrite) { // Add input to orphan if (orphans) { - var some = orphans.some(function(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; - }, this); + var some = orphans.some(checkOrphan, this); if (!some) orphans = null; } diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 302c0c5f..896455a1 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -40,7 +40,7 @@ function TX(data, block) { } // ps = Pending Since - this.ps = this.ts === 0 ? +new Date / 1000 : 0; + this.ps = this.ts === 0 ? +new Date() / 1000 : 0; } module.exports = TX; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 99f440d9..737ffdc8 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -23,7 +23,7 @@ function toArray(msg, enc) { } } else if (enc === 'hex') { msg = msg.replace(/[^a-z0-9]+/ig, ''); - if (msg.length % 2 != 0) + if (msg.length % 2 !== 0) msg = '0' + msg; for (var i = 0; i < msg.length; i += 8) { var slice = msg.slice(i, i + 8); @@ -292,7 +292,7 @@ utils.nextTick = function nextTick(fn) { function RequestCache() { this.map = {}; this.count = 0; -}; +} utils.RequestCache = RequestCache; RequestCache.prototype.add = function add(id, cb) { @@ -323,5 +323,12 @@ utils.asyncify = function asyncify(fn) { utils.nextTick(function() { 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; +};