Fix divide-by-zero error in indexed_ratio

Dividing by `max_block_number` assumes it is 1-based, but it is 0-based,
so add 1.
pull/985/head
Luke Imhoff 6 years ago
parent e690c3219b
commit 21ace095ba
  1. 24
      apps/block_scout_web/test/block_scout_web/features/viewing_app_test.exs
  2. 4
      apps/explorer/lib/explorer/chain.ex
  3. 4
      apps/explorer/test/explorer/chain_test.exs

@ -7,50 +7,58 @@ defmodule BlockScoutWeb.ViewingAppTest do
describe "loading bar when indexing" do
test "shows blocks indexed percentage", %{session: session} do
for index <- 6..10 do
for index <- 5..9 do
insert(:block, number: index)
end
assert Explorer.Chain.indexed_ratio() == 0.5
session
|> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("50% Blocks Indexed"))
end
test "shows tokens loading", %{session: session} do
for index <- 1..10 do
for index <- 0..9 do
insert(:block, number: index)
end
assert Explorer.Chain.indexed_ratio() == 1.0
session
|> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("Indexing Tokens"))
end
test "live updates blocks indexed percentage", %{session: session} do
for index <- 6..10 do
for index <- 5..9 do
insert(:block, number: index)
end
assert Explorer.Chain.indexed_ratio() == 0.5
session
|> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("50% Blocks Indexed"))
insert(:block, number: 5)
insert(:block, number: 4)
Notifier.handle_event({:chain_event, :blocks, :catchup, []})
assert_has(session, AppPage.indexed_status("60% Blocks Indexed"))
end
test "live updates when blocks are fully indexed", %{session: session} do
for index <- 2..10 do
for index <- 1..9 do
insert(:block, number: index)
end
assert Explorer.Chain.indexed_ratio() == 0.9
session
|> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("90% Blocks Indexed"))
insert(:block, number: 1)
insert(:block, number: 0)
Notifier.handle_event({:chain_event, :blocks, :catchup, []})
assert_has(session, AppPage.indexed_status("Indexing Tokens"))
@ -58,10 +66,12 @@ defmodule BlockScoutWeb.ViewingAppTest do
test "live removes message when chain is indexed", %{session: session} do
[block | _] =
for index <- 1..10 do
for index <- 0..9 do
insert(:block, number: index)
end
assert Explorer.Chain.indexed_ratio() == 1.0
session
|> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("Indexing Tokens"))

@ -842,7 +842,7 @@ defmodule Explorer.Chain do
@doc """
The percentage of indexed blocks on the chain.
iex> for index <- 6..10 do
iex> for index <- 5..9 do
...> insert(:block, number: index)
...> end
iex> Explorer.Chain.indexed_ratio()
@ -859,7 +859,7 @@ defmodule Explorer.Chain do
with {:ok, min_block_number} <- min_block_number(),
{:ok, max_block_number} <- max_block_number() do
indexed_blocks = max_block_number - min_block_number + 1
indexed_blocks / max_block_number
indexed_blocks / (max_block_number + 1)
else
{:error, _} -> 0
end

@ -820,7 +820,7 @@ defmodule Explorer.ChainTest do
describe "indexed_ratio/0" do
test "returns indexed ratio" do
for index <- 6..10 do
for index <- 5..9 do
insert(:block, number: index)
end
@ -832,7 +832,7 @@ defmodule Explorer.ChainTest do
end
test "returns 1.0 if fully indexed blocks" do
for index <- 1..10 do
for index <- 0..9 do
insert(:block, number: index)
end

Loading…
Cancel
Save