general: updated controllers for new api from bitcoind

This commit is contained in:
Braydon Fuller 2016-03-17 12:18:03 -04:00
parent c42b8d9d91
commit e4f585ad15
4 changed files with 139 additions and 96 deletions

View File

@ -37,11 +37,14 @@ BlockController.prototype.block = function(req, res, next, hash) {
return common.handleErrors(err, res); return common.handleErrors(err, res);
} }
var info = self.node.services.bitcoind.getBlockIndex(hash); self.node.services.bitcoind.getBlockHeader(hash, function(err, info) {
info.isMainChain = self.node.services.bitcoind.isMainChain(hash); if (err) {
return common.handleErrors(err, res);
}
req.block = self.transformBlock(block, info);
next();
});
req.block = self.transformBlock(block, info);
next();
}); });
}; };
@ -52,7 +55,7 @@ BlockController.prototype.transformBlock = function(block, info) {
}); });
return { return {
hash: block.hash, hash: block.hash,
confirmations: this.node.services.db.tip.__height - info.height + 1, confirmations: this.node.services.bitcoind.height - info.height + 1,
size: block.toBuffer().length, size: block.toBuffer().length,
height: info.height, height: info.height,
version: blockObj.header.version, version: blockObj.header.version,
@ -62,11 +65,11 @@ BlockController.prototype.transformBlock = function(block, info) {
nonce: blockObj.header.nonce, nonce: blockObj.header.nonce,
bits: blockObj.header.bits.toString(16), bits: blockObj.header.bits.toString(16),
difficulty: block.header.getDifficulty(), difficulty: block.header.getDifficulty(),
chainwork: info.chainWork, chainwork: info.chainwork,
previousblockhash: blockObj.header.prevHash, previousblockhash: blockObj.header.prevHash,
nextblockhash: this.node.services.bitcoind.getNextBlockHash(block.hash), nextblockhash: info.nextblockhash,
reward: this.getBlockReward(info.height) / 1e8, reward: this.getBlockReward(info.height) / 1e8,
isMainChain: info.isMainChain, isMainChain: (info.confirmations !== -1),
poolInfo: this.getPoolInfo(block) poolInfo: this.getPoolInfo(block)
}; };
}; };
@ -81,13 +84,13 @@ BlockController.prototype.show = function(req, res) {
}; };
BlockController.prototype.blockIndex = function(req, res, next, height) { BlockController.prototype.blockIndex = function(req, res, next, height) {
var info = this.node.services.bitcoind.getBlockIndex(parseInt(height)); this.node.services.bitcoind.getBlockHeader(parseInt(height), function(err, info) {
if(!info) { if (err) {
return common.handleErrors(null, res); return common.handleErrors(err, res);
} }
res.jsonp({
res.jsonp({ blockHash: info.hash
blockHash: info.hash });
}); });
}; };
@ -139,6 +142,7 @@ BlockController.prototype.list = function(req, res) {
if(err) { if(err) {
return next(err); return next(err);
} }
var br = new bitcore.encoding.BufferReader(blockBuffer); var br = new bitcore.encoding.BufferReader(blockBuffer);
// take a shortcut to get number of transactions and the blocksize. // take a shortcut to get number of transactions and the blocksize.
// Also reads the coinbase transaction and only that. // Also reads the coinbase transaction and only that.
@ -151,21 +155,23 @@ BlockController.prototype.list = function(req, res) {
var txlength = br.readVarintNum(); var txlength = br.readVarintNum();
info.transactions = [bitcore.Transaction().fromBufferReader(br)]; info.transactions = [bitcore.Transaction().fromBufferReader(br)];
var index = self.node.services.bitcoind.getBlockIndex(hash); self.node.services.bitcoind.getBlockHeader(hash, function(err, blockHeader) {
var block = {
height: index.height, var block = {
height: blockHeader.height,
size: blockBuffer.length, size: blockBuffer.length,
hash: hash, hash: hash,
time: header.time, time: header.time,
txlength: txlength, txlength: txlength,
poolInfo: self.getPoolInfo(info) poolInfo: self.getPoolInfo(info)
}; };
if(moreTs > header.timestamp) { if(moreTs > header.timestamp) {
moreTs = header.timestamp; moreTs = header.timestamp;
} }
return next(null, block); return next(null, block);
});
}); });
}, },
function(err, blocks) { function(err, blocks) {

View File

@ -51,7 +51,7 @@ var InsightAPI = function(options) {
this.txController = new TxController(this.node); this.txController = new TxController(this.node);
}; };
InsightAPI.dependencies = ['address', 'web']; InsightAPI.dependencies = ['bitcoind', 'web'];
inherits(InsightAPI, BaseService); inherits(InsightAPI, BaseService);

View File

@ -1,78 +1,112 @@
'use strict'; 'use strict';
var common = require('./common');
function StatusController(node) { function StatusController(node) {
this.node = node; this.node = node;
} }
StatusController.prototype.show = function(req, res) { StatusController.prototype.show = function(req, res) {
var option = req.query.q; var option = req.query.q;
switch(option) { switch(option) {
case 'getDifficulty': case 'getDifficulty':
res.jsonp(this.getDifficulty()); this.getDifficulty(function(err, result) {
break; if (err) {
case 'getLastBlockHash': return common.handleErrors(err, res);
res.jsonp(this.getLastBlockHash()); }
break; res.jsonp(result);
case 'getBestBlockHash': });
res.jsonp(this.getBestBlockHash()); break;
break; case 'getLastBlockHash':
case 'getInfo': res.jsonp(this.getLastBlockHash());
default: break;
res.jsonp(this.getInfo()); case 'getBestBlockHash':
this.getBestBlockHash(function(err, result) {
if (err) {
return common.handleErrors(err, res);
}
res.jsonp(result);
});
break;
case 'getInfo':
default:
this.getInfo(function(err, result) {
if (err) {
return common.handleErrors(err, res);
}
res.jsonp({
info: result
});
});
} }
}; };
StatusController.prototype.getInfo = function() { StatusController.prototype.getInfo = function(callback) {
var info = this.node.services.bitcoind.getInfo(); this.node.services.bitcoind.getInfo(callback);
return {
info: info
};
}; };
StatusController.prototype.getLastBlockHash = function() { StatusController.prototype.getLastBlockHash = function() {
var hash = this.node.services.db.tip.hash; var hash = this.node.services.bitcoind.tiphash;
return { return {
syncTipHash: hash, syncTipHash: hash,
lastblockhash: hash lastblockhash: hash
}; };
}; };
StatusController.prototype.getBestBlockHash = function() { StatusController.prototype.getBestBlockHash = function(callback) {
var hash = this.node.services.bitcoind.getBestBlockHash(); this.node.services.bitcoind.getBestBlockHash(function(err, hash) {
return { if (err) {
bestblockhash: hash return callback(err);
}; }
callback(null, {
bestblockhash: hash
});
});
}; };
StatusController.prototype.getDifficulty = function() { StatusController.prototype.getDifficulty = function(callback) {
var info = this.node.services.bitcoind.getInfo(); this.node.services.bitcoind.getInfo(function(err, info) {
return { if (err) {
difficulty: info.difficulty return callback(err);
}; }
callback(null, {
difficulty: info.difficulty
});
});
}; };
StatusController.prototype.sync = function(req, res) { StatusController.prototype.sync = function(req, res) {
var self = this;
var status = 'syncing'; var status = 'syncing';
if(this.node.services.bitcoind.isSynced() && this.node.services.db.tip.__height === this.node.services.bitcoind.height) {
status = 'finished';
}
// Not exactly the total blockchain height, this.node.services.bitcoind.isSynced(function(err, synced) {
// but we will reach 100% when our db and bitcoind are both fully synced if (err) {
var totalHeight = this.node.services.bitcoind.height / (this.node.services.bitcoind.syncPercentage() / 100); return common.handleErrors(err, res);
}
if (synced) {
status = 'finished';
}
var info = { self.node.services.bitcoind.syncPercentage(function(err, percentage) {
status: status, if (err) {
blockChainHeight: this.node.services.bitcoind.height, return common.handleErrors(err, res);
syncPercentage: Math.round(this.node.services.db.tip.__height / totalHeight * 100), }
height: this.node.services.db.tip.__height, var info = {
error: null, status: status,
type: 'bitcore node' blockChainHeight: self.node.services.bitcoind.height,
}; syncPercentage: Math.round(percentage),
height: self.node.services.bitcoind.height,
error: null,
type: 'bitcore node'
};
res.jsonp(info);
});
});
res.jsonp(info);
}; };
// Hard coded to make insight ui happy, but not applicable // Hard coded to make insight ui happy, but not applicable

