Skip to content

Commit

Permalink
feat: store average block time in a genserver
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel authored and KronicDeth committed Jan 3, 2019
1 parent e88fb5c commit b02383a
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule BlockScoutWeb.ChainController do
alias BlockScoutWeb.ChainView
alias Explorer.{Chain, PagingOptions, Repo}
alias Explorer.Chain.{Address, Block, Transaction}
alias Explorer.Counters.AverageBlockTime
alias Explorer.ExchangeRates.Token
alias Explorer.Market
alias Phoenix.View
Expand All @@ -17,7 +18,7 @@ defmodule BlockScoutWeb.ChainController do
conn,
"show.html",
address_count: Chain.count_addresses_with_balance_from_cache(),
average_block_time: Chain.average_block_time(),
average_block_time: AverageBlockTime.average_block_time(),
exchange_rate: exchange_rate,
chart_data_path: market_history_chart_path(conn, :show),
transaction_estimated_count: transaction_estimated_count,
Expand Down
3 changes: 2 additions & 1 deletion apps/block_scout_web/lib/block_scout_web/notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule BlockScoutWeb.Notifier do
alias BlockScoutWeb.Endpoint
alias Explorer.{Chain, Market, Repo}
alias Explorer.Chain.{Address, InternalTransaction, Transaction}
alias Explorer.Counters.AverageBlockTime
alias Explorer.ExchangeRates.Token

def handle_event({:chain_event, :addresses, :realtime, addresses}) do
Expand Down Expand Up @@ -109,7 +110,7 @@ defmodule BlockScoutWeb.Notifier do

defp broadcast_block(block) do
preloaded_block = Repo.preload(block, [[miner: :names], :transactions, :rewards])
average_block_time = Chain.average_block_time()
average_block_time = AverageBlockTime.average_block_time(preloaded_block)

