tendermint_light_client/
state.rs1use std::collections::{HashMap, HashSet};
4
5use contracts::*;
6
7use crate::{
8 store::LightStore,
9 verifier::types::{Height, LightBlock},
10};
11
12pub type VerificationTrace = HashMap<Height, HashSet<Height>>;
14
15#[derive(Debug)]
17pub struct State {
18 pub light_store: Box<dyn LightStore>,
20
21 pub verification_trace: VerificationTrace,
23}
24
25impl State {
26 pub fn new(light_store: impl LightStore + 'static) -> Self {
28 Self {
29 light_store: Box::new(light_store),
30 verification_trace: VerificationTrace::new(),
31 }
32 }
33
34 #[requires(height <= target_height)]
39 pub fn trace_block(&mut self, target_height: Height, height: Height) {
40 self.verification_trace
41 .entry(target_height)
42 .or_insert_with(|| {
43 let mut trace = HashSet::new();
44 trace.insert(target_height);
45 trace
46 })
47 .insert(height);
48 }
49
50 pub fn get_trace(&self, target_height: Height) -> Vec<LightBlock> {
52 let mut trace = self
53 .verification_trace
54 .get(&target_height)
55 .unwrap_or(&HashSet::new())
56 .iter()
57 .flat_map(|&height| self.light_store.get_trusted_or_verified(height))
58 .collect::<Vec<_>>();
59
60 trace.sort_by_key(|lb| lb.height());
61 trace
62 }
63}