solana-ledger 4.0.0-beta.2

Solana ledger
Documentation
use {
    crate::{
        blockstore::Blockstore,
        blockstore_processor::{
            self, BlockstoreProcessorError, ProcessOptions, TransactionStatusSender,
        },
        entry_notifier_service::EntryNotifierSender,
        use_snapshot_archives_at_startup::{self, UseSnapshotArchivesAtStartup},
    },
    agave_snapshots::{
        error::SnapshotError,
        paths as snapshot_paths,
        snapshot_archive_info::{
            FullSnapshotArchiveInfo, IncrementalSnapshotArchiveInfo, SnapshotArchiveInfoGetter,
        },
        snapshot_config::SnapshotConfig,
        snapshot_hash::{FullSnapshotHash, IncrementalSnapshotHash, StartingSnapshotHashes},
    },
    log::*,
    solana_accounts_db::accounts_update_notifier_interface::AccountsUpdateNotifier,
    solana_genesis_config::GenesisConfig,
    solana_runtime::{bank_forks::BankForks, snapshot_bank_utils, snapshot_utils},
    std::{
        path::PathBuf,
        sync::{Arc, RwLock, atomic::AtomicBool},
    },
    thiserror::Error,
};

#[derive(Error, Debug)]
pub enum BankForksUtilsError {
    #[error("accounts path(s) not present when booting from snapshot")]
    AccountPathsNotPresent,

    #[error(
        "failed to load bank: {source}, full snapshot archive: {full_snapshot_archive}, \
         incremental snapshot archive: {incremental_snapshot_archive}"
    )]
    BankFromSnapshotsArchive {
        source: Box<SnapshotError>,
        full_snapshot_archive: String,
        incremental_snapshot_archive: String,
    },

    #[error(
        "there is no local state to startup from. Ensure --{flag} is NOT set to \"{value}\" and \
         restart"
    )]
    NoBankSnapshotDirectory { flag: String, value: String },

    #[error("failed to load bank from snapshot '{path}': {source}")]
    BankFromSnapshotsDirectory {
        source: SnapshotError,
        path: PathBuf,
    },

    #[error("failed to process blockstore from genesis: {0}")]
    ProcessBlockstoreFromGenesis(#[source] BlockstoreProcessorError),
}

pub type BankAndHashes = (Arc<RwLock<BankForks>>, Option<StartingSnapshotHashes>);

/// Load the banks via genesis
pub fn load_bank_forks_from_genesis(
    genesis_config: &GenesisConfig,
    blockstore: &Blockstore,
    account_paths: Vec<PathBuf>,
    process_options: &ProcessOptions,
    transaction_status_sender: Option<&TransactionStatusSender>,
    entry_notification_sender: Option<&EntryNotifierSender>,
    accounts_update_notifier: Option<AccountsUpdateNotifier>,
    exit: Arc<AtomicBool>,
) -> Result<BankAndHashes, BankForksUtilsError> {
    info!("Processing ledger from genesis");
    let bank_forks = blockstore_processor::process_blockstore_for_bank_0(
        genesis_config,
        blockstore,
        account_paths,
        process_options,
        transaction_status_sender,
        entry_notification_sender,
        accounts_update_notifier,
        exit,
    )
    .map_err(BankForksUtilsError::ProcessBlockstoreFromGenesis)?;

    let root_bank = bank_forks.read().unwrap().root_bank();
    root_bank.register_hard_forks(process_options.new_hard_forks.as_ref());

    Ok((bank_forks, None))
}

fn get_snapshots_to_load(
    snapshot_config: &SnapshotConfig,
) -> Option<(
    FullSnapshotArchiveInfo,
    Option<IncrementalSnapshotArchiveInfo>,
)> {
    if !snapshot_config.should_load_snapshots() {
        info!("Snapshots disabled");
        return None;
    };

    let Some(full_snapshot_archive_info) = snapshot_paths::get_highest_full_snapshot_archive_info(
        &snapshot_config.full_snapshot_archives_dir,
    ) else {
        warn!(
            "No snapshot package found in directory: {}",
            snapshot_config.full_snapshot_archives_dir.display()
        );
        return None;
    };

    let incremental_snapshot_archive_info =
        snapshot_paths::get_highest_incremental_snapshot_archive_info(
            &snapshot_config.incremental_snapshot_archives_dir,
            full_snapshot_archive_info.slot(),
        );

    Some((
        full_snapshot_archive_info,
        incremental_snapshot_archive_info,
    ))
}

