This commit is contained in:
Chris Kleeschulte 2017-01-30 14:59:53 -05:00
parent 28cd106842
commit 735810919a
2 changed files with 48 additions and 12 deletions

View File

@ -182,7 +182,7 @@ DB.prototype.start = function(callback) {
this.node.once('ready', function() {
// start syncing
self._sync.initialSync();
self._sync.sync();
// Notify that there is a new tip
self.node.services.bitcoind.on('tip', function() {

View File

@ -40,7 +40,7 @@ Sync.prototype.initialSync = function() {
processSerial.on('finish', function() {
self.syncing = false;
self.emit('initialsync');
self.emit('synced');
});
this.blockStream
@ -62,20 +62,13 @@ Sync.prototype.sync = function() {
this.syncing = true;
this.blockStream = new BlockStream();
var processBoth = new ProcessBoth(this.db);
var writeStream = new WriteStream(this.db);
var start = Date.now();
this._handleErrors(this.blockStream);
this._handleErrors(processBoth);
this._handleErrors(writeStream);
writeStream.on('finish', function() {
var end = Date.now();
console.log('Total time: ', (end - start) + ' ms');
console.log('Concurrent write time: ', writeStreamSlow.writeTime + ' ms');
console.log('Serial write time: ', writeStreamFast.writeTime + ' ms');
self.emit('synced');
//TODO what should happen here
processBoth.on('finish', function() {
self.syncing = false;
});
this.blockStream
@ -383,4 +376,47 @@ WriteStream.prototype._write = function(obj, enc, callback) {
});
};
var ProcessBoth = function(highWaterMark, db) {
Writable.call(this, {objectMode: true, highWaterMark: highWaterMark});
this.db = db;
};
inherits(ProcessBoth, Writable);
ProcessBoth.prototype._write = function(block, encoding, callback) {
async.parallel([function(next) {
self.db.getConcurrentBlockOperations(block, true, function(err, operations) {
if(err) {
return callback(err);
}
operations.push(self.db.getConcurrentTipOperation(block, true));
next(null, operations);
});
}, function(next) {
self.db.getSerialBlockOperations(block, true, function(err, operations) {
if(err) {
return callback(err);
}
operations.push(self.db.getTipOperation(block, true));
next(null, operations);
});
}], function(err, results) {
if(err) {
return callback(err);
}
var operations = results[0].concat(results[1]);
self.db.store.batch(operations, function(err) {
if(err) {
return callback(err);
}
self.db.tip = block;
self.db.concurrentTip = block;
callback();
});
});
};
module.exports = Sync;