Skip to content

Commit

Permalink
Move write function out of impl Bank
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Czabaniuk committed Aug 9, 2023
1 parent 30b632a commit 3f8d9d2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions core/src/replay_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use {
solana_rpc_client_api::response::SlotUpdate,
solana_runtime::{
accounts_background_service::AbsRequestSender,
bank::{Bank, NewBankOptions},
bank::{bank_hash_details, Bank, NewBankOptions},
bank_forks::{BankForks, MAX_ROOT_DISTANCE_FOR_VOTE_ONLY},
commitment::BlockCommitmentCache,
prioritization_fee_cache::PrioritizationFeeCache,
Expand Down Expand Up @@ -1500,7 +1500,7 @@ impl ReplayStage {
let bank = w_bank_forks
.remove(*slot)
.expect("BankForks should not have been purged yet");
let _ = bank.write_hash_details_file();
let _ = bank_hash_details::write_bank_hash_details_file(&bank);
((*slot, bank.bank_id()), bank)
})
.unzip()
Expand Down
4 changes: 2 additions & 2 deletions ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use {
accounts::Accounts,
accounts_db::CalcAccountsHashDataSource,
accounts_index::ScanConfig,
bank::{Bank, RewardCalculationEvent, TotalAccountsStats},
bank::{bank_hash_details, Bank, RewardCalculationEvent, TotalAccountsStats},
bank_forks::BankForks,
hardened_unpack::MAX_GENESIS_ARCHIVE_UNPACKED_SIZE,
runtime_config::RuntimeConfig,
Expand Down Expand Up @@ -2679,7 +2679,7 @@ fn main() {
}
if write_bank_file {
let working_bank = bank_forks.read().unwrap().working_bank();
let _ = working_bank.write_hash_details_file();
let _ = bank_hash_details::write_bank_hash_details_file(&working_bank);
}
exit_signal.store(true, Ordering::Relaxed);
system_monitor_service.join().unwrap();
Expand Down
38 changes: 1 addition & 37 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ struct VerifyAccountsHashConfig {
}

mod address_lookup_table;
mod bank_hash_details;
use bank_hash_details::BankHashDetails;
pub mod bank_hash_details;
mod builtin_programs;
pub mod epoch_accounts_hash_utils;
mod metrics;
Expand Down Expand Up @@ -8223,41 +8222,6 @@ impl Bank {
}
false
}

/// Output the components that comprise bank hash
pub fn write_hash_details_file(&self) -> std::result::Result<(), String> {
let details = BankHashDetails::try_from(self)?;

let slot = details.slot;
let hash = &details.bank_hash;
let file_name = format!("{slot}-{hash}.json");
let parent_dir = self
.rc
.accounts
.accounts_db
.get_base_working_path()
.join("bank_hash_details");
let path = parent_dir.join(file_name);
// A file with the same name implies the same hash for this slot. Skip
// rewriting a duplicate file in this scenario
if !path.exists() {
info!("writing details of bank {} to {}", slot, path.display());

// std::fs::write may fail (depending on platform) if the full directory
// path does not exist. So, call std::fs_create_dir_all first.
// https://doc.rust-lang.org/std/fs/fn.write.html
_ = std::fs::create_dir_all(parent_dir);
let file = std::fs::File::create(&path).map_err(|err| {
format!(
"Unable to create bank hash file at {}: {err}",
path.display()
)
})?;
serde_json::to_writer_pretty(file, &details)
.map_err(|err| format!("Unable to write bank hash file contents: {err}"))?;
}
Ok(())
}
}

/// Compute how much an account has changed size. This function is useful when the data size delta
Expand Down
36 changes: 36 additions & 0 deletions runtime/src/bank/bank_hash_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use {
super::Bank,
base64::{prelude::BASE64_STANDARD, Engine},
log::*,
serde::{
de::{self, Deserialize, Deserializer},
ser::{Serialize, SerializeSeq, Serializer},
Expand Down Expand Up @@ -162,6 +163,41 @@ impl<'de> Deserialize<'de> for BankHashAccounts {
}
}

/// Output the components that comprise bank hash
pub fn write_bank_hash_details_file(bank: &Bank) -> std::result::Result<(), String> {
let details = BankHashDetails::try_from(bank)?;

let slot = details.slot;
let hash = &details.bank_hash;
let file_name = format!("{slot}-{hash}.json");
let parent_dir = bank
.rc
.accounts
.accounts_db
.get_base_working_path()
.join("bank_hash_details");
let path = parent_dir.join(file_name);
// A file with the same name implies the same hash for this slot. Skip
// rewriting a duplicate file in this scenario
if !path.exists() {
info!("writing details of bank {} to {}", slot, path.display());

// std::fs::write may fail (depending on platform) if the full directory
// path does not exist. So, call std::fs_create_dir_all first.
// https://doc.rust-lang.org/std/fs/fn.write.html
_ = std::fs::create_dir_all(parent_dir);
let file = std::fs::File::create(&path).map_err(|err| {
format!(
"Unable to create bank hash file at {}: {err}",
path.display()
)
})?;
serde_json::to_writer_pretty(file, &details)
.map_err(|err| format!("Unable to write bank hash file contents: {err}"))?;
}
Ok(())
}

#[cfg(test)]
pub mod tests {
use super::*;
Expand Down

0 comments on commit 3f8d9d2

Please sign in to comment.