From 2ac70bc9a2176b0a5f817bc4526ffb71eefc8a2b Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Wed, 16 Aug 2017 16:30:33 -0400 Subject: [PATCH 1/5] Fixed Bcoin error returned as data, fixed dead /txs endpoint missing error, added ttl to bcoin requests but large addrs still take too long --- server/config/index.js | 1 + server/lib/api/address.js | 2 + server/lib/api/transaction.js | 289 ++++++++++++++++------------------ server/lib/db/blocks.js | 3 +- server/lib/db/transactions.js | 1 + 5 files changed, 141 insertions(+), 155 deletions(-) diff --git a/server/config/index.js b/server/config/index.js index 2dd1526..4c45558 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -29,6 +29,7 @@ const config = { ticker_prop: 'bitstamp', max_blocks: 72, max_txs: 10, + request_ttl: 100000, }, }; diff --git a/server/lib/api/address.js b/server/lib/api/address.js index ed0cab9..6359400 100644 --- a/server/lib/api/address.js +++ b/server/lib/api/address.js @@ -3,12 +3,14 @@ const request = require('request'); const config = require('../../config'); const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`; +const TTL = config.api.request_ttl; module.exports = function AddressAPI(router) { router.get('/addr/:addr', (req, res) => { const addr = req.params.addr || ''; // Get Bcoin data return request(`${API_URL}/tx/address/${addr}`, + { timeout: TTL }, (error, bcoinRes, bcoinTxs) => { if (error) { logger.log('error', diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index fb79b87..6948402 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -5,6 +5,7 @@ const db = require('../db'); const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`; const MAX_TXS = config.api.max_txs; +const TTL = config.api.request_ttl; module.exports = function transactionAPI(router) { // Txs by txid @@ -18,50 +19,52 @@ module.exports = function transactionAPI(router) { } const height = blockHeight; // Bcoin transaction data - return request(`${API_URL}/tx/${req.params.txid}`, (error, localRes, tx) => { - if (error) { - logger.log('error', - `${error}`); - return res.status(404).send(); - } - // Catch JSON errors - try { - tx = JSON.parse(tx); - } catch (e) { - logger.log('error', - `${e}`); - return res.status(404).send(); - } - if (!tx || !tx.hash) { - logger.log('error', - 'No results found'); - return res.status(404).send(); - } + return request(`${API_URL}/tx/${req.params.txid}`, + { timeout: TTL }, + (error, localRes, tx) => { + if (error) { + logger.log('error', + `${error}`); + return res.status(404).send(); + } + // Catch JSON errors + try { + tx = JSON.parse(tx); + } catch (e) { + logger.log('error', + `${e}`); + return res.status(404).send(); + } + if (!tx || !tx.hash) { + logger.log('error', + 'No results found'); + return res.status(404).send(); + } - // Return UI JSON - return res.send({ - txid: tx.hash, - version: tx.version, - time: tx.ps, - blocktime: tx.ps, - locktime: tx.locktime, - blockhash: tx.block, - fees: tx.fee / 1e8, - confirmations: (height - tx.height) + 1, - 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, - })), - isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', + // Return UI JSON + return res.send({ + txid: tx.hash, + version: tx.version, + time: tx.ps, + blocktime: tx.ps, + locktime: tx.locktime, + blockhash: tx.block, + fees: tx.fee / 1e8, + confirmations: (height - tx.height) + 1, + 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, + })), + isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', + }); }); - }); }); }); @@ -83,49 +86,53 @@ module.exports = function transactionAPI(router) { } const height = blockHeight; // Get Bcoin data - return request(`${API_URL}/block/${req.query.block}`, (error, localRes, block) => { - if (error) { - logger.log('error', - `${error}`); - } - // Catch JSON errors - try { - block = JSON.parse(block); - } catch (e) { - logger.log('error', - `${e}`); - return res.status(404).send(); - } - if (!block.txs.length) { - logger.log('error', - `${'No tx results'}`); - res.status(404).send(); - } - // Setup UI JSON - const totalPages = Math.ceil(block.txs.length / MAX_TXS); - block.txs = block.txs.slice(rangeStart, rangeEnd); + return request(`${API_URL}/block/${req.query.block}`, + { timeout: TTL }, + (error, localRes, block) => { + if (error) { + logger.log('error', + `${error}`); + return res.status(404).send(); + } + // Catch JSON errors + try { + block = JSON.parse(block); + } catch (e) { + logger.log('error', + `${e}`); + return res.status(404).send(); + } - return res.send({ - pagesTotal: totalPages, - txs: block.txs.map(tx => ({ - txid: tx.hash, - fees: tx.fee / 1e8, - confirmations: (height - block.height) + 1, - 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, + if (block.error) { + logger.log('error', + `${'No tx results'}`); + return res.status(404).send(); + } + // Setup UI JSON + const totalPages = Math.ceil(block.txs.length / MAX_TXS); + block.txs = block.txs.slice(rangeStart, rangeEnd); + + return res.send({ + pagesTotal: totalPages, + txs: block.txs.map(tx => ({ + txid: tx.hash, + fees: tx.fee / 1e8, + confirmations: (height - block.height) + 1, + 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, + })), + isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', })), - vout: tx.outputs.map(output => ({ - scriptPubKey: { - addresses: [output.address], - }, - value: output.value / 1e8, - })), - isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', - })), + }); }); - }); }); } else if (req.query.address) { // Get txs by address, start with best height to calc confirmations @@ -139,83 +146,57 @@ module.exports = function transactionAPI(router) { const height = blockHeight; const addr = req.query.address || ''; - return request(`${API_URL}/tx/address/${addr}`, (error, localRes, txs) => { - if (error) { - logger.log('error', - `${error}`); - return res.status(404).send(); - } - // Catch JSON errors - try { - txs = JSON.parse(txs); - } catch (e) { - logger.log('error', - `${e}`); - return res.status(404).send(); - } - // Setup UI JSON - return res.send({ - pagesTotal: 1, - txs: txs.map(tx => ({ - txid: tx.hash, - fees: tx.fee / 1e8, - confirmations: (height - tx.height) + 1, - 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, + logger.log('debug', + 'Warning: Requesting data from Bcoin, may take some time'); + + return request(`${API_URL}/tx/address/${addr}`, + { timeout: TTL }, + (error, localRes, txs) => { + if (error) { + logger.log('error', + `${error}`); + return res.status(404).send(); + } + // Catch JSON errors + try { + txs = JSON.parse(txs); + } catch (e) { + logger.log('error', + `${e}`); + return res.status(404).send(); + } + // Bcoin returns error as part of data object + if (txs.error) { + logger.log('error', + `${'No tx results'}`); + return res.status(404).send(); + } + // Setup UI JSON + return res.send({ + pagesTotal: 1, + txs: txs.map(tx => ({ + txid: tx.hash, + fees: tx.fee / 1e8, + confirmations: (height - tx.height) + 1, + 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, + })), + isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', })), - vout: tx.outputs.map(output => ({ - scriptPubKey: { - addresses: [output.address], - }, - value: output.value / 1e8, - })), - isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', - })), + }); }); - }); }); } else { // Get last n txs - db.txs.getTransactions( - {}, - {}, - MAX_TXS, - (err, txs) => { - if (err) { - logger.log('err', - `getTransactions: ${err}`); - res.status(404).send(); - } - return res.json({ - pagesTotal: 1, - txs: 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, - })), - vout: tx.outputs.map(output => ({ - value: output.value, - n: 0, - scriptPubKey: { - hex: '', - asm: '', - addresses: [output.address], - type: output.type, - }, - spentTxid: '', - spentIndex: 0, - spentHeight: 0, - })), - })), - }); - }, - ); + return res.status(404).send({ error: 'Block hash or address expected' }); } }); diff --git a/server/lib/db/blocks.js b/server/lib/db/blocks.js index d0c9a53..deb101f 100644 --- a/server/lib/db/blocks.js +++ b/server/lib/db/blocks.js @@ -21,6 +21,7 @@ function getBlocks(params, options, limit, cb) { if (limit < 1) { limit = 1; } + // Query mongo Block.find( params, @@ -59,7 +60,7 @@ function getBestHeight(cb) { if (err) { logger.log('error', `getBlock: ${err.err}`); - return cb(err); + return cb(err, null); } return cb(null, block.height); }); diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index 455727d..905d578 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -26,6 +26,7 @@ function getTransactions(params, options, limit, cb) { params, defaultOptions, (err, txs) => { + console.log(txs) if (err) { logger.log('error', `getTransactions: ${err}`); From 1cb0a6b3853c8e6fcfca54b3837e456417bf96f0 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Wed, 16 Aug 2017 21:23:29 -0400 Subject: [PATCH 2/5] indexes and experimenting with mem db --- server/config/index.js | 2 +- server/lib/api/address.js | 2 ++ server/lib/api/transaction.js | 2 +- server/models/input.js | 2 ++ server/models/output.js | 2 ++ server/models/transaction.js | 2 ++ 6 files changed, 10 insertions(+), 2 deletions(-) diff --git a/server/config/index.js b/server/config/index.js index 4c45558..827006d 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -4,7 +4,7 @@ const config = { bcoin_http: 'localhost', bcoin: { network: 'main', - db: 'leveldb', + db: 'mem', prefix: '.', checkpoints: true, workers: false, diff --git a/server/lib/api/address.js b/server/lib/api/address.js index 6359400..722adb2 100644 --- a/server/lib/api/address.js +++ b/server/lib/api/address.js @@ -8,6 +8,8 @@ const TTL = config.api.request_ttl; module.exports = function AddressAPI(router) { router.get('/addr/:addr', (req, res) => { const addr = req.params.addr || ''; + logger.log('debug', + 'Warning: Requesting data from Bcoin by address, may take some time'); // Get Bcoin data return request(`${API_URL}/tx/address/${addr}`, { timeout: TTL }, diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index 6948402..d07a398 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -147,7 +147,7 @@ module.exports = function transactionAPI(router) { const addr = req.query.address || ''; logger.log('debug', - 'Warning: Requesting data from Bcoin, may take some time'); + 'Warning: Requesting data from Bcoin by address, may take some time'); return request(`${API_URL}/tx/address/${addr}`, { timeout: TTL }, diff --git a/server/models/input.js b/server/models/input.js index 9f2c067..a6ebc1b 100644 --- a/server/models/input.js +++ b/server/models/input.js @@ -10,6 +10,8 @@ const InputSchema = new Schema({ address: { type: String, default: '' }, }); +InputSchema.index({ address: 1 }); + const Input = mongoose.model('Input', InputSchema); module.exports = Input; diff --git a/server/models/output.js b/server/models/output.js index 65f2194..56694eb 100644 --- a/server/models/output.js +++ b/server/models/output.js @@ -9,6 +9,8 @@ const OutputSchema = new Schema({ type: { type: String, default: '' }, }); +OutputSchema.index({ address: 1 }); + const Output = mongoose.model('Output', OutputSchema); module.exports = Output; diff --git a/server/models/transaction.js b/server/models/transaction.js index 0c5974b..8d30aba 100644 --- a/server/models/transaction.js +++ b/server/models/transaction.js @@ -22,6 +22,8 @@ const TransactionSchema = new Schema({ network: { type: String, default: '' }, }); +TransactionSchema.index({ hash: 1 }); + const Transaction = mongoose.model('Transaction', TransactionSchema); module.exports = Transaction; From 3e7f263e188cc12168ea506047e5fca20e225479 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Wed, 16 Aug 2017 21:56:56 -0400 Subject: [PATCH 3/5] bestBlockHeight set by node now. Less cb Hell. --- server/lib/api/transaction.js | 298 ++++++++++++++++------------------ server/lib/db/blocks.js | 25 ++- server/lib/db/transactions.js | 4 +- server/lib/node/index.js | 2 + 4 files changed, 162 insertions(+), 167 deletions(-) diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index d07a398..c834902 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -11,60 +11,53 @@ module.exports = function transactionAPI(router) { // Txs by txid router.get('/tx/:txid', (req, res) => { // Get max block height for calculating confirmations - db.blocks.getBestHeight( - (err, blockHeight) => { - if (err) { - logger.log('err', err); + const height = db.blocks.bestHeight(); + // Bcoin transaction data + return request(`${API_URL}/tx/${req.params.txid}`, + { timeout: TTL }, + (error, localRes, tx) => { + if (error) { + logger.log('error', + `${error}`); + return res.status(404).send(); + } + // Catch JSON errors + try { + tx = JSON.parse(tx); + } catch (e) { + logger.log('error', + `${e}`); + return res.status(404).send(); + } + if (!tx || !tx.hash) { + logger.log('error', + 'No results found'); return res.status(404).send(); } - const height = blockHeight; - // Bcoin transaction data - return request(`${API_URL}/tx/${req.params.txid}`, - { timeout: TTL }, - (error, localRes, tx) => { - if (error) { - logger.log('error', - `${error}`); - return res.status(404).send(); - } - // Catch JSON errors - try { - tx = JSON.parse(tx); - } catch (e) { - logger.log('error', - `${e}`); - return res.status(404).send(); - } - if (!tx || !tx.hash) { - logger.log('error', - 'No results found'); - return res.status(404).send(); - } - // Return UI JSON - return res.send({ - txid: tx.hash, - version: tx.version, - time: tx.ps, - blocktime: tx.ps, - locktime: tx.locktime, - blockhash: tx.block, - fees: tx.fee / 1e8, - confirmations: (height - tx.height) + 1, - 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, - })), - isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', - }); - }); + // Return UI JSON + return res.send({ + txid: tx.hash, + version: tx.version, + time: tx.ps, + blocktime: tx.ps, + locktime: tx.locktime, + blockhash: tx.block, + fees: tx.fee / 1e8, + confirmations: (height - tx.height) + 1, + 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, + })), + isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', + }); }); }); @@ -78,126 +71,111 @@ module.exports = function transactionAPI(router) { const rangeEnd = rangeStart + MAX_TXS; // get txs for blockhash, start with best height to calc confirmations if (req.query.block) { - db.blocks.getBestHeight( - (err, blockHeight) => { - if (err) { - logger.log('err', err); + + const height = db.blocks.bestHeight(); + // Get Bcoin data + return request(`${API_URL}/block/${req.query.block}`, + { timeout: TTL }, + (error, localRes, block) => { + if (error) { + logger.log('error', + `${error}`); + return res.status(404).send(); + } + // Catch JSON errors + try { + block = JSON.parse(block); + } catch (e) { + logger.log('error', + `${e}`); return res.status(404).send(); } - const height = blockHeight; - // Get Bcoin data - return request(`${API_URL}/block/${req.query.block}`, - { timeout: TTL }, - (error, localRes, block) => { - if (error) { - logger.log('error', - `${error}`); - return res.status(404).send(); - } - // Catch JSON errors - try { - block = JSON.parse(block); - } catch (e) { - logger.log('error', - `${e}`); - return res.status(404).send(); - } - if (block.error) { - logger.log('error', - `${'No tx results'}`); - return res.status(404).send(); - } - // Setup UI JSON - const totalPages = Math.ceil(block.txs.length / MAX_TXS); - block.txs = block.txs.slice(rangeStart, rangeEnd); + if (block.error) { + logger.log('error', + `${'No tx results'}`); + return res.status(404).send(); + } + // Setup UI JSON + const totalPages = Math.ceil(block.txs.length / MAX_TXS); + block.txs = block.txs.slice(rangeStart, rangeEnd); - return res.send({ - pagesTotal: totalPages, - txs: block.txs.map(tx => ({ - txid: tx.hash, - fees: tx.fee / 1e8, - confirmations: (height - block.height) + 1, - 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, - })), - isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', - })), - }); - }); + return res.send({ + pagesTotal: totalPages, + txs: block.txs.map(tx => ({ + txid: tx.hash, + fees: tx.fee / 1e8, + confirmations: (height - block.height) + 1, + 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, + })), + isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', + })), + }); }); } else if (req.query.address) { // Get txs by address, start with best height to calc confirmations - db.blocks.getBestHeight( - (err, blockHeight) => { - if (err) { - logger.log('err', err); + const height = db.blocks.bestHeight(); + const addr = req.query.address || ''; + + logger.log('debug', + 'Warning: Requesting data from Bcoin by address, may take some time'); + + return request(`${API_URL}/tx/address/${addr}`, + { timeout: TTL }, + (error, localRes, txs) => { + if (error) { + logger.log('error', + `${error}`); return res.status(404).send(); } - - const height = blockHeight; - const addr = req.query.address || ''; - - logger.log('debug', - 'Warning: Requesting data from Bcoin by address, may take some time'); - - return request(`${API_URL}/tx/address/${addr}`, - { timeout: TTL }, - (error, localRes, txs) => { - if (error) { - logger.log('error', - `${error}`); - return res.status(404).send(); - } - // Catch JSON errors - try { - txs = JSON.parse(txs); - } catch (e) { - logger.log('error', - `${e}`); - return res.status(404).send(); - } - // Bcoin returns error as part of data object - if (txs.error) { - logger.log('error', - `${'No tx results'}`); - return res.status(404).send(); - } - // Setup UI JSON - return res.send({ - pagesTotal: 1, - txs: txs.map(tx => ({ - txid: tx.hash, - fees: tx.fee / 1e8, - confirmations: (height - tx.height) + 1, - 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, - })), - isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', - })), - }); - }); + // Catch JSON errors + try { + txs = JSON.parse(txs); + } catch (e) { + logger.log('error', + `${e}`); + return res.status(404).send(); + } + // Bcoin returns error as part of data object + if (txs.error) { + logger.log('error', + `${'No tx results'}`); + return res.status(404).send(); + } + // Setup UI JSON + return res.send({ + pagesTotal: 1, + txs: txs.map(tx => ({ + txid: tx.hash, + fees: tx.fee / 1e8, + confirmations: (height - tx.height) + 1, + 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, + })), + isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', + })), + }); }); - } else { - // Get last n txs - return res.status(404).send({ error: 'Block hash or address expected' }); } + // Get last n txs + return res.status(404).send({ error: 'Block hash or address expected' }); }); router.get('/rawtx/:txid', (req, res) => { diff --git a/server/lib/db/blocks.js b/server/lib/db/blocks.js index deb101f..5659d34 100644 --- a/server/lib/db/blocks.js +++ b/server/lib/db/blocks.js @@ -4,6 +4,8 @@ const config = require('../../config'); const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours +let bestBlockHeight = 0; + function getBlocks(params, options, limit, cb) { // Do not return mongo ids const defaultOptions = { _id: 0 }; @@ -54,20 +56,31 @@ function getBlock(params, options, limit, cb) { return cb(null, blocks[0]); }); } -// Highest known height -function getBestHeight(cb) { +// Highest known height in mongo +function getBestHeight() { getBlock({}, {}, 1, (err, block) => { if (err) { logger.log('error', - `getBlock: ${err.err}`); - return cb(err, null); + `getBestHeight: ${err.err}`); + return; } - return cb(null, block.height); + bestBlockHeight = block.height; }); } +// 1e9 limit = ~2M years from now +// Mostly for sync to set height +function bestHeight(height) { + if (Number.isInteger(height) && + height > 0 && + height < 1 * 1e9) { + bestBlockHeight = height; + return bestBlockHeight; + } + return bestBlockHeight; +} module.exports = { getBlock, getBlocks, - getBestHeight, + bestHeight, }; diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index 905d578..bfd90c3 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -2,6 +2,9 @@ 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) { @@ -26,7 +29,6 @@ function getTransactions(params, options, limit, cb) { params, defaultOptions, (err, txs) => { - console.log(txs) if (err) { logger.log('error', `getTransactions: ${err}`); diff --git a/server/lib/node/index.js b/server/lib/node/index.js index 266f5b1..bea48c0 100644 --- a/server/lib/node/index.js +++ b/server/lib/node/index.js @@ -3,6 +3,7 @@ const logger = require('../../lib/logger'); const BlockParser = require('../parser').Block; const config = require('../../config'); const socket = require('../../lib/api/socket'); +const db = require('../../lib/db'); const node = new FullNode(config.bcoin); @@ -18,6 +19,7 @@ function start() { node.chain.on('connect', (entry, block) => { BlockParser.parse(entry, block); socket.processBlock(entry, block); + db.blocks.bestHeight(entry.height); }); node.on('error', (err) => { From 4b6482e0ade48173d526b8836954cbf46acf965b Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Thu, 17 Aug 2017 14:37:08 -0400 Subject: [PATCH 4/5] add scriptsigs to txs for circle-plus info on tx lists --- server/config/index.js | 2 +- server/lib/api/transaction.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/server/config/index.js b/server/config/index.js index 827006d..4c45558 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -4,7 +4,7 @@ const config = { bcoin_http: 'localhost', bcoin: { network: 'main', - db: 'mem', + db: 'leveldb', prefix: '.', checkpoints: true, workers: false, diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index c834902..752723b 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -35,6 +35,8 @@ module.exports = function transactionAPI(router) { return res.status(404).send(); } + console.log(tx); + // Return UI JSON return res.send({ txid: tx.hash, @@ -49,9 +51,13 @@ module.exports = function transactionAPI(router) { vin: tx.inputs.map(input => ({ addr: input.coin ? input.coin.address : '', value: input.coin ? input.coin.value / 1e8 : 0, + scriptSig: { + asm: input.script, + }, })), vout: tx.outputs.map(output => ({ scriptPubKey: { + asm: output.script, addresses: [output.address], }, value: output.value / 1e8, @@ -71,7 +77,6 @@ module.exports = function transactionAPI(router) { const rangeEnd = rangeStart + MAX_TXS; // get txs for blockhash, start with best height to calc confirmations if (req.query.block) { - const height = db.blocks.bestHeight(); // Get Bcoin data return request(`${API_URL}/block/${req.query.block}`, @@ -110,9 +115,13 @@ module.exports = function transactionAPI(router) { vin: tx.inputs.map(input => ({ addr: input.coin ? input.coin.address : '', value: input.coin ? input.coin.value / 1e8 : 0, + scriptSig: { + asm: input.script, + }, })), vout: tx.outputs.map(output => ({ scriptPubKey: { + asm: output.script, addresses: [output.address], }, value: output.value / 1e8, @@ -162,9 +171,13 @@ module.exports = function transactionAPI(router) { vin: tx.inputs.map(input => ({ addr: input.coin ? input.coin.address : '', value: input.coin ? input.coin.value / 1e8 : 0, + scriptSig: { + asm: input.script, + }, })), vout: tx.outputs.map(output => ({ scriptPubKey: { + asm: output.script, addresses: [output.address], }, value: output.value / 1e8, From d148b5ec7710dab823c9da677f4c00bf0dd84cf0 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Thu, 17 Aug 2017 15:11:27 -0400 Subject: [PATCH 5/5] updated readme instructions --- README.md | 4 ++-- server/lib/api/transaction.js | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f4feeca..b733135 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,14 @@ ## Requirements -Insight requires [Node.js](https://nodejs.org) and [MongoDB](https://www.mongodb.com/). Consider using [n](https://github.com/tj/n) and [m](https://github.com/aheckmann/m) to install the latest versions. +Insight requires [Node.js](https://nodejs.org) 8.2 and [MongoDB](https://www.mongodb.com/). Consider using [n](https://github.com/tj/n) and [m](https://github.com/aheckmann/m) to install the latest versions. ## Quick Start To get started, clone this repository, then – with `mongod` running – install and run insight: ```bash -git clone -b next https://github.com/bitpay/insight.git && cd insight +git clone -b next https://github.com/bitpay/insight.git && cd insight/server npm install npm start ``` diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index 752723b..7afbc52 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -35,8 +35,6 @@ module.exports = function transactionAPI(router) { return res.status(404).send(); } - console.log(tx); - // Return UI JSON return res.send({ txid: tx.hash,