diff --git a/CHANGELOG.md b/CHANGELOG.md index c07548d592fe..1ac9998300f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [#9502](https://github.com/blockscout/blockscout/pull/9502) - Add batch_size and concurrency envs for tt token type migration - [#9493](https://github.com/blockscout/blockscout/pull/9493) - Fix API response for unknown blob hashes +- [#9426](https://github.com/blockscout/blockscout/pull/9426) - Fix tabs counter cache bug ### Chore diff --git a/apps/explorer/lib/explorer/chain/address/counters.ex b/apps/explorer/lib/explorer/chain/address/counters.ex index 9f0aea47d855..25cb152caf88 100644 --- a/apps/explorer/lib/explorer/chain/address/counters.ex +++ b/apps/explorer/lib/explorer/chain/address/counters.ex @@ -483,7 +483,7 @@ defmodule Explorer.Chain.Address.Counters do case res do {:ok, {txs_type, txs_hashes}} when txs_type in @txs_types -> acc - |> (&Map.put(&1, :txs_types, [txs_type | &1[:txs_types] || []])).() + |> (&Map.put(&1, :txs_types, [txs_type | &1[:txs_types]])).() |> (&Map.put(&1, :txs_hashes, &1[:txs_hashes] ++ txs_hashes)).() {:ok, {type, counter}} -> diff --git a/apps/explorer/lib/explorer/chain/cache/addresses_tabs_counters.ex b/apps/explorer/lib/explorer/chain/cache/addresses_tabs_counters.ex index 750617da62c9..20d30199629a 100644 --- a/apps/explorer/lib/explorer/chain/cache/addresses_tabs_counters.ex +++ b/apps/explorer/lib/explorer/chain/cache/addresses_tabs_counters.ex @@ -20,9 +20,8 @@ defmodule Explorer.Chain.Cache.AddressesTabsCounters do end @spec set_counter(counter_type, String.t(), non_neg_integer()) :: :ok - def set_counter(counter_type, address_hash, counter, need_to_modify_state? \\ true) do + def set_counter(counter_type, address_hash, counter) do :ets.insert(@cache_name, {cache_key(address_hash, counter_type), {DateTime.utc_now(), counter}}) - if need_to_modify_state?, do: ignore_txs(counter_type, address_hash) :ok end @@ -42,10 +41,6 @@ defmodule Explorer.Chain.Cache.AddressesTabsCounters do address_hash |> task_cache_key(counter_type) |> fetch_from_cache(@cache_name, nil) end - @spec ignore_txs(atom, String.t()) :: :ignore | :ok - def ignore_txs(:txs, address_hash), do: GenServer.cast(__MODULE__, {:ignore_txs, address_hash}) - def ignore_txs(_counter_type, _address_hash), do: :ignore - def save_txs_counter_progress(address_hash, results) do GenServer.cast(__MODULE__, {:set_txs_state, address_hash, results}) end @@ -67,11 +62,6 @@ defmodule Explorer.Chain.Cache.AddressesTabsCounters do {:ok, %{}} end - @impl true - def handle_cast({:ignore_txs, address_hash}, state) do - {:noreply, Map.put(state, lowercased_string(address_hash), {:updated, DateTime.utc_now()})} - end - @impl true def handle_cast({:set_txs_state, address_hash, %{txs_types: txs_types} = results}, state) do address_hash = lowercased_string(address_hash) @@ -95,16 +85,22 @@ defmodule Explorer.Chain.Cache.AddressesTabsCounters do |> Enum.count() |> min(Counters.counters_limit()) - if counter == Counters.counters_limit() || Enum.count(address_state[:txs_types]) == 3 do - set_counter(:txs, address_hash, counter, false) - {:noreply, Map.put(state, address_hash, {:updated, DateTime.utc_now()})} - else - {:noreply, Map.put(state, address_hash, address_state)} + cond do + Enum.count(address_state[:txs_types]) == 3 -> + set_counter(:txs, address_hash, counter) + {:noreply, Map.put(state, address_hash, nil)} + + counter == Counters.counters_limit() -> + set_counter(:txs, address_hash, counter) + {:noreply, Map.put(state, address_hash, :limit_value)} + + true -> + {:noreply, Map.put(state, address_hash, address_state)} end end end - defp ignored?({:updated, datetime}), do: up_to_date?(datetime, ttl()) + defp ignored?(:limit_value), do: true defp ignored?(_), do: false defp check_staleness(nil), do: nil