View File

@ -31,7 +31,7 @@ TxController.prototype.transaction = function(req, res, next, txid) {
return common.handleErrors(err, res); return common.handleErrors(err, res);
} }
transaction.populateInputs(self.node.services.db, [], function(err) { transaction.populateInputs(self.node.services.bitcoind, [], function(err) {
if(err) { if(err) {
return res.send({ return res.send({
error: err.toString() error: err.toString()
@ -58,7 +58,7 @@ TxController.prototype.transformTransaction = function(transaction, callback) {
var confirmations = 0; var confirmations = 0;
if(transaction.__height >= 0) { if(transaction.__height >= 0) {
confirmations = this.node.services.db.tip.__height - transaction.__height + 1; confirmations = this.node.services.bitcoind.height - transaction.__height + 1;
} }
var transformed = { var transformed = {
@ -179,7 +179,7 @@ TxController.prototype.transformOutput = function(txid, output, index, callback)
queryMempool: true queryMempool: true
}; };
self.node.services.address.getInputForOutput( self.node.services.bitcoind.getInputForOutput(
txid, txid,
index, index,
options, options,
@ -269,36 +269,39 @@ TxController.prototype.list = function(req, res) {
return common.handleErrors(err, res); return common.handleErrors(err, res);
} }
var blockInfo = self.node.services.bitcoind.getBlockIndex(block.hash); self.node.services.bitcoind.getBlockHeader(block.hash, function(err, blockInfo) {
var txs = block.transactions; var txs = block.transactions;
var totalTxs = txs.length; var totalTxs = txs.length;
if(!_.isUndefined(page)) { if(!_.isUndefined(page)) {
txs = txs.splice(page * pageLength, pageLength); txs = txs.splice(page * pageLength, pageLength);
pagesTotal = Math.ceil(totalTxs / pageLength); pagesTotal = Math.ceil(totalTxs / pageLength);
}
async.mapSeries(txs, function(tx, next) {
tx.__blockHash = block.hash;
tx.__height = blockInfo.height;
tx.__timestamp = block.header.time;
tx.populateInputs(self.node.services.db, [], function(err) {
if(err) {
return next(err);
}
self.transformTransaction(tx, next);
});
}, function(err, transformed) {
if(err) {
return common.handleErrors(err, res);
} }
res.jsonp({ async.mapSeries(txs, function(tx, next) {
pagesTotal: pagesTotal, tx.__blockHash = block.hash;
txs: transformed tx.__height = blockInfo.height;
tx.__timestamp = block.header.time;
tx.populateInputs(self.node.services.bitcoind, [], function(err) {
if(err) {
return next(err);
}
self.transformTransaction(tx, next);
});
}, function(err, transformed) {
if(err) {
return common.handleErrors(err, res);
}
res.jsonp({
pagesTotal: pagesTotal,
txs: transformed
});
}); });
}); });
}); });
} else if(address) { } else if(address) {
var options = { var options = {