diff --git a/CHANGELOG.md b/CHANGELOG.md index 47fa6833a2..050899668d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - [#5968](https://github.com/blockscout/blockscout/pull/5968) - Add call type in the response of txlistinternal API method - [#5860](https://github.com/blockscout/blockscout/pull/5860) - Integrate rust verifier micro-service ([blockscout-rs/verifier](https://github.com/blockscout/blockscout-rs/tree/main/verification)) +- [#6001](https://github.com/blockscout/blockscout/pull/6001) - Add ETHEREUM_JSONRPC_DISABLE_ARCHIVE_BALANCES env var that filters requests and query node only if the block quantity is "latest" ### Fixes - [#5978](https://github.com/blockscout/blockscout/pull/5978) - Allow timestamp param in the log of eth_getTransactionReceipt method diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex index ab6399f930..3f64a2b991 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex @@ -184,7 +184,18 @@ defmodule EthereumJSONRPC do ) :: {:ok, FetchedBalances.t()} | {:error, reason :: term} def fetch_balances(params_list, json_rpc_named_arguments) when is_list(params_list) and is_list(json_rpc_named_arguments) do - id_to_params = id_to_params(params_list) + filtered_params = + if Application.get_env(:ethereum_jsonrpc, :disable_archive_balances?) do + params_list + |> Enum.filter(fn + %{block_quantity: "latest"} -> true + _ -> false + end) + else + params_list + end + + id_to_params = id_to_params(filtered_params) with {:ok, responses} <- id_to_params diff --git a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs index 75827da438..ea513d560d 100644 --- a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs +++ b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs @@ -913,3 +913,65 @@ defmodule EthereumJSONRPCTest do end end end + +defmodule EthereumJSONRPCSyncTest do + use EthereumJSONRPC.Case, async: false + + import EthereumJSONRPC.Case + import Mox + + alias EthereumJSONRPC.FetchedBalances + setup :verify_on_exit! + + @moduletag :capture_log + + describe "fetch_balances/1" do + setup do + initial_env = Application.get_all_env(:indexer) + on_exit(fn -> Application.put_all_env([{:indexer, initial_env}]) end) + end + + test "ignores all request with block_quantity != latest when env ETHEREUM_JSONRPC_DISABLE_ARCHIVE_BALANCES is true", + %{ + json_rpc_named_arguments: json_rpc_named_arguments + } do + hash = "0x8bf38d4764929064f2d4d3a56520a76ab3df415b" + expected_fetched_balance = 1 + + expect(EthereumJSONRPC.Mox, :json_rpc, 1, fn [ + %{ + id: 0, + jsonrpc: "2.0", + method: "eth_getBalance", + params: [^hash, "latest"] + } + ], + _options -> + {:ok, [%{id: 0, result: EthereumJSONRPC.integer_to_quantity(expected_fetched_balance)}]} + end) + + Application.put_env(:ethereum_jsonrpc, :disable_archive_balances?, "true") + + assert EthereumJSONRPC.fetch_balances( + [ + %{block_quantity: "0x1", hash_data: hash}, + %{block_quantity: "0x2", hash_data: hash}, + %{block_quantity: "0x3", hash_data: hash}, + %{block_quantity: "0x4", hash_data: hash}, + %{block_quantity: "latest", hash_data: hash} + ], + json_rpc_named_arguments + ) == + {:ok, + %FetchedBalances{ + params_list: [ + %{ + address_hash: hash, + block_number: :error, + value: expected_fetched_balance + } + ] + }} + end + end +end diff --git a/config/runtime.exs b/config/runtime.exs index 9405efe3cc..59fecb5631 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -146,7 +146,8 @@ config :block_scout_web, BlockScoutWeb.Chain.Address.CoinBalance, config :ethereum_jsonrpc, rpc_transport: if(System.get_env("ETHEREUM_JSONRPC_TRANSPORT", "http") == "http", do: :http, else: :ipc), - ipc_path: System.get_env("IPC_PATH") + ipc_path: System.get_env("IPC_PATH"), + disable_archive_balances?: System.get_env("ETHEREUM_JSONRPC_DISABLE_ARCHIVE_BALANCES", "false") == "true" debug_trace_transaction_timeout = System.get_env("ETHEREUM_JSONRPC_DEBUG_TRACE_TRANSACTION_TIMEOUT", "5s") config :ethereum_jsonrpc, EthereumJSONRPC.Geth, debug_trace_transaction_timeout: debug_trace_transaction_timeout diff --git a/docker-compose/envs/common-blockscout.env b/docker-compose/envs/common-blockscout.env index 73fb2b2329..037dd823fd 100644 --- a/docker-compose/envs/common-blockscout.env +++ b/docker-compose/envs/common-blockscout.env @@ -9,6 +9,7 @@ LOGO=/images/blockscout_logo.svg LOGO_FOOTER=/images/blockscout_logo.svg # ETHEREUM_JSONRPC_WS_URL= ETHEREUM_JSONRPC_TRANSPORT=http +ETHEREUM_JSONRPC_DISABLE_ARCHIVE_BALANCES=false IPC_PATH= NETWORK_PATH=/ API_PATH=/ diff --git a/docker/Makefile b/docker/Makefile index 403680c9d4..f16843d91e 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -52,6 +52,9 @@ endif ifdef ETHEREUM_JSONRPC_TRANSPORT BLOCKSCOUT_CONTAINER_PARAMS += -e 'ETHEREUM_JSONRPC_TRANSPORT=$(ETHEREUM_JSONRPC_TRANSPORT)' endif +ifdef ETHEREUM_JSONRPC_DISABLE_ARCHIVE_BALANCES + BLOCKSCOUT_CONTAINER_PARAMS += -e 'ETHEREUM_JSONRPC_DISABLE_ARCHIVE_BALANCES=$(ETHEREUM_JSONRPC_DISABLE_ARCHIVE_BALANCES)' +endif ifdef IPC_PATH BLOCKSCOUT_CONTAINER_PARAMS += -e 'IPC_PATH=$(IPC_PATH)' endif