From ee814636b55a793f686407d20db91f23367fdcf9 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 7 Feb 2019 16:45:56 +0300 Subject: [PATCH 1/7] pass external libraries to the Solidity compiler --- .../smart_contract/solidity/code_compiler.ex | 19 +++++-------------- apps/explorer/priv/compile_solc.js | 4 ++++ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex b/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex index cb23687cda..e9b01836c4 100644 --- a/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex +++ b/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex @@ -61,6 +61,8 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do } """ def run(name, compiler_version, code, optimize, external_libs \\ %{}) do + external_libs_string = Jason.encode!(external_libs) + {response, _status} = System.cmd( "node", @@ -69,16 +71,15 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do code, compiler_version, optimize_value(optimize), - @new_contract_name + @new_contract_name, + external_libs_string ] ) with {:ok, contracts} <- Jason.decode(response), %{"abi" => abi, "evm" => %{"deployedBytecode" => %{"object" => bytecode}}} <- get_contract_info(contracts, name) do - bytecode_with_libraries = add_library_addresses(bytecode, external_libs) - - {:ok, %{"abi" => abi, "bytecode" => bytecode_with_libraries, "name" => name}} + {:ok, %{"abi" => abi, "bytecode" => bytecode, "name" => name}} else {:error, %Jason.DecodeError{}} -> {:error, :compilation} @@ -105,16 +106,6 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do end end - defp add_library_addresses(bytecode, external_libs) do - Enum.reduce(external_libs, bytecode, fn {library_name, address}, acc -> - placeholder = String.replace(@new_contract_name, ".", "\.") <> ":" <> library_name - regex = Regex.compile!("_+#{placeholder}_+") - address = String.replace(address, "0x", "") - - String.replace(acc, regex, address) - end) - end - def parse_error(%{"error" => error}), do: {:error, [error]} def parse_error(%{"errors" => errors}), do: {:error, errors} def parse_error({:error, _} = error), do: error diff --git a/apps/explorer/priv/compile_solc.js b/apps/explorer/priv/compile_solc.js index 17c2c8bd8e..3c4bd0b78b 100755 --- a/apps/explorer/priv/compile_solc.js +++ b/apps/explorer/priv/compile_solc.js @@ -6,6 +6,7 @@ var sourceCode = process.argv[2]; var version = process.argv[3]; var optimize = process.argv[4]; var newContractName = process.argv[5]; +var externalLibraries = JSON.parse(process.argv[6]) var compiled_code = solc.loadRemoteVersion(version, function (err, solcSnapshot) { if (err) { @@ -24,6 +25,9 @@ var compiled_code = solc.loadRemoteVersion(version, function (err, solcSnapshot) enabled: optimize == '1', runs: 200 }, + libraries: { + [newContractName]: externalLibraries + }, outputSelection: { '*': { '*': ['*'] From ff0db8c32b08e3b87dab3252f269781cbfd1834a Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 8 Feb 2019 14:58:42 +0300 Subject: [PATCH 2/7] add unique index on address_hash, block_hash and address_type for block_rewards --- .../20190208113202_add_unique_index_to_rewards.exs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs diff --git a/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs b/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs new file mode 100644 index 0000000000..cd306c7977 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs @@ -0,0 +1,12 @@ +defmodule Explorer.Repo.Migrations.AddUniqueIndexToRewards do + use Ecto.Migration + + def change do + create( + unique_index( + :block_rewards, + [:address_hash, :block_hash, :address_type] + ) + ) + end +end From 5cf4f860204b700ffc176ded668b33842885f561 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 8 Feb 2019 15:08:32 +0300 Subject: [PATCH 3/7] deduplicate input data --- .../lib/explorer/chain/import/runner/block/rewards.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex index 3ce55fbf07..2ada9eaa7d 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex @@ -53,7 +53,11 @@ defmodule Explorer.Chain.Import.Runner.Block.Rewards do on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0) # order so that row ShareLocks are grabbed in a consistent order - ordered_changes_list = Enum.sort_by(changes_list, &{&1.address_hash, &1.address_type, &1.block_hash}) + ordered_changes_list = + changes_list + |> Enum.uniq_by(&{&1.address_hash, &1.address_type, &1.block_hash}) + |> Enum.sort_by(&{&1.address_hash, &1.address_type, &1.block_hash}) + Import.insert_changes_list( repo, From b9bcd6ce98b1a9f2f0770ebe405d102e15803854 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 8 Feb 2019 15:10:50 +0300 Subject: [PATCH 4/7] mix format --- apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex index 2ada9eaa7d..c30a919b59 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex @@ -58,7 +58,6 @@ defmodule Explorer.Chain.Import.Runner.Block.Rewards do |> Enum.uniq_by(&{&1.address_hash, &1.address_type, &1.block_hash}) |> Enum.sort_by(&{&1.address_hash, &1.address_type, &1.block_hash}) - Import.insert_changes_list( repo, ordered_changes_list, From 537006d029f30421b0195dcccdd86ed8df84cada Mon Sep 17 00:00:00 2001 From: Brian McMichael Date: Fri, 8 Feb 2019 13:52:06 -0500 Subject: [PATCH 5/7] Optionally configure cert paths via envvar --- apps/block_scout_web/config/dev.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/block_scout_web/config/dev.exs b/apps/block_scout_web/config/dev.exs index 8feafba7cb..ae05490a86 100644 --- a/apps/block_scout_web/config/dev.exs +++ b/apps/block_scout_web/config/dev.exs @@ -11,8 +11,8 @@ config :block_scout_web, BlockScoutWeb.Endpoint, https: [ port: 4001, cipher_suite: :strong, - certfile: "priv/cert/selfsigned.pem", - keyfile: "priv/cert/selfsigned_key.pem" + certfile: System.get_env("CERTFILE") || "priv/cert/selfsigned.pem", + keyfile: System.get_env("KEYFILE") || "priv/cert/selfsigned_key.pem" ], debug_errors: true, code_reloader: true, From 122d8baa84773158d1c588e779e3828021c5874d Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 11 Feb 2019 11:09:41 +0300 Subject: [PATCH 6/7] deduplicate input params in block reward fetcher --- .../lib/explorer/chain/import/runner/block/rewards.ex | 5 +---- apps/indexer/lib/indexer/block/reward/fetcher.ex | 4 +++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex index c30a919b59..3ce55fbf07 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex @@ -53,10 +53,7 @@ defmodule Explorer.Chain.Import.Runner.Block.Rewards do on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0) # order so that row ShareLocks are grabbed in a consistent order - ordered_changes_list = - changes_list - |> Enum.uniq_by(&{&1.address_hash, &1.address_type, &1.block_hash}) - |> Enum.sort_by(&{&1.address_hash, &1.address_type, &1.block_hash}) + ordered_changes_list = Enum.sort_by(changes_list, &{&1.address_hash, &1.address_type, &1.block_hash}) Import.insert_changes_list( repo, diff --git a/apps/indexer/lib/indexer/block/reward/fetcher.ex b/apps/indexer/lib/indexer/block/reward/fetcher.ex index 7e89166c74..fe1d5d2bf2 100644 --- a/apps/indexer/lib/indexer/block/reward/fetcher.ex +++ b/apps/indexer/lib/indexer/block/reward/fetcher.ex @@ -177,7 +177,8 @@ defmodule Indexer.Block.Reward.Fetcher do |> Enum.map(& &1.block_hash) |> Chain.gas_payment_by_block_hash() - Enum.map(beneficiaries_params, fn %{block_hash: block_hash} = beneficiary -> + beneficiaries_params + |> Enum.map(fn %{block_hash: block_hash} = beneficiary -> case gas_payment_by_block_hash do %{^block_hash => gas_payment} -> {:ok, minted} = Wei.cast(beneficiary.reward) @@ -187,6 +188,7 @@ defmodule Indexer.Block.Reward.Fetcher do beneficiary end end) + |> Enum.uniq() end defp import_block_reward_params(block_rewards_params) when is_list(block_rewards_params) do From 0be39ea2ec1abe0968d9f8dc035de4695c14ffb5 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 11 Feb 2019 17:18:36 +0300 Subject: [PATCH 7/7] fix cardinality error issue --- .../lib/indexer/block/reward/fetcher.ex | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/apps/indexer/lib/indexer/block/reward/fetcher.ex b/apps/indexer/lib/indexer/block/reward/fetcher.ex index fe1d5d2bf2..0c190f9719 100644 --- a/apps/indexer/lib/indexer/block/reward/fetcher.ex +++ b/apps/indexer/lib/indexer/block/reward/fetcher.ex @@ -177,18 +177,20 @@ defmodule Indexer.Block.Reward.Fetcher do |> Enum.map(& &1.block_hash) |> Chain.gas_payment_by_block_hash() - beneficiaries_params - |> Enum.map(fn %{block_hash: block_hash} = beneficiary -> - case gas_payment_by_block_hash do - %{^block_hash => gas_payment} -> - {:ok, minted} = Wei.cast(beneficiary.reward) - %{beneficiary | reward: Wei.sum(minted, gas_payment)} - - _ -> - beneficiary + Enum.map(beneficiaries_params, fn %{block_hash: block_hash, address_type: address_type} = beneficiary -> + if address_type == :validator do + case gas_payment_by_block_hash do + %{^block_hash => gas_payment} -> + {:ok, minted} = Wei.cast(beneficiary.reward) + %{beneficiary | reward: Wei.sum(minted, gas_payment)} + + _ -> + beneficiary + end + else + beneficiary end end) - |> Enum.uniq() end defp import_block_reward_params(block_rewards_params) when is_list(block_rewards_params) do