From 9c33fe7179e923e39a4989460c0bb4184f9c599d Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 28 Jun 2019 14:26:41 +0300 Subject: [PATCH] allow to exclude uncles from average block time calculation --- apps/explorer/config/config.exs | 4 ++- .../explorer/counters/average_block_time.ex | 11 ++++++- apps/explorer/mix.exs | 1 - .../counters/average_block_time_test.exs | 32 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/apps/explorer/config/config.exs b/apps/explorer/config/config.exs index 9a601e6c68..d834d23384 100644 --- a/apps/explorer/config/config.exs +++ b/apps/explorer/config/config.exs @@ -12,7 +12,9 @@ config :explorer, token_functions_reader_max_retries: 3, allowed_evm_versions: System.get_env("ALLOWED_EVM_VERSIONS") || - "homestead,tangerineWhistle,spuriousDragon,byzantium,constantinople,petersburg" + "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) config :explorer, Explorer.Counters.AverageBlockTime, enabled: true diff --git a/apps/explorer/lib/explorer/counters/average_block_time.ex b/apps/explorer/lib/explorer/counters/average_block_time.ex index 450d6b8fb0..233c15f8b3 100644 --- a/apps/explorer/lib/explorer/counters/average_block_time.ex +++ b/apps/explorer/lib/explorer/counters/average_block_time.ex @@ -70,8 +70,17 @@ defmodule Explorer.Counters.AverageBlockTime do select: {block.number, block.timestamp} ) + query = + if Application.get_env(:explorer, :include_uncles_in_average_block_time) do + timestamps_query + else + from(block in timestamps_query, + where: block.consensus == true + ) + end + timestamps = - timestamps_query + query |> Repo.all() |> Enum.sort_by(fn {_, timestamp} -> timestamp end, &>=/2) |> Enum.map(fn {number, timestamp} -> diff --git a/apps/explorer/mix.exs b/apps/explorer/mix.exs index 1e8c38fbe5..79574b9354 100644 --- a/apps/explorer/mix.exs +++ b/apps/explorer/mix.exs @@ -92,7 +92,6 @@ defmodule Explorer.Mixfile do {:math, "~> 0.3.0"}, {:mock, "~> 0.3.0", only: [:test], runtime: false}, {:mox, "~> 0.4", only: [:test]}, - {:nimble_csv, "~> 0.6.0"}, {:poison, "~> 3.1"}, {:nimble_csv, "~> 0.6.0"}, {:postgrex, ">= 0.0.0"}, diff --git a/apps/explorer/test/explorer/counters/average_block_time_test.exs b/apps/explorer/test/explorer/counters/average_block_time_test.exs index 0f7c328bb3..a827000b31 100644 --- a/apps/explorer/test/explorer/counters/average_block_time_test.exs +++ b/apps/explorer/test/explorer/counters/average_block_time_test.exs @@ -11,6 +11,8 @@ defmodule Explorer.Counters.AverageBlockTimeTest do start_supervised!(AverageBlockTime) Application.put_env(:explorer, AverageBlockTime, enabled: true) + Application.put_env(:explorer, :include_uncles_in_average_block_time, true) + on_exit(fn -> Application.put_env(:explorer, AverageBlockTime, enabled: false) end) @@ -43,6 +45,36 @@ defmodule Explorer.Counters.AverageBlockTimeTest do assert AverageBlockTime.average_block_time() == Timex.Duration.parse!("PT3S") end + test "excludes uncles if include_uncles_in_average_block_time is set to false" do + block_number = 99_999_999 + Application.put_env(:explorer, :include_uncles_in_average_block_time, false) + + first_timestamp = Timex.now() + + insert(:block, number: block_number, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 3)) + insert(:block, number: block_number, consensus: false, timestamp: Timex.shift(first_timestamp, seconds: 4)) + insert(:block, number: block_number + 1, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 5)) + + AverageBlockTime.refresh() + + assert AverageBlockTime.average_block_time() == Timex.Duration.parse!("PT2S") + end + + test "excludes uncles if include_uncles_in_average_block_time is set to true" do + block_number = 99_999_999 + Application.put_env(:explorer, :include_uncles_in_average_block_time, true) + + first_timestamp = Timex.now() + + insert(:block, number: block_number, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 3)) + insert(:block, number: block_number, consensus: false, timestamp: Timex.shift(first_timestamp, seconds: 4)) + insert(:block, number: block_number + 1, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 5)) + + AverageBlockTime.refresh() + + assert AverageBlockTime.average_block_time() == Timex.Duration.parse!("PT1S") + end + test "when there are no uncles sorts by block number" do block_number = 99_999_999