Merge pull request #117 from kleetus/blockchain_synced

GetProgress, will report the GuessVerificationProgress for the blockchain
This commit is contained in:
Patrick Nagurny 2015-08-12 15:32:09 -04:00
commit 223aebf98c
7 changed files with 66 additions and 12 deletions

View File

@ -377,6 +377,8 @@ Note that if you already have a bitcore-node database, and you want to query dat
- `daemon.getTransactionWithBlockInfo(txid, queryMempool, callback)` - Similar to getTransaction but will also include the block timestamp and height.
- `daemon.getMempoolOutputs(address)` - Will return an array of outputs that match an address from the mempool.
- `daemon.getInfo()` - Basic information about the chain including total number of blocks.
- `daemon.isSynced()` - Returns a boolean if the daemon is fully synced (not the initial block download)
- `daemon.syncPercentage()` - Returns the current estimate of blockchain download as a percentage.
- `daemon.stop([callback])` - Stop the JavaScript bitcoin node safely, the callback will be called when bitcoind is closed. This will also be done automatically on `process.exit`. It also takes the bitcoind node off the libuv event loop. If the daemon object is the only thing on the event loop. Node will simply close.
## License

View File

@ -15,17 +15,23 @@ var configuration = {
var node = new BitcoinNode(configuration);
var count = 0;
var interval;
var interval = false;
function logSyncStatus() {
log.info('Sync Status: Tip:', node.chain.tip.hash, 'Height:', node.chain.tip.__height, 'Rate:', count/10, 'blocks per second');
}
node.on('synced', function() {
// Stop logging of sync status
clearInterval(interval);
interval = false;
logSyncStatus();
});
node.on('ready', function() {
var io = socketio(configuration.port);
interval = setInterval(function() {
log.info('Sync Status: Tip:', node.chain.tip.hash, 'Height:', node.chain.tip.__height, 'Rate:', count/10, 'blocks per second');
count = 0;
}, 10000);
io.on('connection', function(socket) {
var bus = node.openBus();
@ -135,4 +141,11 @@ node.on('error', function(err) {
node.chain.on('addblock', function(block) {
count++;
// Initialize logging if not already instantiated
if (!interval) {
interval = setInterval(function() {
logSyncStatus();
count = 0;
}, 10000);
}
});

View File

@ -233,6 +233,14 @@ Daemon.prototype.start = function(options, callback) {
}, 1000);
};
Daemon.prototype.isSynced = function() {
return bitcoind.isSynced();
};
Daemon.prototype.syncPercentage = function() {
return bitcoind.syncPercentage();
};
Daemon.prototype.getBlock = function(blockhash, callback) {
return bitcoind.getBlock(blockhash, callback);
};

View File

@ -223,8 +223,6 @@ Node.prototype._syncBitcoind = function() {
self.bitcoindSyncing = true;
self.chain.lastSavedMetadataThreshold = 30000;
log.info('Starting Bitcoind Sync');
var height;
async.whilst(function() {
@ -271,14 +269,19 @@ Node.prototype._syncBitcoind = function() {
}
});
}, function(err) {
log.info('Stopping Bitcoind Sync');
self.bitcoindSyncing = false;
self.chain.lastSavedMetadataThreshold = 0;
if (err) {
Error.captureStackTrace(err);
return self.emit('error', err);
}
self.emit('synced');
// If bitcoind is completely synced
if (self.bitcoind.isSynced()) {
self.emit('synced');
}
});
};
@ -383,7 +386,8 @@ Node.prototype._initializeBitcoind = function() {
// Notify that there is a new tip
this.bitcoind.on('tip', function(height) {
log.info('Bitcoin Core Daemon New Height:', height);
var percentage = self.bitcoind.syncPercentage();
log.info('Bitcoin Core Daemon New Height:', height, 'Percentage:', percentage);
self.bitcoindHeight = height;
self._syncBitcoind();
});

View File

@ -178,6 +178,28 @@ struct async_tx_data {
static bool
set_cooked(void);
/**
* SyncPercentage()
* bitcoind.syncPercentage()
* provides a float value >= indicating the progress of the blockchain sync
*/
NAN_METHOD(SyncPercentage) {
const CChainParams& chainParams = Params();
float progress = 0;
progress = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip());
NanReturnValue(NanNew<Number>(progress * 100));
};
/**
* IsSynced()
* bitcoind.isSynced()
* returns a boolean of bitcoin is fully synced
*/
NAN_METHOD(IsSynced) {
bool isDownloading = IsInitialBlockDownload();
NanReturnValue(NanNew<Boolean>(!isDownloading));
};
NAN_METHOD(StartTxMon) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
@ -1631,6 +1653,8 @@ init(Handle<Object> target) {
NODE_SET_METHOD(target, "sendTransaction", SendTransaction);
NODE_SET_METHOD(target, "estimateFee", EstimateFee);
NODE_SET_METHOD(target, "startTxMon", StartTxMon);
NODE_SET_METHOD(target, "syncPercentage", SyncPercentage);
NODE_SET_METHOD(target, "isSynced", IsSynced);
}

View File

@ -30,3 +30,5 @@ NAN_METHOD(AddMempoolUncheckedTransaction);
NAN_METHOD(SendTransaction);
NAN_METHOD(EstimateFee);
NAN_METHOD(StartTxMon);
NAN_METHOD(SyncPercentage);
NAN_METHOD(IsSynced);

View File

@ -217,7 +217,8 @@ describe('Bitcoind Node', function() {
var blockBuffer = new Buffer(blockData);
var block = Block.fromBuffer(blockBuffer);
node.bitcoind = {
getBlock: sinon.stub().callsArgWith(1, null, blockBuffer)
getBlock: sinon.stub().callsArgWith(1, null, blockBuffer),
isSynced: sinon.stub().returns(true)
};
node.chain = {
tip: {