Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Blockscout live issues #918

Merged
merged 11 commits into from
Jun 12, 2023
2 changes: 1 addition & 1 deletion .github/workflows/helm_deploy_dispatch.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: 'Deploy Helm Chart'
run-name: 'helmfile ${{ github.event.inputs.action }} on ${{ github.event.inputs.deployment }} - ${{ github.event.inputs.image-commit }}'
run-name: 'helmfile ${{ github.event.inputs.action }} on ${{ github.event.inputs.deployment }} - ${{ github.sha }}'

on:
workflow_dispatch:
Expand Down
2 changes: 1 addition & 1 deletion apps/block_scout_web/.iex.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ defmodule Clabs.Debug do

def token_tx_for_valora_address do
{:ok,hsh} = Address.cast("0x6131a6d616a4be3737b38988847270a64bc10caa")
Explorer.GraphQL.token_txtransfers_query_for_address(hsh, 26)
Explorer.GraphQL.token_txtransfers_query_for_address(hsh, 26, 0)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ defmodule BlockScoutWeb.VerifiedContractsController do
page_number: params |> fetch_page_number() |> Integer.to_string(),
contracts_count: Chain.count_contracts_from_cache(),
verified_contracts_count: Chain.count_verified_contracts_from_cache(),
new_contracts_count: Chain.count_new_contracts_from_cache(),
# celo: disabling new contract count
new_contracts_count: nil,
new_verified_contracts_count: Chain.count_new_verified_contracts_from_cache()
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ defmodule BlockScoutWeb.Resolvers.TokenTransferTx do
alias Absinthe.Relay.Connection
alias Explorer.{GraphQL, Repo}

def get_by(_, %{address_hash: address_hash, first: first} = args, _) do
def get_by(_, %{address_hash: address_hash, first: limit} = args, _) do
connection_args = Map.take(args, [:after, :before, :first, :last])

offset =
case Connection.offset(args) do
{:ok, offset} when is_integer(offset) -> offset
_ -> 0
end

address_hash
|> GraphQL.token_txtransfers_query_for_address(first)
|> GraphQL.token_txtransfers_query_for_address(offset, limit)
|> Connection.from_query(&Repo.all/1, connection_args, options(args))
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
<span class="text-muted"><%= gettext ("Total") %></span>
</div>
<div class="d-flex flex-column">
<h1 class="text-success"><%= if 0 |> Decimal.new() |> Decimal.lt?(@new_contracts_count), do: "+" %><%= BlockScoutWeb.Cldr.Number.to_string!(@new_contracts_count, format: "#,###") %></h1>
<span class="text-muted"><%= gettext ("Last 24h") %></span>
<%= if ! is_nil(@new_contracts_count) do %>
<h1 class="text-success"><%= if 0 |> Decimal.new() |> Decimal.lt?(@new_contracts_count), do: "+" %><%= BlockScoutWeb.Cldr.Number.to_string!(@new_contracts_count, format: "#,###") %></h1>
<span class="text-muted"><%= gettext ("Last 24h") %></span>
<% end %>
</div>
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions apps/block_scout_web/priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1496,8 +1496,8 @@ msgstr ""
msgid "Is Yul contract"
msgstr ""

#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:13
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:26
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:14
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:28
#, elixir-autogen, elixir-format
msgid "Last 24h"
msgstr ""
Expand Down Expand Up @@ -2723,7 +2723,7 @@ msgid "Topics"
msgstr ""

#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:9
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:22
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:24
#, elixir-autogen, elixir-format
msgid "Total"
msgstr ""
Expand Down Expand Up @@ -2994,7 +2994,7 @@ msgstr ""
msgid "Verified"
msgstr ""

#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:18
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:20
#: lib/block_scout_web/templates/verified_contracts/index.html.eex:5
#, elixir-autogen, elixir-format
msgid "Verified Contracts"
Expand Down
8 changes: 4 additions & 4 deletions apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -1496,8 +1496,8 @@ msgstr ""
msgid "Is Yul contract"
msgstr ""

