Browse Source

Fix for blockchair addressApi which doesn't give block heights for txids

fix-133-memory-crash
Dan Janosik 6 years ago
parent
commit
cab4cf6a8c
No known key found for this signature in database GPG Key ID: C6F8CE9FFDB2CED2
  1. 9
      app/api/coreApi.js
  2. 58
      routes/baseActionsRouter.js

9
app/api/coreApi.js

@ -588,7 +588,13 @@ function getBlocksByHash(blockHashes) {
} }
Promise.all(promises).then(function(results) { Promise.all(promises).then(function(results) {
resolve(results); var result = {};
results.forEach(function(item) {
result[item.hash] = item;
});
resolve(result);
}).catch(function(err) { }).catch(function(err) {
reject(err); reject(err);
@ -876,6 +882,7 @@ module.exports = {
getBlockByHeight: getBlockByHeight, getBlockByHeight: getBlockByHeight,
getBlocksByHeight: getBlocksByHeight, getBlocksByHeight: getBlocksByHeight,
getBlockByHash: getBlockByHash, getBlockByHash: getBlockByHash,
getBlocksByHash: getBlocksByHash,
getBlockByHashWithTransactions: getBlockByHashWithTransactions, getBlockByHashWithTransactions: getBlockByHashWithTransactions,
getRawTransaction: getRawTransaction, getRawTransaction: getRawTransaction,
getRawTransactions: getRawTransactions, getRawTransactions: getRawTransactions,

58
routes/baseActionsRouter.js

@ -664,7 +664,12 @@ router.get("/address/:address", function(req, res, next) {
if (addressDetails.txids) { if (addressDetails.txids) {
var txids = addressDetails.txids; var txids = addressDetails.txids;
// if the active addressApi gives us blockHeightsByTxid, it saves us work, so try to use it
var blockHeightsByTxid = {}; var blockHeightsByTxid = {};
if (addressDetails.blockHeightsByTxid) {
blockHeightsByTxid = addressDetails.blockHeightsByTxid;
}
res.locals.txids = txids; res.locals.txids = txids;
@ -672,6 +677,51 @@ router.get("/address/:address", function(req, res, next) {
res.locals.transactions = rawTxResult.transactions; res.locals.transactions = rawTxResult.transactions;
res.locals.txInputsByTransaction = rawTxResult.txInputsByTransaction; 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;
}
}
resolve2();
}).catch(function(err) {
utils.logError("78ewrgwetg3", err);
reject2(err);
});
}));
}
Promise.all(blockHeightsPromises).then(function() {
var addrGainsByTx = {}; var addrGainsByTx = {};
var addrLossesByTx = {}; var addrLossesByTx = {};
@ -682,8 +732,6 @@ router.get("/address/:address", function(req, res, next) {
var tx = rawTxResult.transactions[i]; var tx = rawTxResult.transactions[i];
var txInputs = rawTxResult.txInputsByTransaction[tx.txid]; var txInputs = rawTxResult.txInputsByTransaction[tx.txid];
blockHeightsByTxid[tx.txid] = tx.height;
for (var j = 0; j < tx.vout.length; j++) { 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 (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) { if (addrGainsByTx[tx.txid] == null) {
@ -717,6 +765,12 @@ router.get("/address/:address", function(req, res, next) {
resolve(); resolve();
}).catch(function(err) {
utils.logError("230wefrhg0egt3", err);
reject(err);
});
}).catch(function(err) { }).catch(function(err) {
utils.logError("asdgf07uh23", err); utils.logError("asdgf07uh23", err);

Loading…
Cancel
Save