minor improvements.

This commit is contained in:
Christopher Jeffrey 2016-06-02 17:02:27 -07:00
parent 2037060c35
commit 33f7b38433
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 34 additions and 22 deletions

View File

@ -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)

View File

@ -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();
};

View File

@ -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();