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,12 +37,15 @@ 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); req.block = self.transformBlock(block, info);
next(); next();
}); });
});
}; };
BlockController.prototype.transformBlock = function(block, info) { BlockController.prototype.transformBlock = function(block, info) {
@ -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,14 +84,14 @@ 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
}); });
});
}; };
// List blocks by date // List blocks by date
@ -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,9 +155,10 @@ 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 = { var block = {
height: index.height, height: blockHeader.height,
size: blockBuffer.length, size: blockBuffer.length,
hash: hash, hash: hash,
time: header.time, time: header.time,
@ -167,6 +172,7 @@ BlockController.prototype.list = function(req, res) {
return next(null, block); return next(null, block);
}); });
});
}, },
function(err, blocks) { function(err, blocks) {
if(err) { if(err) {

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) {
if (err) {
return common.handleErrors(err, res);
}
res.jsonp(result);
});
break; break;
case 'getLastBlockHash': case 'getLastBlockHash':
res.jsonp(this.getLastBlockHash()); res.jsonp(this.getLastBlockHash());
break; break;
case 'getBestBlockHash': case 'getBestBlockHash':
res.jsonp(this.getBestBlockHash()); this.getBestBlockHash(function(err, result) {
if (err) {
return common.handleErrors(err, res);
}
res.jsonp(result);
});
break; break;
case 'getInfo': case 'getInfo':
default: default:
res.jsonp(this.getInfo()); 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) {
return callback(err);
}
callback(null, {
bestblockhash: hash 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) {
return callback(err);
}
callback(null, {
difficulty: info.difficulty 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) {
this.node.services.bitcoind.isSynced(function(err, synced) {
if (err) {
return common.handleErrors(err, res);
}
if (synced) {
status = 'finished'; status = 'finished';
} }
// Not exactly the total blockchain height, self.node.services.bitcoind.syncPercentage(function(err, percentage) {
// 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);
}
var info = { var info = {
status: status, status: status,
blockChainHeight: this.node.services.bitcoind.height, blockChainHeight: self.node.services.bitcoind.height,
syncPercentage: Math.round(this.node.services.db.tip.__height / totalHeight * 100), syncPercentage: Math.round(percentage),
height: this.node.services.db.tip.__height, height: self.node.services.bitcoind.height,
error: null, error: null,
type: 'bitcore node' 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,7 +269,7 @@ 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;
@ -283,7 +283,7 @@ TxController.prototype.list = function(req, res) {
tx.__height = blockInfo.height; tx.__height = blockInfo.height;
tx.__timestamp = block.header.time; tx.__timestamp = block.header.time;
tx.populateInputs(self.node.services.db, [], function(err) { tx.populateInputs(self.node.services.bitcoind, [], function(err) {
if(err) { if(err) {
return next(err); return next(err);
} }
@ -299,6 +299,9 @@ TxController.prototype.list = function(req, res) {
txs: transformed txs: transformed
}); });
}); });
});
}); });
} else if(address) { } else if(address) {
var options = { var options = {