chore: Add primary key to address_tags table (#10818)

* chore: Add primary key to address_tags table

* Remove create_chainlink_oracle_tag/0 function as it is unused
pull/10827/head
Victor Baranov 2 months ago committed by GitHub
parent 96bca66205
commit d95332fc6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 79
      apps/explorer/lib/explorer/tags/address_tag.ex
  2. 57
      apps/explorer/lib/explorer/tags/address_tag_cataloger.ex
  3. 13
      apps/explorer/lib/explorer/tags/address_to_tag.ex
  4. 9
      apps/explorer/priv/repo/migrations/20240923173516_address_tags_add_primary_key.exs

@ -1,6 +1,6 @@
defmodule Explorer.Tags.AddressTag do defmodule Explorer.Tags.AddressTag do
@moduledoc """ @moduledoc """
Represents a Tag object. Represents an address Tag object.
""" """
use Explorer.Schema use Explorer.Schema
@ -9,11 +9,12 @@ defmodule Explorer.Tags.AddressTag do
import Ecto.Query, import Ecto.Query,
only: [ only: [
from: 2 from: 2,
select: 3
] ]
alias Explorer.Repo alias Explorer.Repo
alias Explorer.Tags.{AddressTag, AddressToTag} alias Explorer.Tags.AddressToTag
@typedoc """ @typedoc """
* `:id` - id of Tag * `:id` - id of Tag
@ -21,7 +22,7 @@ defmodule Explorer.Tags.AddressTag do
* `:display_name` - Label's display name * `:display_name` - Label's display name
""" """
typed_schema "address_tags" do typed_schema "address_tags" do
field(:label, :string, null: false) field(:label, :string, primary_key: true, null: false)
field(:display_name, :string, null: false) field(:display_name, :string, null: false)
has_many(:tag_id, AddressToTag, foreign_key: :id) has_many(:tag_id, AddressToTag, foreign_key: :id)
@ -38,55 +39,61 @@ defmodule Explorer.Tags.AddressTag do
|> unique_constraint(:label, name: :address_tags_label_index) |> unique_constraint(:label, name: :address_tags_label_index)
end end
def set_tag(name, display_name) do @spec set(String.t() | nil, String.t() | nil) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()} | :invalid
tag = get_tag(name) def set(name, display_name)
def set(nil, _), do: :invalid
def set(_, nil), do: :invalid
def set(name, display_name) do
tag = get_by_label(name)
if tag do if tag do
tag tag
|> AddressTag.changeset(%{display_name: display_name}) |> __MODULE__.changeset(%{display_name: display_name})
|> Repo.update() |> Repo.update()
else else
%AddressTag{} %__MODULE__{}
|> AddressTag.changeset(%{label: name, display_name: display_name}) |> __MODULE__.changeset(%{label: name, display_name: display_name})
|> Repo.insert() |> Repo.insert()
end end
end end
def get_tag_id(nil), do: nil @doc """
Fetches AddressTag.t() by label name from the DB
def get_tag_id(label) do """
query = @spec get_id_by_label(String.t()) :: non_neg_integer()
from( def get_id_by_label(nil), do: nil
tag in AddressTag,
where: tag.label == ^label,
select: tag.id
)
query def get_id_by_label(label) do
label
|> get_by_label_query()
|> select([tag], tag.id)
|> Repo.one() |> Repo.one()
end end
def get_tag(nil), do: nil @doc """
Fetches all AddressTag.t() from the DB
"""
@spec get_all() :: __MODULE__.t()
def get_all do
__MODULE__
|> Repo.all()
end
def get_tag(label) do defp get_by_label(nil), do: nil
query =
from(
tag in AddressTag,
where: tag.label == ^label
)
query defp get_by_label(label) do
label
|> get_by_label_query()
|> Repo.one() |> Repo.one()
end end
def get_all_tags do defp get_by_label_query(label) do
query = from(
from( tag in __MODULE__,
tag in AddressTag, where: tag.label == ^label
select: tag )
)
query
|> Repo.all()
end end
end end

