Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
add cli for enable acc hash calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Feb 27, 2024
1 parent 8023d05 commit b282849
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
57 changes: 40 additions & 17 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ pub const ACCOUNTS_DB_CONFIG_FOR_TESTING: AccountsDbConfig = AccountsDbConfig {
create_ancient_storage: CreateAncientStorage::Pack,
test_partitioned_epoch_rewards: TestPartitionedEpochRewards::CompareResults,
test_skip_rewrites_but_include_in_bank_hash: false,
enable_accumulate_account_hash_calculation: false,
};
pub const ACCOUNTS_DB_CONFIG_FOR_BENCHMARKS: AccountsDbConfig = AccountsDbConfig {
index: Some(ACCOUNTS_INDEX_CONFIG_FOR_BENCHMARKS),
Expand All @@ -515,6 +516,7 @@ pub const ACCOUNTS_DB_CONFIG_FOR_BENCHMARKS: AccountsDbConfig = AccountsDbConfig
create_ancient_storage: CreateAncientStorage::Pack,
test_partitioned_epoch_rewards: TestPartitionedEpochRewards::None,
test_skip_rewrites_but_include_in_bank_hash: false,
enable_accumulate_account_hash_calculation: false,
};

pub type BinnedHashData = Vec<Vec<CalculateHashIntermediate>>;
Expand Down Expand Up @@ -560,6 +562,7 @@ pub struct AccountsDbConfig {
/// how to create ancient storages
pub create_ancient_storage: CreateAncientStorage,
pub test_partitioned_epoch_rewards: TestPartitionedEpochRewards,
pub enable_accumulate_account_hash_calculation: bool,
}

#[cfg(not(test))]
Expand Down Expand Up @@ -1371,6 +1374,8 @@ pub struct AccountsDb {
/// true if this client should skip rewrites but still include those rewrites in the bank hash as if rewrites had occurred.
pub test_skip_rewrites_but_include_in_bank_hash: bool,

pub enable_accumulate_account_hash_calculation: bool,

pub accounts_cache: AccountsCache,

write_cache_limit_bytes: Option<u64>,
Expand Down Expand Up @@ -2472,6 +2477,7 @@ impl AccountsDb {
partitioned_epoch_rewards_config: PartitionedEpochRewardsConfig::default(),
epoch_accounts_hash_manager: EpochAccountsHashManager::new_invalid(),
test_skip_rewrites_but_include_in_bank_hash: false,
enable_accumulate_account_hash_calculation: false,
}
}

