fix verifier, add tests

pull/1414/head
Ayrat Badykov 6 years ago
parent 0f3c7585c8
commit 64764d6c8f
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 3
      apps/explorer/lib/explorer/chain.ex
  2. 2
      apps/explorer/lib/explorer/smart_contract/verifier.ex
  3. 51
      apps/explorer/test/explorer/smart_contract/verifier_test.exs

@ -1892,7 +1892,8 @@ defmodule Explorer.Chain do
!is_nil(transaction.contracts_creation_transaction) ->
Data.to_string(transaction.contracts_creation_transaction.input)
true -> ""
true ->
""
end
end

@ -44,7 +44,7 @@ defmodule Explorer.SmartContract.Verifier do
generated_bytecode != blockchain_bytecode ->
{:error, :generated_bytecode}
!ConstructorArguments.verify(address_hash, bytecode, arguments_data) ->
!ConstructorArguments.verify(address_hash, blockchain_bytecode, arguments_data) ->
{:error, :constructor_arguments}
true ->

@ -56,6 +56,57 @@ defmodule Explorer.SmartContract.VerifierTest do
assert abi != nil
end
test "verifies smart contract with constructor arguments", %{
contract_code_info: contract_code_info
} do
contract_address = insert(:contract_address, contract_code: contract_code_info.bytecode)
constructor_arguments = "0102030405"
params = %{
"contract_source_code" => contract_code_info.source_code,
"compiler_version" => contract_code_info.version,
"name" => contract_code_info.name,
"optimization" => contract_code_info.optimized,
"constructor_arguments" => constructor_arguments
}
:transaction
|> insert(
created_contract_address_hash: contract_address.hash,
input: Verifier.extract_bytecode(contract_code_info.bytecode) <> constructor_arguments
)
|> with_block()
assert {:ok, %{abi: abi}} = Verifier.evaluate_authenticity(contract_address.hash, params)
assert abi != nil
end
test "returns error when constructor arguments do not match", %{
contract_code_info: contract_code_info
} do
contract_address = insert(:contract_address, contract_code: contract_code_info.bytecode)
constructor_arguments = "0102030405"
params = %{
"contract_source_code" => contract_code_info.source_code,
"compiler_version" => contract_code_info.version,
"name" => contract_code_info.name,
"optimization" => contract_code_info.optimized,
"constructor_arguments" => constructor_arguments
}
:transaction
|> insert(
created_contract_address_hash: contract_address.hash,
input: Verifier.extract_bytecode(contract_code_info.bytecode) <> "010203"
)
|> with_block()
assert {:error, :constructor_arguments} = Verifier.evaluate_authenticity(contract_address.hash, params)
end
test "returns error when bytecode doesn't match", %{contract_code_info: contract_code_info} do
contract_address = insert(:contract_address, contract_code: contract_code_info.bytecode)

Loading…
Cancel
Save