Skip to content

Commit

Permalink
Added an initial implementation of the config/spec
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-o committed Oct 16, 2024
1 parent dd3c1bb commit 0ad9cd5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
57 changes: 57 additions & 0 deletions lib/beacon_api/controllers/v1/config_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule BeaconApi.V1.ConfigController do
use BeaconApi, :controller
require Logger

alias BeaconApi.ApiSpec
alias BeaconApi.ErrorController
alias BeaconApi.Helpers
alias BeaconApi.Utils
alias LambdaEthereumConsensus.Store.BlockBySlot
alias LambdaEthereumConsensus.Store.Blocks
alias LambdaEthereumConsensus.Store.StoreDb

plug(OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true)

@chain_spec_removed_keys ["ATTESTATION_SUBNET_COUNT", "KZG_COMMITMENT_INCLUSION_PROOF_DEPTH", "UPDATE_TIMEOUT"]
@chain_spec_renamed_keys [{"MAXIMUM_GOSSIP_CLOCK_DISPARITY", "MAXIMUM_GOSSIP_CLOCK_DISPARITY_MILLIS"}]
@chain_spec_hex_fields [
"TERMINAL_BLOCK_HASH",
"GENESIS_FORK_VERSION",
"ALTAIR_FORK_VERSION",
"BELLATRIX_FORK_VERSION",
"CAPELLA_FORK_VERSION",
"DENEB_FORK_VERSION",
"ELECTRA_FORK_VERSION",
"DEPOSIT_CONTRACT_ADDRESS",
"MESSAGE_DOMAIN_INVALID_SNAPPY",
"MESSAGE_DOMAIN_VALID_SNAPPY",
]

# NOTE: this function is required by OpenApiSpex, and should return the information
# of each specific endpoint. We just return the specific entry from the parsed spec.
def open_api_operation(:get_spec),
do: ApiSpec.spec().paths["/eth/v1/config/spec"].get

@spec get_spec(Plug.Conn.t(), any) :: Plug.Conn.t()
def get_spec(conn, _params), do: json(conn, %{"data" => chain_spec()})

defp chain_spec() do
ChainSpec.get_all()
|> Map.drop(@chain_spec_removed_keys)
|> rename_keys(@chain_spec_renamed_keys)
|> Map.new(fn
{k, v} when is_integer(v) -> {k, Integer.to_string(v)}
{k, v} when k in @chain_spec_hex_fields -> {k, Utils.hex_encode(v)}
{k, v} -> {k, v}
end)
end

defp rename_keys(config, renamed_keys) do
renamed_keys |> Enum.reduce(config, fn {old_key, new_key}, config ->
case Map.get(config, old_key) do
nil -> config
value -> Map.put_new(config, new_key, value) |> Map.delete(old_key)
end
end)
end
end
4 changes: 4 additions & 0 deletions lib/beacon_api/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ defmodule BeaconApi.Router do
get("/states/:state_id/finality_checkpoints", BeaconController, :get_finality_checkpoints)
end

scope "config" do
get("/spec", ConfigController, :get_spec)
end

scope "/node" do
get("/health", NodeController, :health)
get("/identity", NodeController, :identity)
Expand Down
2 changes: 2 additions & 0 deletions lib/chain_spec/chain_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ defmodule ChainSpec do
# NOTE: this only works correctly for Capella
def get(name), do: get_config().get(name)

def get_all(), do: get_config().get_all()

def get_genesis_validators_root() do
Application.fetch_env!(:lambda_ethereum_consensus, __MODULE__)
|> Keyword.fetch!(:genesis_validators_root)
Expand Down

0 comments on commit 0ad9cd5

Please sign in to comment.