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

autodetection of constructor arguments #2821

Merged
merged 15 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

### Features
- [#2862](https://github.com/poanetwork/blockscout/pull/2862) - Coin total supply from DB API endpoint
- [#2822](https://github.com/poanetwork/blockscout/pull/2822) - Estimated address count on the main page, if cache is empty
- [#2821](https://github.com/poanetwork/blockscout/pull/2821) - add autodetection of constructor arguments
- [#2825](https://github.com/poanetwork/blockscout/pull/2825) - separate token transfers and transactions
- [#2787](https://github.com/poanetwork/blockscout/pull/2787) - async fetching of address counters
- [#2791](https://github.com/poanetwork/blockscout/pull/2791) - add ipc client
- [#2449](https://github.com/poanetwork/blockscout/pull/2449) - add ability to send notification events through postgres notify
- [#2822](https://github.com/poanetwork/blockscout/pull/2822) - Estimated address count on the main page, if cache is empty


### Fixes
- [#2864](https://github.com/poanetwork/blockscout/pull/2864) - add token instance metadata type check
Expand Down
8 changes: 8 additions & 0 deletions apps/block_scout_web/assets/js/pages/verification_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ if ($contractVerificationPage.length) {
$(this).hide()
})

$('.autodetectfalse').on('click', function () {
if ($(this).prop('checked')) { $('.constructor-arguments').show() }
})

$('.autodetecttrue').on('click', function () {
if ($(this).prop('checked')) { $('.constructor-arguments').hide() }
})

$('.js-smart-contract-form-reset').on('click', function () {
$('.js-contract-library-form-group').removeClass('active')
$('.js-contract-library-form-group').first().addClass('active')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@
</div>

<div class="smart-contract-form-group">
<div class="smart-contract-form-group-inner-wrapper">
<%= label f, "Try to fetch contructor arguments automatically" %>
<div class="center-column">
<div class="form-radios-group">
<div class="radio-big">
<%= radio_button f, :autodetect_contructor_args, false, checked: true, class: "form-check-input autodetectfalse" %>
<div class="radio-icon"></div>
<%= label :autodetect_contructor_args, :false, gettext("No"), class: "radio-text" %>
</div>
<div class="radio-big">
<%= radio_button f, :autodetect_contructor_args, true, class: "form-check-input autodetecttrue", "aria-describedby": "optimization-help-block" %>
<div class="radio-icon"></div>
<%= label :autodetect_contructor_args, :true, gettext("Yes"), class: "radio-text" %>
</div>
</div>
<%= error_tag f, :autodetect_contructor_args, id: "optimization-help-block", class: "text-danger form-error" %>
</div>
</div>
</div>

<div class="smart-contract-form-group constructor-arguments">
<div class="smart-contract-form-group-inner-wrapper">
<%= label f, :contructor_arguments, gettext("ABI-encoded Constructor Arguments (if required by the contract)") %>
<div class="center-column">
Expand Down
34 changes: 18 additions & 16 deletions apps/block_scout_web/priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ msgid "A string with the name of the module to be invoked."
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:104
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:125
msgid "ABI-encoded Constructor Arguments (if required by the contract)"
msgstr ""

Expand Down Expand Up @@ -266,7 +266,7 @@ msgid "Call Code"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:253
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:274
#: lib/block_scout_web/templates/api_docs/_action_tile.html.eex:47
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:54
msgid "Cancel"
Expand Down Expand Up @@ -370,7 +370,7 @@ msgid "Contract Creation Code"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:118
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:139
msgid "Contract Libraries"
msgstr ""

Expand Down Expand Up @@ -971,20 +971,20 @@ msgid "Less than"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:133
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:155
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:177
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:199
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:221
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:154
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:176
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:198
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:220
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:242
msgid "Library Address"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:123
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:145
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:167
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:189
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:211
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:144
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:166
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:188
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:210
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:232
msgid "Library Name"
msgstr ""

Expand All @@ -1010,7 +1010,7 @@ msgid "Loading..."
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:247
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:268
msgid "Loading...."
msgstr ""

Expand Down Expand Up @@ -1097,6 +1097,7 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:67
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:110
msgid "No"
msgstr ""

Expand Down Expand Up @@ -1217,7 +1218,7 @@ msgid "Request URL"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:250
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:271
msgid "Reset"
msgstr ""

Expand Down Expand Up @@ -1579,7 +1580,7 @@ msgid "Verify & Publish"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:249
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:270
msgid "Verify & publish"
msgstr ""

Expand Down Expand Up @@ -1654,6 +1655,7 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:72
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:115
msgid "Yes"
msgstr ""

Expand Down
34 changes: 18 additions & 16 deletions apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ msgid "A string with the name of the module to be invoked."
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:104
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:125
msgid "ABI-encoded Constructor Arguments (if required by the contract)"
msgstr ""

Expand Down Expand Up @@ -266,7 +266,7 @@ msgid "Call Code"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:253
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:274
#: lib/block_scout_web/templates/api_docs/_action_tile.html.eex:47
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:54
msgid "Cancel"
Expand Down Expand Up @@ -370,7 +370,7 @@ msgid "Contract Creation Code"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:118
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:139
msgid "Contract Libraries"
msgstr ""

Expand Down Expand Up @@ -971,20 +971,20 @@ msgid "Less than"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:133
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:155
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:177
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:199
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:221
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:154
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:176
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:198
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:220
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:242
msgid "Library Address"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:123
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:145
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:167
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:189
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:211
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:144
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:166
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:188
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:210
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:232
msgid "Library Name"
msgstr ""

Expand All @@ -1010,7 +1010,7 @@ msgid "Loading..."
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:247
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:268
msgid "Loading...."
msgstr ""

Expand Down Expand Up @@ -1097,6 +1097,7 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:67
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:110
msgid "No"
msgstr ""

Expand Down Expand Up @@ -1217,7 +1218,7 @@ msgid "Request URL"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:250
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:271
msgid "Reset"
msgstr ""

Expand Down Expand Up @@ -1579,7 +1580,7 @@ msgid "Verify & Publish"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:249
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:270
msgid "Verify & publish"
msgstr ""

Expand Down Expand Up @@ -1654,6 +1655,7 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:72
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:115
msgid "Yes"
msgstr ""

Expand Down
6 changes: 6 additions & 0 deletions apps/explorer/lib/explorer/smart_contract/publisher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ defmodule Explorer.SmartContract.Publisher do
params_with_external_libaries = add_external_libraries(params, external_libraries)

case Verifier.evaluate_authenticity(address_hash, params_with_external_libaries) do
{:ok, %{abi: abi, contructor_arguments: contructor_arguments}} ->
params_with_contructor_arguments =
Map.put(params_with_external_libaries, "constructor_arguments", contructor_arguments)

publish_smart_contract(address_hash, params_with_contructor_arguments, abi)

{:ok, %{abi: abi}} ->
publish_smart_contract(address_hash, params_with_external_libaries, abi)

Expand Down
26 changes: 22 additions & 4 deletions apps/explorer/lib/explorer/smart_contract/verifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Explorer.SmartContract.Verifier do
constructor_arguments = Map.get(params, "constructor_arguments", "")
evm_version = Map.get(params, "evm_version")
optimization_runs = Map.get(params, "optimization_runs", 200)
autodetect_contructor_arguments = params |> Map.get("autodetect_contructor_args", "false") |> parse_boolean()

solc_output =
CodeCompiler.run(
Expand All @@ -53,13 +54,18 @@ defmodule Explorer.SmartContract.Verifier do
external_libs: external_libraries
)

compare_bytecodes(solc_output, address_hash, constructor_arguments)
compare_bytecodes(solc_output, address_hash, constructor_arguments, autodetect_contructor_arguments)
end

defp compare_bytecodes({:error, :name}, _, _), do: {:error, :name}
defp compare_bytecodes({:error, _}, _, _), do: {:error, :compilation}
defp compare_bytecodes({:error, :name}, _, _, _), do: {:error, :name}
defp compare_bytecodes({:error, _}, _, _, _), do: {:error, :compilation}

defp compare_bytecodes({:ok, %{"abi" => abi, "bytecode" => bytecode}}, address_hash, arguments_data) do
defp compare_bytecodes(
{:ok, %{"abi" => abi, "bytecode" => bytecode}},
address_hash,
arguments_data,
autodetect_contructor_arguments
) do
generated_bytecode = extract_bytecode(bytecode)

"0x" <> blockchain_bytecode =
Expand All @@ -73,6 +79,15 @@ defmodule Explorer.SmartContract.Verifier do
!try_library_verification(generated_bytecode, blockchain_bytecode_without_whisper) ->
{:error, :generated_bytecode}

has_constructor_with_params?(abi) && autodetect_contructor_arguments ->
result = ConstructorArguments.find_constructor_arguments(address_hash, abi)

if result do
{:ok, %{abi: abi, contructor_arguments: result}}
else
{:error, :constructor_arguments}
end

has_constructor_with_params?(abi) &&
!ConstructorArguments.verify(address_hash, blockchain_bytecode_without_whisper, arguments_data) ->
{:error, :constructor_arguments}
Expand Down Expand Up @@ -162,4 +177,7 @@ defmodule Explorer.SmartContract.Verifier do
defp has_constructor_with_params?(abi) do
Enum.any?(abi, fn el -> el["type"] == "constructor" && el["inputs"] != [] end)
end

defp parse_boolean("true"), do: true
defp parse_boolean("false"), do: false
end
Loading