Skip to content

feat: add --listen-address and fix edge-cases #1055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ switches = [
log_file: :string,
beacon_api: :boolean,
beacon_api_port: :integer,
listen_address: [:string, :keep],
discovery_port: :integer,
boot_nodes: :string,
keystore_file: :string,
Expand Down Expand Up @@ -46,6 +47,7 @@ enable_metrics = Keyword.get(args, :metrics, false)
metrics_port = Keyword.get(args, :metrics_port, if(enable_metrics, do: 9568, else: nil))
beacon_api_port = Keyword.get(args, :beacon_api_port, nil)
enable_beacon_api = Keyword.get(args, :beacon_api, not is_nil(beacon_api_port))
listen_addresses = Keyword.get_values(args, :listen_address)
discovery_port = Keyword.get(args, :discovery_port, 9000)
cli_bootnodes = Keyword.get(args, :boot_nodes, "")
keystore = Keyword.get(args, :keystore_file)
Expand Down Expand Up @@ -118,7 +120,10 @@ bootnodes =
|> Enum.reject(&(&1 == ""))
|> Enum.concat(bootnodes)

config :lambda_ethereum_consensus, :discovery, port: discovery_port, bootnodes: bootnodes
config :lambda_ethereum_consensus, :libp2p,
port: discovery_port,
bootnodes: bootnodes,
listen_addr: listen_addresses

# Engine API

Expand Down
16 changes: 9 additions & 7 deletions lib/lambda_ethereum_consensus/beacon/beacon_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,17 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
def handle_info(:on_tick, state) do
schedule_next_tick()
time = :os.system_time(:second)
ForkChoice.on_tick(time)

# TODO: reduce time between ticks to account for gnosis' 5s slot time.
old_logical_time = compute_logical_time(state)
new_state = %BeaconChainState{state | time: time}
new_logical_time = compute_logical_time(new_state)

if old_logical_time != new_logical_time do
notify_subscribers(new_logical_time)
if time >= state.genesis_time do
ForkChoice.on_tick(time)
# TODO: reduce time between ticks to account for gnosis' 5s slot time.
old_logical_time = compute_logical_time(state)
new_logical_time = compute_logical_time(new_state)

if old_logical_time != new_logical_time do
notify_subscribers(new_logical_time)
end
end

{:noreply, new_state}
Expand Down
18 changes: 16 additions & 2 deletions lib/lambda_ethereum_consensus/beacon/beacon_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,33 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconNode do
end

defp get_libp2p_args() do
config = Application.fetch_env!(:lambda_ethereum_consensus, :discovery)
config = Application.fetch_env!(:lambda_ethereum_consensus, :libp2p)
port = Keyword.fetch!(config, :port)
bootnodes = Keyword.fetch!(config, :bootnodes)

listen_addr = Keyword.fetch!(config, :listen_addr) |> Enum.map(&parse_listen_addr/1)

if Enum.empty?(bootnodes) do
Logger.warning("No bootnodes configured.")
end

[
listen_addr: [],
listen_addr: listen_addr,
enable_discovery: true,
discovery_addr: "0.0.0.0:#{port}",
bootnodes: bootnodes
]
end

defp parse_listen_addr(addr) do
case String.split(addr, ":") do
[ip, port] ->
"/ip4/#{ip}/tcp/#{port}"

_ ->
Logger.error("Invalid listen address: #{addr}")
Logger.flush()
System.halt(2)
end
end
end
6 changes: 3 additions & 3 deletions lib/lambda_ethereum_consensus/beacon/sync_blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ defmodule LambdaEthereumConsensus.Beacon.SyncBlocks do
initial_slot = Misc.compute_start_slot_at_epoch(checkpoint.epoch) + 1
last_slot = BeaconChain.get_current_slot()

chunks =
if last_slot > 0 do
Enum.chunk_every(initial_slot..last_slot, @blocks_per_chunk)
|> Enum.map(fn chunk ->
first_slot = List.first(chunk)
last_slot = List.last(chunk)
count = last_slot - first_slot + 1
%{from: first_slot, count: count}
end)

perform_sync(chunks)
|> perform_sync()
end
end

@spec perform_sync([chunk()]) :: :ok
Expand Down
4 changes: 2 additions & 2 deletions lib/libp2p_port.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
]

@type init_arg ::
{:listen_addr, String.t()}
{:listen_addr, [String.t()]}
| {:enable_discovery, boolean()}
| {:discovery_addr, String.t()}
| {:bootnodes, [String.t()]}
Expand All @@ -67,7 +67,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
* `:opts` - a Keyword list of options to pass onto the GenServer.
Defaults to `[name: __MODULE__]`.

* `:listen_addr` - the address to listen on.
* `:listen_addr` - the addresses to listen on.
* `:enable_discovery` - boolean that specifies if the discovery service
should be started.
* `:discovery_addr` - the address used by the discovery service.
Expand Down