don't lookup at table when table isn't inited

pull/1855/head
saneery 6 years ago
parent df2228e7ae
commit 1bd25dcc45
  1. 8
      apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex
  2. 14
      apps/explorer/lib/explorer/known_tokens/known_tokens.ex
  3. 7
      apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs
  4. 7
      apps/explorer/test/explorer/known_tokens/known_tokens_test.exs

@ -90,7 +90,7 @@ defmodule Explorer.ExchangeRates do
""" """
@spec lookup(String.t()) :: Token.t() | nil @spec lookup(String.t()) :: Token.t() | nil
def lookup(symbol) do def lookup(symbol) do
if store() == :ets do if store() == :ets and enabled?() do
case :ets.lookup(table_name(), symbol) do case :ets.lookup(table_name(), symbol) do
[tuple | _] when is_tuple(tuple) -> Token.from_tuple(tuple) [tuple | _] when is_tuple(tuple) -> Token.from_tuple(tuple)
_ -> nil _ -> nil
@ -133,4 +133,10 @@ defmodule Explorer.ExchangeRates do
defp store do defp store do
config(:store) || :ets config(:store) || :ets
end end
defp enabled? do
:explorer
|> Application.fetch_env!(__MODULE__)
|> Keyword.fetch!(:enabled)
end
end end

@ -81,7 +81,11 @@ defmodule Explorer.KnownTokens do
""" """
@spec list :: [{String.t(), Hash.Address.t()}] @spec list :: [{String.t(), Hash.Address.t()}]
def list do def list do
list_from_store(store()) if enabled?() do
list_from_store(store())
else
[]
end
end end
@doc """ @doc """
@ -89,7 +93,7 @@ defmodule Explorer.KnownTokens do
""" """
@spec lookup(String.t()) :: {:ok, Hash.Address.t()} | :error | nil @spec lookup(String.t()) :: {:ok, Hash.Address.t()} | :error | nil
def lookup(symbol) do def lookup(symbol) do
if store() == :ets do if store() == :ets && enabled?()do
case :ets.lookup(table_name(), symbol) do case :ets.lookup(table_name(), symbol) do
[{_symbol, address} | _] -> Hash.Address.cast(address) [{_symbol, address} | _] -> Hash.Address.cast(address)
_ -> nil _ -> nil
@ -128,4 +132,10 @@ defmodule Explorer.KnownTokens do
defp store do defp store do
config(:store) || :ets config(:store) || :ets
end end
defp enabled? do
:explorer
|> Application.fetch_env!(__MODULE__)
|> Keyword.fetch!(:enabled)
end
end end

@ -19,6 +19,7 @@ defmodule Explorer.ExchangeRatesTest do
Application.put_env(:explorer, Explorer.ExchangeRates.Source, source: TestSource) Application.put_env(:explorer, Explorer.ExchangeRates.Source, source: TestSource)
Application.put_env(:explorer, Explorer.ExchangeRates, table_name: :rates) Application.put_env(:explorer, Explorer.ExchangeRates, table_name: :rates)
Application.put_env(:explorer, Explorer.ExchangeRates, enabled: true)
on_exit(fn -> on_exit(fn ->
Application.put_env(:explorer, Explorer.ExchangeRates.Source, source_configuration) Application.put_env(:explorer, Explorer.ExchangeRates.Source, source_configuration)
@ -135,4 +136,10 @@ defmodule Explorer.ExchangeRatesTest do
assert z == ExchangeRates.lookup("z") assert z == ExchangeRates.lookup("z")
assert nil == ExchangeRates.lookup("nope") assert nil == ExchangeRates.lookup("nope")
end end
test "lookup when desibled" do
Application.put_env(:explorer, Explorer.ExchangeRates, enabled: false)
assert nil == ExchangeRates.lookup("z")
end
end end

@ -21,6 +21,7 @@ defmodule Explorer.KnownTokensTest do
Application.put_env(:explorer, Explorer.KnownTokens.Source, source: TestSource) 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, table_name: :known_tokens)
Application.put_env(:explorer, Explorer.KnownTokens, enabled: true)
on_exit(fn -> on_exit(fn ->
Application.put_env(:explorer, Explorer.KnownTokens.Source, source_configuration) 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 Hash.Address.cast("0x0000000000000000000000000000000000000001") == KnownTokens.lookup("TEST1")
assert nil == KnownTokens.lookup("nope") assert nil == KnownTokens.lookup("nope")
end end
test "lookup when desibled" do
Application.put_env(:explorer, Explorer.KnownTokens, enabled: false)
assert nil == KnownTokens.lookup("z")
end
end end

Loading…
Cancel
Save