Skip to content

Commit a1c00e5

Browse files
authored
Break genesis loading and checks out of load_blockstore() (solana-labs#2436)
Currently, load_blockstore() does much more than just load the blockstore. Aside from the name being confusing, everything being in that single function caused some inefficiency, such as loading genesis a second time to have the config available earlier. So, break opening genesis and genesis related checks into a separate function.
1 parent d0027dd commit a1c00e5

File tree

1 file changed

+46
-32
lines changed

1 file changed

+46
-32
lines changed

core/src/validator.rs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ use {
3535
accounts_db::{AccountShrinkThreshold, AccountsDbConfig},
3636
accounts_index::AccountSecondaryIndexes,
3737
accounts_update_notifier_interface::AccountsUpdateNotifier,
38-
hardened_unpack::{open_genesis_config, MAX_GENESIS_ARCHIVE_UNPACKED_SIZE},
38+
hardened_unpack::{
39+
open_genesis_config, OpenGenesisConfigError, MAX_GENESIS_ARCHIVE_UNPACKED_SIZE,
40+
},
3941
utils::{move_and_async_delete_path, move_and_async_delete_path_contents},
4042
},
4143
solana_client::connection_cache::{ConnectionCache, Protocol},
@@ -591,9 +593,7 @@ impl Validator {
591593
"ledger directory does not exist or is not accessible: {ledger_path:?}"
592594
));
593595
}
594-
let genesis_config =
595-
open_genesis_config(ledger_path, config.max_genesis_archive_unpacked_size)
596-
.context("Failed to open genesis config")?;
596+
let genesis_config = load_genesis(config, ledger_path)?;
597597

598598
metrics_config_sanity_check(genesis_config.cluster_type)?;
599599

