diff --git a/CHANGELOG.md b/CHANGELOG.md index ad3f98f921..7cd9165b22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Current ### Features +- [#2352](https://github.com/poanetwork/blockscout/pull/2352) - Fetch rewards in parallel with transactions - [#2294](https://github.com/poanetwork/blockscout/pull/2294) - add healthy block period checking endpoint ### Fixes 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 dc71bed50b..f6d9b8a30f 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 @@ -23,7 +23,8 @@ defmodule BlockScoutWeb.AddressTransactionController do [token_transfers: :token] => :optional, [token_transfers: :to_address] => :optional, [token_transfers: :from_address] => :optional, - [token_transfers: :token_contract_address] => :optional + [token_transfers: :token_contract_address] => :optional, + :block => :required } ] @@ -34,7 +35,6 @@ defmodule BlockScoutWeb.AddressTransactionController do {:ok, address} <- Chain.hash_to_address(address_hash, address_options, false) do options = @transaction_necessity_by_association - |> put_in([:necessity_by_association, :block], :required) |> Keyword.merge(paging_options(params)) |> Keyword.merge(current_filter(params)) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 05d6407dfb..17116658bf 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -222,25 +222,17 @@ defmodule Explorer.Chain do Transaction.t() ] def address_to_transactions_with_rewards(address_hash, options \\ []) when is_list(options) do - direction = Keyword.get(options, :direction) - necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) paging_options = Keyword.get(options, :paging_options, @default_paging_options) - transaction_hashes_from_token_transfers = - TokenTransfer.where_any_address_fields_match(direction, address_hash, paging_options) - - transactions_list = - paging_options - |> fetch_transactions() - |> Transaction.where_transaction_matches(transaction_hashes_from_token_transfers, direction, address_hash) - |> join_associations(necessity_by_association) - |> Transaction.preload_token_transfers(address_hash) - |> Repo.all() - if Application.get_env(:block_scout_web, BlockScoutWeb.Chain)[:has_emission_funds] do + rewards_task = + Task.async(fn -> + Reward.fetch_emission_rewards_tuples(address_hash, paging_options) + end) + address_hash - |> Reward.fetch_emission_rewards_tuples(paging_options) - |> Enum.concat(transactions_list) + |> address_to_transactions_without_rewards(paging_options, options) + |> Enum.concat(Task.await(rewards_task)) |> Enum.sort_by(fn item -> case item do {%Reward{} = emission_reward, _} -> @@ -252,10 +244,25 @@ defmodule Explorer.Chain do end) |> Enum.take(paging_options.page_size) else - transactions_list + address_to_transactions_without_rewards(address_hash, paging_options, options) end end + defp address_to_transactions_without_rewards(address_hash, paging_options, options) do + direction = Keyword.get(options, :direction) + necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) + + transaction_hashes_from_token_transfers = + TokenTransfer.where_any_address_fields_match(direction, address_hash, paging_options) + + paging_options + |> fetch_transactions() + |> Transaction.where_transaction_matches(transaction_hashes_from_token_transfers, direction, address_hash) + |> join_associations(necessity_by_association) + |> Transaction.preload_token_transfers(address_hash) + |> Repo.all() + end + @spec address_to_logs(Hash.Address.t(), Keyword.t()) :: [Log.t()] def address_to_logs(address_hash, options \\ []) when is_list(options) do paging_options = Keyword.get(options, :paging_options) || %PagingOptions{page_size: 50}