From a8c0c1ba92084a9f27534c379b0f2e6b60dc5577 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Mon, 5 Aug 2024 06:47:50 -0600 Subject: [PATCH] limited usage of infinite timeout (#10551) --- .../lib/explorer/chain/arbitrum/reader.ex | 101 ++++++++++++++---- .../lib/indexer/fetcher/arbitrum/utils/db.ex | 33 +----- 2 files changed, 83 insertions(+), 51 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/arbitrum/reader.ex b/apps/explorer/lib/explorer/chain/arbitrum/reader.ex index e944d588ca..1d5916fcf4 100644 --- a/apps/explorer/lib/explorer/chain/arbitrum/reader.ex +++ b/apps/explorer/lib/explorer/chain/arbitrum/reader.ex @@ -40,7 +40,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do ) query - |> Repo.one() + |> Repo.one(timeout: :infinity) end @doc """ @@ -60,7 +60,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do ) query - |> Repo.one() + |> Repo.one(timeout: :infinity) end @doc """ @@ -86,7 +86,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do |> order_by(desc: :block_number) |> limit(1) |> select([log], log.block_number) - |> Repo.one() + |> Repo.one(timeout: :infinity) end @doc """ @@ -107,7 +107,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do |> order_by(desc: :block_number) |> limit(1) |> select([rollup_tx], rollup_tx.block_number) - |> Repo.one() + |> Repo.one(timeout: :infinity) end @doc """ @@ -132,7 +132,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do case query # :required is used since the situation when commit transaction is not found is not possible |> Chain.join_associations(%{:commitment_transaction => :required}) - |> Repo.one() do + |> Repo.one(timeout: :infinity) do nil -> nil batch -> batch.commitment_transaction.block_number end @@ -160,7 +160,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do case query # :required is used since the situation when commit transaction is not found is not possible |> Chain.join_associations(%{:commitment_transaction => :required}) - |> Repo.one() do + |> Repo.one(timeout: :infinity) do nil -> nil batch -> batch.commitment_transaction.block_number end @@ -205,7 +205,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do where: lt.hash in ^l1_tx_hashes ) - Repo.all(query, timeout: :infinity) + Repo.all(query) end @doc """ @@ -227,7 +227,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do where: lt.hash in ^l1_tx_hashes ) - Repo.all(query, timeout: :infinity) + Repo.all(query) end @doc """ @@ -253,7 +253,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do # :required is used since execution records in the table are created only when # the corresponding execution transaction is indexed |> Chain.join_associations(%{:execution_transaction => :required}) - |> Repo.all(timeout: :infinity) + |> Repo.all() end @doc """ @@ -305,7 +305,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do where: lt.block_number <= ^finalized_block and lt.status == :unfinalized ) - Repo.all(query, timeout: :infinity) + Repo.all(query) end @doc """ @@ -357,7 +357,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do ) query - |> Repo.all(timeout: :infinity) + |> Repo.all() end @doc """ @@ -426,7 +426,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do case query # :required is used since existence of the confirmation id is checked above |> Chain.join_associations(%{:confirmation_transaction => :required}) - |> Repo.one() do + |> Repo.one(timeout: :infinity) do nil -> nil @@ -480,7 +480,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do ) query - |> Repo.one() + |> Repo.one(timeout: :infinity) end @doc """ @@ -502,7 +502,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do ) query - |> Repo.one() + |> Repo.one(timeout: :infinity) end @doc """ @@ -532,7 +532,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do order_by: [asc: rb.block_number] ) - Repo.all(query, timeout: :infinity) + Repo.all(query) end @doc """ @@ -553,7 +553,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do where: rb.batch_number == ^batch_number and not is_nil(rb.confirmation_id) ) - Repo.aggregate(query, :count, timeout: :infinity) + Repo.aggregate(query, :count) end @doc """ @@ -588,7 +588,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do order_by: [desc: msg.message_id] ) - Repo.all(query, timeout: :infinity) + Repo.all(query) end def l2_to_l1_messages(status, nil) when status in [:initiated, :sent, :confirmed, :relayed] do @@ -598,7 +598,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do order_by: [desc: msg.message_id] ) - Repo.all(query, timeout: :infinity) + Repo.all(query) end @doc """ @@ -704,7 +704,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do defp do_messages_count(direction, options) do Message |> where([msg], msg.direction == ^direction) - |> select_repo(options).aggregate(:count, timeout: :infinity) + |> select_repo(options).aggregate(:count) end @doc """ @@ -1223,7 +1223,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do ) query - |> Repo.all(timeout: :infinity) + |> Repo.all() end @doc """ @@ -1248,7 +1248,7 @@ defmodule Explorer.Chain.Arbitrum.Reader do ) query - |> Repo.all(timeout: :infinity) + |> Repo.all() |> Enum.reduce(%{}, fn {batch_number, l1_block_number}, acc -> Map.put(acc, batch_number, l1_block_number) end) @@ -1267,6 +1267,63 @@ defmodule Explorer.Chain.Arbitrum.Reader do select: {min(batch.number), max(batch.number)} ) - Repo.one(query) + Repo.one(query, timeout: :infinity) + end + + ##################################################################################### + ### Below are the functions that implement functionality not specific to Arbitrum ### + ##################################################################################### + + @doc """ + Checks if a block with the given block number exists. + + This function queries the database to determine if a block with the specified + block number exists and has been marked as having reached consensus. + + ## Parameters + - `block_number`: The number of the block to check. + + ## Returns + - `true` if the block exists and has reached consensus. + - `false` otherwise. + """ + @spec rollup_block_exists?(FullBlock.block_number()) :: boolean() + def rollup_block_exists?(block_number) do + query = + from( + block in FullBlock, + where: block.number == ^block_number and block.consensus == true + ) + + Repo.exists?(query, timeout: :infinity) + end + + @doc """ + Retrieves full details of rollup blocks, including associated transactions, for each + block number specified in the input list. + + ## Parameters + - `list_of_block_numbers`: A list of block numbers for which full block details are to be retrieved. + + ## Returns + - A list of `Explorer.Chain.Block` instances containing detailed information for each + block number in the input list. Returns an empty list if no blocks are found for the given numbers. + """ + @spec rollup_blocks([FullBlock.block_number()]) :: [FullBlock.t()] + def rollup_blocks(list_of_block_numbers) + + def rollup_blocks([]), do: [] + + def rollup_blocks(list_of_block_numbers) do + query = + from( + block in FullBlock, + where: block.number in ^list_of_block_numbers + ) + + query + # :optional is used since a block may not have any transactions + |> Chain.join_associations(%{:transactions => :optional}) + |> Repo.all() end end diff --git a/apps/indexer/lib/indexer/fetcher/arbitrum/utils/db.ex b/apps/indexer/lib/indexer/fetcher/arbitrum/utils/db.ex index 66fe5b7035..8c9c640e12 100644 --- a/apps/indexer/lib/indexer/fetcher/arbitrum/utils/db.ex +++ b/apps/indexer/lib/indexer/fetcher/arbitrum/utils/db.ex @@ -3,11 +3,9 @@ defmodule Indexer.Fetcher.Arbitrum.Utils.Db do Common functions to simplify DB routines for Indexer.Fetcher.Arbitrum fetchers """ - import Ecto.Query, only: [from: 2] - import Indexer.Fetcher.Arbitrum.Utils.Logging, only: [log_warning: 1, log_info: 1] - alias Explorer.{Chain, Repo} + alias Explorer.Chain alias Explorer.Chain.Arbitrum alias Explorer.Chain.Arbitrum.Reader alias Explorer.Chain.Block, as: FullBlock @@ -388,8 +386,7 @@ defmodule Indexer.Fetcher.Arbitrum.Utils.Db do end @doc """ - Retrieves full details of rollup blocks, including associated transactions, for each - block number specified in the input list. + Retrieves full details of rollup blocks, including associated transactions, for each block number specified in the input list. ## Parameters - `list_of_block_numbers`: A list of block numbers for which full block details are to be retrieved. @@ -399,23 +396,7 @@ defmodule Indexer.Fetcher.Arbitrum.Utils.Db do block number in the input list. Returns an empty list if no blocks are found for the given numbers. """ @spec rollup_blocks([FullBlock.block_number()]) :: [FullBlock.t()] - def rollup_blocks(list_of_block_numbers) - - def rollup_blocks([]), do: [] - - def rollup_blocks(list_of_block_numbers) - when is_list(list_of_block_numbers) do - query = - from( - block in FullBlock, - where: block.number in ^list_of_block_numbers - ) - - query - # :optional is used since a block may not have any transactions - |> Chain.join_associations(%{:transactions => :optional}) - |> Repo.all(timeout: :infinity) - end + def rollup_blocks(list_of_block_numbers), do: Reader.rollup_blocks(list_of_block_numbers) @doc """ Retrieves unfinalized L1 transactions that are involved in changing the statuses @@ -942,13 +923,7 @@ defmodule Indexer.Fetcher.Arbitrum.Utils.Db do # the block just after it rollup_tail = Application.get_all_env(:indexer)[:first_block] + 1 - query = - from( - block in FullBlock, - where: block.number == ^rollup_tail and block.consensus == true - ) - - if(is_nil(query |> Repo.one()), do: false, else: true) + Reader.rollup_block_exists?(rollup_tail) end @spec lifecycle_transaction_to_map(Arbitrum.LifecycleTransaction.t()) :: Arbitrum.LifecycleTransaction.to_import()