Merge pull request #1855 from poanetwork/exchange-rate-disable

Don't lookup in table when table isn't initiated
pull/1859/head
Victor Baranov 6 years ago committed by GitHub
commit 95f50cead4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      apps/block_scout_web/test/block_scout_web/channels/exchange_rate_channel_test.exs
  2. 1
      apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs
  3. 4
      apps/explorer/lib/explorer/application.ex
  4. 6
      apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex
  5. 12
      apps/explorer/lib/explorer/known_tokens/known_tokens.ex
  6. 7
      apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs
  7. 7
      apps/explorer/test/explorer/known_tokens/known_tokens_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([])

@ -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([])

@ -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

@ -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 && enabled?() do
case :ets.lookup(table_name(), symbol) do
[tuple | _] when is_tuple(tuple) -> Token.from_tuple(tuple)
_ -> nil
@ -133,4 +133,8 @@ defmodule Explorer.ExchangeRates do
defp store do
config(:store) || :ets
end
defp enabled? do
Application.get_env(:explorer, __MODULE__, [])[:enabled] == true
end
end

@ -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,8 @@ defmodule Explorer.KnownTokens do
defp store do
config(:store) || :ets
end
defp enabled? do
Application.get_env(:explorer, __MODULE__, [])[:enabled] == true
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, 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 disabled" do
Application.put_env(:explorer, Explorer.ExchangeRates, enabled: false)
assert nil == ExchangeRates.lookup("z")
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, 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 disabled" do
Application.put_env(:explorer, Explorer.KnownTokens, enabled: false)
assert nil == KnownTokens.lookup("z")
end
end

Loading…
Cancel
Save