daml-grpc 0.3.0

Daml Ledger GRPC API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::convert::TryFrom;

use chrono::{DateTime, Utc};

use crate::data::{DamlError, DamlResult};
use crate::grpc_protobuf::com::daml::ledger::api::v2::admin::upload_dar_file_request::VettingChange;
use crate::grpc_protobuf::com::daml::ledger::api::v2::admin::vetted_packages_change::{Operation, Unvet, Vet};
use crate::grpc_protobuf::com::daml::ledger::api::v2::admin::{
    PackageDetails, UpdateVettedPackagesForceFlag, VettedPackagesChange, VettedPackagesRef,
};
use crate::grpc_protobuf::com::daml::ledger::api::v2::prior_topology_serial::Serial;
use crate::grpc_protobuf::com::daml::ledger::api::v2::{
    GetPackageResponse, HashFunction, PackageMetadataFilter, PackageReference, PackageStatus, PriorTopologySerial,
    TopologyStateFilter, VettedPackage, VettedPackages,
};
use crate::util;
use crate::util::Required;

/// The contents of a Daml-LF package returned by `PackageService.GetPackage`.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DamlPackage {
    payload: Vec<u8>,
    hash: String,
    hash_function: DamlHashFunction,
}

impl DamlPackage {
    pub fn new(
        payload: impl Into<Vec<u8>>,
        hash: impl Into<String>,
        hash_function: impl Into<DamlHashFunction>,
    ) -> Self {
        Self {
            payload: payload.into(),
            hash: hash.into(),
            hash_function: hash_function.into(),
        }
    }

    pub fn payload(&self) -> &[u8] {
        &self.payload
    }

    pub fn take_payload(self) -> Vec<u8> {
        self.payload
    }

    pub fn hash(&self) -> &str {
        &self.hash
    }

    pub const fn hash_function(&self) -> &DamlHashFunction {
        &self.hash_function
    }
}

impl TryFrom<GetPackageResponse> for DamlPackage {
    type Error = DamlError;

    fn try_from(response: GetPackageResponse) -> DamlResult<Self> {
        Ok(Self::new(
            response.archive_payload,
            response.hash,
            HashFunction::try_from(response.hash_function).ok().req()?,
        ))
    }
}

/// Whether the participant has registered a given Daml-LF package and is
/// willing to interpret commands that reference it.
///
/// v2 renamed v1's `PACKAGE_STATUS_UNKNOWN` to `PACKAGE_STATUS_UNSPECIFIED`;
/// the semantics ("the participant doesn't know about this package") are
/// unchanged.
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum DamlPackageStatus {
    Unspecified,
    Registered,
}

impl From<PackageStatus> for DamlPackageStatus {
    fn from(status: PackageStatus) -> Self {
        match status {
            PackageStatus::Unspecified => DamlPackageStatus::Unspecified,
            PackageStatus::Registered => DamlPackageStatus::Registered,
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum DamlHashFunction {
    Sha256,
}

impl From<HashFunction> for DamlHashFunction {
    fn from(hash_function: HashFunction) -> Self {
        match hash_function {
            HashFunction::Sha256 => DamlHashFunction::Sha256,
        }
    }
}

/// Detailed information about a Daml DAR package, as reported by
/// `PackageManagementService.ListKnownPackages`.
///
/// v2 dropped `source_description` and added `name` and `version`, which are
/// drawn from the package metadata inside the DAR.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DamlPackageDetails {
    pub package_id: String,
    pub package_size: u64,
    pub known_since: DateTime<Utc>,
    pub name: String,
    pub version: String,
}

impl TryFrom<PackageDetails> for DamlPackageDetails {
    type Error = DamlError;

    fn try_from(details: PackageDetails) -> DamlResult<Self> {
        Ok(Self {
            package_id: details.package_id,
            package_size: details.package_size,
            known_since: util::from_grpc_timestamp(&details.known_since.req()?)?,
            name: details.name,
            version: details.version,
        })
    }
}

/// A reference to a Daml-LF package by id + name + version. Returned in v2
/// alongside many event/contract messages so clients can resolve interface
/// implementations and version constraints without a separate lookup.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DamlPackageReference {
    pub package_id: String,
    pub package_name: String,
    pub package_version: String,
}

impl From<PackageReference> for DamlPackageReference {
    fn from(r: PackageReference) -> Self {
        Self {
            package_id: r.package_id,
            package_name: r.package_name,
            package_version: r.package_version,
        }
    }
}

/// A single vetted package as reported by
/// `PackageService.ListVettedPackages`. Vetting bounds (`valid_from_inclusive`,
/// `valid_until_exclusive`) are open intervals when `None`.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DamlVettedPackage {
    pub package_id: String,
    pub valid_from_inclusive: Option<DateTime<Utc>>,
    pub valid_until_exclusive: Option<DateTime<Utc>>,
    pub package_name: Option<String>,
    pub package_version: Option<String>,
}

