Fix average block time

pull/8891/head
Qwerty5Uiop 1 year ago
parent 1c1d143c29
commit bd2d21b2cb
  1. 2
      CHANGELOG.md
  2. 4
      apps/explorer/lib/explorer/counters/average_block_time.ex
  3. 50
      apps/explorer/test/explorer/counters/average_block_time_test.exs

@ -6,6 +6,8 @@
### Fixes ### Fixes
- [#8891](https://github.com/blockscout/blockscout/pull/8891) - Fix average block time
### Chore ### Chore
## 5.3.2-beta ## 5.3.2-beta

@ -84,7 +84,7 @@ defmodule Explorer.Counters.AverageBlockTime do
timestamps = timestamps =
timestamps_row 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} -> |> Enum.map(fn {number, timestamp} ->
{number, DateTime.to_unix(timestamp, :millisecond)} {number, DateTime.to_unix(timestamp, :millisecond)}
end) end)
@ -125,7 +125,7 @@ defmodule Explorer.Counters.AverageBlockTime do
defp compose_durations(durations, block_number, last_block_number, last_timestamp, timestamp) do defp compose_durations(durations, block_number, last_block_number, last_timestamp, timestamp) do
block_numbers_range = last_block_number - block_number block_numbers_range = last_block_number - block_number
if block_numbers_range == 0 do if block_numbers_range <= 0 do
{durations, block_number, timestamp} {durations, block_number, timestamp}
else else
duration = (last_timestamp - timestamp) / block_numbers_range duration = (last_timestamp - timestamp) / block_numbers_range

@ -129,5 +129,55 @@ defmodule Explorer.Counters.AverageBlockTimeTest do
assert AverageBlockTime.average_block_time() == Timex.Duration.parse!("PT3S") assert AverageBlockTime.average_block_time() == Timex.Duration.parse!("PT3S")
end 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
end end

Loading…
Cancel
Save