bitcore-node-zcash/lib/Sync.js

290 lines
7.1 KiB
JavaScript
Raw Normal View History

2014-01-10 11:02:33 -08:00
'use strict';
2014-01-07 10:21:59 -08:00
2014-01-10 11:02:33 -08:00
require('classtool');
2014-01-10 11:02:33 -08:00
function spec() {
2014-02-14 11:35:32 -08:00
var sockets = require('../app/controllers/socket.js');
var BlockDb = require('./BlockDb').class();
var TransactionDb = require('./TransactionDb').class();
2014-02-12 08:11:27 -08:00
var config = require('../config/config');
var networks = require('bitcore/networks');
2014-02-14 11:35:32 -08:00
var async = require('async');
2014-01-15 12:36:49 -08:00
function Sync(opts) {
this.opts = opts || {};
2014-02-14 11:35:32 -08:00
this.bDb = new BlockDb(opts);
this.txDb = new TransactionDb(opts);
this.txDb.on('tx_for_address', this.handleTxForAddress.bind(this));
this.txDb.on('new_tx', this.handleNewTx.bind(this));
this.bDb.on('new_block', this.handleNewBlock.bind(this));
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
}
2014-01-18 13:28:24 -08:00
2014-02-05 06:23:41 -08:00
Sync.prototype.close = function(cb) {
var self = this;
self.txDb.close(function() {
self.bDb.close(cb);
});
2014-01-18 13:28:24 -08:00
};
Sync.prototype.destroy = function(next) {
var self = this;
async.series([
2014-02-14 11:35:32 -08:00
function(b) {
self.bDb.drop(b);
},
function(b) {
self.txDb.drop(b);
},
], next);
};
2014-01-30 18:16:43 -08:00
2014-02-08 05:57:37 -08:00
/*
* 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)
2014-02-14 11:35:32 -08:00
* \
2014-02-08 05:57:37 -08:00
* NEW
*
* 1) Declare D-E orphans (and possible invalidate TXs on them)
*
* Case 2)
* A-B-C-D-E(TIP)
2014-02-14 11:35:32 -08:00
* \
2014-02-08 05:57:37 -08:00
* F-G-NEW
* 1) Set F-G as connected (mark TXs as valid)
2014-02-14 11:35:32 -08:00
* 2) Declare D-E orphans (and possible invalidate TXs on them)
2014-02-08 05:57:37 -08:00
*
*
* Case 3)
*
* A-B-C-D-E(TIP) ... NEW
*
* NEW is ignored (if allowReorgs is false)
*
2014-02-14 11:35:32 -08:00
*
2014-02-08 05:57:37 -08:00
*/
2014-02-03 18:30:46 -08:00
2014-02-08 15:09:54 -08:00
Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) {
if (typeof allowReorgs === 'function') {
cb = allowReorgs;
allowReorgs = true;
}
if (!b) return cb();
var self = this;
2014-02-08 15:09:54 -08:00
var oldTip, oldNext, needReorg = false;
2014-02-08 05:57:37 -08:00
var newPrev = b.previousblockhash;
2014-01-15 12:36:49 -08:00
2014-02-08 05:57:37 -08:00
async.series([
2014-02-14 11:35:32 -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
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
});
},
function(c) {
self.txDb.createFromBlock(b, function(err) {
2014-02-09 14:33:39 -08:00
return c(err);
});
2014-02-14 11:35:32 -08:00
},
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();
2014-02-14 11:35:32 -08:00
self.bDb.setTip(b.hash, function(err) {
return c(err);
});
},
function(c) {
self.bDb.setNext(newPrev, b.hash, function(err) {
return c(err);
2014-02-14 11:35:32 -08:00
});
}
2014-02-14 11:35:32 -08:00
],
2014-02-08 05:57:37 -08:00
function(err) {
2014-02-14 11:35:32 -08:00
if (err && err.toString().match(/WARN/)) {
err = null;
}
2014-02-08 05:57:37 -08:00
return cb(err);
2014-02-03 18:30:46 -08:00
});
};
2014-02-08 05:57:37 -08:00
Sync.prototype.processReorg = function(oldTip, oldNext, newPrev, cb) {
2014-02-03 22:22:58 -08:00
var self = this;
var orphanizeFrom;
2014-02-03 22:22:58 -08:00
2014-02-08 05:57:37 -08:00
async.series([
2014-02-14 11:35:32 -08:00
function(c) {
self.bDb.isMain(newPrev, function(err, val) {
if (!val) return c();
2014-02-04 14:55:58 -08:00
2014-02-14 11:35:32 -08:00
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);
});
});
2014-02-08 05:57:37 -08:00
});
2014-02-14 11:35:32 -08:00
},
function(c) {
if (!orphanizeFrom) return c();
self.setBranchOrphan(orphanizeFrom, function(err) {
return c(err);
});
},
],
function(err) {
return cb(err);
});
2014-02-08 05:57:37 -08:00
};
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);
});
};
2014-02-08 05:57:37 -08:00
Sync.prototype.setBranchOrphan = function(fromHash, cb) {
var self = this,
2014-02-14 11:35:32 -08:00
hashInterator = fromHash;
2014-02-08 05:57:37 -08:00
async.whilst(
2014-02-14 11:35:32 -08:00
function() {
return hashInterator;
},
2014-02-08 05:57:37 -08:00
function(c) {
self.setBlockMain(hashInterator, false, function(err) {
if (err) return cb(err);
2014-02-14 11:35:32 -08:00
self.bDb.getNext(hashInterator, function(err, val) {
2014-02-08 05:57:37 -08:00
hashInterator = val;
return c(err);
});
});
}, cb);
};
Sync.prototype.setBranchConnectedBackwards = function(fromHash, cb) {
var self = this,
2014-02-14 11:35:32 -08:00
hashInterator = fromHash,
lastHash = fromHash,
isMain;
2014-02-08 05:57:37 -08:00
async.doWhilst(
function(c) {
2014-02-14 11:35:32 -08:00
self.setBlockMain(hashInterator, true, function(err) {
2014-02-08 05:57:37 -08:00
if (err) return c(err);
2014-02-14 11:35:32 -08:00
self.bDb.getPrev(hashInterator, function(err, val) {
2014-02-08 05:57:37 -08:00
if (err) return c(err);
2014-02-14 11:35:32 -08:00
lastHash = hashInterator;
2014-02-08 05:57:37 -08:00
hashInterator = val;
2014-02-14 11:35:32 -08:00
self.bDb.isMain(hashInterator, function(err, val) {
2014-02-08 05:57:37 -08:00
isMain = val;
return c();
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-02-14 11:35:32 -08:00
function() {
return hashInterator && !isMain;
},
function(err) {
console.log('\tFound yBlock:', hashInterator);
return cb(err, hashInterator, lastHash);
}
);
2014-02-03 22:22:58 -08:00
};
2014-01-16 08:01:32 -08:00
2014-01-29 10:10:36 -08:00
2014-02-14 11:35:32 -08:00
Sync.prototype.handleTxForAddress = function(data) {
if (this.opts.shouldBroadcast) {
sockets.broadcastAddressTx(data.address, data.txid);
}
};
2014-01-16 08:01:32 -08:00
2014-02-14 11:35:32 -08:00
Sync.prototype.handleNewTx = function(data) {
if (this.opts.shouldBroadcast) {
2014-02-14 11:49:22 -08:00
sockets.broadcastTx(data.tx);
2014-02-14 11:35:32 -08:00
}
};
Sync.prototype.handleNewBlock = function(data) {
if (this.opts.shouldBroadcast) {
sockets.broadcastBlock(data.blockid);
2014-01-29 10:10:36 -08:00
}
};
Sync.prototype.storeTxs = function(txs, cb) {
var self = this;
2014-02-14 11:35:32 -08:00
self.txDb.createFromArray(txs, null, function(err) {
2014-01-29 10:10:36 -08:00
if (err) return cb(err);
2014-01-16 08:01:32 -08:00
return cb(err);
});
};
2014-02-12 08:11:27 -08:00
return Sync;
2014-01-10 11:02:33 -08:00
}
2014-01-07 10:21:59 -08:00
module.defineClass(spec);