ic-query 0.30.12

Internet Computer query library for NNS, SNS, ICRC, system canisters, and public network metadata
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
//! Module: nns::registry::replay::session
//!
//! Responsibility: coordinate exact-target replay and compact provenance commitments.
//! Does not own: source calls, persistence, catalog projection, or assurance promotion.
//! Boundary: the first batch pins the target; provenance advances only after atomic application.

use super::{
    NnsRegistryReplayError, NnsRegistryReplayLimits, NnsRegistryReplayProgress,
    NnsRegistryReplayState, apply_validated_batch_through, validated_batch_prefix_counts,
};
use crate::nns::registry::{
    NnsCertifiedRegistryDeltaBatchReport, NnsCertifiedRegistryDeltaBatchRequest,
    validate_nns_certified_registry_delta_batch,
};
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeSet,
    io::{self, Write},
};

const EVIDENCE_CHAIN_DOMAIN: &[u8] = b"ic-query:nns-certified-registry-evidence-chain:v1\0";
const COMPLETE_STATE_DOMAIN: &[u8] = b"ic-query:nns-certified-registry-state:v1\0";

/// Version of the replay evidence-chain and complete-state digest contracts.
pub const NNS_REGISTRY_REPLAY_PROVENANCE_SCHEMA_VERSION: u32 = 1;

///
/// NnsRegistryReplaySessionLimits
///
/// Explicit cumulative evidence and state ceilings for one exact-target replay session.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct NnsRegistryReplaySessionLimits {
    /// Maximum Registry version that may be selected from the first batch.
    pub max_registry_versions: u64,
    /// Maximum certified delta batches admitted by the session.
    pub max_batches: u64,
    /// Maximum reported Registry query calls across admitted batches.
    pub max_query_calls: u64,
    /// Maximum reported encoded response bytes across admitted batches.
    pub max_response_bytes: u64,
    /// Payload ceilings enforced on the reconstructed current state.
    pub state: NnsRegistryReplayLimits,
}

impl NnsRegistryReplaySessionLimits {
    /// Create explicit session ceilings without choosing hidden bootstrap defaults.
    #[must_use]
    pub const fn new(
        max_registry_versions: u64,
        max_batches: u64,
        max_query_calls: u64,
        max_response_bytes: u64,
        state: NnsRegistryReplayLimits,
    ) -> Self {
        Self {
            max_registry_versions,
            max_batches,
            max_query_calls,
            max_response_bytes,
            state,
        }
    }
}

///
/// NnsRegistryReplaySession
///
/// Pure cumulative replay coordination pinned to the first batch's certified latest version.
///

#[derive(Debug, Eq, PartialEq)]
pub struct NnsRegistryReplaySession {
    limits: NnsRegistryReplaySessionLimits,
    state: NnsRegistryReplayState,
    selected_version: Option<u64>,
    highest_certified_latest_version: Option<u64>,
    root_key_digest: Option<String>,
    source_endpoints: BTreeSet<String>,
    minimum_certificate_time_nanos: Option<u64>,
    maximum_certificate_time_nanos: Option<u64>,
    evidence_chain_digest: Option<[u8; 32]>,
    complete_state_digest: Option<[u8; 32]>,
    batch_count: u64,
    query_call_count: u64,
    response_bytes: u64,
    applied_mutation_count: u64,
}

impl NnsRegistryReplaySession {
    /// Create a version-zero replay session with explicit cumulative limits.
    #[must_use]
    pub const fn new(limits: NnsRegistryReplaySessionLimits) -> Self {
        Self {
            limits,
            state: NnsRegistryReplayState::new(),
            selected_version: None,
            highest_certified_latest_version: None,
            root_key_digest: None,
            source_endpoints: BTreeSet::new(),
            minimum_certificate_time_nanos: None,
            maximum_certificate_time_nanos: None,
            evidence_chain_digest: None,
            complete_state_digest: None,
            batch_count: 0,
            query_call_count: 0,
            response_bytes: 0,
            applied_mutation_count: 0,
        }
    }

