From 7626983536cdce6ed9b456059f8d8a9370433dec Mon Sep 17 00:00:00 2001 From: "OEM Configuration (temporary user)" Date: Tue, 8 Sep 2020 21:57:55 -0700 Subject: [PATCH] Fix tests --- runtime/src/accounts_db.rs | 156 ++++++++++++++++++------------------- 1 file changed, 77 insertions(+), 79 deletions(-) diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index f5335a0cbcd32f..c1ffc76078db91 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -2340,28 +2340,26 @@ pub mod tests { db.store(1, &[(&pubkey, &account)]); db.store(1, &[(&pubkeys[0], &account)]); { - let stores = db.storage.read().unwrap(); - let slot_0_stores = &stores.0.get(&0).unwrap(); - let slot_1_stores = &stores.0.get(&1).unwrap(); + let slot_0_stores = &db.storage.0.get(&0).unwrap().clone(); + let slot_1_stores = &db.storage.0.get(&1).unwrap().clone(); assert_eq!(slot_0_stores.len(), 1); assert_eq!(slot_1_stores.len(), 1); - assert_eq!(slot_0_stores[&0].count(), 2); + assert_eq!(slot_0_stores.get(&0).unwrap().count(), 2); assert_eq!(slot_1_stores[&1].count(), 2); - assert_eq!(slot_0_stores[&0].approx_stored_count(), 2); + assert_eq!(slot_0_stores.get(&0).unwrap().approx_stored_count(), 2); assert_eq!(slot_1_stores[&1].approx_stored_count(), 2); } // adding root doesn't change anything db.add_root(1); { - let stores = db.storage.read().unwrap(); - let slot_0_stores = &stores.0.get(&0).unwrap(); - let slot_1_stores = &stores.0.get(&1).unwrap(); + let slot_0_stores = &db.storage.0.get(&0).unwrap().clone(); + let slot_1_stores = &db.storage.0.get(&1).unwrap().clone(); assert_eq!(slot_0_stores.len(), 1); assert_eq!(slot_1_stores.len(), 1); - assert_eq!(slot_0_stores[&0].count(), 2); + assert_eq!(slot_0_stores.get(&0).unwrap().count(), 2); assert_eq!(slot_1_stores[&1].count(), 2); - assert_eq!(slot_0_stores[&0].approx_stored_count(), 2); + assert_eq!(slot_0_stores.get(&0).unwrap().approx_stored_count(), 2); assert_eq!(slot_1_stores[&1].approx_stored_count(), 2); } @@ -2369,14 +2367,13 @@ pub mod tests { // decremented db.store(2, &[(&pubkeys[0], &account)]); { - let stores = db.storage.read().unwrap(); - let slot_0_stores = &stores.0.get(&0).unwrap(); - let slot_1_stores = &stores.0.get(&1).unwrap(); + let slot_0_stores = &db.storage.0.get(&0).unwrap().clone(); + let slot_1_stores = &db.storage.0.get(&1).unwrap().clone(); assert_eq!(slot_0_stores.len(), 1); assert_eq!(slot_1_stores.len(), 1); - assert_eq!(slot_0_stores[&0].count(), 1); + assert_eq!(slot_0_stores.get(&0).unwrap().count(), 1); assert_eq!(slot_1_stores[&1].count(), 2); - assert_eq!(slot_0_stores[&0].approx_stored_count(), 2); + assert_eq!(slot_0_stores.get(&0).unwrap().approx_stored_count(), 2); assert_eq!(slot_1_stores[&1].approx_stored_count(), 2); } } @@ -2426,7 +2423,7 @@ pub mod tests { db.remove_unrooted_slot(unrooted_slot); assert!(db.load_slow(&ancestors, &key).is_none()); assert!(db.bank_hashes.read().unwrap().get(&unrooted_slot).is_none()); - assert!(db.storage.read().unwrap().0.get(&unrooted_slot).is_none()); + assert!(db.storage.0.get(&unrooted_slot).is_none()); assert!(db .accounts_index .read() @@ -2522,9 +2519,8 @@ pub mod tests { } fn check_storage(accounts: &AccountsDB, slot: Slot, count: usize) -> bool { - let storage = accounts.storage.read().unwrap(); - assert_eq!(storage.0[&slot].len(), 1); - let slot_storage = storage.0.get(&slot).unwrap(); + assert_eq!(accounts.storage.0.get(&slot).unwrap().len(), 1); + let slot_storage = accounts.storage.0.get(&slot).unwrap(); let mut total_count: usize = 0; for store in slot_storage.values() { assert_eq!(store.status(), AccountStorageStatus::Available); @@ -2627,14 +2623,11 @@ pub mod tests { } let mut append_vec_histogram = HashMap::new(); - for storage in accounts - .storage - .read() - .unwrap() - .0 - .values() - .flat_map(|x| x.values()) - { + let mut all_storages = vec![]; + for slot_storage in accounts.storage.0.iter() { + all_storages.extend(slot_storage.values().cloned()) + } + for storage in all_storages { *append_vec_histogram.entry(storage.slot).or_insert(0) += 1; } for count in append_vec_histogram.values() { @@ -2652,23 +2645,32 @@ pub mod tests { let account1 = Account::new(1, DEFAULT_FILE_SIZE as usize / 2, &pubkey1); accounts.store(0, &[(&pubkey1, &account1)]); { - let stores = accounts.storage.read().unwrap(); - assert_eq!(stores.0.len(), 1); - assert_eq!(stores.0[&0][&0].count(), 1); - assert_eq!(stores.0[&0][&0].status(), AccountStorageStatus::Available); + let stores = &accounts.storage.0; + assert_eq!(stores.len(), 1); + assert_eq!(stores.get(&0).unwrap()[&0].count(), 1); + assert_eq!( + stores.get(&0).unwrap()[&0].status(), + AccountStorageStatus::Available + ); } let pubkey2 = Pubkey::new_rand(); let account2 = Account::new(1, DEFAULT_FILE_SIZE as usize / 2, &pubkey2); accounts.store(0, &[(&pubkey2, &account2)]); { - let stores = accounts.storage.read().unwrap(); - assert_eq!(stores.0.len(), 1); - assert_eq!(stores.0[&0].len(), 2); - assert_eq!(stores.0[&0][&0].count(), 1); - assert_eq!(stores.0[&0][&0].status(), AccountStorageStatus::Full); - assert_eq!(stores.0[&0][&1].count(), 1); - assert_eq!(stores.0[&0][&1].status(), AccountStorageStatus::Available); + let stores = &accounts.storage.0; + assert_eq!(stores.len(), 1); + assert_eq!(stores.get(&0).unwrap().len(), 2); + assert_eq!(stores.get(&0).unwrap()[&0].count(), 1); + assert_eq!( + stores.get(&0).unwrap()[&0].status(), + AccountStorageStatus::Full + ); + assert_eq!(stores.get(&0).unwrap()[&1].count(), 1); + assert_eq!( + stores.get(&0).unwrap()[&1].status(), + AccountStorageStatus::Available + ); } let ancestors = vec![(0, 0)].into_iter().collect(); assert_eq!( @@ -2685,15 +2687,15 @@ pub mod tests { let index = i % 2; accounts.store(0, &[(&pubkey1, &account1)]); { - let stores = accounts.storage.read().unwrap(); - assert_eq!(stores.0.len(), 1); - assert_eq!(stores.0[&0].len(), 3); - assert_eq!(stores.0[&0][&0].count(), count[index]); - assert_eq!(stores.0[&0][&0].status(), status[0]); - assert_eq!(stores.0[&0][&1].count(), 1); - assert_eq!(stores.0[&0][&1].status(), status[1]); - assert_eq!(stores.0[&0][&2].count(), count[index ^ 1]); - assert_eq!(stores.0[&0][&2].status(), status[0]); + let stores = &accounts.storage.0; + assert_eq!(stores.len(), 1); + assert_eq!(stores.get(&0).unwrap().len(), 3); + assert_eq!(stores.get(&0).unwrap()[&0].count(), count[index]); + assert_eq!(stores.get(&0).unwrap()[&0].status(), status[0]); + assert_eq!(stores.get(&0).unwrap()[&1].count(), 1); + assert_eq!(stores.get(&0).unwrap()[&1].status(), status[1]); + assert_eq!(stores.get(&0).unwrap()[&2].count(), count[index ^ 1]); + assert_eq!(stores.get(&0).unwrap()[&2].status(), status[0]); } let ancestors = vec![(0, 0)].into_iter().collect(); assert_eq!( @@ -2749,7 +2751,7 @@ pub mod tests { accounts.add_root(1); //slot is still there, since gc is lazy - assert!(accounts.storage.read().unwrap().0[&0].get(&id).is_some()); + assert!(accounts.storage.0.get(&0).unwrap().get(&id).is_some()); //store causes clean accounts.store(1, &[(&pubkey, &account)]); @@ -2758,7 +2760,7 @@ pub mod tests { accounts.print_accounts_stats("pre-clean"); accounts.clean_accounts(); accounts.process_dead_slots(None); - assert!(accounts.storage.read().unwrap().0.get(&0).is_none()); + assert!(accounts.storage.0.get(&0).is_none()); //new value is there let ancestors = vec![(1, 1)].into_iter().collect(); @@ -2767,9 +2769,7 @@ pub mod tests { impl AccountsDB { fn alive_account_count_in_store(&self, slot: Slot) -> usize { - let storage = self.storage.read().unwrap(); - - let slot_storage = storage.0.get(&slot); + let slot_storage = self.storage.0.get(&slot); if let Some(slot_storage) = slot_storage { slot_storage.values().map(|store| store.count()).sum() } else { @@ -2778,9 +2778,7 @@ pub mod tests { } fn all_account_count_in_append_vec(&self, slot: Slot) -> usize { - let storage = self.storage.read().unwrap(); - - let slot_storage = storage.0.get(&slot); + let slot_storage = self.storage.0.get(&slot); if let Some(slot_storage) = slot_storage { let count = slot_storage .values() @@ -3043,9 +3041,11 @@ pub mod tests { } fn assert_no_stores(accounts: &AccountsDB, slot: Slot) { - let stores = accounts.storage.read().unwrap(); - info!("{:?}", stores.0.get(&slot)); - assert!(stores.0.get(&slot).is_none() || stores.0.get(&slot).unwrap().is_empty()); + let slot_store = accounts.storage.0.get(&slot); + if let Some(ref x) = slot_store { + info!("{:?}", x.value()); + } + assert!(slot_store.is_none() || slot_store.unwrap().is_empty()); } #[test] @@ -3204,16 +3204,7 @@ pub mod tests { accounts.store(current_slot, &[(&pubkey2, &account2)]); // Store enough accounts such that an additional store for slot 2 is created. - while accounts - .storage - .read() - .unwrap() - .0 - .get(¤t_slot) - .unwrap() - .len() - < 2 - { + while accounts.storage.0.get(¤t_slot).unwrap().len() < 2 { accounts.store(current_slot, &[(&filler_account_pubkey, &filler_account)]); } accounts.add_root(current_slot); @@ -3818,13 +3809,7 @@ pub mod tests { let after_slot = base_slot + 1; db.store(base_slot, &[(&key, &account)]); - db.storage - .write() - .unwrap() - .0 - .get_mut(&base_slot) - .unwrap() - .clear(); + db.storage.0.get_mut(&base_slot).unwrap().clear(); db.add_root(base_slot); assert!(db.get_snapshot_storages(after_slot).is_empty()); @@ -3861,8 +3846,14 @@ pub mod tests { db.add_root(base_slot); assert_eq!(1, db.get_snapshot_storages(after_slot).len()); - let storage = db.storage.read().unwrap(); - storage.0[&0].values().next().unwrap().remove_account(); + db.storage + .0 + .get(&0) + .unwrap() + .values() + .next() + .unwrap() + .remove_account(); assert!(db.get_snapshot_storages(after_slot).is_empty()); } @@ -3873,8 +3864,15 @@ pub mod tests { let pubkey = Pubkey::new_rand(); let account = Account::new(1, 0, &Account::default().owner); accounts.store(0, &[(&pubkey, &account)]); - let storage = accounts.storage.read().unwrap(); - let storage_entry = storage.0[&0].values().next().unwrap(); + let storage_entry = accounts + .storage + .0 + .get(&0) + .unwrap() + .values() + .next() + .unwrap() + .clone(); storage_entry.remove_account(); storage_entry.remove_account(); }