Skip to content

Commit

Permalink
Add bank API to wait on background verify
Browse files Browse the repository at this point in the history
  • Loading branch information
bw-solana committed Aug 8, 2022
1 parent c8c5466 commit 21fb1d3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
6 changes: 1 addition & 5 deletions ledger/src/blockstore_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,7 @@ pub fn process_blockstore_from_root(
// We might be promptly restarted after bad capitalization was detected while creating newer snapshot.
// In that case, we're most likely restored from the last good snapshot and replayed up to this root.
// So again check here for the bad capitalization to avoid to continue until the next snapshot creation.
bank.rc
.accounts
.accounts_db
.verify_accounts_hash_in_bg
.wait_for_complete();
bank.wait_for_startup_verification_complete();
if !bank.calculate_and_verify_capitalization(opts.accounts_db_test_hash_calculation) {
return Err(
BlockstoreProcessorError::RootBankWithMismatchedCapitalization(
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ pub struct AccountsDb {
/// number of slots remaining where filler accounts should be added
pub filler_account_slots_remaining: AtomicU64,

pub verify_accounts_hash_in_bg: VerifyAccountsHashInBackground,
pub(crate) verify_accounts_hash_in_bg: VerifyAccountsHashInBackground,

// # of passes should be a function of the total # of accounts that are active.
// higher passes = slower total time, lower dynamic memory usage
Expand Down
8 changes: 8 additions & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3717,6 +3717,14 @@ impl Bank {
.verification_complete()
}

pub fn wait_for_startup_verification_complete(&self) {
self.rc
.accounts
.accounts_db
.verify_accounts_hash_in_bg
.wait_for_complete()
}

pub fn get_fee_for_message_with_lamports_per_signature(
&self,
message: &SanitizedMessage,
Expand Down
10 changes: 2 additions & 8 deletions runtime/src/verify_accounts_hash_in_background.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! at startup, verify accounts hash in the background
use {
crate::waitable_condvar::WaitableCondvar,
log::*,
std::{
sync::{
atomic::{AtomicBool, Ordering},
Expand All @@ -13,7 +12,7 @@ use {
};

#[derive(Debug)]
pub struct VerifyAccountsHashInBackground {
pub(crate) struct VerifyAccountsHashInBackground {
/// true when verification has completed or never had to run in background
pub(crate) verified: Arc<AtomicBool>,
/// enable waiting for verification to become complete
Expand Down Expand Up @@ -41,7 +40,6 @@ impl Default for VerifyAccountsHashInBackground {
impl VerifyAccountsHashInBackground {
/// start the bg thread to do the verification
pub(crate) fn start(&self, start: impl FnOnce() -> JoinHandle<bool>) {
warn!("BWLOG: starting bg thread for verification");
// note that we're not verified before
self.verified.store(false, Ordering::Release);
*self.thread.lock().unwrap() = Some(start());
Expand All @@ -57,32 +55,28 @@ impl VerifyAccountsHashInBackground {
/// This can occur because it completed in the background
/// or if the verification was run in the foreground.
pub(crate) fn verification_complete(&self) {
warn!("BWLOG: verification complete");
self.verified.store(true, Ordering::Release);
}

/// block until bg process is complete
pub fn wait_for_complete(&self) {
// just now completing
warn!("BWLOG: entering wait_for_complete");
let mut lock = self.thread.lock().unwrap();
if lock.is_none() {
warn!("BWLOG: lock is none");
return; // nothing to do
}
let result = lock.take().unwrap().join().unwrap();
if !result {
panic!("initial hash verification failed");
}
warn!("BWLOG: result is success");
// we never have to check again
self.verification_complete();
}

/// return true if bg hash verification is complete
/// return false if bg hash verification has not completed yet
/// if hash verification failed, a panic will occur
pub fn check_complete(&self) -> bool {
pub(crate) fn check_complete(&self) -> bool {
if self.verified.load(Ordering::Acquire) {
// already completed
return true;
Expand Down

0 comments on commit 21fb1d3

Please sign in to comment.