Skip to content

Commit b1e9a3e

Browse files
committed
Ticker removal
1 parent 170db89 commit b1e9a3e

File tree

3 files changed

+40
-80
lines changed

3 files changed

+40
-80
lines changed

lib/lambda_ethereum_consensus/beacon/beacon_node.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconNode do
3636

3737
children =
3838
[
39-
{LambdaEthereumConsensus.Ticker, [LambdaEthereumConsensus.Libp2pPort]},
4039
{LambdaEthereumConsensus.Libp2pPort, libp2p_args},
4140
{Task.Supervisor, name: PruneStatesSupervisor},
4241
{Task.Supervisor, name: PruneBlocksSupervisor},

lib/lambda_ethereum_consensus/ticker.ex

Lines changed: 0 additions & 48 deletions
This file was deleted.

lib/libp2p_port.ex

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
99

1010
use GenServer
1111

12+
@tick_time 1000
13+
1214
alias LambdaEthereumConsensus.Beacon.PendingBlocks
1315
alias LambdaEthereumConsensus.Beacon.SyncBlocks
1416
alias LambdaEthereumConsensus.ForkChoice
@@ -109,11 +111,6 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
109111
GenServer.start_link(__MODULE__, args, opts)
110112
end
111113

112-
@spec on_tick(Types.uint64()) :: :ok
113-
def on_tick(time) do
114-
GenServer.cast(__MODULE__, {:on_tick, time})
115-
end
116-
117114
@spec notify_new_block(Types.slot(), Types.root()) :: :ok
118115
def notify_new_block(slot, head_root) do
119116
# TODO: This is quick workarround to notify the libp2p port about new blocks from within
@@ -403,6 +400,8 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
403400
"[Optimistic Sync] Waiting #{@sync_delay_millis / 1000} seconds to discover some peers before requesting blocks."
404401
)
405402

403+
schedule_next_tick()
404+
406405
{:ok,
407406
%{
408407
genesis_time: genesis_time,
@@ -435,26 +434,6 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
435434
{:noreply, state}
436435
end
437436

438-
@impl GenServer
439-
def handle_cast({:on_tick, time}, %{genesis_time: genesis_time} = state)
440-
when time < genesis_time,
441-
do: {:noreply, state}
442-
443-
def handle_cast({:on_tick, time}, %{genesis_time: genesis_time, slot_data: slot_data} = state) do
444-
# TODO: we probably want to remove this from here, but we keep it here to have this serialized
445-
# with respect to the other fork choice store modifications.
446-
447-
ForkChoice.on_tick(time)
448-
449-
new_slot_data = compute_slot(genesis_time, time)
450-
451-
updated_state = maybe_tick_validators(slot_data != new_slot_data, new_slot_data, state)
452-
453-
log_new_slot(slot_data, new_slot_data)
454-
455-
{:noreply, updated_state}
456-
end
457-
458437
def handle_cast(
459438
{:send_request, peer_id, protocol_id, message, handler},
460439
%{
@@ -505,6 +484,14 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
505484
{:noreply, state}
506485
end
507486

487+
@impl GenServer
488+
def handle_info(:on_tick, %{genesis_time: genesis_time} = state) do
489+
schedule_next_tick()
490+
time = :os.system_time(:second)
491+
492+
{:noreply, on_tick(time, state)}
493+
end
494+
508495
@impl GenServer
509496
def handle_info(:sync_blocks, state) do
510497
blocks_to_download = SyncBlocks.run()
@@ -743,6 +730,23 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
743730
end)
744731
end
745732

733+
defp on_tick(time, %{genesis_time: genesis_time} = state) when time < genesis_time, do: state
734+
735+
defp on_tick(time, %{genesis_time: genesis_time, slot_data: slot_data} = state) do
736+
# TODO: we probably want to remove this (ForkChoice.on_tick) from here, but we keep it
737+
# here to have this serialized with respect to the other fork choice store modifications.
738+
739+
ForkChoice.on_tick(time)
740+
741+
new_slot_data = compute_slot(genesis_time, time)
742+
743+
updated_state = maybe_tick_validators(slot_data != new_slot_data, new_slot_data, state)
744+
745+
maybe_log_new_slot(slot_data, new_slot_data)
746+
747+
updated_state
748+
end
749+
746750
defp maybe_tick_validators(false = _slot_data_changed, _slot_data, state), do: state
747751

748752
defp maybe_tick_validators(true, slot_data, %{validators: validators} = state) do
@@ -773,10 +777,15 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
773777
defp notify_validator({pubkey, validator}, {:new_block, slot, head_root}),
774778
do: {pubkey, Validator.handle_new_block(slot, head_root, validator)}
775779

776-
@spec compute_slot(Types.uint64(), Types.uint64()) :: slot_data()
780+
defp schedule_next_tick() do
781+
# For millisecond precision
782+
time_to_next_tick = @tick_time - rem(:os.system_time(:millisecond), @tick_time)
783+
Process.send_after(__MODULE__, :on_tick, time_to_next_tick)
784+
end
785+
777786
defp compute_slot(genesis_time, time) do
778-
# TODO: This was copied as it is from the Clock to convert it into just a Ticker,
779-
# slot calculations are spread across modules, we should probably centralize them.
787+
# TODO: This was copied as it is from the Clock, slot calculations are spread
788+
# across modules, we should probably centralize them.
780789
elapsed_time = time - genesis_time
781790

782791
slot_thirds = div(elapsed_time * 3, ChainSpec.get("SECONDS_PER_SLOT"))
@@ -792,13 +801,13 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
792801
{slot, slot_third}
793802
end
794803

795-
defp log_new_slot({slot, _third}, {slot, _another_third}), do: :ok
804+
defp maybe_log_new_slot({slot, _third}, {slot, _another_third}), do: :ok
796805

797-
defp log_new_slot({_prev_slot, _thrid}, {slot, :first_third}) do
806+
defp maybe_log_new_slot({_prev_slot, _thrid}, {slot, :first_third}) do
798807
# TODO: It used :sync, :store as the slot event in the old Clock, double-check.
799808
:telemetry.execute([:sync, :store], %{slot: slot})
800809
Logger.info("[Libp2p] Slot transition", slot: slot)
801810
end
802811

803-
defp log_new_slot(_, _), do: :ok
812+
defp maybe_log_new_slot(_, _), do: :ok
804813
end

0 commit comments

Comments
 (0)