Skip to content

Commit 05191dc

Browse files
feat(explorer): support search for aggregation mode (#1944)
Co-authored-by: JuArce <[email protected]>
1 parent c7b139c commit 05191dc

File tree

4 files changed

+78
-32
lines changed

4 files changed

+78
-32
lines changed

explorer/lib/explorer/models/aggregated_proofs.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,25 @@ 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+
select: proof,
74+
where: proof.merkle_root == ^merkle_root,
75+
order_by: [desc: proof.block_number],
76+
limit: 1
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,
85+
select: proof,
7386
order_by: [desc: proof.block_number],
7487
limit: ^size,
75-
offset: ^((page - 1) * size),
76-
select: proof
88+
offset: ^((page - 1) * size)
7789
)
7890

7991
Explorer.Repo.all(query)

explorer/lib/explorer/models/aggregation_mode_proof.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,22 @@ defmodule AggregationModeProof do
5252
def get_all_proof_hashes(id) do
5353
query =
5454
from(proof in AggregationModeProof,
55-
where: proof.agg_proof_id == ^id,
56-
select: proof.proof_hash
55+
select: proof.proof_hash,
56+
where: proof.agg_proof_id == ^id
5757
)
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+
select: proof,
66+
where: proof.proof_hash == ^hash,
67+
order_by: [desc: proof.inserted_at],
68+
limit: 1
69+
)
70+
71+
Explorer.Repo.one(query)
72+
end
6173
end

explorer/lib/explorer_web/components/search.ex

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
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 in a batch
15+
# If so, redirect to search to show all the batches where this proofs exists
16+
Proofs.get_number_of_batches_containing_proof(hash) > 0 ->
17+
{:noreply, push_navigate(socket, to: ~p"/search?q=#{hash}")}
18+
19+
# See if the hash belongs to the root of a batch
20+
Batches.get_batch(%{merkle_root: hash}) != nil ->
21+
{:noreply, push_navigate(socket, to: ~p"/batches/#{hash}")}
22+
23+
# See if the hash belongs to an aggregated proof merkle root
24+
(proof = AggregatedProofs.get_newest_aggregated_proof_by_merkle_root(hash)) != nil ->
25+
{:noreply, push_navigate(socket, to: ~p"/aggregated_proofs/#{proof.id}")}
26+
27+
# Finally, see if the hash belongs to a proof of an aggregated proof
28+
(proof = AggregationModeProof.get_newest_proof_by_hash(hash)) != nil ->
29+
{:noreply, push_navigate(socket, to: ~p"/aggregated_proofs/#{proof.agg_proof_id}")}
30+
31+
# Otherwise inform the user nothing was found
32+
true ->
33+
{:noreply,
34+
socket
35+
|> put_flash!(:error, "No batch or proof was found with the provided hash.")}
1536
end
1637

1738
:invalid_hash ->
1839
{:noreply,
1940
socket
20-
|> assign(batch_merkle_root: input_hash)
21-
|> put_flash!(:error, "Please enter a valid proof batch hash (0x69...).")}
41+
|> put_flash!(:error, "Please enter a valid hash (0x69...).")}
2242
end
2343
end
2444

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

explorer/lib/explorer_web/live/pages/search/index.ex

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,27 @@ defmodule ExplorerWeb.Search.Index do
5252
Search Results for "<%= @hash |> Helpers.shorten_hash() %>"
5353
</.card_preheding>
5454
<%= if @results != nil or @results != [] do %>
55-
<.table id="results" rows={@results}>
56-
<:col :let={result} label="Batch Hash" class="text-left">
57-
<.link
58-
navigate={~p"/batches/#{result}"}
59-
class="flex justify-between group group-hover:text-foreground/80"
60-
>
61-
<span class="items-center group-hover:text-foreground/80 hidden md:inline">
62-
<%= result %>
63-
</span>
64-
<span class="items-center group-hover:text-foreground/80 md:hidden">
65-
<%= result |> Helpers.shorten_hash(12) %>
66-
</span>
67-
<.right_arrow />
68-
<.tooltip>
69-
<%= result %>
70-
</.tooltip>
71-
</.link>
72-
</:col>
73-
</.table>
55+
<.card_background>
56+
<.table id="results" rows={@results}>
57+
<:col :let={result} label="Batch Hash" class="text-left">
58+
<.link
59+
navigate={~p"/batches/#{result}"}
60+
class="flex justify-between group group-hover:text-foreground/80"
61+
>
62+
<span class="items-center group-hover:text-foreground/80 hidden md:inline">
63+
<%= result %>
64+
</span>
65+
<span class="items-center group-hover:text-foreground/80 md:hidden">
66+
<%= result |> Helpers.shorten_hash(12) %>
67+
</span>
68+
<.right_arrow />
69+
<.tooltip>
70+
<%= result %>
71+
</.tooltip>
72+
</.link>
73+
</:col>
74+
</.table>
75+
</.card_background>
7476
<div class="flex gap-x-2 justify-center items-center">
7577
<%= if @current_page != 1 do %>
7678
<.link patch={~p"/search?q=#{@hash}&page=#{@current_page - 1}"}>

0 commit comments

Comments
 (0)