Skip to content

Commit 8c2bb5f

Browse files
authored
fix: add blocks remaining check on sync_blocks handler (#1229)
1 parent ad6bb31 commit 8c2bb5f

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

lib/libp2p_port.ex

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -431,15 +431,12 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
431431
)
432432

433433
Enum.each(blocks, &PendingBlocks.add_block/1)
434-
new_state = Map.update!(state, :blocks_remaining, fn n -> n - n_blocks - missing end)
435434

436-
if new_state.blocks_remaining > 0 do
437-
Logger.info("[Optimistic Sync] Blocks remaining: #{new_state.blocks_remaining}")
438-
{:noreply, new_state}
439-
else
440-
Logger.info("[Optimistic Sync] Sync completed. Subscribing to gossip topics.")
441-
{:noreply, subscribe_to_gossip_topics(new_state)}
442-
end
435+
new_state =
436+
Map.update!(state, :blocks_remaining, fn n -> n - n_blocks - missing end)
437+
|> subscribe_if_no_blocks()
438+
439+
{:noreply, new_state}
443440
end
444441

445442
@impl GenServer
@@ -455,7 +452,11 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
455452
@impl GenServer
456453
def handle_info(:sync_blocks, state) do
457454
blocks_to_download = SyncBlocks.run()
458-
{:noreply, state |> Map.put(:blocks_remaining, blocks_to_download)}
455+
456+
new_state =
457+
state |> Map.put(:blocks_remaining, blocks_to_download) |> subscribe_if_no_blocks()
458+
459+
{:noreply, new_state}
459460
end
460461

461462
@impl GenServer
@@ -650,6 +651,16 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
650651
Enum.map(module.topics(), fn topic -> {module, topic} end)
651652
end
652653

654+
defp subscribe_if_no_blocks(state) do
655+
if state.blocks_remaining > 0 do
656+
Logger.info("[Optimistic Sync] Blocks remaining: #{state.blocks_remaining}")
657+
state
658+
else
659+
Logger.info("[Optimistic Sync] Sync completed. Subscribing to gossip topics.")
660+
subscribe_to_gossip_topics(state)
661+
end
662+
end
663+
653664
defp subscribe_to_gossip_topics(state) do
654665
[
655666
LambdaEthereumConsensus.P2P.Gossip.BeaconBlock,

0 commit comments

Comments
 (0)