Skip to content

Commit

Permalink
Merge branch 'main' into dockerizing-beam
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Apr 23, 2024
2 parents e4336a8 + d2728c3 commit 42fd7ec
Show file tree
Hide file tree
Showing 120 changed files with 1,170 additions and 663 deletions.
2 changes: 1 addition & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
{Credo.Check.Readability.ModuleDoc, []},
{Credo.Check.Readability.ModuleNames, []},
{Credo.Check.Readability.ParenthesesInCondition, []},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, [parens: true]},
{Credo.Check.Readability.PipeIntoAnonymousFunctions, []},
{Credo.Check.Readability.PredicateFunctionNames, []},
{Credo.Check.Readability.PreferImplicitTry, []},
Expand Down
54 changes: 33 additions & 21 deletions config/runtime.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import Config
alias LambdaEthereumConsensus.Beacon.StoreSetup
alias LambdaEthereumConsensus.ForkChoice
alias LambdaEthereumConsensus.SszEx
alias Types.BeaconStateDeneb

switches = [
network: :string,
Expand Down Expand Up @@ -41,8 +45,10 @@ validator_file = Keyword.get(args, :validator_file)
enable_beacon_api = Keyword.get(args, :beacon_api, false)
beacon_api_port = Keyword.get(args, :beacon_api_port, 4000)

config :lambda_ethereum_consensus, LambdaEthereumConsensus.ForkChoice,
checkpoint_sync_url: checkpoint_sync_url
if not is_nil(testnet_dir) and not is_nil(checkpoint_sync_url) do
IO.puts("Both checkpoint sync and testnet url specified (only one should be specified).")
System.halt(2)
end

valid_modes = ["full", "db"]
raw_mode = Keyword.get(args, :mode, "full")
Expand All @@ -57,43 +63,49 @@ mode =

config :lambda_ethereum_consensus, LambdaEthereumConsensus, mode: mode

datadir = Keyword.get(args, :datadir, "level_db/#{network}")
# DB setup
default_datadir =
case testnet_dir do
nil -> "level_db/#{network}"
_ -> "level_db/local_testnet"
end

datadir = Keyword.get(args, :datadir, default_datadir)
File.mkdir_p!(datadir)
config :lambda_ethereum_consensus, LambdaEthereumConsensus.Store.Db, dir: datadir

chain_config =
# Network setup
{chain_config, bootnodes} =
case testnet_dir do
nil ->
config = ConfigUtils.parse_config(network)
bootnodes = YamlElixir.read_from_file!("config/networks/#{network}/boot_enr.yaml")

%{
config: config,
genesis_validators_root: config.genesis_validators_root(),
bootnodes: bootnodes
}
{config, bootnodes}

testnet_dir ->
Path.join(testnet_dir, "config.yaml") |> CustomConfig.load_from_file!()
bootnodes = Path.join(testnet_dir, "boot_enr.yaml") |> YamlElixir.read_from_file!()
{CustomConfig, bootnodes}
end

# TODO: compute this from the genesis block
genesis_validators_root = <<0::256>>
# We use put_env here as we need this immediately after to read the state.
Application.put_env(:lambda_ethereum_consensus, ChainSpec, config: chain_config)

bootnodes = Path.join(testnet_dir, "boot_enr.yaml") |> YamlElixir.read_from_file!()
strategy = StoreSetup.make_strategy!(testnet_dir, checkpoint_sync_url)

%{
config: CustomConfig,
genesis_validators_root: genesis_validators_root,
bootnodes: bootnodes
}
genesis_validators_root =
case strategy do
{:file, state} -> state.genesis_validators_root
_ -> chain_config.genesis_validators_root()
end

config :lambda_ethereum_consensus, ChainSpec,
config: Map.fetch!(chain_config, :config),
genesis_validators_root: Map.fetch!(chain_config, :genesis_validators_root)
config: chain_config,
genesis_validators_root: genesis_validators_root

config :lambda_ethereum_consensus, StoreSetup, strategy: strategy

# Configures peer discovery
bootnodes = Map.fetch!(chain_config, :bootnodes)
config :lambda_ethereum_consensus, :discovery, port: 9000, bootnodes: bootnodes

# Engine API
Expand Down
2 changes: 1 addition & 1 deletion lib/beacon_api/api_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ defmodule BeaconApi.ApiSpec do
|> OpenApiSpex.OpenApi.Decode.decode()

@impl OpenApi
def spec, do: @ethspec
def spec(), do: @ethspec
end
4 changes: 2 additions & 2 deletions lib/beacon_api/beacon_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule BeaconApi do
those modules here.
"""

def router do
def router() do
quote do
use Phoenix.Router, helpers: false

Expand All @@ -27,7 +27,7 @@ defmodule BeaconApi do
end
end

def controller do
def controller() do
quote do
use Phoenix.Controller,
formats: [:json]
Expand Down
6 changes: 3 additions & 3 deletions lib/chain_spec/chain_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ defmodule ChainSpec do
Single entrypoint for fetching chain-specific constants.
"""

def get_config,
def get_config(),
do: Application.fetch_env!(:lambda_ethereum_consensus, __MODULE__) |> Keyword.fetch!(:config)

def get_preset, do: get_config().get("PRESET_BASE") |> String.to_atom()
def get_preset(), do: get_config().get("PRESET_BASE") |> String.to_atom()

def get_fork_version_for_epoch(epoch) do
if epoch >= get("DENEB_FORK_EPOCH") do
Expand All @@ -19,7 +19,7 @@ defmodule ChainSpec do
# NOTE: this only works correctly for Capella
def get(name), do: get_config().get(name)

def get_genesis_validators_root do
def get_genesis_validators_root() do
Application.fetch_env!(:lambda_ethereum_consensus, __MODULE__)
|> Keyword.fetch!(:genesis_validators_root)
end
Expand Down
28 changes: 24 additions & 4 deletions lib/chain_spec/configs/custom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@ defmodule CustomConfig do
def load_from_file!(path) do
config = ConfigUtils.load_config_from_file!(path)
preset = Map.fetch!(config, "PRESET_BASE") |> ConfigUtils.parse_preset()
merged_config = Map.merge(preset.get_preset(), config)
base_config = Map.fetch!(config, "CONFIG_NAME") |> ConfigUtils.parse_config()

merged_config =
preset.get_preset()
|> Map.merge(base_config.get_all())
|> Map.merge(config)

Application.put_env(:lambda_ethereum_consensus, __MODULE__, merged: merged_config)
end

defp get_config,
do: Application.get_env(:lambda_ethereum_consensus, __MODULE__) |> Keyword.fetch!(:merged)
@impl GenConfig
def get_all() do
Application.get_env(:lambda_ethereum_consensus, __MODULE__)
|> Keyword.fetch!(:merged)
|> Map.new(fn {k, v} -> {k, parse_int(v)} end)
end

@impl GenConfig
def get(key), do: get_config() |> Map.fetch!(key)
def get(key), do: get_all() |> Map.fetch!(key)

# Parses as integer if parsable. If not, returns original value.
defp parse_int(v) when is_binary(v) do
case Integer.parse(v) do
{i, ""} -> i
_ -> v
end
end

defp parse_int(v), do: v
end
8 changes: 8 additions & 0 deletions lib/chain_spec/configs/gen_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ defmodule ChainSpec.GenConfig do

@impl unquote(__MODULE__)
def get(key), do: Map.fetch!(@__unified, key)

@impl unquote(__MODULE__)
def get_all(), do: @__unified
end
end

@doc """
Fetches a value from config.
"""
@callback get(String.t()) :: term()

@doc """
Fetches the full config dictionary.
"""
@callback get_all() :: map()
end
2 changes: 1 addition & 1 deletion lib/chain_spec/configs/gnosis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ defmodule GnosisConfig do
genesis_validators_root =
Base.decode16!("F5DCB5564E829AAB27264B9BECD5DFAA017085611224CB3036F573368DBB9D47")

def genesis_validators_root, do: unquote(genesis_validators_root)
def genesis_validators_root(), do: unquote(genesis_validators_root)
end
2 changes: 1 addition & 1 deletion lib/chain_spec/configs/holesky.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ defmodule HoleskyConfig do
genesis_validators_root =
Base.decode16!("9143AA7C615A7F7115E2B6AAC319C03529DF8242AE705FBA9DF39B79C59FA8B1")

def genesis_validators_root, do: unquote(genesis_validators_root)
def genesis_validators_root(), do: unquote(genesis_validators_root)
end
2 changes: 1 addition & 1 deletion lib/chain_spec/configs/mainnet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ defmodule MainnetConfig do
genesis_validators_root =
Base.decode16!("4B363DB94E286120D76EB905340FDD4E54BFE9F06BF33FF6CF5AD27F511BFE95")

def genesis_validators_root, do: unquote(genesis_validators_root)
def genesis_validators_root(), do: unquote(genesis_validators_root)
end
2 changes: 1 addition & 1 deletion lib/chain_spec/configs/minimal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ defmodule MinimalConfig do
"""
use ChainSpec.GenConfig, file: "config/networks/minimal/config.yaml"

def genesis_validators_root, do: <<0::256>>
def genesis_validators_root(), do: <<0::256>>
end
2 changes: 1 addition & 1 deletion lib/chain_spec/configs/sepolia.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ defmodule SepoliaConfig do
genesis_validators_root =
Base.decode16!("D8EA171F3C94AEA21EBC42A1ED61052ACF3F9209C00E4EFBAADDAC09ED9B8078")

def genesis_validators_root, do: unquote(genesis_validators_root)
def genesis_validators_root(), do: unquote(genesis_validators_root)
end
2 changes: 1 addition & 1 deletion lib/chain_spec/presets/gen_preset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule ChainSpec.GenPreset do
@behaviour unquote(__MODULE__)

@impl unquote(__MODULE__)
def get_preset, do: @__parsed_preset
def get_preset(), do: @__parsed_preset
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/chain_spec/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule ConfigUtils do
def load_config_from_file!(path) do
path
|> File.read!()
|> String.replace(~r/(0x[0-9a-fA-F]+)/, "'\\g{1}'")
|> String.replace(~r/ (0x[0-9a-fA-F]+)/, " '\\g{1}'")
|> YamlElixir.read_from_string!()
|> Stream.map(fn
{k, "0x" <> hash} -> {k, Base.decode16!(hash, case: :mixed)}
Expand Down
Loading

0 comments on commit 42fd7ec

Please sign in to comment.