Skip to main content

celestia_client/
fraud.rs

1use std::pin::Pin;
2use std::sync::Arc;
3
4use async_stream::try_stream;
5use celestia_rpc::FraudClient;
6use futures_util::{Stream, StreamExt};
7
8use crate::Result;
9use crate::api::fraud::{Proof, ProofType};
10use crate::client::ClientInner;
11
12/// Fraud API for quering bridge nodes.
13pub struct FraudApi {
14    inner: Arc<ClientInner>,
15}
16
17impl FraudApi {
18    pub(crate) fn new(inner: Arc<ClientInner>) -> FraudApi {
19        FraudApi { inner }
20    }
21
22    /// Fetches fraud proofs from node by their type.
23    pub async fn get(&self, proof_type: ProofType) -> Result<Vec<Proof>> {
24        Ok(self.inner.rpc.fraud_get(proof_type).await?)
25    }
26
27    /// Subscribe to fraud proof by its type.
28    pub fn subscribe(
29        &self,
30        proof_type: ProofType,
31    ) -> Pin<Box<dyn Stream<Item = Result<Proof>> + Send + 'static>> {
32        let inner = self.inner.clone();
33
34        // we need to re-stream it to map error and satisfy 'static
35        try_stream! {
36            let mut subscription = inner.rpc.fraud_subscribe(proof_type);
37            while let Some(item) = subscription.next().await {
38                // TODO: Should we validate proof?
39                yield item?;
40            }
41        }
42        .boxed()
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    use crate::test_utils::ensure_serializable_deserializable;
51
52    #[allow(dead_code)]
53    #[allow(unused_variables)]
54    #[allow(unreachable_code)]
55    #[allow(clippy::diverging_sub_expression)]
56    async fn enforce_serde_bounds() {
57        // intentionally no-run, compile only test
58        let api = FraudApi::new(unimplemented!());
59
60        let proof_type = ensure_serializable_deserializable(unimplemented!());
61        ensure_serializable_deserializable(api.get(proof_type).await.unwrap());
62
63        let proof_type = ensure_serializable_deserializable(unimplemented!());
64        ensure_serializable_deserializable(
65            api.subscribe(proof_type).next().await.unwrap().unwrap(),
66        );
67    }
68}