/// Load the banks via snapshot if snapshots are available, otherwise return `Ok(None)`
pub fn try_load_bank_forks_from_snapshot(
    genesis_config: &GenesisConfig,
    account_paths: &[PathBuf],
    snapshot_config: &SnapshotConfig,
    process_options: &ProcessOptions,
    accounts_update_notifier: Option<AccountsUpdateNotifier>,
    exit: Arc<AtomicBool>,
) -> Result<Option<BankAndHashes>, BankForksUtilsError> {
    let Some((full_snapshot_archive_info, incremental_snapshot_archive_info)) =
        get_snapshots_to_load(snapshot_config)
    else {
        return Ok(None);
    };

    info!(
        "Initializing bank snapshots dir: {}",
        snapshot_config.bank_snapshots_dir.display()
    );
    std::fs::create_dir_all(&snapshot_config.bank_snapshots_dir)
        .expect("create bank snapshots dir");

    // Fail hard here if snapshot fails to load, don't silently continue
    if account_paths.is_empty() {
        return Err(BankForksUtilsError::AccountPathsNotPresent);
    }

    let latest_snapshot_archive_slot = std::cmp::max(
        full_snapshot_archive_info.slot(),
        incremental_snapshot_archive_info
            .as_ref()
            .map(SnapshotArchiveInfoGetter::slot)
            .unwrap_or(0),
    );

    let fastboot_snapshot = match process_options.use_snapshot_archives_at_startup {
        UseSnapshotArchivesAtStartup::Always => None,
        UseSnapshotArchivesAtStartup::Never => {
            let Some(bank_snapshot) =
                snapshot_utils::get_highest_loadable_bank_snapshot(snapshot_config)
            else {
                return Err(BankForksUtilsError::NoBankSnapshotDirectory {
                    flag: use_snapshot_archives_at_startup::cli::LONG_ARG.to_string(),
                    value: UseSnapshotArchivesAtStartup::Never.to_string(),
                });
            };
            // If a newer snapshot archive was downloaded, it is possible that its slot is
            // higher than the local state we will load.  Did the user intend for this?
            if bank_snapshot.slot < latest_snapshot_archive_slot {
                warn!(
                    "Starting up from local state at slot {}, which is *older* than the latest \
                     snapshot archive at slot {}. If this is not desired, change the --{} CLI \
                     option to *not* \"{}\" and restart.",
                    bank_snapshot.slot,
                    latest_snapshot_archive_slot,
                    use_snapshot_archives_at_startup::cli::LONG_ARG,
                    UseSnapshotArchivesAtStartup::Never,
                );
            }
            Some(bank_snapshot)
        }
        UseSnapshotArchivesAtStartup::WhenNewest => {
            snapshot_utils::get_highest_loadable_bank_snapshot(snapshot_config)
                .filter(|bank_snapshot| bank_snapshot.slot >= latest_snapshot_archive_slot)
        }
    };

    let bank = if let Some(fastboot_snapshot) = fastboot_snapshot {
        snapshot_bank_utils::bank_from_snapshot_dir(
            account_paths,
            &fastboot_snapshot,
            genesis_config,
            &process_options.runtime_config,
            process_options.debug_keys.clone(),
            process_options.limit_load_slot_count_from_snapshot,
            process_options.verify_index,
            process_options.accounts_db_config.clone(),
            accounts_update_notifier,
            exit,
        )
        .map_err(|err| BankForksUtilsError::BankFromSnapshotsDirectory {
            source: err,
            path: fastboot_snapshot.snapshot_path(),
        })?
    } else {
        // Given that we are going to boot from an archive, the append vecs held in the snapshot dirs for fast-boot should
        // be released.  They will be released by the account_background_service anyway.  But in the case of the account_paths
        // using memory-mounted file system, they are not released early enough to give space for the new append-vecs from
        // the archives, causing the out-of-memory problem.  So, purge the snapshot dirs upfront before loading from the archive.
        snapshot_utils::purge_all_bank_snapshots(&snapshot_config.bank_snapshots_dir);

        snapshot_bank_utils::bank_from_snapshot_archives(
            account_paths,
            &snapshot_config.bank_snapshots_dir,
            &full_snapshot_archive_info,
            incremental_snapshot_archive_info.as_ref(),
            genesis_config,
            &process_options.runtime_config,
            process_options.debug_keys.clone(),
            process_options.limit_load_slot_count_from_snapshot,
            process_options.accounts_db_skip_shrink,
            process_options.accounts_db_force_initial_clean,
            process_options.verify_index,
            process_options.accounts_db_config.clone(),
            accounts_update_notifier,
            exit,
        )
        .map_err(|err| BankForksUtilsError::BankFromSnapshotsArchive {
            source: Box::new(err),
            full_snapshot_archive: full_snapshot_archive_info.path().display().to_string(),
            incremental_snapshot_archive: incremental_snapshot_archive_info
                .as_ref()
                .map(|archive| archive.path().display().to_string())
                .unwrap_or("none".to_string()),
        })?
    };

    // We must inform accounts-db of the latest full snapshot slot, which is used by the background
    // processes to handle zero lamport accounts.  Since we've now successfully loaded the bank
    // from snapshots, this is a good time to do that update.
    // Note, this must only be set if we should generate snapshots, so that we correctly
    // handle (i.e. purge) zero lamport accounts.
    if snapshot_config.should_generate_snapshots() {
        bank.rc
            .accounts
            .accounts_db
            .set_latest_full_snapshot_slot(full_snapshot_archive_info.slot());
    } else {
        assert!(
            bank.rc
                .accounts
                .accounts_db
                .latest_full_snapshot_slot()
                .is_none()
        );
    }

    let full_snapshot_hash = FullSnapshotHash((
        full_snapshot_archive_info.slot(),
        *full_snapshot_archive_info.hash(),
    ));
    let incremental_snapshot_hash =
        incremental_snapshot_archive_info.map(|incremental_snapshot_archive_info| {
            IncrementalSnapshotHash((
                incremental_snapshot_archive_info.slot(),
                *incremental_snapshot_archive_info.hash(),
            ))
        });
    let starting_snapshot_hashes = StartingSnapshotHashes {
        full: full_snapshot_hash,
        incremental: incremental_snapshot_hash,
    };
    bank.register_hard_forks(process_options.new_hard_forks.as_ref());

    Ok(Some((
        BankForks::new_rw_arc(bank),
        Some(starting_snapshot_hashes),
    )))
}