Add block_type to search results (#8530)

* Add block_type to search results

* Changelog
pull/8552/head
nikitosing 1 year ago committed by GitHub
parent 7e089c90ed
commit 2e8d9060c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 22
      apps/block_scout_web/lib/block_scout_web/views/api/v2/search_view.ex
  3. 36
      apps/block_scout_web/test/block_scout_web/controllers/api/v2/search_controller_test.exs

@ -4,6 +4,7 @@
### Features
- [#8530](https://github.com/blockscout/blockscout/pull/8530) - Add `block_type` to search results
- [#8180](https://github.com/blockscout/blockscout/pull/8180) - Deposits and Withdrawals for Polygon Edge
- [#7996](https://github.com/blockscout/blockscout/pull/7996) - Add CoinBalance fetcher init query limit

@ -1,7 +1,8 @@
defmodule BlockScoutWeb.API.V2.SearchView do
use BlockScoutWeb, :view
alias BlockScoutWeb.Endpoint
alias BlockScoutWeb.{BlockView, Endpoint}
alias Explorer.Chain
alias Explorer.Chain.{Address, Block, Hash, Transaction}
def render("search_results.json", %{search_results: search_results, next_page_params: next_page_params}) do
@ -53,12 +54,21 @@ defmodule BlockScoutWeb.API.V2.SearchView do
def prepare_search_result(%{type: "block"} = search_result) do
block_hash = hash_to_string(search_result.block_hash)
{:ok, block} =
Chain.hash_to_block(hash(search_result.block_hash),
necessity_by_association: %{
:nephews => :optional
},
api?: true
)
%{
"type" => search_result.type,
"block_number" => search_result.block_number,
"block_hash" => block_hash,
"url" => block_path(Endpoint, :show, block_hash),
"timestamp" => search_result.timestamp
"timestamp" => search_result.timestamp,
"block_type" => block |> BlockView.block_type() |> String.downcase()
}
end
@ -76,6 +86,14 @@ defmodule BlockScoutWeb.API.V2.SearchView do
defp hash_to_string(%Hash{bytes: bytes}), do: hash_to_string(bytes)
defp hash_to_string(hash), do: "0x" <> Base.encode16(hash, case: :lower)
defp hash(%Hash{} = hash), do: hash
defp hash(bytes),
do: %Hash{
byte_count: 32,
bytes: bytes
}
defp redirect_search_results(%Address{} = item) do
%{"type" => "address", "parameter" => Address.checksum(item.hash)}
end

@ -28,6 +28,7 @@ defmodule BlockScoutWeb.API.V2.SearchControllerTest do
item = Enum.at(response["items"], 0)
assert item["type"] == "block"
assert item["block_type"] == "block"
assert item["block_number"] == block.number
assert item["block_hash"] == to_string(block.hash)
assert item["url"] =~ to_string(block.hash)
@ -47,6 +48,38 @@ defmodule BlockScoutWeb.API.V2.SearchControllerTest do
assert item["timestamp"] == block.timestamp |> to_string() |> String.replace(" ", "T")
end
test "search reorg", %{conn: conn} do
block = insert(:block, consensus: false)
request = get(conn, "/api/v2/search?q=#{block.hash}")
assert response = json_response(request, 200)
assert Enum.count(response["items"]) == 1
assert response["next_page_params"] == nil
item = Enum.at(response["items"], 0)
assert item["type"] == "block"
assert item["block_type"] == "reorg"
assert item["block_number"] == block.number
assert item["block_hash"] == to_string(block.hash)
assert item["url"] =~ to_string(block.hash)
request = get(conn, "/api/v2/search?q=#{block.number}")
assert response = json_response(request, 200)
assert Enum.count(response["items"]) == 1
assert response["next_page_params"] == nil
item = Enum.at(response["items"], 0)
assert item["type"] == "block"
assert item["block_type"] == "reorg"
assert item["block_number"] == block.number
assert item["block_hash"] == to_string(block.hash)
assert item["url"] =~ to_string(block.hash)
end
test "search address", %{conn: conn} do
address = insert(:address)
name = insert(:unique_address_name, address: address)
@ -326,6 +359,9 @@ defmodule BlockScoutWeb.API.V2.SearchControllerTest do
assert block_hashes == blocks |> Enum.reverse() |> Enum.map(fn block -> to_string(block.hash) end) ||
block_hashes == blocks |> Enum.map(fn block -> to_string(block.hash) end)
assert response |> Enum.filter(fn x -> x["block_type"] == "block" end) |> Enum.count() == 1
assert response |> Enum.filter(fn x -> x["block_type"] == "reorg" end) |> Enum.count() == 1
end
test "returns empty list and don't crash", %{conn: conn} do

Loading…
Cancel
Save