#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:13
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:26
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:14
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:28
#, elixir-autogen, elixir-format
msgid "Last 24h"
msgstr ""
Expand Down Expand Up @@ -2723,7 +2723,7 @@ msgid "Topics"
msgstr ""

#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:9
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:22
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:24
#, elixir-autogen, elixir-format
msgid "Total"
msgstr ""
Expand Down Expand Up @@ -2994,7 +2994,7 @@ msgstr ""
msgid "Verified"
msgstr ""

#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:18
#: lib/block_scout_web/templates/verified_contracts/_stats.html.eex:20
#: lib/block_scout_web/templates/verified_contracts/index.html.eex:5
#, elixir-autogen, elixir-format
msgid "Verified Contracts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule BlockScoutWeb.VerifiedContractsControllerTest do

alias Explorer.Chain.Cache.{
ContractsCounter,
NewContractsCounter,
# NewContractsCounter,
NewVerifiedContractsCounter,
VerifiedContractsCounter
}
Expand All @@ -16,8 +16,9 @@ defmodule BlockScoutWeb.VerifiedContractsControllerTest do
test "returns 200", %{conn: conn} do
start_supervised!(ContractsCounter)
ContractsCounter.consolidate()
start_supervised!(NewContractsCounter)
NewContractsCounter.consolidate()
# celo: disable new contracts count
# start_supervised!(NewContractsCounter)
# NewContractsCounter.consolidate()
start_supervised!(NewVerifiedContractsCounter)
NewVerifiedContractsCounter.consolidate()
start_supervised!(VerifiedContractsCounter)
Expand Down
3 changes: 2 additions & 1 deletion apps/explorer/lib/explorer/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ defmodule Explorer.Application do
configure(Explorer.Market.History.Cataloger),
configure(Explorer.Chain.Cache.TokenExchangeRate),
configure(Explorer.Chain.Cache.ContractsCounter),
configure(Explorer.Chain.Cache.NewContractsCounter),
# celo: disable new contract count
# configure(Explorer.Chain.Cache.NewContractsCounter),
configure(Explorer.Chain.Cache.VerifiedContractsCounter),
configure(Explorer.Chain.Cache.NewVerifiedContractsCounter),
configure(Explorer.Chain.Transaction.History.Historian),
Expand Down
22 changes: 15 additions & 7 deletions apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Explorer.Chain do
BlockNumber,
Blocks,
ContractsCounter,
NewContractsCounter,
# NewContractsCounter, celo: disable new contracts counter
NewVerifiedContractsCounter,
Transactions,
Uncles,
Expand Down Expand Up @@ -1428,7 +1428,10 @@ defmodule Explorer.Chain do
address_verified_twin_contract_updated =
address_verified_twin_contract
|> Map.put(:address_hash, hash)
|> Map.put_new(:metadata_from_verified_twin, true)
|> Map.put(:metadata_from_verified_twin, true)
|> Map.put(:implementation_address_hash, nil)
|> Map.put(:implementation_name, nil)
|> Map.put(:implementation_fetched_at, nil)

address_result
|> Map.put(:smart_contract, address_verified_twin_contract_updated)
Expand Down Expand Up @@ -1963,7 +1966,10 @@ defmodule Explorer.Chain do
address_verified_twin_contract_updated =
address_verified_twin_contract
|> Map.put(:address_hash, hash)
|> Map.put_new(:metadata_from_verified_twin, true)
|> Map.put(:metadata_from_verified_twin, true)
|> Map.put(:implementation_address_hash, nil)
|> Map.put(:implementation_name, nil)
|> Map.put(:implementation_fetched_at, nil)

