From ddec341d950fc93b1c24252b3f35102aabf27d7b Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 2 Dec 2019 10:36:38 +0300 Subject: [PATCH] fix address sum cache on the first call address sum cache starts task to fetch a value from the DB and return nil --- CHANGELOG.md | 1 + .../api/rpc/stats_controller_test.exs | 8 ++++++++ .../lib/explorer/chain/cache/address_sum.ex | 16 +++++++++++----- .../explorer/chain/cache/address_sum_test.exs | 2 +- .../runner/address/token_balances_test.exs | 2 +- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62ce88bd8d..2dc425ea27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features ### Fixes +- [#2901](https://github.com/poanetwork/blockscout/pull/2901) - fix address sum cache ### Chore diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs index a57db2221b..5a7e3f29ea 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs @@ -7,6 +7,12 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do alias Explorer.ExchangeRates.Token alias Explorer.ExchangeRates.Source.TestSource + setup do + Supervisor.terminate_child(Explorer.Supervisor, Explorer.Chain.Cache.AddressSum.child_id()) + Supervisor.restart_child(Explorer.Supervisor, Explorer.Chain.Cache.AddressSum.child_id()) + :ok + end + describe "tokensupply" do test "with missing contract address", %{conn: conn} do params = %{ @@ -106,6 +112,8 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do describe "ethsupply" do test "returns total supply from DB", %{conn: conn} do + insert(:address, fetched_coin_balance: 6) + params = %{ "module" => "stats", "action" => "ethsupply" diff --git a/apps/explorer/lib/explorer/chain/cache/address_sum.ex b/apps/explorer/lib/explorer/chain/cache/address_sum.ex index b0d3326312..4a66e1369c 100644 --- a/apps/explorer/lib/explorer/chain/cache/address_sum.ex +++ b/apps/explorer/lib/explorer/chain/cache/address_sum.ex @@ -16,11 +16,13 @@ defmodule Explorer.Chain.Cache.AddressSum do alias Explorer.Chain defp handle_fallback(:sum) do - # This will get the task PID if one exists and launch a new task if not - # See next `handle_fallback` definition - get_async_task() + result = fetch_from_db() - {:return, nil} + if Application.get_env(:explorer, __MODULE__)[:enabled] do + {:update, result} + else + {:return, result} + end end defp handle_fallback(:async_task) do @@ -29,7 +31,7 @@ defmodule Explorer.Chain.Cache.AddressSum do {:ok, task} = Task.start(fn -> try do - result = Chain.fetch_sum_coin_total_supply() + result = fetch_from_db() set_sum(result) rescue @@ -45,6 +47,10 @@ defmodule Explorer.Chain.Cache.AddressSum do {:update, task} end + defp fetch_from_db do + Chain.fetch_sum_coin_total_supply() + end + # By setting this as a `callback` an async task will be started each time the # `sum` expires (unless there is one already running) defp async_task_on_deletion({:delete, _, :sum}), do: get_async_task() diff --git a/apps/explorer/test/explorer/chain/cache/address_sum_test.exs b/apps/explorer/test/explorer/chain/cache/address_sum_test.exs index 707c70635f..ac712c5287 100644 --- a/apps/explorer/test/explorer/chain/cache/address_sum_test.exs +++ b/apps/explorer/test/explorer/chain/cache/address_sum_test.exs @@ -12,7 +12,7 @@ defmodule Explorer.Chain.Cache.AddressSumTest do test "returns default address sum" do result = AddressSum.get_sum() - assert is_nil(result) + assert result == 0 end test "updates cache if initial value is zero" do diff --git a/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs b/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs index a40cd8599e..a35fbcc578 100644 --- a/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs +++ b/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs @@ -93,7 +93,7 @@ defmodule Explorer.Chain.Import.Runner.Address.TokenBalancesTest do value_fetched_at: DateTime.utc_now() } - run_changes(new_changes, options) |> IO.inspect() + run_changes(new_changes, options) end end