Skip to content

Commit

Permalink
Merge branch 'master' into ab-set-log-block-number-from-transaction-b…
Browse files Browse the repository at this point in the history
…lock-number
  • Loading branch information
acravenho authored Feb 21, 2019
2 parents e9dd284 + 1a56a5a commit 0b8811f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
3 changes: 2 additions & 1 deletion apps/block_scout_web/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
9 changes: 1 addition & 8 deletions apps/block_scout_web/lib/block_scout_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 47 additions & 1 deletion apps/indexer/lib/indexer/code/fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,29 @@ 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}
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, _} ->
Expand All @@ -119,6 +140,31 @@ 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}
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
5 changes: 5 additions & 0 deletions apps/indexer/test/indexer/code/fetcher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))

Expand Down

0 comments on commit 0b8811f

Please sign in to comment.