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 = {
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',

View File

@ -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,

View File

@ -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(
{},

View File

@ -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;

View File

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