layer_climb_core/querier/
fetch.rs1use tracing::instrument;
6
7use super::basic::BlockHeightReq;
8use crate::prelude::*;
9
10impl QueryClient {
11 #[instrument]
12 pub async fn fetch_signed_header(
13 &self,
14 height: Option<u64>,
15 ) -> Result<layer_climb_proto::tendermint::SignedHeader> {
16 self.run_with_middleware(SignedHeaderReq { height }).await
17 }
18 #[instrument]
19 pub async fn fetch_block_events(
20 &self,
21 block_height: u64,
22 ) -> Result<Vec<tendermint::abci::Event>> {
23 self.run_with_middleware(BlockEventsReq {
24 height: block_height,
25 })
26 .await
27 }
28}
29
30#[derive(Clone, Debug)]
31struct SignedHeaderReq {
32 pub height: Option<u64>,
33}
34
35impl QueryRequest for SignedHeaderReq {
36 type QueryResponse = layer_climb_proto::tendermint::SignedHeader;
37
38 async fn request(
39 &self,
40 client: QueryClient,
41 ) -> Result<layer_climb_proto::tendermint::SignedHeader> {
42 let height = match self.height {
43 Some(height) => height,
44 None => BlockHeightReq {}.request(client.clone()).await?,
45 };
46
47 Ok(client
49 .rpc_client()?
50 .commit(height)
51 .await?
52 .signed_header
53 .into())
54 }
55}
56
57#[derive(Clone, Debug)]
58struct BlockEventsReq {
59 pub height: u64,
60}
61
62impl QueryRequest for BlockEventsReq {
63 type QueryResponse = Vec<tendermint::abci::Event>;
64
65 async fn request(&self, client: QueryClient) -> Result<Vec<tendermint::abci::Event>> {
66 let mut response = client.rpc_client()?.block_results(self.height).await?;
68
69 let mut events: Vec<tendermint::abci::Event> = vec![];
70
71 if let Some(begin_block_events) = &mut response.begin_block_events {
72 events.append(begin_block_events);
73 }
74
75 if let Some(txs_results) = &mut response.txs_results {
76 for tx_result in txs_results {
77 if tx_result.code != tendermint::abci::Code::Ok {
78 continue;
80 }
81
82 events.append(&mut tx_result.events);
83 }
84 }
85
86 if let Some(end_block_events) = &mut response.end_block_events {
87 events.append(end_block_events);
88 }
89
90 Ok(events)
91 }
92}