impl TryFrom<VettedPackage> for DamlVettedPackage {
    type Error = DamlError;

    fn try_from(p: VettedPackage) -> DamlResult<Self> {
        Ok(Self {
            package_id: p.package_id,
            valid_from_inclusive: p.valid_from_inclusive.as_ref().map(util::from_grpc_timestamp).transpose()?,
            valid_until_exclusive: p.valid_until_exclusive.as_ref().map(util::from_grpc_timestamp).transpose()?,
            // The proto marks these as required-when-present at the participant.
            // We surface "" as None so callers don't have to disambiguate empty
            // strings from "field actually missing".
            package_name: Some(p.package_name).filter(|s| !s.is_empty()),
            package_version: Some(p.package_version).filter(|s| !s.is_empty()),
        })
    }
}

/// A list of packages vetted on a given participant + synchronizer pair.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DamlVettedPackages {
    pub packages: Vec<DamlVettedPackage>,
    pub participant_id: String,
    pub synchronizer_id: String,
    pub topology_serial: u32,
}

impl TryFrom<VettedPackages> for DamlVettedPackages {
    type Error = DamlError;

    fn try_from(v: VettedPackages) -> DamlResult<Self> {
        Ok(Self {
            packages: v.packages.into_iter().map(DamlVettedPackage::try_from).collect::<DamlResult<Vec<_>>>()?,
            participant_id: v.participant_id,
            synchronizer_id: v.synchronizer_id,
            topology_serial: v.topology_serial,
        })
    }
}

/// Filter for `ListVettedPackages` by package metadata.
///
/// Both fields are OR-combined: a package matches if its id appears in
/// `package_ids` *or* its name starts with one of `package_name_prefixes`.
/// An empty filter matches every vetted package.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DamlPackageMetadataFilter {
    pub package_ids: Vec<String>,
    pub package_name_prefixes: Vec<String>,
}

impl From<DamlPackageMetadataFilter> for PackageMetadataFilter {
    fn from(f: DamlPackageMetadataFilter) -> Self {
        Self {
            package_ids: f.package_ids,
            package_name_prefixes: f.package_name_prefixes,
        }
    }
}

/// Filter for `ListVettedPackages` by participant + synchronizer topology.
///
/// Both fields are AND-combined when set: a package matches only if it is
/// hosted on one of `participant_ids` *and* vetted on one of
/// `synchronizer_ids`. An empty filter matches every topology pair.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DamlTopologyStateFilter {
    pub participant_ids: Vec<String>,
    pub synchronizer_ids: Vec<String>,
}

impl From<DamlTopologyStateFilter> for TopologyStateFilter {
    fn from(f: DamlTopologyStateFilter) -> Self {
        Self {
            participant_ids: f.participant_ids,
            synchronizer_ids: f.synchronizer_ids,
        }
    }
}

/// A single page of `ListVettedPackages` results. Carries the cursor
/// required to fetch the next page; an empty `next_page_token` means there
/// are no more pages.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DamlVettedPackagesPage {
    pub vetted_packages: Vec<DamlVettedPackages>,
    pub next_page_token: String,
}

// ---------------------------------------------------------------------------
// PackageManagementService: DAR upload + vetting administration
// ---------------------------------------------------------------------------

/// How the participant should treat packages contained in an uploaded DAR
/// w.r.t. vetting. `Unspecified` defers to the server default, which is
/// "vet everything".
#[derive(Debug, Eq, PartialEq, Clone, Copy, Default)]
pub enum DamlVettingChange {
    #[default]
    Unspecified,
    VetAllPackages,
    DontVetAnyPackages,
}