    /// Validate and atomically admit one caller-supplied certified delta batch.
    ///
    /// The first accepted batch selects its certified latest Registry version.
    /// Later reports may observe a newer latest version, but mutations after the
    /// selected version are not applied.
    pub fn apply_batch(
        &mut self,
        request: &NnsCertifiedRegistryDeltaBatchRequest,
        report: &NnsCertifiedRegistryDeltaBatchReport,
    ) -> Result<NnsRegistryReplayProgress, NnsRegistryReplayError> {
        validate_nns_certified_registry_delta_batch(request, report)?;
        if self.state.through_version() != report.requested_version {
            return Err(NnsRegistryReplayError::VersionMismatch {
                state_version: self.state.through_version(),
                requested_version: report.requested_version,
            });
        }
        if let Some(selected_version) = self.selected_version {
            if self.state.through_version() == selected_version {
                return Err(NnsRegistryReplayError::SessionComplete { selected_version });
            }
            if report.certified_latest_version < selected_version {
                return Err(NnsRegistryReplayError::CertifiedVersionRegressed {
                    selected_version,
                    certified_latest_version: report.certified_latest_version,
                });
            }
        }
        if let Some(expected_root_key_digest) = &self.root_key_digest
            && expected_root_key_digest != &report.certification.root_key_digest
        {
            return Err(NnsRegistryReplayError::RootKeyDigestMismatch {
                expected_root_key_digest: expected_root_key_digest.clone(),
                actual_root_key_digest: report.certification.root_key_digest.clone(),
            });
        }

        let selected_version = self
            .selected_version
            .unwrap_or(report.certified_latest_version);
        enforce_session_limit(
            "selected Registry versions",
            selected_version,
            self.limits.max_registry_versions,
        )?;
        let candidate_batch_count = checked_add(self.batch_count, 1)?;
        enforce_session_limit(
            "batch count",
            candidate_batch_count,
            self.limits.max_batches,
        )?;
        let candidate_query_call_count =
            checked_add(self.query_call_count, report.query_call_count)?;
        enforce_session_limit(
            "query call count",
            candidate_query_call_count,
            self.limits.max_query_calls,
        )?;
        let report_response_bytes =
            u64::try_from(report.response_bytes).map_err(|_| NnsRegistryReplayError::Accounting)?;
        let candidate_response_bytes = checked_add(self.response_bytes, report_response_bytes)?;
        enforce_session_limit(
            "response bytes",
            candidate_response_bytes,
            self.limits.max_response_bytes,
        )?;
        let (_, batch_applied_mutation_count) =
            validated_batch_prefix_counts(report, selected_version)?;
        let batch_applied_mutation_count = u64::try_from(batch_applied_mutation_count)
            .map_err(|_| NnsRegistryReplayError::Accounting)?;
        let candidate_applied_mutation_count =
            checked_add(self.applied_mutation_count, batch_applied_mutation_count)?;
        let candidate_provenance = self.candidate_provenance(report)?;

        let progress = apply_validated_batch_through(
            &mut self.state,
            report,
            selected_version,
            self.limits.state,
        )?;
        self.applied_mutation_count = candidate_applied_mutation_count;
        self.selected_version = Some(selected_version);
        self.highest_certified_latest_version = Some(
            self.highest_certified_latest_version
                .map_or(report.certified_latest_version, |version| {
                    version.max(report.certified_latest_version)
                }),
        );
        self.root_key_digest = Some(report.certification.root_key_digest.clone());
        self.publish_provenance(candidate_provenance, selected_version);
        self.batch_count = candidate_batch_count;
        self.query_call_count = candidate_query_call_count;
        self.response_bytes = candidate_response_bytes;
        Ok(progress)
    }

