Browse Source

several fixes for searching

fix-133-memory-crash
Dan Janosik 7 years ago
parent
commit
215f24a1eb
  1. 9
      app/utils.js
  2. 50
      routes/baseActionsRouter.js

9
app/utils.js

@ -85,11 +85,18 @@ function getRandomString(length, chars) {
return result;
}
function isHex(h) {
var a = parseInt(h, 16);
return (a.toString(16) === h.toLowerCase());
}
module.exports = {
doSmartRedirect: doSmartRedirect,
redirectToConnectPageIfNeeded: redirectToConnectPageIfNeeded,
hex2ascii: hex2ascii,
getBlockReward: getBlockReward,
splitArrayIntoChunks: splitArrayIntoChunks,
getRandomString: getRandomString
getRandomString: getRandomString,
isHex: isHex
};

50
routes/baseActionsRouter.js

@ -187,13 +187,11 @@ router.post("/search", function(req, res) {
var query = req.body.query.toLowerCase();
rpcApi.getRawTransaction(query).then(function(tx) {
if (tx) {
res.redirect("/tx/" + query);
req.session.query = req.body.query;
return;
}
console.log("query.hex = " + utils.isHex(query));
if (query.length == 64) {
rpcApi.getBlockByHash(query).then(function(blockByHash) {
if (blockByHash) {
res.redirect("/block/" + query);
@ -201,14 +199,39 @@ router.post("/search", function(req, res) {
return;
}
if (isNaN(query)) {
req.session.userMessage = "No results found for query: " + query;
if (utils.isHex(query)) {
rpcApi.getRawTransaction(query).then(function(tx) {
if (tx) {
res.redirect("/tx/" + query);
return;
}
req.session.userMessage = "No results found for hash: " + query;
res.redirect("/");
return;
}).catch(function(err) {
res.locals.userMessage = "Error: " + err;
res.render("index");
});
} else {
req.session.userMessage = "No results found for query: " + query;
res.redirect("/");
}
}).catch(function(err) {
res.locals.userMessage = "Error: " + err;
res.render("index");
});
} else if (!isNaN(query)) {
rpcApi.getBlockByHeight(parseInt(query)).then(function(blockByHeight) {
if (blockByHeight) {
res.redirect("/block-height/" + query);
@ -220,12 +243,15 @@ router.post("/search", function(req, res) {
res.redirect("/");
});
});
}).catch(function(err) {
res.locals.userMessage = "Unable to connect to Bitcoin Node at " + env.bitcoind.host + ":" + env.bitcoind.port;
} else {
req.session.userMessage = "Invalid query: " + query;
res.redirect("/");
return;
}
res.render("index");
});
});
router.get("/block-height/:blockHeight", function(req, res) {

Loading…
Cancel
Save