Skip to content

Commit e7faf69

Browse files
committed
Merge branch 'main' into validator-state-management-refactor
2 parents 2055e78 + 4c814a3 commit e7faf69

File tree

6 files changed

+52
-46
lines changed

6 files changed

+52
-46
lines changed

lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do
259259
%{slot: slot, body: body} = head_block
260260

261261
OperationsCollector.notify_new_block(head_block)
262-
Libp2pPort.notify_new_block(slot, head_root)
262+
Libp2pPort.notify_new_head(slot, head_root)
263263
ExecutionChain.notify_new_block(slot, body.eth1_data, body.execution_payload)
264264

265265
update_fork_choice_data(

lib/lambda_ethereum_consensus/validator/setup.ex

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule LambdaEthereumConsensus.Validator.Setup do
1818
defp setup_validators(_s, _r, keystore_dir, keystore_pass_dir)
1919
when is_nil(keystore_dir) or is_nil(keystore_pass_dir) do
2020
Logger.warning(
21-
"[Validator] No keystore_dir or keystore_pass_dir provided. Validators won't start."
21+
"[Validator] No keystore_dir or keystore_pass_dir provided. Validator will not start."
2222
)
2323

2424
[]
@@ -46,7 +46,9 @@ defmodule LambdaEthereumConsensus.Validator.Setup do
4646
when is_binary(keystore_dir) and is_binary(keystore_pass_dir) do
4747
keystore_dir
4848
|> File.ls!()
49-
|> map_rejecting_nils(&paths_from_filename(keystore_dir, keystore_pass_dir, &1, Path.extname(&1)))
49+
|> map_rejecting_nils(
50+
&paths_from_filename(keystore_dir, keystore_pass_dir, &1, Path.extname(&1))
51+
)
5052
|> map_rejecting_nils(&decode_key/1)
5153
end
5254

@@ -78,6 +80,29 @@ defmodule LambdaEthereumConsensus.Validator.Setup do
7880
nil
7981
end
8082

83+
@spec notify_validators([Validator.state()], tuple()) :: [Validator.state()]
84+
def notify_validators(validators, msg) do
85+
start_time = System.monotonic_time(:millisecond)
86+
87+
Logger.debug("[Validator] Notifying all Validators with message: #{inspect(msg)}")
88+
89+
updated_validators = Enum.map(validators, &notify_validator(&1, msg))
90+
91+
end_time = System.monotonic_time(:millisecond)
92+
93+
Logger.debug(
94+
"[Validator] #{inspect(msg)} notified to all Validators after #{end_time - start_time} ms"
95+
)
96+
97+
updated_validators
98+
end
99+
100+
defp notify_validator(validator, {:on_tick, slot_data}),
101+
do: Validator.handle_tick(slot_data, validator)
102+
103+
defp notify_validator(validator, {:new_head, slot, head_root}),
104+
do: Validator.handle_new_head(slot, head_root, validator)
105+
81106
defp map_rejecting_nils(enumerable, fun) do
82107
Enum.reduce(enumerable, [], fn elem, acc ->
83108
case fun.(elem) do

lib/lambda_ethereum_consensus/validator/validator.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ defmodule LambdaEthereumConsensus.Validator do
9595
end
9696
end
9797

98-
@spec handle_new_block(Types.slot(), Types.root(), state) :: state
99-
def handle_new_block(slot, head_root, %{validator: %{index: nil}} = state) do
98+
@spec handle_new_head(Types.slot(), Types.root(), state) :: state
99+
def handle_new_head(slot, head_root, %{validator: %{index: nil}} = state) do
100100
log_error("-1", "setup validator", "index not present handle block",
101101
slot: slot,
102102
root: head_root
@@ -105,8 +105,8 @@ defmodule LambdaEthereumConsensus.Validator do
105105
state
106106
end
107107

108-
def handle_new_block(slot, head_root, state) do
109-
log_debug(state.validator.index, "recieved new block", slot: slot, root: head_root)
108+
def handle_new_head(slot, head_root, state) do
109+
log_debug(state.validator.index, "recieved new head", slot: slot, root: head_root)
110110

111111
# TODO: this doesn't take into account reorgs
112112
state

lib/libp2p_port.ex

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,14 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
111111
GenServer.start_link(__MODULE__, args, opts)
112112
end
113113

114-
@spec notify_new_block(Types.slot(), Types.root()) :: :ok
115-
def notify_new_block(slot, head_root) do
116-
# TODO: This is quick workarround to notify the libp2p port about new blocks from within
114+
@spec notify_new_head(Types.slot(), Types.root()) :: :ok
115+
def notify_new_head(slot, head_root) do
116+
# TODO: This is quick workarround to notify the libp2p port about new heads from within
117117
# the ForkChoice.recompute_head/1 without moving the validators to the store this
118118
# allows to deferr that move until we simplify the state and remove duplicates.
119-
send(self(), {:new_block, slot, head_root})
119+
# THIS IS NEEDED BECAUSE FORKCHOICE IS CURRENTLY RUNNING ON LIBP2P PORT.
120+
# It could be a simple cast in the future if that's not the case anymore.
121+
send(self(), {:new_head, slot, head_root})
120122
end
121123

122124
@doc """
@@ -503,8 +505,9 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
503505
end
504506

505507
@impl GenServer
506-
def handle_info({:new_block, slot, head_root}, %{validators: validators} = state) do
507-
updated_validators = notify_validators(validators, {:new_block, slot, head_root})
508+
def handle_info({:new_head, slot, head_root}, %{validators: validators} = state) do
509+
updated_validators =
510+
Validator.Setup.notify_validators(validators, {:new_head, slot, head_root})
508511

509512
{:noreply, %{state | validators: updated_validators}}
510513
end
@@ -740,43 +743,21 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
740743

741744
new_slot_data = compute_slot(genesis_time, time)
742745

743-
updated_state = maybe_tick_validators(slot_data != new_slot_data, new_slot_data, state)
746+
updated_state =
747+
if slot_data == new_slot_data do
748+
state
749+
else
750+
updated_validators =
751+
Validator.Setup.notify_validators(state.validators, {:on_tick, new_slot_data})
752+
753+
%{state | slot_data: new_slot_data, validators: updated_validators}
754+
end
744755

745756
maybe_log_new_slot(slot_data, new_slot_data)
746757

747758
updated_state
748759
end
749760

750-
defp maybe_tick_validators(false = _slot_data_changed, _slot_data, state), do: state
751-
752-
defp maybe_tick_validators(true, slot_data, %{validators: validators} = state) do
753-
updated_validators = notify_validators(validators, {:on_tick, slot_data})
754-
755-
%{state | slot_data: slot_data, validators: updated_validators}
756-
end
757-
758-
defp notify_validators(validators, msg) do
759-
start_time = System.monotonic_time(:millisecond)
760-
761-
Logger.debug("[Libp2p] Notifying all Validators with message: #{inspect(msg)}")
762-
763-
updated_validators = Enum.map(validators, &notify_validator(&1, msg))
764-
765-
end_time = System.monotonic_time(:millisecond)
766-
767-
Logger.debug(
768-
"[Validator Manager] #{inspect(msg)} notified to all Validators after #{end_time - start_time} ms"
769-
)
770-
771-
updated_validators
772-
end
773-
774-
defp notify_validator(validator, {:on_tick, slot_data}),
775-
do: Validator.handle_tick(slot_data, validator)
776-
777-
defp notify_validator(validator, {:new_block, slot, head_root}),
778-
do: Validator.handle_new_block(slot, head_root, validator)
779-
780761
defp schedule_next_tick() do
781762
# For millisecond precision
782763
time_to_next_tick = @tick_time - rem(:os.system_time(:millisecond), @tick_time)

test/unit/beacon_api/beacon_api_v1_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ defmodule Unit.BeaconApiTest.V1 do
159159
alias LambdaEthereumConsensus.P2P.Metadata
160160
patch(ForkChoice, :get_fork_version, fn -> ChainSpec.get("DENEB_FORK_VERSION") end)
161161

162-
start_link_supervised!({Libp2pPort, genesis_time: 42})
162+
start_link_supervised!({Libp2pPort, genesis_time: :os.system_time(:second)})
163163
Metadata.init()
164164
identity = Libp2pPort.get_node_identity()
165165
metadata = Metadata.get_metadata()

test/unit/libp2p_port_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Unit.Libp2pPortTest do
1717
end
1818

1919
defp start_port(name \\ Libp2pPort, init_args \\ []) do
20-
start_link_supervised!({Libp2pPort, [opts: [name: name], genesis_time: 42] ++ init_args},
20+
start_link_supervised!({Libp2pPort, [opts: [name: name], genesis_time: :os.system_time(:second)] ++ init_args},
2121
id: name
2222
)
2323
end

0 commit comments

Comments
 (0)