persist historical_roots (#24029)

This commit is contained in:
Jeff Washington (jwash) 2022-04-04 13:13:11 -05:00 committed by GitHub
parent 132f08486a
commit 6a7f6585ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 4 deletions

View File

@ -429,8 +429,8 @@ where
snapshot_version,
snapshot_slot,
snapshot_bank_hash_info,
_snapshot_historical_roots,
_snapshot_historical_roots_with_hash,
snapshot_historical_roots,
snapshot_historical_roots_with_hash,
) = snapshot_accounts_db_fields.collapse_into()?;
let snapshot_storages = snapshot_storages.into_iter().collect::<Vec<_>>();
@ -441,6 +441,12 @@ where
.unwrap_or_else(|err| panic!("Failed to create directory {}: {}", path.display(), err));
}
reconstruct_historical_roots(
&accounts_db,
snapshot_historical_roots,
snapshot_historical_roots_with_hash,
);
// Remap the deserialized AppendVec paths to point to correct local paths
let num_collisions = AtomicUsize::new(0);
let next_append_vec_id = AtomicAppendVecId::new(0);
@ -576,3 +582,25 @@ where
ReconstructedAccountsDbInfo { accounts_data_len },
))
}
/// populate 'historical_roots' from 'snapshot_historical_roots' and 'snapshot_historical_roots_with_hash'
fn reconstruct_historical_roots(
accounts_db: &AccountsDb,
mut snapshot_historical_roots: Vec<Slot>,
snapshot_historical_roots_with_hash: Vec<(Slot, Hash)>,
) {
// inflate 'historical_roots'
// inserting into 'historical_roots' needs to be in order
// combine the slots into 1 vec, then sort
// dups are ok
snapshot_historical_roots.extend(
snapshot_historical_roots_with_hash
.into_iter()
.map(|(root, _)| root),
);
snapshot_historical_roots.sort_unstable();
let mut roots_tracker = accounts_db.accounts_index.roots_tracker.write().unwrap();
snapshot_historical_roots.into_iter().for_each(|root| {
roots_tracker.historical_roots.insert(root);
});
}

View File

@ -238,14 +238,13 @@ impl<'a> TypeContext<'a> for Context {
.unwrap_or_else(|| panic!("No bank_hashes entry for slot {}", serializable_db.slot))
.clone();
// for now, historical_roots is the same as 'roots' and is redundant with the storages we persist in the snapshot
let historical_roots = serializable_db
.accounts_db
.accounts_index
.roots_tracker
.read()
.unwrap()
.alive_roots
.historical_roots
.get_all();
let historical_roots_with_hash = Vec::<(Slot, Hash)>::default();

View File

@ -333,3 +333,35 @@ mod test_bank_serialize {
.serialize(s)
}
}
#[test]
fn test_reconstruct_historical_roots() {
{
let db = AccountsDb::default_for_tests();
let historical_roots = vec![];
let historical_roots_with_hash = vec![];
reconstruct_historical_roots(&db, historical_roots, historical_roots_with_hash);
let roots_tracker = db.accounts_index.roots_tracker.read().unwrap();
assert!(roots_tracker.historical_roots.is_empty());
}
{
let db = AccountsDb::default_for_tests();
let historical_roots = vec![1];
let historical_roots_with_hash = vec![(0, Hash::default())];
reconstruct_historical_roots(&db, historical_roots, historical_roots_with_hash);
let roots_tracker = db.accounts_index.roots_tracker.read().unwrap();
assert_eq!(roots_tracker.historical_roots.get_all(), vec![0, 1]);
}
{
let db = AccountsDb::default_for_tests();
let historical_roots = vec![2, 1];
let historical_roots_with_hash = vec![0, 5]
.into_iter()
.map(|slot| (slot, Hash::default()))
.collect();
reconstruct_historical_roots(&db, historical_roots, historical_roots_with_hash);
let roots_tracker = db.accounts_index.roots_tracker.read().unwrap();
assert_eq!(roots_tracker.historical_roots.get_all(), vec![0, 1, 2, 5]);
}
}