xmtp_api_d14n/endpoints/v3/mls/
publish_commit_log.rs

1use derive_builder::Builder;
2use prost::Message;
3use prost::bytes::Bytes;
4use std::borrow::Cow;
5use xmtp_proto::api::{BodyError, Endpoint};
6use xmtp_proto::mls_v1::{BatchPublishCommitLogRequest, PublishCommitLogRequest};
7
8#[derive(Debug, Builder, Default)]
9#[builder(setter(strip_option), build_fn(error = "BodyError"))]
10pub struct PublishCommitLog {
11    #[builder(setter(into))]
12    commit_log_entries: Vec<PublishCommitLogRequest>,
13}
14
15impl PublishCommitLog {
16    pub fn builder() -> PublishCommitLogBuilder {
17        Default::default()
18    }
19}
20
21impl Endpoint for PublishCommitLog {
22    type Output = ();
23    fn grpc_endpoint(&self) -> Cow<'static, str> {
24        xmtp_proto::path_and_query::<BatchPublishCommitLogRequest>()
25    }
26
27    fn body(&self) -> Result<Bytes, BodyError> {
28        Ok(BatchPublishCommitLogRequest {
29            requests: self.commit_log_entries.clone(),
30        }
31        .encode_to_vec()
32        .into())
33    }
34}
35
36#[cfg(test)]
37mod test {
38    use crate::v3::PublishCommitLog;
39    use xmtp_api_grpc::error::GrpcError;
40    use xmtp_api_grpc::test::NodeGoClient;
41    use xmtp_common::rand_vec;
42    use xmtp_proto::xmtp::mls::api::v1::*;
43    use xmtp_proto::{api, prelude::*};
44
45    #[xmtp_common::test]
46    fn test_file_descriptor() {
47        let pnq = xmtp_proto::path_and_query::<BatchPublishCommitLogRequest>();
48        println!("{}", pnq);
49    }
50
51    #[xmtp_common::test]
52    fn test_grpc_endpoint_returns_correct_path() {
53        let endpoint = PublishCommitLog::default();
54        assert_eq!(
55            endpoint.grpc_endpoint(),
56            "/xmtp.mls.api.v1.MlsApi/BatchPublishCommitLog"
57        );
58    }
59
60    #[xmtp_common::test]
61    async fn test_publish_commit_log() {
62        let client = NodeGoClient::create();
63        let client = client.build().unwrap();
64        let endpoint = PublishCommitLog::builder()
65            .commit_log_entries(vec![PublishCommitLogRequest {
66                group_id: rand_vec::<16>(),
67                serialized_commit_log_entry: rand_vec::<32>(),
68                signature: None,
69            }])
70            .build()
71            .unwrap();
72
73        let err = api::ignore(endpoint).query(&client).await.unwrap_err();
74        // the request will fail b/c we're using dummy data but
75        // we just care if the endpoint is working
76        match err {
77            ApiClientError::<GrpcError>::ClientWithEndpoint {
78                source: GrpcError::Status(ref s),
79                ..
80            } => {
81                assert!(s.message().contains("invalid commit log entry"), "{}", err);
82            }
83            _ => panic!("request failed"),
84        }
85    }
86}