Small refactoring

pull/9098/head
POA 10 months ago
parent 811f69e65b
commit ae812aa633
  1. 23
      apps/indexer/lib/indexer/fetcher/shibarium/l1.ex
  2. 24
      apps/indexer/lib/indexer/fetcher/zkevm/bridge.ex
  3. 32
      apps/indexer/lib/indexer/helper.ex
  4. 3
      config/runtime.exs

@ -23,8 +23,6 @@ defmodule Indexer.Fetcher.Shibarium.L1 do
import Indexer.Fetcher.Shibarium.Helper, import Indexer.Fetcher.Shibarium.Helper,
only: [calc_operation_hash: 5, prepare_insert_items: 2, recalculate_cached_count: 0] 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.Shibarium.Bridge
alias Explorer.{Chain, Repo} alias Explorer.{Chain, Repo}
alias Indexer.{BoundQueue, Helper} alias Indexer.{BoundQueue, Helper}
@ -348,25 +346,6 @@ defmodule Indexer.Fetcher.Shibarium.L1 do
end end
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 defp get_last_l1_item do
query = query =
from(sb in Bridge, from(sb in Bridge,
@ -581,7 +560,7 @@ defmodule Indexer.Fetcher.Shibarium.L1 do
timestamps = timestamps =
events events
|> filter_deposit_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 -> |> Enum.reduce(%{}, fn block, acc ->
block_number = quantity_to_integer(Map.get(block, "number")) block_number = quantity_to_integer(Map.get(block, "number"))
{:ok, timestamp} = DateTime.from_unix(quantity_to_integer(Map.get(block, "timestamp"))) {:ok, timestamp} = DateTime.from_unix(quantity_to_integer(Map.get(block, "timestamp")))

@ -19,8 +19,7 @@ defmodule Indexer.Fetcher.Zkevm.Bridge do
import Explorer.Helper, only: [decode_data: 2] import Explorer.Helper, only: [decode_data: 2]
alias EthereumJSONRPC.Block.ByNumber alias EthereumJSONRPC.Logs
alias EthereumJSONRPC.{Blocks, Logs}
alias Explorer.Chain.Hash alias Explorer.Chain.Hash
alias Explorer.Chain.Zkevm.BridgeL1Token alias Explorer.Chain.Zkevm.BridgeL1Token
alias Explorer.{Chain, Repo} alias Explorer.{Chain, Repo}
@ -201,7 +200,7 @@ defmodule Indexer.Fetcher.Zkevm.Bridge do
defp blocks_to_timestamps(events, json_rpc_named_arguments) do defp blocks_to_timestamps(events, json_rpc_named_arguments) do
events 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 -> |> Enum.reduce(%{}, fn block, acc ->
block_number = quantity_to_integer(Map.get(block, "number")) block_number = quantity_to_integer(Map.get(block, "number"))
{:ok, timestamp} = DateTime.from_unix(quantity_to_integer(Map.get(block, "timestamp"))) {:ok, timestamp} = DateTime.from_unix(quantity_to_integer(Map.get(block, "timestamp")))
@ -209,25 +208,6 @@ defmodule Indexer.Fetcher.Zkevm.Bridge do
end) end)
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 defp operation_type(first_topic, is_l1) do
if first_topic == @bridge_event do if first_topic == @bridge_event do
if is_l1, do: :deposit, else: :withdrawal if is_l1, do: :deposit, else: :withdrawal

@ -14,6 +14,7 @@ defmodule Indexer.Helper do
] ]
alias EthereumJSONRPC.Block.ByNumber alias EthereumJSONRPC.Block.ByNumber
alias EthereumJSONRPC.Blocks
alias Explorer.Chain.Hash alias Explorer.Chain.Hash
@spec address_correct?(binary()) :: boolean() @spec address_correct?(binary()) :: boolean()
@ -151,6 +152,37 @@ defmodule Indexer.Helper do
end end
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 """ @doc """
Fetches block timestamp by its number using RPC request. Fetches block timestamp by its number using RPC request.
Performs a specified number of retries (up to) if the first attempt returns error. Performs a specified number of retries (up to) if the first attempt returns error.

@ -748,7 +748,8 @@ config :indexer, Indexer.Fetcher.Zkevm.TransactionBatch,
config :indexer, Indexer.Fetcher.Zkevm.TransactionBatch.Supervisor, config :indexer, Indexer.Fetcher.Zkevm.TransactionBatch.Supervisor,
enabled: 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") Code.require_file("#{config_env()}.exs", "config/runtime")

Loading…
Cancel
Save