From 05cc52aff1507d6c01235db2c4b4d8013c78c561 Mon Sep 17 00:00:00 2001 From: Mike the Tike Date: Fri, 17 Dec 2021 17:17:16 +0200 Subject: [PATCH] feat: add search by commitment to explorer --- applications/tari_explorer/app.js | 12 +++-- applications/tari_explorer/routes/blocks.js | 1 - applications/tari_explorer/routes/search.js | 56 +++++++++++++++++++++ applications/tari_explorer/views/index.hbs | 6 +++ applications/tari_explorer/views/search.hbs | 14 ++++++ clients/base_node_grpc_client/src/index.js | 1 + 6 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 applications/tari_explorer/routes/search.js create mode 100644 applications/tari_explorer/views/search.hbs diff --git a/applications/tari_explorer/app.js b/applications/tari_explorer/app.js index 05ef4262ad..6c060cfb42 100644 --- a/applications/tari_explorer/app.js +++ b/applications/tari_explorer/app.js @@ -8,6 +8,7 @@ const asciichart = require("asciichart") var indexRouter = require("./routes/index") var blocksRouter = require("./routes/blocks") var mempoolRouter = require("./routes/mempool") +var searchRouter = require("./routes/search") var hbs = require("hbs") hbs.registerHelper("hex", function (buffer) { @@ -50,9 +51,13 @@ hbs.registerHelper("percentbar", function (a, b) { }) hbs.registerHelper("chart", function (data, height) { - return asciichart.plot(data, { - height: height, - }) + if (data.length > 0) { + return asciichart.plot(data, { + height: height, + }) + } else { + return "**No data**" + } }) var app = express() @@ -74,6 +79,7 @@ app.use(express.static(path.join(__dirname, "public"))) app.use("/", indexRouter) app.use("/blocks", blocksRouter) app.use("/mempool", mempoolRouter) +app.use("/search", searchRouter) // catch 404 and forward to error handler app.use(function (req, res, next) { diff --git a/applications/tari_explorer/routes/blocks.js b/applications/tari_explorer/routes/blocks.js index 66adb19c61..1d7c9d57ff 100644 --- a/applications/tari_explorer/routes/blocks.js +++ b/applications/tari_explorer/routes/blocks.js @@ -3,7 +3,6 @@ var { createClient } = require("../baseNodeClient") var express = require("express") var router = express.Router() -/* GET home page. */ router.get("/:height", async function (req, res) { let client = createClient() let height = req.params.height diff --git a/applications/tari_explorer/routes/search.js b/applications/tari_explorer/routes/search.js new file mode 100644 index 0000000000..f59c7caaa9 --- /dev/null +++ b/applications/tari_explorer/routes/search.js @@ -0,0 +1,56 @@ +// Copyright 2021. The Tari Project +// +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the +// following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following +// disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the +// following disclaimer in the documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote +// products derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +var { createClient } = require("../baseNodeClient") + +var express = require("express") +var router = express.Router() + +router.get("/", async function (req, res) { + let client = createClient() + let commitments = ( + req.query.comm || + req.query.commitment || + req.query.c || + "" + ).split(",") + + if (commitments.length === 0) { + res.status(404) + return + } + let hexCommitments = [] + for (let i = 0; i < commitments.length; i++) { + hexCommitments.push(Buffer.from(commitments[i], "hex")) + } + console.log(hexCommitments) + let result = await client.searchUtxos({ + hexCommitments, + }) + + console.log(result) + res.render("search", { + items: result, + }) +}) + +module.exports = router diff --git a/applications/tari_explorer/views/index.hbs b/applications/tari_explorer/views/index.hbs index 00fb83054d..409660acbf 100644 --- a/applications/tari_explorer/views/index.hbs +++ b/applications/tari_explorer/views/index.hbs @@ -148,3 +148,9 @@
+ +
+ + + +
\ No newline at end of file diff --git a/applications/tari_explorer/views/search.hbs b/applications/tari_explorer/views/search.hbs new file mode 100644 index 0000000000..5a2dbeac4d --- /dev/null +++ b/applications/tari_explorer/views/search.hbs @@ -0,0 +1,14 @@ +

{{title}}

+
+

Found in blocks:

+{{#if items.length}} + + {{else}} +

!!! No results found

+{{/if}} \ No newline at end of file diff --git a/clients/base_node_grpc_client/src/index.js b/clients/base_node_grpc_client/src/index.js index 1faec4f662..86ef607dbb 100644 --- a/clients/base_node_grpc_client/src/index.js +++ b/clients/base_node_grpc_client/src/index.js @@ -37,6 +37,7 @@ function Client(address = "127.0.0.1:18142") { "getBlocks", "getMempoolTransactions", "getTipInfo", + "searchUtxos" ]; methods.forEach((method) => { this[method] = (arg) => this.inner[method]().sendMessage(arg);