@ -9,7 +9,6 @@ defmodule Explorer.Tags.AddressTag.Cataloger do
alias Explorer.EnvVarTranslator alias Explorer.EnvVarTranslator
alias Explorer.Tags.{AddressTag, AddressToTag} alias Explorer.Tags.{AddressTag, AddressToTag}
alias Explorer.Validator.MetadataRetriever alias Explorer.Validator.MetadataRetriever
alias Poison.Parser
def start_link(_) do def start_link(_) do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__) GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
@ -24,9 +23,6 @@ defmodule Explorer.Tags.AddressTag.Cataloger do
@impl GenServer @impl GenServer
def handle_info(:fetch_tags, state) do def handle_info(:fetch_tags, state) do
# set tag for every chainlink oracle
create_chainlink_oracle_tag()
create_new_tags() create_new_tags()
send(self(), :bind_addresses) send(self(), :bind_addresses)
@ -47,7 +43,7 @@ defmodule Explorer.Tags.AddressTag.Cataloger do
# set L2 tag # set L2 tag
set_l2_tag() set_l2_tag()
all_tags = AddressTag.get_all_tags() all_tags = AddressTag.get_all()
all_tags all_tags
|> Enum.each(fn %{label: tag_name} -> |> Enum.each(fn %{label: tag_name} ->
@ -68,21 +64,6 @@ defmodule Explorer.Tags.AddressTag.Cataloger do
|> String.replace(".", "_") |> String.replace(".", "_")
end end
def create_chainlink_oracle_tag do
chainlink_oracles_config = Application.get_env(:block_scout_web, :chainlink_oracles)
if chainlink_oracles_config do
chainlink_oracles_config
|> Parser.parse!(%{keys: :atoms!})
|> Enum.each(fn %{:name => name, :address => address} ->
chainlink_tag_name = "chainlink oracle #{String.downcase(name)}"
AddressTag.set_tag(chainlink_tag_name, chainlink_tag_name)
tag_id = AddressTag.get_tag_id(chainlink_tag_name)
AddressToTag.set_tag_to_addresses(tag_id, [address])
end)
end
end
defp set_tag_for_multiple_env_var_addresses(env_vars, tag) do defp set_tag_for_multiple_env_var_addresses(env_vars, tag) do
addresses = addresses =
env_vars env_vars
@ -92,7 +73,7 @@ defmodule Explorer.Tags.AddressTag.Cataloger do
|> String.downcase() |> String.downcase()
end) end)
tag_id = AddressTag.get_tag_id(tag) tag_id = AddressTag.get_id_by_label(tag)
AddressToTag.set_tag_to_addresses(tag_id, addresses) AddressToTag.set_tag_to_addresses(tag_id, addresses)
end end
@ -112,23 +93,23 @@ defmodule Explorer.Tags.AddressTag.Cataloger do
end) end)
end) end)
tag_id = AddressTag.get_tag_id(tag) tag_id = AddressTag.get_id_by_label(tag)
AddressToTag.set_tag_to_addresses(tag_id, addresses) AddressToTag.set_tag_to_addresses(tag_id, addresses)
end end
def create_new_tags do defp create_new_tags do
tags = EnvVarTranslator.map_array_env_var_to_list(:new_tags) tags = EnvVarTranslator.map_array_env_var_to_list(:new_tags)
tags tags
|> Enum.each(fn %{tag: tag_name, title: tag_display_name} -> |> Enum.each(fn %{tag: tag_name, title: tag_display_name} ->
AddressTag.set_tag(tag_name, tag_display_name) AddressTag.set(tag_name, tag_display_name)
end) end)
end end
defp set_tag_for_env_var_multiple_addresses(env_var, tag) do defp set_tag_for_env_var_multiple_addresses(env_var, tag) do
addresses = env_var_string_array_to_list(env_var) addresses = env_var_string_array_to_list(env_var)
tag_id = AddressTag.get_tag_id(tag) tag_id = AddressTag.get_id_by_label(tag)
AddressToTag.set_tag_to_addresses(tag_id, addresses) AddressToTag.set_tag_to_addresses(tag_id, addresses)
end end
@ -152,7 +133,7 @@ defmodule Explorer.Tags.AddressTag.Cataloger do
defp set_validator_tag do defp set_validator_tag do
validators = MetadataRetriever.fetch_validators_list() validators = MetadataRetriever.fetch_validators_list()
FetchValidatorInfoOnDemand.trigger_fetch(validators) FetchValidatorInfoOnDemand.trigger_fetch(validators)
tag_id = AddressTag.get_tag_id("validator") tag_id = AddressTag.get_id_by_label("validator")
AddressToTag.set_tag_to_addresses(tag_id, validators) AddressToTag.set_tag_to_addresses(tag_id, validators)
end end
@ -177,28 +158,4 @@ defmodule Explorer.Tags.AddressTag.Cataloger do
defp set_l2_tag do defp set_l2_tag do
set_tag_for_multiple_env_var_addresses(["CUSTOM_CONTRACT_ADDRESSES_AOX"], "l2") set_tag_for_multiple_env_var_addresses(["CUSTOM_CONTRACT_ADDRESSES_AOX"], "l2")
end end
def set_chainlink_oracle_tag do
chainlink_oracles = chainlink_oracles_list()
tag_id = AddressTag.get_tag_id("chainlink oracle")
AddressToTag.set_tag_to_addresses(tag_id, chainlink_oracles)
end
defp chainlink_oracles_list do
chainlink_oracles_config = Application.get_env(:block_scout_web, :chainlink_oracles)
if chainlink_oracles_config do
try do
chainlink_oracles_config
|> Parser.parse!(%{keys: :atoms!})
|> Enum.map(fn %{:name => _name, :address => address} -> address end)
rescue
_ ->
[]
end
else
[]
end
end
end end

