verify smart contracts with evm versions

pull/1547/head
Ayrat Badykov 6 years ago
parent 24350c1192
commit 876f1bf9e5
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 7
      apps/block_scout_web/lib/block_scout_web/controllers/address_contract_verification_controller.ex
  2. 5
      apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex
  3. 2018
      apps/block_scout_web/test/support/fixture/smart_contract/solc_bin.json
  4. 13
      apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex
  5. 4
      apps/explorer/lib/explorer/smart_contract/verifier.ex
  6. 3
      apps/explorer/priv/compile_solc.js
  7. 46
      apps/explorer/test/explorer/smart_contract/solidity/code_compiler_test.exs

@ -21,10 +21,13 @@ defmodule BlockScoutWeb.AddressContractVerificationController do
%{ %{
"address_id" => address_hash_string, "address_id" => address_hash_string,
"smart_contract" => smart_contract, "smart_contract" => smart_contract,
"external_libraries" => external_libraries "external_libraries" => external_libraries,
"evm_version" => evm_version
} }
) do ) do
case Publisher.publish(address_hash_string, smart_contract, external_libraries) do smart_sontact_with_evm_version = Map.put(smart_contract, "evm_version", evm_version)
case Publisher.publish(address_hash_string, smart_sontact_with_evm_version, external_libraries) do
{:ok, _smart_contract} -> {:ok, _smart_contract} ->
redirect(conn, to: address_contract_path(conn, :index, address_hash_string)) redirect(conn, to: address_contract_path(conn, :index, address_hash_string))

@ -27,6 +27,11 @@
<%= error_tag f, :compiler_version, id: "compiler-help-block", class: "text-danger" %> <%= error_tag f, :compiler_version, id: "compiler-help-block", class: "text-danger" %>
</div> </div>
<div class="form-group">
<%= label :evm_version, :evm_version, gettext("EVM Vesion") %>
<%= text_input :evm_version, :evmversion, class: "form-control", "aria-describedby": "contract-name-help-block" %>
</div>
<div class="form-group mb-4"> <div class="form-group mb-4">
<%= label f, "Optimization" %> <%= label f, "Optimization" %>

@ -4,6 +4,7 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do
""" """
@new_contract_name "New.sol" @new_contract_name "New.sol"
@allowed_evm_versions ["homestead", "tangerineWhistle", "spuriousDragon", "byzantium", "constantinople"]
@doc """ @doc """
Compiles a code in the solidity command line. Compiles a code in the solidity command line.
@ -60,9 +61,16 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do
} }
} }
""" """
def run(name, compiler_version, code, optimize, external_libs \\ %{}) do def run(name, compiler_version, code, optimize, evm_version \\ "byzantium", external_libs \\ %{}) do
external_libs_string = Jason.encode!(external_libs) external_libs_string = Jason.encode!(external_libs)
evm_version =
if evm_version in @allowed_evm_versions do
evm_version
else
"byzantium"
end
{response, _status} = {response, _status} =
System.cmd( System.cmd(
"node", "node",
@ -72,7 +80,8 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do
compiler_version, compiler_version,
optimize_value(optimize), optimize_value(optimize),
@new_contract_name, @new_contract_name,
external_libs_string external_libs_string,
evm_version
] ]
) )

@ -23,8 +23,10 @@ defmodule Explorer.SmartContract.Verifier do
compiler_version = Map.fetch!(params, "compiler_version") compiler_version = Map.fetch!(params, "compiler_version")
external_libraries = Map.get(params, "external_libraries", %{}) external_libraries = Map.get(params, "external_libraries", %{})
constructor_arguments = Map.get(params, "constructor_arguments", "") constructor_arguments = Map.get(params, "constructor_arguments", "")
evm_version = Map.get(params, "evm_version", "byzantium")
solc_output = CodeCompiler.run(name, compiler_version, contract_source_code, optimization, external_libraries) solc_output =
CodeCompiler.run(name, compiler_version, contract_source_code, optimization, evm_version, external_libraries)
compare_bytecodes(solc_output, address_hash, constructor_arguments) compare_bytecodes(solc_output, address_hash, constructor_arguments)
end end

@ -7,6 +7,7 @@ var version = process.argv[3];
var optimize = process.argv[4]; var optimize = process.argv[4];
var newContractName = process.argv[5]; var newContractName = process.argv[5];
var externalLibraries = JSON.parse(process.argv[6]) var externalLibraries = JSON.parse(process.argv[6])
var evmVersion = process.argv[7]
var compiled_code = solc.loadRemoteVersion(version, function (err, solcSnapshot) { var compiled_code = solc.loadRemoteVersion(version, function (err, solcSnapshot) {
if (err) { if (err) {
@ -20,7 +21,7 @@ var compiled_code = solc.loadRemoteVersion(version, function (err, solcSnapshot)
} }
}, },
settings: { settings: {
evmVersion: 'byzantium', evmVersion: evmVersion,
optimizer: { optimizer: {
enabled: optimize == '1', enabled: optimize == '1',
runs: 200 runs: 200

@ -65,6 +65,7 @@ defmodule Explorer.SmartContract.Solidity.CodeCompilerTest do
compiler_version, compiler_version,
contract, contract,
optimize, optimize,
"byzantium",
external_libraries external_libraries
) )
@ -75,7 +76,50 @@ defmodule Explorer.SmartContract.Solidity.CodeCompilerTest do
end) end)
end end
test "compile in an older solidity version" do test "compiles with constantinople evm version" do
optimize = false
name = "MyTest"
code = """
pragma solidity 0.5.2;
contract MyTest {
constructor() public {
}
mapping(address => bytes32) public myMapping;
function contractHash(address _addr) public {
bytes32 hash;
assembly { hash := extcodehash(_addr) }
myMapping[_addr] = hash;
}
function justHash(bytes memory _bytes)
public
pure
returns (bytes32)
{
return keccak256(_bytes);
}
}
"""
version = "v0.5.2+commit.1df8f40c"
evm_version = "constantinople"
response = CodeCompiler.run(name, version, code, optimize, evm_version)
assert {:ok,
%{
"abi" => _,
"bytecode" => _,
"name" => _
}} = response
end
test "compiles in an older solidity version" do
optimize = false optimize = false
name = "SimpleStorage" name = "SimpleStorage"

Loading…
Cancel
Save