blockstore: Make is_orphan() a method of SlotMeta (#34889)

The old function's only input is a SlotMeta, so makes sense to move
it a member function of SlotMeta
This commit is contained in:
steviez 2024-01-22 19:14:51 -06:00 committed by GitHub
parent 098076f5ca
commit 9122193e17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 14 deletions

View File

@ -1803,7 +1803,7 @@ impl Blockstore {
}; };
// Parent for slot meta should have been set by this point // Parent for slot meta should have been set by this point
assert!(!is_orphan(slot_meta)); assert!(!slot_meta.is_orphan());
let new_consumed = if slot_meta.consumed == index { let new_consumed = if slot_meta.consumed == index {
let mut current_index = index + 1; let mut current_index = index + 1;
@ -3822,7 +3822,8 @@ impl Blockstore {
let meta_backup = &slot_meta_entry.old_slot_meta; let meta_backup = &slot_meta_entry.old_slot_meta;
{ {
let mut meta_mut = meta.borrow_mut(); let mut meta_mut = meta.borrow_mut();
let was_orphan_slot = meta_backup.is_some() && is_orphan(meta_backup.as_ref().unwrap()); let was_orphan_slot =
meta_backup.is_some() && meta_backup.as_ref().unwrap().is_orphan();
// If: // If:
// 1) This is a new slot // 1) This is a new slot
@ -3848,7 +3849,7 @@ impl Blockstore {
// If the parent of `slot` is a newly inserted orphan, insert it into the orphans // If the parent of `slot` is a newly inserted orphan, insert it into the orphans
// column family // column family
if is_orphan(&RefCell::borrow(&*prev_slot_meta)) { if RefCell::borrow(&*prev_slot_meta).is_orphan() {
write_batch.put::<cf::Orphans>(prev_slot, &true)?; write_batch.put::<cf::Orphans>(prev_slot, &true)?;
} }
} }
@ -3956,7 +3957,7 @@ impl Blockstore {
// during the chaining process, see the function find_slot_meta_in_cached_state() // during the chaining process, see the function find_slot_meta_in_cached_state()
// for details. Slots that are orphans are missing a parent_slot, so we should // for details. Slots that are orphans are missing a parent_slot, so we should
// fill in the parent now that we know it. // fill in the parent now that we know it.
if is_orphan(&meta) { if meta.is_orphan() {
meta.parent_slot = Some(parent_slot); meta.parent_slot = Some(parent_slot);
} }
@ -4216,12 +4217,6 @@ fn find_slot_meta_in_cached_state<'a>(
} }
} }
fn is_orphan(meta: &SlotMeta) -> bool {
// If we have no parent, then this is the head of a detached chain of
// slots
meta.parent_slot.is_none()
}
// 1) Chain current_slot to the previous slot defined by prev_slot_meta // 1) Chain current_slot to the previous slot defined by prev_slot_meta
fn chain_new_slot_to_prev_slot( fn chain_new_slot_to_prev_slot(
prev_slot_meta: &mut SlotMeta, prev_slot_meta: &mut SlotMeta,
@ -6324,7 +6319,7 @@ pub mod tests {
.meta(1) .meta(1)
.expect("Expect database get to succeed") .expect("Expect database get to succeed")
.unwrap(); .unwrap();
assert!(is_orphan(&meta)); assert!(meta.is_orphan());
assert_eq!( assert_eq!(
blockstore.orphans_iterator(0).unwrap().collect::<Vec<_>>(), blockstore.orphans_iterator(0).unwrap().collect::<Vec<_>>(),
vec![1] vec![1]
@ -6340,12 +6335,12 @@ pub mod tests {
.meta(1) .meta(1)
.expect("Expect database get to succeed") .expect("Expect database get to succeed")
.unwrap(); .unwrap();
assert!(!is_orphan(&meta)); assert!(!meta.is_orphan());
let meta = blockstore let meta = blockstore
.meta(0) .meta(0)
.expect("Expect database get to succeed") .expect("Expect database get to succeed")
.unwrap(); .unwrap();
assert!(is_orphan(&meta)); assert!(meta.is_orphan());
assert_eq!( assert_eq!(
blockstore.orphans_iterator(0).unwrap().collect::<Vec<_>>(), blockstore.orphans_iterator(0).unwrap().collect::<Vec<_>>(),
vec![0] vec![0]
@ -6369,7 +6364,7 @@ pub mod tests {
.meta(i) .meta(i)
.expect("Expect database get to succeed") .expect("Expect database get to succeed")
.unwrap(); .unwrap();
assert!(!is_orphan(&meta)); assert!(!meta.is_orphan());
} }
// Orphans cf is empty // Orphans cf is empty
assert!(blockstore.orphans_cf.is_empty().unwrap()); assert!(blockstore.orphans_cf.is_empty().unwrap());

View File

@ -263,6 +263,13 @@ impl SlotMeta {
Some(self.consumed) == self.last_index.map(|ix| ix + 1) Some(self.consumed) == self.last_index.map(|ix| ix + 1)
} }
/// Returns a boolean indicating whether this meta's parent slot is known.
/// This value being true indicates that this meta's slot is the head of a
/// detached chain of slots.
pub(crate) fn is_orphan(&self) -> bool {
self.parent_slot.is_none()
}
/// Returns a boolean indicating whether the meta is connected. /// Returns a boolean indicating whether the meta is connected.
pub fn is_connected(&self) -> bool { pub fn is_connected(&self) -> bool {
self.connected_flags.contains(ConnectedFlags::CONNECTED) self.connected_flags.contains(ConnectedFlags::CONNECTED)