Browse Source

paginated blocks

master
Mayank 5 years ago
parent
commit
a3d29d2818
No known key found for this signature in database GPG Key ID: D037D60476CE748C
  1. 62
      logic/bitcoind.js
  2. 8
      routes/v1/bitcoind/info.js
  3. 5
      services/bitcoind.js

62
logic/bitcoind.js

@ -103,20 +103,6 @@ async function getVersion() {
return { version: version }; // eslint-disable-line object-shorthand return { version: version }; // eslint-disable-line object-shorthand
} }
async function getBlock(hash) {
const blockObj = await bitcoindService.getBlock(hash);
return {
block: hash,
confirmations: blockObj.result.confirmations,
size: blockObj.result.size,
height: blockObj.result.height,
blocktime: blockObj.result.time,
prevblock: blockObj.result.previousblockhash,
nextblock: blockObj.result.nextblockhash,
transactions: blockObj.result.tx
}
}
async function getTransaction(txid) { async function getTransaction(txid) {
const transactionObj = await bitcoindService.getTransaction(txid); const transactionObj = await bitcoindService.getTransaction(txid);
return { return {
@ -137,6 +123,53 @@ async function getNetworkInfo() {
return networkInfo.result; // eslint-disable-line object-shorthand return networkInfo.result; // eslint-disable-line object-shorthand
} }
async function getBlock(hash) {
const blockObj = await bitcoindService.getBlock(hash);
return {
block: hash,
confirmations: blockObj.result.confirmations,
size: blockObj.result.size,
height: blockObj.result.height,
blocktime: blockObj.result.time,
prevblock: blockObj.result.previousblockhash,
nextblock: blockObj.result.nextblockhash,
transactions: blockObj.result.tx
}
}
async function getBlocks(fromHeight, toHeight) {
const startingBlockHashObj = await bitcoindService.getBlockHash(toHeight);
let currentHeight = toHeight;
let currentHash = startingBlockHashObj.result;
const blocks = [];
//loop until we reach "toHeight"
for (let currentHeight = toHeight; currentHeight >= fromHeight; currentHeight--) {
console.log(currentHeight);
const blockObj = await bitcoindService.getBlock(currentHash);
const block = blockObj.result;
currentHash = block.previousblockhash;
const formattedBlock = {
hash: block.hash,
height: block.height,
numTransactions: block.tx.length,
confirmations: block.confirmations,
time: block.time,
size: block.size
};
blocks.push(formattedBlock);
//terminate loop if we reach the genesis block
if (!currentHash) {
break;
}
}
return { blocks: blocks }; // eslint-disable-line object-shorthand
}
async function getBlockHash(height) { async function getBlockHash(height) {
const getBlockHashObj = await bitcoindService.getBlockHash(height); const getBlockHashObj = await bitcoindService.getBlockHash(height);
@ -179,6 +212,7 @@ module.exports = {
getTransaction, getTransaction,
getBlock, getBlock,
getBlockCount, getBlockCount,
getBlocks,
getConnectionsCount, getConnectionsCount,
getNetworkInfo, getNetworkInfo,
getMempoolInfo, getMempoolInfo,

8
routes/v1/bitcoind/info.js

@ -67,6 +67,14 @@ router.get('/block/:id', auth.jwt, safeHandler((req, res) =>
.then(blockhash => res.json(blockhash)) .then(blockhash => res.json(blockhash))
)); ));
router.get('/blocks', auth.jwt, safeHandler((req, res) => {
const fromHeight = parseInt(req.query.from);
const toHeight = parseInt(req.query.to);
bitcoind.getBlocks(fromHeight, toHeight)
.then(blocks => res.json(blocks))
}
));
router.get('/txid/:id', auth.jwt, safeHandler((req, res) => router.get('/txid/:id', auth.jwt, safeHandler((req, res) =>
bitcoind.getTransaction(req.params.id) bitcoind.getTransaction(req.params.id)
.then(txhash => res.json(txhash)) .then(txhash => res.json(txhash))

5
services/bitcoind.js

@ -64,6 +64,10 @@ function promiseifyParamTwo(rpcObj, rpcFn, param1, param2, what) {
}); });
} }
function getBestBlockHash() {
return promiseify(rpcClient, rpcClient.getBestBlockHash, 'best block hash');
}
function getBlockHash(height) { function getBlockHash(height) {
return promiseifyParam(rpcClient, rpcClient.getBlockHash, height, 'block height'); return promiseifyParam(rpcClient, rpcClient.getBlockHash, height, 'block height');
} }
@ -113,6 +117,7 @@ function help() {
module.exports = { module.exports = {
getMiningInfo, getMiningInfo,
getBestBlockHash,
getBlockHash, getBlockHash,
getBlock, getBlock,
getTransaction, getTransaction,

Loading…
Cancel
Save