From 39f19401c38e1eed23686ada77c7f14e9102143c Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 4 Jul 2019 12:11:52 +0300 Subject: [PATCH] add healthy block period checking query --- .../controllers/api/v1/health_controller.ex | 9 +++++++ apps/explorer/config/config.exs | 3 ++- apps/explorer/lib/explorer/chain.ex | 26 +++++++++++++++++++ apps/explorer/test/explorer/chain_test.exs | 18 +++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex new file mode 100644 index 0000000000..abacc3c67b --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex @@ -0,0 +1,9 @@ +defmodule BlockScoutWeb.API.V1.HealthController do + use BlockScoutWeb, :controller + + alias Explorer.Chain + + def last_block_status(conn, _) do + # Chain.last_block_status() + end +end diff --git a/apps/explorer/config/config.exs b/apps/explorer/config/config.exs index d834d23384..c63fe3e24c 100644 --- a/apps/explorer/config/config.exs +++ b/apps/explorer/config/config.exs @@ -14,7 +14,8 @@ config :explorer, System.get_env("ALLOWED_EVM_VERSIONS") || "homestead,tangerineWhistle,spuriousDragon,byzantium,constantinople,petersburg", include_uncles_in_average_block_time: - if(System.get_env("UNCLES_IN_AVERAGE_BLOCK_TIME") == "false", do: false, else: true) + if(System.get_env("UNCLES_IN_AVERAGE_BLOCK_TIME") == "false", do: false, else: true), + healthy_blocks_period: System.get_env("HEALTHY_BLOCKS_PERIOD") || :timer.minutes(5) config :explorer, Explorer.Counters.AverageBlockTime, enabled: true diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 62ff70afb8..d2d53046e2 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -1769,6 +1769,32 @@ defmodule Explorer.Chain do Repo.one!(query) end + @spec last_block_status() :: {:ok, non_neg_integer()} | {:error, non_neg_integer | nil} + def last_block_status do + query = + from(block in Block, + select: {block.number, block.timestamp}, + where: block.consensus == true, + order_by: [desc: block.number], + limit: 1 + ) + + case Repo.one(query) do + nil -> + {:error, :no_blocks} + + {_number, timestamp} -> + now = DateTime.utc_now() + last_block_period = DateTime.diff(now, timestamp, :millisecond) + + if last_block_period > Application.get_env(:explorer, :healthy_blocks_period) do + {:error, last_block_period} + else + {:ok, last_block_period} + end + end + end + @doc """ Calculates the ranges of missing consensus blocks in `range`. diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 91a4bff8db..2e28d9d269 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -50,6 +50,24 @@ defmodule Explorer.ChainTest do end end + describe "last_block_status/0" do + test "return no_blocks errors if db is empty" do + assert {:error, :no_blocks} = Chain.last_block_status() + end + + test "returns {:ok, last_block_period} if block is in healthy period" do + insert(:block, consensus: true) + + assert {:ok, _} = Chain.last_block_status() + end + + test "return {:ok, last_block_period} if block is not in healthy period" do + insert(:block, consensus: true, timestamp: Timex.shift(DateTime.utc_now(), hours: -50)) + + assert {:error, _} = Chain.last_block_status() + end + end + describe "address_to_logs/2" do test "fetches logs" do address = insert(:address)