Skip to content

Commit

Permalink
Rename slot_hash => bank_hash in AcoountsDB (solana-labs#7579)
Browse files Browse the repository at this point in the history
* Rename slot_hash => bank_hash in AcoountsDB
  • Loading branch information
ryoqun authored and sakridge committed Jan 14, 2020
1 parent 371fdc6 commit cdf8ce9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
6 changes: 3 additions & 3 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,10 @@ impl Accounts {
}

pub fn bank_hash_at(&self, slot_id: Slot) -> BankHash {
let slot_hashes = self.accounts_db.slot_hashes.read().unwrap();
*slot_hashes
let bank_hashes = self.accounts_db.bank_hashes.read().unwrap();
*bank_hashes
.get(&slot_id)
.expect("No accounts hash was found for this bank, that should not be possible")
.expect("No bank hash was found for this bank, that should not be possible")
}

/// This function will prevent multiple threads from modifying the same account state at the
Expand Down
53 changes: 25 additions & 28 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ impl<'a> Serialize for AccountsDBSerialize<'a> {
let account_storage_serialize = AccountStorageSerialize::new(&*storage, self.slot);
serialize_into(&mut wr, &account_storage_serialize).map_err(Error::custom)?;
serialize_into(&mut wr, &version).map_err(Error::custom)?;
let slot_hashes = self.accounts_db.slot_hashes.read().unwrap();
let bank_hashes = self.accounts_db.bank_hashes.read().unwrap();
serialize_into(
&mut wr,
&(self.slot, &*slot_hashes.get(&self.slot).unwrap()),
&(self.slot, &*bank_hashes.get(&self.slot).unwrap()),
)
.map_err(Error::custom)?;
let len = wr.position() as usize;
Expand Down Expand Up @@ -384,7 +384,7 @@ pub struct AccountsDB {
/// the accounts
min_num_stores: usize,

pub slot_hashes: RwLock<HashMap<Slot, BankHash>>,
pub bank_hashes: RwLock<HashMap<Slot, BankHash>>,
}

impl Default for AccountsDB {
Expand All @@ -404,7 +404,7 @@ impl Default for AccountsDB {
.build()
.unwrap(),
min_num_stores: num_threads,
slot_hashes: RwLock::new(HashMap::default()),
bank_hashes: RwLock::new(HashMap::default()),
}
}
}
Expand Down Expand Up @@ -521,12 +521,9 @@ impl AccountsDB {
let version: u64 = deserialize_from(&mut stream)
.map_err(|_| AccountsDB::get_io_error("write version deserialize error"))?;

let slot_hash: (Slot, BankHash) = deserialize_from(&mut stream)
.map_err(|_| AccountsDB::get_io_error("slot hashes deserialize error"))?;
self.slot_hashes
.write()
.unwrap()
.insert(slot_hash.0, slot_hash.1);
let (slot, bank_hash): (Slot, BankHash) = deserialize_from(&mut stream)
.map_err(|_| AccountsDB::get_io_error("bank hashes deserialize error"))?;
self.bank_hashes.write().unwrap().insert(slot, bank_hash);

// Process deserialized data, set necessary fields in self
*self.paths.write().unwrap() = local_account_paths.to_vec();
Expand Down Expand Up @@ -717,11 +714,11 @@ impl AccountsDB {
}

pub fn set_hash(&self, slot: Slot, parent_slot: Slot) {
let mut slot_hashes = self.slot_hashes.write().unwrap();
let hash = *slot_hashes
let mut bank_hashes = self.bank_hashes.write().unwrap();
let hash = *bank_hashes
.get(&parent_slot)
.expect("accounts_db::set_hash::no parent slot");
slot_hashes.insert(slot, hash);
bank_hashes.insert(slot, hash);
}

pub fn load(
Expand Down Expand Up @@ -1007,22 +1004,22 @@ impl AccountsDB {
for hash in hashes {
calculated_hash.xor(hash);
}
let slot_hashes = self.slot_hashes.read().unwrap();
if let Some(found_hash) = slot_hashes.get(&slot) {
let bank_hashes = self.bank_hashes.read().unwrap();
if let Some(found_hash) = bank_hashes.get(&slot) {
if calculated_hash == *found_hash {
Ok(())
} else {
Err(MismatchedBankHash)
}
} else {
Err(BankHashVerificatonError::MissingBankHash)
Err(MissingBankHash)
}
}

pub fn xor_in_hash_state(&self, slot_id: Slot, hash: BankHash) {
let mut slot_hashes = self.slot_hashes.write().unwrap();
let slot_hash_state = slot_hashes.entry(slot_id).or_insert_with(BankHash::default);
slot_hash_state.xor(hash);
let mut bank_hashes = self.bank_hashes.write().unwrap();
let bank_hash = bank_hashes.entry(slot_id).or_insert_with(BankHash::default);
bank_hash.xor(hash);
}

fn update_index(
Expand Down Expand Up @@ -1100,9 +1097,9 @@ impl AccountsDB {
}
}
{
let mut slot_hashes = self.slot_hashes.write().unwrap();
let mut bank_hashes = self.bank_hashes.write().unwrap();
for slot in dead_slots.iter() {
slot_hashes.remove(slot);
bank_hashes.remove(slot);
}
}
}
Expand Down Expand Up @@ -1837,11 +1834,11 @@ pub mod tests {
);

// Get the hash for the latest slot, which should be the only hash in the
// slot_hashes map on the deserialized AccountsDb
assert_eq!(daccounts.slot_hashes.read().unwrap().len(), 1);
// bank_hashes map on the deserialized AccountsDb
assert_eq!(daccounts.bank_hashes.read().unwrap().len(), 1);
assert_eq!(
daccounts.slot_hashes.read().unwrap().get(&latest_slot),
accounts.slot_hashes.read().unwrap().get(&latest_slot)
daccounts.bank_hashes.read().unwrap().get(&latest_slot),
accounts.bank_hashes.read().unwrap().get(&latest_slot)
);

print_count_and_status("daccounts", &daccounts);
Expand Down Expand Up @@ -2242,14 +2239,14 @@ pub mod tests {
db.add_root(some_slot);
assert_matches!(db.verify_bank_hash(some_slot, &ancestors), Ok(_));

db.slot_hashes.write().unwrap().remove(&some_slot).unwrap();
db.bank_hashes.write().unwrap().remove(&some_slot).unwrap();
assert_matches!(
db.verify_bank_hash(some_slot, &ancestors),
Err(MissingBankHash)
);

let some_bank_hash = BankHash::from_hash(&Hash::new(&[0xca; HASH_BYTES]));
db.slot_hashes
db.bank_hashes
.write()
.unwrap()
.insert(some_slot, some_bank_hash);
Expand All @@ -2267,7 +2264,7 @@ pub mod tests {
let some_slot: Slot = 0;
let ancestors = vec![(some_slot, 0)].into_iter().collect();

db.slot_hashes
db.bank_hashes
.write()
.unwrap()
.insert(some_slot, BankHash::default());
Expand Down

0 comments on commit cdf8ce9

Please sign in to comment.