From cb9d58b0dc90902f4e1f9c2931136843fe08e153 Mon Sep 17 00:00:00 2001 From: Dan Janosik Date: Fri, 19 Feb 2021 13:10:11 -0500 Subject: [PATCH] more graceful degradation for /address pages I believe we can do "better" than this in some cases but I'm going with this for graceful-degradataion-v1 --- routes/baseRouter.js | 166 ++++++++++++++++++++++--------------------- views/address.pug | 6 ++ 2 files changed, 91 insertions(+), 81 deletions(-) diff --git a/routes/baseRouter.js b/routes/baseRouter.js index c8ac31fdb..ee316a546 100644 --- a/routes/baseRouter.js +++ b/routes/baseRouter.js @@ -991,118 +991,122 @@ router.get("/address/:address", function(req, res, next) { } res.locals.txids = txids; - - coreApi.getRawTransactionsWithInputs(txids).then(function(rawTxResult) { - res.locals.transactions = rawTxResult.transactions; - res.locals.txInputsByTransaction = rawTxResult.txInputsByTransaction; - - // for coinbase txs, we need the block height in order to calculate subsidy to display - var coinbaseTxs = []; - for (var i = 0; i < rawTxResult.transactions.length; i++) { - var tx = rawTxResult.transactions[i]; - - for (var j = 0; j < tx.vin.length; j++) { - if (tx.vin[j].coinbase) { - // addressApi sometimes has blockHeightByTxid already available, otherwise we need to query for it - if (!blockHeightsByTxid[tx.txid]) { - coinbaseTxs.push(tx); + + if (global.txindexAvailable) { + coreApi.getRawTransactionsWithInputs(txids).then(function(rawTxResult) { + res.locals.transactions = rawTxResult.transactions; + res.locals.txInputsByTransaction = rawTxResult.txInputsByTransaction; + + // for coinbase txs, we need the block height in order to calculate subsidy to display + var coinbaseTxs = []; + for (var i = 0; i < rawTxResult.transactions.length; i++) { + var tx = rawTxResult.transactions[i]; + + for (var j = 0; j < tx.vin.length; j++) { + if (tx.vin[j].coinbase) { + // addressApi sometimes has blockHeightByTxid already available, otherwise we need to query for it + if (!blockHeightsByTxid[tx.txid]) { + coinbaseTxs.push(tx); + } } } } - } - var coinbaseTxBlockHashes = []; - var blockHashesByTxid = {}; - coinbaseTxs.forEach(function(tx) { - coinbaseTxBlockHashes.push(tx.blockhash); - blockHashesByTxid[tx.txid] = tx.blockhash; - }); - - var blockHeightsPromises = []; - if (coinbaseTxs.length > 0) { - // we need to query some blockHeights by hash for some coinbase txs - blockHeightsPromises.push(new Promise(function(resolve2, reject2) { - coreApi.getBlocksByHash(coinbaseTxBlockHashes).then(function(blocksByHashResult) { - for (var txid in blockHashesByTxid) { - if (blockHashesByTxid.hasOwnProperty(txid)) { - blockHeightsByTxid[txid] = blocksByHashResult[blockHashesByTxid[txid]].height; + var coinbaseTxBlockHashes = []; + var blockHashesByTxid = {}; + coinbaseTxs.forEach(function(tx) { + coinbaseTxBlockHashes.push(tx.blockhash); + blockHashesByTxid[tx.txid] = tx.blockhash; + }); + + var blockHeightsPromises = []; + if (coinbaseTxs.length > 0) { + // we need to query some blockHeights by hash for some coinbase txs + blockHeightsPromises.push(new Promise(function(resolve2, reject2) { + coreApi.getBlocksByHash(coinbaseTxBlockHashes).then(function(blocksByHashResult) { + for (var txid in blockHashesByTxid) { + if (blockHashesByTxid.hasOwnProperty(txid)) { + blockHeightsByTxid[txid] = blocksByHashResult[blockHashesByTxid[txid]].height; + } } - } - resolve2(); + resolve2(); - }).catch(function(err) { - res.locals.pageErrors.push(utils.logError("78ewrgwetg3", err)); + }).catch(function(err) { + res.locals.pageErrors.push(utils.logError("78ewrgwetg3", err)); - reject2(err); - }); - })); - } + reject2(err); + }); + })); + } - Promise.all(blockHeightsPromises).then(function() { - var addrGainsByTx = {}; - var addrLossesByTx = {}; + Promise.all(blockHeightsPromises).then(function() { + var addrGainsByTx = {}; + var addrLossesByTx = {}; - res.locals.addrGainsByTx = addrGainsByTx; - res.locals.addrLossesByTx = addrLossesByTx; + res.locals.addrGainsByTx = addrGainsByTx; + res.locals.addrLossesByTx = addrLossesByTx; - var handledTxids = []; + var handledTxids = []; - for (var i = 0; i < rawTxResult.transactions.length; i++) { - var tx = rawTxResult.transactions[i]; - var txInputs = rawTxResult.txInputsByTransaction[tx.txid]; - - if (handledTxids.includes(tx.txid)) { - continue; - } + for (var i = 0; i < rawTxResult.transactions.length; i++) { + var tx = rawTxResult.transactions[i]; + var txInputs = rawTxResult.txInputsByTransaction[tx.txid]; + + if (handledTxids.includes(tx.txid)) { + continue; + } - handledTxids.push(tx.txid); + handledTxids.push(tx.txid); - for (var j = 0; j < tx.vout.length; j++) { - if (tx.vout[j].value > 0 && tx.vout[j].scriptPubKey && tx.vout[j].scriptPubKey.addresses && tx.vout[j].scriptPubKey.addresses.includes(address)) { - if (addrGainsByTx[tx.txid] == null) { - addrGainsByTx[tx.txid] = new Decimal(0); - } + for (var j = 0; j < tx.vout.length; j++) { + if (tx.vout[j].value > 0 && tx.vout[j].scriptPubKey && tx.vout[j].scriptPubKey.addresses && tx.vout[j].scriptPubKey.addresses.includes(address)) { + if (addrGainsByTx[tx.txid] == null) { + addrGainsByTx[tx.txid] = new Decimal(0); + } - addrGainsByTx[tx.txid] = addrGainsByTx[tx.txid].plus(new Decimal(tx.vout[j].value)); + addrGainsByTx[tx.txid] = addrGainsByTx[tx.txid].plus(new Decimal(tx.vout[j].value)); + } } - } - for (var j = 0; j < tx.vin.length; j++) { - var txInput = txInputs[j]; - var vinJ = tx.vin[j]; + for (var j = 0; j < tx.vin.length; j++) { + var txInput = txInputs[j]; + var vinJ = tx.vin[j]; - if (txInput != null) { - if (txInput && txInput.scriptPubKey && txInput.scriptPubKey.addresses && txInput.scriptPubKey.addresses.includes(address)) { - if (addrLossesByTx[tx.txid] == null) { - addrLossesByTx[tx.txid] = new Decimal(0); - } + if (txInput != null) { + if (txInput && txInput.scriptPubKey && txInput.scriptPubKey.addresses && txInput.scriptPubKey.addresses.includes(address)) { + if (addrLossesByTx[tx.txid] == null) { + addrLossesByTx[tx.txid] = new Decimal(0); + } - addrLossesByTx[tx.txid] = addrLossesByTx[tx.txid].plus(new Decimal(txInput.value)); + addrLossesByTx[tx.txid] = addrLossesByTx[tx.txid].plus(new Decimal(txInput.value)); + } } } + + //debugLog("tx: " + JSON.stringify(tx)); + //debugLog("txInputs: " + JSON.stringify(txInputs)); } - //debugLog("tx: " + JSON.stringify(tx)); - //debugLog("txInputs: " + JSON.stringify(txInputs)); - } + res.locals.blockHeightsByTxid = blockHeightsByTxid; + + resolve(); - res.locals.blockHeightsByTxid = blockHeightsByTxid; + }).catch(function(err) { + res.locals.pageErrors.push(utils.logError("230wefrhg0egt3", err)); - resolve(); + reject(err); + }); }).catch(function(err) { - res.locals.pageErrors.push(utils.logError("230wefrhg0egt3", err)); + res.locals.pageErrors.push(utils.logError("asdgf07uh23", err)); reject(err); }); - - }).catch(function(err) { - res.locals.pageErrors.push(utils.logError("asdgf07uh23", err)); - - reject(err); - }); + } else { + resolve(); + } } else { // no addressDetails.txids available diff --git a/views/address.pug b/views/address.pug index 92fc32518..bf9e9c6f7 100644 --- a/views/address.pug +++ b/views/address.pug @@ -316,6 +316,12 @@ block content pre code.json #{JSON.stringify(err, null, 4)} + else if (!global.txindexAvailable && transactions.length == 0) + ol.text-monospace(start=(offset + 1)) + each txid in txids + li + a(href=`./tx/${txid}`) #{txid} + else if (transactions.length == 0) span No transactions found