From 4e2ba9ae0c4ffa209ccb02f22a7ef07397553f3d Mon Sep 17 00:00:00 2001 From: pasqu4le Date: Tue, 9 Jul 2019 16:27:28 +0200 Subject: [PATCH] Reduce function input to address' hash only where possible Problem: a lot of function take an Explorer.Chain.Address as an parameter, but only use its hash. This makes tracing the necessary scope of functions harder and is one of the cause for unnecessary queries/code execution. Solution: refactor this functions to take only an address' hash. --- CHANGELOG.md | 1 + .../address_coin_balance_controller.ex | 4 +- .../address_contract_controller.ex | 4 +- .../controllers/address_controller.ex | 12 +- .../address_decompiled_contract_controller.ex | 4 +- ...address_internal_transaction_controller.ex | 6 +- .../controllers/address_logs_controller.ex | 8 +- .../address_read_contract_controller.ex | 4 +- .../controllers/address_token_controller.ex | 4 +- .../address_token_transfer_controller.ex | 4 +- .../address_transaction_controller.ex | 6 +- .../address_validation_controller.ex | 11 +- .../block_scout_web/resolvers/transaction.ex | 4 +- .../templates/address/overview.html.eex | 4 +- .../tokens/overview/_details.html.eex | 2 +- .../lib/block_scout_web/views/address_view.ex | 12 +- .../views/address_view_test.exs | 2 +- apps/explorer/lib/explorer/chain.ex | 54 +++----- .../address_token_transfer_csv_exporter.ex | 16 +-- .../chain/address_transaction_csv_exporter.ex | 14 +- apps/explorer/lib/explorer/graphql.ex | 7 +- apps/explorer/test/explorer/chain_test.exs | 125 +++++++++--------- apps/explorer/test/explorer/graphql_test.exs | 18 +-- 23 files changed, 158 insertions(+), 168 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c18c34df3..9f08609d12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - [#2291](https://github.com/poanetwork/blockscout/pull/2291) - dashboard fix for md resolution, transactions load fix, block info row fix, addresses page issue, check mark issue ### Chore +- [#2325](https://github.com/poanetwork/blockscout/pull/2325) - Reduce function input to address' hash only where possible - [#2305](https://github.com/poanetwork/blockscout/pull/2305) - Improve Address controllers - [#2302](https://github.com/poanetwork/blockscout/pull/2302) - fix names for xDai source - [#2289](https://github.com/poanetwork/blockscout/pull/2289) - Optional websockets for dev environment diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_coin_balance_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_coin_balance_controller.ex index d43f99a382..33a01959c9 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_coin_balance_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_coin_balance_controller.ex @@ -64,8 +64,8 @@ defmodule BlockScoutWeb.AddressCoinBalanceController do address: address, coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), - transaction_count: transaction_count(address), - validation_count: validation_count(address), + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash), current_path: current_path(conn) ) end diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_contract_controller.ex index 34238a018d..bc4df8c4be 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_contract_controller.ex @@ -16,8 +16,8 @@ defmodule BlockScoutWeb.AddressContractController do address: address, coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), - transaction_count: transaction_count(address), - validation_count: validation_count(address) + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash) ) else :error -> diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex index 6ce33a2773..9f955fa672 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex @@ -5,7 +5,7 @@ defmodule BlockScoutWeb.AddressController do alias BlockScoutWeb.AddressView alias Explorer.{Chain, Market} - alias Explorer.Chain.Address + alias Explorer.Chain.Hash alias Explorer.ExchangeRates.Token alias Phoenix.View @@ -45,7 +45,7 @@ defmodule BlockScoutWeb.AddressController do exchange_rate: exchange_rate, total_supply: total_supply, tx_count: tx_count, - validation_count: validation_count(address) + validation_count: validation_count(address.hash) ) end) @@ -69,11 +69,11 @@ defmodule BlockScoutWeb.AddressController do redirect(conn, to: address_transaction_path(conn, :index, id)) end - def transaction_count(%Address{} = address) do - Chain.total_transactions_sent_by_address(address) + def transaction_count(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do + Chain.total_transactions_sent_by_address(address_hash) end - def validation_count(%Address{} = address) do - Chain.address_to_validation_count(address) + def validation_count(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do + Chain.address_to_validation_count(address_hash) end end diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_decompiled_contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_decompiled_contract_controller.ex index ddee462ba2..be591ce384 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_decompiled_contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_decompiled_contract_controller.ex @@ -16,8 +16,8 @@ defmodule BlockScoutWeb.AddressDecompiledContractController do address: address, coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), - transaction_count: transaction_count(address), - validation_count: validation_count(address) + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash) ) else :error -> diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_internal_transaction_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_internal_transaction_controller.ex index fd5813a1d4..15aa87e69a 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_internal_transaction_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_internal_transaction_controller.ex @@ -28,7 +28,7 @@ defmodule BlockScoutWeb.AddressInternalTransactionController do |> Keyword.merge(paging_options(params)) |> Keyword.merge(current_filter(params)) - internal_transactions_plus_one = Chain.address_to_internal_transactions(address, full_options) + internal_transactions_plus_one = Chain.address_to_internal_transactions(address_hash, full_options) {internal_transactions, next_page} = split_list_by_page(internal_transactions_plus_one) next_page_path = @@ -71,8 +71,8 @@ defmodule BlockScoutWeb.AddressInternalTransactionController do current_path: current_path(conn), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), filter: params["filter"], - transaction_count: transaction_count(address), - validation_count: validation_count(address) + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash) ) else :error -> diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_logs_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_logs_controller.ex index 55070b4cd8..e349ecd9ef 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_logs_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_logs_controller.ex @@ -17,7 +17,7 @@ defmodule BlockScoutWeb.AddressLogsController do def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), {:ok, address} <- Chain.hash_to_address(address_hash, [], false) do - logs_plus_one = Chain.address_to_logs(address, paging_options(params)) + logs_plus_one = Chain.address_to_logs(address_hash, paging_options(params)) {results, next_page} = split_list_by_page(logs_plus_one) next_page_url = @@ -63,8 +63,8 @@ defmodule BlockScoutWeb.AddressLogsController do current_path: current_path(conn), coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), - transaction_count: transaction_count(address), - validation_count: validation_count(address) + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash) ) else _ -> @@ -79,7 +79,7 @@ defmodule BlockScoutWeb.AddressLogsController do formatted_topic = if String.starts_with?(topic, "0x"), do: topic, else: "0x" <> topic - logs_plus_one = Chain.address_to_logs(address, topic: formatted_topic) + logs_plus_one = Chain.address_to_logs(address_hash, topic: formatted_topic) {results, next_page} = split_list_by_page(logs_plus_one) diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_read_contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_read_contract_controller.ex index a9a30cc0d0..d57aa30807 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_read_contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_read_contract_controller.ex @@ -23,8 +23,8 @@ defmodule BlockScoutWeb.AddressReadContractController do address: address, coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), - transaction_count: transaction_count(address), - validation_count: validation_count(address) + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash) ) else :error -> diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_token_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_token_controller.ex index 8d5a60a1ac..d77baa605a 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_token_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_token_controller.ex @@ -64,8 +64,8 @@ defmodule BlockScoutWeb.AddressTokenController do current_path: current_path(conn), coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), - transaction_count: transaction_count(address), - validation_count: validation_count(address) + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash) ) else :error -> diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_token_transfer_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_token_transfer_controller.ex index 2a1e47b295..4e0023b067 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_token_transfer_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_token_transfer_controller.ex @@ -85,8 +85,8 @@ defmodule BlockScoutWeb.AddressTokenTransferController do exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), current_path: current_path(conn), token: token, - transaction_count: transaction_count(address), - validation_count: validation_count(address) + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash) ) else :error -> diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex index f0ff5f3f12..dc71bed50b 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex @@ -38,7 +38,7 @@ defmodule BlockScoutWeb.AddressTransactionController do |> Keyword.merge(paging_options(params)) |> Keyword.merge(current_filter(params)) - results_plus_one = Chain.address_to_transactions_with_rewards(address, options) + results_plus_one = Chain.address_to_transactions_with_rewards(address_hash, options) {results, next_page} = split_list_by_page(results_plus_one) next_page_url = @@ -97,8 +97,8 @@ defmodule BlockScoutWeb.AddressTransactionController do coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), filter: params["filter"], - transaction_count: transaction_count(address), - validation_count: validation_count(address), + transaction_count: transaction_count(address_hash), + validation_count: validation_count(address_hash), current_path: current_path(conn) ) else diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_validation_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_validation_controller.ex index 84036923de..7226dffcf8 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_validation_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_validation_controller.ex @@ -17,7 +17,7 @@ defmodule BlockScoutWeb.AddressValidationController do def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), - {:ok, address} <- Chain.find_or_insert_address_from_hash(address_hash, [], false) do + {:ok, _} <- Chain.find_or_insert_address_from_hash(address_hash, [], false) do full_options = Keyword.merge( [ @@ -31,7 +31,7 @@ defmodule BlockScoutWeb.AddressValidationController do paging_options(params) ) - blocks_plus_one = Chain.get_blocks_validated_by_address(full_options, address) + blocks_plus_one = Chain.get_blocks_validated_by_address(full_options, address_hash) {blocks, next_page} = split_list_by_page(blocks_plus_one) next_page_path = @@ -63,9 +63,6 @@ defmodule BlockScoutWeb.AddressValidationController do else :error -> unprocessable_entity(conn) - - {:error, :not_found} -> - not_found(conn) end end @@ -78,8 +75,8 @@ defmodule BlockScoutWeb.AddressValidationController do address: address, coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address), current_path: current_path(conn), - transaction_count: transaction_count(address), - validation_count: validation_count(address), + transaction_count: transaction_count(address.hash), + validation_count: validation_count(address.hash), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null() ) else diff --git a/apps/block_scout_web/lib/block_scout_web/resolvers/transaction.ex b/apps/block_scout_web/lib/block_scout_web/resolvers/transaction.ex index aa54a8e6da..fe96463794 100644 --- a/apps/block_scout_web/lib/block_scout_web/resolvers/transaction.ex +++ b/apps/block_scout_web/lib/block_scout_web/resolvers/transaction.ex @@ -12,8 +12,8 @@ defmodule BlockScoutWeb.Resolvers.Transaction do end end - def get_by(%Address{} = address, args, _) do - address + def get_by(%Address{hash: address_hash}, args, _) do + address_hash |> GraphQL.address_to_transactions_query() |> Connection.from_query(&Repo.all/1, args, options(args)) end diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex index e6178a4e72..3cb95a5331 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex @@ -74,7 +74,7 @@ <%= if contract?(@address) do %> <%= gettext(">=") %> - <%= incoming_transaction_count(@address) %> + <%= incoming_transaction_count(@address.hash) %> <%= gettext("Incoming Transactions") %> <% else %> @@ -146,7 +146,7 @@