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-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-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)
|
|
|
|
* 2) Declare D-E orphans (and possible invalidate TXs on them)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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-20 14:07:25 -07:00
|
|
|
var oldTip, oldNext, height, needReorg = false;
|
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([
|
2014-01-19 07:33:39 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
function(c) {
|
|
|
|
self.bDb.has(b.hash, function(err, val) {
|
|
|
|
return c(err ||
|
|
|
|
(val ? new Error('WARN: Ignoring already existing block:' + b.hash) : null));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(c) {
|
|
|
|
if (!allowReorgs) return c();
|
|
|
|
self.bDb.has(newPrev, function(err, val) {
|
|
|
|
if (!val && newPrev.match(/^0+$/)) return c();
|
2014-02-14 11:35:32 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
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;
|
|
|
|
if (!hash) height = -1
|
|
|
|
else height = h || 0;
|
|
|
|
if (oldTip && newPrev !== oldTip) {
|
|
|
|
needReorg = true;
|
|
|
|
console.log('## REORG Triggered, tip mismatch');
|
|
|
|
}
|
2014-03-05 18:03:56 -08:00
|
|
|
return c();
|
|
|
|
});
|
|
|
|
},
|
2014-05-21 11:51:24 -07:00
|
|
|
function(c) {
|
|
|
|
self.txDb.createFromBlock(b, height, function(err) {
|
|
|
|
return c(err);
|
|
|
|
});
|
|
|
|
},
|
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();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(c) {
|
2014-05-20 14:07:25 -07:00
|
|
|
self.bDb.add(b, height + 1, c);
|
2014-03-05 18:03:56 -08:00
|
|
|
},
|
|
|
|
function(c) {
|
|
|
|
if (!needReorg) return c();
|
2014-05-20 14:07:25 -07:00
|
|
|
console.log('NEW TIP: %s #%d NEED REORG (old tip: %s)', b.hash, height + 1, oldTip);
|
|
|
|
self.processReorg(oldTip, oldNext, newPrev, height + 1, c);
|
2014-03-05 18:03:56 -08:00
|
|
|
},
|
|
|
|
function(c) {
|
|
|
|
if (!allowReorgs) return c();
|
2014-05-20 14:07:25 -07:00
|
|
|
self.bDb.setTip(b.hash, height + 1, 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-02-08 11:20:51 -08:00
|
|
|
|
2014-05-20 14:07:25 -07:00
|
|
|
Sync.prototype.processReorg = function(oldTip, oldNext, newPrev, newHeight, cb) {
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this;
|
2014-02-04 14:55:58 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var orphanizeFrom;
|
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) {
|
|
|
|
if (!height) return c();
|
2014-02-08 05:57:37 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
console.log('# Reorg Case 1)');
|
|
|
|
// case 1
|
|
|
|
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-03-05 18:03:56 -08:00
|
|
|
console.log('# Reorg Case 2)');
|
2014-05-20 14:07:25 -07:00
|
|
|
self.setBranchConnectedBackwards(newPrev, newHeight-1, function(err, yHash, newYHashNext) {
|
2014-02-08 05:57:37 -08:00
|
|
|
if (err) return c(err);
|
2014-03-05 18:03:56 -08:00
|
|
|
self.bDb.getNext(yHash, function(err, yHashNext) {
|
|
|
|
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();
|
|
|
|
self.setBranchOrphan(orphanizeFrom, function(err) {
|
|
|
|
return c(err);
|
|
|
|
});
|
2014-02-14 11:35:32 -08:00
|
|
|
},
|
2014-03-05 18:03:56 -08:00
|
|
|
],
|
|
|
|
function(err) {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-05-20 14:07:25 -07:00
|
|
|
//height = false => unconnnected
|
|
|
|
Sync.prototype.setBlockHeight = function(hash, height, cb) {
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this;
|
2014-05-20 14:07:25 -07:00
|
|
|
self.bDb.setHeight(hash, height, function(err) {
|
2014-03-05 18:03:56 -08:00
|
|
|
if (err) return cb(err);
|
2014-05-21 11:51:24 -07:00
|
|
|
return self.txDb.handleBlockChange(hash, height>-1? true : false, cb);
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Sync.prototype.setBranchOrphan = function(fromHash, cb) {
|
|
|
|
var self = this,
|
|
|
|
hashInterator = fromHash;
|
|
|
|
|
|
|
|
async.whilst(
|
|
|
|
function() {
|
|
|
|
return hashInterator;
|
|
|
|
},
|
|
|
|
function(c) {
|
2014-05-20 14:07:25 -07:00
|
|
|
self.setBlockHeight(hashInterator, false, 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-20 14:07:25 -07:00
|
|
|
Sync.prototype.setBranchConnectedBackwards = function(fromHash, initialHeight, cb) {
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this,
|
|
|
|
hashInterator = fromHash,
|
|
|
|
lastHash = fromHash,
|
|
|
|
isMain;
|
|
|
|
|
|
|
|
async.doWhilst(
|
|
|
|
function(c) {
|
2014-05-21 11:51:24 -07:00
|
|
|
self.setBlockHeight(hashInterator, initialHeight--, function(err) {
|
2014-03-05 18:03:56 -08:00
|
|
|
if (err) return c(err);
|
|
|
|
self.bDb.getPrev(hashInterator, function(err, val) {
|
|
|
|
if (err) return c(err);
|
|
|
|
lastHash = hashInterator;
|
|
|
|
hashInterator = val;
|
2014-05-21 11:51:24 -07:00
|
|
|
self.bDb.getHeight(hashInterator, function(err, height) {
|
|
|
|
isMain = height ? 1 : 0;
|
2014-03-05 18:03:56 -08:00
|
|
|
return c();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
return hashInterator && !isMain;
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
console.log('\tFound yBlock:', hashInterator);
|
|
|
|
return cb(err, hashInterator, lastHash);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
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) {
|
|
|
|
var self = this;
|
2014-05-21 11:51:24 -07:00
|
|
|
self.txDb.createFromArray(txs, null, null, function(err) {
|
2014-03-05 18:03:56 -08:00
|
|
|
if (err) return cb(err);
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
};
|
2014-02-12 08:11:27 -08:00
|
|
|
|
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
module.exports = require('soop')(Sync);
|