tx size, tx parser, mongo replies for tx by hash

This commit is contained in:
tenthirtyone 2017-08-16 23:07:06 -04:00
parent 1b0c1b1250
commit a5d74393d1
6 changed files with 84 additions and 22 deletions

View File

@ -15,12 +15,13 @@ module.exports = function transactionAPI(router) {
// Bcoin transaction data
const txid = req.params.txid || '';
db.blocks.getTxById(txid, (err, transaction) => {
db.txs.getTxById(txid, (err, transaction) => {
if (err) {
logger.log('err',
`getTxById: ${err}`);
return err;
return res.status(400).send();
}
const tx = transaction;
return res.send({
txid: tx.hash,

View File

@ -81,25 +81,8 @@ function bestHeight(height) {
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 = {
getBlock,
getBlocks,
bestHeight,
getTxById,
};

View File

@ -2,9 +2,6 @@ const Transactions = require('../../models/transaction.js');
const logger = require('../logger');
const config = require('../../config');
// For now, blocks handles these calls.
// These will be replaced with more advanced mongo
const MAX_TXS = config.api.max_txs;
function getTransactions(params, options, limit, cb) {
@ -57,7 +54,25 @@ function getTransaction(params, options, limit, cb) {
});
}
function getTxById(txid, cb) {
getTransaction(
{ hash: txid },
{},
1,
(err, transaction) => {
if (err) {
logger.log('err',
`/rawblock/:blockHash: ${err}`);
return cb(err);
}
console.log(transaction);
return cb(null, transaction);
});
}
module.exports = {
getTransaction,
getTransactions,
getTxById,
};

View File

@ -1,6 +1,7 @@
const FullNode = require('bcoin/lib/node/fullnode');
const logger = require('../../lib/logger');
const BlockParser = require('../parser').Block;
const TxParser = require('../parser').Transaction;
const config = require('../../config');
const socket = require('../../lib/api/socket');
const db = require('../../lib/db');
@ -18,6 +19,7 @@ function start() {
node.chain.on('connect', (entry, block) => {
BlockParser.parse(entry, block);
TxParser.parse(entry, block.txs);
socket.processBlock(entry, block);
db.blocks.bestHeight(entry.height);
});

View File

@ -1,5 +1,7 @@
const Block = require('./block');
const Transaction = require('./transaction');
module.exports = {
Block,
Transaction,
};

View File

@ -0,0 +1,59 @@
const TxModel = require('../../models/transaction');
const InputModel = require('../../models/input');
const OutputModel = require('../../models/output');
const config = require('../../config');
const util = require('../../lib/util');
const logger = require('../logger');
function parse(entry, txs) {
txs.forEach((tx) => {
const txJSON = tx.toJSON();
const txRAW = tx.toRaw();
const t = new TxModel({
hash: txJSON.hash,
witnessHash: txJSON.witnessHash,
fee: txJSON.fee,
rate: txJSON.rate,
size: txRAW.length,
ps: txJSON.ps,
height: entry.height,
block: util.revHex(entry.hash),
ts: entry.ts,
date: txJSON.date,
index: txJSON.index,
version: txJSON.version,
flag: txJSON.flag,
inputs: tx.inputs.map((input) => {
const inputJSON = input.toJSON();
return new InputModel({
prevout: inputJSON.prevout,
script: inputJSON.script,
witness: inputJSON.witness,
sequence: inputJSON.sequence,
address: inputJSON.address,
});
}),
outputs: tx.outputs.map((output) => {
const outputJSON = output.toJSON();
return new OutputModel({
address: outputJSON.address,
script: outputJSON.script,
value: outputJSON.value,
});
}),
lockTime: txJSON.locktime,
chain: config.bcoin.network,
});
t.save((err) => {
if (err) {
logger.log('error', err.message);
}
});
});
}
module.exports = {
parse,
};