Expand Down Expand Up @@ -2540,6 +2546,11 @@ impl AccountsDb {
.map(|config| config.test_skip_rewrites_but_include_in_bank_hash)
.unwrap_or_default();

let enable_accumulate_account_hash_calculation = accounts_db_config
.as_ref()
.map(|config| config.enable_accumulate_account_hash_calculation)
.unwrap_or_default();

let partitioned_epoch_rewards_config: PartitionedEpochRewardsConfig =
PartitionedEpochRewardsConfig::new(test_partitioned_epoch_rewards);

Expand All @@ -2559,6 +2570,7 @@ impl AccountsDb {
partitioned_epoch_rewards_config,
exhaustively_verify_refcounts,
test_skip_rewrites_but_include_in_bank_hash,
enable_accumulate_account_hash_calculation,
..Self::default_with_accounts_index(
accounts_index,
base_working_path,
Expand Down Expand Up @@ -7888,7 +7900,10 @@ impl AccountsDb {
///
/// As part of calculating the accounts delta hash, get a list of accounts modified this slot
/// (aka dirty pubkeys) and add them to `self.uncleaned_pubkeys` for future cleaning.
pub fn calculate_accounts_delta_hash(&self, slot: Slot) -> (AccountsDeltaHash, Vec<(Pubkey, AccountHash)>) {
pub fn calculate_accounts_delta_hash(
&self,
slot: Slot,
) -> (AccountsDeltaHash, Vec<(Pubkey, AccountHash)>) {
self.calculate_accounts_delta_hash_internal(slot, None, HashMap::default())
}

Expand All @@ -7898,28 +7913,36 @@ impl AccountsDb {
mut ancestors: Ancestors,
accumulated_accounts_hash: &mut Hash,
pubkey_hash: Vec<(Pubkey, AccountHash)>,
old_written_accounts: &RwLock<HashMap<Pubkey, (Option<AccountSharedData>, Option<AccountHash>)>>
old_written_accounts: &RwLock<
HashMap<Pubkey, (Option<AccountSharedData>, Option<AccountHash>)>,
>,
) {
// we are assuming it was easy to lookup a hash for everything written in `slot` when we were calculating the delta hash. So, caller passes in `pubkey_hash`
// note we don't need rewrites in `pubkey_hash`. these accounts had the same hash before and after. So, we only have to consider what was written that changed.
ancestors.remove(&slot);
let old_written_accounts = old_written_accounts.read().unwrap();
// if we want to look it up ourselves: let (hashes, _scan_us, _accumulate) = self.get_pubkey_hash_for_slot(slot);
let old = pubkey_hash.iter().map(|(k, _)| {
if let Some((account, hash)) = old_written_accounts.get(k) {
Some(hash.unwrap()) // todo on demand calculate, calculate in bg
}
else {
self.load_with_fixed_root(&ancestors, k).map(|(account, _)| Self::hash_account(&account, k))
}
}).collect::<Vec<_>>();
pubkey_hash.into_iter().zip(old.into_iter()).for_each(|((k, new_hash), old_hash)| {
if let Some(old) = old_hash {
// todo if old == new, then we can avoid this update altogether
// todo subtract accumulated_accounts_hash -= old_hash
}
// todo add accumulated_accounts_hash += new_hash
});
let old = pubkey_hash
.iter()
.map(|(k, _)| {
if let Some((account, hash)) = old_written_accounts.get(k) {
Some(hash.unwrap()) // todo on demand calculate, calculate in bg
} else {
self.load_with_fixed_root(&ancestors, k)
.map(|(account, _)| Self::hash_account(&account, k))
}
})
.collect::<Vec<_>>();
pubkey_hash
.into_iter()
.zip(old.into_iter())
.for_each(|((k, new_hash), old_hash)| {
if let Some(old) = old_hash {
// todo if old == new, then we can avoid this update altogether
// todo subtract accumulated_accounts_hash -= old_hash
}
// todo add accumulated_accounts_hash += new_hash
});
}

/// Calculate accounts delta hash for `slot`
Expand Down
4 changes: 3 additions & 1 deletion ledger-tool/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ pub fn get_accounts_db_config(
test_partitioned_epoch_rewards,
test_skip_rewrites_but_include_in_bank_hash: arg_matches
.is_present("accounts_db_test_skip_rewrites"),
..AccountsDbConfig::default()
enable_accumulate_account_hash_calculation: arg_matches
.is_present("enable_accumulate_account_hash_calculation")
..AccountsDbConfig::default(),
}
}

Expand Down
14 changes: 14 additions & 0 deletions ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,13 @@ fn main() {
bank delta hash calculation",
)
.hidden(hidden_unless_forced());

let accounts_db_enable_accumulate_account_hash_calculation =
Arg::with_name("enable_accumulate_account_hash_calculation")
.long("enable-accumulate-account-hash-calculation")
.help("Enable accumulate account hash calculation")
.hidden(hidden_unless_forced());

let account_paths_arg = Arg::with_name("account_paths")
.long("accounts")
.value_name("PATHS")
Expand Down Expand Up @@ -946,6 +953,7 @@ fn main() {
.arg(&accountsdb_verify_refcounts)
.arg(&accounts_db_skip_initial_hash_calc_arg)
.arg(&accounts_db_test_skip_rewrites_but_include_in_bank_hash)
.arg(&accounts_db_enable_accumulate_account_hash_calculation)
.arg(&use_snapshot_archives_at_startup),
)
.subcommand(
Expand All @@ -959,6 +967,7 @@ fn main() {
.arg(&accountsdb_verify_refcounts)
.arg(&accounts_db_skip_initial_hash_calc_arg)
.arg(&accounts_db_test_skip_rewrites_but_include_in_bank_hash)
.arg(&accounts_db_enable_accumulate_account_hash_calculation)
.arg(&use_snapshot_archives_at_startup),
)
.subcommand(
Expand All @@ -976,6 +985,7 @@ fn main() {
.arg(&accountsdb_skip_shrink)
.arg(&accountsdb_verify_refcounts)
.arg(&accounts_db_test_skip_rewrites_but_include_in_bank_hash)
.arg(&accounts_db_enable_accumulate_account_hash_calculation)
.arg(&verify_index_arg)
.arg(&accounts_db_skip_initial_hash_calc_arg)
.arg(&ancient_append_vecs)
Expand Down Expand Up @@ -1073,6 +1083,7 @@ fn main() {
.arg(&disable_disk_index)
.arg(&accountsdb_verify_refcounts)
.arg(&accounts_db_test_skip_rewrites_but_include_in_bank_hash)
.arg(&accounts_db_enable_accumulate_account_hash_calculation)
.arg(&accounts_db_skip_initial_hash_calc_arg)
.arg(&halt_at_slot_arg)
.arg(&hard_forks_arg)
Expand Down Expand Up @@ -1114,6 +1125,7 @@ fn main() {
.arg(&disable_disk_index)
.arg(&accountsdb_verify_refcounts)
.arg(&accounts_db_test_skip_rewrites_but_include_in_bank_hash)
.arg(&accounts_db_enable_accumulate_account_hash_calculation)
.arg(&accounts_db_skip_initial_hash_calc_arg)
.arg(&accountsdb_skip_shrink)
.arg(&ancient_append_vecs)
Expand Down Expand Up @@ -1331,6 +1343,7 @@ fn main() {
.arg(&disable_disk_index)
.arg(&accountsdb_verify_refcounts)
.arg(&accounts_db_test_skip_rewrites_but_include_in_bank_hash)
.arg(&accounts_db_enable_accumulate_account_hash_calculation)
.arg(&accounts_db_skip_initial_hash_calc_arg)
.arg(&halt_at_slot_arg)
.arg(&hard_forks_arg)
Expand Down Expand Up @@ -1392,6 +1405,7 @@ fn main() {
.arg(&disable_disk_index)
.arg(&accountsdb_verify_refcounts)
.arg(&accounts_db_test_skip_rewrites_but_include_in_bank_hash)
.arg(&accounts_db_enable_accumulate_account_hash_calculation)
.arg(&accounts_db_skip_initial_hash_calc_arg)
.arg(&halt_at_slot_arg)
.arg(&hard_forks_arg)
Expand Down
7 changes: 7 additions & 0 deletions validator/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,13 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> {
.conflicts_with("partitioned_epoch_rewards_compare_calculation")
.hidden(hidden_unless_forced()),
)
.arg(
Arg::with_name("enable_accumulate_account_hash_calculation")
.long("enable-accumulate-account-hash-calculation")
.takes_value(false)
.help("Enable accumulate account hash calculation")
.hidden(hidden_unless_forced()),
)
.arg(
Arg::with_name("accounts_index_path")
.long("accounts-index-path")
Expand Down
2 changes: 2 additions & 0 deletions validator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,8 @@ pub fn main() {
test_partitioned_epoch_rewards,
test_skip_rewrites_but_include_in_bank_hash: matches
.is_present("accounts_db_test_skip_rewrites"),
enable_accumulate_account_hash_calculation: matches
.is_present("enable_accumulate_account_hash_calculation"),
..AccountsDbConfig::default()
};

Expand Down

0 comments on commit b282849

Please sign in to comment.