forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bank: add lightweight bank hash cache to reduce bank forks usage
- Loading branch information
1 parent
b6b5b1a
commit e9d0c6a
Showing
5 changed files
with
159 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//! Lightweight cache tracking the bank hashes of replayed banks. | ||
//! This can be useful to avoid read-locking bank forks just to query the bank hash. | ||
use { | ||
solana_sdk::{clock::Slot, hash::Hash}, | ||
std::collections::BTreeMap, | ||
}; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct BankHashCache { | ||
hashes: BTreeMap<Slot, Hash>, | ||
} | ||
|
||
impl BankHashCache { | ||
/// Insert new frozen bank, returning the hash of the previously dumped bank if it exists | ||
pub fn freeze(&mut self, slot: Slot, hash: Hash) -> Option<Hash> { | ||
self.hashes.insert(slot, hash) | ||
} | ||
|
||
/// Returns the replayed bank hash of `slot`. | ||
/// If `slot` has been dumped, returns the previously replayed hash. | ||
pub fn bank_hash(&self, slot: Slot) -> Option<Hash> { | ||
self.hashes.get(&slot).copied() | ||
} | ||
|
||
/// Removes `slots` from the cache. Intended to be used with `BankForks::prune_non_rooted` | ||
pub fn prune<I>(&mut self, slots: I) | ||
where | ||
I: Iterator<Item = Slot>, | ||
{ | ||
for slot in slots { | ||
self.hashes.remove(&slot); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use { | ||
crate::{ | ||
bank::Bank, | ||
bank_forks::BankForks, | ||
genesis_utils::{create_genesis_config, GenesisConfigInfo}, | ||
}, | ||
solana_sdk::pubkey::Pubkey, | ||
}; | ||
|
||
#[test] | ||
fn test_bank_hash_cache() { | ||
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000); | ||
let bank0 = Bank::new_for_tests(&genesis_config); | ||
bank0.freeze(); | ||
let slot = bank0.slot(); | ||
let hash = bank0.hash(); | ||
let bank_forks = BankForks::new_rw_arc(bank0); | ||
let bank_hash_cache = bank_forks.read().unwrap().bank_hash_cache(); | ||
assert_eq!( | ||
bank_hash_cache.read().unwrap().bank_hash(slot).unwrap(), | ||
hash | ||
); | ||
|
||
let bank0 = bank_forks.read().unwrap().get(slot).unwrap(); | ||
let slot = 10; | ||
let bank10 = Bank::new_from_parent(bank0, &Pubkey::new_unique(), slot); | ||
bank_forks.write().unwrap().insert(bank10); | ||
bank_forks.read().unwrap()[slot].freeze(); | ||
assert_eq!( | ||
bank_hash_cache.read().unwrap().bank_hash(slot).unwrap(), | ||
bank_forks.read().unwrap()[slot].hash() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters