Fix unhandled error; Add regression tests; Add missing existance smart-contracts checks

pull/6642/head
Никита Поздняков 2 years ago
parent 61b3ce5164
commit ec87839a20
No known key found for this signature in database
GPG Key ID: F344106F9804FE5F
  1. 18
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/smart_contract_controller.ex
  2. 57
      apps/block_scout_web/test/block_scout_web/controllers/api/v2/smart_contract_controller_test.exs

@ -5,6 +5,7 @@ defmodule BlockScoutWeb.API.V2.SmartContractController do
alias BlockScoutWeb.{AccessHelpers, AddressView} alias BlockScoutWeb.{AccessHelpers, AddressView}
alias BlockScoutWeb.AddressContractVerificationController, as: VerificationController alias BlockScoutWeb.AddressContractVerificationController, as: VerificationController
alias Ecto.Association.NotLoaded
alias Explorer.Chain alias Explorer.Chain
alias Explorer.Chain.SmartContract alias Explorer.Chain.SmartContract
alias Explorer.SmartContract.{Reader, Writer} alias Explorer.SmartContract.{Reader, Writer}
@ -90,7 +91,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractController do
with {:format, {:ok, address_hash}} <- {:format, Chain.string_to_address_hash(address_hash_string)}, with {:format, {:ok, address_hash}} <- {:format, Chain.string_to_address_hash(address_hash_string)},
{:ok, false} <- AccessHelpers.restricted_access?(address_hash_string, params), {:ok, false} <- AccessHelpers.restricted_access?(address_hash_string, params),
{:not_found, {:ok, address}} <- {:not_found, {:ok, address}} <-
{:not_found, Chain.find_contract_address(address_hash, @smart_contract_address_options)} do {:not_found, Chain.find_contract_address(address_hash, @smart_contract_address_options)},
{:not_found, false} <- {:not_found, is_nil(address.smart_contract)} do
implementation_address_hash_string = implementation_address_hash_string =
address.smart_contract address.smart_contract
|> SmartContract.get_implementation_address_hash() |> SmartContract.get_implementation_address_hash()
@ -109,7 +111,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractController do
with {:format, {:ok, address_hash}} <- {:format, Chain.string_to_address_hash(address_hash_string)}, with {:format, {:ok, address_hash}} <- {:format, Chain.string_to_address_hash(address_hash_string)},
{:ok, false} <- AccessHelpers.restricted_access?(address_hash_string, params), {:ok, false} <- AccessHelpers.restricted_access?(address_hash_string, params),
{:not_found, {:ok, address}} <- {:not_found, {:ok, address}} <-
{:not_found, Chain.find_contract_address(address_hash, @smart_contract_address_options)} do {:not_found, Chain.find_contract_address(address_hash, @smart_contract_address_options)},
{:not_found, false} <- {:not_found, is_nil(address.smart_contract)} do
implementation_address_hash_string = implementation_address_hash_string =
address.smart_contract address.smart_contract
|> SmartContract.get_implementation_address_hash() |> SmartContract.get_implementation_address_hash()
@ -133,7 +136,16 @@ defmodule BlockScoutWeb.API.V2.SmartContractController do
with {:format, {:ok, address_hash}} <- {:format, Chain.string_to_address_hash(address_hash_string)}, with {:format, {:ok, address_hash}} <- {:format, Chain.string_to_address_hash(address_hash_string)},
{:ok, false} <- AccessHelpers.restricted_access?(address_hash_string, params), {:ok, false} <- AccessHelpers.restricted_access?(address_hash_string, params),
{:not_found, {:ok, _address}} <- {:not_found, Chain.find_contract_address(address_hash, [])} do {:not_found, {:ok, address}} <-
{:not_found,
Chain.find_contract_address(address_hash,
necessity_by_association: %{
:smart_contract => :optional
}
)},
{:not_found, true} <-
{:not_found,
!is_nil(custom_abi) || (address.smart_contract && !match?(%NotLoaded{}, address.smart_contract))} do
%{output: output, names: names} = %{output: output, names: names} =
if custom_abi do if custom_abi do
Reader.query_function_with_names_custom_abi( Reader.query_function_with_names_custom_abi(

@ -122,6 +122,13 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422) assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422)
end end
test "return 404 on unverified contract", %{conn: conn} do
address = insert(:contract_address)
request = get(conn, "/api/v2/smart-contracts/#{Address.checksum(address.hash)}/methods-read")
assert %{"message" => "Not found"} = json_response(request, 404)
end
test "get read-methods", %{conn: conn} do test "get read-methods", %{conn: conn} do
abi = [ abi = [
%{ %{
@ -206,6 +213,19 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422) assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422)
end end
test "return 404 on unverified contract", %{conn: conn} do
address = insert(:contract_address)
request =
post(conn, "/api/v2/smart-contracts/#{Address.checksum(address.hash)}/query-read-method", %{
"contract_type" => "regular",
"args" => ["0xfffffffffffffffffffffffffffffffffffffffe"],
"method_id" => "c683630d"
})
assert %{"message" => "Not found"} = json_response(request, 404)
end
test "query-read-method", %{conn: conn} do test "query-read-method", %{conn: conn} do
abi = [ abi = [
%{ %{
@ -231,6 +251,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
} }
] ]
blockchain_get_code_mock()
expect( expect(
EthereumJSONRPC.Mox, EthereumJSONRPC.Mox,
:json_rpc, :json_rpc,
@ -295,6 +317,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
} }
] ]
blockchain_get_code_mock()
expect( expect(
EthereumJSONRPC.Mox, EthereumJSONRPC.Mox,
:json_rpc, :json_rpc,
@ -349,6 +373,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
} }
] ]
blockchain_get_code_mock()
expect( expect(
EthereumJSONRPC.Mox, EthereumJSONRPC.Mox,
:json_rpc, :json_rpc,
@ -402,6 +428,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
} }
] ]
blockchain_get_code_mock()
expect( expect(
EthereumJSONRPC.Mox, EthereumJSONRPC.Mox,
:json_rpc, :json_rpc,
@ -447,6 +475,13 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422) assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422)
end end
test "return 404 on unverified contract", %{conn: conn} do
address = insert(:contract_address)
request = get(conn, "/api/v2/smart-contracts/#{Address.checksum(address.hash)}/methods-write")
assert %{"message" => "Not found"} = json_response(request, 404)
end
test "get write-methods", %{conn: conn} do test "get write-methods", %{conn: conn} do
abi = [ abi = [
%{ %{
@ -704,6 +739,13 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422) assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422)
end end
test "return 404 on unverified contract", %{conn: conn} do
address = insert(:contract_address)
request = get(conn, "/api/v2/smart-contracts/#{Address.checksum(address.hash)}/methods-read-proxy")
assert %{"message" => "Not found"} = json_response(request, 404)
end
test "get read-methods", %{conn: conn} do test "get read-methods", %{conn: conn} do
abi = [ abi = [
%{ %{
@ -843,6 +885,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
target_contract = insert(:smart_contract, abi: abi) target_contract = insert(:smart_contract, abi: abi)
blockchain_get_code_mock()
expect(EthereumJSONRPC.Mox, :json_rpc, fn %{ expect(EthereumJSONRPC.Mox, :json_rpc, fn %{
id: 0, id: 0,
method: "eth_getStorageAt", method: "eth_getStorageAt",
@ -928,6 +972,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
target_contract = insert(:smart_contract, abi: abi) target_contract = insert(:smart_contract, abi: abi)
blockchain_get_code_mock()
expect(EthereumJSONRPC.Mox, :json_rpc, fn %{ expect(EthereumJSONRPC.Mox, :json_rpc, fn %{
id: 0, id: 0,
method: "eth_getStorageAt", method: "eth_getStorageAt",
@ -997,6 +1043,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
target_contract = insert(:smart_contract, abi: abi) target_contract = insert(:smart_contract, abi: abi)
blockchain_get_code_mock()
expect(EthereumJSONRPC.Mox, :json_rpc, fn %{ expect(EthereumJSONRPC.Mox, :json_rpc, fn %{
id: 0, id: 0,
method: "eth_getStorageAt", method: "eth_getStorageAt",
@ -1065,6 +1113,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
target_contract = insert(:smart_contract, abi: abi) target_contract = insert(:smart_contract, abi: abi)
blockchain_get_code_mock()
expect(EthereumJSONRPC.Mox, :json_rpc, fn %{ expect(EthereumJSONRPC.Mox, :json_rpc, fn %{
id: 0, id: 0,
method: "eth_getStorageAt", method: "eth_getStorageAt",
@ -1123,6 +1173,13 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422) assert %{"message" => "Invalid parameter(s)"} = json_response(request, 422)
end end
test "return 404 on unverified contract", %{conn: conn} do
address = insert(:contract_address)
request = get(conn, "/api/v2/smart-contracts/#{Address.checksum(address.hash)}/methods-write-proxy")
assert %{"message" => "Not found"} = json_response(request, 404)
end
test "get write-methods", %{conn: conn} do test "get write-methods", %{conn: conn} do
abi = [ abi = [
%{ %{

Loading…
Cancel
Save