From becbba4aba18bab0ff6a5c7360108048391c32f7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 12 Jun 2016 10:17:29 -0700 Subject: [PATCH] lint. optimize. --- lib/bcoin/chaindb.js | 28 +++++++++------------------- lib/bcoin/coins.js | 8 +++++--- lib/bcoin/protocol/framer.js | 2 +- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index d4baaf3f..f604bce0 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -10,7 +10,6 @@ var EventEmitter = require('events').EventEmitter; var constants = bcoin.protocol.constants; var utils = require('./utils'); var assert = utils.assert; -var pad32 = utils.pad32; var DUMMY = new Buffer([0]); var BufferWriter = require('./writer'); var BufferReader = require('./reader'); @@ -36,14 +35,9 @@ var Parser = bcoin.protocol.parser; */ var layout = { - R: function R() { - var key = new BufferWriter(); - key.writeU8(0x52); // R - key = key.render(); - if (utils.isBrowser) - key = key.toString('hex'); - return key; - }, + R: !utils.isBrowser + ? new Buffer([0x52]) // R + : '52', e: function e(hash) { var key = new BufferWriter(); key.writeU8(0x65); // e @@ -592,7 +586,7 @@ ChainDB.prototype.save = function save(entry, block, view, connect, callback) { batch.put(layout.n(entry.prevBlock), hash); batch.put(layout.H(entry.height), hash); - batch.put(layout.R(), hash); + batch.put(layout.R, hash); this.emit('add entry', entry); @@ -610,7 +604,7 @@ ChainDB.prototype.save = function save(entry, block, view, connect, callback) { ChainDB.prototype.getTip = function getTip(callback) { var self = this; - return this.db.fetch(layout.R(), function(data) { + return this.db.fetch(layout.R, function(data) { assert(data.length === 32, 'Database corruption.'); return data.toString('hex'); }, function(err, hash) { @@ -636,7 +630,7 @@ ChainDB.prototype.connect = function connect(entry, block, view, callback) { batch.put(layout.n(entry.prevBlock), hash); batch.put(layout.H(entry.height), hash); - batch.put(layout.R(), hash); + batch.put(layout.R, hash); this.cacheHash.set(entry.hash, entry); this.cacheHeight.set(entry.height, entry); @@ -667,7 +661,7 @@ ChainDB.prototype.disconnect = function disconnect(entry, callback) { batch.del(layout.n(entry.prevBlock)); batch.del(layout.H(entry.height)); - batch.put(layout.R(), new Buffer(entry.prevBlock, 'hex')); + batch.put(layout.R, new Buffer(entry.prevBlock, 'hex')); this.cacheHeight.remove(entry.height); @@ -777,7 +771,7 @@ ChainDB.prototype.reset = function reset(block, callback) { batch = self.db.batch(); if (tip.hash === entry.hash) { - batch.put(layout.R(), new Buffer(tip.hash, 'hex')); + batch.put(layout.R, new Buffer(tip.hash, 'hex')); return batch.write(callback); } @@ -878,7 +872,6 @@ ChainDB.prototype.removeBlock = function removeBlock(hash, batch, callback) { */ ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callback) { - var self = this; var undo = new BufferWriter(); var i, j, tx, input, output, key, addresses, address, hash, coins, raw; @@ -973,7 +966,7 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, batch, callb ChainDB.prototype.disconnectBlock = function disconnectBlock(block, batch, callback) { var self = this; - var i, j, tx, input, output, key, addresses, address, hash, view, coins, raw; + var i, j, tx, input, output, key, addresses, address, hash, coins, raw; if (this.options.spv) return utils.nextTick(callback); @@ -1132,9 +1125,6 @@ ChainDB.prototype.getCoin = function getCoin(hash, index, callback) { if (coins) { callback = utils.asyncify(callback); - if (raw) - return callback(null, coins); - try { coins = bcoin.coins.parseCoin(coins, hash, index); } catch (e) { diff --git a/lib/bcoin/coins.js b/lib/bcoin/coins.js index f32083e4..2981bafe 100644 --- a/lib/bcoin/coins.js +++ b/lib/bcoin/coins.js @@ -186,7 +186,7 @@ Coins.prototype.toRaw = function toRaw(writer) { Coins.parseRaw = function parseRaw(data, hash, index) { var p = new BufferReader(data); var i = 0; - var version, height, coins, mask, prefix, offset, size; + var version, height, coin, coins, mask, prefix, offset, size; version = p.readVarint(); height = p.readU32(); @@ -236,10 +236,12 @@ Coins.parseRaw = function parseRaw(data, hash, index) { continue; } - coins.outputs.push(new DeferredCoin(offset, size, data)); + coin = new DeferredCoin(offset, size, data); if (index != null) - return coins.outputs[0].toCoin(coins, i); + return coin.toCoin(coins, i); + + coins.outputs.push(coin); i++; } diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 14e79550..52f7f8bf 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -1195,7 +1195,7 @@ Framer.alert = function alert(data, key, writer) { var network = bcoin.network.get(data.network); var p, i, payload, hash, time; var version, relayUntil, expiration, id, cancel; - var cancels, count, i, minVer, maxVer, subVers; + var cancels, minVer, maxVer, subVers; var priority, comment, statusBar, reserved; if (!key && network.alertPrivateKey)