address_result
|> Map.put(:smart_contract, address_verified_twin_contract_updated)
Expand Down Expand Up @@ -4575,9 +4581,10 @@ defmodule Explorer.Chain do
if address_verified_twin_contract do
address_verified_twin_contract
|> Map.put(:address_hash, address_hash)
|> Map.put(:implementation_address_hash, current_smart_contract.implementation_address_hash)
|> Map.put(:implementation_name, current_smart_contract.implementation_name)
|> Map.put(:implementation_fetched_at, current_smart_contract.implementation_fetched_at)
|> Map.put(:metadata_from_verified_twin, true)
|> Map.put(:implementation_address_hash, nil)
|> Map.put(:implementation_name, nil)
|> Map.put(:implementation_fetched_at, nil)
else
current_smart_contract
end
Expand Down Expand Up @@ -7161,7 +7168,8 @@ defmodule Explorer.Chain do
end

def count_new_contracts_from_cache do
NewContractsCounter.fetch()
0
# celo: disable new contract count NewContractsCounter.fetch()
end

def address_counters(address) do
Expand Down
54 changes: 35 additions & 19 deletions apps/explorer/lib/explorer/chain/smart_contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ defmodule Explorer.Chain.SmartContract do
field(:implementation_address_hash, Hash.Address, default: nil)
field(:autodetect_constructor_args, :boolean, virtual: true)
field(:is_yul, :boolean, virtual: true)
field(:metadata_from_verified_twin, :boolean, virtual: true)

has_many(
:decompiled_smart_contracts,
Expand Down Expand Up @@ -529,7 +530,11 @@ defmodule Explorer.Chain.SmartContract do

def proxy_contract?(_), do: false

def get_implementation_address_hash(%__MODULE__{abi: nil}), do: false
def get_implementation_address_hash(%__MODULE__{abi: nil}), do: {nil, nil}

def get_implementation_address_hash(%__MODULE__{metadata_from_verified_twin: true} = smart_contract) do
get_implementation_address_hash({:updated, smart_contract})
end

def get_implementation_address_hash(
%__MODULE__{
Expand All @@ -540,7 +545,7 @@ defmodule Explorer.Chain.SmartContract do
updated_smart_contract =
if Application.get_env(:explorer, :enable_caching_implementation_data_of_proxy) &&
check_implementation_refetch_neccessity(implementation_fetched_at) do
Chain.address_hash_to_smart_contract(address_hash)
Chain.address_hash_to_smart_contract_without_twin(address_hash)
else
smart_contract
end
Expand All @@ -555,11 +560,13 @@ defmodule Explorer.Chain.SmartContract do
abi: abi,
implementation_address_hash: implementation_address_hash_from_db,
implementation_name: implementation_name_from_db,
implementation_fetched_at: implementation_fetched_at
implementation_fetched_at: implementation_fetched_at,
metadata_from_verified_twin: metadata_from_verified_twin
}}
) do
if check_implementation_refetch_neccessity(implementation_fetched_at) do
get_implementation_address_hash_task = Task.async(fn -> get_implementation_address_hash(address_hash, abi) end)
get_implementation_address_hash_task =
Task.async(fn -> get_implementation_address_hash(address_hash, abi, metadata_from_verified_twin) end)

timeout = Application.get_env(:explorer, :implementation_data_fetching_timeout)

Expand Down Expand Up @@ -624,8 +631,9 @@ defmodule Explorer.Chain.SmartContract do
end
end

@spec get_implementation_address_hash(Hash.Address.t(), list()) :: {String.t() | nil, String.t() | nil}
defp get_implementation_address_hash(proxy_address_hash, abi)
@spec get_implementation_address_hash(Hash.Address.t(), list(), boolean() | nil) ::
{String.t() | nil, String.t() | nil}
defp get_implementation_address_hash(proxy_address_hash, abi, metadata_from_verified_twin)
when not is_nil(proxy_address_hash) and not is_nil(abi) do
implementation_method_abi =
abi
Expand All @@ -651,10 +659,10 @@ defmodule Explorer.Chain.SmartContract do
get_implementation_address_hash_eip_1967(proxy_address_hash)
end

save_implementation_data(implementation_address, proxy_address_hash)
save_implementation_data(implementation_address, proxy_address_hash, metadata_from_verified_twin)
end

