From 3ab544f5008ba494f83b331511c8a219c70dc2d1 Mon Sep 17 00:00:00 2001 From: Qwerty5Uiop Date: Fri, 22 Dec 2023 19:05:21 +0600 Subject: [PATCH] Set trace_block_ranges to first..last if not set --- .../ethereum_jsonrpc/utility/ranges_helper.ex | 45 +++++++++---------- .../explorer/chain/import/runner/blocks.ex | 6 +-- .../import/runner/internal_transactions.ex | 25 ++++------- config/runtime.exs | 15 +++++-- 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/utility/ranges_helper.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/utility/ranges_helper.ex index 75f00bccde..f7220b044d 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/utility/ranges_helper.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/utility/ranges_helper.ex @@ -4,38 +4,37 @@ defmodule EthereumJSONRPC.Utility.RangesHelper do Helper for ranges manipulations. """ + @default_trace_block_ranges "0..latest" + @spec traceable_block_number?(integer() | nil) :: boolean() def traceable_block_number?(block_number) do - case Application.get_env(:indexer, :trace_block_ranges) do - nil -> - min_block = Application.get_env(:indexer, :trace_first_block) - max_block = Application.get_env(:indexer, :trace_last_block) - - !block_number || (block_number >= min_block && if(max_block, do: block_number <= max_block, else: true)) - - block_ranges -> - parsed_ranges = parse_block_ranges(block_ranges) - - number_in_ranges?(block_number, parsed_ranges) + if trace_ranges_present?() do + number_in_ranges?(block_number, get_trace_block_ranges()) + else + true end end @spec filter_traceable_block_numbers([integer()]) :: [integer()] def filter_traceable_block_numbers(block_numbers) do - case Application.get_env(:indexer, :trace_block_ranges) do - nil -> - min_block = Application.get_env(:indexer, :trace_first_block) - max_block = Application.get_env(:indexer, :trace_last_block) - - Enum.filter(block_numbers, fn block_number -> - block_number >= min_block && if max_block, do: block_number <= max_block, else: true - end) + if trace_ranges_present?() do + trace_block_ranges = get_trace_block_ranges() + Enum.filter(block_numbers, &number_in_ranges?(&1, trace_block_ranges)) + else + block_numbers + end + end - block_ranges -> - parsed_ranges = parse_block_ranges(block_ranges) + @spec trace_ranges_present? :: boolean() + def trace_ranges_present? do + Application.get_env(:indexer, :trace_block_ranges) != @default_trace_block_ranges + end - Enum.filter(block_numbers, &number_in_ranges?(&1, parsed_ranges)) - end + @spec get_trace_block_ranges :: [Range.t() | integer()] + def get_trace_block_ranges do + :indexer + |> Application.get_env(:trace_block_ranges) + |> parse_block_ranges() end @spec parse_block_ranges(binary()) :: [Range.t() | integer()] diff --git a/apps/explorer/lib/explorer/chain/import/runner/blocks.ex b/apps/explorer/lib/explorer/chain/import/runner/blocks.ex index 6ff8ee7d64..83a426b313 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/blocks.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/blocks.ex @@ -887,11 +887,7 @@ defmodule Explorer.Chain.Import.Runner.Blocks do end defp filter_by_height_range(blocks, filter_func) do - trace_block_ranges = Application.get_env(:indexer, :trace_block_ranges) - minimal_block_height = Application.get_env(:indexer, :trace_first_block) - maximal_block_height = Application.get_env(:indexer, :trace_last_block) - - if trace_block_ranges || minimal_block_height > 0 || maximal_block_height do + if RangesHelper.trace_ranges_present?() do Enum.filter(blocks, &filter_func.(&1)) else blocks diff --git a/apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex b/apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex index 4caf850df1..2ef9cfe1fe 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex @@ -751,24 +751,15 @@ defmodule Explorer.Chain.Import.Runner.InternalTransactions do end defp traceable_blocks_dynamic_query do - case Application.get_env(:indexer, :trace_block_ranges) do - nil -> - min_block = Application.get_env(:indexer, :trace_first_block) - max_block = Application.get_env(:indexer, :trace_last_block) + if RangesHelper.trace_ranges_present?() do + block_ranges = RangesHelper.get_trace_block_ranges() - filter_by_min_dynamic = dynamic([block], block.number > ^min_block) - - if max_block, - do: dynamic([block], ^filter_by_min_dynamic and block.number < ^max_block), - else: filter_by_min_dynamic - - block_ranges -> - parsed_ranges = RangesHelper.parse_block_ranges(block_ranges) - - Enum.reduce(parsed_ranges, dynamic([_], false), fn - _from.._to = range, acc -> dynamic([block], ^acc or block.number in ^range) - num_to_latest, acc -> dynamic([block], ^acc or block.number >= ^num_to_latest) - end) + Enum.reduce(block_ranges, dynamic([_], false), fn + _from.._to = range, acc -> dynamic([block], ^acc or block.number in ^range) + num_to_latest, acc -> dynamic([block], ^acc or block.number >= ^num_to_latest) + end) + else + dynamic([_], true) end end end diff --git a/config/runtime.exs b/config/runtime.exs index e8fec294af..f876325abb 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -459,15 +459,24 @@ config :explorer, Explorer.MicroserviceInterfaces.BENS, ### Indexer ### ############### +trace_first_block = ConfigHelper.parse_integer_env_var("TRACE_FIRST_BLOCK", 0) +trace_last_block = ConfigHelper.parse_integer_or_nil_env_var("TRACE_LAST_BLOCK") + +trace_block_ranges = + case ConfigHelper.safe_get_env("TRACE_BLOCK_RANGES", nil) do + "" -> "#{trace_first_block}..#{trace_last_block || "latest"}" + ranges -> ranges + end + config :indexer, block_transformer: ConfigHelper.block_transformer(), metadata_updater_milliseconds_interval: ConfigHelper.parse_time_env_var("TOKEN_METADATA_UPDATE_INTERVAL", "48h"), block_ranges: System.get_env("BLOCK_RANGES"), first_block: ConfigHelper.parse_integer_env_var("FIRST_BLOCK", 0), last_block: ConfigHelper.parse_integer_or_nil_env_var("LAST_BLOCK"), - trace_block_ranges: System.get_env("TRACE_BLOCK_RANGES"), - trace_first_block: ConfigHelper.parse_integer_env_var("TRACE_FIRST_BLOCK", 0), - trace_last_block: ConfigHelper.parse_integer_or_nil_env_var("TRACE_LAST_BLOCK"), + trace_block_ranges: trace_block_ranges, + trace_first_block: trace_first_block, + trace_last_block: trace_last_block, fetch_rewards_way: System.get_env("FETCH_REWARDS_WAY", "trace_block"), memory_limit: ConfigHelper.indexer_memory_limit(), receipts_batch_size: ConfigHelper.parse_integer_env_var("INDEXER_RECEIPTS_BATCH_SIZE", 250),