cometbft_light_client/
instance.rs1use cometbft::block::Height;
4
5use crate::{
6 errors::Error,
7 light_client::LightClient,
8 state::State,
9 verifier::types::{LightBlock, Status},
10};
11
12#[derive(Debug)]
14pub struct Instance {
15 pub light_client: LightClient,
17
18 pub state: State,
20}
21
22impl Instance {
23 pub fn new(light_client: LightClient, state: State) -> Self {
25 Self {
26 light_client,
27 state,
28 }
29 }
30
31 pub fn peer_id(&self) -> &cometbft::node::Id {
33 &self.light_client.peer
34 }
35
36 pub fn latest_trusted(&self) -> Option<LightBlock> {
38 self.state.light_store.highest(Status::Trusted)
39 }
40
41 pub fn trust_block(&mut self, lb: &LightBlock) {
43 self.state.light_store.update(lb, Status::Trusted);
44 }
45
46 pub fn get_or_fetch_block(&mut self, height: Height) -> Result<LightBlock, Error> {
48 let (block, _) = self
49 .light_client
50 .get_or_fetch_block(height, &mut self.state)
51 .map_err(|e| {
52 if e.to_string()
54 .contains("must be less than or equal to the current blockchain height")
55 {
56 Error::height_too_high(height, Height::default())
58 } else {
59 e
60 }
61 })?;
62
63 Ok(block)
64 }
65}