@@ -703,7 +703,6 @@ impl Validator {
703703
PohTimingReportService::new(poh_timing_point_receiver, exit.clone());
704704

705705
let (
706-
genesis_config,
707706
bank_forks,
708707
blockstore,
709708
original_blockstore_root,
@@ -727,6 +726,7 @@ impl Validator {
727726
) = load_blockstore(
728727
config,
729728
ledger_path,
729+
&genesis_config,
730730
exit.clone(),
731731
&start_progress,
732732
accounts_update_notifier,
@@ -1809,10 +1809,44 @@ fn blockstore_options_from_config(config: &ValidatorConfig) -> BlockstoreOptions
18091809
}
18101810
}
18111811

1812+
fn load_genesis(
1813+
config: &ValidatorConfig,
1814+
ledger_path: &Path,
1815+
) -> Result<GenesisConfig, ValidatorError> {
1816+
let genesis_config = open_genesis_config(ledger_path, config.max_genesis_archive_unpacked_size)
1817+
.map_err(ValidatorError::OpenGenesisConfig)?;
1818+
1819+
// This needs to be limited otherwise the state in the VoteAccount data
1820+
// grows too large
1821+
let leader_schedule_slot_offset = genesis_config.epoch_schedule.leader_schedule_slot_offset;
1822+
let slots_per_epoch = genesis_config.epoch_schedule.slots_per_epoch;
1823+
let leader_epoch_offset = (leader_schedule_slot_offset + slots_per_epoch - 1) / slots_per_epoch;
1824+
assert!(leader_epoch_offset <= MAX_LEADER_SCHEDULE_EPOCH_OFFSET);
1825+
1826+
let genesis_hash = genesis_config.hash();
1827+
info!("genesis hash: {}", genesis_hash);
1828+
1829+
if let Some(expected_genesis_hash) = config.expected_genesis_hash {
1830+
if genesis_hash != expected_genesis_hash {
1831+
return Err(ValidatorError::GenesisHashMismatch(
1832+
genesis_hash,
1833+
expected_genesis_hash,
1834+
));
1835+
}
1836+
}
1837+
1838+
if !config.no_poh_speed_test {
1839+
check_poh_speed(&genesis_config, None)?;
1840+
}
1841+
1842+
Ok(genesis_config)
1843+
}
1844+
18121845
#[allow(clippy::type_complexity)]
18131846
fn load_blockstore(
18141847
config: &ValidatorConfig,
18151848
ledger_path: &Path,
1849+
genesis_config: &GenesisConfig,
18161850
exit: Arc<AtomicBool>,
18171851
start_progress: &Arc<RwLock<ValidatorStartProgress>>,
18181852
accounts_update_notifier: Option<AccountsUpdateNotifier>,
@@ -1821,7 +1855,6 @@ fn load_blockstore(
18211855
poh_timing_point_sender: Option<PohTimingSender>,
18221856
) -> Result<
18231857
(
1824-
GenesisConfig,
18251858
Arc<RwLock<BankForks>>,
18261859
Arc<Blockstore>,
18271860
Slot,
@@ -1838,30 +1871,6 @@ fn load_blockstore(
18381871
> {
18391872
info!("loading ledger from {:?}...", ledger_path);
18401873
*start_progress.write().unwrap() = ValidatorStartProgress::LoadingLedger;
1841-
let genesis_config = open_genesis_config(ledger_path, config.max_genesis_archive_unpacked_size)
1842-
.map_err(|err| format!("Failed to open genesis config: {err}"))?;
1843-
1844-
// This needs to be limited otherwise the state in the VoteAccount data
1845-
// grows too large
1846-
let leader_schedule_slot_offset = genesis_config.epoch_schedule.leader_schedule_slot_offset;
1847-
let slots_per_epoch = genesis_config.epoch_schedule.slots_per_epoch;
1848-
let leader_epoch_offset = (leader_schedule_slot_offset + slots_per_epoch - 1) / slots_per_epoch;
1849-
assert!(leader_epoch_offset <= MAX_LEADER_SCHEDULE_EPOCH_OFFSET);
1850-
1851-
let genesis_hash = genesis_config.hash();
1852-
info!("genesis hash: {}", genesis_hash);
1853-
1854-
if let Some(expected_genesis_hash) = config.expected_genesis_hash {
1855-
if genesis_hash != expected_genesis_hash {
1856-
return Err(format!(
1857-
"genesis hash mismatch: hash={genesis_hash} expected={expected_genesis_hash}. Delete the ledger directory to continue: {ledger_path:?}",
1858-
));
1859-
}
1860-
}
1861-
1862-
if !config.no_poh_speed_test {
1863-
check_poh_speed(&genesis_config, None).map_err(|err| format!("{err}"))?;
1864-
}
18651874

18661875
let mut blockstore =
18671876
Blockstore::open_with_options(ledger_path, blockstore_options_from_config(config))
@@ -1918,7 +1927,7 @@ fn load_blockstore(
19181927

19191928
let (bank_forks, mut leader_schedule_cache, starting_snapshot_hashes) =
19201929
bank_forks_utils::load_bank_forks(
1921-
&genesis_config,
1930+
genesis_config,
19221931
&blockstore,
19231932
config.account_paths.clone(),
19241933
Some(&config.snapshot_config),
@@ -1950,7 +1959,6 @@ fn load_blockstore(
19501959
}
19511960

19521961
Ok((
1953-
genesis_config,
19541962
bank_forks,
19551963
blockstore,
19561964
original_blockstore_root,
@@ -2338,9 +2346,15 @@ pub enum ValidatorError {
23382346
#[error("Bad expected bank hash")]
23392347
BadExpectedBankHash,
23402348

2349+
#[error("genesis hash mismatch: actual={0}, expected={1}")]
2350+
GenesisHashMismatch(Hash, Hash),
2351+
23412352
#[error("Ledger does not have enough data to wait for supermajority")]
23422353
NotEnoughLedgerData,
23432354

2355+
#[error("failed to open genesis: {0}")]
2356+
OpenGenesisConfig(#[source] OpenGenesisConfigError),
2357+
23442358
#[error("{0}")]
23452359
Other(String),
23462360

0 commit comments

Comments
 (0)