2015-07-15 15:13:41 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
2015-08-31 06:00:00 -07:00
|
|
|
var fs = require('fs');
|
2015-07-15 15:13:41 -07:00
|
|
|
var async = require('async');
|
2015-08-26 12:18:58 -07:00
|
|
|
var levelup = require('levelup');
|
|
|
|
var leveldown = require('leveldown');
|
2015-08-31 06:00:00 -07:00
|
|
|
var mkdirp = require('mkdirp');
|
2015-07-15 15:13:41 -07:00
|
|
|
var bitcore = require('bitcore');
|
2015-08-31 06:00:00 -07:00
|
|
|
var BufferUtil = bitcore.util.buffer;
|
2015-08-31 06:00:00 -07:00
|
|
|
var Networks = bitcore.Networks;
|
2015-08-27 07:10:07 -07:00
|
|
|
var Block = bitcore.Block;
|
2015-07-21 13:59:08 -07:00
|
|
|
var $ = bitcore.util.preconditions;
|
2015-08-31 06:00:00 -07:00
|
|
|
var index = require('../');
|
2015-08-26 12:18:58 -07:00
|
|
|
var errors = index.errors;
|
|
|
|
var log = index.log;
|
2015-08-31 06:00:00 -07:00
|
|
|
var Transaction = require('../transaction');
|
2015-08-31 06:00:00 -07:00
|
|
|
var Service = require('../service');
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
/**
|
2015-08-31 06:00:00 -07:00
|
|
|
* Represents the current state of the bitcoin blockchain. Other services
|
2015-08-31 06:00:00 -07:00
|
|
|
* can extend the data that is indexed by implementing a `blockHandler` method.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} options.datadir - The bitcoin data directory
|
|
|
|
* @param {Node} options.node - A reference to the node
|
|
|
|
*/
|
2015-07-15 15:13:41 -07:00
|
|
|
function DB(options) {
|
2015-08-31 06:00:00 -07:00
|
|
|
/* jshint maxstatements: 20 */
|
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
if (!(this instanceof DB)) {
|
|
|
|
return new DB(options);
|
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
if (!options) {
|
2015-07-15 15:13:41 -07:00
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
Service.call(this, options);
|
2015-08-26 12:18:58 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
this.tip = null;
|
|
|
|
this.genesis = null;
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
$.checkState(this.node.network, 'Node is expected to have a "network" property');
|
|
|
|
this.network = this.node.network;
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
this._setDataPath();
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
this.levelupStore = leveldown;
|
|
|
|
if (options.store) {
|
|
|
|
this.levelupStore = options.store;
|
|
|
|
}
|
2015-08-24 13:33:44 -07:00
|
|
|
|
2015-08-06 13:19:36 -07:00
|
|
|
this.subscriptions = {
|
|
|
|
transaction: [],
|
|
|
|
block: []
|
|
|
|
};
|
2015-07-15 15:13:41 -07:00
|
|
|
}
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
util.inherits(DB, Service);
|
2015-08-31 06:00:00 -07:00
|
|
|
|
|
|
|
DB.dependencies = ['bitcoind'];
|
|
|
|
|
|
|
|
DB.prototype._setDataPath = function() {
|
|
|
|
$.checkState(this.node.datadir, 'Node is expected to have a "datadir" property');
|
|
|
|
var regtest = Networks.get('regtest');
|
|
|
|
if (this.node.network === Networks.livenet) {
|
|
|
|
this.dataPath = this.node.datadir + '/bitcore-node.db';
|
|
|
|
} else if (this.node.network === Networks.testnet) {
|
|
|
|
this.dataPath = this.node.datadir + '/testnet3/bitcore-node.db';
|
|
|
|
} else if (this.node.network === regtest) {
|
|
|
|
this.dataPath = this.node.datadir + '/regtest/bitcore-node.db';
|
|
|
|
} else {
|
|
|
|
throw new Error('Unknown network: ' + this.network);
|
|
|
|
}
|
2015-08-26 12:18:58 -07:00
|
|
|
};
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-20 14:50:14 -07:00
|
|
|
DB.prototype.start = function(callback) {
|
2015-09-04 12:05:32 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
var self = this;
|
2015-08-31 06:00:00 -07:00
|
|
|
if (!fs.existsSync(this.dataPath)) {
|
|
|
|
mkdirp.sync(this.dataPath);
|
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
this.genesis = Block.fromBuffer(this.node.services.bitcoind.genesisBuffer);
|
2015-08-31 06:00:00 -07:00
|
|
|
this.store = levelup(this.dataPath, { db: this.levelupStore });
|
2015-08-31 06:00:00 -07:00
|
|
|
this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this));
|
2015-08-31 06:00:00 -07:00
|
|
|
|
|
|
|
this.once('ready', function() {
|
|
|
|
log.info('Bitcoin Database Ready');
|
|
|
|
|
|
|
|
// Notify that there is a new tip
|
2015-08-31 06:00:00 -07:00
|
|
|
self.node.services.bitcoind.on('tip', function(height) {
|
2015-08-31 06:00:00 -07:00
|
|
|
if(!self.node.stopping) {
|
|
|
|
self.sync();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Does our database already have a tip?
|
|
|
|
self.getMetadata(function(err, metadata) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
} else if(!metadata || !metadata.tip) {
|
|
|
|
self.tip = self.genesis;
|
|
|
|
self.tip.__height = 0;
|
|
|
|
self.connectBlock(self.genesis, function(err) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('addblock', self.genesis);
|
|
|
|
self.saveMetadata();
|
|
|
|
self.sync();
|
|
|
|
self.emit('ready');
|
|
|
|
setImmediate(callback);
|
|
|
|
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.getBlock(metadata.tip, function(err, tip) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
self.tip = tip;
|
2015-09-04 10:52:41 -07:00
|
|
|
var blockIndex = self.node.services.bitcoind.getBlockIndex(self.tip.hash);
|
|
|
|
if (!blockIndex) {
|
|
|
|
return callback(new Error('Could not get height for tip.'));
|
|
|
|
}
|
|
|
|
self.tip.__height = blockIndex.height;
|
2015-08-31 06:00:00 -07:00
|
|
|
self.sync();
|
|
|
|
self.emit('ready');
|
|
|
|
setImmediate(callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-08-20 14:50:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype.stop = function(callback) {
|
2015-09-10 09:43:50 -07:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Wait until syncing stops and all db operations are completed before closing leveldb
|
|
|
|
async.whilst(function() {
|
|
|
|
return self.bitcoindSyncing;
|
|
|
|
}, function(next) {
|
|
|
|
setTimeout(next, 10);
|
|
|
|
}, function() {
|
|
|
|
self.store.close(callback);
|
|
|
|
});
|
2015-08-20 14:50:14 -07:00
|
|
|
};
|
2015-08-06 13:19:36 -07:00
|
|
|
|
2015-08-28 13:06:26 -07:00
|
|
|
DB.prototype.getInfo = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
setImmediate(function() {
|
|
|
|
var info = self.node.bitcoind.getInfo();
|
|
|
|
callback(null, info);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
/**
|
|
|
|
* Closes the underlying store database
|
|
|
|
* @param {Function} callback - A function that accepts: Error
|
|
|
|
*/
|
|
|
|
DB.prototype.close = function(callback) {
|
|
|
|
this.store.close(callback);
|
|
|
|
};
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
DB.prototype.transactionHandler = function(txInfo) {
|
|
|
|
var tx = Transaction().fromBuffer(txInfo.buffer);
|
|
|
|
for (var i = 0; i < this.subscriptions.transaction.length; i++) {
|
|
|
|
this.subscriptions.transaction[i].emit('transaction', {
|
|
|
|
rejected: !txInfo.mempool,
|
|
|
|
tx: tx
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype.getAPIMethods = function() {
|
|
|
|
var methods = [
|
|
|
|
['getBlock', this, this.getBlock, 1],
|
|
|
|
['getTransaction', this, this.getTransaction, 2],
|
|
|
|
['getTransactionWithBlockInfo', this, this.getTransactionWithBlockInfo, 2],
|
|
|
|
['sendTransaction', this, this.sendTransaction, 1],
|
|
|
|
['estimateFee', this, this.estimateFee, 1]
|
|
|
|
];
|
|
|
|
return methods;
|
|
|
|
};
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
DB.prototype.getBlock = function(hash, callback) {
|
2015-08-31 06:00:00 -07:00
|
|
|
this.node.services.bitcoind.getBlock(hash, function(err, blockData) {
|
2015-08-31 06:00:00 -07:00
|
|
|
if (err) {
|
2015-07-15 15:13:41 -07:00
|
|
|
return callback(err);
|
|
|
|
}
|
2015-08-27 07:10:07 -07:00
|
|
|
callback(null, Block.fromBuffer(blockData));
|
2015-07-15 15:13:41 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-16 11:09:10 -07:00
|
|
|
DB.prototype.getTransaction = function(txid, queryMempool, callback) {
|
2015-08-31 06:00:00 -07:00
|
|
|
this.node.services.bitcoind.getTransaction(txid, queryMempool, function(err, txBuffer) {
|
2015-08-31 06:00:00 -07:00
|
|
|
if (err) {
|
2015-07-16 11:09:10 -07:00
|
|
|
return callback(err);
|
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
if (!txBuffer) {
|
2015-08-03 15:11:11 -07:00
|
|
|
return callback(new errors.Transaction.NotFound());
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-16 11:09:10 -07:00
|
|
|
callback(null, Transaction().fromBuffer(txBuffer));
|
|
|
|
});
|
|
|
|
};
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-29 07:24:29 -07:00
|
|
|
DB.prototype.getTransactionWithBlockInfo = function(txid, queryMempool, callback) {
|
2015-08-31 06:00:00 -07:00
|
|
|
this.node.services.bitcoind.getTransactionWithBlockInfo(txid, queryMempool, function(err, obj) {
|
2015-08-31 06:00:00 -07:00
|
|
|
if (err) {
|
2015-07-29 07:24:29 -07:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var tx = Transaction().fromBuffer(obj.buffer);
|
2015-08-28 09:57:01 -07:00
|
|
|
tx.__blockHash = obj.blockHash;
|
2015-07-29 14:13:51 -07:00
|
|
|
tx.__height = obj.height;
|
2015-07-29 07:24:29 -07:00
|
|
|
tx.__timestamp = obj.timestamp;
|
|
|
|
|
|
|
|
callback(null, tx);
|
2015-07-29 14:13:51 -07:00
|
|
|
});
|
2015-08-03 15:11:11 -07:00
|
|
|
};
|
2015-07-29 07:24:29 -07:00
|
|
|
|
2015-08-06 13:19:36 -07:00
|
|
|
DB.prototype.sendTransaction = function(tx, callback) {
|
2015-08-31 06:00:00 -07:00
|
|
|
if (tx instanceof Transaction) {
|
2015-08-06 13:19:36 -07:00
|
|
|
tx = tx.toString();
|
|
|
|
}
|
|
|
|
$.checkArgument(typeof tx === 'string', 'Argument must be a hex string or Transaction');
|
|
|
|
|
|
|
|
try {
|
2015-08-31 06:00:00 -07:00
|
|
|
var txid = this.node.services.bitcoind.sendTransaction(tx);
|
2015-08-06 13:19:36 -07:00
|
|
|
return callback(null, txid);
|
|
|
|
} catch(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype.estimateFee = function(blocks, callback) {
|
|
|
|
var self = this;
|
|
|
|
setImmediate(function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
callback(null, self.node.services.bitcoind.estimateFee(blocks));
|
2015-08-06 13:19:36 -07:00
|
|
|
});
|
2015-08-24 13:33:44 -07:00
|
|
|
};
|
2015-08-06 13:19:36 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
DB.prototype.getPublishEvents = function() {
|
|
|
|
return [
|
|
|
|
{
|
2015-09-03 14:29:28 -07:00
|
|
|
name: 'db/transaction',
|
2015-08-31 06:00:00 -07:00
|
|
|
scope: this,
|
|
|
|
subscribe: this.subscribe.bind(this, 'transaction'),
|
|
|
|
unsubscribe: this.unsubscribe.bind(this, 'transaction')
|
|
|
|
},
|
|
|
|
{
|
2015-09-03 14:29:28 -07:00
|
|
|
name: 'db/block',
|
2015-08-31 06:00:00 -07:00
|
|
|
scope: this,
|
|
|
|
subscribe: this.subscribe.bind(this, 'block'),
|
|
|
|
unsubscribe: this.unsubscribe.bind(this, 'block')
|
|
|
|
}
|
|
|
|
];
|
2015-07-20 10:48:13 -07:00
|
|
|
};
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
DB.prototype.subscribe = function(name, emitter) {
|
|
|
|
this.subscriptions[name].push(emitter);
|
2015-07-22 12:34:15 -07:00
|
|
|
};
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
DB.prototype.unsubscribe = function(name, emitter) {
|
|
|
|
var index = this.subscriptions[name].indexOf(emitter);
|
|
|
|
if (index > -1) {
|
|
|
|
this.subscriptions[name].splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will give the previous hash for a block.
|
|
|
|
* @param {String} blockHash
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
DB.prototype.getPrevHash = function(blockHash, callback) {
|
2015-08-31 06:00:00 -07:00
|
|
|
var blockIndex = this.node.services.bitcoind.getBlockIndex(blockHash);
|
2015-08-31 06:00:00 -07:00
|
|
|
setImmediate(function() {
|
|
|
|
if (blockIndex) {
|
|
|
|
callback(null, blockIndex.prevHash);
|
|
|
|
} else {
|
|
|
|
callback(new Error('Could not get prevHash, block not found'));
|
|
|
|
}
|
|
|
|
});
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
/**
|
|
|
|
* Saves metadata to the database
|
|
|
|
* @param {Function} callback - A function that accepts: Error
|
|
|
|
*/
|
2015-08-31 06:00:00 -07:00
|
|
|
DB.prototype.saveMetadata = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
|
2015-09-01 11:29:04 -07:00
|
|
|
function defaultCallback(err) {
|
|
|
|
if (err) {
|
|
|
|
self.emit('error', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback = callback || defaultCallback;
|
2015-08-31 06:00:00 -07:00
|
|
|
|
|
|
|
var metadata = {
|
2015-09-04 10:52:41 -07:00
|
|
|
tip: self.tip ? self.tip.hash : null
|
2015-08-31 06:00:00 -07:00
|
|
|
};
|
|
|
|
|
2015-09-01 11:29:04 -07:00
|
|
|
this.store.put('metadata', JSON.stringify(metadata), {}, callback);
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
};
|
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
/**
|
|
|
|
* Retrieves metadata from the database
|
|
|
|
* @param {Function} callback - A function that accepts: Error and Object
|
|
|
|
*/
|
|
|
|
DB.prototype.getMetadata = function(callback) {
|
|
|
|
var self = this;
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
self.store.get('metadata', {}, function(err, data) {
|
2015-08-31 06:00:00 -07:00
|
|
|
if (err instanceof levelup.errors.NotFoundError) {
|
2015-08-26 12:18:58 -07:00
|
|
|
return callback(null, {});
|
2015-08-31 06:00:00 -07:00
|
|
|
} else if (err) {
|
2015-08-26 12:18:58 -07:00
|
|
|
return callback(err);
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
var metadata;
|
|
|
|
try {
|
|
|
|
metadata = JSON.parse(data);
|
|
|
|
} catch(e) {
|
|
|
|
return callback(new Error('Could not parse metadata'));
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
callback(null, metadata);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-08-31 06:00:00 -07:00
|
|
|
* Connects a block to the database and add indexes
|
|
|
|
* @param {Block} block - The bitcore block
|
|
|
|
* @param {Function} callback
|
2015-08-26 12:18:58 -07:00
|
|
|
*/
|
2015-08-31 06:00:00 -07:00
|
|
|
DB.prototype.connectBlock = function(block, callback) {
|
2015-07-15 15:13:41 -07:00
|
|
|
log.debug('DB handling new chain block');
|
2015-08-31 06:00:00 -07:00
|
|
|
this.runAllBlockHandlers(block, true, callback);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
/**
|
|
|
|
* Disconnects a block from the database and removes indexes
|
|
|
|
* @param {Block} block - The bitcore block
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
DB.prototype.disconnectBlock = function(block, callback) {
|
2015-07-21 13:59:08 -07:00
|
|
|
log.debug('DB removing chain block');
|
2015-08-31 06:00:00 -07:00
|
|
|
this.runAllBlockHandlers(block, false, callback);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
/**
|
2015-08-31 06:00:00 -07:00
|
|
|
* Will collect all database operations for a block from other services
|
2015-08-31 06:00:00 -07:00
|
|
|
* and save to the database.
|
|
|
|
* @param {Block} block - The bitcore block
|
|
|
|
* @param {Boolean} add - If the block is being added/connected or removed/disconnected
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
DB.prototype.runAllBlockHandlers = function(block, add, callback) {
|
2015-07-15 15:13:41 -07:00
|
|
|
var self = this;
|
2015-07-21 13:59:08 -07:00
|
|
|
var operations = [];
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-06 13:19:36 -07:00
|
|
|
// Notify block subscribers
|
2015-08-31 06:00:00 -07:00
|
|
|
for (var i = 0; i < this.subscriptions.block.length; i++) {
|
2015-08-18 13:16:07 -07:00
|
|
|
this.subscriptions.block[i].emit('block', block.hash);
|
2015-08-06 13:19:36 -07:00
|
|
|
}
|
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
async.eachSeries(
|
2015-08-31 06:00:00 -07:00
|
|
|
this.node.services,
|
2015-08-31 06:00:00 -07:00
|
|
|
function(mod, next) {
|
2015-09-09 11:30:25 -07:00
|
|
|
if(mod.blockHandler) {
|
|
|
|
$.checkArgument(typeof mod.blockHandler === 'function', 'blockHandler must be a function');
|
|
|
|
|
|
|
|
mod.blockHandler.call(mod, block, add, function(err, ops) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
if (ops) {
|
|
|
|
$.checkArgument(Array.isArray(ops), 'blockHandler for ' + mod.name + ' returned non-array');
|
|
|
|
operations = operations.concat(ops);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setImmediate(next);
|
|
|
|
}
|
2015-07-21 13:59:08 -07:00
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
log.debug('Updating the database with operations', operations);
|
|
|
|
self.store.batch(operations, callback);
|
2015-07-20 12:38:56 -07:00
|
|
|
}
|
2015-07-21 13:59:08 -07:00
|
|
|
);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
/**
|
|
|
|
* This function will find the common ancestor between the current chain and a forked block,
|
|
|
|
* by moving backwards from the forked block until it meets the current chain.
|
|
|
|
* @param {Block} block - The new tip that forks the current chain.
|
|
|
|
* @param {Function} done - A callback function that is called when complete.
|
|
|
|
*/
|
|
|
|
DB.prototype.findCommonAncestor = function(block, done) {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
var mainPosition = self.tip.hash;
|
|
|
|
var forkPosition = block.hash;
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
var mainHashesMap = {};
|
|
|
|
var forkHashesMap = {};
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-04 10:52:41 -07:00
|
|
|
mainHashesMap[mainPosition] = true;
|
2015-09-04 08:32:04 -07:00
|
|
|
forkHashesMap[forkPosition] = true;
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
var commonAncestor = null;
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
async.whilst(
|
|
|
|
function() {
|
|
|
|
return !commonAncestor;
|
|
|
|
},
|
|
|
|
function(next) {
|
2015-09-04 10:52:41 -07:00
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
if(mainPosition) {
|
2015-09-04 10:52:41 -07:00
|
|
|
var mainBlockIndex = self.node.services.bitcoind.getBlockIndex(mainPosition);
|
2015-09-04 08:32:04 -07:00
|
|
|
if(mainBlockIndex && mainBlockIndex.prevHash) {
|
|
|
|
mainHashesMap[mainBlockIndex.prevHash] = true;
|
|
|
|
mainPosition = mainBlockIndex.prevHash;
|
|
|
|
} else {
|
|
|
|
mainPosition = null;
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
if(forkPosition) {
|
2015-09-04 10:52:41 -07:00
|
|
|
var forkBlockIndex = self.node.services.bitcoind.getBlockIndex(forkPosition);
|
2015-09-04 08:32:04 -07:00
|
|
|
if(forkBlockIndex && forkBlockIndex.prevHash) {
|
|
|
|
forkHashesMap[forkBlockIndex.prevHash] = true;
|
|
|
|
forkPosition = forkBlockIndex.prevHash;
|
|
|
|
} else {
|
|
|
|
forkPosition = null;
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
if(forkPosition && mainHashesMap[forkPosition]) {
|
|
|
|
commonAncestor = forkPosition;
|
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
if(mainPosition && forkHashesMap[mainPosition]) {
|
|
|
|
commonAncestor = mainPosition;
|
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
if(!mainPosition && !forkPosition) {
|
|
|
|
return next(new Error('Unknown common ancestor'));
|
|
|
|
}
|
|
|
|
|
|
|
|
setImmediate(next);
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
done(err, commonAncestor);
|
|
|
|
}
|
|
|
|
);
|
2015-08-31 06:00:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will attempt to rewind the chain to the common ancestor
|
|
|
|
* between the current chain and a forked block.
|
|
|
|
* @param {Block} block - The new tip that forks the current chain.
|
|
|
|
* @param {Function} done - A callback function that is called when complete.
|
|
|
|
*/
|
|
|
|
DB.prototype.syncRewind = function(block, done) {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.findCommonAncestor(block, function(err, ancestorHash) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-09-04 10:52:41 -07:00
|
|
|
log.warn('Reorg common ancestor found:', ancestorHash);
|
2015-08-31 06:00:00 -07:00
|
|
|
// Rewind the chain to the common ancestor
|
|
|
|
async.whilst(
|
|
|
|
function() {
|
|
|
|
// Wait until the tip equals the ancestor hash
|
|
|
|
return self.tip.hash !== ancestorHash;
|
|
|
|
},
|
|
|
|
function(removeDone) {
|
|
|
|
|
|
|
|
var tip = self.tip;
|
|
|
|
|
|
|
|
// TODO: expose prevHash as a string from bitcore
|
|
|
|
var prevHash = BufferUtil.reverse(tip.header.prevHash).toString('hex');
|
|
|
|
|
|
|
|
self.getBlock(prevHash, function(err, previousTip) {
|
|
|
|
if (err) {
|
|
|
|
removeDone(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undo the related indexes for this block
|
|
|
|
self.disconnectBlock(tip, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return removeDone(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the new tip
|
|
|
|
previousTip.__height = self.tip.__height - 1;
|
|
|
|
self.tip = previousTip;
|
|
|
|
self.saveMetadata();
|
|
|
|
self.emit('removeblock', tip);
|
|
|
|
removeDone();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}, done
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will synchronize additional indexes for the chain based on
|
|
|
|
* the current active chain in the bitcoin daemon. In the event that there is
|
|
|
|
* a reorganization in the daemon, the chain will rewind to the last common
|
|
|
|
* ancestor and then resume syncing.
|
|
|
|
*/
|
|
|
|
DB.prototype.sync = function() {
|
|
|
|
var self = this;
|
|
|
|
|
2015-09-10 09:43:50 -07:00
|
|
|
if (self.bitcoindSyncing || self.node.stopping || !self.tip) {
|
2015-08-31 06:00:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.bitcoindSyncing = true;
|
|
|
|
|
|
|
|
var height;
|
|
|
|
|
|
|
|
async.whilst(function() {
|
|
|
|
height = self.tip.__height;
|
2015-08-31 06:00:00 -07:00
|
|
|
return height < self.node.services.bitcoind.height && !self.node.stopping;
|
2015-08-31 06:00:00 -07:00
|
|
|
}, function(done) {
|
2015-08-31 06:00:00 -07:00
|
|
|
self.node.services.bitcoind.getBlock(height + 1, function(err, blockBuffer) {
|
2015-08-31 06:00:00 -07:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var block = Block.fromBuffer(blockBuffer);
|
|
|
|
|
|
|
|
// TODO: expose prevHash as a string from bitcore
|
|
|
|
var prevHash = BufferUtil.reverse(block.header.prevHash).toString('hex');
|
|
|
|
|
|
|
|
if (prevHash === self.tip.hash) {
|
|
|
|
|
|
|
|
// This block appends to the current chain tip and we can
|
|
|
|
// immediately add it to the chain and create indexes.
|
|
|
|
|
|
|
|
// Populate height
|
|
|
|
block.__height = self.tip.__height + 1;
|
|
|
|
|
2015-09-04 08:32:04 -07:00
|
|
|
// Create indexes
|
|
|
|
self.connectBlock(block, function(err) {
|
2015-08-31 06:00:00 -07:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-09-04 08:32:04 -07:00
|
|
|
self.tip = block;
|
|
|
|
log.debug('Saving metadata');
|
|
|
|
self.saveMetadata();
|
|
|
|
log.debug('Chain added block to main chain');
|
|
|
|
self.emit('addblock', block);
|
|
|
|
setImmediate(done);
|
2015-08-31 06:00:00 -07:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// This block doesn't progress the current tip, so we'll attempt
|
|
|
|
// to rewind the chain to the common ancestor of the block and
|
|
|
|
// then we can resume syncing.
|
2015-09-03 13:07:35 -07:00
|
|
|
log.warn('Beginning reorg! Current tip: ' + self.tip.hash + '; New tip: ' + block.hash);
|
|
|
|
self.syncRewind(block, function(err) {
|
|
|
|
if(err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-09-03 13:07:35 -07:00
|
|
|
log.warn('Reorg complete. New tip is ' + self.tip.hash);
|
|
|
|
done();
|
|
|
|
});
|
2015-08-31 06:00:00 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
Error.captureStackTrace(err);
|
|
|
|
return self.node.emit('error', err);
|
|
|
|
}
|
|
|
|
|
2015-09-10 09:43:50 -07:00
|
|
|
self.bitcoindSyncing = false;
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
if(self.node.stopping) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If bitcoind is completely synced
|
2015-08-31 06:00:00 -07:00
|
|
|
if (self.node.services.bitcoind.isSynced()) {
|
2015-08-31 06:00:00 -07:00
|
|
|
self.node.emit('synced');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-07-15 15:13:41 -07:00
|
|
|
module.exports = DB;
|