|
| 1 | +defmodule BeaconApi.V1.ConfigController do |
| 2 | + use BeaconApi, :controller |
| 3 | + require Logger |
| 4 | + |
| 5 | + alias BeaconApi.ApiSpec |
| 6 | + alias BeaconApi.ErrorController |
| 7 | + alias BeaconApi.Helpers |
| 8 | + alias BeaconApi.Utils |
| 9 | + alias LambdaEthereumConsensus.Store.BlockBySlot |
| 10 | + alias LambdaEthereumConsensus.Store.Blocks |
| 11 | + alias LambdaEthereumConsensus.Store.StoreDb |
| 12 | + |
| 13 | + plug(OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true) |
| 14 | + |
| 15 | + @chain_spec_removed_keys ["ATTESTATION_SUBNET_COUNT", "KZG_COMMITMENT_INCLUSION_PROOF_DEPTH", "UPDATE_TIMEOUT"] |
| 16 | + @chain_spec_renamed_keys [{"MAXIMUM_GOSSIP_CLOCK_DISPARITY", "MAXIMUM_GOSSIP_CLOCK_DISPARITY_MILLIS"}] |
| 17 | + @chain_spec_hex_fields [ |
| 18 | + "TERMINAL_BLOCK_HASH", |
| 19 | + "GENESIS_FORK_VERSION", |
| 20 | + "ALTAIR_FORK_VERSION", |
| 21 | + "BELLATRIX_FORK_VERSION", |
| 22 | + "CAPELLA_FORK_VERSION", |
| 23 | + "DENEB_FORK_VERSION", |
| 24 | + "ELECTRA_FORK_VERSION", |
| 25 | + "DEPOSIT_CONTRACT_ADDRESS", |
| 26 | + "MESSAGE_DOMAIN_INVALID_SNAPPY", |
| 27 | + "MESSAGE_DOMAIN_VALID_SNAPPY", |
| 28 | + ] |
| 29 | + |
| 30 | + # NOTE: this function is required by OpenApiSpex, and should return the information |
| 31 | + # of each specific endpoint. We just return the specific entry from the parsed spec. |
| 32 | + def open_api_operation(:get_spec), |
| 33 | + do: ApiSpec.spec().paths["/eth/v1/config/spec"].get |
| 34 | + |
| 35 | + @spec get_spec(Plug.Conn.t(), any) :: Plug.Conn.t() |
| 36 | + def get_spec(conn, _params), do: json(conn, %{"data" => chain_spec()}) |
| 37 | + |
| 38 | + defp chain_spec() do |
| 39 | + ChainSpec.get_all() |
| 40 | + |> Map.drop(@chain_spec_removed_keys) |
| 41 | + |> rename_keys(@chain_spec_renamed_keys) |
| 42 | + |> Map.new(fn |
| 43 | + {k, v} when is_integer(v) -> {k, Integer.to_string(v)} |
| 44 | + {k, v} when k in @chain_spec_hex_fields -> {k, Utils.hex_encode(v)} |
| 45 | + {k, v} -> {k, v} |
| 46 | + end) |
| 47 | + end |
| 48 | + |
| 49 | + defp rename_keys(config, renamed_keys) do |
| 50 | + renamed_keys |> Enum.reduce(config, fn {old_key, new_key}, config -> |
| 51 | + case Map.get(config, old_key) do |
| 52 | + nil -> config |
| 53 | + value -> Map.put_new(config, new_key, value) |> Map.delete(old_key) |
| 54 | + end |
| 55 | + end) |
| 56 | + end |
| 57 | +end |
0 commit comments