heddle-thread-api 0.28.10

Native Thread clients and durable peer replication over Iroh
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
// SPDX-License-Identifier: Apache-2.0
//! Bounded client frames within a single, complete source publication.
use prost::Message;

use super::{Error, PublicationOriginals};
use crate::contract::{PublishContentClientFrame, ReplicationOperations};

const BATCH_OPERATIONS: usize = 128;
const BATCH_BYTES: usize = 256 * 1024;
const PUBLICATION_OPERATIONS: usize = 10_000;
const PUBLICATION_BYTES: usize = 16 * 1024 * 1024;

fn fits(operations: usize, bytes: usize, operation_id: &str, frame_limit: usize) -> bool {
    // Operations is one length-delimited field; include the full protobuf
    // wrapper and the caller's operation ID in the negotiated frame budget.
    let frame_bytes = PublishContentClientFrame {
        client_operation_id: operation_id.into(),
        body: None,
    }
    .encoded_len()
        + 1
        + prost::length_delimiter_len(bytes)
        + bytes;
    operations <= BATCH_OPERATIONS && bytes <= BATCH_BYTES && frame_bytes <= frame_limit
}

fn append(batch: &mut ReplicationOperations, unit: ReplicationOperations) {
    if batch.operations.is_empty() {
        batch.import_authority = unit.import_authority.clone();
        batch.native_authority = unit.native_authority.clone();
    }
    batch.operations.extend(unit.operations);
    batch.authority_admissions.extend(unit.authority_admissions);
    for evidence in unit.boundary_acceptances {
        if !batch.boundary_acceptances.contains(&evidence) {
            batch.boundary_acceptances.push(evidence);
        }
    }
}

