From bd2d21b2cb01be5560762188a4bc9ac37d89d6e2 Mon Sep 17 00:00:00 2001 From: Qwerty5Uiop Date: Mon, 27 Nov 2023 18:19:28 +0600 Subject: [PATCH] Fix average block time --- CHANGELOG.md | 2 + .../explorer/counters/average_block_time.ex | 4 +- .../counters/average_block_time_test.exs | 50 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf4c4c7ee..1e92e99ffb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Fixes +- [#8891](https://github.com/blockscout/blockscout/pull/8891) - Fix average block time + ### Chore ## 5.3.2-beta diff --git a/apps/explorer/lib/explorer/counters/average_block_time.ex b/apps/explorer/lib/explorer/counters/average_block_time.ex index 9075415854..9c7338dc9e 100644 --- a/apps/explorer/lib/explorer/counters/average_block_time.ex +++ b/apps/explorer/lib/explorer/counters/average_block_time.ex @@ -84,7 +84,7 @@ defmodule Explorer.Counters.AverageBlockTime do timestamps = timestamps_row - |> Enum.sort_by(fn {_, timestamp} -> timestamp end, &>=/2) + |> Enum.sort_by(fn {_, timestamp} -> timestamp end, &Timex.after?/2) |> Enum.map(fn {number, timestamp} -> {number, DateTime.to_unix(timestamp, :millisecond)} end) @@ -125,7 +125,7 @@ defmodule Explorer.Counters.AverageBlockTime do defp compose_durations(durations, block_number, last_block_number, last_timestamp, timestamp) do block_numbers_range = last_block_number - block_number - if block_numbers_range == 0 do + if block_numbers_range <= 0 do {durations, block_number, timestamp} else duration = (last_timestamp - timestamp) / block_numbers_range 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 d9e0cfb35c..3472276d19 100644 --- a/apps/explorer/test/explorer/counters/average_block_time_test.exs +++ b/apps/explorer/test/explorer/counters/average_block_time_test.exs @@ -129,5 +129,55 @@ defmodule Explorer.Counters.AverageBlockTimeTest do assert AverageBlockTime.average_block_time() == Timex.Duration.parse!("PT3S") end + + test "timestamps are compared correctly" do + block_number = 99_999_999 + + first_timestamp = ~U[2023-08-23 19:04:59.000000Z] + pseudo_after_timestamp = ~U[2022-08-23 19:05:59.000000Z] + + insert(:block, number: block_number, consensus: true, timestamp: pseudo_after_timestamp) + insert(:block, number: block_number + 1, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 3)) + insert(:block, number: block_number + 2, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 6)) + + Enum.each(1..100, fn i -> + insert(:block, + number: block_number + i + 2, + consensus: true, + timestamp: Timex.shift(first_timestamp, seconds: -(101 - i) - 9) + ) + end) + + AverageBlockTime.refresh() + + %{timestamps: timestamps} = :sys.get_state(AverageBlockTime) + + assert Enum.sort_by(timestamps, fn {_bn, ts} -> ts end, &>=/2) == timestamps + end + + test "average time are calculated correctly for blocks that are not in chronological order" do + block_number = 99_999_999 + + first_timestamp = Timex.now() + + insert(:block, number: block_number, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 3)) + insert(:block, number: block_number + 1, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 6)) + insert(:block, number: block_number + 2, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: 9)) + insert(:block, number: block_number + 3, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: -69)) + insert(:block, number: block_number + 4, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: -66)) + insert(:block, number: block_number + 5, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: -63)) + + Enum.each(1..100, fn i -> + insert(:block, + number: block_number + i + 5, + consensus: true, + timestamp: Timex.shift(first_timestamp, seconds: -(101 - i) - 9) + ) + end) + + AverageBlockTime.refresh() + + assert Timex.Duration.to_milliseconds(AverageBlockTime.average_block_time()) == 3000 + end end end