From 1bd25dcc45d64ba25cdf222ff54086eac4f7d1b0 Mon Sep 17 00:00:00 2001 From: saneery Date: Mon, 29 Apr 2019 16:18:54 +0300 Subject: [PATCH 1/5] don't lookup at table when table isn't inited --- .../lib/explorer/exchange_rates/exchange_rates.ex | 8 +++++++- .../lib/explorer/known_tokens/known_tokens.ex | 14 ++++++++++++-- .../exchange_rates/exchange_rates_test.exs | 7 +++++++ .../explorer/known_tokens/known_tokens_test.exs | 7 +++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex b/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex index 514751d8d6..f867e9b0ae 100644 --- a/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex +++ b/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex @@ -90,7 +90,7 @@ defmodule Explorer.ExchangeRates do """ @spec lookup(String.t()) :: Token.t() | nil def lookup(symbol) do - if store() == :ets do + if store() == :ets and enabled?() do case :ets.lookup(table_name(), symbol) do [tuple | _] when is_tuple(tuple) -> Token.from_tuple(tuple) _ -> nil @@ -133,4 +133,10 @@ defmodule Explorer.ExchangeRates do defp store do config(:store) || :ets end + + defp enabled? do + :explorer + |> Application.fetch_env!(__MODULE__) + |> Keyword.fetch!(:enabled) + end end diff --git a/apps/explorer/lib/explorer/known_tokens/known_tokens.ex b/apps/explorer/lib/explorer/known_tokens/known_tokens.ex index baa09784ba..4ef6c62df8 100644 --- a/apps/explorer/lib/explorer/known_tokens/known_tokens.ex +++ b/apps/explorer/lib/explorer/known_tokens/known_tokens.ex @@ -81,7 +81,11 @@ defmodule Explorer.KnownTokens do """ @spec list :: [{String.t(), Hash.Address.t()}] def list do - list_from_store(store()) + if enabled?() do + list_from_store(store()) + else + [] + end end @doc """ @@ -89,7 +93,7 @@ defmodule Explorer.KnownTokens do """ @spec lookup(String.t()) :: {:ok, Hash.Address.t()} | :error | nil def lookup(symbol) do - if store() == :ets do + if store() == :ets && enabled?()do case :ets.lookup(table_name(), symbol) do [{_symbol, address} | _] -> Hash.Address.cast(address) _ -> nil @@ -128,4 +132,10 @@ defmodule Explorer.KnownTokens do defp store do config(:store) || :ets end + + defp enabled? do + :explorer + |> Application.fetch_env!(__MODULE__) + |> Keyword.fetch!(:enabled) + end end diff --git a/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs b/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs index 1d44a7adce..ff1510937a 100644 --- a/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs +++ b/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs @@ -19,6 +19,7 @@ defmodule Explorer.ExchangeRatesTest do Application.put_env(:explorer, Explorer.ExchangeRates.Source, source: TestSource) Application.put_env(:explorer, Explorer.ExchangeRates, table_name: :rates) + Application.put_env(:explorer, Explorer.ExchangeRates, enabled: true) on_exit(fn -> Application.put_env(:explorer, Explorer.ExchangeRates.Source, source_configuration) @@ -135,4 +136,10 @@ defmodule Explorer.ExchangeRatesTest do assert z == ExchangeRates.lookup("z") assert nil == ExchangeRates.lookup("nope") end + + test "lookup when desibled" do + Application.put_env(:explorer, Explorer.ExchangeRates, enabled: false) + + assert nil == ExchangeRates.lookup("z") + end end diff --git a/apps/explorer/test/explorer/known_tokens/known_tokens_test.exs b/apps/explorer/test/explorer/known_tokens/known_tokens_test.exs index 7d5cb21407..a33d3c85e7 100644 --- a/apps/explorer/test/explorer/known_tokens/known_tokens_test.exs +++ b/apps/explorer/test/explorer/known_tokens/known_tokens_test.exs @@ -21,6 +21,7 @@ defmodule Explorer.KnownTokensTest do Application.put_env(:explorer, Explorer.KnownTokens.Source, source: TestSource) Application.put_env(:explorer, Explorer.KnownTokens, table_name: :known_tokens) + Application.put_env(:explorer, Explorer.KnownTokens, enabled: true) on_exit(fn -> Application.put_env(:explorer, Explorer.KnownTokens.Source, source_configuration) @@ -128,4 +129,10 @@ defmodule Explorer.KnownTokensTest do assert Hash.Address.cast("0x0000000000000000000000000000000000000001") == KnownTokens.lookup("TEST1") assert nil == KnownTokens.lookup("nope") end + + test "lookup when desibled" do + Application.put_env(:explorer, Explorer.KnownTokens, enabled: false) + + assert nil == KnownTokens.lookup("z") + end end From c4583bdb8b0a94fd0f828b5f13306f1d109dbac0 Mon Sep 17 00:00:00 2001 From: saneery Date: Mon, 29 Apr 2019 16:28:52 +0300 Subject: [PATCH 2/5] mix format --- apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex | 2 +- apps/explorer/lib/explorer/known_tokens/known_tokens.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex b/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex index f867e9b0ae..b4dc0229f9 100644 --- a/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex +++ b/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex @@ -90,7 +90,7 @@ defmodule Explorer.ExchangeRates do """ @spec lookup(String.t()) :: Token.t() | nil def lookup(symbol) do - if store() == :ets and enabled?() do + if store() == :ets && enabled?() do case :ets.lookup(table_name(), symbol) do [tuple | _] when is_tuple(tuple) -> Token.from_tuple(tuple) _ -> nil diff --git a/apps/explorer/lib/explorer/known_tokens/known_tokens.ex b/apps/explorer/lib/explorer/known_tokens/known_tokens.ex index 4ef6c62df8..caca671540 100644 --- a/apps/explorer/lib/explorer/known_tokens/known_tokens.ex +++ b/apps/explorer/lib/explorer/known_tokens/known_tokens.ex @@ -93,7 +93,7 @@ defmodule Explorer.KnownTokens do """ @spec lookup(String.t()) :: {:ok, Hash.Address.t()} | :error | nil def lookup(symbol) do - if store() == :ets && enabled?()do + if store() == :ets && enabled?() do case :ets.lookup(table_name(), symbol) do [{_symbol, address} | _] -> Hash.Address.cast(address) _ -> nil From 6db7dbfefe7e0f83db15b9f422bd417f8fe37223 Mon Sep 17 00:00:00 2001 From: saneery Date: Mon, 29 Apr 2019 16:58:41 +0300 Subject: [PATCH 3/5] update exchange rates test in block_scout_web app --- .../test/block_scout_web/channels/exchange_rate_channel_test.exs | 1 + .../controllers/api/rpc/stats_controller_test.exs | 1 + 2 files changed, 2 insertions(+) diff --git a/apps/block_scout_web/test/block_scout_web/channels/exchange_rate_channel_test.exs b/apps/block_scout_web/test/block_scout_web/channels/exchange_rate_channel_test.exs index acdf441750..f41a764c6e 100644 --- a/apps/block_scout_web/test/block_scout_web/channels/exchange_rate_channel_test.exs +++ b/apps/block_scout_web/test/block_scout_web/channels/exchange_rate_channel_test.exs @@ -16,6 +16,7 @@ defmodule BlockScoutWeb.ExchangeRateChannelTest do configuration = Application.get_env(:explorer, Explorer.ExchangeRates) Application.put_env(:explorer, Explorer.ExchangeRates, source: TestSource) Application.put_env(:explorer, Explorer.ExchangeRates, table_name: :rates) + Application.put_env(:explorer, Explorer.ExchangeRates, enabled: true) ExchangeRates.init([]) 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 7c47ed622e..9a7c26e9ee 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 @@ -107,6 +107,7 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do configuration = Application.get_env(:explorer, Explorer.ExchangeRates) Application.put_env(:explorer, Explorer.ExchangeRates, source: TestSource) Application.put_env(:explorer, Explorer.ExchangeRates, table_name: :rates) + Application.put_env(:explorer, Explorer.ExchangeRates, enabled: true) ExchangeRates.init([]) From 1c39e0a2ed1a34138585080d03082e571c0d6719 Mon Sep 17 00:00:00 2001 From: saneery Date: Tue, 30 Apr 2019 11:11:41 +0300 Subject: [PATCH 4/5] fix typo --- .../test/explorer/exchange_rates/exchange_rates_test.exs | 2 +- apps/explorer/test/explorer/known_tokens/known_tokens_test.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs b/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs index ff1510937a..d1bed67de1 100644 --- a/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs +++ b/apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs @@ -137,7 +137,7 @@ defmodule Explorer.ExchangeRatesTest do assert nil == ExchangeRates.lookup("nope") end - test "lookup when desibled" do + test "lookup when disabled" do Application.put_env(:explorer, Explorer.ExchangeRates, enabled: false) assert nil == ExchangeRates.lookup("z") diff --git a/apps/explorer/test/explorer/known_tokens/known_tokens_test.exs b/apps/explorer/test/explorer/known_tokens/known_tokens_test.exs index a33d3c85e7..9076cc1d13 100644 --- a/apps/explorer/test/explorer/known_tokens/known_tokens_test.exs +++ b/apps/explorer/test/explorer/known_tokens/known_tokens_test.exs @@ -130,7 +130,7 @@ defmodule Explorer.KnownTokensTest do assert nil == KnownTokens.lookup("nope") end - test "lookup when desibled" do + test "lookup when disabled" do Application.put_env(:explorer, Explorer.KnownTokens, enabled: false) assert nil == KnownTokens.lookup("z") From 7e0f3cba33384414f4cdfb78b249b0a11de260f1 Mon Sep 17 00:00:00 2001 From: saneery Date: Wed, 1 May 2019 13:16:53 +0300 Subject: [PATCH 5/5] fetch env without raise --- apps/explorer/lib/explorer/application.ex | 4 +--- apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex | 4 +--- apps/explorer/lib/explorer/known_tokens/known_tokens.ex | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/apps/explorer/lib/explorer/application.ex b/apps/explorer/lib/explorer/application.ex index 080b880211..47b30cfa2b 100644 --- a/apps/explorer/lib/explorer/application.ex +++ b/apps/explorer/lib/explorer/application.ex @@ -56,9 +56,7 @@ defmodule Explorer.Application do end defp should_start?(process) do - :explorer - |> Application.fetch_env!(process) - |> Keyword.fetch!(:enabled) + Application.get_env(:explorer, process, [])[:enabled] == true end defp configure(process) do diff --git a/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex b/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex index b4dc0229f9..f0ed6f0f54 100644 --- a/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex +++ b/apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex @@ -135,8 +135,6 @@ defmodule Explorer.ExchangeRates do end defp enabled? do - :explorer - |> Application.fetch_env!(__MODULE__) - |> Keyword.fetch!(:enabled) + Application.get_env(:explorer, __MODULE__, [])[:enabled] == true end end diff --git a/apps/explorer/lib/explorer/known_tokens/known_tokens.ex b/apps/explorer/lib/explorer/known_tokens/known_tokens.ex index caca671540..aff9dea74b 100644 --- a/apps/explorer/lib/explorer/known_tokens/known_tokens.ex +++ b/apps/explorer/lib/explorer/known_tokens/known_tokens.ex @@ -134,8 +134,6 @@ defmodule Explorer.KnownTokens do end defp enabled? do - :explorer - |> Application.fetch_env!(__MODULE__) - |> Keyword.fetch!(:enabled) + Application.get_env(:explorer, __MODULE__, [])[:enabled] == true end end