pull/1714/head
Ayrat Badykov 6 years ago
parent e0ac68f22e
commit 4444f15e62
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 11
      apps/explorer/lib/explorer/counters/average_block_time.ex
  2. 38
      apps/explorer/test/explorer/counters/average_block_time_test.exs
  3. 14
      apps/indexer/test/indexer/coin_balance/on_demand_fetcher_test.exs

@ -34,6 +34,10 @@ defmodule Explorer.Counters.AverageBlockTime do
end
end
def refresh do
GenServer.call(__MODULE__, :refresh_timestamps)
end
## Server
@impl true
def init(_) do
@ -45,6 +49,11 @@ defmodule Explorer.Counters.AverageBlockTime do
@impl true
def handle_call(:average_block_time, _from, %{average: average} = state), do: {:reply, average, state}
@impl true
def handle_call(:refresh_timestamps, _, _) do
{:reply, :ok, refresh_timestamps()}
end
@impl true
def handle_info(:refresh_timestamps, _) do
Process.send_after(self(), :refresh_timestamps, @refresh_period)
@ -56,7 +65,7 @@ defmodule Explorer.Counters.AverageBlockTime do
timestamps_query =
from(block in Block,
limit: 100,
offset: 1,
offset: 0,
order_by: [desc: block.number],
select: {block.number, block.timestamp}
)

@ -5,8 +5,6 @@ defmodule Explorer.Counters.AverageBlockTimeTest do
alias Explorer.Counters.AverageBlockTime
defp block(number, last, duration), do: %{number: number, timestamp: Timex.shift(last, seconds: duration)}
setup do
start_supervised!(AverageBlockTime)
Application.put_env(:explorer, AverageBlockTime, enabled: true)
@ -26,41 +24,5 @@ defmodule Explorer.Counters.AverageBlockTimeTest do
test "without blocks duration is 0" do
assert AverageBlockTime.average_block_time() == Timex.Duration.parse!("PT0S")
end
test "with only one block, the duration is 0" do
now = Timex.now()
block = block(0, now, 0)
assert AverageBlockTime.average_block_time(block) == Timex.Duration.parse!("PT0S")
end
test "once there are two blocks, the duration is the average distance between them all" do
now = Timex.now()
block0 = block(0, now, 0)
block1 = block(1, now, 2)
block2 = block(2, now, 6)
AverageBlockTime.average_block_time(block0)
assert AverageBlockTime.average_block_time(block1) == Timex.Duration.parse!("PT2S")
assert AverageBlockTime.average_block_time(block2) == Timex.Duration.parse!("PT3S")
end
test "only the last 100 blocks are considered" do
now = Timex.now()
block0 = block(0, now, 0)
block1 = block(1, now, 2000)
AverageBlockTime.average_block_time(block0)
AverageBlockTime.average_block_time(block1)
for i <- 1..100 do
block = block(i + 1, now, 2000 + i)
AverageBlockTime.average_block_time(block)
end
assert AverageBlockTime.average_block_time() == Timex.Duration.parse!("PT1S")
end
end
end

@ -41,10 +41,9 @@ defmodule Indexer.CoinBalance.OnDemandFetcherTest do
# we space these very far apart so that we know it will consider the 0th block stale (it calculates how far
# back we'd need to go to get 24 hours in the past)
block_0 = insert(:block, number: 0, timestamp: Timex.shift(now, hours: -50))
AverageBlockTime.average_block_time(block_0)
block_1 = insert(:block, number: 1, timestamp: now)
AverageBlockTime.average_block_time(block_1)
insert(:block, number: 0, timestamp: Timex.shift(now, hours: -50))
insert(:block, number: 1, timestamp: now)
AverageBlockTime.refresh()
stale_address = insert(:address, fetched_coin_balance: 1, fetched_coin_balance_block_number: 0)
current_address = insert(:address, fetched_coin_balance: 1, fetched_coin_balance_block_number: 1)
@ -89,10 +88,9 @@ defmodule Indexer.CoinBalance.OnDemandFetcherTest do
# we space these very far apart so that we know it will consider the 0th block stale (it calculates how far
# back we'd need to go to get 24 hours in the past)
block_0 = insert(:block, number: 0, timestamp: Timex.shift(now, hours: -50))
AverageBlockTime.average_block_time(block_0)
block_1 = insert(:block, number: 1, timestamp: now)
AverageBlockTime.average_block_time(block_1)
insert(:block, number: 0, timestamp: Timex.shift(now, hours: -50))
insert(:block, number: 1, timestamp: now)
AverageBlockTime.refresh()
:ok
end

Loading…
Cancel
Save