From 7d9c4f297221e22d9a01e0b7fde39aec31dcf46d Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 20 Feb 2019 14:20:26 +0300 Subject: [PATCH 1/3] fetch address balances in code fetcher --- apps/indexer/lib/indexer/code/fetcher.ex | 54 ++++++++++++++++++- .../test/indexer/code/fetcher_test.exs | 5 ++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/apps/indexer/lib/indexer/code/fetcher.ex b/apps/indexer/lib/indexer/code/fetcher.ex index 153f7b8cdb0d..561e1c0066ca 100644 --- a/apps/indexer/lib/indexer/code/fetcher.ex +++ b/apps/indexer/lib/indexer/code/fetcher.ex @@ -99,8 +99,32 @@ defmodule Indexer.Code.Fetcher do {:ok, create_address_codes} -> addresses_params = AddressExtraction.extract_addresses(%{codes: create_address_codes.params_list}) + import_with_balances(addresses_params, entries, json_rpc_named_arguments) + + {:error, reason} -> + Logger.error(fn -> ["failed to fetch contract codes: ", inspect(reason)] end, + error_count: Enum.count(entries) + ) + + {:retry, entries} + + :ignore -> + :ok + end + end + + defp import_with_balances(addresses_params, entries, json_rpc_named_arguments) do + entries + |> coin_balances_request_params() + |> EthereumJSONRPC.fetch_balances(json_rpc_named_arguments) + |> case do + {:ok, fetched_balances} -> + balance_addresses_params = balances_params_to_address_params(fetched_balances.params_list) + + merged_addresses_params = AddressExtraction.merge_addresses(addresses_params ++ balance_addresses_params) + case Chain.import(%{ - addresses: %{params: addresses_params}, + addresses: %{params: merged_addresses_params}, timeout: :infinity }) do {:ok, _} -> @@ -119,6 +143,34 @@ defmodule Indexer.Code.Fetcher do {:retry, entries} end + + {:error, reason} -> + Logger.error(fn -> ["failed to fetch contract codes: ", inspect(reason)] end, + error_count: Enum.count(entries) + ) + + {:retry, entries} + + :ignore -> + :ok end end + + def balances_params_to_address_params(balances_params) do + balances_params + |> Enum.group_by(fn %{address_hash: address_hash} -> address_hash end) + |> Map.values() + |> Stream.map(&Enum.max_by(&1, fn %{block_number: block_number} -> block_number end)) + |> Enum.map(fn %{address_hash: address_hash, block_number: block_number, value: value} -> + %{hash: address_hash, fetched_coin_balance_block_number: block_number, fetched_coin_balance: value} + end) + end + + defp coin_balances_request_params(entries) do + Enum.map(entries, fn {block_number, created_contract_address_hash_bytes, _transaction_hash_bytes} -> + {:ok, created_contract_address_hash} = Hash.Address.cast(created_contract_address_hash_bytes) + + %{block_quantity: integer_to_quantity(block_number), hash_data: to_string(created_contract_address_hash)} + end) + end end diff --git a/apps/indexer/test/indexer/code/fetcher_test.exs b/apps/indexer/test/indexer/code/fetcher_test.exs index b02be1b49c80..0debf679e485 100644 --- a/apps/indexer/test/indexer/code/fetcher_test.exs +++ b/apps/indexer/test/indexer/code/fetcher_test.exs @@ -75,6 +75,9 @@ defmodule Indexer.Code.FetcherTest do |> expect(:json_rpc, fn [%{id: id, method: "eth_getCode", params: [^address, ^block_quantity]}], _options -> {:ok, [%{id: id, result: code}]} end) + |> expect(:json_rpc, fn [%{id: id, method: "eth_getBalance", params: [^address, ^block_quantity]}], _options -> + {:ok, [%{id: id, result: "0x0"}]} + end) end insert(:address, hash: address) @@ -93,6 +96,8 @@ defmodule Indexer.Code.FetcherTest do end) assert to_string(fetched_address.contract_code) == code + assert fetched_address.fetched_coin_balance + assert fetched_address.fetched_coin_balance_block_number == block_number updated_transaction = Repo.one!(from(transaction in Transaction, where: transaction.hash == ^hash)) From d510d7d123285515c6e461c7e8d51ef75354ae5c Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 20 Feb 2019 14:35:26 +0300 Subject: [PATCH 2/3] fix dialyzer --- apps/indexer/lib/indexer/code/fetcher.ex | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apps/indexer/lib/indexer/code/fetcher.ex b/apps/indexer/lib/indexer/code/fetcher.ex index 561e1c0066ca..f22c3f074637 100644 --- a/apps/indexer/lib/indexer/code/fetcher.ex +++ b/apps/indexer/lib/indexer/code/fetcher.ex @@ -107,9 +107,6 @@ defmodule Indexer.Code.Fetcher do ) {:retry, entries} - - :ignore -> - :ok end end @@ -150,9 +147,6 @@ defmodule Indexer.Code.Fetcher do ) {:retry, entries} - - :ignore -> - :ok end end From fb68e8ffdf52d1ee8ceec53dc6a4bc4690193280 Mon Sep 17 00:00:00 2001 From: zachdaniel Date: Thu, 21 Feb 2019 11:16:39 -0500 Subject: [PATCH 3/3] fix: configure BLOCKSCOUT_VERSION in a standard way --- apps/block_scout_web/config/config.exs | 3 ++- apps/block_scout_web/lib/block_scout_web.ex | 9 +-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/apps/block_scout_web/config/config.exs b/apps/block_scout_web/config/config.exs index 6b5d1fcd0d0f..60cc509a83ee 100644 --- a/apps/block_scout_web/config/config.exs +++ b/apps/block_scout_web/config/config.exs @@ -8,7 +8,8 @@ use Mix.Config # General application configuration config :block_scout_web, namespace: BlockScoutWeb, - ecto_repos: [Explorer.Repo] + ecto_repos: [Explorer.Repo], + version: System.get_env("BLOCKSCOUT_VERSION") config :block_scout_web, BlockScoutWeb.Chain, network: System.get_env("NETWORK"), diff --git a/apps/block_scout_web/lib/block_scout_web.ex b/apps/block_scout_web/lib/block_scout_web.ex index 68ff0c002ed1..9a4c6cab09ab 100644 --- a/apps/block_scout_web/lib/block_scout_web.ex +++ b/apps/block_scout_web/lib/block_scout_web.ex @@ -16,14 +16,7 @@ defmodule BlockScoutWeb do below. Instead, define any helper function in modules and import those modules here. """ - - # This is intentionally compiled in. Version is set - # at compile time, and we don't want to make a system - # env call to retreive it every time anyone loads any - # page. - @version System.get_env("BLOCKSCOUT_VERSION") || "unknown" - - def version(), do: @version + def version(), do: Application.get_env(:block_scout_web, :version) def controller do quote do