defp get_implementation_address_hash(proxy_address_hash, abi) when is_nil(proxy_address_hash) or is_nil(abi) do
defp get_implementation_address_hash(_proxy_address_hash, _abi, _) do
{nil, nil}
end

Expand Down Expand Up @@ -794,28 +802,30 @@ defmodule Explorer.Chain.SmartContract do
abi_decode_address_output(implementation_address)
end

defp save_implementation_data(nil, _), do: {nil, nil}
defp save_implementation_data(nil, _, _), do: {nil, nil}

defp save_implementation_data(empty_address_hash_string, proxy_address_hash)
defp save_implementation_data(empty_address_hash_string, proxy_address_hash, metadata_from_verified_twin)
when empty_address_hash_string in [
"0x",
"0x0",
"0x0000000000000000000000000000000000000000000000000000000000000000",
@burn_address_hash_str
] do
proxy_address_hash
|> Chain.address_hash_to_smart_contract_without_twin()
|> changeset(%{
implementation_name: nil,
implementation_address_hash: nil,
implementation_fetched_at: DateTime.utc_now()
})
|> Repo.update()
if is_nil(metadata_from_verified_twin) or !metadata_from_verified_twin do
proxy_address_hash
|> Chain.address_hash_to_smart_contract_without_twin()
|> changeset(%{
implementation_name: nil,
implementation_address_hash: nil,
implementation_fetched_at: DateTime.utc_now()
})
|> Repo.update()
end

{:empty, :empty}
end

defp save_implementation_data(implementation_address_hash_string, proxy_address_hash)
defp save_implementation_data(implementation_address_hash_string, proxy_address_hash, _)
when is_binary(implementation_address_hash_string) do
with {:ok, address_hash} <- Chain.string_to_address_hash(implementation_address_hash_string),
proxy_contract <- Chain.address_hash_to_smart_contract_without_twin(proxy_address_hash),
Expand Down Expand Up @@ -845,6 +855,12 @@ defmodule Explorer.Chain.SmartContract do

{implementation_address_hash_string, nil}

true ->
{:ok, address_hash} = Chain.string_to_address_hash(implementation_address_hash_string)
smart_contract = Chain.address_hash_to_smart_contract(address_hash)

{implementation_address_hash_string, smart_contract && smart_contract.name}

_ ->
{implementation_address_hash_string, nil}
end
Expand Down
13 changes: 6 additions & 7 deletions apps/explorer/lib/explorer/graphql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ defmodule Explorer.GraphQL do
)
end

def token_txtransfers_query_for_address(address_hash, first) do
tt_limit = first * 2
def token_txtransfers_query_for_address(address_hash, offset, limit) do
page = floor(offset / limit) + 1
growing_limit = limit * (page + 1)

tokens =
from(
Expand All @@ -254,8 +255,7 @@ defmodule Explorer.GraphQL do
desc: tt.from_address_hash,
desc: tt.to_address_hash
],
limit: ^tt_limit,
offset: 0
limit: ^growing_limit
)

from(
Expand All @@ -282,8 +282,7 @@ defmodule Explorer.GraphQL do
input: tx.input,
nonce: tx.nonce,
block_number: tt.block_number
},
limit: ^first
}
)
|> order_by([transaction: t],
desc: t.block_number,
Expand Down Expand Up @@ -425,7 +424,7 @@ defmodule Explorer.GraphQL do
nonce: tx.nonce,
block_number: tt.block_number,
token_type: token.type,
token_id: tt.token_id
token_id: fragment("COALESCE(?, (COALESCE(?, ARRAY[]::Decimal[]))[1])", tt.token_id, tt.token_ids)
},
order_by: [desc: tt.block_number, desc: tt.amount, desc: tt.log_index]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Explorer.Chain.Cache.NewContractsCounterTest do
alias Explorer.Chain.Cache.NewContractsCounter
alias Explorer.Chain

# celo: disabling new contract count
@tag :skip
test "populates the cache with the number of new contracts (last 24h)" do
:transaction
|> insert(created_contract_code_indexed_at: Timex.shift(Timex.now(), hours: -1))
Expand Down
Loading