diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index e4f308bd..97350683 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -105,6 +105,9 @@ Chain.prototype._init = function _init() { if (!self.synced && self.height < self.network.block.slowHeight) return; + if (self.options.spv) + return; + bcoin.debug('Block %s (%d) added to chain.', utils.revHex(entry.hash), entry.height); }); @@ -285,6 +288,7 @@ Chain.prototype._preload = function _preload(callback) { var buf, height, stream; var request = require('./http/request'); var locker = new bcoin.locker(); + var flushed = 0; if (!this.options.preload) return callback(); @@ -295,7 +299,7 @@ Chain.prototype._preload = function _preload(callback) { if (this.network.type !== 'main') return callback(); - bcoin.debug('Loading %s', url); + bcoin.debug('Loading %s.', url); function parseHeader(buf) { var headers = bcoin.protocol.parser.parseBlockHeaders(buf); @@ -315,6 +319,9 @@ Chain.prototype._preload = function _preload(callback) { return callback(err); } + if ((++flushed % 50000) === 0) + utils.print('Flushed %d headers to DB.', flushed); + if (locker.jobs.length === 0 && save.ended) return callback(); @@ -334,10 +341,16 @@ Chain.prototype._preload = function _preload(callback) { }; stream.on('response', function(res) { + var height = +res.headers['content-length'] / 80 | 0; if (res.statusCode >= 400) { stream.destroy(); return callback(new Error('Bad response code: ' + res.statusCode)); } + if (chainHeight > height - 30000) { + bcoin.debug('Preload height is %d. Skipping.', height); + stream.destroy(); + return callback(); + } }); stream.on('error', function(err) { @@ -365,9 +378,6 @@ Chain.prototype._preload = function _preload(callback) { buf.size += data.length; } - if (blocks.length === 0) - return; - for (i = 0; i < blocks.length; i++) { data = blocks[i]; @@ -405,9 +415,7 @@ Chain.prototype._preload = function _preload(callback) { // Create a chain entry. entry = new bcoin.chainentry(self, data, lastEntry); - if (entry.height <= chainHeight) - self.db.addCache(entry); - else + if (entry.height > chainHeight) save(entry); if ((height + 1) % 50000 === 0) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index ecc016bc..d6a65de8 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1283,6 +1283,8 @@ Pool.prototype._removePeer = function _removePeer(peer) { */ Pool.prototype.watch = function watch(data, enc) { + if (!this.options.spv) + return; this.spvFilter.add(data, enc); this.updateWatch(); }; @@ -1292,6 +1294,8 @@ Pool.prototype.watch = function watch(data, enc) { */ Pool.prototype.unwatch = function unwatch() { + if (!this.options.spv) + return; this.spvFilter.reset(); this.updateWatch(); }; diff --git a/lib/bcoin/spvnode.js b/lib/bcoin/spvnode.js index 5b36851f..ea43aa87 100644 --- a/lib/bcoin/spvnode.js +++ b/lib/bcoin/spvnode.js @@ -160,21 +160,6 @@ SPVNode.prototype._init = function _init() { }); }); }, - function(next) { - var i; - self.walletdb.getUnconfirmed(function(err, txs) { - if (err) - return next(err); - - if (txs.length > 0) - bcoin.debug('Rebroadcasting %d transactions.', txs.length); - - for (i = 0; i < txs.length; i++) - self.pool.broadcast(txs[i]); - - next(); - }); - }, function(next) { var i; self.walletdb.getAddresses(function(err, hashes) { @@ -190,6 +175,21 @@ SPVNode.prototype._init = function _init() { next(); }); }, + function(next) { + var i; + self.walletdb.getUnconfirmed(function(err, txs) { + if (err) + return next(err); + + if (txs.length > 0) + bcoin.debug('Rebroadcasting %d transactions.', txs.length); + + for (i = 0; i < txs.length; i++) + self.pool.broadcast(txs[i]); + + next(); + }); + }, function(next) { if (!self.chain.options.preload) return next();