Skip to content

Commit

Permalink
Merge branch 'master' into feature/#1476-add-styles-for-POSDAO-network
Browse files Browse the repository at this point in the history
* master: (28 commits)
  feat: verify contracts via an RPC endpoint
  Remove obsolete InvalidConsensus.Worker
  Discard child block with parent_hash not matching hash of imported block
  Expand non-consensus block regression test to test for race conditions
  Remove obsolete ConsensusEnsurer
  feat: add not_decompiled_with_version filter
  Update fetcher_test.exs
  mix format
  fix test
  update gettext
  fix build
  remove fetching token 2 times
  update CHANGELOG
  update tokens in fetcher
  add CHANGELOG entry
  exclude decompiled smart contract from encoding
  add CHANGELOG entry
  update metadata in controller
  add CHANGELOG entry
  decrease token metadata update interval
  ...
  • Loading branch information
gabitoesmiapodo committed Apr 4, 2019
2 parents 64c3ccd + c634172 commit ee635fa
Show file tree
Hide file tree
Showing 33 changed files with 714 additions and 468 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,12 +2,18 @@

### Features

- [1654](https://github.com/poanetwork/blockscout/pull/1654) - add decompiled code tab
- [1661](https://github.com/poanetwork/blockscout/pull/1661) - try to compile smart contract with the latest evm version
- [#1662](https://github.com/poanetwork/blockscout/pull/1662) - allow specifying number of optimization runs
- [#1654](https://github.com/poanetwork/blockscout/pull/1654) - add decompiled code tab
- [#1661](https://github.com/poanetwork/blockscout/pull/1661) - try to compile smart contract with the latest evm version
- [#1665](https://github.com/poanetwork/blockscout/pull/1665) - Add contract verification RPC endpoint.

### Fixes

- [#1669](https://github.com/poanetwork/blockscout/pull/1669) - do not fail if multiple matching tokens are found
- [#1691](https://github.com/poanetwork/blockscout/pull/1691) - decrease token metadata update interval
- [#1688](https://github.com/poanetwork/blockscout/pull/1688) - do not fail if failure reason is atom
- [#1692](https://github.com/poanetwork/blockscout/pull/1692) - exclude decompiled smart contract from encoding
- [#1684](https://github.com/poanetwork/blockscout/pull/1684) - Discard child block with parent_hash not matching hash of imported block

### Chore

Expand Down
Expand Up @@ -26,10 +26,14 @@ defmodule BlockScoutWeb.AddressContractVerificationController do
"address_id" => address_hash_string,
"smart_contract" => smart_contract,
"external_libraries" => external_libraries,
"evm_version" => evm_version
"evm_version" => evm_version,
"optimization" => optimization
}
) do
smart_sontact_with_evm_version = Map.put(smart_contract, "evm_version", evm_version["evm_version"])
smart_sontact_with_evm_version =
smart_contract
|> Map.put("evm_version", evm_version["evm_version"])
|> Map.put("optimization_runs", parse_optimization_runs(optimization))

case Publisher.publish(address_hash_string, smart_sontact_with_evm_version, external_libraries) do
{:ok, _smart_contract} ->
Expand All @@ -45,4 +49,11 @@ defmodule BlockScoutWeb.AddressContractVerificationController do
)
end
end

def parse_optimization_runs(%{"runs" => runs}) do
case Integer.parse(runs) do
{integer, ""} -> integer
_ -> 200
end
end
end
Expand Up @@ -4,10 +4,28 @@ defmodule BlockScoutWeb.API.RPC.ContractController do
alias BlockScoutWeb.API.RPC.Helpers
alias Explorer.Chain
alias Explorer.Chain.SmartContract
alias Explorer.SmartContract.Publisher

def verify(conn, %{"addressHash" => address_hash} = params) do
with {:params, {:ok, fetched_params}} <- {:params, fetch_verify_params(params)},
{:params, external_libraries} <-
{:params, fetch_external_libraries(params)},
{:publish, {:ok, smart_contract}} <-
{:publish, Publisher.publish(address_hash, fetched_params, external_libraries)},
preloaded_smart_contract <- SmartContract.preload_decompiled_smart_contract(smart_contract) do
render(conn, :verify, %{contract: preloaded_smart_contract, address_hash: address_hash})
else
{:publish, _} ->
render(conn, :error, error: "Something went wrong while publishing the contract.")

{:params, {:error, error}} ->
render(conn, :error, error: error)
end
end

def listcontracts(conn, params) do
with pagination_options <- Helpers.put_pagination_options(%{}, params),
{:params, {:ok, options}} <- {:params, add_filter(pagination_options, params)} do
{:params, {:ok, options}} <- {:params, add_filters(pagination_options, params)} do
options_with_defaults =
options
|> Map.put_new(:page_number, 0)
Expand Down Expand Up @@ -71,7 +89,8 @@ defmodule BlockScoutWeb.API.RPC.ContractController do
Chain.list_verified_contracts(page_size, offset)

:decompiled ->
Chain.list_decompiled_contracts(page_size, offset)
not_decompiled_with_version = Map.get(opts, :not_decompiled_with_version)
Chain.list_decompiled_contracts(page_size, offset, not_decompiled_with_version)

:unverified ->
Chain.list_unverified_contracts(page_size, offset)
Expand All @@ -84,6 +103,12 @@ defmodule BlockScoutWeb.API.RPC.ContractController do
end
end

defp add_filters(options, params) do
options
|> add_filter(params)
|> add_not_decompiled_with_version(params)
end

defp add_filter(options, params) do
with {:param, {:ok, value}} <- {:param, Map.fetch(params, "filter")},
{:validation, {:ok, filter}} <- {:validation, contracts_filter(value)} do
Expand All @@ -94,6 +119,17 @@ defmodule BlockScoutWeb.API.RPC.ContractController do
end
end

defp add_not_decompiled_with_version({:ok, options}, params) do
case Map.fetch(params, "not_decompiled_with_version") do
{:ok, value} -> {:ok, Map.put(options, :not_decompiled_with_version, value)}
:error -> {:ok, options}
end
end

defp add_not_decompiled_with_version(options, _params) do
options
end

defp contracts_filter(nil), do: {:ok, nil}
defp contracts_filter(1), do: {:ok, :verified}
defp contracts_filter(2), do: {:ok, :decompiled}
Expand Down Expand Up @@ -137,4 +173,71 @@ defmodule BlockScoutWeb.API.RPC.ContractController do

{:contract, result}
end

defp fetch_verify_params(params) do
{:ok, %{}}
|> required_param(params, "addressHash", "address_hash")
|> required_param(params, "name", "name")
|> required_param(params, "compilerVersion", "compiler_version")
|> required_param(params, "optimization", "optimization")
|> required_param(params, "contractSourceCode", "contract_source_code")
|> optional_param(params, "evmVersion", "evm_version")
|> optional_param(params, "constructorArguments", "constructor_arguments")
|> optional_param(params, "optimizationRuns", "optimization_runs")
|> parse_optimization_runs()
end

defp parse_optimization_runs({:ok, %{"optimization_runs" => runs} = opts}) when is_bitstring(runs) do
{:ok, Map.put(opts, "optimization_runs", 200)}
end

defp parse_optimization_runs({:ok, %{"optimization_runs" => runs} = opts}) when is_integer(runs) do
{:ok, opts}
end

defp parse_optimization_runs({:ok, opts}) do
{:ok, Map.put(opts, "optimization_runs", 200)}
end

defp parse_optimization_runs(other), do: other

defp fetch_external_libraries(params) do
Enum.reduce(1..5, %{}, fn number, acc ->
case Map.fetch(params, "library#{number}Name") do
{:ok, library_name} ->
library_address = Map.get(params, "library#{number}Address")

acc
|> Map.put("library#{number}_name", library_name)
|> Map.put("library#{number}_address", library_address)

:error ->
acc
end
end)
end

defp required_param({:error, _} = error, _, _, _), do: error

defp required_param({:ok, map}, params, key, new_key) do
case Map.fetch(params, key) do
{:ok, value} ->
{:ok, Map.put(map, new_key, value)}

:error ->
{:error, "#{key} is required."}
end
end

defp optional_param({:error, _} = error, _, _, _), do: error

defp optional_param({:ok, map}, params, key, new_key) do
case Map.fetch(params, key) do
{:ok, value} ->
{:ok, Map.put(map, new_key, value)}

:error ->
{:ok, map}
end
end
end
Expand Up @@ -66,6 +66,7 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do
def call_controller(conn, controller, action) do
{:ok, controller.call(conn, action)}
rescue
Conn.WrapperError -> :error
Conn.WrapperError ->
:error
end
end

0 comments on commit ee635fa

Please sign in to comment.