Skip to content

Commit 12fa5a8

Browse files
committed
Remove some persisted head stuff
1 parent 91eab38 commit 12fa5a8

File tree

5 files changed

+20
-38
lines changed

5 files changed

+20
-38
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -610,33 +610,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
610610
})
611611
}
612612

613-
/// Persists the head tracker and fork choice.
613+
/// Return a database operation for writing the `PersistedBeaconChain` to disk.
614614
///
615-
/// We do it atomically even though no guarantees need to be made about blocks from
616-
/// the head tracker also being present in fork choice.
617-
pub fn persist_head_and_fork_choice(&self) -> Result<(), Error> {
618-
let mut batch = vec![];
619-
620-
let _fork_choice_timer = metrics::start_timer(&metrics::PERSIST_FORK_CHOICE);
621-
batch.push(self.persist_fork_choice_in_batch());
622-
623-
self.store.hot_db.do_atomically(batch)?;
624-
625-
Ok(())
626-
}
627-
628-
/// Return a `PersistedBeaconChain` without reference to a `BeaconChain`.
629-
pub fn make_persisted_head(genesis_block_root: Hash256) -> PersistedBeaconChain {
630-
PersistedBeaconChain { genesis_block_root }
631-
}
632-
633-
/// Return a database operation for writing the beacon chain head to disk.
634-
pub fn persist_head_in_batch(&self) -> KeyValueStoreOp {
635-
Self::persist_head_in_batch_standalone(self.genesis_block_root)
636-
}
637-
615+
/// These days the `PersistedBeaconChain` is only used to store the genesis block root, so it
616+
/// should only ever be written once at startup. It used to be written more frequently, but
617+
/// this is no longer necessary.
638618
pub fn persist_head_in_batch_standalone(genesis_block_root: Hash256) -> KeyValueStoreOp {
639-
Self::make_persisted_head(genesis_block_root).as_kv_store_op(BEACON_CHAIN_DB_KEY)
619+
PersistedBeaconChain { genesis_block_root }.as_kv_store_op(BEACON_CHAIN_DB_KEY)
640620
}
641621

642622
/// Load fork choice from disk, returning `None` if it isn't found.
@@ -7261,7 +7241,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
72617241
impl<T: BeaconChainTypes> Drop for BeaconChain<T> {
72627242
fn drop(&mut self) {
72637243
let drop = || -> Result<(), Error> {
7264-
self.persist_head_and_fork_choice()?;
7244+
self.persist_fork_choice()?;
72657245
self.persist_op_pool()?;
72667246
self.persist_eth1_cache()
72677247
};

beacon_node/beacon_chain/src/canonical_head.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use slot_clock::SlotClock;
5353
use state_processing::AllCaches;
5454
use std::sync::Arc;
5555
use std::time::Duration;
56-
use store::{iter::StateRootsIterator, KeyValueStoreOp, StoreItem};
56+
use store::{iter::StateRootsIterator, KeyValueStore, KeyValueStoreOp, StoreItem};
5757
use task_executor::{JoinHandle, ShutdownReason};
5858
use types::*;
5959

@@ -848,7 +848,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
848848
);
849849

850850
if is_epoch_transition || reorg_distance.is_some() {
851-
self.persist_head_and_fork_choice()?;
851+
self.persist_fork_choice()?;
852852
self.op_pool.prune_attestations(self.epoch()?);
853853
}
854854

@@ -1006,6 +1006,14 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
10061006
Ok(())
10071007
}
10081008

1009+
/// Persist fork choice to disk, writing immediately.
1010+
pub fn persist_fork_choice(&self) -> Result<(), Error> {
1011+
let _fork_choice_timer = metrics::start_timer(&metrics::PERSIST_FORK_CHOICE);
1012+
let batch = vec![self.persist_fork_choice_in_batch()];
1013+
self.store.hot_db.do_atomically(batch)?;
1014+
Ok(())
1015+
}
1016+
10091017
/// Return a database operation for writing fork choice to disk.
10101018
pub fn persist_fork_choice_in_batch(&self) -> KeyValueStoreOp {
10111019
Self::persist_fork_choice_in_batch_standalone(&self.canonical_head.fork_choice_read_lock())

beacon_node/beacon_chain/src/metrics.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,6 @@ pub static BALANCES_CACHE_MISSES: LazyLock<Result<IntCounter>> = LazyLock::new(|
613613
/*
614614
* Persisting BeaconChain components to disk
615615
*/
616-
pub static PERSIST_HEAD: LazyLock<Result<Histogram>> = LazyLock::new(|| {
617-
try_create_histogram(
618-
"beacon_persist_head",
619-
"Time taken to persist the canonical head",
620-
)
621-
});
622616
pub static PERSIST_OP_POOL: LazyLock<Result<Histogram>> = LazyLock::new(|| {
623617
try_create_histogram(
624618
"beacon_persist_op_pool",

beacon_node/beacon_chain/tests/payload_invalidation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,8 +1420,8 @@ async fn recover_from_invalid_head_after_persist_and_reboot() {
14201420

14211421
let slot_clock = rig.harness.chain.slot_clock.clone();
14221422

1423-
// Forcefully persist the head and fork choice.
1424-
rig.harness.chain.persist_head_and_fork_choice().unwrap();
1423+
// Forcefully persist fork choice.
1424+
rig.harness.chain.persist_fork_choice().unwrap();
14251425

14261426
let resumed = BeaconChainHarness::builder(MainnetEthSpec)
14271427
.default_spec()

beacon_node/beacon_chain/tests/store_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,8 +2763,8 @@ async fn finalizes_after_resuming_from_db() {
27632763

27642764
harness
27652765
.chain
2766-
.persist_head_and_fork_choice()
2767-
.expect("should persist the head and fork choice");
2766+
.persist_fork_choice()
2767+
.expect("should persist fork choice");
27682768
harness
27692769
.chain
27702770
.persist_op_pool()

0 commit comments

Comments
 (0)