Skip to content

Commit 0ad9cd5

Browse files
committed
Added an initial implementation of the config/spec
1 parent dd3c1bb commit 0ad9cd5

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

lib/beacon_api/router.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ defmodule BeaconApi.Router do
1717
get("/states/:state_id/finality_checkpoints", BeaconController, :get_finality_checkpoints)
1818
end
1919

20+
scope "config" do
21+
get("/spec", ConfigController, :get_spec)
22+
end
23+
2024
scope "/node" do
2125
get("/health", NodeController, :health)
2226
get("/identity", NodeController, :identity)

lib/chain_spec/chain_spec.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ defmodule ChainSpec do
1919
# NOTE: this only works correctly for Capella
2020
def get(name), do: get_config().get(name)
2121

22+
def get_all(), do: get_config().get_all()
23+
2224
def get_genesis_validators_root() do
2325
Application.fetch_env!(:lambda_ethereum_consensus, __MODULE__)
2426
|> Keyword.fetch!(:genesis_validators_root)

0 commit comments

Comments
 (0)