impl From<DamlVettingChange> for VettingChange {
    fn from(v: DamlVettingChange) -> Self {
        match v {
            DamlVettingChange::Unspecified => VettingChange::Unspecified,
            DamlVettingChange::VetAllPackages => VettingChange::VetAllPackages,
            DamlVettingChange::DontVetAnyPackages => VettingChange::DontVetAnyPackages,
        }
    }
}

/// A reference matching one or more vetted packages. At least one of
/// `package_id` or `package_name` must be set; empty fields act as
/// wildcards. `Vet` operations require unique matches; `Unvet`
/// operations may match multiple.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DamlVettedPackagesRef {
    pub package_id: String,
    pub package_name: String,
    pub package_version: String,
}

impl From<DamlVettedPackagesRef> for VettedPackagesRef {
    fn from(r: DamlVettedPackagesRef) -> Self {
        Self {
            package_id: r.package_id,
            package_name: r.package_name,
            package_version: r.package_version,
        }
    }
}

/// One step in an `UpdateVettedPackages` request. `Vet` adds or extends
/// vetting bounds; `Unvet` removes packages from the vetted set. Changes
/// are applied in order and either all succeed or all fail.
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum DamlVettedPackagesChange {
    Vet {
        packages: Vec<DamlVettedPackagesRef>,
        /// `None` removes the lower bound (vetted from the beginning of
        /// time); `Some(t)` overwrites any prior lower bound.
        new_valid_from_inclusive: Option<DateTime<Utc>>,
        /// `None` removes the upper bound (vetted indefinitely);
        /// `Some(t)` overwrites any prior upper bound.
        new_valid_until_exclusive: Option<DateTime<Utc>>,
    },
    Unvet {
        packages: Vec<DamlVettedPackagesRef>,
    },
}

impl TryFrom<DamlVettedPackagesChange> for VettedPackagesChange {
    type Error = DamlError;

    fn try_from(c: DamlVettedPackagesChange) -> DamlResult<Self> {
        let op = match c {
            DamlVettedPackagesChange::Vet {
                packages,
                new_valid_from_inclusive,
                new_valid_until_exclusive,
            } => Operation::Vet(Vet {
                packages: packages.into_iter().map(Into::into).collect(),
                new_valid_from_inclusive: new_valid_from_inclusive.map(util::to_grpc_timestamp).transpose()?,
                new_valid_until_exclusive: new_valid_until_exclusive.map(util::to_grpc_timestamp).transpose()?,
            }),
            DamlVettedPackagesChange::Unvet {
                packages,
            } => Operation::Unvet(Unvet {
                packages: packages.into_iter().map(Into::into).collect(),
            }),
        };
        Ok(Self {
            operation: Some(op),
        })
    }
}

/// Concurrency-control token for `UpdateVettedPackages`. The participant
/// rejects the update if its current topology-transaction serial doesn't
/// match the supplied value.
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum DamlPriorTopologySerial {
    /// The participant must currently be at this serial.
    Prior(u32),
    /// The participant must have no prior vetting transactions on this
    /// (participant, synchronizer) pair.
    NoPrior,
}

impl From<DamlPriorTopologySerial> for PriorTopologySerial {
    fn from(s: DamlPriorTopologySerial) -> Self {
        let serial = match s {
            DamlPriorTopologySerial::Prior(v) => Serial::Prior(v),
            DamlPriorTopologySerial::NoPrior => Serial::NoPrior(()),
        };
        Self {
            serial: Some(serial),
        }
    }
}

/// Opt-in escapes for vetting updates that are normally rejected because
/// they would compromise upgrade safety. Use sparingly.
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum DamlUpdateVettedPackagesForceFlag {
    AllowVetIncompatibleUpgrades,
    AllowUnvettedDependencies,
}

impl From<DamlUpdateVettedPackagesForceFlag> for i32 {
    fn from(f: DamlUpdateVettedPackagesForceFlag) -> Self {
        let v: UpdateVettedPackagesForceFlag = f.into();
        v as i32
    }
}

impl From<DamlUpdateVettedPackagesForceFlag> for UpdateVettedPackagesForceFlag {
    fn from(f: DamlUpdateVettedPackagesForceFlag) -> Self {
        match f {
            DamlUpdateVettedPackagesForceFlag::AllowVetIncompatibleUpgrades => {
                UpdateVettedPackagesForceFlag::AllowVetIncompatibleUpgrades
            },
            DamlUpdateVettedPackagesForceFlag::AllowUnvettedDependencies => {
                UpdateVettedPackagesForceFlag::AllowUnvettedDependencies
            },
        }
    }
}