@ -1,6 +1,6 @@
defmodule Explorer.Tags.AddressToTag do defmodule Explorer.Tags.AddressToTag do
@moduledoc """ @moduledoc """
Represents ann Address to Tag relation. Represents Address to Tag relation.
""" """
use Explorer.Schema use Explorer.Schema
@ -9,7 +9,7 @@ defmodule Explorer.Tags.AddressToTag do
alias Explorer.{Chain, Repo} alias Explorer.{Chain, Repo}
alias Explorer.Chain.{Address, Hash} alias Explorer.Chain.{Address, Hash}
alias Explorer.Tags.{AddressTag, AddressToTag} alias Explorer.Tags.AddressTag
# Notation.import_types(BlockScoutWeb.GraphQL.Schema.Types) # Notation.import_types(BlockScoutWeb.GraphQL.Schema.Types)
@ -53,7 +53,7 @@ defmodule Explorer.Tags.AddressToTag do
defp get_address_hashes_mapped_to_tag(tag_id) do defp get_address_hashes_mapped_to_tag(tag_id) do
query = query =
from( from(
att in AddressToTag, att in __MODULE__,
where: att.tag_id == ^tag_id, where: att.tag_id == ^tag_id,
select: att.address_hash select: att.address_hash
) )
@ -62,6 +62,7 @@ defmodule Explorer.Tags.AddressToTag do
|> Repo.all() |> Repo.all()
end end
@spec set_tag_to_addresses(non_neg_integer(), list()) :: any()
def set_tag_to_addresses(tag_id, address_hash_string_list) do def set_tag_to_addresses(tag_id, address_hash_string_list) do
current_address_hashes = get_address_hashes_mapped_to_tag(tag_id) current_address_hashes = get_address_hashes_mapped_to_tag(tag_id)
@ -105,10 +106,10 @@ defmodule Explorer.Tags.AddressToTag do
end) end)
|> Enum.filter(&(!is_nil(&1))) |> Enum.filter(&(!is_nil(&1)))
if not Enum.empty?(addresses_to_delete) do unless Enum.empty?(addresses_to_delete) do
delete_query_base = delete_query_base =
from( from(
att in AddressToTag, att in __MODULE__,
where: att.tag_id == ^tag_id where: att.tag_id == ^tag_id
) )
@ -119,7 +120,7 @@ defmodule Explorer.Tags.AddressToTag do
Repo.delete_all(delete_query) Repo.delete_all(delete_query)
end end
Repo.insert_all(AddressToTag, changeset_to_add_list, Repo.insert_all(__MODULE__, changeset_to_add_list,
on_conflict: :nothing, on_conflict: :nothing,
conflict_target: [:address_hash, :tag_id] conflict_target: [:address_hash, :tag_id]
) )

@ -0,0 +1,9 @@
defmodule Explorer.Repo.Migrations.AddressTagsAddPrimaryKey do
use Ecto.Migration
def change do
alter table(:address_tags) do
modify(:label, :string, null: false, primary_key: true)
end
end
end
Loading…
Cancel
Save