From 1bd25dcc45d64ba25cdf222ff54086eac4f7d1b0 Mon Sep 17 00:00:00 2001 From: saneery Date: Mon, 29 Apr 2019 16:18:54 +0300 Subject: [PATCH] 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