RPC hookups fill the gaps way better than mongo in some places. See changes

This commit is contained in:
tenthirtyone 2017-08-15 02:00:04 -04:00
parent 90adbf3ece
commit b1e540c8df
5 changed files with 105 additions and 140 deletions

View File

@ -1,15 +1,17 @@
const config = { const config = {
start_node: false, start_node: true,
logging: 'debug', logging: 'debug',
bcoin: { bcoin: {
network: 'main', network: 'main',
db: 'leveldb', db: 'leveldb',
prefix: '.', prefix: '.',
checkpoints: true, checkpoints: true,
workers: true, workers: false,
logLevel: 'info', logLevel: 'info',
'max-inbound': 10, 'max-inbound': 10,
'max-outbound': 10, 'max-outbound': 10,
'index-tx': true,
'index-address': true,
}, },
mongodb: { mongodb: {
uri: 'mongodb://localhost/bitcore', uri: 'mongodb://localhost/bitcore',

View File

@ -1,7 +1,7 @@
const Block = require('../../models/block.js'); const Block = require('../../models/block.js');
const logger = require('../logger'); const logger = require('../logger');
const MAX_BLOCKS = 200; const MAX_BLOCKS = 100;
function getBlock(params, options, limit, cb) { function getBlock(params, options, limit, cb) {
const defaultOptions = { _id: 0 }; const defaultOptions = { _id: 0 };
@ -76,6 +76,7 @@ module.exports = function BlockAPI(router) {
res.status(501).send(); res.status(501).send();
logger.log('err', err); logger.log('err', err);
} }
res.json({ res.json({
blocks: blocks.map(block => ({ blocks: blocks.map(block => ({
hash: block.hash, hash: block.hash,

View File

@ -1,6 +1,7 @@
const Block = require('../../models/block.js'); const Block = require('../../models/block.js');
const Transaction = require('../../models/transaction'); const Transaction = require('../../models/transaction');
const logger = require('../logger'); const logger = require('../logger');
const request = require('request');
const MAX_TXS = 20; const MAX_TXS = 20;
const MAX_BLOCKS = 1; const MAX_BLOCKS = 1;
@ -39,150 +40,106 @@ function getTransactions(params, options, cb) {
module.exports = function transactionAPI(router) { module.exports = function transactionAPI(router) {
router.get('/tx/:txid', (req, res) => { router.get('/tx/:txid', (req, res) => {
getBlock( request(`http://localhost:8332/tx/${req.params.txid}`, (err, localRes, body) => {
{ 'txs.hash': req.params.txid },
{ },
MAX_BLOCKS,
(err, blocks) => {
if (err) { if (err) {
res.status(501).send(); logger.log('error',
logger.log('err', err); `${err}`);
} }
try {
if (blocks[0] && blocks[0].txs) { body = JSON.parse(body);
let t = blocks[0].txs.filter(tx => tx.hash === req.params.txid); } catch (e) {
t = t[0]; logger.log('error',
console.log(t); `${err}`);
console.log(t.inputs); }
console.log(t.inputs); res.send({
// Map bcoin model to insight-api txid: body.hash,
res.json({ version: body.version,
txid: t.hash, time: body.ps,
version: t.version, blocktime: body.ps,
locktime: t.lockTime, locktime: body.locktime,
vin: t.inputs.map(input => ({ blockhash: body.block,
coinbase: input.script, fees: body.fee,
sequence: input.sequence, valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
n: 0, vin: body.inputs.map(input => ({
addr: input.coin ? input.coin.address : '',
value: input.coin ? input.coin.value / 1e8 : 0,
})), })),
vout: t.outputs.map(output => ({ vout: body.outputs.map(output => ({
value: output.value / 1e8,
n: 0,
scriptPubKey: { scriptPubKey: {
hex: output.script,
asm: '',
addresses: [output.address], addresses: [output.address],
type: null,
}, },
spentTxId: null, value: output.value / 1e8,
spentIndex: null,
spentHeight: null,
})), })),
blockhash: t.block, isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
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();
}
}); });
}); });
router.get('/txs', (req, res) => { router.get('/txs', (req, res) => {
if (req.query.block) { if (req.query.block) {
getBlock( request(`http://localhost:8332/block/${req.query.block}`, (err, localRes, body) => {
{ hash: req.query.block },
{ rawBlock: 0 },
MAX_BLOCKS,
(err, block) => {
if (err) { if (err) {
res.status(501).send(); logger.log('error',
logger.log('err', err); `${err}`);
} }
if (block[0]) { try {
const b = block[0]; body = JSON.parse(body);
res.json({ } catch (e) {
logger.log('error',
`${err}`);
}
res.send({
pagesTotal: 1, pagesTotal: 1,
txs: b.txs.map(tx => ({ txs: body.txs.map(tx => ({
txid: tx.hash, txid: tx.hash,
version: tx.version, fees: tx.fee,
locktime: tx.locktime, valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
vin: tx.inputs.map(input => ({ vin: tx.inputs.map(input => ({
coinbase: input.script, addr: input.coin ? input.coin.address : '',
sequence: input.sequence, value: input.coin ? input.coin.value / 1e8 : 0,
n: 0,
addr: input.address,
})), })),
vout: tx.outputs.map(output => ({ vout: tx.outputs.map(output => ({
value: output.value / 1e8,
n: 0,
scriptPubKey: { scriptPubKey: {
hex: '',
asm: '',
addresses: [output.address], addresses: [output.address],
type: output.type,
}, },
spentTxid: '', value: output.value / 1e8,
spentIndex: 0,
spentHeight: 0,
})), })),
output: tx.outputs,
})), })),
}); });
} else {
res.send();
}
}); });
} else if (req.query.address) { } else if (req.query.address) {
getBlock( request(`http://localhost:8332/tx/address/${req.query.address}`, (err, localRes, body) => {
{ $or:
[
{ 'txs.outputs.address': req.query.address },
{ 'txs.inputs.prevout.hash': req.query.address },
],
},
{ rawBlock: 0 },
MAX_BLOCKS,
(err, block) => {
if (err) { if (err) {
res.status(501).send(); logger.log('error',
logger.log('err', err); `${err}`);
} else if (block[0]) { }
const b = block[0]; try {
res.json({ body = JSON.parse(body);
} catch (e) {
logger.log('error',
`${err}`);
}
console.log(body);
res.send({
pagesTotal: 1, pagesTotal: 1,
txs: b.txs.map(tx => ({ txs: body.map(tx => ({
txid: tx.hash, txid: tx.hash,
version: tx.version, fees: tx.fee,
locktime: tx.locktime, valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
vin: tx.inputs.map(input => ({ vin: tx.inputs.map(input => ({
coinbase: input.script, addr: input.coin ? input.coin.address : '',
sequence: input.sequence, value: input.coin ? input.coin.value / 1e8 : 0,
n: 0,
addr: input.address,
})), })),
vout: tx.outputs.map(output => ({ vout: tx.outputs.map(output => ({
value: output.value / 1e8,
n: 0,
scriptPubKey: { scriptPubKey: {
hex: '',
asm: '',
addresses: [output.address], addresses: [output.address],
type: output.type,
}, },
spentTxid: '', value: output.value / 1e8,
spentIndex: 0,
spentHeight: 0,
})), })),
output: tx.outputs,
})), })),
}); });
} else {
res.send();
}
}); });
} else { } else {
getTransactions( getTransactions(

View File

@ -26,6 +26,9 @@ const BlockSchema = new Schema({
id: false, id: false,
}); });
BlockSchema.index({ hash: 1 });
BlockSchema.index({ height: 1 });
const Block = mongoose.model('Block', BlockSchema); const Block = mongoose.model('Block', BlockSchema);
module.exports = Block; module.exports = Block;

View File

@ -22,6 +22,8 @@ const TransactionSchema = new Schema({
network: String, network: String,
}); });
TransactionSchema.index({ hash: 1 });
const Transaction = mongoose.model('Transaction', TransactionSchema); const Transaction = mongoose.model('Transaction', TransactionSchema);
module.exports = Transaction; module.exports = Transaction;