linera_rpc/
client.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use linera_base::{
5    crypto::CryptoHash,
6    data_types::{BlobContent, NetworkDescription},
7    identifiers::{BlobId, ChainId},
8};
9use linera_chain::{
10    data_types::BlockProposal,
11    types::{
12        ConfirmedBlockCertificate, LiteCertificate, TimeoutCertificate, ValidatedBlockCertificate,
13    },
14};
15use linera_core::{
16    data_types::{ChainInfoQuery, ChainInfoResponse},
17    node::{CrossChainMessageDelivery, NodeError, NotificationStream, ValidatorNode},
18};
19
20use crate::grpc::GrpcClient;
21#[cfg(with_simple_network)]
22use crate::simple::SimpleClient;
23
24#[derive(Clone)]
25pub enum Client {
26    Grpc(GrpcClient),
27    #[cfg(with_simple_network)]
28    Simple(SimpleClient),
29}
30
31impl From<GrpcClient> for Client {
32    fn from(client: GrpcClient) -> Self {
33        Self::Grpc(client)
34    }
35}
36
37#[cfg(with_simple_network)]
38impl From<SimpleClient> for Client {
39    fn from(client: SimpleClient) -> Self {
40        Self::Simple(client)
41    }
42}
43
44impl ValidatorNode for Client {
45    type NotificationStream = NotificationStream;
46
47    async fn handle_block_proposal(
48        &self,
49        proposal: BlockProposal,
50    ) -> Result<ChainInfoResponse, NodeError> {
51        match self {
52            Client::Grpc(grpc_client) => grpc_client.handle_block_proposal(proposal).await,
53
54            #[cfg(with_simple_network)]
55            Client::Simple(simple_client) => simple_client.handle_block_proposal(proposal).await,
56        }
57    }
58
59    async fn handle_lite_certificate(
60        &self,
61        certificate: LiteCertificate<'_>,
62        delivery: CrossChainMessageDelivery,
63    ) -> Result<ChainInfoResponse, NodeError> {
64        match self {
65            Client::Grpc(grpc_client) => {
66                grpc_client
67                    .handle_lite_certificate(certificate, delivery)
68                    .await
69            }
70
71            #[cfg(with_simple_network)]
72            Client::Simple(simple_client) => {
73                simple_client
74                    .handle_lite_certificate(certificate, delivery)
75                    .await
76            }
77        }
78    }
79
80    async fn handle_timeout_certificate(
81        &self,
82        certificate: TimeoutCertificate,
83    ) -> Result<ChainInfoResponse, NodeError> {
84        match self {
85            Client::Grpc(grpc_client) => grpc_client.handle_timeout_certificate(certificate).await,
86
87            #[cfg(with_simple_network)]
88            Client::Simple(simple_client) => {
89                simple_client.handle_timeout_certificate(certificate).await
90            }
91        }
92    }
93
94    async fn handle_confirmed_certificate(
95        &self,
96        certificate: ConfirmedBlockCertificate,
97        delivery: CrossChainMessageDelivery,
98    ) -> Result<ChainInfoResponse, NodeError> {
99        match self {
100            Client::Grpc(grpc_client) => {
101                grpc_client
102                    .handle_confirmed_certificate(certificate, delivery)
103                    .await
104            }
105
106            #[cfg(with_simple_network)]
107            Client::Simple(simple_client) => {
108                simple_client
109                    .handle_confirmed_certificate(certificate, delivery)
110                    .await
111            }
112        }
113    }
114
115    async fn handle_validated_certificate(
116        &self,
117        certificate: ValidatedBlockCertificate,
118    ) -> Result<ChainInfoResponse, NodeError> {
119        match self {
120            Client::Grpc(grpc_client) => {
121                grpc_client.handle_validated_certificate(certificate).await
122            }
123
124            #[cfg(with_simple_network)]
125            Client::Simple(simple_client) => {
126                simple_client
127                    .handle_validated_certificate(certificate)
128                    .await
129            }
130        }
131    }
132
133    async fn handle_chain_info_query(
134        &self,
135        query: ChainInfoQuery,
136    ) -> Result<ChainInfoResponse, NodeError> {
137        match self {
138            Client::Grpc(grpc_client) => grpc_client.handle_chain_info_query(query).await,
139
140            #[cfg(with_simple_network)]
141            Client::Simple(simple_client) => simple_client.handle_chain_info_query(query).await,
142        }
143    }
144
145    async fn subscribe(&self, chains: Vec<ChainId>) -> Result<Self::NotificationStream, NodeError> {
146        Ok(match self {
147            Client::Grpc(grpc_client) => Box::pin(grpc_client.subscribe(chains).await?),
148
149            #[cfg(with_simple_network)]
150            Client::Simple(simple_client) => Box::pin(simple_client.subscribe(chains).await?),
151        })
152    }
153
154    async fn get_version_info(&self) -> Result<linera_version::VersionInfo, NodeError> {
155        Ok(match self {
156            Client::Grpc(grpc_client) => grpc_client.get_version_info().await?,
157
158            #[cfg(with_simple_network)]
159            Client::Simple(simple_client) => simple_client.get_version_info().await?,
160        })
161    }
162
163    async fn get_network_description(&self) -> Result<NetworkDescription, NodeError> {
164        Ok(match self {
165            Client::Grpc(grpc_client) => grpc_client.get_network_description().await?,
166
167            #[cfg(with_simple_network)]
168            Client::Simple(simple_client) => simple_client.get_network_description().await?,
169        })
170    }
171
172    async fn upload_blob(&self, content: BlobContent) -> Result<BlobId, NodeError> {
173        Ok(match self {
174            Client::Grpc(grpc_client) => grpc_client.upload_blob(content).await?,
175
176            #[cfg(with_simple_network)]
177            Client::Simple(simple_client) => simple_client.upload_blob(content).await?,
178        })
179    }
180
181    async fn download_blob(&self, blob_id: BlobId) -> Result<BlobContent, NodeError> {
182        Ok(match self {
183            Client::Grpc(grpc_client) => grpc_client.download_blob(blob_id).await?,
184
185            #[cfg(with_simple_network)]
186            Client::Simple(simple_client) => simple_client.download_blob(blob_id).await?,
187        })
188    }
189
190    async fn download_pending_blob(
191        &self,
192        chain_id: ChainId,
193        blob_id: BlobId,
194    ) -> Result<BlobContent, NodeError> {
195        Ok(match self {
196            Client::Grpc(grpc_client) => {
197                grpc_client.download_pending_blob(chain_id, blob_id).await?
198            }
199
200            #[cfg(with_simple_network)]
201            Client::Simple(simple_client) => {
202                simple_client
203                    .download_pending_blob(chain_id, blob_id)
204                    .await?
205            }
206        })
207    }
208
209    async fn handle_pending_blob(
210        &self,
211        chain_id: ChainId,
212        blob: BlobContent,
213    ) -> Result<ChainInfoResponse, NodeError> {
214        Ok(match self {
215            Client::Grpc(grpc_client) => grpc_client.handle_pending_blob(chain_id, blob).await?,
216
217            #[cfg(with_simple_network)]
218            Client::Simple(simple_client) => {
219                simple_client.handle_pending_blob(chain_id, blob).await?
220            }
221        })
222    }
223
224    async fn download_certificate(
225        &self,
226        hash: CryptoHash,
227    ) -> Result<ConfirmedBlockCertificate, NodeError> {
228        Ok(match self {
229            Client::Grpc(grpc_client) => grpc_client.download_certificate(hash).await?,
230
231            #[cfg(with_simple_network)]
232            Client::Simple(simple_client) => simple_client.download_certificate(hash).await?,
233        })
234    }
235
236    async fn download_certificates(
237        &self,
238        hashes: Vec<CryptoHash>,
239    ) -> Result<Vec<ConfirmedBlockCertificate>, NodeError> {
240        Ok(match self {
241            Client::Grpc(grpc_client) => grpc_client.download_certificates(hashes).await?,
242
243            #[cfg(with_simple_network)]
244            Client::Simple(simple_client) => simple_client.download_certificates(hashes).await?,
245        })
246    }
247
248    async fn blob_last_used_by(&self, blob_id: BlobId) -> Result<CryptoHash, NodeError> {
249        Ok(match self {
250            Client::Grpc(grpc_client) => grpc_client.blob_last_used_by(blob_id).await?,
251
252            #[cfg(with_simple_network)]
253            Client::Simple(simple_client) => simple_client.blob_last_used_by(blob_id).await?,
254        })
255    }
256
257    async fn missing_blob_ids(&self, blob_ids: Vec<BlobId>) -> Result<Vec<BlobId>, NodeError> {
258        Ok(match self {
259            Client::Grpc(grpc_client) => grpc_client.missing_blob_ids(blob_ids).await?,
260
261            #[cfg(with_simple_network)]
262            Client::Simple(simple_client) => simple_client.missing_blob_ids(blob_ids).await?,
263        })
264    }
265}