Skip to content

Commit

Permalink
Chunk realtime balances requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwerty5Uiop committed Jul 7, 2023
1 parent 668e2dd commit 996a8d6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 68 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#7836](https://github.com/blockscout/blockscout/pull/7836) - Improve unverified email flow
- [#7784](https://github.com/blockscout/blockscout/pull/7784) - Search improvements: Add new fields, light refactoring
- [#7811](https://github.com/blockscout/blockscout/pull/7811) - Filter addresses before insertion
- [#7873](https://github.com/blockscout/blockscout/pull/7873) - Chunk realtime balances requests

### Fixes
- [#7825](https://github.com/blockscout/blockscout/pull/7825) - Fix nginx config for the new frontend websockets
Expand Down
6 changes: 5 additions & 1 deletion apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ defmodule EthereumJSONRPC do
[%{required(:block_quantity) => quantity, required(:hash_data) => data()}],
json_rpc_named_arguments
) :: {:ok, FetchedBalances.t()} | {:error, reason :: term}
def fetch_balances(params_list, json_rpc_named_arguments)
def fetch_balances(params_list, json_rpc_named_arguments, chunk_size \\ nil)
when is_list(params_list) and is_list(json_rpc_named_arguments) do
filtered_params =
if Application.get_env(:ethereum_jsonrpc, :disable_archive_balances?) do
Expand All @@ -203,6 +203,7 @@ defmodule EthereumJSONRPC do
with {:ok, responses} <-
id_to_params
|> FetchedBalances.requests()
|> chunk_requests(chunk_size)
|> json_rpc(json_rpc_named_arguments) do
{:ok, FetchedBalances.from_responses(responses, id_to_params)}
end
Expand Down Expand Up @@ -550,6 +551,9 @@ defmodule EthereumJSONRPC do
end
end

defp chunk_requests(requests, nil), do: requests
defp chunk_requests(requests, chunk_size), do: Enum.chunk_every(requests, chunk_size)

def first_block_to_fetch(config) do
string_value = Application.get_env(:indexer, config)

Expand Down
4 changes: 4 additions & 0 deletions apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ defmodule EthereumJSONRPC.HTTP do
end
end

def json_rpc([batch | _] = chunked_batch_request, options) when is_list(batch) do
chunked_json_rpc(chunked_batch_request, options, [])
end

def json_rpc(batch_request, options) when is_list(batch_request) do
chunked_json_rpc([batch_request], options, [])
end
Expand Down
2 changes: 1 addition & 1 deletion apps/indexer/lib/indexer/block/realtime/fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ defmodule Indexer.Block.Realtime.Fetcher do
) do
case options
|> fetch_balances_params_list()
|> EthereumJSONRPC.fetch_balances(json_rpc_named_arguments) do
|> EthereumJSONRPC.fetch_balances(json_rpc_named_arguments, CoinBalance.batch_size()) do
{:ok, %FetchedBalances{params_list: params_list, errors: []}} ->
merged_addresses_params =
%{address_coin_balances: params_list}
Expand Down
2 changes: 2 additions & 0 deletions apps/indexer/lib/indexer/fetcher/coin_balance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ defmodule Indexer.Fetcher.CoinBalance do
@default_max_batch_size 500
@default_max_concurrency 4

def batch_size, do: defaults()[:max_batch_size]

@doc """
Asynchronously fetches balances for each address `hash` at the `block_number`.
"""
Expand Down
142 changes: 76 additions & 66 deletions apps/indexer/test/indexer/block/realtime/fetcher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -472,30 +472,32 @@ defmodule Indexer.Block.Realtime.FetcherTest do
]}

[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x40b18103537c0f15d5e137dd8ddd019b84949d16", "0x3C365F"]
},
%{
id: 1,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x5ee341ac44d344ade1ca3a771c59b98eb2a77df2", "0x3C365F"]
},
%{
id: 2,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x66c9343c7e8ca673a1fedf9dbf2cd7936dbbf7e3", "0x3C3660"]
},
%{
id: 3,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x698bf6943bab687b2756394624aa183f434f65da", "0x3C365F"]
}
[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x40b18103537c0f15d5e137dd8ddd019b84949d16", "0x3C365F"]
},
%{
id: 1,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x5ee341ac44d344ade1ca3a771c59b98eb2a77df2", "0x3C365F"]
},
%{
id: 2,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x66c9343c7e8ca673a1fedf9dbf2cd7936dbbf7e3", "0x3C3660"]
},
%{
id: 3,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x698bf6943bab687b2756394624aa183f434f65da", "0x3C365F"]
}
]
],
_ ->
{:ok,
Expand Down Expand Up @@ -964,30 +966,32 @@ defmodule Indexer.Block.Realtime.FetcherTest do
]}

[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x40b18103537c0f15d5e137dd8ddd019b84949d16", "0x3C365F"]
},
%{
id: 1,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x5ee341ac44d344ade1ca3a771c59b98eb2a77df2", "0x3C365F"]
},
%{
id: 2,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x66c9343c7e8ca673a1fedf9dbf2cd7936dbbf7e3", "0x3C3660"]
},
%{
id: 3,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x698bf6943bab687b2756394624aa183f434f65da", "0x3C365F"]
}
[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x40b18103537c0f15d5e137dd8ddd019b84949d16", "0x3C365F"]
},
%{
id: 1,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x5ee341ac44d344ade1ca3a771c59b98eb2a77df2", "0x3C365F"]
},
%{
id: 2,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x66c9343c7e8ca673a1fedf9dbf2cd7936dbbf7e3", "0x3C3660"]
},
%{
id: 3,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x698bf6943bab687b2756394624aa183f434f65da", "0x3C365F"]
}
]
],
_ ->
{:ok,
Expand Down Expand Up @@ -1183,23 +1187,27 @@ defmodule Indexer.Block.Realtime.FetcherTest do
]}

[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x5ee341ac44d344ade1ca3a771c59b98eb2a77df2", "0x3C365F"]
}
[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x5ee341ac44d344ade1ca3a771c59b98eb2a77df2", "0x3C365F"]
}
]
],
_ ->
{:ok, [%{id: 0, jsonrpc: "2.0", result: "0x53474fa377a46000"}]}

[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x66c9343c7e8ca673a1fedf9dbf2cd7936dbbf7e3", "0x3C3660"]
}
[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x66c9343c7e8ca673a1fedf9dbf2cd7936dbbf7e3", "0x3C3660"]
}
]
],
_ ->
{:ok, [%{id: 0, jsonrpc: "2.0", result: "0x53507afe51f28000"}]}
Expand All @@ -1224,12 +1232,14 @@ defmodule Indexer.Block.Realtime.FetcherTest do
]}

[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x5ee341ac44d344ade1ca3a771c59b98eb2a77df2", "0x3C365F"]
}
[
%{
id: 0,
jsonrpc: "2.0",
method: "eth_getBalance",
params: ["0x5ee341ac44d344ade1ca3a771c59b98eb2a77df2", "0x3C365F"]
}
]
],
_ ->
{:ok, [%{id: 0, jsonrpc: "2.0", result: "0x53474fa377a46000"}]}
Expand Down

0 comments on commit 996a8d6

Please sign in to comment.