fix: Fix metadata preload (#9925)

* fix: Fix metadata preload

* Add regression test
pull/9943/head
nikitosing 7 months ago committed by GitHub
parent 631af84a6c
commit af4c29db8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 79
      apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs
  2. 4
      apps/explorer/lib/explorer/chain/address/metadata_preloader.ex
  3. 7
      apps/explorer/lib/explorer/microservice_interfaces/bens.ex

@ -24,6 +24,7 @@ defmodule BlockScoutWeb.API.V2.AddressControllerTest do
alias Explorer.Account.WatchlistAddress
alias Explorer.Chain.Address.CurrentTokenBalance
alias Plug.Conn
import Explorer.Chain, only: [hash_to_lower_case_string: 1]
import Mox
@ -1763,6 +1764,84 @@ defmodule BlockScoutWeb.API.V2.AddressControllerTest do
check_paginated_response(response, response_2nd_page, logs)
end
# https://github.com/blockscout/blockscout/issues/9926
test "regression test for 9926", %{conn: conn} do
address = insert(:address, hash: "0x036cec1a199234fC02f72d29e596a09440825f1C")
tx =
:transaction
|> insert()
|> with_block()
log =
insert(:log,
transaction: tx,
index: 1,
block: tx.block,
block_number: tx.block_number,
address: address
)
bypass = Bypass.open()
old_chain_id = Application.get_env(:block_scout_web, :chain_id)
chain_id = 1
Application.put_env(:block_scout_web, :chain_id, chain_id)
old_env_bens = Application.get_env(:explorer, Explorer.MicroserviceInterfaces.BENS)
Application.put_env(:explorer, Explorer.MicroserviceInterfaces.BENS,
service_url: "http://localhost:#{bypass.port}",
enabled: true
)
old_env_metadata = Application.get_env(:explorer, Explorer.MicroserviceInterfaces.Metadata)
Application.put_env(:explorer, Explorer.MicroserviceInterfaces.Metadata,
service_url: "http://localhost:#{bypass.port}",
enabled: true
)
Bypass.expect_once(bypass, "POST", "api/v1/#{chain_id}/addresses:batch_resolve_names", fn conn ->
Conn.resp(
conn,
200,
Jason.encode!(%{
"names" => %{
to_string(address) => "test.eth"
}
})
)
end)
Bypass.expect_once(bypass, "POST", "api/v1/metadata", fn conn ->
Conn.resp(
conn,
200,
Jason.encode!(%{
"addresses" => %{
to_string(address) => %{"tags" => [%{"slug" => "tag", "meta" => "{\"styles\":\"danger_high\"}"}]}
}
})
)
end)
request = get(conn, "/api/v2/addresses/#{address.hash}/logs")
assert response = json_response(request, 200)
assert Enum.count(response["items"]) == 1
assert response["next_page_params"] == nil
compare_item(log, Enum.at(response["items"], 0))
log = Enum.at(response["items"], 0)
assert log["address"]["ens_domain_name"] == "test.eth"
assert log["address"]["metadata"] == %{"tags" => [%{"slug" => "tag", "meta" => %{"styles" => "danger_high"}}]}
Application.put_env(:block_scout_web, :chain_id, old_chain_id)
Application.put_env(:explorer, Explorer.MicroserviceInterfaces.BENS, old_env_bens)
Application.put_env(:explorer, Explorer.MicroserviceInterfaces.Metadata, old_env_metadata)
Bypass.down(bypass)
end
test "logs can be filtered by topic", %{conn: conn} do
address = insert(:address)

@ -300,4 +300,8 @@ defmodule Explorer.Chain.Address.MetadataPreloader do
defp alter_address(%Address{} = address, address_hash, names, :metadata) do
%Address{address | metadata: names[Address.checksum(address_hash)]}
end
defp alter_address(map, address_hash, names, field) when is_map(map) do
Map.put(map, field, names[Address.checksum(address_hash)])
end
end

@ -137,7 +137,12 @@ defmodule Explorer.MicroserviceInterfaces.BENS do
def enabled?, do: Microservice.check_enabled(__MODULE__) == :ok
defp batch_resolve_name_url do
"#{addresses_url()}:batch-resolve-names"
# workaround for https://github.com/PSPDFKit-labs/bypass/issues/122
if Mix.env() == :test do
"#{addresses_url()}:batch_resolve_names"
else
"#{addresses_url()}:batch-resolve-names"
end
end
defp address_lookup_url do

Loading…
Cancel
Save