From b899b64d43d3496e93f7a88ed59a1462ff11a14d Mon Sep 17 00:00:00 2001 From: nikitosing <32202610+nikitosing@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:40:40 +0300 Subject: [PATCH] fix: invalid metadata requests (#11210) * fix: prevent empty requests to metadata service * fix: remove empty hashes from request parameters * Small fix * Add docs --------- Co-authored-by: Leonid Tyurin --- .../chain/address/metadata_preloader.ex | 5 ++-- .../microservice_interfaces/metadata.ex | 24 ++++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/address/metadata_preloader.ex b/apps/explorer/lib/explorer/chain/address/metadata_preloader.ex index 0159591531..85cdebe5e2 100644 --- a/apps/explorer/lib/explorer/chain/address/metadata_preloader.ex +++ b/apps/explorer/lib/explorer/chain/address/metadata_preloader.ex @@ -86,9 +86,8 @@ defmodule Explorer.Chain.Address.MetadataPreloader do def preload_metadata_to_list(items) do address_hash_strings = items - |> Enum.reduce([], fn item, acc -> - item_to_address_hash_strings(item) ++ acc - end) + |> Enum.flat_map(&item_to_address_hash_strings/1) + |> Enum.filter(&(&1 != "")) |> Enum.uniq() case Metadata.get_addresses_tags(address_hash_strings) do diff --git a/apps/explorer/lib/explorer/microservice_interfaces/metadata.ex b/apps/explorer/lib/explorer/microservice_interfaces/metadata.ex index ac3b1f7889..cf7aa99203 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/metadata.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/metadata.ex @@ -18,16 +18,34 @@ defmodule Explorer.MicroserviceInterfaces.Metadata do @page_size 50 @request_error_msg "Error while sending request to Metadata microservice" - @spec get_addresses_tags([String.t()]) :: {:error, :disabled | <<_::416>> | Jason.DecodeError.t()} | {:ok, any()} + @doc """ + Retrieves tags for a list of addresses. + + ## Parameters + - `addresses`: A list of addresses for which tags need to be fetched. + + ## Returns + - A map with metadata tags from microservice. Returns `:ignore` when the input list is empty. + + ## Examples + + iex> get_addresses_tags([]) + :ignore + + """ + @spec get_addresses_tags([String.t()]) :: + {:error, :disabled | <<_::416>> | Jason.DecodeError.t()} | {:ok, any()} | :ignore + def get_addresses_tags([]), do: :ignore + def get_addresses_tags(addresses) do with :ok <- Microservice.check_enabled(__MODULE__) do - body = %{ + params = %{ addresses: Enum.join(addresses, ","), tags_limit: @tags_per_address_limit, chain_id: Application.get_env(:block_scout_web, :chain_id) } - http_get_request(addresses_metadata_url(), body) + http_get_request(addresses_metadata_url(), params) end end