From e79724a111f270ac2b71eb62c2d4dd58253bc004 Mon Sep 17 00:00:00 2001 From: igamigo Date: Thu, 21 Sep 2023 10:40:34 -0300 Subject: [PATCH] Put gateway functionality behind flag (#181) * put gateway functionality behind env variable flag * update readme * update doc * change toc back * fix warning --- .github/workflows/deploy-testing.yaml | 1 + README.md | 7 +++ ansible/playbooks/deployment.yaml | 1 + ansible/playbooks/templates/.env.j2 | 1 + config/runtime.exs | 3 +- docs/explorer.md | 2 + lib/starknet_explorer/block/block_updater.ex | 11 +++- lib/starknet_explorer/block/block_utils.ex | 22 ++++++-- .../live/pages/block_detail.ex | 53 +++++++++++-------- .../live/transaction_live.ex | 8 +-- lib/starknet_explorer_web/live/utils.ex | 2 + 11 files changed, 77 insertions(+), 34 deletions(-) diff --git a/.github/workflows/deploy-testing.yaml b/.github/workflows/deploy-testing.yaml index d71cef19..f27ac3ee 100644 --- a/.github/workflows/deploy-testing.yaml +++ b/.github/workflows/deploy-testing.yaml @@ -50,6 +50,7 @@ jobs: GIT_BRANCH: ${{ github.head_ref || github.ref_name }} ANSIBLE_STDOUT_CALLBACK: "yaml" ENABLE_LISTENER: "true" + ENABLE_GATEWAY_DATA: "true" CONTINUATION_TOKEN_FORMAT: "large" run: | ansible-playbook -i ansible/inventory.yaml ansible/playbooks/deployment.yaml diff --git a/README.md b/README.md index 8bba27e0..b6f649b3 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ - [Requirements](#requirements) - [Local development](#local-development) + - [Setup](#setup) - [RPC Provider](#rpc-provider) - [RPC with Juno](#rpc-with-juno) - [Database](#database) @@ -47,6 +48,12 @@ export TESTNET_RPC_API_HOST=testnet_rpc_hostname export TESTNET_2_RPC_API_HOST=testnet_2_rpc_hostname ``` +Some of the desired data related to Starknet is not currently available through the RPC standard. Because of this, the explorer also gets information through the feeder gateway API. In order to enable this functionality, the variable `ENABLE_GATEWAY_DATA` needs to be set to `true` (if nothing is set, it will default to `false`). Note that this is only pertinent to the Starknet networks and not other particular networks that are compatible with the RPC standard. + +```bash +export ENABLE_GATEWAY_DATA=true +`````` + ### RPC with Juno ```bash diff --git a/ansible/playbooks/deployment.yaml b/ansible/playbooks/deployment.yaml index 775a3221..71691115 100644 --- a/ansible/playbooks/deployment.yaml +++ b/ansible/playbooks/deployment.yaml @@ -80,6 +80,7 @@ aws_region: "{{ lookup('ansible.builtin.env', 'AWS_REGION') }}" prover_storage: "{{ lookup('ansible.builtin.env', 'PROVER_STORAGE') }}" enable_listener: "{{ lookup('ansible.builtin.env', 'ENABLE_LISTENER') }}" + enable_gateway_data: "{{ lookup('ansible.builtin.env', 'ENABLE_GATEWAY_DATA') }}" - name: Create user systemd directory ansible.builtin.file: diff --git a/ansible/playbooks/templates/.env.j2 b/ansible/playbooks/templates/.env.j2 index 4f87032b..836fed1e 100644 --- a/ansible/playbooks/templates/.env.j2 +++ b/ansible/playbooks/templates/.env.j2 @@ -10,3 +10,4 @@ AWS_SECRET_ACCESS_KEY={{ aws_secret_access_key }} AWS_REGION={{ aws_region }} PROVER_STORAGE={{ prover_storage }} ENABLE_LISTENER={{ enable_listener }} +ENABLE_GATEWAY_DATA={{ enable_gateway_data }} diff --git a/config/runtime.exs b/config/runtime.exs index ffce91a6..83be8026 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -48,7 +48,8 @@ config :starknet_explorer, testnet_host: testnet_rpc_host, testnet_2_host: testnet_2_rpc_host, enable_listener: enable_listener?, - continuation_token_format: continuation_token_format + continuation_token_format: continuation_token_format, + enable_gateway_data: System.get_env("ENABLE_GATEWAY_DATA") == "true" config :starknet_explorer, rpc_host: rpc_host diff --git a/docs/explorer.md b/docs/explorer.md index 0c69517a..b6b76c49 100644 --- a/docs/explorer.md +++ b/docs/explorer.md @@ -44,6 +44,8 @@ deployed contracts and so on. So, what we're missing is: [starknet_docs_contract_classes]: https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/contract-classes/ [starknet_docs_events]: https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/starknet-events/ +> Gateway data is fetched and shown only if the environment variable `ENABLE_GATEWAY_DATA` is set to `true`. Otherwise, that logic is skipped to be able to support networks that are only compatible with the Starknet RPC standard. + ### Explorer data As a way to have a cache and a full view into the network's data, we're building a sqlite database. Currently it has tables for blocks, diff --git a/lib/starknet_explorer/block/block_updater.ex b/lib/starknet_explorer/block/block_updater.ex index 84d21425..8dcb03b8 100644 --- a/lib/starknet_explorer/block/block_updater.ex +++ b/lib/starknet_explorer/block/block_updater.ex @@ -14,8 +14,15 @@ defmodule StarknetExplorer.BlockUpdater do network: network } - Logger.info("starting block updater") - Process.send_after(self(), :update_gas_fee, 100) + case Application.get_env(:starknet_explorer, :enable_gateway_data) do + true -> + Logger.info("starting block updater") + Process.send_after(self(), :update_gas_fee, 100) + + _ -> + :skip + end + {:ok, state} end diff --git a/lib/starknet_explorer/block/block_utils.ex b/lib/starknet_explorer/block/block_utils.ex index 954ec61d..199eb9df 100644 --- a/lib/starknet_explorer/block/block_utils.ex +++ b/lib/starknet_explorer/block/block_utils.ex @@ -20,15 +20,27 @@ defmodule StarknetExplorer.BlockUtils do end def store_block(block = %{"block_number" => block_number}, network) do - with {:ok, receipts} <- receipts_for_block(block, network), - {:ok, gateway_block = %{"gas_price" => gas_price}} <- - StarknetExplorer.Gateway.fetch_block(block_number, network) do + with {:ok, receipts} <- receipts_for_block(block, network) do block = block - |> Map.put("gas_fee_in_wei", gas_price) - |> Map.put("execution_resources", calculate_gateway_block_steps(gateway_block)) |> Map.put("network", network) + block = + case Application.get_env(:starknet_explorer, :enable_gateway_data) do + true -> + {:ok, gateway_block = %{"gas_price" => gas_price}} = + StarknetExplorer.Gateway.fetch_block(block_number, network) + + block + |> Map.put("gas_fee_in_wei", gas_price) + |> Map.put("execution_resources", calculate_gateway_block_steps(gateway_block)) + + _ -> + block + |> Map.put("gas_fee_in_wei", "0") + |> Map.put("execution_resources", 0) + end + Block.insert_from_rpc_response(block, receipts, network) end end diff --git a/lib/starknet_explorer_web/live/pages/block_detail.ex b/lib/starknet_explorer_web/live/pages/block_detail.ex index 9b062c75..75d93e78 100644 --- a/lib/starknet_explorer_web/live/pages/block_detail.ex +++ b/lib/starknet_explorer_web/live/pages/block_detail.ex @@ -152,7 +152,14 @@ defmodule StarknetExplorerWeb.BlockDetailLive do block_age: Utils.get_block_age(block) ] - Process.send_after(self(), :get_gateway_information, 200) + case Application.get_env(:starknet_explorer, :enable_gateway_data) do + true -> + Process.send_after(self(), :get_gateway_information, 200) + + _ -> + :skip + end + {:ok, assign(socket, assigns)} end @@ -691,32 +698,34 @@ defmodule StarknetExplorerWeb.BlockDetailLive do -
-
- Gas Price -
-
-
-
- <%= @gas_price %> ETH + <%= if Application.get_env(:starknet_explorer, :enable_gateway_data) do %> +
+
+ Gas Price +
+
+
+
+ <%= @gas_price %> ETH +
-
-
-
- Total execution resources -
-
-
- <%= "#{@execution_resources} steps" %> +
+
+ Total execution resources +
+
+
+ <%= "#{@execution_resources} steps" %> +
-
+ <% end %> """ end diff --git a/lib/starknet_explorer_web/live/transaction_live.ex b/lib/starknet_explorer_web/live/transaction_live.ex index 808b3704..f1c5b353 100644 --- a/lib/starknet_explorer_web/live/transaction_live.ex +++ b/lib/starknet_explorer_web/live/transaction_live.ex @@ -1,7 +1,7 @@ defmodule StarknetExplorerWeb.TransactionLive do use StarknetExplorerWeb, :live_view alias StarknetExplorerWeb.Utils - alias StarknetExplorer.{Data, Message, Rpc, BlockUtils} + alias StarknetExplorer.{Data, Message, Rpc} defp transaction_header(assigns) do ~H""" @@ -584,11 +584,11 @@ defmodule StarknetExplorerWeb.TransactionLive do transaction = case transaction.type do "L1_HANDLER" -> - max_fee = Utils.hex_wei_to_eth(transaction.max_fee) - transaction |> Map.put(:max_fee, max_fee) + transaction _ -> - transaction + max_fee = Utils.hex_wei_to_eth(transaction.max_fee) + transaction |> Map.put(:max_fee, max_fee) end receipt = transaction.receipt |> Map.put(:actual_fee, actual_fee) diff --git a/lib/starknet_explorer_web/live/utils.ex b/lib/starknet_explorer_web/live/utils.ex index 82644827..3df1b5e1 100644 --- a/lib/starknet_explorer_web/live/utils.ex +++ b/lib/starknet_explorer_web/live/utils.ex @@ -44,6 +44,8 @@ defmodule StarknetExplorerWeb.Utils do |> Decimal.new() |> Decimal.div(@wei_to_eth_constant) |> Decimal.to_string(:normal) + |> String.trim_trailing("0") + |> String.replace_suffix(".", ".0") end def atomize_keys(map) when is_map(map) do