Merge pull request #7934 from blockscout/vb-small-refactoring-consensus

Explicitly set consensus == true in queries (convenient for search)
pull/7936/head
Victor Baranov 1 year ago committed by GitHub
commit 3ff97afbc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 2
      apps/explorer/lib/explorer/account/notifier/summary.ex
  3. 8
      apps/explorer/lib/explorer/chain.ex
  4. 18
      apps/explorer/lib/explorer/chain/address/coin_balance.ex
  5. 4
      apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex
  6. 2
      apps/explorer/lib/explorer/smart_contract/vyper/code_compiler.ex
  7. 2
      apps/indexer/lib/indexer/temporary/uncles_without_index.ex

@ -25,6 +25,7 @@
### Chore ### Chore
- [#7934](https://github.com/blockscout/blockscout/pull/7934) - Explicitly set consensus == true in queries (convenient for search)
- [#7901](https://github.com/blockscout/blockscout/pull/7901) - Fix Docker image build - [#7901](https://github.com/blockscout/blockscout/pull/7901) - Fix Docker image build
- [#7890](https://github.com/blockscout/blockscout/pull/7890), [#7918](https://github.com/blockscout/blockscout/pull/7918) - Resolve warning: Application.get_env/2 is discouraged in the module body, use Application.compile_env/3 instead - [#7890](https://github.com/blockscout/blockscout/pull/7890), [#7918](https://github.com/blockscout/blockscout/pull/7918) - Resolve warning: Application.get_env/2 is discouraged in the module body, use Application.compile_env/3 instead
- [#7863](https://github.com/blockscout/blockscout/pull/7863) - Add max_age for account sessions - [#7863](https://github.com/blockscout/blockscout/pull/7863) - Add max_age for account sessions

@ -3,8 +3,6 @@ defmodule Explorer.Account.Notifier.Summary do
Compose a summary from transactions Compose a summary from transactions
""" """
require Logger
alias Explorer alias Explorer
alias Explorer.Account.Notifier.Summary alias Explorer.Account.Notifier.Summary
alias Explorer.{Chain, Repo} alias Explorer.{Chain, Repo}

@ -764,7 +764,7 @@ defmodule Explorer.Chain do
def block_reward(block_number) do def block_reward(block_number) do
block_hash = block_hash =
Block Block
|> where([block], block.number == ^block_number and block.consensus) |> where([block], block.number == ^block_number and block.consensus == true)
|> select([block], block.hash) |> select([block], block.hash)
|> Repo.one!() |> Repo.one!()
@ -784,7 +784,7 @@ defmodule Explorer.Chain do
left_join: transaction in assoc(block, :transactions), left_join: transaction in assoc(block, :transactions),
inner_join: emission_reward in EmissionReward, inner_join: emission_reward in EmissionReward,
on: fragment("? <@ ?", block.number, emission_reward.block_range), on: fragment("? <@ ?", block.number, emission_reward.block_range),
where: block.number == ^block_number and block.consensus, where: block.number == ^block_number and block.consensus == true,
group_by: [emission_reward.reward, block.hash], group_by: [emission_reward.reward, block.hash],
select: %Wei{ select: %Wei{
value: coalesce(sum(transaction.gas_used * transaction.gas_price), 0) + emission_reward.reward value: coalesce(sum(transaction.gas_used * transaction.gas_price), 0) + emission_reward.reward
@ -6373,8 +6373,8 @@ defmodule Explorer.Chain do
defp find_block_timestamp(number, options) do defp find_block_timestamp(number, options) do
Block Block
|> where([b], b.number == ^number) |> where([block], block.number == ^number)
|> select([b], b.timestamp) |> select([block], block.timestamp)
|> limit(1) |> limit(1)
|> select_repo(options).one() |> select_repo(options).one()
end end

@ -104,19 +104,19 @@ defmodule Explorer.Chain.Address.CoinBalance do
Application.get_env(:block_scout_web, BlockScoutWeb.Chain.Address.CoinBalance)[:coin_balance_history_days] Application.get_env(:block_scout_web, BlockScoutWeb.Chain.Address.CoinBalance)[:coin_balance_history_days]
CoinBalance CoinBalance
|> join(:inner, [cb], b in Block, on: cb.block_number == b.number) |> join(:inner, [cb], block in Block, on: cb.block_number == block.number)
|> where([cb], cb.address_hash == ^address_hash) |> where([cb], cb.address_hash == ^address_hash)
|> limit_time_interval(days_to_consider, block_timestamp) |> limit_time_interval(days_to_consider, block_timestamp)
|> group_by([cb, b], fragment("date_trunc('day', ?)", b.timestamp)) |> group_by([cb, block], fragment("date_trunc('day', ?)", block.timestamp))
|> order_by([cb, b], fragment("date_trunc('day', ?)", b.timestamp)) |> order_by([cb, block], fragment("date_trunc('day', ?)", block.timestamp))
|> select([cb, b], %{date: type(fragment("date_trunc('day', ?)", b.timestamp), :date), value: max(cb.value)}) |> select([cb, block], %{date: type(fragment("date_trunc('day', ?)", block.timestamp), :date), value: max(cb.value)})
end end
def limit_time_interval(query, days_to_consider, nil) do def limit_time_interval(query, days_to_consider, nil) do
query query
|> where( |> where(
[cb, b], [cb, block],
b.timestamp >= block.timestamp >=
fragment("date_trunc('day', now() - CAST(? AS INTERVAL))", ^%Postgrex.Interval{days: days_to_consider}) fragment("date_trunc('day', now() - CAST(? AS INTERVAL))", ^%Postgrex.Interval{days: days_to_consider})
) )
end end
@ -124,8 +124,8 @@ defmodule Explorer.Chain.Address.CoinBalance do
def limit_time_interval(query, days_to_consider, %{timestamp: timestamp}) do def limit_time_interval(query, days_to_consider, %{timestamp: timestamp}) do
query query
|> where( |> where(
[cb, b], [cb, block],
b.timestamp >= block.timestamp >=
fragment( fragment(
"(? AT TIME ZONE ?) - CAST(? AS INTERVAL)", "(? AT TIME ZONE ?) - CAST(? AS INTERVAL)",
^timestamp, ^timestamp,
@ -146,7 +146,7 @@ defmodule Explorer.Chain.Address.CoinBalance do
cb in subquery(coin_balance_query), cb in subquery(coin_balance_query),
inner_join: block in Block, inner_join: block in Block,
on: cb.block_number == block.number, on: cb.block_number == block.number,
where: block.consensus, where: block.consensus == true,
select: %{timestamp: block.timestamp, value: cb.value} select: %{timestamp: block.timestamp, value: cb.value}
) )
end end

@ -282,7 +282,7 @@ defmodule Explorer.Chain.Import.Runner.InternalTransactions do
query = query =
from( from(
block in Block, block in Block,
where: block.number in ^block_numbers and block.consensus, where: block.number in ^block_numbers and block.consensus == true,
select: block.hash, select: block.hash,
# Enforce Block ShareLocks order (see docs: sharelocks.md) # Enforce Block ShareLocks order (see docs: sharelocks.md)
order_by: [asc: block.hash], order_by: [asc: block.hash],
@ -672,7 +672,7 @@ defmodule Explorer.Chain.Import.Runner.InternalTransactions do
update_query = update_query =
from( from(
block in Block, block in Block,
where: block.number in ^invalid_block_numbers and block.consensus, where: block.number in ^invalid_block_numbers and block.consensus == true,
where: block.number > ^minimal_block, where: block.number > ^minimal_block,
select: block.hash, select: block.hash,
# ShareLocks order already enforced by `acquire_blocks` (see docs: sharelocks.md) # ShareLocks order already enforced by `acquire_blocks` (see docs: sharelocks.md)

@ -5,8 +5,6 @@ defmodule Explorer.SmartContract.Vyper.CodeCompiler do
alias Explorer.SmartContract.VyperDownloader alias Explorer.SmartContract.VyperDownloader
require Logger
@spec run(Keyword.t()) :: {:ok, map} | {:error, :compilation | :name} @spec run(Keyword.t()) :: {:ok, map} | {:error, :compilation | :name}
def run(params) do def run(params) do
compiler_version = Keyword.fetch!(params, :compiler_version) compiler_version = Keyword.fetch!(params, :compiler_version)

@ -52,7 +52,7 @@ defmodule Indexer.Temporary.UnclesWithoutIndex do
query = query =
from(bsdr in SecondDegreeRelation, from(bsdr in SecondDegreeRelation,
join: block in assoc(bsdr, :nephew), join: block in assoc(bsdr, :nephew),
where: is_nil(bsdr.index) and is_nil(bsdr.uncle_fetched_at) and block.consensus, where: is_nil(bsdr.index) and is_nil(bsdr.uncle_fetched_at) and block.consensus == true,
select: bsdr.nephew_hash, select: bsdr.nephew_hash,
group_by: bsdr.nephew_hash group_by: bsdr.nephew_hash
) )

Loading…
Cancel
Save