Bringing back other parsers

This commit is contained in:
tenthirtyone 2017-08-16 22:49:13 -04:00
parent 3e7f263e18
commit 1b0c1b1250
5 changed files with 62 additions and 48 deletions

View File

@ -4,7 +4,7 @@ const config = {
bcoin_http: 'localhost', bcoin_http: 'localhost',
bcoin: { bcoin: {
network: 'main', network: 'main',
db: 'mem', db: 'leveldb',
prefix: '.', prefix: '.',
checkpoints: true, checkpoints: true,
workers: false, workers: false,

View File

@ -13,65 +13,50 @@ module.exports = function transactionAPI(router) {
// Get max block height for calculating confirmations // Get max block height for calculating confirmations
const height = db.blocks.bestHeight(); const height = db.blocks.bestHeight();
// Bcoin transaction data // Bcoin transaction data
return request(`${API_URL}/tx/${req.params.txid}`, const txid = req.params.txid || '';
{ timeout: TTL },
(error, localRes, tx) => {
if (error) {
logger.log('error',
`${error}`);
return res.status(404).send();
}
// Catch JSON errors
try {
tx = JSON.parse(tx);
} catch (e) {
logger.log('error',
`${e}`);
return res.status(404).send();
}
if (!tx || !tx.hash) {
logger.log('error',
'No results found');
return res.status(404).send();
}
// Return UI JSON db.blocks.getTxById(txid, (err, transaction) => {
return res.send({ if (err) {
txid: tx.hash, logger.log('err',
version: tx.version, `getTxById: ${err}`);
time: tx.ps, return err;
blocktime: tx.ps, }
locktime: tx.locktime, const tx = transaction;
blockhash: tx.block, return res.send({
fees: tx.fee / 1e8, txid: tx.hash,
confirmations: (height - tx.height) + 1, version: tx.version,
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, time: tx.ps,
vin: tx.inputs.map(input => ({ blocktime: tx.ps,
addr: input.coin ? input.coin.address : '', locktime: tx.locktime,
value: input.coin ? input.coin.value / 1e8 : 0, blockhash: tx.block,
})), fees: tx.fee / 1e8,
vout: tx.outputs.map(output => ({ confirmations: (height - tx.height) + 1,
scriptPubKey: { valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
addresses: [output.address], vin: tx.inputs.map(input => ({
}, addr: input.coin ? input.coin.address : '',
value: output.value / 1e8, value: input.coin ? input.coin.value / 1e8 : 0,
})), })),
isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', vout: tx.outputs.map(output => ({
}); scriptPubKey: {
addresses: [output.address],
},
value: output.value / 1e8,
})),
isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
}); });
});
}); });
// /txs is overloaded. Next ver separate concerns // /txs is overloaded. Next ver separate concerns
// query by block // query by block
// query by address // query by address
// last n txs // last n txs - haha jk YOU 404
router.get('/txs', (req, res) => { router.get('/txs', (req, res) => {
const pageNum = parseInt(req.query.pageNum, 10) || 0; const pageNum = parseInt(req.query.pageNum, 10) || 0;
const rangeStart = pageNum * MAX_TXS; const rangeStart = pageNum * MAX_TXS;
const rangeEnd = rangeStart + MAX_TXS; const rangeEnd = rangeStart + MAX_TXS;
// get txs for blockhash, start with best height to calc confirmations // get txs for blockhash, start with best height to calc confirmations
if (req.query.block) { if (req.query.block) {
const height = db.blocks.bestHeight(); const height = db.blocks.bestHeight();
// Get Bcoin data // Get Bcoin data
return request(`${API_URL}/block/${req.query.block}`, return request(`${API_URL}/block/${req.query.block}`,

View File

@ -6,6 +6,8 @@ const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours
let bestBlockHeight = 0; let bestBlockHeight = 0;
// This naive querying will be replaced by more advanced mongo
function getBlocks(params, options, limit, cb) { function getBlocks(params, options, limit, cb) {
// Do not return mongo ids // Do not return mongo ids
const defaultOptions = { _id: 0 }; const defaultOptions = { _id: 0 };
@ -56,7 +58,7 @@ function getBlock(params, options, limit, cb) {
return cb(null, blocks[0]); return cb(null, blocks[0]);
}); });
} }
// Highest known height in mongo // Highest known height in mongo - Not Used
function getBestHeight() { function getBestHeight() {
getBlock({}, {}, 1, (err, block) => { getBlock({}, {}, 1, (err, block) => {
if (err) { if (err) {
@ -79,8 +81,25 @@ function bestHeight(height) {
return bestBlockHeight; return bestBlockHeight;
} }
function getTxById(txid, cb) {
getBlock(
{ 'txs.hash': txid },
{},
1,
(err, block) => {
if (err) {
logger.log('err',
`/rawblock/:blockHash: ${err}`);
return cb(err);
}
const transaction = block.txs.filter(tx => tx.hash === txid).reduce(a => a[0]);
return cb(null, transaction);
});
}
module.exports = { module.exports = {
getBlock, getBlock,
getBlocks, getBlocks,
bestHeight, bestHeight,
getTxById,
}; };

View File

@ -23,11 +23,13 @@ function parse(entry, block) {
nonce: blockJSON.nonce, nonce: blockJSON.nonce,
txs: block.txs.map((tx) => { txs: block.txs.map((tx) => {
const txJSON = tx.toJSON(); const txJSON = tx.toJSON();
const txRAW = tx.toRaw();
return { return {
hash: txJSON.hash, hash: txJSON.hash,
witnessHash: txJSON.witnessHash, witnessHash: txJSON.witnessHash,
fee: txJSON.fee, fee: txJSON.fee,
rate: txJSON.rate, rate: txJSON.rate,
size: txRAW.length,
ps: txJSON.ps, ps: txJSON.ps,
height: entry.height, height: entry.height,
block: util.revHex(entry.hash), block: util.revHex(entry.hash),
@ -71,6 +73,13 @@ function parse(entry, block) {
} }
}); });
} }
// Fill in behind blocks and update tx inputs
function updateInputs(txid, address) {
// Use txid and output address to get value
// Get addr / value from prev out
// update input
}
module.exports = { module.exports = {
parse, parse,

View File

@ -3,6 +3,7 @@ const mongoose = require('mongoose');
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
const InputSchema = new Schema({ const InputSchema = new Schema({
value: { type: Number, default: 0 },
prevout: { type: Object, default: {} }, prevout: { type: Object, default: {} },
script: { type: String, default: '' }, script: { type: String, default: '' },
witness: { type: String, default: '' }, witness: { type: String, default: '' },