2014-01-10 11:02:33 -08:00
|
|
|
'use strict';
|
2014-01-07 10:21:59 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var imports = require('soop').imports();
|
|
|
|
|
2014-03-10 08:46:48 -07:00
|
|
|
var config = imports.config || require('../config/config');
|
2014-05-07 05:45:44 -07:00
|
|
|
var bitcore = require('bitcore');
|
|
|
|
var networks = bitcore.networks;
|
2014-03-05 18:03:56 -08:00
|
|
|
var async = require('async');
|
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
var logger = require('./logger').logger;
|
|
|
|
var d = logger.log;
|
|
|
|
var info = logger.info;
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-03-20 10:06:37 -07:00
|
|
|
var syncId = 0;
|
2014-03-05 18:03:56 -08:00
|
|
|
|
|
|
|
function Sync(opts) {
|
2014-03-20 10:06:37 -07:00
|
|
|
this.id = syncId++;
|
2014-03-05 18:03:56 -08:00
|
|
|
this.opts = opts || {};
|
2014-03-10 08:46:48 -07:00
|
|
|
this.bDb = require('./BlockDb').default();
|
|
|
|
this.txDb = require('./TransactionDb').default();
|
2014-03-05 18:03:56 -08:00
|
|
|
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
|
2014-05-23 17:23:44 -07:00
|
|
|
this.cachedLastHash = null;
|
2014-03-05 18:03:56 -08:00
|
|
|
}
|
2014-01-10 11:57:16 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
Sync.prototype.close = function(cb) {
|
|
|
|
var self = this;
|
|
|
|
self.txDb.close(function() {
|
|
|
|
self.bDb.close(cb);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Sync.prototype.destroy = function(next) {
|
|
|
|
var self = this;
|
|
|
|
async.series([
|
|
|
|
|
|
|
|
function(b) {
|
|
|
|
self.bDb.drop(b);
|
|
|
|
},
|
|
|
|
function(b) {
|
|
|
|
self.txDb.drop(b);
|
|
|
|
},
|
|
|
|
], next);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Arrives a NEW block, which is the new TIP
|
|
|
|
*
|
|
|
|
* Case 0) Simple case
|
|
|
|
* A-B-C-D-E(TIP)-NEW
|
|
|
|
*
|
|
|
|
* Case 1)
|
|
|
|
* A-B-C-D-E(TIP)
|
|
|
|
* \
|
|
|
|
* NEW
|
|
|
|
*
|
|
|
|
* 1) Declare D-E orphans (and possible invalidate TXs on them)
|
|
|
|
*
|
|
|
|
* Case 2)
|
|
|
|
* A-B-C-D-E(TIP)
|
|
|
|
* \
|
|
|
|
* F-G-NEW
|
|
|
|
* 1) Set F-G as connected (mark TXs as valid)
|
2014-05-23 17:23:44 -07:00
|
|
|
* 2) Set new heights in F-G-NEW
|
|
|
|
* 3) Declare D-E orphans (and possible invalidate TXs on them)
|
2014-03-05 18:03:56 -08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Case 3)
|
|
|
|
*
|
|
|
|
* A-B-C-D-E(TIP) ... NEW
|
|
|
|
*
|
|
|
|
* NEW is ignored (if allowReorgs is false)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) {
|
|
|
|
|
|
|
|
if (typeof allowReorgs === 'function') {
|
|
|
|
cb = allowReorgs;
|
|
|
|
allowReorgs = true;
|
2014-02-17 22:40:55 -08:00
|
|
|
}
|
2014-03-05 18:03:56 -08:00
|
|
|
if (!b) return cb();
|
2014-01-18 13:28:24 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this;
|
2014-05-23 17:23:44 -07:00
|
|
|
var oldTip, oldNext, oldHeight, needReorg = false, height = -1;
|
2014-03-05 18:03:56 -08:00
|
|
|
var newPrev = b.previousblockhash;
|
2014-01-18 13:28:24 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
async.series([
|
|
|
|
function(c) {
|
2014-05-23 17:23:44 -07:00
|
|
|
// TODO? remove this check?
|
2014-03-05 18:03:56 -08:00
|
|
|
self.bDb.has(b.hash, function(err, val) {
|
|
|
|
return c(err ||
|
|
|
|
(val ? new Error('WARN: Ignoring already existing block:' + b.hash) : null));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(c) {
|
2014-05-23 17:23:44 -07:00
|
|
|
if (!allowReorgs || newPrev === self.cachedLastHash) return c();
|
2014-03-05 18:03:56 -08:00
|
|
|
self.bDb.has(newPrev, function(err, val) {
|
2014-05-23 17:23:44 -07:00
|
|
|
// Genesis? no problem
|
2014-03-05 18:03:56 -08:00
|
|
|
if (!val && newPrev.match(/^0+$/)) return c();
|
|
|
|
return c(err ||
|
|
|
|
(!val ? new Error('NEED_SYNC Ignoring block with non existing prev:' + b.hash) : null));
|
|
|
|
});
|
2014-02-14 11:35:32 -08:00
|
|
|
},
|
2014-03-05 18:03:56 -08:00
|
|
|
function(c) {
|
|
|
|
if (!allowReorgs) return c();
|
2014-05-20 14:07:25 -07:00
|
|
|
self.bDb.getTip(function(err, hash, h) {
|
|
|
|
oldTip = hash;
|
2014-05-23 17:23:44 -07:00
|
|
|
oldHeight = hash ? (h || 0) : -1
|
|
|
|
|
2014-05-20 14:07:25 -07:00
|
|
|
if (oldTip && newPrev !== oldTip) {
|
|
|
|
needReorg = true;
|
|
|
|
console.log('## REORG Triggered, tip mismatch');
|
|
|
|
}
|
2014-03-05 18:03:56 -08:00
|
|
|
return c();
|
|
|
|
});
|
|
|
|
},
|
2014-05-23 17:23:44 -07:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
function(c) {
|
|
|
|
if (!needReorg) return c();
|
|
|
|
self.bDb.getNext(newPrev, function(err, val) {
|
|
|
|
if (err) return c(err);
|
|
|
|
oldNext = val;
|
|
|
|
return c();
|
|
|
|
});
|
|
|
|
},
|
2014-05-23 17:23:44 -07:00
|
|
|
function(c) {
|
|
|
|
if (!allowReorgs) return c();
|
|
|
|
if (needReorg) {
|
|
|
|
info('NEW TIP: %s NEED REORG (old tip: %s #%d)', b.hash, oldTip, oldHeight);
|
|
|
|
self.processReorg(oldTip, oldNext, newPrev, oldHeight, function(err, h) {
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
height = h;
|
|
|
|
return c();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
height = oldHeight + 1;
|
|
|
|
return c();
|
|
|
|
}
|
2014-03-05 18:03:56 -08:00
|
|
|
},
|
|
|
|
function(c) {
|
2014-05-23 17:23:44 -07:00
|
|
|
self.cachedLastHash = b.hash; // just for speed up.
|
|
|
|
self.bDb.add(b, height, c);
|
2014-03-05 18:03:56 -08:00
|
|
|
},
|
|
|
|
function(c) {
|
|
|
|
if (!allowReorgs) return c();
|
2014-05-23 17:23:44 -07:00
|
|
|
self.bDb.setTip(b.hash, height, function(err) {
|
2014-03-05 18:03:56 -08:00
|
|
|
return c(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(c) {
|
|
|
|
self.bDb.setNext(newPrev, b.hash, function(err) {
|
|
|
|
return c(err);
|
|
|
|
});
|
|
|
|
}
|
2014-02-08 05:57:37 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
],
|
|
|
|
function(err) {
|
|
|
|
if (err && err.toString().match(/WARN/)) {
|
|
|
|
err = null;
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
};
|
2014-02-03 22:22:58 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
Sync.prototype.processReorg = function(oldTip, oldNext, newPrev, oldHeight, cb) {
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this;
|
2014-02-04 14:55:58 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
var orphanizeFrom, newHeight;
|
2014-02-08 05:57:37 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
async.series([
|
2014-02-08 11:20:51 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
function(c) {
|
2014-05-21 11:51:24 -07:00
|
|
|
self.bDb.getHeight(newPrev, function(err, height) {
|
2014-05-23 17:23:44 -07:00
|
|
|
if (!height) return c(new Error('Could not found block:' + newPrev));
|
|
|
|
if (height<0) return c();
|
2014-02-08 05:57:37 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
newHeight = height + 1;
|
|
|
|
info('# Reorg Case 1) OldNext: %s NewHeight: %d', oldNext, newHeight);
|
2014-03-05 18:03:56 -08:00
|
|
|
orphanizeFrom = oldNext;
|
|
|
|
return c(err);
|
|
|
|
});
|
2014-02-14 11:35:32 -08:00
|
|
|
},
|
2014-02-08 05:57:37 -08:00
|
|
|
function(c) {
|
2014-03-05 18:03:56 -08:00
|
|
|
if (orphanizeFrom) return c();
|
2014-02-08 05:57:37 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
info('# Reorg Case 2)');
|
|
|
|
self.setBranchConnectedBackwards(newPrev, function(err, yHash, newYHashNext, height) {
|
2014-02-08 05:57:37 -08:00
|
|
|
if (err) return c(err);
|
2014-05-23 17:23:44 -07:00
|
|
|
newHeight = height;
|
2014-03-05 18:03:56 -08:00
|
|
|
self.bDb.getNext(yHash, function(err, yHashNext) {
|
2014-05-23 17:23:44 -07:00
|
|
|
// Connect the new branch, and orphanize the old one.
|
2014-03-05 18:03:56 -08:00
|
|
|
orphanizeFrom = yHashNext;
|
|
|
|
self.bDb.setNext(yHash, newYHashNext, function(err) {
|
|
|
|
return c(err);
|
2014-02-04 14:55:58 -08:00
|
|
|
});
|
2014-02-08 05:57:37 -08:00
|
|
|
});
|
2014-02-03 22:22:58 -08:00
|
|
|
});
|
|
|
|
},
|
2014-03-05 18:03:56 -08:00
|
|
|
function(c) {
|
|
|
|
if (!orphanizeFrom) return c();
|
2014-05-23 17:23:44 -07:00
|
|
|
self._setBranchOrphan(orphanizeFrom, function(err) {
|
2014-03-05 18:03:56 -08:00
|
|
|
return c(err);
|
|
|
|
});
|
2014-02-14 11:35:32 -08:00
|
|
|
},
|
2014-03-05 18:03:56 -08:00
|
|
|
],
|
|
|
|
function(err) {
|
2014-05-23 17:23:44 -07:00
|
|
|
return cb(err, newHeight);
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
Sync.prototype._setBranchOrphan = function(fromHash, cb) {
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this,
|
|
|
|
hashInterator = fromHash;
|
|
|
|
|
|
|
|
async.whilst(
|
|
|
|
function() {
|
|
|
|
return hashInterator;
|
|
|
|
},
|
|
|
|
function(c) {
|
2014-05-23 17:23:44 -07:00
|
|
|
self.bDb.setBlockNotMain(hashInterator, function(err) {
|
2014-03-05 18:03:56 -08:00
|
|
|
if (err) return cb(err);
|
|
|
|
self.bDb.getNext(hashInterator, function(err, val) {
|
|
|
|
hashInterator = val;
|
|
|
|
return c(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, cb);
|
|
|
|
};
|
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
Sync.prototype.setBranchConnectedBackwards = function(fromHash, cb) {
|
|
|
|
//console.log('[Sync.js.219:setBranchConnectedBackwards:]',fromHash); //TODO
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this,
|
|
|
|
hashInterator = fromHash,
|
|
|
|
lastHash = fromHash,
|
2014-05-23 17:23:44 -07:00
|
|
|
yHeight,
|
|
|
|
branch = [];
|
2014-03-05 18:03:56 -08:00
|
|
|
|
|
|
|
async.doWhilst(
|
|
|
|
function(c) {
|
2014-05-23 17:23:44 -07:00
|
|
|
branch.unshift(hashInterator);
|
|
|
|
|
|
|
|
self.bDb.getPrev(hashInterator, function(err, val) {
|
2014-03-05 18:03:56 -08:00
|
|
|
if (err) return c(err);
|
2014-05-23 17:23:44 -07:00
|
|
|
lastHash = hashInterator;
|
|
|
|
hashInterator = val;
|
|
|
|
self.bDb.getHeight(hashInterator, function(err, height) {
|
|
|
|
yHeight = height;
|
|
|
|
return c();
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function() {
|
2014-05-25 13:34:49 -07:00
|
|
|
return hashInterator && yHeight<=0;
|
2014-03-05 18:03:56 -08:00
|
|
|
},
|
2014-05-23 17:23:44 -07:00
|
|
|
function() {
|
|
|
|
info('\tFound yBlock: %s #%d', hashInterator, yHeight);
|
|
|
|
var heightIter = yHeight + 1;
|
|
|
|
var hashIter;
|
|
|
|
async.whilst(
|
|
|
|
function() {
|
|
|
|
hashIter = branch.shift();
|
|
|
|
return hashIter;
|
|
|
|
},
|
|
|
|
function(c) {
|
2014-05-25 13:34:49 -07:00
|
|
|
self.bDb.setBlockMain(hashIter, heightIter++, c);
|
2014-05-23 17:23:44 -07:00
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
return cb(err, hashInterator, lastHash, heightIter);
|
|
|
|
});
|
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
2014-01-16 08:01:32 -08:00
|
|
|
|
2014-01-29 10:10:36 -08:00
|
|
|
|
2014-05-21 11:51:24 -07:00
|
|
|
//Store unconfirmed TXs
|
2014-03-05 18:03:56 -08:00
|
|
|
Sync.prototype.storeTxs = function(txs, cb) {
|
2014-05-23 17:23:44 -07:00
|
|
|
this.txDb.addMany(txs, cb);
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
2014-02-12 08:11:27 -08:00
|
|
|
|
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
module.exports = require('soop')(Sync);
|