Endpoint.broadcast("blocks:new_block", "new_block", %{
block: preloaded_block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@
</div>
</div>
<div class="dashboard-banner-network-stats">
<div class="dashboard-banner-network-stats-item">
<span class="dashboard-banner-network-stats-label">
<%= gettext "Average block time" %>
</span>
<span class="dashboard-banner-network-stats-value" data-selector="average-block-time">
<%= Timex.format_duration(@average_block_time, :humanized) %>
</span>
</div>
<%= case @average_block_time do %>
<% {:error, :disabled} -> %>
<%= nil %>
<% average_block_time -> %>
<div class="dashboard-banner-network-stats-item">
<span class="dashboard-banner-network-stats-label">
<%= gettext "Average block time" %>
</span>
<span class="dashboard-banner-network-stats-value" data-selector="average-block-time">
<%= Timex.format_duration(average_block_time, :humanized) %>
</span>
</div>
<% end %>
<div class="dashboard-banner-network-stats-item">
<span class="dashboard-banner-network-stats-label">
<%= gettext "Total transactions" %>
Expand Down
24 changes: 12 additions & 12 deletions apps/block_scout_web/priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ msgid "All"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:37
#: lib/block_scout_web/templates/chain/show.html.eex:41
msgid "Average block time"
msgstr ""

Expand Down Expand Up @@ -175,7 +175,7 @@ msgid "BlockScout provides analytics data, API, and Smart Contract tools for the
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:67
#: lib/block_scout_web/templates/chain/show.html.eex:72
#: lib/block_scout_web/templates/layout/_topnav.html.eex:16
#: lib/block_scout_web/templates/layout/_topnav.html.eex:20
msgid "Blocks"
Expand Down Expand Up @@ -572,7 +572,7 @@ msgid "More internal transactions have come in"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:91
#: lib/block_scout_web/templates/chain/show.html.eex:96
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:14
#: lib/block_scout_web/templates/transaction/index.html.eex:14
msgid "More transactions have come in"
Expand Down Expand Up @@ -949,7 +949,7 @@ msgid "Total Supply"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:45
#: lib/block_scout_web/templates/chain/show.html.eex:50
msgid "Total transactions"
msgstr ""

Expand Down Expand Up @@ -983,7 +983,7 @@ msgstr ""
#: lib/block_scout_web/templates/block_transaction/index.html.eex:23
#: lib/block_scout_web/templates/block_transaction/index.html.eex:26
#: lib/block_scout_web/templates/block_transaction/index.html.eex:35
#: lib/block_scout_web/templates/chain/show.html.eex:88
#: lib/block_scout_web/templates/chain/show.html.eex:93
#: lib/block_scout_web/templates/layout/_topnav.html.eex:35
#: lib/block_scout_web/views/address_view.ex:253
msgid "Transactions"
Expand Down Expand Up @@ -1058,12 +1058,12 @@ msgid "Verify & publish"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:66
#: lib/block_scout_web/templates/chain/show.html.eex:71
msgid "View All Blocks →"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:87
#: lib/block_scout_web/templates/chain/show.html.eex:92
msgid "View All Transactions →"
msgstr ""

Expand Down Expand Up @@ -1103,7 +1103,7 @@ msgid "WEI"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:53
#: lib/block_scout_web/templates/chain/show.html.eex:58
msgid "Wallet addresses"
msgstr ""

Expand Down Expand Up @@ -1187,8 +1187,8 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:19
#: lib/block_scout_web/templates/address_validation/index.html.eex:63
#: lib/block_scout_web/templates/address_validation/index.html.eex:82
#: lib/block_scout_web/templates/chain/show.html.eex:79
#: lib/block_scout_web/templates/chain/show.html.eex:105
#: lib/block_scout_web/templates/chain/show.html.eex:84
#: lib/block_scout_web/templates/chain/show.html.eex:110
#: lib/block_scout_web/templates/tokens/read_contract/index.html.eex:24
msgid "Loading..."
msgstr ""
Expand Down Expand Up @@ -1399,7 +1399,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:26
#: lib/block_scout_web/templates/address_transaction/index.html.eex:55
#: lib/block_scout_web/templates/address_validation/index.html.eex:70
#: lib/block_scout_web/templates/chain/show.html.eex:71
#: lib/block_scout_web/templates/chain/show.html.eex:76
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:23
#: lib/block_scout_web/templates/tokens/holder/index.html.eex:22
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:20
Expand Down Expand Up @@ -1522,6 +1522,6 @@ msgid "Emission Contract"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:97
#: lib/block_scout_web/templates/chain/show.html.eex:102
msgid "Something went wrong, click to retry."
msgstr ""
24 changes: 12 additions & 12 deletions apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ msgid "All"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:37
#: lib/block_scout_web/templates/chain/show.html.eex:41
msgid "Average block time"
msgstr ""

Expand Down Expand Up @@ -175,7 +175,7 @@ msgid "BlockScout provides analytics data, API, and Smart Contract tools for the
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:67
#: lib/block_scout_web/templates/chain/show.html.eex:72
#: lib/block_scout_web/templates/layout/_topnav.html.eex:16
#: lib/block_scout_web/templates/layout/_topnav.html.eex:20
msgid "Blocks"
Expand Down Expand Up @@ -572,7 +572,7 @@ msgid "More internal transactions have come in"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:91
#: lib/block_scout_web/templates/chain/show.html.eex:96
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:14
#: lib/block_scout_web/templates/transaction/index.html.eex:14
msgid "More transactions have come in"
Expand Down Expand Up @@ -949,7 +949,7 @@ msgid "Total Supply"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:45
#: lib/block_scout_web/templates/chain/show.html.eex:50
msgid "Total transactions"
msgstr ""

Expand Down Expand Up @@ -983,7 +983,7 @@ msgstr ""
#: lib/block_scout_web/templates/block_transaction/index.html.eex:23
#: lib/block_scout_web/templates/block_transaction/index.html.eex:26
#: lib/block_scout_web/templates/block_transaction/index.html.eex:35
#: lib/block_scout_web/templates/chain/show.html.eex:88
#: lib/block_scout_web/templates/chain/show.html.eex:93
#: lib/block_scout_web/templates/layout/_topnav.html.eex:35
#: lib/block_scout_web/views/address_view.ex:253
msgid "Transactions"
Expand Down Expand Up @@ -1058,12 +1058,12 @@ msgid "Verify & publish"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:66
#: lib/block_scout_web/templates/chain/show.html.eex:71
msgid "View All Blocks →"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:87
#: lib/block_scout_web/templates/chain/show.html.eex:92
msgid "View All Transactions →"
msgstr ""

Expand Down Expand Up @@ -1103,7 +1103,7 @@ msgid "WEI"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:53
#: lib/block_scout_web/templates/chain/show.html.eex:58
msgid "Wallet addresses"
msgstr ""

Expand Down Expand Up @@ -1187,8 +1187,8 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:19
#: lib/block_scout_web/templates/address_validation/index.html.eex:63
#: lib/block_scout_web/templates/address_validation/index.html.eex:82
#: lib/block_scout_web/templates/chain/show.html.eex:79
#: lib/block_scout_web/templates/chain/show.html.eex:105
#: lib/block_scout_web/templates/chain/show.html.eex:84
#: lib/block_scout_web/templates/chain/show.html.eex:110
#: lib/block_scout_web/templates/tokens/read_contract/index.html.eex:24
msgid "Loading..."
msgstr ""
Expand Down Expand Up @@ -1399,7 +1399,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:26
#: lib/block_scout_web/templates/address_transaction/index.html.eex:55
#: lib/block_scout_web/templates/address_validation/index.html.eex:70
#: lib/block_scout_web/templates/chain/show.html.eex:71
#: lib/block_scout_web/templates/chain/show.html.eex:76
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:23
#: lib/block_scout_web/templates/tokens/holder/index.html.eex:22
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:20
Expand Down Expand Up @@ -1522,6 +1522,6 @@ msgid "Emission Contract"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:97
#: lib/block_scout_web/templates/chain/show.html.eex:102
msgid "Something went wrong, click to retry."
msgstr ""
2 changes: 2 additions & 0 deletions apps/explorer/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ config :explorer,
coin: System.get_env("COIN") || "POA",
token_functions_reader_max_retries: 3

config :explorer, Explorer.Counters.AverageBlockTime, enabled: true

config :explorer, Explorer.Counters.AddressesWithBalanceCounter, enabled: true, enable_consolidation: true

config :explorer, Explorer.ExchangeRates, enabled: true, store: :ets
Expand Down
2 changes: 2 additions & 0 deletions apps/explorer/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ config :explorer, Explorer.ExchangeRates, enabled: false, store: :ets

config :explorer, Explorer.KnownTokens, enabled: false, store: :ets

config :explorer, Explorer.Counters.AverageBlockTime, enabled: false

config :explorer, Explorer.Counters.AddressesWithBalanceCounter, enabled: false, enable_consolidation: false

config :explorer, Explorer.Market.History.Cataloger, enabled: false
Expand Down
1 change: 1 addition & 0 deletions apps/explorer/lib/explorer/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule Explorer.Application do
configure(Explorer.KnownTokens),
configure(Explorer.Market.History.Cataloger),
configure(Explorer.Counters.AddressesWithBalanceCounter),
configure(Explorer.Counters.AverageBlockTime),
configure(Explorer.Validator.MetadataProcessor)
]
|> List.flatten()
Expand Down
26 changes: 0 additions & 26 deletions apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ defmodule Explorer.Chain do
alias Explorer.{PagingOptions, Repo}

alias Dataloader.Ecto, as: DataloaderEcto
alias Timex.Duration

@default_paging_options %PagingOptions{page_size: 50}

Expand Down Expand Up @@ -287,31 +286,6 @@ defmodule Explorer.Chain do
|> Repo.all()
end

@doc """
The average time it took to mine/validate the last <= 100 `t:Explorer.Chain.Block.t/0`
"""
@spec average_block_time :: %Timex.Duration{}
def average_block_time do
{:ok, %Postgrex.Result{rows: [[%Postgrex.Interval{months: 0, days: days, secs: seconds}]]}} =
SQL.query(
Repo,
"""
SELECT coalesce(avg(difference), interval '0 seconds')
FROM (
SELECT b.timestamp - lag(b.timestamp) over (order by b.timestamp) as difference
FROM (SELECT * FROM blocks ORDER BY number DESC LIMIT 101) b
LIMIT 100 OFFSET 1
) t
""",
[]
)

hours = days * 24
minutes = 0
microseconds = 0
Duration.from_clock({hours, minutes, seconds, microseconds})
end

@doc """
The `t:Explorer.Chain.Address.t/0` `balance` in `unit`.
"""
Expand Down
Loading

0 comments on commit b02383a

Please sign in to comment.