-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an initial implementation of the config/spec
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters