ccp_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 std::collections::HashMap;
30use std::time::Duration;
31
32pub use jsonrpsee::core::ClientError;
33use jsonrpsee::proc_macros::rpc;
34pub use jsonrpsee::types::ErrorObjectOwned;
35
36use ccp_shared::proof::BatchRequest;
37use ccp_shared::proof::BatchResponse;
38use ccp_shared::proof::ProofIdx;
39use ccp_shared::types::Difficulty;
40use ccp_shared::types::GlobalNonce;
41use ccp_shared::types::LogicalCoreId;
42use ccp_shared::types::PhysicalCoreId;
43use ccp_shared::types::CUID;
44
45// n.b.: the rpc macro also defines CcpRpcClient type which is a working async JSON RPC client.
46#[rpc(server, client, namespace = "ccp")]
47pub trait CCPRpc {
48    #[method(name = "on_active_commitment", param_kind = map)]
49    async fn on_active_commitment(
50        &self,
51        global_nonce: GlobalNonce,
52        difficulty: Difficulty,
53        cu_allocation: HashMap<PhysicalCoreId, CUID>,
54    ) -> Result<(), ErrorObjectOwned>;
55
56    #[method(name = "on_no_active_commitment")]
57    async fn on_no_active_commitment(&self) -> Result<(), ErrorObjectOwned>;
58
59    #[method(name = "get_proofs_after")]
60    async fn get_proofs_after(
61        &self,
62        last_known_proofs: HashMap<CUID, ProofIdx>,
63        limit: usize,
64    ) -> Result<Vec<BatchResponse>, ErrorObjectOwned>;
65
66    #[method(name = "get_batch_proofs_after")]
67    async fn get_batch_proofs_after(
68        &self,
69        reqs: HashMap<CUID, BatchRequest>,
70        min_batch_count: usize,
71        max_batch_count: usize,
72    ) -> Result<Vec<BatchResponse>, ErrorObjectOwned>;
73
74    #[method(name = "realloc_utility_cores", param_kind = map)]
75    async fn realloc_utility_cores(&self, utility_core_ids: Vec<LogicalCoreId>);
76}
77
78pub struct CCPRpcHttpClient {
79    inner: jsonrpsee::http_client::HttpClient,
80}
81
82impl CCPRpcHttpClient {
83    pub async fn new(endpoint_url: String) -> Result<Self, ClientError> {
84        let inner = jsonrpsee::http_client::HttpClientBuilder::default().build(endpoint_url)?;
85
86        Ok(Self { inner })
87    }
88
89    pub async fn with_timeout(
90        endpoint_url: String,
91        request_timeout: Duration,
92    ) -> Result<Self, ClientError> {
93        let builder =
94            jsonrpsee::http_client::HttpClientBuilder::default().request_timeout(request_timeout);
95        let inner = builder.build(endpoint_url)?;
96
97        Ok(Self { inner })
98    }
99
100    #[inline]
101    pub fn from_http_client(client: jsonrpsee::http_client::HttpClient) -> Self {
102        Self { inner: client }
103    }
104
105    pub async fn on_active_commitment(
106        &self,
107        global_nonce: GlobalNonce,
108        difficulty: Difficulty,
109        cu_allocation: HashMap<PhysicalCoreId, CUID>,
110    ) -> Result<(), ClientError> {
111        CCPRpcClient::on_active_commitment(&self.inner, global_nonce, difficulty, cu_allocation)
112            .await
113    }
114
115    #[inline]
116    pub async fn on_no_active_commitment(&self) -> Result<(), ClientError> {
117        CCPRpcClient::on_no_active_commitment(&self.inner).await
118    }
119
120    #[inline]
121    pub async fn get_proofs_after(
122        &self,
123        last_known_proofs: HashMap<CUID, ProofIdx>,
124        limit: usize,
125    ) -> Result<Vec<BatchResponse>, ClientError> {
126        CCPRpcClient::get_proofs_after(&self.inner, last_known_proofs, limit).await
127    }
128
129    #[inline]
130    pub async fn get_batch_proofs_after(
131        &self,
132        reqs: HashMap<CUID, BatchRequest>,
133        min_batch_count: usize,
134        max_batch_count: usize,
135    ) -> Result<Vec<BatchResponse>, ClientError> {
136        CCPRpcClient::get_batch_proofs_after(&self.inner, reqs, min_batch_count, max_batch_count)
137            .await
138    }
139
140    #[inline]
141    pub async fn realloc_utility_cores(
142        &self,
143        utility_core_ids: Vec<LogicalCoreId>,
144    ) -> Result<(), ClientError> {
145        CCPRpcClient::realloc_utility_cores(&self.inner, utility_core_ids).await
146    }
147}