@@ -34,7 +34,6 @@ use crate::execution_payload::{get_execution_payload, NotifyExecutionLayer, Prep
3434use crate :: fetch_blobs:: EngineGetBlobsOutput ;
3535use crate :: fork_choice_signal:: { ForkChoiceSignalRx , ForkChoiceSignalTx , ForkChoiceWaitResult } ;
3636use crate :: graffiti_calculator:: GraffitiCalculator ;
37- use crate :: head_tracker:: { HeadTracker , HeadTrackerReader , SszHeadTracker } ;
3837use crate :: kzg_utils:: reconstruct_blobs;
3938use crate :: light_client_finality_update_verification:: {
4039 Error as LightClientFinalityUpdateError , VerifiedLightClientFinalityUpdate ,
@@ -58,7 +57,7 @@ use crate::observed_block_producers::ObservedBlockProducers;
5857use crate :: observed_data_sidecars:: ObservedDataSidecars ;
5958use crate :: observed_operations:: { ObservationOutcome , ObservedOperations } ;
6059use crate :: observed_slashable:: ObservedSlashable ;
61- use crate :: persisted_beacon_chain:: { PersistedBeaconChain , DUMMY_CANONICAL_HEAD_BLOCK_ROOT } ;
60+ use crate :: persisted_beacon_chain:: PersistedBeaconChain ;
6261use crate :: persisted_fork_choice:: PersistedForkChoice ;
6362use crate :: pre_finalization_cache:: PreFinalizationBlockCache ;
6463use crate :: shuffling_cache:: { BlockShufflingIds , ShufflingCache } ;
@@ -454,8 +453,6 @@ pub struct BeaconChain<T: BeaconChainTypes> {
454453 /// A handler for events generated by the beacon chain. This is only initialized when the
455454 /// HTTP server is enabled.
456455 pub event_handler : Option < ServerSentEventHandler < T :: EthSpec > > ,
457- /// Used to track the heads of the beacon chain.
458- pub ( crate ) head_tracker : Arc < HeadTracker > ,
459456 /// Caches the attester shuffling for a given epoch and shuffling key root.
460457 pub shuffling_cache : RwLock < ShufflingCache > ,
461458 /// A cache of eth1 deposit data at epoch boundaries for deposit finalization
@@ -607,57 +604,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
607604 } )
608605 }
609606
610- /// Persists the head tracker and fork choice .
607+ /// Return a database operation for writing the `PersistedBeaconChain` to disk .
611608 ///
612- /// We do it atomically even though no guarantees need to be made about blocks from
613- /// the head tracker also being present in fork choice.
614- pub fn persist_head_and_fork_choice ( & self ) -> Result < ( ) , Error > {
615- let mut batch = vec ! [ ] ;
616-
617- let _head_timer = metrics:: start_timer ( & metrics:: PERSIST_HEAD ) ;
618-
619- // Hold a lock to head_tracker until it has been persisted to disk. Otherwise there's a race
620- // condition with the pruning thread which can result in a block present in the head tracker
621- // but absent in the DB. This inconsistency halts pruning and dramastically increases disk
622- // size. Ref: https://github.com/sigp/lighthouse/issues/4773
623- let head_tracker = self . head_tracker . 0 . read ( ) ;
624- batch. push ( self . persist_head_in_batch ( & head_tracker) ) ;
625-
626- let _fork_choice_timer = metrics:: start_timer ( & metrics:: PERSIST_FORK_CHOICE ) ;
627- batch. push ( self . persist_fork_choice_in_batch ( ) ) ;
628-
629- self . store . hot_db . do_atomically ( batch) ?;
630- drop ( head_tracker) ;
631-
632- Ok ( ( ) )
633- }
634-
635- /// Return a `PersistedBeaconChain` without reference to a `BeaconChain`.
636- pub fn make_persisted_head (
637- genesis_block_root : Hash256 ,
638- head_tracker_reader : & HeadTrackerReader ,
639- ) -> PersistedBeaconChain {
640- PersistedBeaconChain {
641- _canonical_head_block_root : DUMMY_CANONICAL_HEAD_BLOCK_ROOT ,
642- genesis_block_root,
643- ssz_head_tracker : SszHeadTracker :: from_map ( head_tracker_reader) ,
644- }
645- }
646-
647- /// Return a database operation for writing the beacon chain head to disk.
648- pub fn persist_head_in_batch (
649- & self ,
650- head_tracker_reader : & HeadTrackerReader ,
651- ) -> KeyValueStoreOp {
652- Self :: persist_head_in_batch_standalone ( self . genesis_block_root , head_tracker_reader)
653- }
654-
655- pub fn persist_head_in_batch_standalone (
656- genesis_block_root : Hash256 ,
657- head_tracker_reader : & HeadTrackerReader ,
658- ) -> KeyValueStoreOp {
659- Self :: make_persisted_head ( genesis_block_root, head_tracker_reader)
660- . as_kv_store_op ( BEACON_CHAIN_DB_KEY )
609+ /// These days the `PersistedBeaconChain` is only used to store the genesis block root, so it
610+ /// should only ever be written once at startup. It used to be written more frequently, but
611+ /// this is no longer necessary.
612+ pub fn persist_head_in_batch_standalone ( genesis_block_root : Hash256 ) -> KeyValueStoreOp {
613+ PersistedBeaconChain { genesis_block_root } . as_kv_store_op ( BEACON_CHAIN_DB_KEY )
661614 }
662615
663616 /// Load fork choice from disk, returning `None` if it isn't found.
@@ -1450,12 +1403,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
14501403 ///
14511404 /// Returns `(block_root, block_slot)`.
14521405 pub fn heads ( & self ) -> Vec < ( Hash256 , Slot ) > {
1453- self . head_tracker . heads ( )
1454- }
1455-
1456- /// Only used in tests.
1457- pub fn knows_head ( & self , block_hash : & SignedBeaconBlockHash ) -> bool {
1458- self . head_tracker . contains_head ( ( * block_hash) . into ( ) )
1406+ self . canonical_head
1407+ . fork_choice_read_lock ( )
1408+ . proto_array ( )
1409+ . heads_descended_from_finalization :: < T :: EthSpec > ( )
1410+ . iter ( )
1411+ . map ( |node| ( node. root , node. slot ) )
1412+ . collect ( )
14591413 }
14601414
14611415 /// Returns the `BeaconState` at the given slot.
@@ -1735,8 +1689,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
17351689 let notif = ManualFinalizationNotification {
17361690 state_root : state_root. into ( ) ,
17371691 checkpoint,
1738- head_tracker : self . head_tracker . clone ( ) ,
1739- genesis_block_root : self . genesis_block_root ,
17401692 } ;
17411693
17421694 self . store_migrator . process_manual_finalization ( notif) ;
@@ -3763,7 +3715,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
37633715 state,
37643716 parent_block,
37653717 parent_eth1_finalization_data,
3766- confirmed_state_roots,
37673718 consensus_context,
37683719 } = import_data;
37693720
@@ -3787,7 +3738,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
37873738 block,
37883739 block_root,
37893740 state,
3790- confirmed_state_roots,
37913741 payload_verification_outcome. payload_verification_status ,
37923742 parent_block,
37933743 parent_eth1_finalization_data,
@@ -3825,7 +3775,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
38253775 signed_block : AvailableBlock < T :: EthSpec > ,
38263776 block_root : Hash256 ,
38273777 mut state : BeaconState < T :: EthSpec > ,
3828- confirmed_state_roots : Vec < Hash256 > ,
38293778 payload_verification_status : PayloadVerificationStatus ,
38303779 parent_block : SignedBlindedBeaconBlock < T :: EthSpec > ,
38313780 parent_eth1_finalization_data : Eth1FinalizationData ,
@@ -4013,11 +3962,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
40133962
40143963 let block = signed_block. message ( ) ;
40153964 let db_write_timer = metrics:: start_timer ( & metrics:: BLOCK_PROCESSING_DB_WRITE ) ;
4016- ops. extend (
4017- confirmed_state_roots
4018- . into_iter ( )
4019- . map ( StoreOp :: DeleteStateTemporaryFlag ) ,
4020- ) ;
40213965 ops. push ( StoreOp :: PutBlock ( block_root, signed_block. clone ( ) ) ) ;
40223966 ops. push ( StoreOp :: PutState ( block. state_root ( ) , & state) ) ;
40233967
@@ -4044,9 +3988,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
40443988 // about it.
40453989 let block_time_imported = timestamp_now ( ) ;
40463990
4047- let parent_root = block. parent_root ( ) ;
4048- let slot = block. slot ( ) ;
4049-
40503991 let current_eth1_finalization_data = Eth1FinalizationData {
40513992 eth1_data : state. eth1_data ( ) . clone ( ) ,
40523993 eth1_deposit_index : state. eth1_deposit_index ( ) ,
@@ -4067,9 +4008,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
40674008 } ) ;
40684009 }
40694010
4070- self . head_tracker
4071- . register_block ( block_root, parent_root, slot) ;
4072-
40734011 metrics:: stop_timer ( db_write_timer) ;
40744012
40754013 metrics:: inc_counter ( & metrics:: BLOCK_PROCESSING_SUCCESSES ) ;
@@ -7203,7 +7141,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
72037141impl < T : BeaconChainTypes > Drop for BeaconChain < T > {
72047142 fn drop ( & mut self ) {
72057143 let drop = || -> Result < ( ) , Error > {
7206- self . persist_head_and_fork_choice ( ) ?;
7144+ self . persist_fork_choice ( ) ?;
72077145 self . persist_op_pool ( ) ?;
72087146 self . persist_eth1_cache ( )
72097147 } ;
0 commit comments