Skip to main content

miden_client/rpc/tonic_client/
mod.rs

1use alloc::borrow::ToOwned;
2use alloc::boxed::Box;
3use alloc::collections::{BTreeMap, BTreeSet};
4use alloc::string::{String, ToString};
5use alloc::vec::Vec;
6use core::error::Error;
7use miden_protocol::asset::{Asset, AssetVault};
8
9use miden_protocol::account::{
10    Account, AccountCode, AccountId, AccountStorage, StorageMap, StorageSlot, StorageSlotType,
11};
12use miden_protocol::address::NetworkId;
13use miden_protocol::block::account_tree::AccountWitness;
14use miden_protocol::block::{BlockHeader, BlockNumber, ProvenBlock};
15use miden_protocol::crypto::merkle::MerklePath;
16use miden_protocol::crypto::merkle::mmr::{Forest, MmrProof};
17use miden_protocol::crypto::merkle::smt::SmtProof;
18use miden_protocol::note::{NoteId, NoteScript, NoteTag, Nullifier};
19use miden_protocol::transaction::{ProvenTransaction, TransactionInputs};
20use miden_protocol::utils::Deserializable;
21use miden_protocol::{EMPTY_WORD, Word};
22use miden_tx::utils::Serializable;
23use miden_tx::utils::sync::RwLock;
24use tonic::Status;
25use tracing::info;
26
27use super::domain::account::{AccountProof, AccountStorageDetails, AccountUpdateSummary};
28use super::domain::{note::FetchedNote, nullifier::NullifierUpdate};
29use super::generated::rpc::account_request::AccountDetailRequest;
30use super::generated::rpc::AccountRequest;
31use super::{
32    Endpoint, FetchedAccount, NodeRpcClient, NodeRpcClientEndpoint, NoteSyncInfo, RpcError,
33    StateSyncInfo,
34};
35use crate::rpc::domain::account_vault::{AccountVaultInfo, AccountVaultUpdate};
36use crate::rpc::domain::storage_map::{StorageMapInfo, StorageMapUpdate};
37use crate::rpc::domain::transaction::TransactionsInfo;
38use crate::rpc::errors::{AcceptHeaderError, GrpcError, RpcConversionError};
39use crate::rpc::generated::rpc::account_request::account_detail_request::storage_map_detail_request::SlotData;
40use crate::rpc::generated::rpc::account_request::account_detail_request::StorageMapDetailRequest;
41use crate::rpc::generated::rpc::BlockRange;
42use crate::rpc::{AccountStateAt, NOTE_IDS_LIMIT, NULLIFIER_PREFIXES_LIMIT, generated as proto};
43use crate::transaction::ForeignAccount;
44
45mod api_client;
46use api_client::api_client_wrapper::ApiClient;
47
48// GRPC CLIENT
49// ================================================================================================
50
51/// Client for the Node RPC API using gRPC.
52///
53/// If the `tonic` feature is enabled, this client will use a `tonic::transport::Channel` to
54/// communicate with the node. In this case the connection will be established lazily when the
55/// first request is made.
56/// If the `web-tonic` feature is enabled, this client will use a `tonic_web_wasm_client::Client`
57/// to communicate with the node.
58///
59/// In both cases, the [`GrpcClient`] depends on the types inside the `generated` module, which
60/// are generated by the build script and also depend on the target architecture.
61pub struct GrpcClient {
62    client: RwLock<Option<ApiClient>>,
63    endpoint: String,
64    timeout_ms: u64,
65    genesis_commitment: RwLock<Option<Word>>,
66}
67
68impl GrpcClient {
69    /// Returns a new instance of [`GrpcClient`] that'll do calls to the provided [`Endpoint`]
70    /// with the given timeout in milliseconds.
71    pub fn new(endpoint: &Endpoint, timeout_ms: u64) -> GrpcClient {
72        GrpcClient {
73            client: RwLock::new(None),
74            endpoint: endpoint.to_string(),
75            timeout_ms,
76            genesis_commitment: RwLock::new(None),
77        }
78    }
79
80    /// Takes care of establishing the RPC connection if not connected yet. It ensures that the
81    /// `rpc_api` field is initialized and returns a write guard to it.
82    async fn ensure_connected(&self) -> Result<ApiClient, RpcError> {
83        if self.client.read().is_none() {
84            self.connect().await?;
85        }
86
87        Ok(self.client.read().as_ref().expect("rpc_api should be initialized").clone())
88    }
89
90    /// Connects to the Miden node, setting the client API with the provided URL, timeout and
91    /// genesis commitment.
92    async fn connect(&self) -> Result<(), RpcError> {
93        let genesis_commitment = *self.genesis_commitment.read();
94        let new_client =
95            ApiClient::new_client(self.endpoint.clone(), self.timeout_ms, genesis_commitment)
96                .await?;
97        let mut client = self.client.write();
98        client.replace(new_client);
99
100        Ok(())
101    }
102
103    // GET ACCOUNT HELPERS
104    // ============================================================================================
105
106    /// Given an [`AccountId`], return the proof for the account.
107    ///
108    /// If the account also has public state, its details will also be retrieved
109    pub async fn fetch_full_account_proof(
110        &self,
111        account_id: AccountId,
112    ) -> Result<(BlockNumber, AccountProof), RpcError> {
113        let mut rpc_api = self.ensure_connected().await?;
114        let has_public_state = account_id.has_public_state();
115        let account_request = {
116            AccountRequest {
117                account_id: Some(account_id.into()),
118                block_num: None,
119                details: {
120                    if has_public_state {
121                        // Since we have to request the storage maps for an account
122                        // we *dont know* anything about, we'll have to do first this
123                        // request, which will tell us about the account's storage slots,
124                        // and then, request the slots in another request.
125                        Some(AccountDetailRequest {
126                            code_commitment: Some(EMPTY_WORD.into()),
127                            asset_vault_commitment: Some(EMPTY_WORD.into()),
128                            storage_maps: vec![],
129                        })
130                    } else {
131                        None
132                    }
133                },
134            }
135        };
136        let account_response = rpc_api
137            .get_account(account_request)
138            .await
139            .map_err(|status| RpcError::from_grpc_error(NodeRpcClientEndpoint::GetAccount, status))?
140            .into_inner();
141        let block_number = account_response.block_num.ok_or(RpcError::ExpectedDataMissing(
142            "GetAccountDetails returned an account without a matching block number for the witness"
143                .to_owned(),
144        ))?;
145        let account_proof = {
146            if has_public_state {
147                let account_details = account_response
148                    .details
149                    .ok_or(RpcError::ExpectedDataMissing("details in public account".to_owned()))?
150                    .into_domain(&BTreeMap::new())?;
151                let storage_header = account_details.storage_details.header;
152                // This variable will hold the storage slots that are maps, below we will use it to
153                // actually fetch the storage maps details, since we now know the names of each
154                // storage slot.
155                let maps_to_request = storage_header
156                    .slots()
157                    .filter(|header| header.slot_type().is_map())
158                    .map(|map| map.name().to_string());
159                let account_request = AccountRequest {
160                    account_id: Some(account_id.into()),
161                    block_num: None,
162                    details: Some(AccountDetailRequest {
163                        code_commitment: Some(EMPTY_WORD.into()),
164                        asset_vault_commitment: Some(EMPTY_WORD.into()),
165                        storage_maps: maps_to_request
166                            .map(|slot_name| StorageMapDetailRequest {
167                                slot_name,
168                                slot_data: Some(SlotData::AllEntries(true)),
169                            })
170                            .collect(),
171                    }),
172                };
173                match rpc_api.get_account(account_request).await {
174                    Ok(account_proof) => account_proof.into_inner().try_into(),
175                    Err(err) => Err(RpcError::ConnectionError(
176                        format!(
177                            "failed to fetch account proof for account: {account_id}, got: {err}"
178                        )
179                        .into(),
180                    )),
181                }
182            } else {
183                account_response.try_into()
184            }
185        };
186        Ok((block_number.block_num.into(), account_proof?))
187    }
188
189    /// Given the storage details for an account and its id, returns a vector with all of its
190    /// storage slots. Keep in mind that if an account triggers the `too_many_entries` flag, there
191    /// will potentially be multiple requests.
192    async fn build_storage_slots(
193        &self,
194        account_id: AccountId,
195        storage_details: &AccountStorageDetails,
196    ) -> Result<Vec<StorageSlot>, RpcError> {
197        let mut slots = vec![];
198        // `SyncStorageMaps` will return information for *every* map for a given account, so this
199        // map_cache value should be fetched only once, hence the None placeholder
200        let mut map_cache: Option<StorageMapInfo> = None;
201        for slot_header in storage_details.header.slots() {
202            // We have two cases for each slot:
203            // - Slot is a value => We simply instance a StorageSlot
204            // - Slot is a map => If the map is 'small', we can simply
205            // build the map from the given entries. Otherwise we will have to
206            // call the SyncStorageMaps RPC method to obtain the data for the map.
207            // With the current setup, one RPC call should be enough.
208            match slot_header.slot_type() {
209                StorageSlotType::Value => {
210                    slots.push(miden_protocol::account::StorageSlot::with_value(
211                        slot_header.name().clone(),
212                        slot_header.value(),
213                    ));
214                },
215                StorageSlotType::Map => {
216                    let map_details = storage_details.find_map_details(slot_header.name()).ok_or(
217                        RpcError::ExpectedDataMissing(format!(
218                            "slot named '{}' was reported as a map, but it does not have a matching map_detail entry",
219                            slot_header.name(),
220                        )),
221                    )?;
222
223                    let storage_map = if map_details.too_many_entries {
224                        let map_info = if let Some(ref info) = map_cache {
225                            info
226                        } else {
227                            let fetched_data =
228                                self.sync_storage_maps(0_u32.into(), None, account_id).await?;
229                            map_cache.insert(fetched_data)
230                        };
231                        let map_entries: Vec<_> = map_info
232                            .updates
233                            .iter()
234                            .filter(|slot_info| slot_info.slot_name == *slot_header.name())
235                            .map(|slot_info| (slot_info.key, slot_info.value))
236                            .collect();
237                        StorageMap::with_entries(map_entries)
238                    } else {
239                        map_details.entries.clone().into_storage_map()
240                    }
241                    .map_err(|err| {
242                        RpcError::InvalidResponse(format!(
243                            "the rpc api returned a non-valid map entry: {err}"
244                        ))
245                    })?;
246
247                    slots.push(miden_protocol::account::StorageSlot::with_map(
248                        slot_header.name().clone(),
249                        storage_map,
250                    ));
251                },
252            }
253        }
254        Ok(slots)
255    }
256}
257
258#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
259#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
260impl NodeRpcClient for GrpcClient {
261    /// Sets the genesis commitment for the client. If the client is already connected, it will be
262    /// updated to use the new commitment on subsequent requests. If the client is not connected,
263    /// the commitment will be stored and used when the client connects. If the genesis commitment
264    /// is already set, this method does nothing.
265    async fn set_genesis_commitment(&self, commitment: Word) -> Result<(), RpcError> {
266        // Check if already set before doing anything else
267        if self.genesis_commitment.read().is_some() {
268            // Genesis commitment is already set, ignoring the new value.
269            return Ok(());
270        }
271
272        // Store the commitment for future connections
273        self.genesis_commitment.write().replace(commitment);
274
275        // If a client is already connected, update it to use the new genesis commitment.
276        // If not connected, the commitment will be used when connect() is called.
277        let mut client_guard = self.client.write();
278        if let Some(client) = client_guard.as_mut() {
279            client.set_genesis_commitment(commitment);
280        }
281
282        Ok(())
283    }
284
285    async fn submit_proven_transaction(
286        &self,
287        proven_transaction: ProvenTransaction,
288        transaction_inputs: TransactionInputs,
289    ) -> Result<BlockNumber, RpcError> {
290        let request = proto::transaction::ProvenTransaction {
291            transaction: proven_transaction.to_bytes(),
292            transaction_inputs: Some(transaction_inputs.to_bytes()),
293        };
294
295        let mut rpc_api = self.ensure_connected().await?;
296
297        let api_response = rpc_api.submit_proven_transaction(request).await.map_err(|status| {
298            RpcError::from_grpc_error(NodeRpcClientEndpoint::SubmitProvenTx, status)
299        })?;
300
301        Ok(BlockNumber::from(api_response.into_inner().block_num))
302    }
303
304    async fn get_block_header_by_number(
305        &self,
306        block_num: Option<BlockNumber>,
307        include_mmr_proof: bool,
308    ) -> Result<(BlockHeader, Option<MmrProof>), RpcError> {
309        let request = proto::rpc::BlockHeaderByNumberRequest {
310            block_num: block_num.as_ref().map(BlockNumber::as_u32),
311            include_mmr_proof: Some(include_mmr_proof),
312        };
313
314        info!("Calling GetBlockHeaderByNumber: {:?}", request);
315
316        let mut rpc_api = self.ensure_connected().await?;
317
318        let api_response = rpc_api.get_block_header_by_number(request).await.map_err(|status| {
319            RpcError::from_grpc_error(NodeRpcClientEndpoint::GetBlockHeaderByNumber, status)
320        })?;
321
322        let response = api_response.into_inner();
323
324        let block_header: BlockHeader = response
325            .block_header
326            .ok_or(RpcError::ExpectedDataMissing("BlockHeader".into()))?
327            .try_into()?;
328
329        let mmr_proof = if include_mmr_proof {
330            let forest = response
331                .chain_length
332                .ok_or(RpcError::ExpectedDataMissing("ChainLength".into()))?;
333            let merkle_path: MerklePath = response
334                .mmr_path
335                .ok_or(RpcError::ExpectedDataMissing("MmrPath".into()))?
336                .try_into()?;
337
338            Some(MmrProof {
339                forest: Forest::new(usize::try_from(forest).expect("u64 should fit in usize")),
340                position: block_header.block_num().as_usize(),
341                merkle_path,
342            })
343        } else {
344            None
345        };
346
347        Ok((block_header, mmr_proof))
348    }
349
350    async fn get_notes_by_id(&self, note_ids: &[NoteId]) -> Result<Vec<FetchedNote>, RpcError> {
351        let mut notes = Vec::with_capacity(note_ids.len());
352        for chunk in note_ids.chunks(NOTE_IDS_LIMIT) {
353            let request = proto::note::NoteIdList {
354                ids: chunk.iter().map(|id| (*id).into()).collect(),
355            };
356
357            let mut rpc_api = self.ensure_connected().await?;
358
359            let api_response = rpc_api.get_notes_by_id(request).await.map_err(|status| {
360                RpcError::from_grpc_error(NodeRpcClientEndpoint::GetNotesById, status)
361            })?;
362
363            let response_notes = api_response
364                .into_inner()
365                .notes
366                .into_iter()
367                .map(FetchedNote::try_from)
368                .collect::<Result<Vec<FetchedNote>, RpcConversionError>>()?;
369
370            notes.extend(response_notes);
371        }
372        Ok(notes)
373    }
374
375    /// Sends a sync state request to the Miden node, validates and converts the response
376    /// into a [`StateSyncInfo`] struct.
377    async fn sync_state(
378        &self,
379        block_num: BlockNumber,
380        account_ids: &[AccountId],
381        note_tags: &BTreeSet<NoteTag>,
382    ) -> Result<StateSyncInfo, RpcError> {
383        let account_ids = account_ids.iter().map(|acc| (*acc).into()).collect();
384
385        let note_tags = note_tags.iter().map(|&note_tag| note_tag.into()).collect();
386
387        let request = proto::rpc::SyncStateRequest {
388            block_num: block_num.as_u32(),
389            account_ids,
390            note_tags,
391        };
392
393        let mut rpc_api = self.ensure_connected().await?;
394
395        let response = rpc_api.sync_state(request).await.map_err(|status| {
396            RpcError::from_grpc_error(NodeRpcClientEndpoint::SyncState, status)
397        })?;
398        response.into_inner().try_into()
399    }
400
401    /// Sends a `GetAccountDetailsRequest` to the Miden node, and extracts an [`FetchedAccount`]
402    /// from the `GetAccountDetailsResponse` response.
403    ///
404    /// # Errors
405    ///
406    /// This function will return an error if:
407    ///
408    /// - There was an error sending the request to the node.
409    /// - The answer had a `None` for one of the expected fields (`account`, `summary`,
410    ///   `account_commitment`, `details`).
411    /// - There is an error during [Account] deserialization.
412    async fn get_account_details(&self, account_id: AccountId) -> Result<FetchedAccount, RpcError> {
413        let (block_number, full_account_proof) = self.fetch_full_account_proof(account_id).await?;
414        let update_summary =
415            AccountUpdateSummary::new(full_account_proof.account_commitment(), block_number);
416
417        // The case for a private account is simple,
418        // we simple use the commitment and its id.
419        if account_id.is_private() {
420            Ok(FetchedAccount::new_private(account_id, update_summary))
421        } else {
422            // An account with public state has to fetch all of its state.
423            // Even more so, an account with a large state will have to do
424            // a couple of extra requests to fetch all of its data.
425            let details =
426                full_account_proof.into_parts().1.ok_or(RpcError::ExpectedDataMissing(
427                    "GetAccountDetails returned a public account without details".to_owned(),
428                ))?;
429            let account_id = details.header.id();
430            let nonce = details.header.nonce();
431            let assets: Vec<Asset> = {
432                if details.vault_details.too_many_assets {
433                    self.sync_account_vault(BlockNumber::from(0), None, account_id)
434                        .await?
435                        .updates
436                        .into_iter()
437                        .filter_map(|update| update.asset)
438                        .collect()
439                } else {
440                    details.vault_details.assets
441                }
442            };
443
444            let slots = self.build_storage_slots(account_id, &details.storage_details).await?;
445            let seed = None;
446            let asset_vault = AssetVault::new(&assets).map_err(|err| {
447                RpcError::InvalidResponse(format!("api rpc returned non-valid assets: {err}"))
448            })?;
449            let account_storage = AccountStorage::new(slots).map_err(|err| {
450                RpcError::InvalidResponse(format!(
451                    "api rpc returned non-valid storage slots: {err}"
452                ))
453            })?;
454            let account =
455                Account::new(account_id, asset_vault, account_storage, details.code, nonce, seed)
456                    .map_err(|err| {
457                    RpcError::InvalidResponse(format!(
458                        "failed to instance an account from the rpc api response: {err}"
459                    ))
460                })?;
461            Ok(FetchedAccount::new_public(account, update_summary))
462        }
463    }
464
465    /// Sends a `GetAccountProof` request to the Miden node, and extracts the [AccountProof]
466    /// from the response, as well as the block number that it was retrieved for.
467    ///
468    /// # Errors
469    ///
470    /// This function will return an error if:
471    ///
472    /// - The requested Account isn't returned by the node.
473    /// - There was an error sending the request to the node.
474    /// - The answer had a `None` for one of the expected fields.
475    /// - There is an error during storage deserialization.
476    async fn get_account(
477        &self,
478        foreign_account: ForeignAccount,
479        account_state: AccountStateAt,
480        known_account_code: Option<AccountCode>,
481    ) -> Result<(BlockNumber, AccountProof), RpcError> {
482        let mut known_codes_by_commitment: BTreeMap<Word, AccountCode> = BTreeMap::new();
483        if let Some(account_code) = known_account_code {
484            known_codes_by_commitment.insert(account_code.commitment(), account_code);
485        }
486
487        let mut rpc_api = self.ensure_connected().await?;
488
489        // Request proofs one-by-one using the singular API
490        let account_id = foreign_account.account_id();
491        let storage_requirements = foreign_account.storage_slot_requirements();
492
493        let storage_maps: Vec<StorageMapDetailRequest> = storage_requirements.clone().into();
494
495        // Only request details for public accounts; include known code commitment for this
496        // account when available
497        let account_details = if account_id.is_public() {
498            Some(AccountDetailRequest {
499                code_commitment: Some(EMPTY_WORD.into()),
500                // TODO: implement a way to request asset vaults
501                // https://github.com/0xMiden/miden-client/issues/1412
502                asset_vault_commitment: None,
503                storage_maps,
504            })
505        } else {
506            None
507        };
508
509        let block_num = match account_state {
510            AccountStateAt::Block(number) => Some(number.into()),
511            AccountStateAt::ChainTip => None,
512        };
513
514        let request = AccountRequest {
515            account_id: Some(account_id.into()),
516            block_num,
517            details: account_details,
518        };
519
520        let response = rpc_api
521            .get_account(request)
522            .await
523            .map_err(|status| RpcError::from_grpc_error(NodeRpcClientEndpoint::GetAccount, status))?
524            .into_inner();
525
526        let account_witness: AccountWitness = response
527            .witness
528            .ok_or(RpcError::ExpectedDataMissing("AccountWitness".to_string()))?
529            .try_into()?;
530
531        // For public accounts, details should be present when requested
532        let headers = if account_witness.id().is_public() {
533            Some(
534                response
535                    .details
536                    .ok_or(RpcError::ExpectedDataMissing("Account.Details".to_string()))?
537                    .into_domain(&known_codes_by_commitment)?,
538            )
539        } else {
540            None
541        };
542
543        let proof = AccountProof::new(account_witness, headers)
544            .map_err(|err| RpcError::InvalidResponse(err.to_string()))?;
545
546        let block_num = response
547            .block_num
548            .ok_or(RpcError::ExpectedDataMissing("response block num".to_string()))?
549            .block_num
550            .into();
551
552        Ok((block_num, proof))
553    }
554
555    /// Sends a `SyncNoteRequest` to the Miden node, and extracts a [`NoteSyncInfo`] from the
556    /// response.
557    async fn sync_notes(
558        &self,
559        block_num: BlockNumber,
560        block_to: Option<BlockNumber>,
561        note_tags: &BTreeSet<NoteTag>,
562    ) -> Result<NoteSyncInfo, RpcError> {
563        let note_tags = note_tags.iter().map(|&note_tag| note_tag.into()).collect();
564
565        let block_range = Some(BlockRange {
566            block_from: block_num.as_u32(),
567            block_to: block_to.map(|b| b.as_u32()),
568        });
569
570        let request = proto::rpc::SyncNotesRequest { block_range, note_tags };
571
572        let mut rpc_api = self.ensure_connected().await?;
573
574        let response = rpc_api.sync_notes(request).await.map_err(|status| {
575            RpcError::from_grpc_error(NodeRpcClientEndpoint::SyncNotes, status)
576        })?;
577
578        response.into_inner().try_into()
579    }
580
581    async fn sync_nullifiers(
582        &self,
583        prefixes: &[u16],
584        block_num: BlockNumber,
585        block_to: Option<BlockNumber>,
586    ) -> Result<Vec<NullifierUpdate>, RpcError> {
587        const MAX_ITERATIONS: u32 = 1000; // Safety limit to prevent infinite loops
588
589        let mut all_nullifiers = BTreeSet::new();
590
591        // Establish RPC connection once before the loop
592        let mut rpc_api = self.ensure_connected().await?;
593
594        // If the prefixes are too many, we need to chunk them into smaller groups to avoid
595        // violating the RPC limit.
596        'chunk_nullifiers: for chunk in prefixes.chunks(NULLIFIER_PREFIXES_LIMIT) {
597            let mut current_block_from = block_num.as_u32();
598
599            for _ in 0..MAX_ITERATIONS {
600                let request = proto::rpc::SyncNullifiersRequest {
601                    nullifiers: chunk.iter().map(|&x| u32::from(x)).collect(),
602                    prefix_len: 16,
603                    block_range: Some(BlockRange {
604                        block_from: current_block_from,
605                        block_to: block_to.map(|b| b.as_u32()),
606                    }),
607                };
608
609                let response = rpc_api.sync_nullifiers(request).await.map_err(|status| {
610                    RpcError::from_grpc_error(NodeRpcClientEndpoint::SyncNullifiers, status)
611                })?;
612                let response = response.into_inner();
613
614                // Convert nullifiers for this batch
615                let batch_nullifiers = response
616                    .nullifiers
617                    .iter()
618                    .map(TryFrom::try_from)
619                    .collect::<Result<Vec<NullifierUpdate>, _>>()
620                    .map_err(|err| RpcError::InvalidResponse(err.to_string()))?;
621
622                all_nullifiers.extend(batch_nullifiers);
623
624                // Check if we need to fetch more pages
625                if let Some(page) = response.pagination_info {
626                    // Ensure we're making progress to avoid infinite loops
627                    if page.block_num < current_block_from {
628                        return Err(RpcError::InvalidResponse(
629                            "invalid pagination: block_num went backwards".to_string(),
630                        ));
631                    }
632
633                    // Calculate target block as minimum between block_to and chain_tip
634                    let target_block =
635                        block_to.map_or(page.chain_tip, |b| b.as_u32().min(page.chain_tip));
636
637                    if page.block_num >= target_block {
638                        // No pagination info or we've reached/passed the target so we're done
639                        continue 'chunk_nullifiers;
640                    }
641                    current_block_from = page.block_num + 1;
642                }
643            }
644            // If we exit the loop, we've hit the iteration limit
645            return Err(RpcError::InvalidResponse(
646                "too many pagination iterations, possible infinite loop".to_string(),
647            ));
648        }
649        Ok(all_nullifiers.into_iter().collect::<Vec<_>>())
650    }
651
652    async fn check_nullifiers(&self, nullifiers: &[Nullifier]) -> Result<Vec<SmtProof>, RpcError> {
653        let mut proofs: Vec<SmtProof> = Vec::with_capacity(nullifiers.len());
654        for chunk in nullifiers.chunks(NULLIFIER_PREFIXES_LIMIT) {
655            let request = proto::rpc::NullifierList {
656                nullifiers: chunk.iter().map(|nul| nul.as_word().into()).collect(),
657            };
658
659            let mut rpc_api = self.ensure_connected().await?;
660
661            let response = rpc_api.check_nullifiers(request).await.map_err(|status| {
662                RpcError::from_grpc_error(NodeRpcClientEndpoint::CheckNullifiers, status)
663            })?;
664
665            let mut response = response.into_inner();
666            let chunk_proofs = response
667                .proofs
668                .iter_mut()
669                .map(|r| r.to_owned().try_into())
670                .collect::<Result<Vec<SmtProof>, RpcConversionError>>()?;
671            proofs.extend(chunk_proofs);
672        }
673        Ok(proofs)
674    }
675
676    async fn get_block_by_number(&self, block_num: BlockNumber) -> Result<ProvenBlock, RpcError> {
677        let request = proto::blockchain::BlockNumber { block_num: block_num.as_u32() };
678
679        let mut rpc_api = self.ensure_connected().await?;
680
681        let response = rpc_api.get_block_by_number(request).await.map_err(|status| {
682            RpcError::from_grpc_error(NodeRpcClientEndpoint::GetBlockByNumber, status)
683        })?;
684
685        let response = response.into_inner();
686        let block =
687            ProvenBlock::read_from_bytes(&response.block.ok_or(RpcError::ExpectedDataMissing(
688                "GetBlockByNumberResponse.block".to_string(),
689            ))?)?;
690
691        Ok(block)
692    }
693
694    async fn get_note_script_by_root(&self, root: Word) -> Result<NoteScript, RpcError> {
695        let request = proto::note::NoteRoot { root: Some(root.into()) };
696
697        let mut rpc_api = self.ensure_connected().await?;
698
699        let response = rpc_api.get_note_script_by_root(request).await.map_err(|status| {
700            RpcError::from_grpc_error(NodeRpcClientEndpoint::GetNoteScriptByRoot, status)
701        })?;
702
703        let response = response.into_inner();
704        let note_script = NoteScript::try_from(
705            response
706                .script
707                .ok_or(RpcError::ExpectedDataMissing("GetNoteScriptByRoot.script".to_string()))?,
708        )?;
709
710        Ok(note_script)
711    }
712
713    async fn sync_storage_maps(
714        &self,
715        block_from: BlockNumber,
716        block_to: Option<BlockNumber>,
717        account_id: AccountId,
718    ) -> Result<StorageMapInfo, RpcError> {
719        let mut all_updates = Vec::new();
720        let mut current_block_from = block_from.as_u32();
721        let mut target_block_reached = false;
722        let mut final_chain_tip = 0;
723        let mut final_block_num = 0;
724
725        let mut rpc_api = self.ensure_connected().await?;
726
727        while !target_block_reached {
728            let request = proto::rpc::SyncAccountStorageMapsRequest {
729                block_range: Some(BlockRange {
730                    block_from: current_block_from,
731                    block_to: block_to.map(|b| b.as_u32()),
732                }),
733                account_id: Some(account_id.into()),
734            };
735
736            let response = rpc_api.sync_account_storage_maps(request).await.map_err(|status| {
737                RpcError::from_grpc_error(NodeRpcClientEndpoint::SyncStorageMaps, status)
738            })?;
739            let response = response.into_inner();
740
741            let batch_updates = response
742                .updates
743                .into_iter()
744                .map(TryInto::try_into)
745                .collect::<Result<Vec<StorageMapUpdate>, _>>()?;
746            all_updates.extend(batch_updates);
747
748            let page = response
749                .pagination_info
750                .ok_or(RpcError::ExpectedDataMissing("pagination_info".to_owned()))?;
751
752            if page.block_num < current_block_from {
753                return Err(RpcError::InvalidResponse(
754                    "invalid pagination: block_num went backwards".to_owned(),
755                ));
756            }
757
758            final_chain_tip = page.chain_tip;
759            final_block_num = page.block_num;
760
761            let target_block = block_to.map_or(page.chain_tip, |b| b.as_u32().min(page.chain_tip));
762
763            target_block_reached = page.block_num >= target_block;
764            current_block_from = page.block_num + 1;
765        }
766
767        Ok(StorageMapInfo {
768            chain_tip: final_chain_tip.into(),
769            block_number: final_block_num.into(),
770            updates: all_updates,
771        })
772    }
773
774    async fn sync_account_vault(
775        &self,
776        block_from: BlockNumber,
777        block_to: Option<BlockNumber>,
778        account_id: AccountId,
779    ) -> Result<AccountVaultInfo, RpcError> {
780        let mut all_updates = Vec::new();
781        let mut current_block_from = block_from.as_u32();
782        let mut target_block_reached = false;
783        let mut final_chain_tip = 0;
784        let mut final_block_num = 0;
785
786        let mut rpc_api = self.ensure_connected().await?;
787
788        while !target_block_reached {
789            let request = proto::rpc::SyncAccountVaultRequest {
790                block_range: Some(BlockRange {
791                    block_from: current_block_from,
792                    block_to: block_to.map(|b| b.as_u32()),
793                }),
794                account_id: Some(account_id.into()),
795            };
796
797            let response = rpc_api
798                .sync_account_vault(request)
799                .await
800                .map_err(|status| {
801                    RpcError::from_grpc_error(NodeRpcClientEndpoint::SyncAccountVault, status)
802                })?
803                .into_inner();
804
805            let batch_updates = response
806                .updates
807                .iter()
808                .map(|u| (*u).try_into())
809                .collect::<Result<Vec<AccountVaultUpdate>, _>>()?;
810            all_updates.extend(batch_updates);
811
812            let page = response
813                .pagination_info
814                .ok_or(RpcError::ExpectedDataMissing("pagination_info".to_owned()))?;
815
816            if page.block_num < current_block_from {
817                return Err(RpcError::InvalidResponse(
818                    "invalid pagination: block_num went backwards".to_owned(),
819                ));
820            }
821
822            final_chain_tip = page.chain_tip;
823            final_block_num = page.block_num;
824
825            let target_block = block_to.map_or(page.chain_tip, |b| b.as_u32().min(page.chain_tip));
826
827            target_block_reached = page.block_num >= target_block;
828            current_block_from = page.block_num + 1;
829        }
830
831        Ok(AccountVaultInfo {
832            chain_tip: final_chain_tip.into(),
833            block_number: final_block_num.into(),
834            updates: all_updates,
835        })
836    }
837
838    async fn sync_transactions(
839        &self,
840        block_from: BlockNumber,
841        block_to: Option<BlockNumber>,
842        account_ids: Vec<AccountId>,
843    ) -> Result<TransactionsInfo, RpcError> {
844        let block_range = Some(BlockRange {
845            block_from: block_from.as_u32(),
846            block_to: block_to.map(|b| b.as_u32()),
847        });
848
849        let account_ids = account_ids.iter().map(|acc_id| (*acc_id).into()).collect();
850
851        let request = proto::rpc::SyncTransactionsRequest { block_range, account_ids };
852
853        let mut rpc_api = self.ensure_connected().await?;
854
855        let response = rpc_api.sync_transactions(request).await.map_err(|status| {
856            RpcError::from_grpc_error(NodeRpcClientEndpoint::SyncTransactions, status)
857        })?;
858
859        response.into_inner().try_into()
860    }
861
862    async fn get_network_id(&self) -> Result<NetworkId, RpcError> {
863        let endpoint_str: &str = &self.endpoint.clone();
864        let endpoint: Endpoint =
865            Endpoint::try_from(endpoint_str).map_err(RpcError::InvalidNodeEndpoint)?;
866        Ok(endpoint.to_network_id())
867    }
868}
869
870// ERRORS
871// ================================================================================================
872
873impl RpcError {
874    pub fn from_grpc_error(endpoint: NodeRpcClientEndpoint, status: Status) -> Self {
875        if let Some(accept_error) = AcceptHeaderError::try_from_message(status.message()) {
876            return Self::AcceptHeaderError(accept_error);
877        }
878
879        let error_kind = GrpcError::from(&status);
880        let source = Box::new(status) as Box<dyn Error + Send + Sync + 'static>;
881
882        Self::GrpcError {
883            endpoint,
884            error_kind,
885            source: Some(source),
886        }
887    }
888}
889
890impl From<&Status> for GrpcError {
891    fn from(status: &Status) -> Self {
892        GrpcError::from_code(status.code() as i32, Some(status.message().to_string()))
893    }
894}
895
896#[cfg(test)]
897mod tests {
898    use std::boxed::Box;
899
900    use miden_protocol::Word;
901
902    use super::GrpcClient;
903    use crate::rpc::{Endpoint, NodeRpcClient};
904
905    fn assert_send_sync<T: Send + Sync>() {}
906
907    #[test]
908    fn is_send_sync() {
909        assert_send_sync::<GrpcClient>();
910        assert_send_sync::<Box<dyn NodeRpcClient>>();
911    }
912
913    // Function that returns a `Send` future from a dynamic trait that must be `Sync`.
914    async fn dyn_trait_send_fut(client: Box<dyn NodeRpcClient>) {
915        // This won't compile if `get_block_header_by_number` doesn't return a `Send+Sync` future.
916        let res = client.get_block_header_by_number(None, false).await;
917        assert!(res.is_ok());
918    }
919
920    #[tokio::test]
921    async fn future_is_send() {
922        let endpoint = &Endpoint::devnet();
923        let client = GrpcClient::new(endpoint, 10000);
924        let client: Box<GrpcClient> = client.into();
925        tokio::task::spawn(async move { dyn_trait_send_fut(client).await });
926    }
927
928    #[tokio::test]
929    async fn set_genesis_commitment_sets_the_commitment_when_its_not_already_set() {
930        let endpoint = &Endpoint::devnet();
931        let client = GrpcClient::new(endpoint, 10000);
932
933        assert!(client.genesis_commitment.read().is_none());
934
935        let commitment = Word::default();
936        client.set_genesis_commitment(commitment).await.unwrap();
937
938        assert_eq!(client.genesis_commitment.read().unwrap(), commitment);
939    }
940
941    #[tokio::test]
942    async fn set_genesis_commitment_does_nothing_if_the_commitment_is_already_set() {
943        use miden_protocol::Felt;
944
945        let endpoint = &Endpoint::devnet();
946        let client = GrpcClient::new(endpoint, 10000);
947
948        let initial_commitment = Word::default();
949        client.set_genesis_commitment(initial_commitment).await.unwrap();
950
951        let new_commitment = Word::from([Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)]);
952        client.set_genesis_commitment(new_commitment).await.unwrap();
953
954        assert_eq!(client.genesis_commitment.read().unwrap(), initial_commitment);
955    }
956
957    #[tokio::test]
958    async fn set_genesis_commitment_updates_the_client_if_already_connected() {
959        let endpoint = &Endpoint::devnet();
960        let client = GrpcClient::new(endpoint, 10000);
961
962        // "Connect" the client
963        client.connect().await.unwrap();
964
965        let commitment = Word::default();
966        client.set_genesis_commitment(commitment).await.unwrap();
967
968        assert_eq!(client.genesis_commitment.read().unwrap(), commitment);
969        assert!(client.client.read().as_ref().is_some());
970    }
971}