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

View File

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

View File

@ -1,78 +1,112 @@
'use strict';
var common = require('./common');
function StatusController(node) {
this.node = node;
}
StatusController.prototype.show = function(req, res) {
var option = req.query.q;
switch(option) {
case 'getDifficulty':
res.jsonp(this.getDifficulty());
break;
case 'getLastBlockHash':
res.jsonp(this.getLastBlockHash());
break;
case 'getBestBlockHash':
res.jsonp(this.getBestBlockHash());
break;
case 'getInfo':
default:
res.jsonp(this.getInfo());
case 'getDifficulty':
this.getDifficulty(function(err, result) {
if (err) {
return common.handleErrors(err, res);
}
res.jsonp(result);
});
break;
case 'getLastBlockHash':
res.jsonp(this.getLastBlockHash());
break;
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() {
var info = this.node.services.bitcoind.getInfo();
return {
info: info
};
StatusController.prototype.getInfo = function(callback) {
this.node.services.bitcoind.getInfo(callback);
};
StatusController.prototype.getLastBlockHash = function() {
var hash = this.node.services.db.tip.hash;
var hash = this.node.services.bitcoind.tiphash;
return {
syncTipHash: hash,
lastblockhash: hash
};
};
StatusController.prototype.getBestBlockHash = function() {
var hash = this.node.services.bitcoind.getBestBlockHash();
return {
bestblockhash: hash
};
StatusController.prototype.getBestBlockHash = function(callback) {
this.node.services.bitcoind.getBestBlockHash(function(err, hash) {
if (err) {
return callback(err);
}
callback(null, {
bestblockhash: hash
});
});
};
StatusController.prototype.getDifficulty = function() {
var info = this.node.services.bitcoind.getInfo();
return {
difficulty: info.difficulty
};
StatusController.prototype.getDifficulty = function(callback) {
this.node.services.bitcoind.getInfo(function(err, info) {
if (err) {
return callback(err);
}
callback(null, {
difficulty: info.difficulty
});
});
};
StatusController.prototype.sync = function(req, res) {
var self = this;
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,
// but we will reach 100% when our db and bitcoind are both fully synced
var totalHeight = this.node.services.bitcoind.height / (this.node.services.bitcoind.syncPercentage() / 100);
this.node.services.bitcoind.isSynced(function(err, synced) {
if (err) {
return common.handleErrors(err, res);
}
if (synced) {
status = 'finished';
}
var info = {
status: status,
blockChainHeight: this.node.services.bitcoind.height,
syncPercentage: Math.round(this.node.services.db.tip.__height / totalHeight * 100),
height: this.node.services.db.tip.__height,
error: null,
type: 'bitcore node'
};
self.node.services.bitcoind.syncPercentage(function(err, percentage) {
if (err) {
return common.handleErrors(err, res);
}
var info = {
status: status,
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

View File

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