diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index 535ada3..d7dc1e3 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -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, diff --git a/server/lib/db/blocks.js b/server/lib/db/blocks.js index 0cdba0c..0b8cd88 100644 --- a/server/lib/db/blocks.js +++ b/server/lib/db/blocks.js @@ -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, }; diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index bfd90c3..9b7ec2c 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -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, }; diff --git a/server/lib/node/index.js b/server/lib/node/index.js index bea48c0..c7a2415 100644 --- a/server/lib/node/index.js +++ b/server/lib/node/index.js @@ -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); }); diff --git a/server/lib/parser/index.js b/server/lib/parser/index.js index 13c2d5b..8989195 100644 --- a/server/lib/parser/index.js +++ b/server/lib/parser/index.js @@ -1,5 +1,7 @@ const Block = require('./block'); +const Transaction = require('./transaction'); module.exports = { Block, + Transaction, }; diff --git a/server/lib/parser/transaction.js b/server/lib/parser/transaction.js new file mode 100644 index 0000000..b727417 --- /dev/null +++ b/server/lib/parser/transaction.js @@ -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, +};