add healthy block period checking query

pull/2294/head
Ayrat Badykov 5 years ago
parent 23d6b2aa10
commit 39f19401c3
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 9
      apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex
  2. 3
      apps/explorer/config/config.exs
  3. 26
      apps/explorer/lib/explorer/chain.ex
  4. 18
      apps/explorer/test/explorer/chain_test.exs

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

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

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

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

Loading…
Cancel
Save