Skip to content

Commit 75e5c99

Browse files
committed
feat: support aggregation mode in explorer search
1 parent 124eba8 commit 75e5c99

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

explorer/lib/explorer/models/aggregated_proofs.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ defmodule AggregatedProofs do
6767
Explorer.Repo.get_by(AggregatedProofs, id: id)
6868
end
6969

70+
def get_newest_aggregated_proof_by_merkle_root(merkle_root) do
71+
query =
72+
from(proof in AggregatedProofs,
73+
order_by: [desc: proof.block_number],
74+
limit: 1,
75+
where: proof.merkle_root == ^merkle_root,
76+
select: proof
77+
)
78+
79+
Explorer.Repo.one(query)
80+
end
81+
7082
def get_paginated_proofs(%{page: page, page_size: size}) do
7183
query =
7284
from(proof in AggregatedProofs,

explorer/lib/explorer/models/aggregation_mode_proof.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,16 @@ defmodule AggregationModeProof do
5858

5959
Explorer.Repo.all(query)
6060
end
61+
62+
def get_newest_proof_by_hash(hash) do
63+
query =
64+
from(proof in AggregationModeProof,
65+
limit: 1,
66+
order_by: [desc: proof.inserted_at],
67+
where: proof.proof_hash == ^hash,
68+
select: proof
69+
)
70+
71+
Explorer.Repo.one(query)
72+
end
6173
end

explorer/lib/explorer_web/components/search.ex

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
defmodule SearchComponent do
2+
require Logger
23
use ExplorerWeb, :live_component
34

45
@impl true
5-
def handle_event("search_batch", %{"batch" => %{"merkle_root" => input_hash}}, socket) do
6-
input_hash
6+
def handle_event("search_batch", %{"search" => search}, socket) do
7+
search
78
|> (fn hash ->
89
if String.match?(hash, ~r/^0x[a-fA-F0-9]+$/), do: {:ok, hash}, else: :invalid_hash
910
end).()
1011
|> case do
1112
{:ok, hash} ->
12-
case Proofs.get_number_of_batches_containing_proof(hash) do
13-
0 -> {:noreply, push_navigate(socket, to: ~p"/batches/#{hash}")}
14-
_ -> {:noreply, push_navigate(socket, to: ~p"/search?q=#{hash}")}
13+
cond do
14+
# See if the hash belongs to a proof
15+
Proofs.get_number_of_batches_containing_proof(hash) > 0 ->
16+
{:noreply, push_navigate(socket, to: ~p"/batches/#{hash}")}
17+
18+
# See if the hash belongs to an aggregated proof merkle root
19+
(proof = AggregatedProofs.get_newest_aggregated_proof_by_merkle_root(hash)) != nil ->
20+
Logger.debug(proof)
21+
{:noreply, push_navigate(socket, to: ~p"/aggregated_proofs/#{proof.id}")}
22+
23+
# Finally, see if the hash belongs to a proof of an aggregated proof
24+
(proof = AggregationModeProof.get_newest_proof_by_hash(hash)) != nil ->
25+
{:noreply, push_navigate(socket, to: ~p"/aggregated_proofs/#{proof.agg_proof_id}")}
26+
27+
# Otherwise inform the user nothing was found
28+
true ->
29+
{:noreply,
30+
socket
31+
|> put_flash!(:error, "No batch or proof was found with the provided hash.")}
1532
end
1633

1734
:invalid_hash ->
1835
{:noreply,
1936
socket
20-
|> assign(batch_merkle_root: input_hash)
21-
|> put_flash!(:error, "Please enter a valid proof batch hash (0x69...).")}
37+
|> put_flash!(:error, "Please enter a valid hash (0x69...).")}
2238
end
2339
end
2440

@@ -42,8 +58,8 @@ defmodule SearchComponent do
4258
id={"input_#{assigns.id}"}
4359
class="pr-10 w-full text-foreground rounded-lg border-foreground/20 bg-card focus:border-foreground/20 focus:ring-accent text-sm"
4460
type="search"
45-
placeholder="Search batch by batch hash or proof hash"
46-
name="batch[merkle_root]"
61+
placeholder="Search by batch hash or proof hash"
62+
name="search"
4763
/>
4864
<.icon name="hero-magnifying-glass-solid" class="absolute right-3 text-foreground/20 size-5 hover:text-foreground" />
4965
</form>

explorer/mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"floki": {:hex, :floki, "0.36.2", "a7da0193538c93f937714a6704369711998a51a6164a222d710ebd54020aa7a3", [:mix], [], "hexpm", "a8766c0bc92f074e5cb36c4f9961982eda84c5d2b8e979ca67f5c268ec8ed580"},
2525
"gettext": {:hex, :gettext, "0.24.0", "6f4d90ac5f3111673cbefc4ebee96fe5f37a114861ab8c7b7d5b30a1108ce6d8", [:mix], [{:expo, "~> 0.5.1", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "bdf75cdfcbe9e4622dd18e034b227d77dd17f0f133853a1c73b97b3d6c770e8b"},
2626
"hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"},
27-
"heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized"]},
27+
"heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized", depth: 1]},
2828
"hpax": {:hex, :hpax, "0.2.0", "5a58219adcb75977b2edce5eb22051de9362f08236220c9e859a47111c194ff5", [:mix], [], "hexpm", "bea06558cdae85bed075e6c036993d43cd54d447f76d8190a8db0dc5893fa2f1"},
2929
"httpoison": {:hex, :httpoison, "2.2.1", "87b7ed6d95db0389f7df02779644171d7319d319178f6680438167d7b69b1f3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "51364e6d2f429d80e14fe4b5f8e39719cacd03eb3f9a9286e61e216feac2d2df"},
3030
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},

0 commit comments

Comments
 (0)