bitcore-node-zcash/lib/Sync.js

264 lines
6.1 KiB
JavaScript

'use strict';
var imports = require('soop').imports();
var config = imports.config || require('../config/config');
var bitcore = require('bitcore');
var networks = bitcore.networks;
var async = require('async');
var syncId = 0;
function Sync(opts) {
this.id = syncId++;
this.opts = opts || {};
this.bDb = require('./BlockDb').default();
this.txDb = require('./TransactionDb').default();
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
}
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;
}
if (!b) return cb();
var self = this;
var oldTip, oldNext, needReorg = false;
var newPrev = b.previousblockhash;
async.series([
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();
return c(err ||
(!val ? new Error('NEED_SYNC Ignoring block with non existing prev:' + b.hash) : null));
});
},
function(c) {
self.txDb.createFromBlock(b, function(err) {
return c(err);
});
},
function(c) {
if (!allowReorgs) return c();
self.bDb.getTip(function(err, val) {
oldTip = val;
if (oldTip && newPrev !== oldTip) needReorg = true;
return c();
});
},
function(c) {
if (!needReorg) return c();
self.bDb.getNext(newPrev, function(err, val) {
if (err) return c(err);
oldNext = val;
return c();
});
},
function(c) {
self.bDb.add(b, c);
},
function(c) {
if (!needReorg) return c();
console.log('NEW TIP: %s NEED REORG (old tip: %s)', b.hash, oldTip);
self.processReorg(oldTip, oldNext, newPrev, c);
},
function(c) {
if (!allowReorgs) return c();
self.bDb.setTip(b.hash, function(err) {
return c(err);
});
},
function(c) {
self.bDb.setNext(newPrev, b.hash, function(err) {
return c(err);
});
}
],
function(err) {
if (err && err.toString().match(/WARN/)) {
err = null;
}
return cb(err);
});
};
Sync.prototype.processReorg = function(oldTip, oldNext, newPrev, cb) {
var self = this;
var orphanizeFrom;
async.series([
function(c) {
self.bDb.isMain(newPrev, function(err, val) {
if (!val) return c();
console.log('# Reorg Case 1)');
// case 1
orphanizeFrom = oldNext;
return c(err);
});
},
function(c) {
if (orphanizeFrom) return c();
console.log('# Reorg Case 2)');
self.setBranchConnectedBackwards(newPrev, function(err, yHash, newYHashNext) {
if (err) return c(err);
self.bDb.getNext(yHash, function(err, yHashNext) {
orphanizeFrom = yHashNext;
self.bDb.setNext(yHash, newYHashNext, function(err) {
return c(err);
});
});
});
},
function(c) {
if (!orphanizeFrom) return c();
self.setBranchOrphan(orphanizeFrom, function(err) {
return c(err);
});
},
],
function(err) {
return cb(err);
});
};
Sync.prototype.setBlockMain = function(hash, isMain, cb) {
var self = this;
self.bDb.setMain(hash, isMain, function(err) {
if (err) return cb(err);
return self.txDb.handleBlockChange(hash, isMain, cb);
});
};
Sync.prototype.setBranchOrphan = function(fromHash, cb) {
var self = this,
hashInterator = fromHash;
async.whilst(
function() {
return hashInterator;
},
function(c) {
self.setBlockMain(hashInterator, false, function(err) {
if (err) return cb(err);
self.bDb.getNext(hashInterator, function(err, val) {
hashInterator = val;
return c(err);
});
});
}, cb);
};
Sync.prototype.setBranchConnectedBackwards = function(fromHash, cb) {
var self = this,
hashInterator = fromHash,
lastHash = fromHash,
isMain;
async.doWhilst(
function(c) {
self.setBlockMain(hashInterator, true, function(err) {
if (err) return c(err);
self.bDb.getPrev(hashInterator, function(err, val) {
if (err) return c(err);
lastHash = hashInterator;
hashInterator = val;
self.bDb.isMain(hashInterator, function(err, val) {
isMain = val;
return c();
});
});
});
},
function() {
return hashInterator && !isMain;
},
function(err) {
console.log('\tFound yBlock:', hashInterator);
return cb(err, hashInterator, lastHash);
}
);
};
Sync.prototype.storeTxs = function(txs, cb) {
var self = this;
self.txDb.createFromArray(txs, null, function(err) {
if (err) return cb(err);
return cb(err);
});
};
module.exports = require('soop')(Sync);