Skip to main content

canton_ledger/
client.rs

1//! The async Ledger API client.
2
3use std::sync::Arc;
4use std::time::Duration;
5
6use canton_core::auth::{self, Intercepted};
7use canton_core::telemetry::{self, TRANSPORT_GRPC};
8use canton_core::{Config, Error, Result};
9use canton_proto::com::daml::ledger::api::v2 as pb;
10use canton_proto::grpc::health::v1 as health_pb;
11use canton_proto::grpc::health::v1::health_check_response::ServingStatus;
12use futures_core::Stream;
13use tokio_stream::StreamExt as _;
14use tonic::transport::Channel;
15
16/// An `EventFormat` with a wildcard (all-templates) filter for each party.
17fn wildcard_event_format(parties: &[String]) -> pb::EventFormat {
18    let filters_by_party = parties
19        .iter()
20        .map(|party| {
21            (
22                party.clone(),
23                pb::Filters {
24                    cumulative: vec![pb::CumulativeFilter {
25                        identifier_filter: Some(
26                            pb::cumulative_filter::IdentifierFilter::WildcardFilter(
27                                pb::WildcardFilter {
28                                    include_created_event_blob: false,
29                                },
30                            ),
31                        ),
32                    }],
33                },
34            )
35        })
36        .collect();
37
38    pb::EventFormat {
39        filters_by_party,
40        filters_for_any_party: None,
41        verbose: true,
42    }
43}
44
45/// Give an execute request a submission id if it has none.
46///
47/// The change ID de-duplicates a submission; the submission id only tells the
48/// completions of separate *attempts* apart, and the Ledger API asks for a
49/// different one on each retry. Generating it per attempt rather than when the
50/// request is built is what makes attempts distinguishable while leaving the
51/// change ID — and therefore the de-duplication — untouched.
52trait HasSubmissionId {
53    fn submission_id_mut(&mut self) -> &mut String;
54}
55
56macro_rules! has_submission_id {
57    ($($t:ty),+) => {$(
58        impl HasSubmissionId for $t {
59            fn submission_id_mut(&mut self) -> &mut String {
60                &mut self.submission_id
61            }
62        }
63    )+};
64}
65
66has_submission_id!(
67    pb::interactive::ExecuteSubmissionRequest,
68    pb::interactive::ExecuteSubmissionAndWaitRequest,
69    pb::interactive::ExecuteSubmissionAndWaitForTransactionRequest
70);
71
72fn fresh_submission_id<T: HasSubmissionId>(mut request: T) -> T {
73    let id = request.submission_id_mut();
74    if id.is_empty() {
75        *id = uuid::Uuid::new_v4().to_string();
76    }
77    request
78}
79
80/// Whether a failed submission is the participant refusing a command it already
81/// has — and, on a retry, one this client is responsible for having sent.
82///
83/// `ALREADY_EXISTS` is what Canton's `DUPLICATE_COMMAND` arrives as. On a first
84/// attempt it means the *caller* reused a change ID from an earlier submission,
85/// which is a genuine rejection they must see. On a retry it means our own
86/// previous attempt was accepted, which is the opposite of a failure — so the
87/// caller of this predicate checks that first.
88fn is_duplicate_of_our_own(status: &tonic::Status) -> bool {
89    status.code() == tonic::Code::AlreadyExists
90}
91
92/// Refuse a command set the participant is certain to refuse.
93///
94/// Every one of these costs a round trip and comes back as a server-side error
95/// that reads like the ledger's fault. They are the caller's, and they are
96/// knowable here.
97fn validate_commands(commands: &pb::Commands) -> Result<()> {
98    if commands.act_as.is_empty() {
99        return Err(Error::InvalidRequest(
100            "a submission needs at least one acting party".to_string(),
101        ));
102    }
103    if commands.commands.is_empty() {
104        return Err(Error::InvalidRequest(
105            "a submission needs at least one command".to_string(),
106        ));
107    }
108    if commands.min_ledger_time_abs.is_some() && commands.min_ledger_time_rel.is_some() {
109        return Err(Error::InvalidRequest(
110            "min_ledger_time_abs and min_ledger_time_rel are mutually exclusive: set one"
111                .to_string(),
112        ));
113    }
114    Ok(())
115}
116
117/// One entry of an Active Contract Set snapshot.
118///
119/// A snapshot is not only active contracts. A reassignment that is half-done at
120/// the snapshot offset appears as an *incomplete* entry — unassigned from one
121/// synchronizer with no matching assignment yet, or assigned to one with the
122/// unassignment out of view — and an application that reads only
123/// [`Active`](Self::Active) sees a contract that has left one synchronizer and
124/// not arrived at the other simply vanish. The Ledger API sends these precisely
125/// so a multi-synchronizer application can bootstrap without that hole, which is
126/// why there is a lossless read.
127///
128/// `#[non_exhaustive]`: the Ledger API may add an entry kind.
129#[derive(Clone, Debug, PartialEq)]
130#[non_exhaustive]
131pub enum AcsEntry {
132    /// A contract active on the synchronizer named in the entry.
133    Active(pb::ActiveContract),
134    /// Unassigned before the snapshot offset with no assignment before it —
135    /// the contract is in flight *out of* this synchronizer.
136    IncompleteUnassigned(pb::IncompleteUnassigned),
137    /// Assigned before the snapshot offset with no unassignment before it.
138    /// Note this does **not** say the contract is active on the target
139    /// synchronizer; the proto is explicit about that.
140    IncompleteAssigned(pb::IncompleteAssigned),
141}
142
143impl AcsEntry {
144    /// The active contract, if this entry is one.
145    #[must_use]
146    pub fn active(&self) -> Option<&pb::ActiveContract> {
147        match self {
148            AcsEntry::Active(active) => Some(active),
149            _ => None,
150        }
151    }
152
153    /// Consume the entry, yielding the active contract if it is one.
154    #[must_use]
155    pub fn into_active(self) -> Option<pb::ActiveContract> {
156        match self {
157            AcsEntry::Active(active) => Some(active),
158            _ => None,
159        }
160    }
161}
162
163/// Convert one wire entry, dropping only the case the participant left empty
164/// (the `oneof` is required, so an empty entry is a participant that sent
165/// nothing to report).
166fn acs_entry(entry: Option<pb::get_active_contracts_response::ContractEntry>) -> Option<AcsEntry> {
167    use pb::get_active_contracts_response::ContractEntry;
168    match entry {
169        Some(ContractEntry::ActiveContract(active)) => Some(AcsEntry::Active(active)),
170        Some(ContractEntry::IncompleteUnassigned(unassigned)) => {
171            Some(AcsEntry::IncompleteUnassigned(unassigned))
172        }
173        Some(ContractEntry::IncompleteAssigned(assigned)) => {
174            Some(AcsEntry::IncompleteAssigned(assigned))
175        }
176        None => None,
177    }
178}
179
180/// The offset of an update, for resumable-stream position tracking.
181fn update_offset(update: &pb::get_updates_response::Update) -> i64 {
182    use pb::get_updates_response::Update;
183    match update {
184        Update::Transaction(t) => t.offset,
185        Update::Reassignment(r) => r.offset,
186        Update::TopologyTransaction(t) => t.offset,
187        Update::OffsetCheckpoint(c) => c.offset,
188    }
189}
190
191/// Build a gRPC service client on the authenticated channel, with this
192/// client's decode limit applied.
193///
194/// A macro rather than a function because `max_decoding_message_size` is an
195/// inherent method on each generated client — tonic exposes no trait for it —
196/// so there is nothing to be generic over. Keeping every construction site
197/// behind one expansion is the point: `tonic`'s 4 MiB default is small enough
198/// that a real ACS page trips it, and a new RPC added later would otherwise
199/// pick the default up silently.
200macro_rules! service {
201    ($self:ident, $ctor:expr) => {
202        $ctor($self.intercepted().await?)
203            .max_decoding_message_size($self.config.max_decoding_message_size())
204    };
205}
206
207/// An async client for the Canton Ledger API over gRPC.
208///
209/// The client owns a lazily-connected [`Channel`]; cloning it is cheap and
210/// clones share the underlying connection pool, so it is safe to hand a clone
211/// to each task.
212#[derive(Clone, Debug)]
213pub struct CantonClient {
214    channel: Channel,
215    config: Arc<Config>,
216}
217
218impl CantonClient {
219    /// Build a lazily-connected client. Returns immediately; the TCP/TLS
220    /// handshake happens on the first RPC.
221    ///
222    /// # Errors
223    /// Returns [`Error::InvalidRequest`] if the endpoint is not a valid URI.
224    pub fn connect_lazy(config: Config) -> Result<Self> {
225        Ok(Self {
226            channel: config.connect_channel()?,
227            config: Arc::new(config),
228        })
229    }
230
231    /// A channel wrapped with a fresh bearer-token interceptor for this call.
232    async fn intercepted(&self) -> Result<Intercepted> {
233        auth::intercepted(&self.channel, self.config.auth()).await
234    }
235
236    /// Run a unary operation under the configured retry policy (no-op when
237    /// retrying is not enabled).
238    async fn with_retry<T, F, Fut>(&self, op: F) -> Result<T>
239    where
240        F: FnMut() -> Fut,
241        Fut: std::future::Future<Output = Result<T>>,
242    {
243        canton_core::retry::run_with_retry(self.config.retry(), op).await
244    }
245
246    /// Return the participant's Ledger API version string (e.g. `"3.5.7"`).
247    ///
248    /// # Errors
249    /// Returns an [`Error`] if the RPC fails.
250    pub async fn version(&self) -> Result<String> {
251        telemetry::instrument("version", TRANSPORT_GRPC, async {
252            self.with_retry(|| async {
253                let mut client =
254                    service!(self, pb::version_service_client::VersionServiceClient::new);
255                let response = client
256                    .get_ledger_api_version(pb::GetLedgerApiVersionRequest {})
257                    .await?
258                    .into_inner();
259                Ok(response.version)
260            })
261            .await
262        })
263        .await
264    }
265
266    /// Probe the participant's overall serving status via the standard
267    /// `grpc.health.v1.Health` service (served on the Ledger API port).
268    ///
269    /// Poll this to react to intermittent or permanent node failure: a healthy
270    /// participant answers [`ServingStatus::Serving`]; an unreachable one
271    /// surfaces a transport [`Error`] (see [`Error::is_retriable`]).
272    ///
273    /// # Errors
274    /// Returns an [`Error`] if the health RPC fails (e.g. the node is down).
275    pub async fn health_check(&self) -> Result<ServingStatus> {
276        telemetry::instrument("health_check", TRANSPORT_GRPC, async {
277            self.with_retry(|| async {
278                let mut client = service!(self, health_pb::health_client::HealthClient::new);
279                // Empty `service` = the server's overall status (Canton does not
280                // register per-service health entries on the Ledger API port).
281                let response = client
282                    .check(health_pb::HealthCheckRequest {
283                        service: String::new(),
284                    })
285                    .await?
286                    .into_inner();
287                Ok(response.status())
288            })
289            .await
290        })
291        .await
292    }
293
294    /// Submit commands **fire-and-forget** (`CommandSubmissionService.Submit`):
295    /// hand the commands to the participant and return promptly without waiting
296    /// for the transaction. Returns the change-ID `command_id` used, so the
297    /// caller can recover the outcome later with [`Self::await_completion`] (or
298    /// the [`Self::completions`] stream).
299    ///
300    /// A fresh UUID `command_id` is generated when the caller did not set one,
301    /// so ledger-side de-duplication behaves correctly across retries.
302    ///
303    /// # Errors
304    /// Returns an [`Error`] if authentication fails or the participant rejects
305    /// the submission synchronously (e.g. a preprocessing error).
306    pub async fn submit(&self, submit: crate::command::Submit) -> Result<String> {
307        let submission = self.submission(submit);
308        let command_id = submission.change_id().command_id().to_string();
309        submission.submit().await?;
310        Ok(command_id)
311    }
312
313    /// Fix a submission's identity **before** sending it, returning a
314    /// [`Submission`](crate::Submission) that carries its
315    /// [`ChangeId`](crate::ChangeId).
316    ///
317    /// This is the handle to reach for when losing the outcome is not an
318    /// option. A submission whose response is lost — a dropped connection, a
319    /// timeout, a retry the participant de-duplicated — may well have
320    /// committed, and the only way back to the answer is the change ID. If the
321    /// SDK generated the command id inside the call that failed, there is no
322    /// change ID to go back with.
323    ///
324    /// ```no_run
325    /// # async fn run(client: canton_ledger::CantonClient, submit: canton_ledger::Submit)
326    /// #     -> canton_ledger::Result<()> {
327    /// use std::time::Duration;
328    ///
329    /// // Where to start reading completions from, taken before submitting.
330    /// let offset = client.ledger_end().await?;
331    /// let submission = client.submission(submit);
332    ///
333    /// if submission.submit_and_wait().await.is_err() {
334    ///     // Ambiguous: ask the ledger what actually happened.
335    ///     let completion = submission.recover(offset, Duration::from_secs(30)).await?;
336    ///     println!("committed after all: {}", completion.update_id);
337    /// }
338    /// # Ok(()) }
339    /// ```
340    #[must_use]
341    pub fn submission(&self, submit: crate::command::Submit) -> crate::submission::Submission {
342        let shape = submit.transaction_shape;
343        let (change_id, commands) = submit.into_commands();
344        crate::submission::Submission::new(self.clone(), change_id, commands, shape)
345    }
346
347    pub(crate) async fn submit_commands(&self, commands: pb::Commands) -> Result<()> {
348        validate_commands(&commands)?;
349        telemetry::instrument("submit", TRANSPORT_GRPC, async move {
350            let attempt = std::sync::atomic::AtomicU32::new(0);
351            let command_id = commands.command_id.clone();
352            self.with_retry(|| {
353                let commands = commands.clone();
354                let command_id = command_id.clone();
355                let retry = attempt.fetch_add(1, std::sync::atomic::Ordering::Relaxed) > 0;
356                async move {
357                    let mut client = service!(
358                        self,
359                        pb::command_submission_service_client::CommandSubmissionServiceClient::new
360                    );
361                    match client
362                        .submit(pb::SubmitRequest {
363                            commands: Some(commands),
364                        })
365                        .await
366                    {
367                        Ok(_) => Ok(()),
368                        Err(status) if retry && is_duplicate_of_our_own(&status) => {
369                            // The command we are retrying is already at the
370                            // participant, and we are the ones who put it
371                            // there: a previous attempt of this same retry loop
372                            // was accepted and its response was lost. Reporting
373                            // the duplicate rejection would tell the caller
374                            // their command failed when it did precisely the
375                            // opposite — the failure mode this whole change ID
376                            // exists to prevent.
377                            tracing::debug!(
378                                %command_id,
379                                "submission retry was de-duplicated; the earlier attempt is the one that landed"
380                            );
381                            Ok(())
382                        }
383                        Err(status) => Err(Error::from(status)),
384                    }
385                }
386            })
387            .await
388        })
389        .await
390    }
391
392    /// Submit commands and wait for the result **without** fetching the
393    /// transaction (`CommandService.SubmitAndWait`): blocks until the command
394    /// commits (or is rejected) and returns the `update_id` and completion
395    /// offset. Lighter than [`Self::submit_and_wait_for_transaction`] when the
396    /// caller does not need the event payload.
397    ///
398    /// # Errors
399    /// Returns an [`Error`] if authentication fails or the command is rejected.
400    /// The retry caveat on [`Self::submit_and_wait_for_transaction`] applies.
401    pub async fn submit_and_wait(
402        &self,
403        submit: crate::command::Submit,
404    ) -> Result<pb::SubmitAndWaitResponse> {
405        self.submission(submit).submit_and_wait().await
406    }
407
408    pub(crate) async fn submit_and_wait_commands(
409        &self,
410        commands: pb::Commands,
411    ) -> Result<pb::SubmitAndWaitResponse> {
412        validate_commands(&commands)?;
413        telemetry::instrument("submit_and_wait", TRANSPORT_GRPC, async move {
414            let request = pb::SubmitAndWaitRequest {
415                commands: Some(commands),
416            };
417            self.with_retry(|| {
418                let request = request.clone();
419                async move {
420                    let mut client =
421                        service!(self, pb::command_service_client::CommandServiceClient::new);
422                    Ok(client.submit_and_wait(request).await?.into_inner())
423                }
424            })
425            .await
426        })
427        .await
428    }
429
430    /// Submit commands and wait for the resulting transaction.
431    ///
432    /// Fills the change ID's `command_id` with a fresh UUID when the caller did
433    /// not set one, so ledger-side de-duplication behaves correctly. The
434    /// returned transaction is shaped as `LEDGER_EFFECTS` and filtered to the
435    /// acting party (wildcard), so created events are visible in the response.
436    ///
437    /// # Errors
438    /// Returns an [`Error`] if authentication fails, the command is rejected, or
439    /// the response contains no transaction.
440    ///
441    /// # Example
442    /// ```no_run
443    /// # async fn run(client: canton_ledger::CantonClient, party: &str, pkg: &str)
444    /// #     -> canton_ledger::Result<()> {
445    /// use canton_ledger::{Submit, create, identifier, record};
446    ///
447    /// let tx = client
448    ///     .submit_and_wait_for_transaction(
449    ///         Submit::new(party)
450    ///             .add_command(create(identifier(pkg, "M", "T"), record(vec![]))),
451    ///     )
452    ///     .await?;
453    /// println!("committed {} at offset {}", tx.update_id, tx.offset);
454    /// # Ok(()) }
455    /// ```
456    ///
457    /// # Retry caveat (exactly-once)
458    /// With retry enabled ([`Config::with_retry`]), a submission that commits
459    /// on the ledger but whose response is lost to a retriable error is re-sent
460    /// with the same `command_id` and de-duplicated by the participant.
461    ///
462    /// [`Self::submit`] can answer that on its own: a duplicate rejection of a
463    /// retry it made itself means its earlier attempt was accepted, which is
464    /// success, so it reports success. **This method cannot.** Its result is
465    /// the committed transaction, and a de-duplicated retry does not carry one
466    /// — so the duplicate rejection reaches the caller, and the transaction has
467    /// to be read back rather than invented.
468    ///
469    /// Take a [`Submission`](crate::Submission) from [`Self::submission`]
470    /// before submitting, and on any error recover the outcome with
471    /// [`Submission::recover`](crate::Submission::recover) — it knows the
472    /// change ID, which is the only way back to a command whose response was
473    /// lost. See the `recover_a_submission` example.
474    pub async fn submit_and_wait_for_transaction(
475        &self,
476        submit: crate::command::Submit,
477    ) -> Result<pb::Transaction> {
478        self.submission(submit)
479            .submit_and_wait_for_transaction()
480            .await
481    }
482
483    pub(crate) async fn submit_and_wait_for_transaction_commands(
484        &self,
485        commands: pb::Commands,
486        shape: crate::request::TransactionShape,
487    ) -> Result<pb::Transaction> {
488        validate_commands(&commands)?;
489        // The filter covers `read_as` as well as `act_as`: the Ledger API's own
490        // default for a submission is both, and a command submitted with
491        // `read_as` set is one whose result the caller expects to see through
492        // those parties too. Filtering to `act_as` alone silently returned a
493        // transaction with events missing.
494        let mut parties = commands.act_as.clone();
495        parties.extend(commands.read_as.iter().cloned());
496        parties.sort_unstable();
497        parties.dedup();
498        let request = pb::SubmitAndWaitForTransactionRequest {
499            transaction_format: Some(pb::TransactionFormat {
500                event_format: Some(wildcard_event_format(&parties)),
501                transaction_shape: shape.as_grpc() as i32,
502            }),
503            commands: Some(commands),
504        };
505        telemetry::instrument(
506            "submit_and_wait_for_transaction",
507            TRANSPORT_GRPC,
508            async move {
509                let response = self
510                    .with_retry(|| {
511                        let request = request.clone();
512                        async move {
513                            let mut client = service!(
514                                self,
515                                pb::command_service_client::CommandServiceClient::new
516                            );
517                            Ok(client
518                                .submit_and_wait_for_transaction(request)
519                                .await?
520                                .into_inner())
521                        }
522                    })
523                    .await?;
524
525                response.transaction.ok_or_else(|| {
526                    Error::UnexpectedResponse("response contained no transaction".to_string())
527                })
528            },
529        )
530        .await
531    }
532
533    /// The synchronizers this participant is connected to for `party`.
534    ///
535    /// Allocating an external party and preparing a submission both name a
536    /// synchronizer, and a caller has no other way to discover one — so this
537    /// is what turns "which synchronizer?" from configuration into a question
538    /// the participant answers.
539    ///
540    /// # Errors
541    /// Returns an [`Error`] if the RPC fails.
542    pub async fn connected_synchronizers(
543        &self,
544        party: &str,
545    ) -> Result<Vec<pb::get_connected_synchronizers_response::ConnectedSynchronizer>> {
546        let request = pb::GetConnectedSynchronizersRequest {
547            party: party.to_string(),
548            ..Default::default()
549        };
550        telemetry::instrument("connected_synchronizers", TRANSPORT_GRPC, async move {
551            let response = self
552                .with_retry(|| {
553                    let request = request.clone();
554                    async move {
555                        let mut client =
556                            service!(self, pb::state_service_client::StateServiceClient::new);
557                        Ok(client
558                            .get_connected_synchronizers(request)
559                            .await?
560                            .into_inner())
561                    }
562                })
563                .await?;
564            Ok(response.connected_synchronizers)
565        })
566        .await
567    }
568
569    /// Interpret a submission without authorizing it, returning the transaction
570    /// and the hash to sign.
571    ///
572    /// The first half of interactive submission: the participant works out what
573    /// the commands do, and hands back a hash for the party's key — which it
574    /// does not hold — to sign. See [`crate::interactive`] for the flow, and
575    /// for how long a prepared transaction stays executable.
576    ///
577    /// Retried like any other read: preparation commits nothing.
578    ///
579    /// # Errors
580    /// As any gRPC call, plus [`Error::UnexpectedResponse`] if the participant
581    /// returns no hash — which would leave nothing to sign.
582    pub async fn prepare_submission(
583        &self,
584        prepare: crate::interactive::Prepare,
585    ) -> Result<crate::interactive::Prepared> {
586        // Built once, outside the instrumented future, so retries reuse one
587        // command id — the change ID stays stable and the submission remains
588        // de-duplication-safe across attempts, exactly as `submit` does.
589        let act_as = prepare.act_as().to_vec();
590        // Carried through: the Ledger API's default event format for
591        // `…_and_wait_for_transaction` covers `act_as` *and* `read_as`, so
592        // dropping it here would silently return a narrower transaction.
593        let read_as = prepare.read_as().to_vec();
594        let (command_id, user_id, request) = prepare.into_request();
595        telemetry::instrument("prepare_submission", TRANSPORT_GRPC, async move {
596            let response = self
597                .with_retry(|| {
598                    let request = request.clone();
599                    async move {
600                        let mut client = service!(
601                            self,
602                            pb::interactive::interactive_submission_service_client::InteractiveSubmissionServiceClient::new
603                        );
604                        Ok(client.prepare_submission(request).await?.into_inner())
605                    }
606                })
607                .await?;
608            crate::interactive::Prepared::from_response(response, act_as, read_as, command_id, user_id)
609        })
610        .await
611    }
612
613    /// Submit a signed prepared transaction and return once the participant has
614    /// accepted it for processing.
615    ///
616    /// Acceptance is not commitment: the outcome arrives on the completion
617    /// stream. Use [`execute_submission_and_wait`](Self::execute_submission_and_wait)
618    /// to wait for it.
619    ///
620    /// # Retry caveat
621    /// With retry enabled, an attempt whose response was lost is retried under
622    /// a fresh submission id and the same change id, and the participant
623    /// answers `ALREADY_EXISTS`: that is the earlier attempt having landed,
624    /// and it is reported as success here, as on
625    /// [`submit`](Self::submit). An `Ok` still means only
626    /// *accepted*; whether it committed is a completion, reachable by
627    /// [`await_completion`](Self::await_completion) with the
628    /// [`Executable::change_id`](crate::interactive::Executable::change_id)
629    /// taken before the call.
630    ///
631    /// # Errors
632    /// As any gRPC call. A missing or wrong signature is a rejection from the
633    /// participant, not a local error.
634    pub async fn execute_submission(
635        &self,
636        executable: crate::interactive::Executable,
637    ) -> Result<()> {
638        let request = executable.into_execute_request();
639        telemetry::instrument("execute_submission", TRANSPORT_GRPC, async move {
640            let attempt = std::sync::atomic::AtomicU32::new(0);
641            self.with_retry(|| {
642                let request = fresh_submission_id(request.clone());
643                let retry = attempt.fetch_add(1, std::sync::atomic::Ordering::Relaxed) > 0;
644                async move {
645                    let mut client = service!(
646                        self,
647                        pb::interactive::interactive_submission_service_client::InteractiveSubmissionServiceClient::new
648                    );
649                    match client.execute_submission(request).await {
650                        Ok(_) => Ok(()),
651                        Err(status) if retry && is_duplicate_of_our_own(&status) => {
652                            // Same reasoning as the ordinary submit: a
653                            // previous attempt of this loop was accepted and
654                            // its answer lost, and reporting the duplicate as
655                            // a failure would say the opposite of what
656                            // happened.
657                            tracing::debug!(
658                                "execute retry was de-duplicated; the earlier attempt is the one that landed"
659                            );
660                            Ok(())
661                        }
662                        Err(status) => Err(Error::from(status)),
663                    }
664                }
665            })
666            .await
667        })
668        .await
669    }
670
671    /// Submit a signed prepared transaction and wait for it to commit.
672    ///
673    /// The response carries the update id **and** the completion offset — the
674    /// offset a follow-up completion or update read starts from, which is why
675    /// it is returned whole rather than as the id alone.
676    ///
677    /// # Retry caveat
678    /// As [`execute_submission`](Self::execute_submission): a retried attempt
679    /// that the participant de-duplicates is reported as the rejection it
680    /// answers with, so on an ambiguous outcome recover through
681    /// [`await_completion`](Self::await_completion) with the change id taken
682    /// before the call.
683    ///
684    /// # Errors
685    /// As any gRPC call, plus a rejection if the transaction fails.
686    pub async fn execute_submission_and_wait(
687        &self,
688        executable: crate::interactive::Executable,
689    ) -> Result<pb::interactive::ExecuteSubmissionAndWaitResponse> {
690        let request = executable.into_execute_and_wait_request();
691        telemetry::instrument("execute_submission_and_wait", TRANSPORT_GRPC, async move {
692            let response = self
693                .with_retry(|| {
694                    let request = fresh_submission_id(request.clone());
695                    async move {
696                        let mut client = service!(
697                            self,
698                            pb::interactive::interactive_submission_service_client::InteractiveSubmissionServiceClient::new
699                        );
700                        Ok(client
701                            .execute_submission_and_wait(request)
702                            .await?
703                            .into_inner())
704                    }
705                })
706                .await?;
707            Ok(response)
708        })
709        .await
710    }
711
712    /// Submit a signed prepared transaction and wait for the committed
713    /// transaction itself.
714    ///
715    /// The events it carries follow
716    /// [`Executable::with_transaction_shape`](crate::interactive::Executable::with_transaction_shape).
717    ///
718    /// # Errors
719    /// As any gRPC call, plus [`Error::UnexpectedResponse`] if the participant
720    /// reports success without a transaction.
721    pub async fn execute_submission_and_wait_for_transaction(
722        &self,
723        executable: crate::interactive::Executable,
724    ) -> Result<pb::Transaction> {
725        let event_format = wildcard_event_format(&executable.witnesses());
726        let request = executable.into_execute_and_wait_for_transaction_request(event_format);
727        telemetry::instrument(
728            "execute_submission_and_wait_for_transaction",
729            TRANSPORT_GRPC,
730            async move {
731                let response = self
732                    .with_retry(|| {
733                        let request = fresh_submission_id(request.clone());
734                        async move {
735                            let mut client = service!(
736                                self,
737                                pb::interactive::interactive_submission_service_client::InteractiveSubmissionServiceClient::new
738                            );
739                            Ok(client
740                                .execute_submission_and_wait_for_transaction(request)
741                                .await?
742                                .into_inner())
743                        }
744                    })
745                    .await?;
746
747                response.transaction.ok_or_else(|| {
748                    Error::UnexpectedResponse("response contained no transaction".to_string())
749                })
750            },
751        )
752        .await
753    }
754
755    /// Subscribe to the command-completion stream for `parties`, starting after
756    /// `begin_offset` (exclusive). Offset checkpoints are filtered out, so the
757    /// stream yields only [`pb::Completion`]s.
758    ///
759    /// # Errors
760    /// Returns an [`Error`] if authentication or opening the stream fails.
761    pub async fn completions(
762        &self,
763        parties: Vec<String>,
764        begin_offset: i64,
765    ) -> Result<impl Stream<Item = Result<pb::Completion>> + Send + use<>> {
766        self.completions_with(crate::request::CompletionsRequest::new(
767            parties,
768            begin_offset,
769        ))
770        .await
771    }
772
773    /// Like [`Self::completions`], with the full request surface: a
774    /// [`CompletionsRequest`](crate::request::CompletionsRequest) additionally
775    /// selects the `user_id` whose command completions to stream (pair it with
776    /// [`Submit::with_user_id`](crate::Submit::with_user_id)).
777    ///
778    /// # Errors
779    /// Returns an [`Error`] if authentication or opening the stream fails.
780    pub async fn completions_with(
781        &self,
782        request: crate::request::CompletionsRequest,
783    ) -> Result<impl Stream<Item = Result<pb::Completion>> + Send + use<>> {
784        request.validate()?;
785        telemetry::instrument("completions", TRANSPORT_GRPC, async move {
786            let mut client = service!(
787                self,
788                pb::command_completion_service_client::CommandCompletionServiceClient::new
789            );
790            let stream = client
791                .completion_stream(request.into_grpc())
792                .await?
793                .into_inner();
794
795            let stream = stream.filter_map(|item| match item {
796                Ok(response) => match response.completion_response {
797                    Some(pb::completion_stream_response::CompletionResponse::Completion(
798                        completion,
799                    )) => Some(Ok(completion)),
800                    _ => None, // skip offset checkpoints
801                },
802                Err(status) => Some(Err(Error::from(status))),
803            });
804            Ok(telemetry::instrument_stream(
805                "completions",
806                TRANSPORT_GRPC,
807                stream,
808            ))
809        })
810        .await
811    }
812
813    /// Recover the completion for a specific command by scanning the completion
814    /// stream from `begin_offset`, up to `timeout`.
815    ///
816    /// This is the command-recovery path: after a crash, lost connection, or
817    /// timeout, the outcome of a pending command is read back from the
818    /// completion endpoint instead of blindly re-submitting. If the command's
819    /// completion reports a non-OK status, this returns [`Error::CommandRejected`].
820    ///
821    /// Matching is on the whole [`ChangeId`](crate::ChangeId) — user, acting
822    /// parties and command id — because that is what identifies a command to
823    /// Canton. A command id on its own is not unique across the users of a
824    /// participant, and answering with somebody else's completion is worse
825    /// than answering with none.
826    ///
827    /// The completion stream is a live subscription that does not self-terminate,
828    /// so `timeout` bounds how long to wait for the target completion.
829    ///
830    /// # Errors
831    /// Returns [`Error::Timeout`] if the completion is not seen within `timeout`,
832    /// [`Error::CommandRejected`] if the ledger rejected the command, or another
833    /// [`Error`] if the stream fails.
834    pub async fn await_completion(
835        &self,
836        change_id: &crate::command::ChangeId,
837        begin_offset: i64,
838        timeout: Duration,
839    ) -> Result<pb::Completion> {
840        let scan = async {
841            let stream = self
842                .completions(change_id.act_as().to_vec(), begin_offset)
843                .await?;
844            tokio::pin!(stream);
845            while let Some(item) = stream.next().await {
846                let completion = item?;
847                if change_id.matches(&completion) {
848                    // A non-OK gRPC status on the completion means the ledger
849                    // rejected the command for business/interpretation reasons.
850                    if let Some(status) = &completion.status {
851                        // google.rpc.Status code 0 == OK; anything else is a rejection.
852                        if status.code != 0 {
853                            return Err(Error::CommandRejected {
854                                code: format!("{:?}", tonic::Code::from(status.code)),
855                                message: status.message.clone(),
856                            });
857                        }
858                    }
859                    return Ok(completion);
860                }
861            }
862            Err(Error::UnexpectedResponse(format!(
863                "completion stream ended before command {} was seen",
864                change_id.command_id()
865            )))
866        };
867
868        tokio::time::timeout(timeout, scan)
869            .await
870            .map_err(|_| Error::Timeout)?
871    }
872
873    /// Return the current ledger end offset as seen by the participant.
874    ///
875    /// A value of `0` means the participant's view of the ledger is empty.
876    /// This is an authenticated endpoint.
877    ///
878    /// # Errors
879    /// Returns an [`Error`] if authentication or the RPC fails.
880    pub async fn ledger_end(&self) -> Result<i64> {
881        telemetry::instrument("ledger_end", TRANSPORT_GRPC, async {
882            self.with_retry(|| async {
883                let mut client = service!(self, pb::state_service_client::StateServiceClient::new);
884                let response = client
885                    .get_ledger_end(pb::GetLedgerEndRequest {})
886                    .await?
887                    .into_inner();
888                Ok(response.offset)
889            })
890            .await
891        })
892        .await
893    }
894
895    /// Fetch the created and/or archived events for a contract by id
896    /// (`EventQueryService.GetEventsByContractId`), with verbose records and
897    /// no created-event blob. To obtain a contract's `created_event_blob`
898    /// (for disclosure), use a template-filtered
899    /// [`Self::active_contracts_with`] read with
900    /// [`ActiveContractsRequest::with_created_event_blobs`](crate::request::ActiveContractsRequest::with_created_event_blobs).
901    ///
902    /// # Errors
903    /// Returns an [`Error`] if authentication or the RPC fails.
904    pub async fn events_by_contract_id(
905        &self,
906        contract_id: impl Into<String>,
907        parties: Vec<String>,
908    ) -> Result<pb::GetEventsByContractIdResponse> {
909        let contract_id = contract_id.into();
910        telemetry::instrument("events_by_contract_id", TRANSPORT_GRPC, async move {
911            // Retried like the other reads: this is a lookup, so a transient
912            // failure is worth another attempt rather than a caller's error
913            // path. Only submissions have a reason to be careful here.
914            self.with_retry(|| {
915                let request = pb::GetEventsByContractIdRequest {
916                    contract_id: contract_id.clone(),
917                    event_format: Some(wildcard_event_format(&parties)),
918                };
919                async move {
920                    let mut client = service!(
921                        self,
922                        pb::event_query_service_client::EventQueryServiceClient::new
923                    );
924                    Ok(client
925                        .get_events_by_contract_id(request)
926                        .await?
927                        .into_inner())
928                }
929            })
930            .await
931        })
932        .await
933    }
934
935    /// Fetch one **page** of the Active Contract Set for `parties` as of
936    /// `active_at_offset`. Returns the page's active contracts and the next page
937    /// token (`None` once the last page has been read); pass the token back in
938    /// to fetch the following page.
939    ///
940    /// # Errors
941    /// Returns an [`Error`] if authentication or the RPC fails.
942    pub async fn active_contracts_page(
943        &self,
944        parties: Vec<String>,
945        active_at_offset: i64,
946        max_page_size: i32,
947        page_token: Option<Vec<u8>>,
948    ) -> Result<(Vec<pb::ActiveContract>, Option<Vec<u8>>)> {
949        let request = crate::request::ActiveContractsRequest::new(parties, active_at_offset);
950        self.active_contracts_page_with(&request, max_page_size, page_token)
951            .await
952    }
953
954    /// Like [`Self::active_contracts_page`], with the full request surface of
955    /// an [`ActiveContractsRequest`](crate::request::ActiveContractsRequest)
956    /// (template/interface filters, created-event blobs, non-verbose records).
957    ///
958    /// # Errors
959    /// Returns an [`Error`] if authentication or the RPC fails.
960    pub async fn active_contracts_page_with(
961        &self,
962        request: &crate::request::ActiveContractsRequest,
963        max_page_size: i32,
964        page_token: Option<Vec<u8>>,
965    ) -> Result<(Vec<pb::ActiveContract>, Option<Vec<u8>>)> {
966        let (entries, next) = self
967            .acs_page_with(request, max_page_size, page_token)
968            .await?;
969        Ok((
970            entries
971                .into_iter()
972                .filter_map(AcsEntry::into_active)
973                .collect(),
974            next,
975        ))
976    }
977
978    /// One page of the Active Contract Set for `parties`, **losslessly**: every
979    /// entry the participant sent, active or incomplete (see [`AcsEntry`]).
980    ///
981    /// # Errors
982    /// Returns an [`Error`] if authentication or the RPC fails.
983    pub async fn acs_page(
984        &self,
985        parties: Vec<String>,
986        active_at_offset: i64,
987        max_page_size: i32,
988        page_token: Option<Vec<u8>>,
989    ) -> Result<(Vec<AcsEntry>, Option<Vec<u8>>)> {
990        let request = crate::request::ActiveContractsRequest::new(parties, active_at_offset);
991        self.acs_page_with(&request, max_page_size, page_token)
992            .await
993    }
994
995    /// Like [`Self::acs_page`], with the full request surface of an
996    /// [`ActiveContractsRequest`](crate::request::ActiveContractsRequest).
997    ///
998    /// # Errors
999    /// Returns an [`Error`] if authentication or the RPC fails.
1000    pub async fn acs_page_with(
1001        &self,
1002        request: &crate::request::ActiveContractsRequest,
1003        max_page_size: i32,
1004        page_token: Option<Vec<u8>>,
1005    ) -> Result<(Vec<AcsEntry>, Option<Vec<u8>>)> {
1006        request.validate()?;
1007        telemetry::instrument("active_contracts_page", TRANSPORT_GRPC, async move {
1008            self.with_retry(|| {
1009                let page_request = pb::GetActiveContractsPageRequest {
1010                    active_at_offset: Some(request.active_at_offset),
1011                    event_format: Some(request.event_format()),
1012                    max_page_size: Some(max_page_size),
1013                    page_token: page_token.clone(),
1014                };
1015                async move {
1016                    let mut client =
1017                        service!(self, pb::state_service_client::StateServiceClient::new);
1018                    let response = client
1019                        .get_active_contracts_page(page_request)
1020                        .await?
1021                        .into_inner();
1022
1023                    let entries = response
1024                        .active_contracts
1025                        .into_iter()
1026                        .filter_map(|entry| acs_entry(entry.contract_entry))
1027                        .collect();
1028                    Ok((entries, response.next_page_token))
1029                }
1030            })
1031            .await
1032        })
1033        .await
1034    }
1035
1036    /// Fetch one **page** of updates in the offset range
1037    /// `(begin_offset_exclusive, end_offset_inclusive]`, optionally in reverse
1038    /// (`descending`) order. Returns the page items and the next page token
1039    /// (`None` once the last page has been read).
1040    ///
1041    /// # Errors
1042    /// Returns an [`Error`] if authentication or the RPC fails.
1043    pub async fn updates_page(
1044        &self,
1045        parties: Vec<String>,
1046        begin_offset_exclusive: i64,
1047        end_offset_inclusive: i64,
1048        max_page_size: i32,
1049        descending: bool,
1050        page_token: Option<Vec<u8>>,
1051    ) -> Result<(Vec<pb::GetUpdateResponse>, Option<Vec<u8>>)> {
1052        let mut request = crate::request::UpdatesRequest::new(parties, begin_offset_exclusive)
1053            .until(end_offset_inclusive);
1054        if descending {
1055            request = request.descending();
1056        }
1057        self.updates_page_with(&request, max_page_size, page_token)
1058            .await
1059    }
1060
1061    /// Like [`Self::updates_page`], with the full request surface of an
1062    /// [`UpdatesRequest`](crate::request::UpdatesRequest) (template/interface
1063    /// filters, transaction shape, descending order, created-event blobs,
1064    /// topology events, non-verbose records). The request's bounds supply the
1065    /// page range, so
1066    /// [`UpdatesRequest::until`](crate::request::UpdatesRequest::until) is
1067    /// required here — the paged read is inherently bounded.
1068    ///
1069    /// # Errors
1070    /// Returns [`Error::InvalidRequest`] if the request has no end offset, or
1071    /// another [`Error`] if authentication or the RPC fails.
1072    pub async fn updates_page_with(
1073        &self,
1074        request: &crate::request::UpdatesRequest,
1075        max_page_size: i32,
1076        page_token: Option<Vec<u8>>,
1077    ) -> Result<(Vec<pb::GetUpdateResponse>, Option<Vec<u8>>)> {
1078        request.validate()?;
1079        let (begin_exclusive, end_inclusive) = request.bounds();
1080        let Some(end_inclusive) = end_inclusive else {
1081            return Err(Error::InvalidRequest(
1082                "updates_page_with requires a bounded request: set UpdatesRequest::until"
1083                    .to_string(),
1084            ));
1085        };
1086        telemetry::instrument("updates_page", TRANSPORT_GRPC, async move {
1087            self.with_retry(|| {
1088                let page_request = pb::GetUpdatesPageRequest {
1089                    begin_offset_exclusive: Some(begin_exclusive),
1090                    end_offset_inclusive: Some(end_inclusive),
1091                    max_page_size: Some(max_page_size),
1092                    update_format: Some(request.update_format()),
1093                    descending_order: request.is_descending(),
1094                    page_token: page_token.clone(),
1095                };
1096                async move {
1097                    let mut client =
1098                        service!(self, pb::update_service_client::UpdateServiceClient::new);
1099                    let response = client.get_updates_page(page_request).await?.into_inner();
1100                    Ok((response.updates, response.next_page_token))
1101                }
1102            })
1103            .await
1104        })
1105        .await
1106    }
1107
1108    /// Stream the Active Contract Set for `parties` as of `active_at_offset`
1109    /// (typically the current ledger end). Yields the active contracts,
1110    /// wildcard-filtered to the given parties.
1111    ///
1112    /// # Errors
1113    /// Returns an [`Error`] if authentication or opening the stream fails.
1114    pub async fn active_contracts(
1115        &self,
1116        parties: Vec<String>,
1117        active_at_offset: i64,
1118    ) -> Result<impl Stream<Item = Result<pb::ActiveContract>> + Send + use<>> {
1119        self.active_contracts_with(crate::request::ActiveContractsRequest::new(
1120            parties,
1121            active_at_offset,
1122        ))
1123        .await
1124    }
1125
1126    /// Like [`Self::active_contracts`], with the full request surface: an
1127    /// [`ActiveContractsRequest`](crate::request::ActiveContractsRequest)
1128    /// additionally filters by template or interface, includes created-event
1129    /// blobs, and drops record labels.
1130    ///
1131    /// # Errors
1132    /// Returns an [`Error`] if authentication or opening the stream fails.
1133    pub async fn active_contracts_with(
1134        &self,
1135        request: crate::request::ActiveContractsRequest,
1136    ) -> Result<impl Stream<Item = Result<pb::ActiveContract>> + Send + use<>> {
1137        let stream = self.acs_entries_with(request).await?;
1138        Ok(stream.filter_map(|item| match item {
1139            Ok(entry) => entry.into_active().map(Ok),
1140            Err(err) => Some(Err(err)),
1141        }))
1142    }
1143
1144    /// Stream the Active Contract Set for `parties` **losslessly**: every entry
1145    /// the participant sends, active or incomplete (see [`AcsEntry`]).
1146    ///
1147    /// # Errors
1148    /// Returns an [`Error`] if authentication or opening the stream fails.
1149    pub async fn acs_entries(
1150        &self,
1151        parties: Vec<String>,
1152        active_at_offset: i64,
1153    ) -> Result<impl Stream<Item = Result<AcsEntry>> + Send + use<>> {
1154        self.acs_entries_with(crate::request::ActiveContractsRequest::new(
1155            parties,
1156            active_at_offset,
1157        ))
1158        .await
1159    }
1160
1161    /// Like [`Self::acs_entries`], with the full request surface of an
1162    /// [`ActiveContractsRequest`](crate::request::ActiveContractsRequest).
1163    ///
1164    /// # Errors
1165    /// Returns an [`Error`] if authentication or opening the stream fails.
1166    pub async fn acs_entries_with(
1167        &self,
1168        request: crate::request::ActiveContractsRequest,
1169    ) -> Result<impl Stream<Item = Result<AcsEntry>> + Send + use<>> {
1170        request.validate()?;
1171        telemetry::instrument("active_contracts", TRANSPORT_GRPC, async move {
1172            let mut client = service!(self, pb::state_service_client::StateServiceClient::new);
1173            let stream = client
1174                .get_active_contracts(pb::GetActiveContractsRequest {
1175                    active_at_offset: request.active_at_offset,
1176                    event_format: Some(request.event_format()),
1177                    stream_continuation_token: None,
1178                })
1179                .await?
1180                .into_inner();
1181
1182            let stream = stream.filter_map(|item| match item {
1183                Ok(response) => acs_entry(response.contract_entry).map(Ok),
1184                Err(status) => Some(Err(Error::from(status))),
1185            });
1186            Ok(telemetry::instrument_stream(
1187                "active_contracts",
1188                TRANSPORT_GRPC,
1189                stream,
1190            ))
1191        })
1192        .await
1193    }
1194
1195    /// Like [`Self::active_contracts`], but **resumable**: reads the ACS
1196    /// snapshot page-by-page (continuation tokens), retrying a failed page on
1197    /// retriable errors from the last token instead of restarting the snapshot
1198    /// from zero. `max_page_size` bounds each page RPC.
1199    pub fn active_contracts_resumable(
1200        &self,
1201        parties: Vec<String>,
1202        active_at_offset: i64,
1203        max_page_size: i32,
1204    ) -> impl Stream<Item = Result<pb::ActiveContract>> + Send + use<> {
1205        self.active_contracts_resumable_with(
1206            crate::request::ActiveContractsRequest::new(parties, active_at_offset),
1207            max_page_size,
1208        )
1209    }
1210
1211    /// Like [`Self::active_contracts_resumable`], with the full request
1212    /// surface of an
1213    /// [`ActiveContractsRequest`](crate::request::ActiveContractsRequest).
1214    pub fn active_contracts_resumable_with(
1215        &self,
1216        request: crate::request::ActiveContractsRequest,
1217        max_page_size: i32,
1218    ) -> impl Stream<Item = Result<pb::ActiveContract>> + Send + use<> {
1219        self.acs_entries_resumable_with(request, max_page_size)
1220            .filter_map(|item| match item {
1221                Ok(entry) => entry.into_active().map(Ok),
1222                Err(err) => Some(Err(err)),
1223            })
1224    }
1225
1226    /// Like [`Self::acs_entries`], but **resumable** and page-based: reads the
1227    /// snapshot page by page and retries a failed page from the last
1228    /// continuation token instead of restarting from zero. Every entry the
1229    /// participant sent is yielded (see [`AcsEntry`]).
1230    pub fn acs_entries_resumable(
1231        &self,
1232        parties: Vec<String>,
1233        active_at_offset: i64,
1234        max_page_size: i32,
1235    ) -> impl Stream<Item = Result<AcsEntry>> + Send + use<> {
1236        self.acs_entries_resumable_with(
1237            crate::request::ActiveContractsRequest::new(parties, active_at_offset),
1238            max_page_size,
1239        )
1240    }
1241
1242    /// Like [`Self::acs_entries_resumable`], with the full request surface of
1243    /// an [`ActiveContractsRequest`](crate::request::ActiveContractsRequest).
1244    pub fn acs_entries_resumable_with(
1245        &self,
1246        request: crate::request::ActiveContractsRequest,
1247        max_page_size: i32,
1248    ) -> impl Stream<Item = Result<AcsEntry>> + Send + use<> {
1249        let client = self.clone();
1250        let (max_reconnects, backoff_unit) = client.reconnect_policy();
1251        async_stream::stream! {
1252            let mut page_token: Option<Vec<u8>> = None;
1253            let mut reconnects = 0u32;
1254            loop {
1255                match client
1256                    .acs_page_with(
1257                        &request,
1258                        max_page_size,
1259                        page_token.clone(),
1260                    )
1261                    .await
1262                {
1263                    Ok((entries, next)) => {
1264                        reconnects = 0;
1265                        for entry in entries {
1266                            yield Ok(entry);
1267                        }
1268                        match next {
1269                            Some(next) => page_token = Some(next),
1270                            None => return, // snapshot fully delivered
1271                        }
1272                    }
1273                    Err(err) if err.is_retriable() => {
1274                        reconnects += 1;
1275                        if reconnects > max_reconnects {
1276                            // The participant's own failure, for the reason the
1277                            // update stream keeps it: the status and its
1278                            // classification are what the caller acts on.
1279                            tracing::warn!(
1280                                max_reconnects,
1281                                "acs page failed to resume; reporting the failure that caused it"
1282                            );
1283                            yield Err(err);
1284                            return;
1285                        }
1286                        tokio::time::sleep(backoff_unit * reconnects).await;
1287                    }
1288                    Err(err) => {
1289                        yield Err(err);
1290                        return;
1291                    }
1292                }
1293            }
1294        }
1295    }
1296
1297    /// The reconnect policy for resumable streams: `(max_reconnects,
1298    /// backoff_unit)`. Derived from the client's [`RetryConfig`] when one is
1299    /// configured (attempts → reconnect budget, initial backoff → step), else
1300    /// the defaults (5 reconnects, 250ms step).
1301    ///
1302    /// [`RetryConfig`]: canton_core::RetryConfig
1303    fn reconnect_policy(&self) -> (u32, Duration) {
1304        match self.config.retry() {
1305            Some(retry) => (retry.max_attempts, retry.initial_backoff),
1306            None => (5, Duration::from_millis(250)),
1307        }
1308    }
1309
1310    /// Stream ledger updates — transactions and reassignments — for `parties`,
1311    /// starting after `begin_offset` (exclusive). Offset checkpoints are
1312    /// filtered out. Reassignments are surfaced as their own case (each
1313    /// carrying the distinct `Unassigned`/`Assigned` events). Topology events
1314    /// are **not** included; ask for them with
1315    /// [`UpdatesRequest::with_topology_events`](crate::request::UpdatesRequest::with_topology_events)
1316    /// on [`Self::updates_with`].
1317    ///
1318    /// # Errors
1319    /// Returns an [`Error`] if authentication or opening the stream fails.
1320    pub async fn updates(
1321        &self,
1322        parties: Vec<String>,
1323        begin_offset: i64,
1324    ) -> Result<impl Stream<Item = Result<pb::get_updates_response::Update>> + Send + use<>> {
1325        self.updates_with(crate::request::UpdatesRequest::new(parties, begin_offset))
1326            .await
1327    }
1328
1329    /// Like [`Self::updates`], with the full request surface: an
1330    /// [`UpdatesRequest`](crate::request::UpdatesRequest) additionally bounds
1331    /// the stream at an end offset (`until` — the catch-up/sync form, after
1332    /// which the stream terminates), filters by template or interface, selects
1333    /// the ACS-delta shape, includes created-event blobs or topology events,
1334    /// and drops reassignments or record labels.
1335    ///
1336    /// # Errors
1337    /// Returns an [`Error`] if authentication or opening the stream fails.
1338    pub async fn updates_with(
1339        &self,
1340        request: crate::request::UpdatesRequest,
1341    ) -> Result<impl Stream<Item = Result<pb::get_updates_response::Update>> + Send + use<>> {
1342        let stream = self.updates_including_checkpoints(request).await?;
1343        Ok(stream.filter(|item| {
1344            !matches!(
1345                item,
1346                Ok(pb::get_updates_response::Update::OffsetCheckpoint(_))
1347            )
1348        }))
1349    }
1350
1351    /// The update stream with `OffsetCheckpoint` frames left in.
1352    ///
1353    /// A checkpoint is not an update anyone subscribed for, which is why
1354    /// [`Self::updates_with`] drops it. It is, however, the participant naming
1355    /// an offset that is safe to restart from — and it arrives on a quiet
1356    /// stream, where no transaction has moved the position for a while. The
1357    /// resumable path needs exactly that, so it reads this and filters for
1358    /// itself.
1359    async fn updates_including_checkpoints(
1360        &self,
1361        request: crate::request::UpdatesRequest,
1362    ) -> Result<impl Stream<Item = Result<pb::get_updates_response::Update>> + Send + use<>> {
1363        request.validate()?;
1364        let (_, end_inclusive) = request.bounds();
1365        if request.is_descending() && end_inclusive.is_none() {
1366            return Err(Error::InvalidRequest(
1367                "descending order requires a bounded request: set UpdatesRequest::until"
1368                    .to_string(),
1369            ));
1370        }
1371        telemetry::instrument("updates", TRANSPORT_GRPC, async move {
1372            let mut client = service!(self, pb::update_service_client::UpdateServiceClient::new);
1373            let stream = client.get_updates(request.into_grpc()).await?.into_inner();
1374
1375            let stream = stream.filter_map(|item| match item {
1376                Ok(response) => response.update.map(Ok),
1377                Err(status) => Some(Err(Error::from(status))),
1378            });
1379            // Instrumented for the life of the stream, not just its opening:
1380            // a subscription that fails an hour in is the failure that matters.
1381            Ok(telemetry::instrument_stream(
1382                "updates",
1383                TRANSPORT_GRPC,
1384                stream,
1385            ))
1386        })
1387        .await
1388    }
1389
1390    /// Like [`Self::updates`], but **resumable**: on a retriable stream error it
1391    /// reconnects from the last offset it *observed* (rather than restarting
1392    /// from `begin_offset` or losing position), with a short backoff and a
1393    /// bounded number of consecutive reconnects (see the client's
1394    /// [`RetryConfig`](canton_core::RetryConfig)).
1395    ///
1396    /// Observed includes the participant's `OffsetCheckpoint` frames, which are
1397    /// filtered out of what this yields but are the only thing that advances the
1398    /// resume point on a stream where nothing is happening. Once the reconnect
1399    /// budget is spent the stream yields **the failure that caused it** — the
1400    /// participant's status, details and retriable classification intact —
1401    /// rather than an error of the SDK's own.
1402    ///
1403    /// # Example
1404    /// ```no_run
1405    /// # async fn run(client: canton_ledger::CantonClient, party: String)
1406    /// #     -> canton_ledger::Result<()> {
1407    /// use tokio_stream::StreamExt as _;
1408    ///
1409    /// let stream = client.updates_resumable(vec![party], 0);
1410    /// tokio::pin!(stream);
1411    /// while let Some(update) = stream.next().await {
1412    ///     println!("update: {:?}", update?);
1413    /// }
1414    /// # Ok(()) }
1415    /// ```
1416    pub fn updates_resumable(
1417        &self,
1418        parties: Vec<String>,
1419        begin_offset: i64,
1420    ) -> impl Stream<Item = Result<pb::get_updates_response::Update>> + Send + use<> {
1421        self.updates_resumable_with(crate::request::UpdatesRequest::new(parties, begin_offset))
1422    }
1423
1424    /// Like [`Self::updates_resumable`], with the full request surface of an
1425    /// [`UpdatesRequest`](crate::request::UpdatesRequest). A bounded request
1426    /// (`until`) makes this a *resilient catch-up read*: reconnects resume
1427    /// from the last observed offset, and the stream ends once the participant
1428    /// closes it at the end offset.
1429    pub fn updates_resumable_with(
1430        &self,
1431        request: crate::request::UpdatesRequest,
1432    ) -> impl Stream<Item = Result<pb::get_updates_response::Update>> + Send + use<> {
1433        let client = self.clone();
1434        let (max_reconnects, backoff_unit) = client.reconnect_policy();
1435        async_stream::stream! {
1436            if let Err(err) = request.validate() {
1437                yield Err(err);
1438                return;
1439            }
1440            // Resume tracking assumes ascending offsets; a descending request
1441            // would silently re-read on every reconnect, so refuse it.
1442            if request.is_descending() {
1443                yield Err(Error::InvalidRequest(
1444                    "the resumable stream requires ascending order; use updates_with \
1445                     (bounded) for descending reads".to_string(),
1446                ));
1447                return;
1448            }
1449            let mut offset = request.begin_exclusive;
1450            let mut reconnects = 0u32;
1451            loop {
1452                // What made this reconnect necessary. Carried out of the inner
1453                // loop so that giving up reports the participant's own failure
1454                // rather than a fresh error of ours: the status, its structured
1455                // details, the correlation id and the retriable classification
1456                // are what an application branches on, and the moment the
1457                // stream gives up is when it needs them most.
1458                let cause = match client
1459                    .updates_including_checkpoints(request.resume_after(offset))
1460                    .await
1461                {
1462                    Ok(stream) => {
1463                        tokio::pin!(stream);
1464                        loop {
1465                            match stream.next().await {
1466                                Some(Ok(update)) => {
1467                                    // Checkpoints are included here: on a quiet
1468                                    // stream a checkpoint is the only thing that
1469                                    // moves the resume point, and after pruning
1470                                    // the older offset may not be servable at all.
1471                                    offset = update_offset(&update);
1472                                    reconnects = 0;
1473                                    if matches!(
1474                                        update,
1475                                        pb::get_updates_response::Update::OffsetCheckpoint(_)
1476                                    ) {
1477                                        continue;
1478                                    }
1479                                    yield Ok(update);
1480                                }
1481                                Some(Err(err)) if err.is_retriable() => break err,
1482                                Some(Err(err)) => {
1483                                    yield Err(err);
1484                                    return;
1485                                }
1486                                None => return, // server closed the stream cleanly
1487                            }
1488                        }
1489                    }
1490                    Err(err) if err.is_retriable() => err,
1491                    Err(err) => {
1492                        yield Err(err);
1493                        return;
1494                    }
1495                };
1496
1497                reconnects += 1;
1498                if reconnects > max_reconnects {
1499                    tracing::warn!(
1500                        max_reconnects,
1501                        offset,
1502                        "update stream gave up resuming; reporting the failure that caused it"
1503                    );
1504                    yield Err(cause);
1505                    return;
1506                }
1507                tokio::time::sleep(backoff_unit * reconnects).await;
1508            }
1509        }
1510    }
1511}
1512
1513#[cfg(test)]
1514mod tests {
1515    use super::CantonClient;
1516    use crate::Config;
1517    use canton_core::Error;
1518
1519    // Compile-time guarantee that the client is Send + Sync, so consumers can
1520    // share it across tasks and `tokio::spawn` work that holds it. (The streaming
1521    // methods carry `+ Send` in their signatures, which the compiler enforces at
1522    // this crate's build time.)
1523    #[test]
1524    fn client_is_send_and_sync() {
1525        fn assert_send_sync<T: Send + Sync>() {}
1526        assert_send_sync::<CantonClient>();
1527    }
1528
1529    #[tokio::test]
1530    async fn connect_lazy_accepts_a_valid_endpoint() {
1531        // connect_lazy builds a lazy Channel, which needs a Tokio runtime.
1532        assert!(CantonClient::connect_lazy(Config::new("http://localhost:3901")).is_ok());
1533    }
1534
1535    #[tokio::test]
1536    async fn connect_lazy_rejects_a_malformed_endpoint() {
1537        for bad in ["", "not a uri", "http://[bad"] {
1538            let result = CantonClient::connect_lazy(Config::new(bad));
1539            assert!(
1540                matches!(result, Err(Error::InvalidRequest(_))),
1541                "endpoint {bad:?} should be rejected as InvalidRequest, got {result:?}"
1542            );
1543        }
1544    }
1545
1546    // The paged read is inherently bounded, so a request without an end offset
1547    // must be refused before any RPC is attempted.
1548    // Every one of these is refused before a connection is even attempted: the
1549    // client is pointed at a port nothing listens on, so anything that reaches
1550    // the wire fails differently.
1551    #[tokio::test]
1552    async fn requests_the_participant_would_refuse_are_refused_here() {
1553        use crate::request::{ActiveContractsRequest, CompletionsRequest, UpdatesRequest};
1554
1555        let Ok(client) = CantonClient::connect_lazy(Config::new("http://localhost:1")) else {
1556            panic!("lazy connect accepts a valid endpoint");
1557        };
1558        let party = || vec!["alice".to_string()];
1559        let invalid = |result: &Result<_, Error>| matches!(result, Err(Error::InvalidRequest(_)));
1560
1561        // Negative offsets.
1562        assert!(invalid(
1563            &client
1564                .updates_with(UpdatesRequest::new(party(), -1))
1565                .await
1566                .map(|_| ())
1567        ));
1568        assert!(invalid(
1569            &client
1570                .acs_entries_with(ActiveContractsRequest::new(party(), -5))
1571                .await
1572                .map(|_| ())
1573        ));
1574        assert!(invalid(
1575            &client
1576                .completions_with(CompletionsRequest::new(party(), -1))
1577                .await
1578                .map(|_| ())
1579        ));
1580
1581        // A range that ends before it begins.
1582        assert!(invalid(
1583            &client
1584                .updates_with(UpdatesRequest::new(party(), 100).until(50))
1585                .await
1586                .map(|_| ())
1587        ));
1588
1589        // No parties: a subscription that looks alive and can never yield.
1590        assert!(invalid(
1591            &client
1592                .updates_with(UpdatesRequest::new(vec![], 0))
1593                .await
1594                .map(|_| ())
1595        ));
1596    }
1597
1598    #[tokio::test]
1599    async fn submissions_that_cannot_succeed_are_refused_here() {
1600        use crate::command::Submit;
1601
1602        let Ok(client) = CantonClient::connect_lazy(Config::new("http://localhost:1")) else {
1603            panic!("lazy connect accepts a valid endpoint");
1604        };
1605
1606        // No commands: a round trip to be told nothing was asked for.
1607        let empty = client.submission(Submit::new("alice"));
1608        assert!(matches!(
1609            empty.submit().await,
1610            Err(Error::InvalidRequest(_))
1611        ));
1612
1613        // The two minimum-ledger-time forms are mutually exclusive.
1614        let both = client.submission(
1615            Submit::new("alice")
1616                .add_command(crate::command::create(
1617                    crate::command::identifier("pkg", "M", "T"),
1618                    crate::command::record(vec![]),
1619                ))
1620                .with_min_ledger_time_rel(std::time::Duration::from_secs(1))
1621                .with_min_ledger_time_abs(prost_types::Timestamp::default()),
1622        );
1623        assert!(matches!(both.submit().await, Err(Error::InvalidRequest(_))));
1624    }
1625
1626    #[tokio::test]
1627    async fn updates_page_with_requires_a_bounded_request() {
1628        let Ok(client) = CantonClient::connect_lazy(Config::new("http://localhost:1")) else {
1629            panic!("lazy connect accepts a valid endpoint");
1630        };
1631        let unbounded = crate::request::UpdatesRequest::new(vec!["p".to_string()], 0);
1632        let result = client.updates_page_with(&unbounded, 10, None).await;
1633        assert!(
1634            matches!(result, Err(Error::InvalidRequest(_))),
1635            "expected InvalidRequest, got {result:?}"
1636        );
1637    }
1638
1639    // Descending order needs an end offset (streams), and never composes with
1640    // resume tracking — both must fail fast, before any RPC.
1641    #[tokio::test]
1642    async fn descending_requires_bounds_and_is_refused_on_resumable() {
1643        use tokio_stream::StreamExt as _;
1644
1645        let Ok(client) = CantonClient::connect_lazy(Config::new("http://localhost:1")) else {
1646            panic!("lazy connect accepts a valid endpoint");
1647        };
1648        let descending_unbounded =
1649            crate::request::UpdatesRequest::new(vec!["p".to_string()], 0).descending();
1650
1651        let result = client.updates_with(descending_unbounded.clone()).await;
1652        assert!(matches!(result.err(), Some(Error::InvalidRequest(_))));
1653
1654        let stream = client.updates_resumable_with(descending_unbounded.until(10));
1655        tokio::pin!(stream);
1656        let Some(first) = stream.next().await else {
1657            panic!("the resumable stream should yield the rejection");
1658        };
1659        assert!(matches!(first, Err(Error::InvalidRequest(_))));
1660    }
1661
1662    // A reassignment carries its `Unassigned` (source) and `Assigned` (target)
1663    // events as distinct cases rather than collapsing them into one "reassign"
1664    // event — the multi-synchronizer faithfulness the update stream promises.
1665    // (LocalNet is single-synchronizer, so this is verified structurally.)
1666    #[test]
1667    fn reassignment_preserves_the_unassigned_assigned_split() {
1668        use super::pb;
1669        use pb::get_updates_response::Update;
1670        use pb::reassignment_event::Event;
1671
1672        let update = Update::Reassignment(pb::Reassignment {
1673            update_id: "u1".to_string(),
1674            events: vec![
1675                pb::ReassignmentEvent {
1676                    event: Some(Event::Unassigned(pb::UnassignedEvent::default())),
1677                },
1678                pb::ReassignmentEvent {
1679                    event: Some(Event::Assigned(pb::AssignedEvent::default())),
1680                },
1681            ],
1682            ..Default::default()
1683        });
1684
1685        let Update::Reassignment(reassignment) = update else {
1686            panic!("expected a reassignment update");
1687        };
1688        assert_eq!(reassignment.events.len(), 2, "both legs are surfaced");
1689        assert!(matches!(
1690            reassignment.events[0].event,
1691            Some(Event::Unassigned(_))
1692        ));
1693        assert!(matches!(
1694            reassignment.events[1].event,
1695            Some(Event::Assigned(_))
1696        ));
1697    }
1698}