    fn candidate_provenance(
        &self,
        report: &NnsCertifiedRegistryDeltaBatchReport,
    ) -> Result<SessionProvenanceCandidate, NnsRegistryReplayError> {
        let evidence_chain_digest = chained_evidence_digest(self.evidence_chain_digest, report)?;
        let mut source_endpoints = self.source_endpoints.clone();
        source_endpoints.insert(report.source_endpoint.clone());
        let certificate_time_nanos = report.certification.certificate_time_nanos;
        Ok(SessionProvenanceCandidate {
            source_endpoints,
            minimum_certificate_time_nanos: self
                .minimum_certificate_time_nanos
                .map_or(certificate_time_nanos, |time| {
                    time.min(certificate_time_nanos)
                }),
            maximum_certificate_time_nanos: self
                .maximum_certificate_time_nanos
                .map_or(certificate_time_nanos, |time| {
                    time.max(certificate_time_nanos)
                }),
            evidence_chain_digest,
        })
    }

    fn publish_provenance(&mut self, candidate: SessionProvenanceCandidate, selected_version: u64) {
        self.source_endpoints = candidate.source_endpoints;
        self.minimum_certificate_time_nanos = Some(candidate.minimum_certificate_time_nanos);
        self.maximum_certificate_time_nanos = Some(candidate.maximum_certificate_time_nanos);
        self.evidence_chain_digest = Some(candidate.evidence_chain_digest);
        self.complete_state_digest = (self.state.through_version() == selected_version)
            .then(|| replay_state_digest(&self.state));
    }

    pub(super) fn ensure_next_source_call_capacity(
        &self,
        maximum_batch_query_calls: u64,
        maximum_batch_response_bytes: u64,
    ) -> Result<(), NnsRegistryReplayError> {
        if let Some(selected_version) = self.selected_version
            && self.state.through_version() == selected_version
        {
            return Err(NnsRegistryReplayError::SessionComplete { selected_version });
        }
        enforce_session_limit(
            "batch count",
            checked_add(self.batch_count, 1)?,
            self.limits.max_batches,
        )?;
        enforce_session_limit(
            "query call count",
            checked_add(self.query_call_count, maximum_batch_query_calls)?,
            self.limits.max_query_calls,
        )?;
        enforce_session_limit(
            "response bytes",
            checked_add(self.response_bytes, maximum_batch_response_bytes)?,
            self.limits.max_response_bytes,
        )
    }

    /// Return the explicit limits governing this session.
    #[must_use]
    pub const fn limits(&self) -> NnsRegistryReplaySessionLimits {
        self.limits
    }

    /// Return the reconstructed Registry state.
    #[must_use]
    pub const fn state(&self) -> &NnsRegistryReplayState {
        &self.state
    }

    /// Consume the session and return its reconstructed Registry state.
    #[must_use]
    pub fn into_state(self) -> NnsRegistryReplayState {
        self.state
    }

    /// Return the exact target selected by the first accepted batch.
    #[must_use]
    pub const fn selected_version(&self) -> Option<u64> {
        self.selected_version
    }

    /// Return the highest certified latest version observed without moving the target.
    #[must_use]
    pub const fn highest_certified_latest_version(&self) -> Option<u64> {
        self.highest_certified_latest_version
    }