/// Outcome of `UpdateVettedPackages`. `past_vetted_packages` is `None`
/// when no prior vetting topology existed for this participant on the
/// target synchronizer.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DamlUpdateVettedPackagesOutcome {
    pub past_vetted_packages: Option<DamlVettedPackages>,
    pub new_vetted_packages: Option<DamlVettedPackages>,
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Each `DamlVettingChange` must map to a distinct proto enum
    /// value. Collisions would silently downgrade a vet operation
    /// (e.g. `VetAllPackages` -> `Unspecified` would defer to the
    /// server default unintentionally).
    #[test]
    fn vetting_change_variants_are_injective() {
        let unspecified: VettingChange = DamlVettingChange::Unspecified.into();
        let vet: VettingChange = DamlVettingChange::VetAllPackages.into();
        let dont_vet: VettingChange = DamlVettingChange::DontVetAnyPackages.into();
        assert_ne!(unspecified as i32, vet as i32);
        assert_ne!(unspecified as i32, dont_vet as i32);
        assert_ne!(vet as i32, dont_vet as i32);
    }

    #[test]
    fn force_flag_variants_are_injective() {
        let incompatible: i32 = DamlUpdateVettedPackagesForceFlag::AllowVetIncompatibleUpgrades.into();
        let unvetted: i32 = DamlUpdateVettedPackagesForceFlag::AllowUnvettedDependencies.into();
        assert_ne!(incompatible, unvetted);
    }

    /// `Prior(7)` vs `NoPrior` must remain distinguishable on the
    /// wire — confusing them with each other would either reject
    /// every update (`NoPrior` on a participant with prior history)
    /// or accept any update (Prior matched against the wrong
    /// serial).
    #[test]
    fn prior_topology_serial_variants_distinct() {
        let prior: PriorTopologySerial = DamlPriorTopologySerial::Prior(7).into();
        let no_prior: PriorTopologySerial = DamlPriorTopologySerial::NoPrior.into();
        match prior.serial {
            Some(Serial::Prior(v)) => assert_eq!(v, 7),
            other => panic!("Prior(7) lost on the way to proto: {other:?}"),
        }
        match no_prior.serial {
            Some(Serial::NoPrior(())) => {},
            other => panic!("NoPrior lost on the way to proto: {other:?}"),
        }
    }

    #[test]
    fn vetted_packages_ref_roundtrips_to_proto() {
        let dto = DamlVettedPackagesRef {
            package_id: "abc123".to_owned(),
            package_name: "my-pkg".to_owned(),
            package_version: "1.0.0".to_owned(),
        };
        let proto: VettedPackagesRef = dto.clone().into();
        assert_eq!(proto.package_id, dto.package_id);
        assert_eq!(proto.package_name, dto.package_name);
        assert_eq!(proto.package_version, dto.package_version);
    }

    #[test]
    fn vetted_packages_change_vet_serialises() {
        use chrono::TimeZone;

        let from = chrono::Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
        let until = chrono::Utc.with_ymd_and_hms(2027, 1, 1, 0, 0, 0).unwrap();
        let dto = DamlVettedPackagesChange::Vet {
            packages: vec![DamlVettedPackagesRef {
                package_id: "abc".to_owned(),
                package_name: "p".to_owned(),
                package_version: "0.1.0".to_owned(),
            }],
            new_valid_from_inclusive: Some(from),
            new_valid_until_exclusive: Some(until),
        };
        let proto: VettedPackagesChange = dto.try_into().expect("convert");
        match proto.operation {
            Some(Operation::Vet(v)) => {
                assert_eq!(v.packages.len(), 1);
                assert_eq!(v.packages[0].package_id, "abc");
                assert!(v.new_valid_from_inclusive.is_some(), "lower bound must be set");
                assert!(v.new_valid_until_exclusive.is_some(), "upper bound must be set");
            },
            other => panic!("Vet variant lost: {other:?}"),
        }
    }

    #[test]
    fn vetted_packages_change_unvet_serialises() {
        let dto = DamlVettedPackagesChange::Unvet {
            packages: vec![DamlVettedPackagesRef::default()],
        };
        let proto: VettedPackagesChange = dto.try_into().expect("convert");
        assert!(matches!(proto.operation, Some(Operation::Unvet(_))));
    }
}