Skip to main content

ccp_cu_rpc_client/
lib.rs

1/*
2 * Copyright 2024 Fluence DAO
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#![warn(rust_2018_idioms)]
18#![warn(rust_2021_compatibility)]
19#![deny(
20    dead_code,
21    nonstandard_style,
22    unused_imports,
23    unused_mut,
24    unused_variables,
25    unused_unsafe,
26    unreachable_patterns
27)]
28
29use hyper_util::rt::TokioIo;
30use tokio::net::UnixStream;
31use tonic::transport::{Channel, Endpoint, Uri};
32use tower::service_fn;
33
34pub use ccp_shared::proof::raw::RawProof;
35pub use ccp_shared::types::EpochParameters;
36
37pub use crate::ccp_collector_rpc::collector_client::CollectorClient;
38pub use crate::ccp_collector_rpc::HashRate;
39pub use crate::ccp_collector_rpc::MalformedRpcProof;
40pub use crate::ccp_collector_rpc::ReportRequest;
41pub use crate::ccp_collector_rpc::RpcProof;
42
43pub mod ccp_collector_rpc {
44    use crate::EpochParameters;
45    use crate::RawProof;
46
47    pub const COLLECTOR_DESCRIPTOR_SET: &[u8] =
48        tonic::include_file_descriptor_set!("collector_descriptor");
49
50    tonic::include_proto!("cu_rpc_client");
51
52    impl From<&RawProof> for RpcProof {
53        fn from(value: &RawProof) -> Self {
54            let epoch = RpcEpochParameters {
55                difficulty: value.epoch.difficulty.to_string(),
56                global_nonce: value.epoch.global_nonce.to_string(),
57            };
58            Self {
59                epoch: Some(epoch),
60                cu_id: value.cu_id.to_string(),
61                local_nonce: value.local_nonce.to_string(),
62                result_hash: value.result_hash.to_string(),
63            }
64        }
65    }
66
67    #[derive(thiserror::Error, Debug, Clone)]
68    pub enum MalformedRpcProof {
69        #[error("field {0} is undefined")]
70        MissingField(&'static str),
71        #[error("field {0} is malformed: {1:?}")]
72        MalformedField(&'static str, hex::FromHexError),
73    }
74
75    impl TryFrom<&RpcProof> for RawProof {
76        type Error = MalformedRpcProof;
77
78        fn try_from(value: &RpcProof) -> Result<Self, Self::Error> {
79            use MalformedRpcProof::*;
80
81            let epoch_params = value.epoch.as_ref().ok_or(MissingField("epoch"))?;
82            let epoch = ccp_shared::types::EpochParameters {
83                global_nonce: epoch_params
84                    .global_nonce
85                    .parse()
86                    .map_err(|e| MalformedField("global_nonce", e))?,
87                difficulty: epoch_params
88                    .difficulty
89                    .parse()
90                    .map_err(|e| MalformedField("difficulty", e))?,
91            };
92
93            Ok(Self {
94                epoch,
95                local_nonce: value
96                    .local_nonce
97                    .parse()
98                    .map_err(|e| MalformedField("local_nonce", e))?,
99                cu_id: value
100                    .cu_id
101                    .parse()
102                    .map_err(|e| MalformedField("cu_id", e))?,
103                result_hash: value
104                    .result_hash
105                    .parse()
106                    .map_err(|e| MalformedField("result_hash", e))?,
107            })
108        }
109    }
110
111    impl HashRate {
112        pub fn cache_creation(duration_secs: f32, epoch: EpochParameters) -> Self {
113            Self {
114                checked_hashes_count: 0,
115                cache_creation: duration_secs,
116                dataset_initialization: 0.0,
117                cc_job_duration_secs: 0.0,
118                found_proofs_count: 0,
119                epoch: Some(epoch.into()),
120            }
121        }
122
123        pub fn dataset_initialization(duration_secs: f32, epoch: EpochParameters) -> Self {
124            Self {
125                checked_hashes_count: 0,
126                cache_creation: 0.0,
127                dataset_initialization: duration_secs,
128                cc_job_duration_secs: 0.0,
129                found_proofs_count: 0,
130                epoch: Some(epoch.into()),
131            }
132        }
133
134        pub fn checked_hashes(duration_secs: f32, count: usize, epoch: EpochParameters) -> Self {
135            Self {
136                checked_hashes_count: count as u64,
137                cache_creation: 0.0,
138                dataset_initialization: 0.0,
139                cc_job_duration_secs: duration_secs,
140                found_proofs_count: 0,
141                epoch: Some(epoch.into()),
142            }
143        }
144
145        pub fn proofs_found(count: u64, epoch: EpochParameters) -> Self {
146            Self {
147                checked_hashes_count: 0,
148                cache_creation: 0.0,
149                dataset_initialization: 0.0,
150                cc_job_duration_secs: 0.0,
151                found_proofs_count: count,
152                epoch: Some(epoch.into()),
153            }
154        }
155    }
156
157    impl From<EpochParameters> for RpcEpochParameters {
158        fn from(value: EpochParameters) -> Self {
159            Self {
160                global_nonce: value.global_nonce.to_string(),
161                difficulty: value.difficulty.to_string(),
162            }
163        }
164    }
165}
166
167pub type ClientError = tonic::Status;
168
169// n.b.: the rpc macro also defines CcpCuRpcClient type which is a working async JSON RPC client.
170pub struct CcpCuRpcHttpClient {
171    proofs_inner: CollectorClient<Channel>,
172}
173
174impl CcpCuRpcHttpClient {
175    pub async fn new_http(url: String) -> Result<Self, tonic::transport::Error> {
176        let proofs_inner = CollectorClient::connect(url.clone()).await?;
177        Ok(Self { proofs_inner })
178    }
179
180    pub async fn new_unix_socket(path: String) -> Result<Self, tonic::transport::Error> {
181        // The URL is dummy and is ignored by the connector function
182        let channel = Endpoint::try_from("http://[::]:9999")?
183            .connect_with_connector(service_fn(move |_: Uri| {
184                let path = path.clone();
185                async {
186                    // Connect to a Uds socket
187                    Ok::<_, std::io::Error>(TokioIo::new(UnixStream::connect(path).await?))
188                }
189            }))
190            .await?;
191        let proofs_inner = CollectorClient::new(channel.clone());
192
193        Ok(Self { proofs_inner })
194    }
195
196    pub async fn auto(endpoint: String) -> Result<Self, tonic::transport::Error> {
197        if endpoint.starts_with("http://")
198            || endpoint.starts_with("https://")
199            || endpoint.starts_with("grpc://")
200        {
201            Self::new_http(endpoint).await
202        } else {
203            Self::new_unix_socket(endpoint).await
204        }
205    }
206
207    pub async fn report(
208        &mut self,
209        proof: &[RawProof],
210        metrics: Vec<crate::ccp_collector_rpc::HashRate>,
211    ) -> Result<(), ClientError> {
212        let proofs: Vec<_> = proof.iter().map(Into::into).collect();
213
214        // it can be empty because some metrics are skipped
215        if !proofs.is_empty() || !metrics.is_empty() {
216            let report_request = ReportRequest { proofs, metrics };
217            self.proofs_inner.report(report_request).await?;
218        }
219        Ok(())
220    }
221}