From ae812aa63399abd369dfd0e8e6f4f34220c7b7a0 Mon Sep 17 00:00:00 2001 From: POA <33550681+poa@users.noreply.github.com> Date: Wed, 24 Jan 2024 14:54:07 +0300 Subject: [PATCH] Small refactoring --- .../lib/indexer/fetcher/shibarium/l1.ex | 23 +------------ .../lib/indexer/fetcher/zkevm/bridge.ex | 24 ++------------ apps/indexer/lib/indexer/helper.ex | 32 +++++++++++++++++++ config/runtime.exs | 3 +- 4 files changed, 37 insertions(+), 45 deletions(-) diff --git a/apps/indexer/lib/indexer/fetcher/shibarium/l1.ex b/apps/indexer/lib/indexer/fetcher/shibarium/l1.ex index c34b8a5acd..906acd2689 100644 --- a/apps/indexer/lib/indexer/fetcher/shibarium/l1.ex +++ b/apps/indexer/lib/indexer/fetcher/shibarium/l1.ex @@ -23,8 +23,6 @@ defmodule Indexer.Fetcher.Shibarium.L1 do import Indexer.Fetcher.Shibarium.Helper, only: [calc_operation_hash: 5, prepare_insert_items: 2, recalculate_cached_count: 0] - alias EthereumJSONRPC.Block.ByNumber - alias EthereumJSONRPC.Blocks alias Explorer.Chain.Shibarium.Bridge alias Explorer.{Chain, Repo} alias Indexer.{BoundQueue, Helper} @@ -348,25 +346,6 @@ defmodule Indexer.Fetcher.Shibarium.L1 do end end - defp get_blocks_by_events(events, json_rpc_named_arguments, retries) do - request = - events - |> Enum.reduce(%{}, fn event, acc -> - Map.put(acc, event["blockNumber"], 0) - end) - |> Stream.map(fn {block_number, _} -> %{number: block_number} end) - |> Stream.with_index() - |> Enum.into(%{}, fn {params, id} -> {id, params} end) - |> Blocks.requests(&ByNumber.request(&1, false, false)) - - error_message = &"Cannot fetch blocks with batch request. Error: #{inspect(&1)}. Request: #{inspect(request)}" - - case Helper.repeated_call(&json_rpc/2, [request, json_rpc_named_arguments], error_message, retries) do - {:ok, results} -> Enum.map(results, fn %{result: result} -> result end) - {:error, _} -> [] - end - end - defp get_last_l1_item do query = from(sb in Bridge, @@ -581,7 +560,7 @@ defmodule Indexer.Fetcher.Shibarium.L1 do timestamps = events |> filter_deposit_events() - |> get_blocks_by_events(json_rpc_named_arguments, 100_000_000) + |> Helper.get_blocks_by_events(json_rpc_named_arguments, 100_000_000) |> Enum.reduce(%{}, fn block, acc -> block_number = quantity_to_integer(Map.get(block, "number")) {:ok, timestamp} = DateTime.from_unix(quantity_to_integer(Map.get(block, "timestamp"))) diff --git a/apps/indexer/lib/indexer/fetcher/zkevm/bridge.ex b/apps/indexer/lib/indexer/fetcher/zkevm/bridge.ex index ccdf81a846..580526b111 100644 --- a/apps/indexer/lib/indexer/fetcher/zkevm/bridge.ex +++ b/apps/indexer/lib/indexer/fetcher/zkevm/bridge.ex @@ -19,8 +19,7 @@ defmodule Indexer.Fetcher.Zkevm.Bridge do import Explorer.Helper, only: [decode_data: 2] - alias EthereumJSONRPC.Block.ByNumber - alias EthereumJSONRPC.{Blocks, Logs} + alias EthereumJSONRPC.Logs alias Explorer.Chain.Hash alias Explorer.Chain.Zkevm.BridgeL1Token alias Explorer.{Chain, Repo} @@ -201,7 +200,7 @@ defmodule Indexer.Fetcher.Zkevm.Bridge do defp blocks_to_timestamps(events, json_rpc_named_arguments) do events - |> get_blocks_by_events(json_rpc_named_arguments, 100_000_000) + |> Helper.get_blocks_by_events(json_rpc_named_arguments, 100_000_000) |> Enum.reduce(%{}, fn block, acc -> block_number = quantity_to_integer(Map.get(block, "number")) {:ok, timestamp} = DateTime.from_unix(quantity_to_integer(Map.get(block, "timestamp"))) @@ -209,25 +208,6 @@ defmodule Indexer.Fetcher.Zkevm.Bridge do end) end - defp get_blocks_by_events(events, json_rpc_named_arguments, retries) do - request = - events - |> Enum.reduce(%{}, fn event, acc -> - Map.put(acc, event.block_number, 0) - end) - |> Stream.map(fn {block_number, _} -> %{number: block_number} end) - |> Stream.with_index() - |> Enum.into(%{}, fn {params, id} -> {id, params} end) - |> Blocks.requests(&ByNumber.request(&1, false, false)) - - error_message = &"Cannot fetch blocks with batch request. Error: #{inspect(&1)}. Request: #{inspect(request)}" - - case Helper.repeated_call(&json_rpc/2, [request, json_rpc_named_arguments], error_message, retries) do - {:ok, results} -> Enum.map(results, fn %{result: result} -> result end) - {:error, _} -> [] - end - end - defp operation_type(first_topic, is_l1) do if first_topic == @bridge_event do if is_l1, do: :deposit, else: :withdrawal diff --git a/apps/indexer/lib/indexer/helper.ex b/apps/indexer/lib/indexer/helper.ex index be0a2fba03..07ee927c6b 100644 --- a/apps/indexer/lib/indexer/helper.ex +++ b/apps/indexer/lib/indexer/helper.ex @@ -14,6 +14,7 @@ defmodule Indexer.Helper do ] alias EthereumJSONRPC.Block.ByNumber + alias EthereumJSONRPC.Blocks alias Explorer.Chain.Hash @spec address_correct?(binary()) :: boolean() @@ -151,6 +152,37 @@ defmodule Indexer.Helper do end end + @doc """ + Fetches blocks info from the given list of events (logs). + Performs a specified number of retries (up to) if the first attempt returns error. + """ + @spec get_blocks_by_events(list(), list(), non_neg_integer()) :: list() + def get_blocks_by_events(events, json_rpc_named_arguments, retries) do + request = + events + |> Enum.reduce(%{}, fn event, acc -> + block_number = + if is_map(event) do + event.block_number + else + event["blockNumber"] + end + + Map.put(acc, block_number, 0) + end) + |> Stream.map(fn {block_number, _} -> %{number: block_number} end) + |> Stream.with_index() + |> Enum.into(%{}, fn {params, id} -> {id, params} end) + |> Blocks.requests(&ByNumber.request(&1, false, false)) + + error_message = &"Cannot fetch blocks with batch request. Error: #{inspect(&1)}. Request: #{inspect(request)}" + + case repeated_call(&json_rpc/2, [request, json_rpc_named_arguments], error_message, retries) do + {:ok, results} -> Enum.map(results, fn %{result: result} -> result end) + {:error, _} -> [] + end + end + @doc """ Fetches block timestamp by its number using RPC request. Performs a specified number of retries (up to) if the first attempt returns error. diff --git a/config/runtime.exs b/config/runtime.exs index 05fc853978..fc3cf7bad1 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -748,7 +748,8 @@ config :indexer, Indexer.Fetcher.Zkevm.TransactionBatch, config :indexer, Indexer.Fetcher.Zkevm.TransactionBatch.Supervisor, enabled: - ConfigHelper.chain_type() == "polygon_zkevm" && ConfigHelper.parse_bool_env_var("INDEXER_POLYGON_ZKEVM_BATCHES_ENABLED") + ConfigHelper.chain_type() == "polygon_zkevm" && + ConfigHelper.parse_bool_env_var("INDEXER_POLYGON_ZKEVM_BATCHES_ENABLED") Code.require_file("#{config_env()}.exs", "config/runtime")