From 01d85628bdb90d64c285fa457585d87d83fd38e7 Mon Sep 17 00:00:00 2001 From: joeuhren <46763106+joeuhren@users.noreply.github.com> Date: Fri, 20 Nov 2020 18:56:25 -0700 Subject: [PATCH] Add client command for getrawtransaction, more indexing speed-up --- lib/database.js | 96 +++++++++++++++++++------------------------------ lib/explorer.js | 20 ++++++++--- 2 files changed, 52 insertions(+), 64 deletions(-) diff --git a/lib/database.js b/lib/database.js index eb49ad8..c58a890 100644 --- a/lib/database.js +++ b/lib/database.js @@ -160,56 +160,50 @@ function find_tx(txid, cb) { }); } -function save_tx(txid, cb) { +function save_tx(txid, blockheight, cb) { //var s_timer = new Date().getTime(); lib.get_rawtransaction(txid, function(tx){ if (tx != 'There was an error. Check your console.') { - lib.get_block(tx.blockhash, function(block){ - if (block) { - lib.prepare_vin(tx, function(vin) { - lib.prepare_vout(tx.vout, txid, vin, ((typeof tx.vjoinsplit === 'undefined' || tx.vjoinsplit == null) ? [] : tx.vjoinsplit), function(vout, nvin) { - lib.syncLoop(vin.length, function (loop) { - var i = loop.iteration(); - update_address(nvin[i].addresses, txid, nvin[i].amount, 'vin', function(){ - loop.next(); + lib.prepare_vin(tx, function(vin) { + lib.prepare_vout(tx.vout, txid, vin, ((typeof tx.vjoinsplit === 'undefined' || tx.vjoinsplit == null) ? [] : tx.vjoinsplit), function(vout, nvin) { + lib.syncLoop(vin.length, function (loop) { + var i = loop.iteration(); + update_address(nvin[i].addresses, txid, nvin[i].amount, 'vin', function(){ + loop.next(); + }); + }, function(){ + lib.syncLoop(vout.length, function (subloop) { + var t = subloop.iteration(); + if (vout[t].addresses) { + update_address(vout[t].addresses, txid, vout[t].amount, 'vout', function(){ + subloop.next(); }); - }, function(){ - lib.syncLoop(vout.length, function (subloop) { - var t = subloop.iteration(); - if (vout[t].addresses) { - update_address(vout[t].addresses, txid, vout[t].amount, 'vout', function(){ - subloop.next(); - }); + } else { + subloop.next(); + } + }, function(){ + lib.calculate_total(vout, function(total){ + var newTx = new Tx({ + txid: tx.txid, + vin: nvin, + vout: vout, + total: total.toFixed(8), + timestamp: tx.time, + blockhash: tx.blockhash, + blockindex: blockheight, + }); + newTx.save(function(err) { + if (err) { + return cb(err); } else { - subloop.next(); + //console.log('txid: '); + return cb(); } - }, function(){ - lib.calculate_total(vout, function(total){ - var newTx = new Tx({ - txid: tx.txid, - vin: nvin, - vout: vout, - total: total.toFixed(8), - timestamp: tx.time, - blockhash: tx.blockhash, - blockindex: block.height, - }); - newTx.save(function(err) { - if (err) { - return cb(err); - } else { - //console.log('txid: '); - return cb(); - } - }); - }); }); }); }); }); - } else { - return cb('block not found: ' + tx.blockhash); - } + }); }); } else { return cb('tx not found: ' + txid); @@ -445,24 +439,6 @@ module.exports = { }); }, - create_tx: function(txid, cb) { - is_locked("db_index", function (exists) { - if (exists) { - console.log("db_index lock file exists..."); - return cb(); - } else { - save_tx(txid, function(err){ - if (err) { - return cb(err); - } else { - //console.log('tx stored: %s', txid); - return cb(); - } - }); - } - }); - }, - create_txs: function(block, cb) { is_locked("db_index", function (exists) { if (exists) { @@ -471,7 +447,7 @@ module.exports = { } else { lib.syncLoop(block.tx.length, function (loop) { var i = loop.iteration(); - save_tx(block.tx[i], function(err){ + save_tx(block.tx[i], block.height, function(err){ if (err) { loop.next(); } else { @@ -853,7 +829,7 @@ module.exports = { tx = null; subloop.next(); } else { - save_tx(block.tx[i], function(err){ + save_tx(block.tx[i], block.height, function(err){ if (err) { console.log(err); } else { diff --git a/lib/explorer.js b/lib/explorer.js index 31862d9..74385e5 100644 --- a/lib/explorer.js +++ b/lib/explorer.js @@ -147,10 +147,22 @@ module.exports = { }, get_rawtransaction: function(hash, cb) { - var uri = base_url + 'getrawtransaction?txid=' + hash + '&decrypt=1'; - request({uri: uri, json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, body) { - return cb(body); - }); + if (settings.use_rpc) { + var uri = base_url + 'getrawtransaction?txid=' + hash + '&decrypt=1'; + request({uri: uri, json: true}, function (error, response, body) { + return cb(body); + }); + } else { + client.command([{method:'getrawtransaction', parameters: [hash, 1]}], function(err, response){ + if(err){console.log('Error: ', err); onlyConsole.trace(err)} + else{ + if(response[0].name == 'RpcError'){ + return cb('There was an error. Check your console.'); + } + return cb(response[0]); + } + }); + } }, get_maxmoney: function(cb) {