Skip to content

Commit 24acec6

Browse files
authored
feat: add --listen-address and fix edge-cases (#1055)
1 parent cf7f38f commit 24acec6

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

config/runtime.exs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ switches = [
1818
log_file: :string,
1919
beacon_api: :boolean,
2020
beacon_api_port: :integer,
21+
listen_address: [:string, :keep],
2122
discovery_port: :integer,
2223
boot_nodes: :string,
2324
keystore_file: :string,
@@ -46,6 +47,7 @@ enable_metrics = Keyword.get(args, :metrics, false)
4647
metrics_port = Keyword.get(args, :metrics_port, if(enable_metrics, do: 9568, else: nil))
4748
beacon_api_port = Keyword.get(args, :beacon_api_port, nil)
4849
enable_beacon_api = Keyword.get(args, :beacon_api, not is_nil(beacon_api_port))
50+
listen_addresses = Keyword.get_values(args, :listen_address)
4951
discovery_port = Keyword.get(args, :discovery_port, 9000)
5052
cli_bootnodes = Keyword.get(args, :boot_nodes, "")
5153
keystore = Keyword.get(args, :keystore_file)
@@ -118,7 +120,10 @@ bootnodes =
118120
|> Enum.reject(&(&1 == ""))
119121
|> Enum.concat(bootnodes)
120122

121-
config :lambda_ethereum_consensus, :discovery, port: discovery_port, bootnodes: bootnodes
123+
config :lambda_ethereum_consensus, :libp2p,
124+
port: discovery_port,
125+
bootnodes: bootnodes,
126+
listen_addr: listen_addresses
122127

123128
# Engine API
124129

lib/lambda_ethereum_consensus/beacon/beacon_chain.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,17 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
170170
def handle_info(:on_tick, state) do
171171
schedule_next_tick()
172172
time = :os.system_time(:second)
173-
ForkChoice.on_tick(time)
174-
175-
# TODO: reduce time between ticks to account for gnosis' 5s slot time.
176-
old_logical_time = compute_logical_time(state)
177173
new_state = %BeaconChainState{state | time: time}
178-
new_logical_time = compute_logical_time(new_state)
179174

180-
if old_logical_time != new_logical_time do
181-
notify_subscribers(new_logical_time)
175+
if time >= state.genesis_time do
176+
ForkChoice.on_tick(time)
177+
# TODO: reduce time between ticks to account for gnosis' 5s slot time.
178+
old_logical_time = compute_logical_time(state)
179+
new_logical_time = compute_logical_time(new_state)
180+
181+
if old_logical_time != new_logical_time do
182+
notify_subscribers(new_logical_time)
183+
end
182184
end
183185

184186
{:noreply, new_state}

lib/lambda_ethereum_consensus/beacon/beacon_node.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,33 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconNode do
7979
end
8080

8181
defp get_libp2p_args() do
82-
config = Application.fetch_env!(:lambda_ethereum_consensus, :discovery)
82+
config = Application.fetch_env!(:lambda_ethereum_consensus, :libp2p)
8383
port = Keyword.fetch!(config, :port)
8484
bootnodes = Keyword.fetch!(config, :bootnodes)
8585

86+
listen_addr = Keyword.fetch!(config, :listen_addr) |> Enum.map(&parse_listen_addr/1)
87+
8688
if Enum.empty?(bootnodes) do
8789
Logger.warning("No bootnodes configured.")
8890
end
8991

9092
[
91-
listen_addr: [],
93+
listen_addr: listen_addr,
9294
enable_discovery: true,
9395
discovery_addr: "0.0.0.0:#{port}",
9496
bootnodes: bootnodes
9597
]
9698
end
99+
100+
defp parse_listen_addr(addr) do
101+
case String.split(addr, ":") do
102+
[ip, port] ->
103+
"/ip4/#{ip}/tcp/#{port}"
104+
105+
_ ->
106+
Logger.error("Invalid listen address: #{addr}")
107+
Logger.flush()
108+
System.halt(2)
109+
end
110+
end
97111
end

lib/lambda_ethereum_consensus/beacon/sync_blocks.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ defmodule LambdaEthereumConsensus.Beacon.SyncBlocks do
2929
initial_slot = Misc.compute_start_slot_at_epoch(checkpoint.epoch) + 1
3030
last_slot = BeaconChain.get_current_slot()
3131

32-
chunks =
32+
if last_slot > 0 do
3333
Enum.chunk_every(initial_slot..last_slot, @blocks_per_chunk)
3434
|> Enum.map(fn chunk ->
3535
first_slot = List.first(chunk)
3636
last_slot = List.last(chunk)
3737
count = last_slot - first_slot + 1
3838
%{from: first_slot, count: count}
3939
end)
40-
41-
perform_sync(chunks)
40+
|> perform_sync()
41+
end
4242
end
4343

4444
@spec perform_sync([chunk()]) :: :ok

lib/libp2p_port.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
4848
]
4949

5050
@type init_arg ::
51-
{:listen_addr, String.t()}
51+
{:listen_addr, [String.t()]}
5252
| {:enable_discovery, boolean()}
5353
| {:discovery_addr, String.t()}
5454
| {:bootnodes, [String.t()]}
@@ -67,7 +67,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
6767
* `:opts` - a Keyword list of options to pass onto the GenServer.
6868
Defaults to `[name: __MODULE__]`.
6969
70-
* `:listen_addr` - the address to listen on.
70+
* `:listen_addr` - the addresses to listen on.
7171
* `:enable_discovery` - boolean that specifies if the discovery service
7272
should be started.
7373
* `:discovery_addr` - the address used by the discovery service.

0 commit comments

Comments
 (0)