From 21ace095babefc1789248b99422e353d0e85625c Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Wed, 24 Oct 2018 15:05:29 -0500 Subject: [PATCH] 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. --- .../features/viewing_app_test.exs | 24 +++++++++++++------ apps/explorer/lib/explorer/chain.ex | 4 ++-- apps/explorer/test/explorer/chain_test.exs | 4 ++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/features/viewing_app_test.exs b/apps/block_scout_web/test/block_scout_web/features/viewing_app_test.exs index 54311ecead..960de0049b 100644 --- a/apps/block_scout_web/test/block_scout_web/features/viewing_app_test.exs +++ b/apps/block_scout_web/test/block_scout_web/features/viewing_app_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")) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index cb387c5e23..9b7a7f241d 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -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 diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 199175defa..2f4e7c4854 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -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