1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use ckb_store::ChainStore;
use ckb_types::{core::BlockNumber, core::BlockView};
pub struct ChainIterator<'a, S: ChainStore<'a>> {
store: &'a S,
current: Option<BlockView>,
tip: BlockNumber,
}
impl<'a, S: ChainStore<'a>> ChainIterator<'a, S> {
pub fn new(store: &'a S) -> Self {
let current = store.get_block_hash(0).and_then(|h| store.get_block(&h));
let tip = store.get_tip_header().expect("store inited").number();
ChainIterator {
store,
current,
tip,
}
}
pub fn len(&self) -> u64 {
self.tip + 1
}
pub fn is_empty(&self) -> bool {
false
}
}
impl<'a, S: ChainStore<'a>> Iterator for ChainIterator<'a, S> {
type Item = BlockView;
fn next(&mut self) -> Option<Self::Item> {
let current = self.current.take();
self.current = match current {
Some(ref b) => {
if let Some(block_hash) = self.store.get_block_hash(b.header().number() + 1) {
self.store.get_block(&block_hash)
} else {
None
}
}
None => None,
};
current
}
fn size_hint(&self) -> (usize, Option<usize>) {
match self.current {
Some(ref b) => (1, Some((self.tip - b.header().number() + 1) as usize)),
None => (0, None),
}
}
}