    /// Return whether the selected exact target has been fully reconstructed.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.selected_version == Some(self.state.through_version())
    }

    /// Return the common root-key digest retained from admitted batches.
    #[must_use]
    pub fn root_key_digest(&self) -> Option<&str> {
        self.root_key_digest.as_deref()
    }

    /// Iterate distinct validated source endpoint strings in canonical order.
    pub fn source_endpoints(&self) -> impl ExactSizeIterator<Item = &str> {
        self.source_endpoints.iter().map(String::as_str)
    }

    /// Return the earliest certificate time retained from admitted reports.
    #[must_use]
    pub const fn minimum_certificate_time_nanos(&self) -> Option<u64> {
        self.minimum_certificate_time_nanos
    }

    /// Return the latest certificate time retained from admitted reports.
    #[must_use]
    pub const fn maximum_certificate_time_nanos(&self) -> Option<u64> {
        self.maximum_certificate_time_nanos
    }

    /// Return the domain-separated digest chain over every admitted validated report.
    ///
    /// This commits to report contents and ordering. For custom sources it does
    /// not replace the source's responsibility to authenticate raw evidence.
    #[must_use]
    pub const fn evidence_chain_digest(&self) -> Option<[u8; 32]> {
        self.evidence_chain_digest
    }

    /// Return the canonical reconstructed-state digest only after exact completion.
    #[must_use]
    pub const fn complete_state_digest(&self) -> Option<[u8; 32]> {
        self.complete_state_digest
    }

    /// Return the number of admitted certified delta batches.
    #[must_use]
    pub const fn batch_count(&self) -> u64 {
        self.batch_count
    }

    /// Return reported Registry query calls across admitted batches.
    #[must_use]
    pub const fn query_call_count(&self) -> u64 {
        self.query_call_count
    }

    /// Return reported encoded response bytes across admitted batches.
    #[must_use]
    pub const fn response_bytes(&self) -> u64 {
        self.response_bytes
    }

    /// Return committed mutations applied through the selected target.
    #[must_use]
    pub const fn applied_mutation_count(&self) -> u64 {
        self.applied_mutation_count
    }
}

struct SessionProvenanceCandidate {
    source_endpoints: BTreeSet<String>,
    minimum_certificate_time_nanos: u64,
    maximum_certificate_time_nanos: u64,
    evidence_chain_digest: [u8; 32],
}

fn chained_evidence_digest(
    previous: Option<[u8; 32]>,
    report: &NnsCertifiedRegistryDeltaBatchReport,
) -> Result<[u8; 32], NnsRegistryReplayError> {
    let mut hasher = Sha256::new();
    hasher.update(EVIDENCE_CHAIN_DOMAIN);
    match previous {
        Some(digest) => {
            hasher.update([1]);
            hasher.update(digest);
        }
        None => hasher.update([0]),
    }
    serde_json::to_writer(Sha256Writer(&mut hasher), report).map_err(|error| {
        NnsRegistryReplayError::EvidenceEncoding {
            reason: error.to_string(),
        }
    })?;
    Ok(hasher.finalize().into())
}

fn replay_state_digest(state: &NnsRegistryReplayState) -> [u8; 32] {
    let mut hasher = Sha256::new();
    hasher.update(COMPLETE_STATE_DOMAIN);
    hasher.update(state.through_version().to_be_bytes());
    hasher.update(encoded_usize(state.entry_count()));
    hasher.update(encoded_usize(state.content_bytes()));
    for (key, value) in state.entries() {
        hash_bytes(&mut hasher, key);
        hash_bytes(&mut hasher, value.value());
        hasher.update(value.last_mutation_version().to_be_bytes());
        hasher.update(value.timestamp_nanoseconds().to_be_bytes());
    }
    hasher.finalize().into()
}

fn hash_bytes(hasher: &mut Sha256, bytes: &[u8]) {
    hasher.update(encoded_usize(bytes.len()));
    hasher.update(bytes);
}

fn encoded_usize(value: usize) -> [u8; 16] {
    let native = value.to_be_bytes();
    let mut encoded = [0; 16];
    encoded[16 - native.len()..].copy_from_slice(&native);
    encoded
}

struct Sha256Writer<'a>(&'a mut Sha256);

impl Write for Sha256Writer<'_> {
    fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
        self.0.update(buffer);
        Ok(buffer.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

fn checked_add(left: u64, right: u64) -> Result<u64, NnsRegistryReplayError> {
    left.checked_add(right)
        .ok_or(NnsRegistryReplayError::Accounting)
}

const fn enforce_session_limit(
    field: &'static str,
    actual: u64,
    maximum: u64,
) -> Result<(), NnsRegistryReplayError> {
    if actual > maximum {
        Err(NnsRegistryReplayError::SessionLimitExceeded {
            field,
            maximum,
            actual,
        })
    } else {
        Ok(())
    }
}