2019-12-02 14:42:05 -08:00
|
|
|
use crate::blocktree_db::Result;
|
2019-11-02 00:38:30 -07:00
|
|
|
use crate::{blocktree::*, blocktree_meta::SlotMeta};
|
2019-10-20 08:54:38 -07:00
|
|
|
use log::*;
|
2019-11-02 00:38:30 -07:00
|
|
|
use solana_sdk::clock::Slot;
|
2019-05-20 23:09:00 -07:00
|
|
|
|
|
|
|
pub struct RootedSlotIterator<'a> {
|
2019-11-02 00:38:30 -07:00
|
|
|
next_slots: Vec<Slot>,
|
2019-10-18 09:28:51 -07:00
|
|
|
blocktree: &'a Blocktree,
|
2019-05-20 23:09:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> RootedSlotIterator<'a> {
|
2019-11-02 00:38:30 -07:00
|
|
|
pub fn new(start_slot: Slot, blocktree: &'a Blocktree) -> Result<Self> {
|
2019-05-20 23:09:00 -07:00
|
|
|
if blocktree.is_root(start_slot) {
|
|
|
|
Ok(Self {
|
|
|
|
next_slots: vec![start_slot],
|
|
|
|
blocktree,
|
|
|
|
})
|
|
|
|
} else {
|
2019-10-17 10:37:30 -07:00
|
|
|
Err(BlocktreeError::SlotNotRooted)
|
2019-05-20 23:09:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<'a> Iterator for RootedSlotIterator<'a> {
|
2019-11-02 00:38:30 -07:00
|
|
|
type Item = (Slot, SlotMeta);
|
2019-05-20 23:09:00 -07:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
// Clone b/c passing the closure to the map below requires exclusive access to
|
|
|
|
// `self`, which is borrowed here if we don't clone.
|
|
|
|
let rooted_slot = self
|
|
|
|
.next_slots
|
|
|
|
.iter()
|
|
|
|
.find(|x| self.blocktree.is_root(**x))
|
|
|
|
.cloned();
|
|
|
|
|
2019-08-23 23:40:20 -07:00
|
|
|
rooted_slot
|
|
|
|
.map(|rooted_slot| {
|
|
|
|
let slot_meta = self
|
|
|
|
.blocktree
|
|
|
|
.meta(rooted_slot)
|
|
|
|
.expect("Database failure, couldnt fetch SlotMeta");
|
|
|
|
|
|
|
|
if slot_meta.is_none() {
|
|
|
|
warn!("Rooted SlotMeta was deleted in between checking is_root and fetch");
|
|
|
|
}
|
|
|
|
|
|
|
|
slot_meta.map(|slot_meta| {
|
|
|
|
self.next_slots = slot_meta.next_slots.clone();
|
|
|
|
(rooted_slot, slot_meta)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.unwrap_or(None)
|
2019-05-20 23:09:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-10-20 08:54:38 -07:00
|
|
|
use crate::blocktree_processor::fill_blocktree_slot_with_ticks;
|
2019-10-18 09:28:51 -07:00
|
|
|
use solana_sdk::hash::Hash;
|
2019-05-20 23:09:00 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rooted_slot_iterator() {
|
2019-11-13 07:14:09 -08:00
|
|
|
let blocktree_path = get_tmp_ledger_path!();
|
2019-05-20 23:09:00 -07:00
|
|
|
let blocktree = Blocktree::open(&blocktree_path).unwrap();
|
2019-05-29 09:43:22 -07:00
|
|
|
blocktree.set_roots(&[0]).unwrap();
|
2019-05-20 23:09:00 -07:00
|
|
|
let ticks_per_slot = 5;
|
|
|
|
/*
|
|
|
|
Build a blocktree in the ledger with the following fork structure:
|
|
|
|
|
|
|
|
slot 0
|
|
|
|
|
|
|
|
|
slot 1 <-- set_root(true)
|
|
|
|
/ \
|
|
|
|
slot 2 |
|
|
|
|
/ |
|
|
|
|
slot 3 |
|
|
|
|
|
|
|
|
|
slot 4
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Fork 1, ending at slot 3
|
|
|
|
let last_entry_hash = Hash::default();
|
|
|
|
let fork_point = 1;
|
|
|
|
let mut fork_hash = Hash::default();
|
|
|
|
for slot in 0..=3 {
|
|
|
|
let parent = {
|
|
|
|
if slot == 0 {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
slot - 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let last_entry_hash = fill_blocktree_slot_with_ticks(
|
|
|
|
&blocktree,
|
|
|
|
ticks_per_slot,
|
|
|
|
slot,
|
|
|
|
parent,
|
|
|
|
last_entry_hash,
|
|
|
|
);
|
|
|
|
|
|
|
|
if slot == fork_point {
|
|
|
|
fork_hash = last_entry_hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork 2, ending at slot 4
|
|
|
|
let _ =
|
|
|
|
fill_blocktree_slot_with_ticks(&blocktree, ticks_per_slot, 4, fork_point, fork_hash);
|
|
|
|
|
|
|
|
// Set a root
|
2019-05-29 09:43:22 -07:00
|
|
|
blocktree.set_roots(&[1, 2, 3]).unwrap();
|
2019-05-20 23:09:00 -07:00
|
|
|
|
|
|
|
// Trying to get an iterator on a different fork will error
|
|
|
|
assert!(RootedSlotIterator::new(4, &blocktree).is_err());
|
|
|
|
|
|
|
|
// Trying to get an iterator on any slot on the root fork should succeed
|
|
|
|
let result: Vec<_> = RootedSlotIterator::new(3, &blocktree)
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(slot, _)| slot)
|
|
|
|
.collect();
|
|
|
|
let expected = vec![3];
|
|
|
|
assert_eq!(result, expected);
|
|
|
|
|
|
|
|
let result: Vec<_> = RootedSlotIterator::new(0, &blocktree)
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(slot, _)| slot)
|
|
|
|
.collect();
|
|
|
|
let expected = vec![0, 1, 2, 3];
|
|
|
|
assert_eq!(result, expected);
|
|
|
|
|
|
|
|
drop(blocktree);
|
|
|
|
Blocktree::destroy(&blocktree_path).expect("Expected successful database destruction");
|
|
|
|
}
|
|
|
|
}
|