From 0092b1a4c3690696486649d3bf30b66e0766b122 Mon Sep 17 00:00:00 2001 From: POA <33550681+poa@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:37:25 +0300 Subject: [PATCH] Draft --- .../lib/indexer/fetcher/zkevm/bridge_l1.ex | 86 +++++++------------ apps/indexer/lib/indexer/supervisor.ex | 8 +- config/runtime.exs | 15 ++++ config/runtime/prod.exs | 2 - 4 files changed, 49 insertions(+), 62 deletions(-) diff --git a/apps/indexer/lib/indexer/fetcher/zkevm/bridge_l1.ex b/apps/indexer/lib/indexer/fetcher/zkevm/bridge_l1.ex index 20679290e9..7f55641755 100644 --- a/apps/indexer/lib/indexer/fetcher/zkevm/bridge_l1.ex +++ b/apps/indexer/lib/indexer/fetcher/zkevm/bridge_l1.ex @@ -72,45 +72,26 @@ defmodule Indexer.Fetcher.Zkevm.BridgeL1 do with {:start_block_undefined, false} <- {:start_block_undefined, is_nil(env[:start_block])}, rpc = env[:rpc], {:rpc_undefined, false} <- {:rpc_undefined, is_nil(rpc)}, - {:deposit_manager_address_is_valid, true} <- - {:deposit_manager_address_is_valid, Helper.is_address_correct?(env[:deposit_manager_proxy])}, - {:ether_predicate_address_is_valid, true} <- - {:ether_predicate_address_is_valid, Helper.is_address_correct?(env[:ether_predicate_proxy])}, - {:erc20_predicate_address_is_valid, true} <- - {:erc20_predicate_address_is_valid, Helper.is_address_correct?(env[:erc20_predicate_proxy])}, - {:erc721_predicate_address_is_valid, true} <- - {:erc721_predicate_address_is_valid, - is_nil(env[:erc721_predicate_proxy]) or Helper.is_address_correct?(env[:erc721_predicate_proxy])}, - {:erc1155_predicate_address_is_valid, true} <- - {:erc1155_predicate_address_is_valid, - is_nil(env[:erc1155_predicate_proxy]) or Helper.is_address_correct?(env[:erc1155_predicate_proxy])}, - {:withdraw_manager_address_is_valid, true} <- - {:withdraw_manager_address_is_valid, Helper.is_address_correct?(env[:withdraw_manager_proxy])}, + {:bridge_contract_address_is_valid, true} <- {:bridge_contract_address_is_valid, Helper.is_address_correct?(env[:bridge_contract])}, start_block = parse_integer(env[:start_block]), false <- is_nil(start_block), true <- start_block > 0, {last_l1_block_number, last_l1_transaction_hash} <- get_last_l1_item(), - {:start_block_valid, true} <- - {:start_block_valid, start_block <= last_l1_block_number || last_l1_block_number == 0}, json_rpc_named_arguments = json_rpc_named_arguments(rpc), + {:ok, block_check_interval, safe_block, safe_block_is_latest} <- get_block_check_interval(json_rpc_named_arguments), + {:start_block_valid, true} <- {:start_block_valid, (start_block <= last_l1_block_number || last_l1_block_number == 0) && start_block <= safe_block}, {:ok, last_l1_tx} <- Helper.get_transaction_by_hash(last_l1_transaction_hash, json_rpc_named_arguments), - {:l1_tx_not_found, false} <- {:l1_tx_not_found, !is_nil(last_l1_transaction_hash) && is_nil(last_l1_tx)}, - {:ok, block_check_interval, latest_block} <- get_block_check_interval(json_rpc_named_arguments), - {:start_block_valid, true} <- {:start_block_valid, start_block <= latest_block} do + {:l1_tx_not_found, false} <- {:l1_tx_not_found, !is_nil(last_l1_transaction_hash) && is_nil(last_l1_tx)} do Process.send(self(), :reorg_monitor, []) Process.send(self(), :continue, []) {:noreply, %{ - deposit_manager_proxy: env[:deposit_manager_proxy], - ether_predicate_proxy: env[:ether_predicate_proxy], - erc20_predicate_proxy: env[:erc20_predicate_proxy], - erc721_predicate_proxy: env[:erc721_predicate_proxy], - erc1155_predicate_proxy: env[:erc1155_predicate_proxy], - withdraw_manager_proxy: env[:withdraw_manager_proxy], + bridge_contract: env[:bridge_contract], block_check_interval: block_check_interval, start_block: max(start_block, last_l1_block_number), - end_block: latest_block, + safe_block: safe_block, + safe_block_is_latest: safe_block_is_latest, json_rpc_named_arguments: json_rpc_named_arguments, reorg_monitor_prev_latest: 0 }} @@ -123,28 +104,8 @@ defmodule Indexer.Fetcher.Zkevm.BridgeL1 do Logger.error("L1 RPC URL is not defined.") {:stop, :normal, %{}} - {:deposit_manager_address_is_valid, false} -> - Logger.error("DepositManagerProxy contract address is invalid or not defined.") - {:stop, :normal, %{}} - - {:ether_predicate_address_is_valid, false} -> - Logger.error("EtherPredicateProxy contract address is invalid or not defined.") - {:stop, :normal, %{}} - - {:erc20_predicate_address_is_valid, false} -> - Logger.error("ERC20PredicateProxy contract address is invalid or not defined.") - {:stop, :normal, %{}} - - {:erc721_predicate_address_is_valid, false} -> - Logger.error("ERC721PredicateProxy contract address is invalid.") - {:stop, :normal, %{}} - - {:erc1155_predicate_address_is_valid, false} -> - Logger.error("ERC1155PredicateProxy contract address is invalid.") - {:stop, :normal, %{}} - - {:withdraw_manager_address_is_valid, false} -> - Logger.error("WithdrawManagerProxy contract address is invalid or not defined.") + {:bridge_contract_address_is_valid, false} -> + Logger.error("PolygonZkEVMBridge contract address is invalid or not defined.") {:stop, :normal, %{}} {:start_block_valid, false} -> @@ -160,7 +121,7 @@ defmodule Indexer.Fetcher.Zkevm.BridgeL1 do {:l1_tx_not_found, true} -> Logger.error( - "Cannot find last L1 transaction from RPC by its hash. Probably, there was a reorg on L1 chain. Please, check shibarium_bridge table." + "Cannot find last L1 transaction from RPC by its hash. Probably, there was a reorg on L1 chain. Please, check zkevm_bridge table." ) {:stop, :normal, %{}} @@ -288,16 +249,16 @@ defmodule Indexer.Fetcher.Zkevm.BridgeL1 do end defp get_block_check_interval(json_rpc_named_arguments) do - with {:ok, latest_block} <- Helper.get_block_number_by_tag("latest", json_rpc_named_arguments), - first_block = max(latest_block - @block_check_interval_range_size, 1), - {:ok, first_block_timestamp} <- Helper.get_block_timestamp_by_number(first_block, json_rpc_named_arguments), - {:ok, last_safe_block_timestamp} <- - Helper.get_block_timestamp_by_number(latest_block, json_rpc_named_arguments) do - block_check_interval = - ceil((last_safe_block_timestamp - first_block_timestamp) / (latest_block - first_block) * 1000 / 2) + {last_safe_block, safe_block_is_latest} = get_safe_block(json_rpc_named_arguments) + + first_block = max(latest_block - @block_check_interval_range_size, 1) + + with {:ok, first_block_timestamp} <- Helper.get_block_timestamp_by_number(first_block, json_rpc_named_arguments), + {:ok, last_safe_block_timestamp} <- Helper.get_block_timestamp_by_number(last_safe_block, json_rpc_named_arguments) do + block_check_interval = ceil((last_safe_block_timestamp - first_block_timestamp) / (last_safe_block - first_block) * 1000 / 2) Logger.info("Block check interval is calculated as #{block_check_interval} ms.") - {:ok, block_check_interval, latest_block} + {:ok, block_check_interval, last_safe_block, safe_block_is_latest} else {:error, error} -> {:error, "Failed to calculate block check interval due to #{inspect(error)}"} @@ -530,6 +491,17 @@ defmodule Indexer.Fetcher.Zkevm.BridgeL1 do ) end + defp get_safe_block(json_rpc_named_arguments) do + case Helper.get_block_number_by_tag("safe", json_rpc_named_arguments) do + {:ok, safe_block} -> + {safe_block, false} + + {:error, :not_found} -> + {:ok, latest_block} = Helper.get_block_number_by_tag("latest", json_rpc_named_arguments, 100_000_000) + {latest_block, true} + end + end + defp json_rpc_named_arguments(rpc_url) do [ transport: EthereumJSONRPC.HTTP, diff --git a/apps/indexer/lib/indexer/supervisor.ex b/apps/indexer/lib/indexer/supervisor.ex index aabb9fa85b..9ced4dee89 100644 --- a/apps/indexer/lib/indexer/supervisor.ex +++ b/apps/indexer/lib/indexer/supervisor.ex @@ -44,8 +44,6 @@ defmodule Indexer.Supervisor do Withdrawal } - alias Indexer.Fetcher.Zkevm.TransactionBatch - alias Indexer.Temporary.{ BlocksTransactionsMismatch, UncatalogedTokenTransfers, @@ -147,7 +145,11 @@ defmodule Indexer.Supervisor do [json_rpc_named_arguments: json_rpc_named_arguments, memory_monitor: memory_monitor] ]), configure(Indexer.Fetcher.Shibarium.L1.Supervisor, [[memory_monitor: memory_monitor]]), - configure(TransactionBatch.Supervisor, [ + configure(Indexer.Fetcher.Zkevm.BridgeL1.Supervisor, [[memory_monitor: memory_monitor]]), + configure(Indexer.Fetcher.Zkevm.BridgeL2.Supervisor, [ + [json_rpc_named_arguments: json_rpc_named_arguments, memory_monitor: memory_monitor] + ]), + configure(Indexer.Fetcher.Zkevm.TransactionBatch.Supervisor, [ [json_rpc_named_arguments: json_rpc_named_arguments, memory_monitor: memory_monitor] ]), {Indexer.Fetcher.Beacon.Blob.Supervisor, [[memory_monitor: memory_monitor]]}, diff --git a/config/runtime.exs b/config/runtime.exs index 6b6fbc9cf1..9c8e13684b 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -727,6 +727,21 @@ config :indexer, Indexer.Fetcher.Shibarium.L1.Supervisor, enabled: ConfigHelper. config :indexer, Indexer.Fetcher.Shibarium.L2.Supervisor, enabled: ConfigHelper.chain_type() == "shibarium" +config :indexer, Indexer.Fetcher.Zkevm.BridgeL1, + rpc: System.get_env("INDEXER_POLYGON_ZKEVM_L1_RPC"), + start_block: System.get_env("INDEXER_POLYGON_ZKEVM_L1_BRIDGE_START_BLOCK"), + bridge_contract: System.get_env("INDEXER_POLYGON_ZKEVM_L1_BRIDGE_CONTRACT"), + native_symbol: System.get_env("INDEXER_POLYGON_ZKEVM_L1_BRIDGE_NATIVE_SYMBOL", "ETH"), + native_decimals: ConfigHelper.parse_integer_env_var("INDEXER_POLYGON_ZKEVM_L1_BRIDGE_NATIVE_DECIMALS", 18) + +config :indexer, Indexer.Fetcher.Zkevm.BridgeL1.Supervisor, enabled: ConfigHelper.chain_type() == "polygon_zkevm" + +config :indexer, Indexer.Fetcher.Zkevm.BridgeL2, + start_block: System.get_env("INDEXER_POLYGON_ZKEVM_L2_BRIDGE_START_BLOCK"), + bridge_contract: System.get_env("INDEXER_POLYGON_ZKEVM_L2_BRIDGE_CONTRACT"), + +config :indexer, Indexer.Fetcher.Zkevm.BridgeL2.Supervisor, enabled: ConfigHelper.chain_type() == "polygon_zkevm" + config :indexer, Indexer.Fetcher.Zkevm.TransactionBatch, chunk_size: ConfigHelper.parse_integer_env_var("INDEXER_POLYGON_ZKEVM_BATCHES_CHUNK_SIZE", 20), recheck_interval: ConfigHelper.parse_integer_env_var("INDEXER_POLYGON_ZKEVM_BATCHES_RECHECK_INTERVAL", 60) diff --git a/config/runtime/prod.exs b/config/runtime/prod.exs index 42e253146b..21f40dc30a 100644 --- a/config/runtime/prod.exs +++ b/config/runtime/prod.exs @@ -78,8 +78,6 @@ config :explorer, Explorer.Repo.PolygonEdge, # Configures PolygonZkevm database config :explorer, Explorer.Repo.PolygonZkevm, url: System.get_env("DATABASE_URL"), - # actually this repo is not started, and its pool size remains unused. - # separating repos for different CHAIN_TYPE is implemented only for the sake of keeping DB schema update relevant to the current chain type pool_size: 1, ssl: ExplorerConfigHelper.ssl_enabled?()