Skip to content

Commit 072e7c3

Browse files
authored
feat: add "DB-only" mode (#692)
1 parent 2217c6a commit 072e7c3

File tree

2 files changed

+85
-36
lines changed

2 files changed

+85
-36
lines changed

config/runtime.exs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
import Config
22

3-
{args, _remaining_args, _errors} =
4-
OptionParser.parse(System.argv(),
5-
switches: [
6-
network: :string,
7-
checkpoint_sync: :string,
8-
execution_endpoint: :string,
9-
execution_jwt: :string,
10-
mock_execution: :boolean
11-
]
12-
)
3+
switches = [
4+
network: :string,
5+
checkpoint_sync: :string,
6+
execution_endpoint: :string,
7+
execution_jwt: :string,
8+
mock_execution: :boolean,
9+
mode: :string
10+
]
11+
12+
is_testing = Config.config_env() == :test
13+
14+
# NOTE: we ignore invalid options because `mix test` passes us all test flags
15+
option = if is_testing, do: :switches, else: :strict
16+
17+
{args, remaining_args} = OptionParser.parse!(System.argv(), [{option, switches}])
18+
19+
if not is_testing and not Enum.empty?(remaining_args) do
20+
invalid_arg = Enum.take(remaining_args, 1)
21+
IO.puts("Unexpected argument received: #{invalid_arg}")
22+
System.halt(1)
23+
end
1324

1425
network = Keyword.get(args, :network, "mainnet")
1526
checkpoint_sync = Keyword.get(args, :checkpoint_sync)
1627
execution_endpoint = Keyword.get(args, :execution_endpoint, "http://localhost:8551")
1728
jwt_path = Keyword.get(args, :execution_jwt)
18-
mock_execution = Keyword.get(args, :mock_execution, config_env() == :test or is_nil(jwt_path))
1929

2030
config :lambda_ethereum_consensus, LambdaEthereumConsensus.ForkChoice,
2131
checkpoint_sync: checkpoint_sync
@@ -26,6 +36,21 @@ configs_per_network = %{
2636
"sepolia" => SepoliaConfig
2737
}
2838

39+
valid_modes = ["full", "db"]
40+
raw_mode = Keyword.get(args, :mode, "full")
41+
42+
mode =
43+
if raw_mode in valid_modes do
44+
String.to_atom(raw_mode)
45+
else
46+
IO.puts("Invalid mode given. Valid modes are: #{Enum.join(valid_modes, ", ")}")
47+
System.halt(2)
48+
end
49+
50+
config :lambda_ethereum_consensus, LambdaEthereumConsensus, mode: mode
51+
52+
mock_execution = Keyword.get(args, :mock_execution, mode == :db or is_nil(jwt_path))
53+
2954
config :lambda_ethereum_consensus, ChainSpec, config: configs_per_network |> Map.fetch!(network)
3055

3156
bootnodes = YamlElixir.read_from_file!("config/networks/#{network}/bootnodes.yaml")

lib/lambda_ethereum_consensus/application.ex

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,11 @@ defmodule LambdaEthereumConsensus.Application do
88

99
@impl true
1010
def start(_type, _args) do
11-
checkpoint_sync =
12-
Application.fetch_env!(:lambda_ethereum_consensus, LambdaEthereumConsensus.ForkChoice)[
13-
:checkpoint_sync
14-
]
15-
16-
jwt_secret =
17-
Application.fetch_env!(
18-
:lambda_ethereum_consensus,
19-
LambdaEthereumConsensus.Execution.EngineApi
20-
)[:jwt_secret]
11+
mode = get_operation_mode()
2112

22-
if is_nil(jwt_secret) do
23-
Logger.warning(
24-
"[EngineAPI] A JWT secret is needed for communication with the execution engine. " <>
25-
"Please specify the file to load it from with the --execution-jwt flag."
26-
)
27-
end
13+
check_jwt_secret(mode)
2814

29-
children = [
30-
LambdaEthereumConsensus.Telemetry,
31-
LambdaEthereumConsensus.Store.Db,
32-
LambdaEthereumConsensus.Store.Blocks,
33-
LambdaEthereumConsensus.Store.BlockStates,
34-
{LambdaEthereumConsensus.Beacon.BeaconNode, [checkpoint_sync]},
35-
LambdaEthereumConsensus.P2P.Metadata,
36-
BeaconApi.Endpoint
37-
]
15+
children = get_children(mode)
3816

3917
# See https://hexdocs.pm/elixir/Supervisor.html
4018
# for other strategies and supported options
@@ -50,4 +28,50 @@ defmodule LambdaEthereumConsensus.Application do
5028
BeaconApi.Endpoint.config_change(changed, removed)
5129
:ok
5230
end
31+
32+
defp get_children(:db) do
33+
[
34+
LambdaEthereumConsensus.Telemetry,
35+
LambdaEthereumConsensus.Store.Db,
36+
LambdaEthereumConsensus.Store.Blocks,
37+
LambdaEthereumConsensus.Store.BlockStates
38+
]
39+
end
40+
41+
defp get_children(:full) do
42+
get_children(:db) ++
43+
[
44+
{LambdaEthereumConsensus.Beacon.BeaconNode, [checkpoint_sync_url()]},
45+
LambdaEthereumConsensus.P2P.Metadata,
46+
BeaconApi.Endpoint
47+
]
48+
end
49+
50+
def checkpoint_sync_url do
51+
Application.fetch_env!(:lambda_ethereum_consensus, LambdaEthereumConsensus.ForkChoice)
52+
|> Keyword.fetch!(:checkpoint_sync)
53+
end
54+
55+
defp get_operation_mode do
56+
Application.fetch_env!(:lambda_ethereum_consensus, LambdaEthereumConsensus)
57+
|> Keyword.fetch!(:mode)
58+
end
59+
60+
defp check_jwt_secret(:db), do: nil
61+
62+
defp check_jwt_secret(:full) do
63+
jwt_secret =
64+
Application.fetch_env!(
65+
:lambda_ethereum_consensus,
66+
LambdaEthereumConsensus.Execution.EngineApi
67+
)
68+
|> Keyword.fetch!(:jwt_secret)
69+
70+
if is_nil(jwt_secret) do
71+
Logger.warning(
72+
"[EngineAPI] A JWT secret is needed for communication with the execution engine. " <>
73+
"Please specify the file to load it from with the --execution-jwt flag."
74+
)
75+
end
76+
end
5377
end

0 commit comments

Comments
 (0)