From e503b7ecadd7ab22eb8dfc82da61792d58d5d7b8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 15 Aug 2016 04:54:46 -0700 Subject: [PATCH] walletdb: fixes and lint. --- lib/bcoin/chaindb.js | 13 ++++++++----- lib/bcoin/http/server.js | 5 +++-- lib/bcoin/pool.js | 13 ++++++++++++- lib/bcoin/walletdb.js | 21 ++++++++++++++------- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index b1b4b489..86b53a2b 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -1126,10 +1126,13 @@ ChainDB.prototype.scan = function scan(start, filter, iter, callback) { var total = 0; var i, j, hashes, address, tx, txs; - if (!start) + if (start == null) start = this.network.genesis.hash; - this.logger.info('Scanning from block %s.', utils.revHex(start)); + if (typeof start === 'number') + this.logger.info('Scanning from height %d.', start); + else + this.logger.info('Scanning from block %s.', utils.revHex(start)); if (Array.isArray(filter)) filter = utils.toMap(filter); @@ -1160,7 +1163,7 @@ ChainDB.prototype.scan = function scan(start, filter, iter, callback) { return next(); self.logger.info('Scanning block %s (%d).', - utils.revHex(hash), + utils.revHex(entry.hash), block.height); txs = []; @@ -1179,12 +1182,12 @@ ChainDB.prototype.scan = function scan(start, filter, iter, callback) { } if (txs.length === 0) - return self.getNextHash(hash, next); + return self.getNextHash(entry.hash, next); iter(entry, txs, function(err) { if (err) return next(err); - self.getNextHash(hash, next); + self.getNextHash(entry.hash, next); }); }); }); diff --git a/lib/bcoin/http/server.js b/lib/bcoin/http/server.js index 4e513a03..d9a843db 100644 --- a/lib/bcoin/http/server.js +++ b/lib/bcoin/http/server.js @@ -1024,7 +1024,7 @@ HTTPServer.prototype._initIO = function _initIO() { if (typeof callback !== 'function') return socket.destroy(); - if (typeof start !== 'string') + if (typeof start !== 'string' && !utils.isNumber(start)) return callback({ error: 'Invalid parameter.' }); try { @@ -1351,7 +1351,8 @@ ClientSocket.prototype.scan = function scan(start, callback) { var self = this; var i; - start = utils.revHex(start); + if (typeof start === 'string') + start = utils.revHex(start); this.chain.db.scan(start, this.filter, function(entry, txs, next) { for (i = 0; i < txs.length; i++) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 7add51d8..04bc4042 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1796,10 +1796,20 @@ Pool.prototype.getLoaderHost = function getLoaderHost() { return this.seeds[0]; host = this.getRandom(this.seeds); + if (host) return host; - return this.getRandom(this.hosts); + host = this.getRandom(this.hosts); + + if (host) + return host; + + this.logger.warning('All seeds banned or ignored. Clearing...'); + this.peers.ignored = {}; + this.peers.misbehaving = {}; + + return this.getRandom(this.seeds); }; /** @@ -1815,6 +1825,7 @@ Pool.prototype.getHost = function getHost() { return; host = this.getRandom(this.seeds, true); + if (host) return host; diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index b39a66a6..6ebb5483 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -140,6 +140,11 @@ WalletDB.prototype._open = function open(callback) { return callback(err); self.depth = depth; + + self.logger.info( + 'WalletDB loaded (depth=%d, height=%d).', + depth, self.height); + self.loadFilter(callback); }); }); @@ -176,7 +181,7 @@ WalletDB.prototype._close = function close(callback) { */ WalletDB.prototype.getDepth = function getDepth(callback) { - var opt, iter, parts, depth; + var iter, parts, depth; // This may seem like a strange way to do // this, but updating a global state when @@ -365,6 +370,7 @@ WalletDB.prototype.unregister = function unregister(wallet) { */ WalletDB.prototype.getWalletID = function getWalletID(id, callback) { + var self = this; var wid; if (!id) @@ -1172,6 +1178,7 @@ WalletDB.prototype.setTip = function setTip(hash, height, callback) { */ WalletDB.prototype.writeBlock = function writeBlock(block, matches, callback) { + var self = this; var batch = this.db.batch(); var i, hash, wallets; @@ -1205,6 +1212,7 @@ WalletDB.prototype.writeBlock = function writeBlock(block, matches, callback) { */ WalletDB.prototype.unwriteBlock = function unwriteBlock(block, callback) { + var self = this; var batch = this.db.batch(); var prev = new WalletBlock(block.prevBlock, block.height - 1); @@ -1252,7 +1260,7 @@ WalletDB.prototype.getWalletsByTX = function getWalletsByTX(hash, callback) { WalletDB.prototype.addBlock = function addBlock(entry, txs, callback, force) { var self = this; - var i, block, matches, hash, unlock; + var block, matches, hash, unlock; unlock = this.txLock.lock(addBlock, [entry, txs, callback], force); @@ -1548,8 +1556,6 @@ Path.prototype.toPath = function() { Path.prototype.toJSON = function toJSON() { return { - wid: this.wid, - id: this.id, name: this.name, change: this.change === 1, path: this.toPath() @@ -1834,9 +1840,9 @@ WalletBlock.prototype.fromEntry = function fromEntry(entry) { WalletBlock.prototype.fromJSON = function fromJSON(json) { this.hash = utils.revHex(json.hash); - this.height = entry.height; - if (entry.prevBlock) - this.prevBlock = utils.revHex(entry.prevBlock); + this.height = json.height; + if (json.prevBlock) + this.prevBlock = utils.revHex(json.prevBlock); return this; }; @@ -1850,6 +1856,7 @@ WalletBlock.prototype.fromRaw = function fromRaw(hash, data) { }; WalletBlock.prototype.fromTip = function fromTip(data) { + var p = new BufferReader(data); this.hash = p.readHash('hex'); this.height = p.readU32(); return this;