diff --git a/apps/explorer/lib/explorer/chain_spec/genesis_data.ex b/apps/explorer/lib/explorer/chain_spec/genesis_data.ex index e34cbc0504..9760ad668f 100644 --- a/apps/explorer/lib/explorer/chain_spec/genesis_data.ex +++ b/apps/explorer/lib/explorer/chain_spec/genesis_data.ex @@ -8,6 +8,7 @@ defmodule Explorer.ChainSpec.GenesisData do require Logger alias Explorer.ChainSpec.Parity.Importer + alias HTTPoison.Response @interval :timer.minutes(2) @@ -56,15 +57,52 @@ defmodule Explorer.ChainSpec.GenesisData do # sobelow_skip ["Traversal"] def fetch_genesis_data do - if Application.get_env(:explorer, __MODULE__)[:chain_spec_path] do - Task.Supervisor.async_nolink(Explorer.GenesisDataTaskSupervisor, fn -> - chain_spec = Application.get_env(:explorer, __MODULE__)[:chain_spec_path] |> File.read!() |> Jason.decode!() - Importer.import_emission_rewards(chain_spec) + path = Application.get_env(:explorer, __MODULE__)[:chain_spec_path] - {:ok, _} = Importer.import_genesis_coin_balances(chain_spec) + if path do + Task.Supervisor.async_nolink(Explorer.GenesisDataTaskSupervisor, fn -> + case fetch_spec(path) do + {:ok, chain_spec} -> + Importer.import_emission_rewards(chain_spec) + {:ok, _} = Importer.import_genesis_coin_balances(chain_spec) + + {:error, reason} -> + Logger.warn(fn -> "Failed to fetch genesis data. #{inspect(reason)}" end) + end end) else Logger.warn(fn -> "Failed to fetch genesis data. Chain spec path is not set." end) end end + + defp fetch_spec(path) do + if valid_url?(path) do + fetch_from_url(path) + else + fetch_from_file(path) + end + end + + defp fetch_from_file(path) do + with {:ok, data} <- File.read(path), + {:ok, json} <- Jason.decode(data) do + {:ok, json} + end + end + + defp fetch_from_url(url) do + case HTTPoison.get(url) do + {:ok, %Response{body: body, status_code: 200}} -> + {:ok, Jason.decode!(body)} + + reason -> + {:error, reason} + end + end + + defp valid_url?(string) do + uri = URI.parse(string) + + uri.scheme != nil && uri.host =~ "." + end end diff --git a/docs/env-variables.md b/docs/env-variables.md index 9bb55b9660..1d0f81b973 100644 --- a/docs/env-variables.md +++ b/docs/env-variables.md @@ -53,7 +53,7 @@ $ export NETWORK=POA | `ADDRESS_WITH_BALANCES`
`_UPDATE_INTERVAL`| | Interval in seconds to restart the task, which calculates addresses with balances. | 30 * 60 | v1.3.9+ | | `LINK_TO_OTHER_EXPLORERS` | | true/false. If true, links to other explorers are added in the footer | (empty) | v1.3.0+ | | `COINMARKETCAP_PAGES` | | the number of pages on coinmarketcap to list in order to find token's price | 10 | v1.3.10+ | -| `CHAIN_SPEC_PATH` | | Chain specification path to import block emission reward ranges and genesis account balances from | (empty) | master | +| `CHAIN_SPEC_PATH` | | Chain specification path (absolute file system path or url) to import block emission reward ranges and genesis account balances from | (empty) | master | | `SUPPORTED_CHAINS` | | Array of supported chains that displays in the footer and in the chains dropdown. This var was introduced in this PR [#1900](https://github.com/poanetwork/blockscout/pull/1900) and looks like an array of JSON objects. | (empty) | v2.0.0+ | | `BLOCK_COUNT_CACHE_PERIOD ` | | time to live of cache in seconds. This var was introduced in [#1876](https://github.com/poanetwork/blockscout/pull/1876) | 600 | v2.0.0+ | | `ALLOWED_EVM_VERSIONS ` | | the comma-separated list of allowed EVM versions for contracts verification. This var was introduced in [#1964](https://github.com/poanetwork/blockscout/pull/1964) | "homestead, tangerineWhistle, spuriousDragon, byzantium, constantinople, petersburg" | v2.0.0+ |