Skip to content

Commit

Permalink
Add verified twin name as fallback name for unverified contract; Fix …
Browse files Browse the repository at this point in the history
…compiler label
  • Loading branch information
nikitosing committed Nov 28, 2022
1 parent db332c7 commit d2a9d4a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- [#6401](https://github.com/blockscout/blockscout/pull/6401) - Add Sol2Uml contract visualization
- [#6481](https://github.com/blockscout/blockscout/pull/6481) - Smart contract verification improvements
- [#6444](https://github.com/blockscout/blockscout/pull/6444) - Add support for yul verification via rust microservice
- [#6440](https://github.com/blockscout/blockscout/pull/6440) - Add support for base64 encoded NFT metadata
- [#6407](https://github.com/blockscout/blockscout/pull/6407) - Indexed ratio for int txs fetching stage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="smart-contract-form-group">
<div class="smart-contract-form-group-inner-wrapper">
<%= label @f, :compiler_version, gettext("Compiler") %>
<%= label :smart_contract, :compiler_version, gettext("Compiler") %>
<div class="center-column">
<%= select @f, :compiler_version, @compiler_versions, class: "form-control border-rounded", "aria-describedby": "compiler-help-block", id: "smart_contract_compiler_version" %>
<%= error_tag @f, :compiler_version, id: "compiler-help-block", class: "text-danger form-error" %>
Expand Down
19 changes: 13 additions & 6 deletions apps/block_scout_web/lib/block_scout_web/views/address_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ defmodule BlockScoutWeb.AddressView do
@doc """
Returns the primary name of an address if available. If there is no names on address function performs preload of names association.
"""
def primary_name(_, second_time? \\ false)

def primary_name(%Address{names: [_ | _] = address_names}, _second_time?) do
def primary_name(%Address{names: [_ | _] = address_names}) do
case Enum.find(address_names, &(&1.primary == true)) do
nil ->
%Address.Name{name: name} = Enum.at(address_names, 0)
Expand All @@ -194,11 +192,20 @@ defmodule BlockScoutWeb.AddressView do
end
end

def primary_name(%Address{names: _} = address, false) do
primary_name(Repo.preload(address, [:names]), true)
def primary_name(%Address{names: %Ecto.Association.NotLoaded{}} = address) do
primary_name(Repo.preload(address, [:names]))
end

def primary_name(%Address{names: _}, true), do: nil
def primary_name(%Address{names: _} = address) do
with false <- is_nil(address.contract_code),
twin <- Chain.get_verified_twin_contract(address),
false <- is_nil(twin) do
twin.name
else
_ ->
nil
end
end

def implementation_name(%Address{smart_contract: %{implementation_name: implementation_name}}),
do: implementation_name
Expand Down
26 changes: 13 additions & 13 deletions apps/block_scout_web/priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ msgstr ""
#: lib/block_scout_web/templates/address/_tabs.html.eex:48
#: lib/block_scout_web/templates/address/overview.html.eex:276
#: lib/block_scout_web/templates/address_validation/index.html.eex:11
#: lib/block_scout_web/views/address_view.ex:374
#: lib/block_scout_web/views/address_view.ex:381
#, elixir-autogen, elixir-format
msgid "Blocks Validated"
msgstr ""
Expand Down Expand Up @@ -588,13 +588,13 @@ msgstr ""
#: lib/block_scout_web/templates/api_docs/_action_tile.html.eex:187
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:126
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:149
#: lib/block_scout_web/views/address_view.ex:367
#: lib/block_scout_web/views/address_view.ex:374
#, elixir-autogen, elixir-format
msgid "Code"
msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:34
#: lib/block_scout_web/views/address_view.ex:373
#: lib/block_scout_web/views/address_view.ex:380
#, elixir-autogen, elixir-format
msgid "Coin Balance History"
msgstr ""
Expand Down Expand Up @@ -1006,7 +1006,7 @@ msgstr ""
msgid "Decoded"
msgstr ""

#: lib/block_scout_web/views/address_view.ex:368
#: lib/block_scout_web/views/address_view.ex:375
#, elixir-autogen, elixir-format
msgid "Decompiled Code"
msgstr ""
Expand Down Expand Up @@ -1515,7 +1515,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_internal_transaction/index.html.eex:17
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:11
#: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:6
#: lib/block_scout_web/views/address_view.ex:364
#: lib/block_scout_web/views/address_view.ex:371
#: lib/block_scout_web/views/transaction_view.ex:510
#, elixir-autogen, elixir-format
msgid "Internal Transactions"
Expand Down Expand Up @@ -1637,7 +1637,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_logs/index.html.eex:10
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:17
#: lib/block_scout_web/templates/transaction_log/index.html.eex:8
#: lib/block_scout_web/views/address_view.ex:375
#: lib/block_scout_web/views/address_view.ex:382
#: lib/block_scout_web/views/transaction_view.ex:511
#, elixir-autogen, elixir-format
msgid "Logs"
Expand Down Expand Up @@ -2113,15 +2113,15 @@ msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:81
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:27
#: lib/block_scout_web/views/address_view.ex:369
#: lib/block_scout_web/views/address_view.ex:376
#: lib/block_scout_web/views/tokens/overview_view.ex:41
#, elixir-autogen, elixir-format
msgid "Read Contract"
msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:88
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:41
#: lib/block_scout_web/views/address_view.ex:370
#: lib/block_scout_web/views/address_view.ex:377
#, elixir-autogen, elixir-format
msgid "Read Proxy"
msgstr ""
Expand Down Expand Up @@ -2757,7 +2757,7 @@ msgstr ""
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:15
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:4
#: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:7
#: lib/block_scout_web/views/address_view.ex:366
#: lib/block_scout_web/views/address_view.ex:373
#: lib/block_scout_web/views/tokens/instance/overview_view.ex:197
#: lib/block_scout_web/views/tokens/overview_view.ex:39
#: lib/block_scout_web/views/transaction_view.ex:509
Expand All @@ -2781,7 +2781,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:13
#: lib/block_scout_web/templates/layout/_topnav.html.eex:78
#: lib/block_scout_web/templates/tokens/index.html.eex:10
#: lib/block_scout_web/views/address_view.ex:363
#: lib/block_scout_web/views/address_view.ex:370
#, elixir-autogen, elixir-format
msgid "Tokens"
msgstr ""
Expand Down Expand Up @@ -2943,7 +2943,7 @@ msgstr ""
#: lib/block_scout_web/templates/block_transaction/index.html.eex:10
#: lib/block_scout_web/templates/chain/show.html.eex:213
#: lib/block_scout_web/templates/layout/_topnav.html.eex:49
#: lib/block_scout_web/views/address_view.ex:365
#: lib/block_scout_web/views/address_view.ex:372
#, elixir-autogen, elixir-format
msgid "Transactions"
msgstr ""
Expand Down Expand Up @@ -3287,14 +3287,14 @@ msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:95
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:34
#: lib/block_scout_web/views/address_view.ex:371
#: lib/block_scout_web/views/address_view.ex:378
#, elixir-autogen, elixir-format
msgid "Write Contract"
msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:102
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:48
#: lib/block_scout_web/views/address_view.ex:372
#: lib/block_scout_web/views/address_view.ex:379
#, elixir-autogen, elixir-format
msgid "Write Proxy"
msgstr ""
Expand Down
26 changes: 13 additions & 13 deletions apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ msgstr ""
#: lib/block_scout_web/templates/address/_tabs.html.eex:48
#: lib/block_scout_web/templates/address/overview.html.eex:276
#: lib/block_scout_web/templates/address_validation/index.html.eex:11
#: lib/block_scout_web/views/address_view.ex:374
#: lib/block_scout_web/views/address_view.ex:381
#, elixir-autogen, elixir-format
msgid "Blocks Validated"
msgstr ""
Expand Down Expand Up @@ -588,13 +588,13 @@ msgstr ""
#: lib/block_scout_web/templates/api_docs/_action_tile.html.eex:187
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:126
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:149
#: lib/block_scout_web/views/address_view.ex:367
#: lib/block_scout_web/views/address_view.ex:374
#, elixir-autogen, elixir-format
msgid "Code"
msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:34
#: lib/block_scout_web/views/address_view.ex:373
#: lib/block_scout_web/views/address_view.ex:380
#, elixir-autogen, elixir-format
msgid "Coin Balance History"
msgstr ""
Expand Down Expand Up @@ -1006,7 +1006,7 @@ msgstr ""
msgid "Decoded"
msgstr ""

#: lib/block_scout_web/views/address_view.ex:368
#: lib/block_scout_web/views/address_view.ex:375
#, elixir-autogen, elixir-format
msgid "Decompiled Code"
msgstr ""
Expand Down Expand Up @@ -1515,7 +1515,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_internal_transaction/index.html.eex:17
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:11
#: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:6
#: lib/block_scout_web/views/address_view.ex:364
#: lib/block_scout_web/views/address_view.ex:371
#: lib/block_scout_web/views/transaction_view.ex:510
#, elixir-autogen, elixir-format
msgid "Internal Transactions"
Expand Down Expand Up @@ -1637,7 +1637,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_logs/index.html.eex:10
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:17
#: lib/block_scout_web/templates/transaction_log/index.html.eex:8
#: lib/block_scout_web/views/address_view.ex:375
#: lib/block_scout_web/views/address_view.ex:382
#: lib/block_scout_web/views/transaction_view.ex:511
#, elixir-autogen, elixir-format
msgid "Logs"
Expand Down Expand Up @@ -2113,15 +2113,15 @@ msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:81
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:27
#: lib/block_scout_web/views/address_view.ex:369
#: lib/block_scout_web/views/address_view.ex:376
#: lib/block_scout_web/views/tokens/overview_view.ex:41
#, elixir-autogen, elixir-format
msgid "Read Contract"
msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:88
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:41
#: lib/block_scout_web/views/address_view.ex:370
#: lib/block_scout_web/views/address_view.ex:377
#, elixir-autogen, elixir-format
msgid "Read Proxy"
msgstr ""
Expand Down Expand Up @@ -2757,7 +2757,7 @@ msgstr ""
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:15
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:4
#: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:7
#: lib/block_scout_web/views/address_view.ex:366
#: lib/block_scout_web/views/address_view.ex:373
#: lib/block_scout_web/views/tokens/instance/overview_view.ex:197
#: lib/block_scout_web/views/tokens/overview_view.ex:39
#: lib/block_scout_web/views/transaction_view.ex:509
Expand All @@ -2781,7 +2781,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:13
#: lib/block_scout_web/templates/layout/_topnav.html.eex:78
#: lib/block_scout_web/templates/tokens/index.html.eex:10
#: lib/block_scout_web/views/address_view.ex:363
#: lib/block_scout_web/views/address_view.ex:370
#, elixir-autogen, elixir-format
msgid "Tokens"
msgstr ""
Expand Down Expand Up @@ -2943,7 +2943,7 @@ msgstr ""
#: lib/block_scout_web/templates/block_transaction/index.html.eex:10
#: lib/block_scout_web/templates/chain/show.html.eex:213
#: lib/block_scout_web/templates/layout/_topnav.html.eex:49
#: lib/block_scout_web/views/address_view.ex:365
#: lib/block_scout_web/views/address_view.ex:372
#, elixir-autogen, elixir-format
msgid "Transactions"
msgstr ""
Expand Down Expand Up @@ -3287,14 +3287,14 @@ msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:95
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:34
#: lib/block_scout_web/views/address_view.ex:371
#: lib/block_scout_web/views/address_view.ex:378
#, elixir-autogen, elixir-format
msgid "Write Contract"
msgstr ""

#: lib/block_scout_web/templates/address/_tabs.html.eex:102
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:48
#: lib/block_scout_web/views/address_view.ex:372
#: lib/block_scout_web/views/address_view.ex:379
#, elixir-autogen, elixir-format
msgid "Write Proxy"
msgstr ""
Expand Down
44 changes: 26 additions & 18 deletions apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4203,6 +4203,30 @@ defmodule Explorer.Chain do
|> repo.insert(on_conflict: :nothing, conflict_target: [:address_hash, :name])
end

def get_verified_twin_contract(%Explorer.Chain.Address{} = target_address) do
case target_address do
%{contract_code: %Chain.Data{bytes: contract_code_bytes}} ->
target_address_hash = target_address.hash

contract_code_md5 = Helper.contract_code_md5(contract_code_bytes)

verified_contract_twin_query =
from(
smart_contract in SmartContract,
where: smart_contract.contract_code_md5 == ^contract_code_md5,
where: smart_contract.address_hash != ^target_address_hash,
select: smart_contract,
limit: 1
)

verified_contract_twin_query
|> Repo.one(timeout: 10_000)

_ ->
nil
end
end

@doc """
Finds metadata for verification of a contract from verified twins: contracts with the same bytecode
which were verified previously, returns a single t:SmartContract.t/0
Expand All @@ -4216,24 +4240,8 @@ defmodule Explorer.Chain do

def get_address_verified_twin_contract(%Explorer.Chain.Hash{} = address_hash) do
with target_address <- Repo.get(Address, address_hash),
false <- is_nil(target_address),
%{contract_code: %Chain.Data{bytes: contract_code_bytes}} <- target_address do
target_address_hash = target_address.hash

contract_code_md5 = Helper.contract_code_md5(contract_code_bytes)

verified_contract_twin_query =
from(
smart_contract in SmartContract,
where: smart_contract.contract_code_md5 == ^contract_code_md5,
where: smart_contract.address_hash != ^target_address_hash,
select: smart_contract,
limit: 1
)

verified_contract_twin =
verified_contract_twin_query
|> Repo.one(timeout: 10_000)
false <- is_nil(target_address) do
verified_contract_twin = get_verified_twin_contract(target_address)

verified_contract_twin_additional_sources = get_contract_additional_sources(verified_contract_twin)

Expand Down

0 comments on commit d2a9d4a

Please sign in to comment.