pub(super) fn bounded_originals(
    originals: &PublicationOriginals,
    operation_id: &str,
    frame_limit: usize,
) -> Result<PublicationOriginals, Error> {
    let operations = originals
        .operations
        .iter()
        .map(|b| b.operations.len())
        .sum();
    if operations > PUBLICATION_OPERATIONS {
        return Err(Error::OriginalBudgetExceeded {
            operations,
            limit_name: "operation count",
            limit: PUBLICATION_OPERATIONS,
            actual: operations,
        });
    }
    if originals.operations.iter().any(|batch| {
        batch.operations.is_empty() || batch.authority_admissions.len() > batch.operations.len()
    }) {
        return Err(Error::Invalid(
            "bounded original genesis and source operations required",
        ));
    }
    let bytes = originals
        .geneses
        .iter()
        .map(Message::encoded_len)
        .sum::<usize>()
        + originals
            .operations
            .iter()
            .map(Message::encoded_len)
            .sum::<usize>();
    if bytes <= PUBLICATION_BYTES
        && originals.operations.iter().all(|batch| {
            fits(
                batch.operations.len(),
                batch.encoded_len(),
                operation_id,
                frame_limit,
            )
        })
    {
        return Ok(originals.clone());
    }
    let mut output = Vec::new();
    let mut current = ReplicationOperations::default();
    let mut index = 0;
    for batch in &originals.operations {
        if batch.operations.len() == 1 && !fits(1, batch.encoded_len(), operation_id, frame_limit) {
            return Err(Error::OriginalOperationTooLarge {
                operation: index + 1,
                bytes: batch.encoded_len(),
                batch_limit: BATCH_BYTES,
                frame_limit,
            });
        }
        // Decode sidecars only when present, to retain the exact receipt and
        // its evidence with the matching original after a frame-size split.
        #[cfg(not(feature = "replication"))]
        if !batch.authority_admissions.is_empty() || !batch.boundary_acceptances.is_empty() {
            return Err(Error::Invalid(
                "publication authority requires replication support",
            ));
        }
        #[cfg(feature = "replication")]
        let received = if batch.authority_admissions.is_empty()
            && batch.import_authority.is_none()
            && batch.native_authority.is_none()
        {
            if !batch.boundary_acceptances.is_empty() {
                return Err(Error::Invalid("unmatched publication boundary evidence"));
            }
            None
        } else {
            Some(
                crate::authority_admission::match_batch(batch)
                    .map_err(|_| Error::Invalid("invalid publication authority batch"))?,
            )
        };
        for (_position, record) in batch.operations.iter().enumerate() {
            index += 1;
            #[allow(unused_mut)]
            let mut unit = ReplicationOperations {
                operations: vec![record.clone()],
                import_authority: batch.import_authority.clone(),
                native_authority: batch.native_authority.clone(),
                ..Default::default()
            };
            #[cfg(feature = "replication")]
            if let Some(receipt) = received
                .as_ref()
                .and_then(|items| items.get(_position))
                .and_then(|item| item.authority_admission.as_ref())
            {
                unit.authority_admissions.push(
                    crate::authority_admission::encode(receipt)
                        .map_err(|_| Error::Invalid("invalid publication authority receipt"))?,
                );
                unit.boundary_acceptances =
                    crate::boundary_acceptance::authority_evidence(Some(receipt))
                        .map_err(|_| Error::Invalid("invalid publication boundary evidence"))?;
            }
            if !fits(1, unit.encoded_len(), operation_id, frame_limit) {
                return Err(Error::OriginalOperationTooLarge {
                    operation: index,
                    bytes: unit.encoded_len(),
                    batch_limit: BATCH_BYTES,
                    frame_limit,
                });
            }
            // Repeated protobuf fields are additive. Subtract only exact
            // duplicate evidence that append will retain once in this batch.
            if !current.operations.is_empty()
                && (current.import_authority != unit.import_authority
                    || current.native_authority != unit.native_authority)
            {
                output.push(std::mem::take(&mut current));
            }
            let duplicate_bundle = if !current.operations.is_empty() {
                unit.import_authority.as_ref().map_or(0, |bundle| {
                    let bytes = bundle.encoded_len();
                    1 + prost::length_delimiter_len(bytes) + bytes
                })
            } else {
                0
            };
            let duplicate_bytes: usize = unit
                .boundary_acceptances
                .iter()
                .filter(|record| current.boundary_acceptances.contains(record))
                .map(|record| {
                    let bytes = record.encoded_len();
                    1 + prost::length_delimiter_len(bytes) + bytes
                })
                .sum::<usize>()
                + duplicate_bundle;
            let bytes = current.encoded_len() + unit.encoded_len() - duplicate_bytes;
            if !fits(
                current.operations.len() + 1,
                bytes,
                operation_id,
                frame_limit,
            ) {
                output.push(std::mem::take(&mut current));
            }
            append(&mut current, unit);
        }
    }
    if !current.operations.is_empty() {
        output.push(current);
    }
    let bytes = originals
        .geneses
        .iter()
        .map(Message::encoded_len)
        .sum::<usize>()
        + output.iter().map(Message::encoded_len).sum::<usize>();
    if bytes > PUBLICATION_BYTES {
        return Err(Error::OriginalBudgetExceeded {
            operations,
            limit_name: "original metadata bytes",
            limit: PUBLICATION_BYTES,
            actual: bytes,
        });
    }
    Ok(PublicationOriginals {
        geneses: originals.geneses.clone(),
        operations: output,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::contract::{SignedRecord, ThreadGenesisRecord, publish_content_client_frame::Body};

    fn originals(count: usize, record_bytes: usize) -> PublicationOriginals {
        PublicationOriginals {
            geneses: vec![ThreadGenesisRecord::default()],
            operations: vec![ReplicationOperations {
                operations: (0..count)
                    .map(|index| SignedRecord {
                        canonical_record: vec![index as u8; record_bytes],
                        ..Default::default()
                    })
                    .collect(),
                ..Default::default()
            }],
        }
    }

    #[test]
    fn batches_by_operation_count_and_exact_encoded_bytes() {
        let input = originals(140, 10);
        let output = bounded_originals(&input, "publication", 512 * 1024).expect("count split");
        assert_eq!(
            output
                .operations
                .iter()
                .map(|batch| batch.operations.len())
                .collect::<Vec<_>>(),
            [128, 12]
        );
        assert_eq!(
            output
                .operations
                .into_iter()
                .flat_map(|b| b.operations)
                .collect::<Vec<_>>(),
            input.operations[0].operations
        );

        // Count alone would allow 128 records. Protobuf field overhead pushes
        // their actual encoded bytes beyond 256 KiB.
        let output = bounded_originals(&originals(128, 2048), "publication", 512 * 1024)
            .expect("byte split");
        assert_eq!(output.operations.len(), 2);
        for batch in output.operations {
            assert!(batch.encoded_len() <= BATCH_BYTES);
        }
        let output =
            bounded_originals(&originals(2, 10), "publication", 512 * 1024).expect("small");
        assert_eq!(output.operations.len(), 1);
    }

    #[test]
    fn negotiated_frames_include_wrapper_and_operation_id() {
        let id = "p".repeat(300);
        let input = originals(12, 300);
        let initial = bounded_originals(&input, &id, 512 * 1024).expect("initial plan");
        assert_eq!(initial.operations.len(), 1);
        let output = bounded_originals(&initial, &id, 1024).expect("Ready narrows frame budget");
        assert_eq!(output.operations.len(), 6);
        for batch in output.operations {
            let frame = PublishContentClientFrame {
                client_operation_id: id.clone(),
                body: Some(Body::Operations(batch)),
            };
            assert!(frame.encoded_len() <= 1024);
        }
    }

    #[cfg(feature = "replication")]
    #[test]
    fn admission_sidecars_stay_with_their_exact_originals() {
        use crypto::{Ed25519Signer, Signer, thread_operation::SignedOperation};
        use heddle_object_model::object::{
            CollaborationActor, ContentHash,
            original_boundary_acceptance::AdmissionBasis,
            thread_authority_admission::{OriginalAuthoritySubject, ThreadAuthorityAdmission},
            thread_replication::{
                OPERATION_FORMAT, ThreadOperation, ThreadOperationBody,
                metadata::{AUTHORITY_FORMAT, Control, ThreadControl},
            },
        };
        use uuid::Uuid;

        use crate::contract::RecordSignature;
        let author = Ed25519Signer::from_seed(&[41; 32]).expect("author");
        let executor = Ed25519Signer::from_seed(&[42; 32]).expect("executor");
        let mut input = originals(0, 0);
        input.operations.clear();
        for index in 1..=3 {
            let control = ThreadControl {
                version: 1,
                spool: Uuid::from_u128(100),
                actor: CollaborationActor {
                    principal_id: Uuid::from_u128(101),
                    agent_id: None,
                },
                authority_digest: ContentHash::compute_typed(AUTHORITY_FORMAT, b"authority"),
                authority_envelope: b"authority".to_vec(),
                client_operation_id: Uuid::from_u128(index),
                occurred_at_ms: 1,
                control: Control::Name(format!("original {index}")),
            };
            let operation = ThreadOperation {
                version: 1,
                thread: ContentHash::from_bytes([43; 32]),
                parents: Default::default(),
                publisher: author.public_key().try_into().expect("key"),
                body: ThreadOperationBody::Metadata(control.encode().expect("control")),
            };
            let signed = SignedOperation::sign(&operation, &author).expect("original");
            let receipt = ThreadAuthorityAdmission {
                version: 3,
                basis: AdmissionBasis::OriginalAuthority,
                spool: control.spool,
                spool_genesis: ContentHash::from_bytes([44; 32]),
                thread: operation.thread,
                subject: OriginalAuthoritySubject::Operation(operation.id().expect("ID")),
                actor: control.actor,
                publisher: operation.publisher,
                authority_digest: control.authority_digest,
                executor: executor.public_key().try_into().expect("key"),
                admitted_at_ms: 2000,
            };
            let receipt = crate::authority_admission::sign(&receipt, &executor).expect("receipt");
            input.operations.push(ReplicationOperations {
                operations: vec![crate::contract::SignedRecord {
                    format: OPERATION_FORMAT.into(),
                    canonical_record: signed.canonical,
                    signatures: vec![RecordSignature {
                        public_key: operation.publisher.to_vec(),
                        signature: signed.signature,
                    }],
                }],
                authority_admissions: vec![receipt],
                ..Default::default()
            });
        }
        let combined = PublicationOriginals {
            geneses: input.geneses.clone(),
            operations: vec![ReplicationOperations {
                operations: input
                    .operations
                    .iter()
                    .flat_map(|batch| batch.operations.clone())
                    .collect(),
                authority_admissions: input
                    .operations
                    .iter()
                    .flat_map(|batch| batch.authority_admissions.clone())
                    .collect(),
                ..Default::default()
            }],
        };
        let large = bounded_originals(&combined, "publication", 512 * 1024).expect("combined");
        assert_eq!(large.operations.len(), 1);
        let unit_bytes = input.operations[0].encoded_len();
        let smaller =
            bounded_originals(&large, "publication", unit_bytes + 64).expect("split with sidecars");
        assert_eq!(smaller.operations.len(), 3);
        for (batch, original) in smaller.operations.iter().zip(&input.operations) {
            assert_eq!(batch, original);
            assert_eq!(
                crate::authority_admission::match_batch(batch)
                    .expect("exact matched sidecar")
                    .len(),
                1
            );
        }
    }

    #[test]
    fn oversized_single_operation_and_publication_return_typed_errors() {
        for (bytes, frame) in [(BATCH_BYTES, 512 * 1024), (1024, 1024)] {
            let error = bounded_originals(&originals(1, bytes), "publication", frame)
                .err()
                .expect("oversized operation");
            assert!(matches!(
                error,
                Error::OriginalOperationTooLarge { operation: 1, .. }
            ));
        }
        let error = bounded_originals(&originals(10_001, 1), "publication", 512 * 1024)
            .err()
            .expect("operation ceiling");
        assert!(matches!(
            error,
            Error::OriginalBudgetExceeded {
                operations: 10_001,
                limit: 10_000,
                actual: 10_001,
                ..
            }
        ));
        let error = bounded_originals(&originals(100, 170_000), "publication", 512 * 1024)
            .err()
            .expect("metadata ceiling");
        assert!(
            matches!(error, Error::OriginalBudgetExceeded { operations: 100, limit_name: "original metadata bytes", limit: PUBLICATION_BYTES, actual } if actual > PUBLICATION_BYTES)
        );
    }
}