@@ -431,15 +431,12 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
431
431
)
432
432
433
433
Enum . each ( blocks , & PendingBlocks . add_block / 1 )
434
- new_state = Map . update! ( state , :blocks_remaining , fn n -> n - n_blocks - missing end )
435
434
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 }
443
440
end
444
441
445
442
@ impl GenServer
@@ -455,7 +452,11 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
455
452
@ impl GenServer
456
453
def handle_info ( :sync_blocks , state ) do
457
454
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 }
459
460
end
460
461
461
462
@ impl GenServer
@@ -650,6 +651,16 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
650
651
Enum . map ( module . topics ( ) , fn topic -> { module , topic } end )
651
652
end
652
653
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
+
653
664
defp subscribe_to_gossip_topics ( state ) do
654
665
[
655
666
LambdaEthereumConsensus.P2P.Gossip.BeaconBlock ,
0 commit comments