From b1e540c8df91fa7c2b2a3c5a21dd7786273a25df Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Tue, 15 Aug 2017 02:00:04 -0400 Subject: [PATCH] RPC hookups fill the gaps way better than mongo in some places. See changes --- server/config/index.js | 6 +- server/lib/api/block.js | 3 +- server/lib/api/transaction.js | 231 ++++++++++++++-------------------- server/models/block.js | 3 + server/models/transaction.js | 2 + 5 files changed, 105 insertions(+), 140 deletions(-) diff --git a/server/config/index.js b/server/config/index.js index 66878ce..0467c00 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -1,15 +1,17 @@ const config = { - start_node: false, + start_node: true, logging: 'debug', bcoin: { network: 'main', db: 'leveldb', prefix: '.', checkpoints: true, - workers: true, + workers: false, logLevel: 'info', 'max-inbound': 10, 'max-outbound': 10, + 'index-tx': true, + 'index-address': true, }, mongodb: { uri: 'mongodb://localhost/bitcore', diff --git a/server/lib/api/block.js b/server/lib/api/block.js index cbc1c01..36e0b53 100644 --- a/server/lib/api/block.js +++ b/server/lib/api/block.js @@ -1,7 +1,7 @@ const Block = require('../../models/block.js'); const logger = require('../logger'); -const MAX_BLOCKS = 200; +const MAX_BLOCKS = 100; function getBlock(params, options, limit, cb) { const defaultOptions = { _id: 0 }; @@ -76,6 +76,7 @@ module.exports = function BlockAPI(router) { res.status(501).send(); logger.log('err', err); } + res.json({ blocks: blocks.map(block => ({ hash: block.hash, diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index 6fd7b1c..2154f7b 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -1,6 +1,7 @@ const Block = require('../../models/block.js'); const Transaction = require('../../models/transaction'); const logger = require('../logger'); +const request = require('request'); const MAX_TXS = 20; const MAX_BLOCKS = 1; @@ -39,151 +40,107 @@ function getTransactions(params, options, cb) { module.exports = function transactionAPI(router) { router.get('/tx/:txid', (req, res) => { - getBlock( - { 'txs.hash': req.params.txid }, - { }, - MAX_BLOCKS, - (err, blocks) => { - if (err) { - res.status(501).send(); - logger.log('err', err); - } - - if (blocks[0] && blocks[0].txs) { - let t = blocks[0].txs.filter(tx => tx.hash === req.params.txid); - t = t[0]; - console.log(t); - console.log(t.inputs); - console.log(t.inputs); - // Map bcoin model to insight-api - res.json({ - txid: t.hash, - version: t.version, - locktime: t.lockTime, - vin: t.inputs.map(input => ({ - coinbase: input.script, - sequence: input.sequence, - n: 0, - })), - vout: t.outputs.map(output => ({ - value: output.value / 1e8, - n: 0, - scriptPubKey: { - hex: output.script, - asm: '', - addresses: [output.address], - type: null, - }, - spentTxId: null, - spentIndex: null, - spentHeight: null, - })), - blockhash: t.block, - blockheight: t.height, - confirmations: 0, - time: 0, - blocktime: 0, - isCoinBase: false, - valueOut: t.outputs.reduce((a, b) => a.value + b.value).value / 1e8, - size: 0, - }); - } else { - res.send(); - } + request(`http://localhost:8332/tx/${req.params.txid}`, (err, localRes, body) => { + if (err) { + logger.log('error', + `${err}`); + } + try { + body = JSON.parse(body); + } catch (e) { + logger.log('error', + `${err}`); + } + res.send({ + txid: body.hash, + version: body.version, + time: body.ps, + blocktime: body.ps, + locktime: body.locktime, + blockhash: body.block, + fees: body.fee, + valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, + vin: body.inputs.map(input => ({ + addr: input.coin ? input.coin.address : '', + value: input.coin ? input.coin.value / 1e8 : 0, + })), + vout: body.outputs.map(output => ({ + scriptPubKey: { + addresses: [output.address], + }, + value: output.value / 1e8, + })), + isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', }); + }); }); router.get('/txs', (req, res) => { if (req.query.block) { - getBlock( - { hash: req.query.block }, - { rawBlock: 0 }, - MAX_BLOCKS, - (err, block) => { - if (err) { - res.status(501).send(); - logger.log('err', err); - } - if (block[0]) { - const b = block[0]; - res.json({ - pagesTotal: 1, - txs: b.txs.map(tx => ({ - txid: tx.hash, - version: tx.version, - locktime: tx.locktime, - vin: tx.inputs.map(input => ({ - coinbase: input.script, - sequence: input.sequence, - n: 0, - addr: input.address, - })), - vout: tx.outputs.map(output => ({ - value: output.value / 1e8, - n: 0, - scriptPubKey: { - hex: '', - asm: '', - addresses: [output.address], - type: output.type, - }, - spentTxid: '', - spentIndex: 0, - spentHeight: 0, - })), - })), - }); - } else { - res.send(); - } + request(`http://localhost:8332/block/${req.query.block}`, (err, localRes, body) => { + if (err) { + logger.log('error', + `${err}`); + } + try { + body = JSON.parse(body); + } catch (e) { + logger.log('error', + `${err}`); + } + res.send({ + pagesTotal: 1, + txs: body.txs.map(tx => ({ + txid: tx.hash, + fees: tx.fee, + valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, + vin: tx.inputs.map(input => ({ + addr: input.coin ? input.coin.address : '', + value: input.coin ? input.coin.value / 1e8 : 0, + })), + vout: tx.outputs.map(output => ({ + scriptPubKey: { + addresses: [output.address], + }, + value: output.value / 1e8, + })), + output: tx.outputs, + })), }); + }); } else if (req.query.address) { - getBlock( - { $or: - [ - { 'txs.outputs.address': req.query.address }, - { 'txs.inputs.prevout.hash': req.query.address }, - ], - }, - { rawBlock: 0 }, - MAX_BLOCKS, - (err, block) => { - if (err) { - res.status(501).send(); - logger.log('err', err); - } else if (block[0]) { - const b = block[0]; - res.json({ - pagesTotal: 1, - txs: b.txs.map(tx => ({ - txid: tx.hash, - version: tx.version, - locktime: tx.locktime, - vin: tx.inputs.map(input => ({ - coinbase: input.script, - sequence: input.sequence, - n: 0, - addr: input.address, - })), - vout: tx.outputs.map(output => ({ - value: output.value / 1e8, - n: 0, - scriptPubKey: { - hex: '', - asm: '', - addresses: [output.address], - type: output.type, - }, - spentTxid: '', - spentIndex: 0, - spentHeight: 0, - })), - })), - }); - } else { - res.send(); - } + request(`http://localhost:8332/tx/address/${req.query.address}`, (err, localRes, body) => { + if (err) { + logger.log('error', + `${err}`); + } + try { + body = JSON.parse(body); + } catch (e) { + logger.log('error', + `${err}`); + } + console.log(body); + res.send({ + pagesTotal: 1, + txs: body.map(tx => ({ + txid: tx.hash, + fees: tx.fee, + valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, + vin: tx.inputs.map(input => ({ + addr: input.coin ? input.coin.address : '', + value: input.coin ? input.coin.value / 1e8 : 0, + })), + vout: tx.outputs.map(output => ({ + scriptPubKey: { + addresses: [output.address], + }, + value: output.value / 1e8, + })), + output: tx.outputs, + })), }); + }); } else { getTransactions( {}, diff --git a/server/models/block.js b/server/models/block.js index 3de6806..515fe70 100644 --- a/server/models/block.js +++ b/server/models/block.js @@ -26,6 +26,9 @@ const BlockSchema = new Schema({ id: false, }); +BlockSchema.index({ hash: 1 }); +BlockSchema.index({ height: 1 }); + const Block = mongoose.model('Block', BlockSchema); module.exports = Block; diff --git a/server/models/transaction.js b/server/models/transaction.js index 73c4f4d..ec0af09 100644 --- a/server/models/transaction.js +++ b/server/models/transaction.js @@ -22,6 +22,8 @@ const TransactionSchema = new Schema({ network: String, }); +TransactionSchema.index({ hash: 1 }); + const Transaction = mongoose.model('Transaction', TransactionSchema); module.exports = Transaction;