miden_client/rpc/mod.rs
1//! Provides an interface for the client to communicate with a Miden node using Remote Procedure
2//! Calls (RPC).
3//!
4//! This module defines the [`NodeRpcClient`] trait which abstracts calls to the RPC protocol used
5//! to:
6//!
7//! - Submit proven transactions.
8//! - Submit proven batches.
9//! - Retrieve block headers (optionally with MMR proofs).
10//! - Sync state updates (including notes, nullifiers, and account updates).
11//! - Fetch details for specific notes and accounts.
12//!
13//! The client implementation adapts to the target environment automatically:
14//! - Native targets use `tonic` transport with TLS.
15//! - `wasm32` targets use `tonic-web-wasm-client` transport.
16//!
17//! ## Example
18//!
19//! ```no_run
20//! # use miden_client::rpc::{Endpoint, NodeRpcClient, GrpcClient, VerifyingRpcClient};
21//! # use miden_protocol::block::BlockNumber;
22//! # #[tokio::main]
23//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
24//! // Create a gRPC client instance (assumes default endpoint configuration), wrapped so that
25//! // node responses are verified against the requests.
26//! let endpoint = Endpoint::new("https".into(), "localhost".into(), Some(57291));
27//! let rpc_client = VerifyingRpcClient::new(GrpcClient::new(&endpoint, 1000));
28//!
29//! // Fetch the latest block header (by passing None).
30//! let (block_header, mmr_proof) = rpc_client.get_block_header_by_number(None, true).await?;
31//!
32//! println!("Latest block number: {}", block_header.block_num());
33//! if let Some(proof) = mmr_proof {
34//! println!("MMR proof received accordingly");
35//! }
36//!
37//! # Ok(())
38//! # }
39//! ```
40//! The client also makes use of this component in order to communicate with the node.
41//!
42//! For further details and examples, see the documentation for the individual methods in the
43//! [`NodeRpcClient`] trait.
44
45use alloc::boxed::Box;
46use alloc::collections::{BTreeMap, BTreeSet};
47use alloc::string::String;
48use alloc::vec::Vec;
49use core::fmt;
50
51use domain::account::{
52 AccountDetails,
53 AccountProof,
54 AccountStorageMapDetails,
55 GetAccountRequest,
56 StorageMapEntries,
57 StorageMapEntry,
58 StorageMapFetch,
59 VaultFetch,
60};
61use domain::note::{FetchedNote, ResolvedSyncNotesBlock, SyncNotesBlock, SyncedNote};
62use domain::nullifier::NullifierUpdate;
63use domain::sync::{ChainMmrInfo, SyncTarget};
64use encryption::{AttestedTransactionEncryptionKey, SealedTransactionInputs};
65use miden_protocol::Word;
66use miden_protocol::account::{Account, AccountId};
67use miden_protocol::address::NetworkId;
68use miden_protocol::batch::{ProposedBatch, ProvenBatch};
69use miden_protocol::block::{BlockHeader, BlockNumber, SignedBlock};
70use miden_protocol::crypto::merkle::mmr::MmrProof;
71use miden_protocol::note::{
72 NoteAttachments,
73 NoteDetails,
74 NoteId,
75 NoteScript,
76 NoteTag,
77 NoteType,
78 Nullifier,
79};
80use miden_protocol::transaction::ProvenTransaction;
81use miden_protocol::vm::ExecutionProof;
82
83use crate::rpc::domain::storage_map::StorageMapInfo;
84
85/// Contains domain types related to RPC requests and responses, as well as utility functions for
86/// dealing with them.
87pub mod domain;
88pub mod encryption;
89
90mod errors;
91pub use errors::*;
92
93mod endpoint;
94pub(crate) use domain::limits::RPC_LIMITS_STORE_SETTING;
95pub use domain::limits::RpcLimits;
96pub use domain::status::{NetworkNoteStatus, NetworkNoteStatusInfo, RpcStatusInfo};
97pub use endpoint::Endpoint;
98
99#[cfg(not(feature = "testing"))]
100mod generated;
101#[cfg(feature = "testing")]
102pub mod generated;
103
104#[cfg(feature = "tonic")]
105mod tonic_client;
106#[cfg(feature = "tonic")]
107pub use tonic_client::GrpcClient;
108
109mod verifying_client;
110pub use verifying_client::VerifyingRpcClient;
111
112use crate::rpc::domain::account_vault::AccountVaultInfo;
113use crate::rpc::domain::transaction::TransactionRecord;
114use crate::store::InputNoteRecord;
115use crate::store::input_note_states::UnverifiedNoteState;
116
117/// Represents the state that we want to retrieve from the network
118#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
119pub enum AccountStateAt {
120 /// Gets the latest state, for the current chain tip
121 #[default]
122 ChainTip,
123 /// Gets the state at a specific block number
124 Block(BlockNumber),
125}
126
127// NODE RPC CLIENT TRAIT
128// ================================================================================================
129
130/// Defines the interface for communicating with the Miden node.
131///
132/// The implementers are responsible for connecting to the Miden node, handling endpoint
133/// requests/responses, and translating responses into domain objects relevant for each of the
134/// endpoints. Implementations do not check that responses correspond to the method's arguments.
135/// Wrap a client in [`VerifyingRpcClient`] to reject mismatched responses.
136#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
137#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
138pub trait NodeRpcClient: Send + Sync {
139 /// Sets the genesis commitment for the client and reconnects to the node providing the genesis
140 /// commitment in the request headers. If the genesis commitment is already set, this method
141 /// does nothing.
142 async fn set_genesis_commitment(&self, commitment: Word) -> Result<(), RpcError>;
143
144 /// Returns the genesis commitment if it has been set, without fetching from the node.
145 fn has_genesis_commitment(&self) -> Option<Word>;
146
147 /// Fetches the validator set's transaction encryption key using the
148 /// `/GetTransactionEncryptionKey` endpoint.
149 ///
150 /// The key arrives attested but untrusted: this endpoint is served by the RPC operator, so the
151 /// response must be passed through [`AttestedTransactionEncryptionKey::verify`] before it is
152 /// used to seal anything.
153 async fn get_transaction_encryption_key(
154 &self,
155 ) -> Result<AttestedTransactionEncryptionKey, RpcError>;
156
157 /// Given a Proven Transaction, send it to the node for it to be included in a future block
158 /// using the `/SubmitProvenTransaction` RPC endpoint.
159 ///
160 /// The transaction inputs are passed already sealed, since sealing needs the client's RNG for
161 /// the scheme's ephemeral key material. See [`encryption`] for how they are produced.
162 ///
163 /// Returns the node's chain tip at submission (not the block the transaction is committed in).
164 async fn submit_proven_transaction(
165 &self,
166 proven_transaction: ProvenTransaction,
167 sealed_transaction_inputs: SealedTransactionInputs,
168 ) -> Result<BlockNumber, RpcError>;
169
170 /// Given a Proven Batch together with the corresponding [`ProposedBatch`] and the list of
171 /// [`SealedTransactionInputs`] (one per transaction, matching the ordering of the batch), sends
172 /// the batch to the node for inclusion in a future block using the `/SubmitProvenBatch` RPC
173 /// endpoint. All transactions in the batch must build on the current mempool state following
174 /// normal transaction submission rules.
175 ///
176 /// Each transaction's inputs are sealed independently against its own transaction ID, because
177 /// the node fans the batch out into one validator submission per transaction. See
178 /// [`encryption`] for how the sealed inputs are produced.
179 ///
180 /// Returns the node's chain tip at submission (not the block the batch is committed in).
181 async fn submit_proven_batch(
182 &self,
183 proven_batch: ProvenBatch,
184 proposed_batch: ProposedBatch,
185 transaction_inputs: Vec<SealedTransactionInputs>,
186 ) -> Result<BlockNumber, RpcError>;
187
188 /// Given a block number, fetches the block header corresponding to that height from the node
189 /// using the `/GetBlockHeaderByNumber` endpoint. If `include_mmr_proof` is set to true and the
190 /// function returns an `Ok`, the second value of the return tuple should always be
191 /// Some(MmrProof).
192 ///
193 /// When `None` is provided, returns info regarding the latest block.
194 ///
195 /// The returned header is not verified against the requested `block_num`;
196 /// [`VerifyingRpcClient`] performs that check.
197 async fn get_block_header_by_number(
198 &self,
199 block_num: Option<BlockNumber>,
200 include_mmr_proof: bool,
201 ) -> Result<(BlockHeader, Option<MmrProof>), RpcError>;
202
203 /// Given a block number, fetches the block corresponding to that height from the node using the
204 /// `/GetBlockByNumber` RPC endpoint.
205 ///
206 /// The node returns the block and its proof in separate fields, so the proof is returned
207 /// alongside the signed block. It is [`None`] when `include_proof` is false, and also when the
208 /// node has not proven the block yet.
209 ///
210 /// The returned block is not verified against the requested `block_num`; [`VerifyingRpcClient`]
211 /// performs that check.
212 async fn get_block_by_number(
213 &self,
214 block_num: BlockNumber,
215 include_proof: bool,
216 ) -> Result<(SignedBlock, Option<ExecutionProof>), RpcError>;
217
218 /// Fetches note-related data for a list of [`NoteId`] using the `/GetNotesById` RPC endpoint.
219 ///
220 /// For [`miden_protocol::note::NoteType::Private`] notes, the response includes only the
221 /// [`miden_protocol::note::NoteMetadata`].
222 ///
223 /// For [`miden_protocol::note::NoteType::Public`] notes, the response includes all note details
224 /// (recipient, assets, script, etc.).
225 ///
226 /// In both cases, a [`miden_protocol::note::NoteInclusionProof`] is returned so the caller can
227 /// verify that each note is part of the block's note tree.
228 ///
229 /// Returned notes are not verified to be among the requested `note_ids`; [`VerifyingRpcClient`]
230 /// performs that check.
231 async fn get_notes_by_id(&self, note_ids: &[NoteId]) -> Result<Vec<FetchedNote>, RpcError>;
232
233 /// Fetches the MMR delta for a given block range using the `/SyncChainMmr` RPC endpoint.
234 ///
235 /// - `current_block_height` is the last block number already present in the caller's MMR.
236 /// - `upper_bound` determines the upper bound of the sync range. Can be a specific block number
237 /// (`BlockNumber`), or a chain tip finality level: `CommittedChainTip` syncs up to the latest
238 /// committed block (the chain tip), while `ProvenChainTip` syncs up to the latest proven
239 /// block which may be behind the committed tip.
240 async fn sync_chain_mmr(
241 &self,
242 current_block_height: BlockNumber,
243 upper_bound: SyncTarget,
244 ) -> Result<ChainMmrInfo, RpcError>;
245
246 /// Fetches the full state of a public account from the node using the `/GetAccount` endpoint,
247 /// and then resolves oversized vault and storage map entries via the `SyncVault` and
248 /// `SyncStorageMap` endpoints when needed.
249 ///
250 /// - `account_id` is the ID of the wanted account.
251 ///
252 /// Returns `Ok(None)` for accounts without public state.
253 async fn get_account_details(
254 &self,
255 account_id: AccountId,
256 ) -> Result<Option<Account>, RpcError> {
257 // Accounts without public state have no full state to fetch; only a commitment is on-chain.
258 if !account_id.is_public() {
259 return Ok(None);
260 }
261
262 // A single request fetches the full public state: every storage map's entries plus the
263 // vault, with the storage layout discovered server-side.
264 let (block_number, mut proof) = self
265 .get_account(
266 account_id,
267 GetAccountRequest::new()
268 .with_storage(StorageMapFetch::All)
269 .with_vault(VaultFetch::Always),
270 )
271 .await?;
272
273 if let Some(details) = proof.details_mut() {
274 self.resolve_oversize_vault(account_id, block_number, details).await?;
275 self.resolve_oversize_storage_maps(account_id, block_number, details).await?;
276 }
277
278 let details = proof.into_details().ok_or(RpcError::ExpectedDataMissing(
279 "public account returned without details".into(),
280 ))?;
281
282 Ok(Some(Account::try_from(&details)?))
283 }
284
285 /// Fetches notes related to the specified tags using the `/SyncNotes` RPC endpoint, paginating
286 /// over the full block range and returning, in block-number order, every block in that range
287 /// that contains at least one note matching the requested tags.
288 ///
289 /// - `block_from`: The starting block number for the range (inclusive).
290 /// - `block_to`: The ending block number for the range (inclusive).
291 /// - `note_tags` is the set of tags used to filter the notes the client is interested in.
292 ///
293 /// Every returned note carries its full metadata, and its attachment content when the response
294 /// carried it. Use [`NodeRpcClient::sync_notes_with_content`] to resolve the rest.
295 ///
296 /// Returned notes are not verified to carry one of the requested `note_tags`;
297 /// [`VerifyingRpcClient`] performs that check.
298 async fn sync_notes(
299 &self,
300 block_from: BlockNumber,
301 block_to: BlockNumber,
302 note_tags: &BTreeSet<NoteTag>,
303 ) -> Result<Vec<SyncNotesBlock>, RpcError>;
304
305 /// Calls [`NodeRpcClient::sync_notes`] for the requested range, then makes a single
306 /// [`NodeRpcClient::get_notes_by_id`] call to resolve the note content the sync response did
307 /// not already carry, according to `fetch`, folding it into each note.
308 ///
309 /// A note whose attachments the sync response already carried needs no request: only notes
310 /// reporting `needs_attachment_fetch` have theirs fetched.
311 ///
312 /// With [`NoteContentFetch::PublicDetailsAndAttachments`], all public notes in the range are
313 /// additionally fetched so the request does not reveal the client's interest set. Narrowing it
314 /// reveals nothing either, since the omissions follow the node's own sync records.
315 ///
316 /// Returns one [`ResolvedSyncNotesBlock`] per matching block, each note carrying its inclusion
317 /// data alongside its content.
318 ///
319 /// A note whose resolved content contradicts its sync record is dropped with a warning rather
320 /// than failing the call, since anyone can commit a note whose content they never publish.
321 async fn sync_notes_with_content(
322 &self,
323 block_from: BlockNumber,
324 block_to: BlockNumber,
325 note_tags: &BTreeSet<NoteTag>,
326 fetch: NoteContentFetch,
327 ) -> Result<Vec<ResolvedSyncNotesBlock>, RpcError> {
328 let blocks = self.sync_notes(block_from, block_to, note_tags).await?;
329 let note_ids: Vec<NoteId> = blocks
330 .iter()
331 .flat_map(|block| block.notes.values())
332 .filter(|note| match fetch {
333 NoteContentFetch::PublicDetailsAndAttachments => {
334 note.note_type() == NoteType::Public || note.needs_attachment_fetch()
335 },
336 NoteContentFetch::AttachmentsOnly => note.needs_attachment_fetch(),
337 })
338 .map(|note| *note.note_id())
339 .collect();
340
341 let mut fetched_content: BTreeMap<NoteId, (Option<NoteDetails>, Option<NoteAttachments>)> =
342 BTreeMap::new();
343 if !note_ids.is_empty() {
344 for fetched_note in self.get_notes_by_id(¬e_ids).await? {
345 let (note_id, details, attachments) = match fetched_note {
346 FetchedNote::Public(note, _) => {
347 let note_id = note.id();
348 let (assets, _, recipient, attachments) = note.into_parts();
349 (note_id, Some(NoteDetails::new(assets, recipient)), attachments)
350 },
351 FetchedNote::Private(note_id, _, attachments, _) => {
352 (note_id, None, attachments)
353 },
354 };
355
356 // An empty set carries nothing, so it is recorded as absent rather than as content:
357 // keeping it would shadow the attachments the note's own sync record may already
358 // have carried, for a public note as much as for a private one.
359 let attachments = (!attachments.is_empty()).then_some(attachments);
360 fetched_content.insert(note_id, (details, attachments));
361 }
362 }
363
364 // Fold the resolved content into each note, keeping the per-block grouping so the inclusion
365 // data (header + MMR path) is carried once per block. `SyncedNote::new` rejects content
366 // that is inconsistent with its sync record (mismatched or missing attachment content);
367 // such notes are dropped rather than failing the sync, since a tracked record is never
368 // stored incomplete this way (it stays expected and can be retried by re-importing), while
369 // a hard error would wedge every sync scanning this block range.
370 let mut synced_blocks = Vec::with_capacity(blocks.len());
371 for block in blocks {
372 let mut notes = BTreeMap::new();
373 for (note_id, committed) in block.notes {
374 // Fetched attachments win when the response actually carried some: a public note's
375 // attachments are bound to the requested id, which `VerifyingRpcClient` checks. The
376 // sync record is the fallback, and a note reporting neither has none.
377 let (details, fetched_attachments) =
378 fetched_content.remove(¬e_id).unwrap_or_default();
379 let attachments = fetched_attachments
380 .or_else(|| committed.attachments().cloned())
381 .unwrap_or_else(NoteAttachments::empty);
382
383 match SyncedNote::new(committed, details, attachments) {
384 Ok(synced_note) => {
385 notes.insert(note_id, synced_note);
386 },
387 Err(err) => {
388 tracing::warn!(%note_id, %err, "skipping synced note with unusable content");
389 },
390 }
391 }
392 synced_blocks.push(ResolvedSyncNotesBlock {
393 block_header: block.block_header,
394 mmr_path: block.mmr_path,
395 notes,
396 });
397 }
398
399 Ok(synced_blocks)
400 }
401
402 /// Fetches the nullifiers corresponding to a list of prefixes using the `/SyncNullifiers` RPC
403 /// endpoint.
404 ///
405 /// - `prefix` is a list of nullifiers prefixes to search for.
406 /// - `block_from`: The starting block number for the range (inclusive).
407 /// - `block_to`: The ending block number for the range (inclusive).
408 ///
409 /// Returned nullifiers are not verified to carry one of the requested prefixes;
410 /// [`VerifyingRpcClient`] performs that check.
411 async fn sync_nullifiers(
412 &self,
413 prefix: &[u16],
414 block_from: BlockNumber,
415 block_to: BlockNumber,
416 ) -> Result<Vec<NullifierUpdate>, RpcError>;
417
418 /// Fetches the account from the node, using the `/GetAccount` endpoint.
419 ///
420 /// The response carries an
421 /// [`AccountWitness`](miden_protocol::block::account_tree::AccountWitness) and the target
422 /// block. Public accounts additionally get [`AccountDetails`]; for private accounts the other
423 /// `request` fields are ignored.
424 ///
425 /// For a fully oversize-resolved account, use [`NodeRpcClient::get_account_details`].
426 ///
427 /// The response block number is not verified against the requested one; [`VerifyingRpcClient`]
428 /// performs that check.
429 ///
430 /// # Errors
431 ///
432 /// - If the account isn't found in the network
433 async fn get_account(
434 &self,
435 account_id: AccountId,
436 request: GetAccountRequest,
437 ) -> Result<(BlockNumber, AccountProof), RpcError>;
438
439 /// Fills in the asset list when the vault came back flagged `too_many_assets`, by querying
440 /// [`NodeRpcClient::sync_account_vault`] over `[GENESIS, block_to]`. No-op when the flag isn't
441 /// set.
442 async fn resolve_oversize_vault(
443 &self,
444 account_id: AccountId,
445 block_to: BlockNumber,
446 details: &mut AccountDetails,
447 ) -> Result<(), RpcError> {
448 if !details.vault_details.too_many_assets {
449 return Ok(());
450 }
451 let vault_info =
452 self.sync_account_vault(BlockNumber::GENESIS, block_to, account_id).await?;
453 // Syncing from genesis merges the full vault history into an absolute patch, so its updated
454 // (non-removed) assets are the account's current vault contents.
455 details.vault_details.assets = vault_info.vault_patch.updated_assets().collect();
456 details.vault_details.too_many_assets = false;
457 Ok(())
458 }
459
460 /// Fills in the entries of any storage map the node reported as oversize, by querying
461 /// [`NodeRpcClient::sync_storage_maps`] over `[GENESIS, block_to]`. No-op when no map is
462 /// oversize.
463 async fn resolve_oversize_storage_maps(
464 &self,
465 account_id: AccountId,
466 block_to: BlockNumber,
467 details: &mut AccountDetails,
468 ) -> Result<(), RpcError> {
469 if !details
470 .storage_details
471 .map_details
472 .iter()
473 .any(AccountStorageMapDetails::is_limit_exceeded)
474 {
475 return Ok(());
476 }
477 let info = self.sync_storage_maps(BlockNumber::GENESIS, block_to, account_id).await?;
478 for map_details in &mut details.storage_details.map_details {
479 if !map_details.is_limit_exceeded() {
480 continue;
481 }
482 // Syncing from genesis merges the full history of each slot into its absolute current
483 // entries, so the result is the complete map content.
484 let entries: Vec<StorageMapEntry> = info
485 .map_entries
486 .get(&map_details.slot_name)
487 .map(|entries| {
488 entries
489 .as_map()
490 .iter()
491 .map(|(key, value)| StorageMapEntry { key: *key, value: *value })
492 .collect()
493 })
494 .unwrap_or_default();
495 map_details.entries = StorageMapEntries::AllEntries(entries);
496 }
497 Ok(())
498 }
499
500 /// Fetches the commit height where the nullifier was consumed. If the nullifier isn't found,
501 /// then `None` is returned. The `block_num` parameter is the block number to start the search
502 /// from (inclusive).
503 ///
504 /// The default implementation of this method makes two RPC requests: one to
505 /// [`NodeRpcClient::get_block_header_by_number`] to resolve the chain tip, and one to
506 /// [`NodeRpcClient::sync_nullifiers`] to search up to that tip.
507 async fn get_nullifier_commit_heights(
508 &self,
509 requested_nullifiers: BTreeSet<Nullifier>,
510 block_from: BlockNumber,
511 ) -> Result<BTreeMap<Nullifier, Option<BlockNumber>>, RpcError> {
512 let prefixes: Vec<u16> =
513 requested_nullifiers.iter().map(crate::note::Nullifier::prefix).collect();
514 let (chain_tip, _) = self.get_block_header_by_number(None, false).await?;
515 let retrieved_nullifiers =
516 self.sync_nullifiers(&prefixes, block_from, chain_tip.block_num()).await?;
517
518 let mut nullifiers_height = BTreeMap::new();
519 for nullifier in requested_nullifiers {
520 if let Some(update) =
521 retrieved_nullifiers.iter().find(|update| update.nullifier == nullifier)
522 {
523 nullifiers_height.insert(nullifier, Some(update.block_num));
524 } else {
525 nullifiers_height.insert(nullifier, None);
526 }
527 }
528
529 Ok(nullifiers_height)
530 }
531
532 /// Fetches public note-related data for a list of [`NoteId`] and builds [`InputNoteRecord`]s
533 /// with it. If a note is not found or it's private, it is ignored and will not be included in
534 /// the returned list.
535 ///
536 /// The default implementation of this method uses [`NodeRpcClient::get_notes_by_id`].
537 async fn get_public_note_records(
538 &self,
539 note_ids: &[NoteId],
540 current_timestamp: Option<u64>,
541 ) -> Result<Vec<InputNoteRecord>, RpcError> {
542 if note_ids.is_empty() {
543 return Ok(vec![]);
544 }
545
546 let mut public_notes = Vec::with_capacity(note_ids.len());
547 let note_details = self.get_notes_by_id(note_ids).await?;
548
549 for detail in note_details {
550 if let FetchedNote::Public(note, inclusion_proof) = detail {
551 let state = UnverifiedNoteState {
552 metadata: *note.metadata(),
553 inclusion_proof,
554 }
555 .into();
556 let attachments = note.attachments().clone();
557 let note = InputNoteRecord::new(note.into(), attachments, current_timestamp, state);
558
559 public_notes.push(note);
560 }
561 }
562
563 Ok(public_notes)
564 }
565
566 /// Given a block number, fetches the block header corresponding to that height from the node
567 /// along with the MMR proof.
568 ///
569 /// The default implementation of this method uses
570 /// [`NodeRpcClient::get_block_header_by_number`].
571 async fn get_block_header_with_proof(
572 &self,
573 block_num: BlockNumber,
574 ) -> Result<(BlockHeader, MmrProof), RpcError> {
575 let (header, proof) = self.get_block_header_by_number(Some(block_num), true).await?;
576 Ok((header, proof.ok_or(RpcError::ExpectedDataMissing(String::from("MmrProof")))?))
577 }
578
579 /// Fetches the note with the specified ID.
580 ///
581 /// The default implementation of this method uses [`NodeRpcClient::get_notes_by_id`].
582 ///
583 /// Errors:
584 /// - [`RpcError::NoteNotFound`] if the note with the specified ID is not found.
585 async fn get_note_by_id(&self, note_id: NoteId) -> Result<FetchedNote, RpcError> {
586 let notes = self.get_notes_by_id(&[note_id]).await?;
587 notes.into_iter().next().ok_or(RpcError::NoteNotFound(note_id))
588 }
589
590 /// Fetches the note script with the specified root, returning `None` if the node has no script
591 /// registered for that root.
592 ///
593 /// A returned script's root is not verified to match the requested `root`;
594 /// [`VerifyingRpcClient`] performs that check.
595 async fn get_note_script_by_root(&self, root: Word) -> Result<Option<NoteScript>, RpcError>;
596
597 /// Fetches storage map updates for specified account and storage slots within a block range,
598 /// using the `/SyncStorageMaps` RPC endpoint.
599 ///
600 /// - `block_from`: The starting block number for the range (inclusive).
601 /// - `block_to`: The ending block number for the range (inclusive). The node rejects values
602 /// greater than the chain tip.
603 /// - `account_id`: The account ID for which to fetch storage map updates.
604 async fn sync_storage_maps(
605 &self,
606 block_from: BlockNumber,
607 block_to: BlockNumber,
608 account_id: AccountId,
609 ) -> Result<StorageMapInfo, RpcError>;
610
611 /// Fetches account vault updates for specified account within a block range, using the
612 /// `/SyncAccountVault` RPC endpoint.
613 ///
614 /// - `block_from`: The starting block number for the range (inclusive).
615 /// - `block_to`: The ending block number for the range (inclusive). The node rejects values
616 /// greater than the chain tip.
617 /// - `account_id`: The account ID for which to fetch storage map updates.
618 async fn sync_account_vault(
619 &self,
620 block_from: BlockNumber,
621 block_to: BlockNumber,
622 account_id: AccountId,
623 ) -> Result<AccountVaultInfo, RpcError>;
624
625 /// Fetches transaction records for specific accounts within a block range using the
626 /// `/SyncTransactions` RPC endpoint.
627 ///
628 /// - `block_from`: The starting block number for the range (inclusive).
629 /// - `block_to`: The ending block number for the range (inclusive).
630 /// - `account_ids`: The account IDs for which to fetch transactions.
631 async fn sync_transactions(
632 &self,
633 block_from: BlockNumber,
634 block_to: BlockNumber,
635 account_ids: Vec<AccountId>,
636 ) -> Result<Vec<TransactionRecord>, RpcError>;
637
638 /// Fetches the network ID of the node.
639 /// Errors:
640 /// - [`RpcError::ExpectedDataMissing`] if the note with the specified root is not found.
641 async fn get_network_id(&self) -> Result<NetworkId, RpcError>;
642
643 /// Fetches the RPC limits configured on the node.
644 ///
645 /// Implementations may cache the result internally to avoid repeated network calls.
646 async fn get_rpc_limits(&self) -> Result<RpcLimits, RpcError>;
647
648 /// Returns the RPC limits if they have been set, without fetching from the node.
649 fn has_rpc_limits(&self) -> Option<RpcLimits>;
650
651 /// Sets the RPC limits internally to be used by the client.
652 async fn set_rpc_limits(&self, limits: RpcLimits);
653
654 /// Fetches the RPC status without requiring Accept header validation.
655 ///
656 /// This is useful for diagnostics when version negotiation fails, as it allows retrieving node
657 /// information even when there's a version mismatch.
658 async fn get_status_unversioned(&self) -> Result<RpcStatusInfo, RpcError>;
659
660 /// Fetches the status of a specific network note ID.
661 ///
662 /// This is useful for debugging when a network note fails.
663 async fn get_network_note_status(
664 &self,
665 note_id: NoteId,
666 ) -> Result<NetworkNoteStatusInfo, RpcError>;
667}
668
669/// Selects which note content [`NodeRpcClient::sync_notes_with_content`] resolves via
670/// `GetNotesById` after syncing note inclusions.
671///
672/// This enables the possibility of optimizing the call by not requesting more data than needed. For
673/// example, when a public note's details are already known (but not the attachments),
674/// `AttachmentsOnly` can be used. One example of this is when importing notes through
675/// `NoteDetails`.
676///
677/// Neither policy requests attachment content the sync response already carried in full, so a note
678/// whose attachments all fit in a single word is never fetched for its attachments alone.
679#[derive(Debug, Clone, Copy, PartialEq, Eq)]
680pub enum NoteContentFetch {
681 /// Fetch the full body of every public note in the range, plus any attachment content still
682 /// missing.
683 PublicDetailsAndAttachments,
684 /// Fetch only the attachment content still missing.
685 AttachmentsOnly,
686}
687
688// RPC API ENDPOINT
689// ================================================================================================
690//
691/// RPC methods for the Miden protocol.
692#[derive(Debug, Clone, Copy)]
693pub enum RpcEndpoint {
694 Status,
695 SyncNullifiers,
696 GetAccount,
697 GetBlockByNumber,
698 GetBlockHeaderByNumber,
699 GetNotesById,
700 SyncChainMmr,
701 SubmitProvenTx,
702 SubmitProvenBatch,
703 SyncNotes,
704 GetNoteScriptByRoot,
705 SyncStorageMaps,
706 SyncAccountVault,
707 SyncTransactions,
708 GetLimits,
709 GetNetworkNoteStatus,
710 GetTransactionEncryptionKey,
711}
712
713impl RpcEndpoint {
714 /// Returns the endpoint name as used in the RPC service definition.
715 pub fn proto_name(&self) -> &'static str {
716 match self {
717 RpcEndpoint::Status => "Status",
718 RpcEndpoint::SyncNullifiers => "SyncNullifiers",
719 RpcEndpoint::GetAccount => "GetAccount",
720 RpcEndpoint::GetBlockByNumber => "GetBlockByNumber",
721 RpcEndpoint::GetBlockHeaderByNumber => "GetBlockHeaderByNumber",
722 RpcEndpoint::GetNotesById => "GetNotesById",
723 RpcEndpoint::SyncChainMmr => "SyncChainMmr",
724 RpcEndpoint::GetTransactionEncryptionKey => "GetTransactionEncryptionKey",
725 RpcEndpoint::SubmitProvenTx => "SubmitProvenTransaction",
726 RpcEndpoint::SubmitProvenBatch => "SubmitProvenBatch",
727 RpcEndpoint::SyncNotes => "SyncNotes",
728 RpcEndpoint::GetNoteScriptByRoot => "GetNoteScriptByRoot",
729 RpcEndpoint::SyncStorageMaps => "SyncStorageMaps",
730 RpcEndpoint::SyncAccountVault => "SyncAccountVault",
731 RpcEndpoint::SyncTransactions => "SyncTransactions",
732 RpcEndpoint::GetLimits => "GetLimits",
733 RpcEndpoint::GetNetworkNoteStatus => "GetNetworkNoteStatus",
734 }
735 }
736
737 /// Returns whether repeating the call is safe when the outcome of the previous attempt is
738 /// unknown.
739 ///
740 /// Submissions are not: the node may have accepted the transaction before the response was
741 /// lost, so a repeat hits already-consumed state and comes back as a conflict that cannot be
742 /// told apart from a genuine double spend.
743 ///
744 /// The match is exhaustive on purpose, so a new endpoint has to be classified before it
745 /// compiles.
746 #[cfg(feature = "tonic")]
747 pub(crate) fn is_idempotent(self) -> bool {
748 match self {
749 RpcEndpoint::SubmitProvenTx | RpcEndpoint::SubmitProvenBatch => false,
750 RpcEndpoint::Status
751 | RpcEndpoint::SyncNullifiers
752 | RpcEndpoint::GetAccount
753 | RpcEndpoint::GetBlockByNumber
754 | RpcEndpoint::GetBlockHeaderByNumber
755 | RpcEndpoint::GetNotesById
756 | RpcEndpoint::SyncChainMmr
757 | RpcEndpoint::SyncNotes
758 | RpcEndpoint::GetNoteScriptByRoot
759 | RpcEndpoint::SyncStorageMaps
760 | RpcEndpoint::SyncAccountVault
761 | RpcEndpoint::SyncTransactions
762 | RpcEndpoint::GetLimits
763 | RpcEndpoint::GetNetworkNoteStatus
764 | RpcEndpoint::GetTransactionEncryptionKey => true,
765 }
766 }
767}
768
769impl fmt::Display for RpcEndpoint {
770 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
771 match self {
772 RpcEndpoint::Status => write!(f, "status"),
773 RpcEndpoint::SyncNullifiers => {
774 write!(f, "sync_nullifiers")
775 },
776 RpcEndpoint::GetAccount => write!(f, "get_account"),
777 RpcEndpoint::GetBlockByNumber => write!(f, "get_block_by_number"),
778 RpcEndpoint::GetBlockHeaderByNumber => {
779 write!(f, "get_block_header_by_number")
780 },
781 RpcEndpoint::GetNotesById => write!(f, "get_notes_by_id"),
782 RpcEndpoint::SyncChainMmr => write!(f, "sync_chain_mmr"),
783 RpcEndpoint::GetTransactionEncryptionKey => {
784 write!(f, "get_transaction_encryption_key")
785 },
786 RpcEndpoint::SubmitProvenTx => write!(f, "submit_proven_transaction"),
787 RpcEndpoint::SubmitProvenBatch => write!(f, "submit_proven_batch"),
788 RpcEndpoint::SyncNotes => write!(f, "sync_notes"),
789 RpcEndpoint::GetNoteScriptByRoot => write!(f, "get_note_script_by_root"),
790 RpcEndpoint::SyncStorageMaps => write!(f, "sync_storage_maps"),
791 RpcEndpoint::SyncAccountVault => write!(f, "sync_account_vault"),
792 RpcEndpoint::SyncTransactions => write!(f, "sync_transactions"),
793 RpcEndpoint::GetLimits => write!(f, "get_limits"),
794 RpcEndpoint::GetNetworkNoteStatus => write!(f, "get_network_note_status"),
795 }
796 }
797}