Merge pull request #2964 from poanetwork/vb-fix-bug-in-constructor-args-in-verification

Fix bug in skipping of constructor arguments in contract verification
pull/2969/head
Victor Baranov 5 years ago committed by GitHub
commit 677a0fba8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 2
      apps/block_scout_web/test/block_scout_web/controllers/api/rpc/contract_controller_test.exs
  3. 2
      apps/block_scout_web/test/block_scout_web/controllers/api/v1/verified_smart_contract_controller_test.exs
  4. 13
      apps/block_scout_web/test/support/fixture/smart_contract/contract_with_lib.json
  5. 5
      apps/explorer/lib/explorer/smart_contract/verifier.ex
  6. 2
      apps/explorer/test/explorer/smart_contract/publisher_test.exs
  7. 44
      apps/explorer/test/explorer/smart_contract/verifier_test.exs
  8. 13
      apps/explorer/test/support/fixture/smart_contract/contract_with_lib.json

@ -6,6 +6,7 @@
- [#2918](https://github.com/poanetwork/blockscout/pull/2918) - Add tokenID for tokentx API action explicitly - [#2918](https://github.com/poanetwork/blockscout/pull/2918) - Add tokenID for tokentx API action explicitly
### Fixes ### Fixes
- [#2964](https://github.com/poanetwork/blockscout/pull/2964) - Fix bug in skipping of constructor arguments in contract verification
- [#2961](https://github.com/poanetwork/blockscout/pull/2961) - Add a guard that addresses is enum in `values` function in `read contract` page - [#2961](https://github.com/poanetwork/blockscout/pull/2961) - Add a guard that addresses is enum in `values` function in `read contract` page
- [#2960](https://github.com/poanetwork/blockscout/pull/2960) - Add BLOCKSCOUT_HOST to docker setup - [#2960](https://github.com/poanetwork/blockscout/pull/2960) - Add BLOCKSCOUT_HOST to docker setup
- [#2956](https://github.com/poanetwork/blockscout/pull/2956) - Add support of 0.6.x version of compiler - [#2956](https://github.com/poanetwork/blockscout/pull/2956) - Add support of 0.6.x version of compiler

@ -647,7 +647,7 @@ defmodule BlockScoutWeb.API.RPC.ContractControllerTest do
test "with external libraries", %{conn: conn} do test "with external libraries", %{conn: conn} do
contract_data = contract_data =
"#{File.cwd!()}/test/support/fixture/smart_contract/compiler_tests.json" "#{File.cwd!()}/test/support/fixture/smart_contract/contract_with_lib.json"
|> File.read!() |> File.read!()
|> Jason.decode!() |> Jason.decode!()
|> List.first() |> List.first()

@ -28,7 +28,7 @@ defmodule BlockScoutWeb.API.V1.VerifiedControllerTest do
test "verifying a smart contract with external libraries", %{conn: conn} do test "verifying a smart contract with external libraries", %{conn: conn} do
contract_data = contract_data =
"#{File.cwd!()}/test/support/fixture/smart_contract/compiler_tests.json" "#{File.cwd!()}/test/support/fixture/smart_contract/contract_with_lib.json"
|> File.read!() |> File.read!()
|> Jason.decode!() |> Jason.decode!()
|> List.first() |> List.first()

@ -0,0 +1,13 @@
[
{
"compiler_version": "v0.5.11+commit.c082d0b4",
"contract": "pragma solidity 0.5.11;library BadSafeMath { function add(uint256 a, uint256 b) public pure returns (uint256) { uint256 c = a + 2 * b; require(c >= a, \"SafeMath: addition overflow\"); return c; }}contract SimpleStorage { uint256 storedData = 10; using BadSafeMath for uint256; function increment(uint256 x) public { storedData = storedData.add(x); } function set(uint256 x) public { storedData = x; } function get() public view returns (uint256) { return storedData; }}",
"expected_bytecode": "608060405234801561001057600080fd5b50600436106100415760003560e01c806360fe47b1146100465780636d4ce63c146100655780637cf5dab01461007f575b600080fd5b6100636004803603602081101561005c57600080fd5b503561009c565b005b61006d6100a1565b60408051918252519081900360200190f35b6100636004803603602081101561009557600080fd5b50356100a7565b600055565b60005490565b600054733662e222908fa35f013bee37695d0510098b6d7363771602f79091836040518363ffffffff1660e01b8152600401808381526020018281526020019250505060206040518083038186803b15801561010257600080fd5b505af4158015610116573d6000803e3d6000fd5b505050506040513d602081101561012c57600080fd5b50516000555056fea265627a7a723158203e59bfb9a5a2e55d38231922c86d8b2ec9b66cb2f6595613674bc4e15290b60764736f6c634300050b0032",
"external_libraries": {
"BadSafeMath": "0x3662e222908fa35f013bee37695d0510098b6d73"
},
"name": "SimpleStorage",
"optimize": true
}
]

@ -60,6 +60,7 @@ defmodule Explorer.SmartContract.Verifier do
defp compare_bytecodes({:error, :name}, _, _, _), do: {:error, :name} defp compare_bytecodes({:error, :name}, _, _, _), do: {:error, :name}
defp compare_bytecodes({:error, _}, _, _, _), do: {:error, :compilation} defp compare_bytecodes({:error, _}, _, _, _), do: {:error, :compilation}
# credo:disable-for-next-line /Complexity/
defp compare_bytecodes( defp compare_bytecodes(
{:ok, %{"abi" => abi, "bytecode" => bytecode}}, {:ok, %{"abi" => abi, "bytecode" => bytecode}},
address_hash, address_hash,
@ -73,6 +74,7 @@ defmodule Explorer.SmartContract.Verifier do
|> Chain.smart_contract_bytecode() |> Chain.smart_contract_bytecode()
blockchain_bytecode_without_whisper = extract_bytecode(blockchain_bytecode) blockchain_bytecode_without_whisper = extract_bytecode(blockchain_bytecode)
empty_constructor_arguments = arguments_data == "" or arguments_data == nil
cond do cond do
generated_bytecode != blockchain_bytecode_without_whisper && generated_bytecode != blockchain_bytecode_without_whisper &&
@ -88,6 +90,9 @@ defmodule Explorer.SmartContract.Verifier do
{:error, :constructor_arguments} {:error, :constructor_arguments}
end end
has_constructor_with_params?(abi) && empty_constructor_arguments ->
{:error, :constructor_arguments}
has_constructor_with_params?(abi) && has_constructor_with_params?(abi) &&
!ConstructorArguments.verify(address_hash, blockchain_bytecode_without_whisper, arguments_data) -> !ConstructorArguments.verify(address_hash, blockchain_bytecode_without_whisper, arguments_data) ->
{:error, :constructor_arguments} {:error, :constructor_arguments}

@ -131,7 +131,7 @@ defmodule Explorer.SmartContract.PublisherTest do
test "validates and creates smart contract with external libraries" do test "validates and creates smart contract with external libraries" do
contract_data = contract_data =
"#{File.cwd!()}/test/support/fixture/smart_contract/compiler_tests.json" "#{File.cwd!()}/test/support/fixture/smart_contract/contract_with_lib.json"
|> File.read!() |> File.read!()
|> Jason.decode!() |> Jason.decode!()
|> List.first() |> List.first()

File diff suppressed because one or more lines are too long

@ -0,0 +1,13 @@
[
{
"compiler_version": "v0.5.11+commit.c082d0b4",
"contract": "pragma solidity 0.5.11;library BadSafeMath { function add(uint256 a, uint256 b) public pure returns (uint256) { uint256 c = a + 2 * b; require(c >= a, \"SafeMath: addition overflow\"); return c; }}contract SimpleStorage { uint256 storedData = 10; using BadSafeMath for uint256; function increment(uint256 x) public { storedData = storedData.add(x); } function set(uint256 x) public { storedData = x; } function get() public view returns (uint256) { return storedData; }}",
"expected_bytecode": "608060405234801561001057600080fd5b50600436106100415760003560e01c806360fe47b1146100465780636d4ce63c146100655780637cf5dab01461007f575b600080fd5b6100636004803603602081101561005c57600080fd5b503561009c565b005b61006d6100a1565b60408051918252519081900360200190f35b6100636004803603602081101561009557600080fd5b50356100a7565b600055565b60005490565b600054733662e222908fa35f013bee37695d0510098b6d7363771602f79091836040518363ffffffff1660e01b8152600401808381526020018281526020019250505060206040518083038186803b15801561010257600080fd5b505af4158015610116573d6000803e3d6000fd5b505050506040513d602081101561012c57600080fd5b50516000555056fea265627a7a723158203e59bfb9a5a2e55d38231922c86d8b2ec9b66cb2f6595613674bc4e15290b60764736f6c634300050b0032",
"external_libraries": {
"BadSafeMath": "0x3662e222908fa35f013bee37695d0510098b6d73"
},
"name": "SimpleStorage",
"optimize": true
}
]
Loading…
Cancel
Save