Skip to content

Commit

Permalink
Allow explicitly specifying the transaction height
Browse files Browse the repository at this point in the history
i.e. `/tx/<txid>?height=<height>`. This can work without txindex.

When txindex is disabled, the block page links to the transactions with
this parameter set.
  • Loading branch information
shesek committed Feb 4, 2021
1 parent 4aafc4e commit c062919
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/api/coreApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,12 @@ function getBlockByHeight(blockHeight) {
});
}

function getBlockHashByHeight(blockHeight) {
return tryCacheThenRpcApi(blockCache, "getBlockHashByHeight-" + blockHeight, ONE_HR, function() {
return rpcApi.getBlockHashByHeight(blockHeight);
});
}

function getBlocksByHeight(blockHeights) {
return new Promise(function(resolve, reject) {
var promises = [];
Expand Down Expand Up @@ -1069,6 +1075,7 @@ module.exports = {
getMiningInfo: getMiningInfo,
getIndexInfo: getIndexInfo,
getBlockByHeight: getBlockByHeight,
getBlockHashByHeight: getBlockHashByHeight,
getBlocksByHeight: getBlocksByHeight,
getBlockByHash: getBlockByHash,
getBlocksByHash: getBlocksByHash,
Expand Down
5 changes: 5 additions & 0 deletions app/api/rpcApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ function getBlockHeaderByHeight(blockHeight) {
});
}

function getBlockHashByHeight(blockHeight) {
return getRpcDataWithParams({method:"getblockhash", parameters:[blockHeight]});
}

function getBlockByHash(blockHash) {
debugLog("getBlockByHash: %s", blockHash);

Expand Down Expand Up @@ -516,6 +520,7 @@ module.exports = {
getBlockStatsByHeight: getBlockStatsByHeight,
getBlockHeaderByHash: getBlockHeaderByHash,
getBlockHeaderByHeight: getBlockHeaderByHeight,
getBlockHashByHeight: getBlockHashByHeight,
getTxOut: getTxOut,

minRpcVersions: minRpcVersions
Expand Down
9 changes: 7 additions & 2 deletions routes/baseRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,11 +828,16 @@ router.get("/tx/:transactionId", function(req, res, next) {

res.locals.result = {};

coreApi.getRawTransactionsWithInputs([txid]).then(function(rawTxResult) {
var txPromise = req.query.height
? coreApi.getBlockHashByHeight(parseInt(req.query.height))
.then(blockhash => coreApi.getRawTransactionsWithInputs([txid], -1, blockhash))
: coreApi.getRawTransactionsWithInputs([txid], -1);

txPromise.then(function(rawTxResult) {
var tx = rawTxResult.transactions[0];

res.locals.result.getrawtransaction = tx;
res.locals.result.txInputs = rawTxResult.txInputsByTransaction[txid];
res.locals.result.txInputs = rawTxResult.txInputsByTransaction[txid] || {};

var promises = [];

Expand Down
3 changes: 2 additions & 1 deletion views/includes/block-content.pug
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ div.tab-content
- var fontawesomeOutputName = "sign-out-alt";

div
- query_height = global.getindexinfo && !global.getindexinfo.txindex ? `?height=${result.getblock.height}` : ''
each tx, txIndex in result.transactions
//pre
// code.json.bg-light #{JSON.stringify(tx, null, 4)}
Expand All @@ -394,7 +395,7 @@ div.tab-content
if (tx && tx.txid)
span(title=`Index in Block: #${(txIndex + offset).toLocaleString()}`, data-toggle="tooltip") ##{(txIndex + offset).toLocaleString()}
span &ndash;
a(href=("./tx/" + tx.txid)) #{tx.txid}
a(href=("./tx/" + tx.txid + query_height)) #{tx.txid}

if (global.specialTransactions && global.specialTransactions[tx.txid])
span
Expand Down

0 comments on commit c062919

Please sign in to comment.