tendermint_light_client_detector/
trace.rs

1use tendermint_light_client::verifier::types::LightBlock;
2
3use super::Error;
4
5/// A trace of the light blocks that were used by the light client to verify a particular header,
6/// in the case of bisection or sequential verification.
7///
8/// The trace always contains at least two light blocks, the trusted block and the target block.
9#[derive(Clone, Debug)]
10pub struct Trace(Vec<LightBlock>);
11
12impl Trace {
13    pub fn new(mut trace: Vec<LightBlock>) -> Result<Self, Error> {
14        if trace.len() < 2 {
15            return Err(Error::trace_too_short(trace));
16        }
17
18        trace.sort_unstable_by_key(|lb| lb.height());
19
20        Ok(Self(trace))
21    }
22
23    pub fn first(&self) -> &LightBlock {
24        self.0.first().expect("trace is empty, which cannot happen")
25    }
26
27    pub fn last(&self) -> &LightBlock {
28        self.0.last().expect("trace is empty, which cannot happen")
29    }
30
31    pub fn len(&self) -> usize {
32        self.0.len()
33    }
34
35    pub fn is_empty(&self) -> bool {
36        self.0.is_empty()
37    }
38
39    pub fn iter(&self) -> std::slice::Iter<'_, LightBlock> {
40        self.0.iter()
41    }
42
43    pub fn into_vec(self) -> Vec<LightBlock> {
44        self.0
45    }
46}
47
48impl IntoIterator for Trace {
49    type Item = LightBlock;
50    type IntoIter = std::vec::IntoIter<Self::Item>;
51
52    fn into_iter(self) -> Self::IntoIter {
53        self.0.into_iter()
54    }
55}