Browse Source

search support for block heights, block hashes, and txids

fix-133-memory-crash
Dan Janosik 8 years ago
parent
commit
07f3f8f706
  1. 17
      app/rpcApi.js
  2. 48
      routes/baseActionsRouter.js
  3. 5
      views/layout.pug

17
app/rpcApi.js

@ -108,6 +108,22 @@ function getBlocksByHeight(blockHeights) {
}); });
} }
function getBlockByHash(blockHash) {
console.log("getBlockByHash: " + blockHash);
return new Promise(function(resolve, reject) {
var client = global.client;
client.cmd('getblock', blockHash, function(err, result, resHeaders) {
if (err) {
console.log("Error 0u2fgewue: " + err);
}
resolve(result);
});
});
}
function getTransactionInputs(rpcClient, transaction) { function getTransactionInputs(rpcClient, transaction) {
console.log("getTransactionInputs: " + transaction.txid); console.log("getTransactionInputs: " + transaction.txid);
@ -246,6 +262,7 @@ module.exports = {
getInfo: getInfo, getInfo: getInfo,
getBlockByHeight: getBlockByHeight, getBlockByHeight: getBlockByHeight,
getBlocksByHeight: getBlocksByHeight, getBlocksByHeight: getBlocksByHeight,
getBlockByHash: getBlockByHash,
getTransactionInputs: getTransactionInputs, getTransactionInputs: getTransactionInputs,
getBlockData: getBlockData, getBlockData: getBlockData,
getRawTransaction: getRawTransaction, getRawTransaction: getRawTransaction,

48
routes/baseActionsRouter.js

@ -125,6 +125,54 @@ router.get("/blocks", function(req, res) {
}); });
}); });
router.post("/search", function(req, res) {
if (!req.body.query) {
req.session.userMessage = "Enter a block height, block hash, or transaction id.";
res.redirect("/");
return;
}
var query = req.body.query;
rpcApi.getRawTransaction(query).then(function(tx) {
if (tx) {
res.redirect("/tx/" + query);
return;
}
rpcApi.getBlockByHash(query).then(function(blockByHash) {
if (blockByHash) {
res.redirect("/block/" + query);
return;
}
if (isNaN(query)) {
req.session.userMessage = "No results found for query: " + query;
res.redirect("/");
return;
}
rpcApi.getBlockByHeight(parseInt(query)).then(function(blockByHeight) {
if (blockByHeight) {
res.redirect("/block-height/" + query);
return;
}
req.session.userMessage = "No results found for query: " + query;
res.redirect("/");
});
});
});
});
router.get("/block-height/:blockHeight", function(req, res) { router.get("/block-height/:blockHeight", function(req, res) {
var client = global.client; var client = global.client;

5
views/layout.pug

@ -30,9 +30,12 @@ html
span BTC RPC Explorer span BTC RPC Explorer
div(class="collapse navbar-collapse", id="navbarNav") div(class="collapse navbar-collapse", id="navbarNav")
if (client) if (client)
ul(class="navbar-nav") ul(class="navbar-nav mr-auto")
li(class="nav-item") li(class="nav-item")
a(href="/terminal", class="nav-link") RPC Terminal a(href="/terminal", class="nav-link") RPC Terminal
form(method="post", action="/search", class="form-inline")
input(class="form-control form-control-sm mr-sm-2", type="text", name="query", placeholder="block height, block hash, txid", style="width: 300px;")
button(class="btn btn-primary btn-sm", type="submit") Search
div(class="container") div(class="container")
if (userMessage) if (userMessage)

Loading…
Cancel
Save