Skip to content

Commit

Permalink
Merge pull request blockscout#1855 from poanetwork/exchange-rate-disable
Browse files Browse the repository at this point in the history
Don't lookup in table when table isn't initiated
  • Loading branch information
vbaranov authored May 1, 2019
2 parents 2e11ff5 + 7e0f3cb commit 95f50ce
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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([])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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([])

Expand Down
4 changes: 1 addition & 3 deletions apps/explorer/lib/explorer/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 10 additions & 2 deletions apps/explorer/lib/explorer/known_tokens/known_tokens.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,19 @@ 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 """
Returns a specific address from the known tokens by symbol
"""
@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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 95f50ce

Please sign in to comment.