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
496
497
498
499
500
501
502
503
504
505
506
507
508
use std::collections::{HashMap, HashSet};
use serde::Serialize;
use crate::ProvenanceMark;
// Helper module for serializing ProvenanceMark as UR string
mod provenance_mark_as_ur {
use bc_ur::UREncodable;
use serde::Serializer;
use crate::ProvenanceMark;
pub fn serialize<S>(
mark: &ProvenanceMark,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&mark.ur_string())
}
}
// Helper module for serializing Vec<ProvenanceMark> as Vec<UR string>
mod provenance_marks_as_ur {
use bc_ur::UREncodable;
use serde::Serializer;
use crate::ProvenanceMark;
pub fn serialize<S>(
marks: &[ProvenanceMark],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde::ser::SerializeSeq;
let mut seq = serializer.serialize_seq(Some(marks.len()))?;
for mark in marks {
seq.serialize_element(&mark.ur_string())?;
}
seq.end()
}
}
// Helper module for serializing dcbor::Date as ISO8601 string
mod date_as_iso8601 {
use serde::Serializer;
pub fn serialize<S>(
date: &dcbor::Date,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&date.to_string())
}
}
/// Format for validation report output
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ValidationReportFormat {
/// Human-readable text format
#[default]
Text,
/// Compact JSON format (no whitespace)
JsonCompact,
/// Pretty-printed JSON format (with indentation)
JsonPretty,
}
/// Issue flagged during validation
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "type", content = "data")]
pub enum ValidationIssue {
/// Hash mismatch between consecutive marks
HashMismatch {
#[serde(with = "hex")]
expected: Vec<u8>,
#[serde(with = "hex")]
actual: Vec<u8>,
},
/// Key mismatch between consecutive marks
KeyMismatch,
/// Sequence number gap
SequenceGap { expected: u32, actual: u32 },
/// Date ordering violation
DateOrdering {
#[serde(serialize_with = "date_as_iso8601::serialize")]
previous: dcbor::Date,
#[serde(serialize_with = "date_as_iso8601::serialize")]
next: dcbor::Date,
},
/// Non-genesis mark at sequence 0
NonGenesisAtZero,
/// Invalid genesis key
InvalidGenesisKey,
}
impl std::fmt::Display for ValidationIssue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ValidationIssue::HashMismatch { expected, actual } => {
write!(
f,
"hash mismatch: expected {}, got {}",
hex::encode(expected),
hex::encode(actual)
)
}
ValidationIssue::KeyMismatch => {
write!(
f,
"key mismatch: current hash was not generated from next key"
)
}
ValidationIssue::SequenceGap { expected, actual } => {
write!(
f,
"sequence number gap: expected {}, got {}",
expected, actual
)
}
ValidationIssue::DateOrdering { previous, next } => {
write!(
f,
"date must be equal or later: previous is {}, next is {}",
previous, next
)
}
ValidationIssue::NonGenesisAtZero => {
write!(f, "non-genesis mark at sequence 0")
}
ValidationIssue::InvalidGenesisKey => {
write!(f, "genesis mark must have key equal to chain_id")
}
}
}
}
impl std::error::Error for ValidationIssue {}
/// A mark with any issues flagged during validation
#[derive(Debug, Clone, Serialize)]
pub struct FlaggedMark {
#[serde(serialize_with = "provenance_mark_as_ur::serialize")]
mark: ProvenanceMark,
issues: Vec<ValidationIssue>,
}
impl FlaggedMark {
fn new(mark: ProvenanceMark) -> Self { Self { mark, issues: Vec::new() } }
fn with_issue(mark: ProvenanceMark, issue: ValidationIssue) -> Self {
Self { mark, issues: vec![issue] }
}
pub fn mark(&self) -> &ProvenanceMark { &self.mark }
pub fn issues(&self) -> &[ValidationIssue] { &self.issues }
}
/// Report for a contiguous sequence of marks within a chain
#[derive(Debug, Clone, Serialize)]
pub struct SequenceReport {
start_seq: u32,
end_seq: u32,
marks: Vec<FlaggedMark>,
}
impl SequenceReport {
pub fn start_seq(&self) -> u32 { self.start_seq }
pub fn end_seq(&self) -> u32 { self.end_seq }
pub fn marks(&self) -> &[FlaggedMark] { &self.marks }
}
/// Report for a chain of marks with the same chain ID
#[derive(Debug, Clone, Serialize)]
pub struct ChainReport {
#[serde(with = "hex")]
chain_id: Vec<u8>,
has_genesis: bool,
#[serde(serialize_with = "provenance_marks_as_ur::serialize")]
marks: Vec<ProvenanceMark>,
sequences: Vec<SequenceReport>,
}
impl ChainReport {
pub fn chain_id(&self) -> &[u8] { &self.chain_id }
pub fn has_genesis(&self) -> bool { self.has_genesis }
pub fn marks(&self) -> &[ProvenanceMark] { &self.marks }
pub fn sequences(&self) -> &[SequenceReport] { &self.sequences }
/// Get the chain ID as a hex string for display
pub fn chain_id_hex(&self) -> String { hex::encode(&self.chain_id) }
}
/// Complete validation report
#[derive(Debug, Clone, Serialize)]
pub struct ValidationReport {
#[serde(serialize_with = "provenance_marks_as_ur::serialize")]
marks: Vec<ProvenanceMark>,
chains: Vec<ChainReport>,
}
impl ValidationReport {
pub fn marks(&self) -> &[ProvenanceMark] { &self.marks }
pub fn chains(&self) -> &[ChainReport] { &self.chains }
/// Format the validation report as human-readable text.
///
/// Returns a formatted string if the report contains interesting
/// information (issues, multiple chains, or multiple sequences).
/// Returns an empty string if the report represents a single perfect chain
/// with no issues.
pub fn format(&self, format: ValidationReportFormat) -> String {
match format {
ValidationReportFormat::Text => self.format_text(),
ValidationReportFormat::JsonCompact => {
serde_json::to_string(self).unwrap_or_default()
}
ValidationReportFormat::JsonPretty => {
serde_json::to_string_pretty(self).unwrap_or_default()
}
}
}
fn format_text(&self) -> String {
if !self.is_interesting() {
return String::new();
}
let mut lines = Vec::new();
// Report summary
lines.push(format!("Total marks: {}", self.marks.len()));
lines.push(format!("Chains: {}", self.chains.len()));
lines.push(String::new());
// Report each chain
for (chain_idx, chain) in self.chains.iter().enumerate() {
// Show short chain ID (first 4 bytes)
let chain_id_hex = chain.chain_id_hex();
let short_chain_id = if chain_id_hex.len() > 8 {
&chain_id_hex[..8]
} else {
&chain_id_hex
};
lines.push(format!("Chain {}: {}", chain_idx + 1, short_chain_id));
if !chain.has_genesis() {
lines.push(" Warning: No genesis mark found".to_string());
}
// Report each sequence
for seq in chain.sequences() {
// Report each mark in the sequence
for flagged_mark in seq.marks() {
let mark = flagged_mark.mark();
let short_id = mark.identifier();
let seq_num = mark.seq();
// Build the mark line with annotations
let mut annotations = Vec::new();
// Check if it's genesis
if mark.is_genesis() {
annotations.push("genesis mark".to_string());
}
// Add issue annotations
for issue in flagged_mark.issues() {
let issue_str = match issue {
ValidationIssue::SequenceGap {
expected,
actual: _,
} => {
format!("gap: {} missing", expected)
}
ValidationIssue::DateOrdering {
previous,
next,
} => {
format!("date {} < {}", previous, next)
}
ValidationIssue::HashMismatch { .. } => {
"hash mismatch".to_string()
}
ValidationIssue::KeyMismatch => {
"key mismatch".to_string()
}
ValidationIssue::NonGenesisAtZero => {
"non-genesis at seq 0".to_string()
}
ValidationIssue::InvalidGenesisKey => {
"invalid genesis key".to_string()
}
};
annotations.push(issue_str);
}
// Format the line
if annotations.is_empty() {
lines.push(format!(" {}: {}", seq_num, short_id));
} else {
lines.push(format!(
" {}: {} ({})",
seq_num,
short_id,
annotations.join(", ")
));
}
}
}
lines.push(String::new());
}
lines.join("\n").trim_end().to_string()
}
/// Check if the validation report contains interesting information.
///
/// Returns false for a single perfect chain with no issues, true otherwise.
fn is_interesting(&self) -> bool {
// Not interesting if empty
if self.chains.is_empty() {
return false;
}
// Check if any chain is missing genesis
for chain in &self.chains {
if !chain.has_genesis() {
return true;
}
}
// Not interesting if single chain with single perfect sequence
if self.chains.len() == 1 {
let chain = &self.chains[0];
if chain.sequences().len() == 1 {
let seq = &chain.sequences()[0];
// Check if the sequence has no issues
if seq.marks().iter().all(|m| m.issues().is_empty()) {
return false;
}
}
}
true
}
/// Check if the validation report has any issues.
///
/// Returns true if there are validation issues, missing genesis,
/// multiple chains, or multiple sequences.
pub fn has_issues(&self) -> bool {
// Missing genesis is considered an issue
for chain in &self.chains {
if !chain.has_genesis() {
return true;
}
}
// Check for validation issues in marks
for chain in &self.chains {
for seq in chain.sequences() {
for mark in seq.marks() {
if !mark.issues().is_empty() {
return true;
}
}
}
}
// Multiple chains or sequences are also considered issues
if self.chains.len() > 1 {
return true;
}
if self.chains.len() == 1 && self.chains[0].sequences().len() > 1 {
return true;
}
false
}
/// Validate a collection of provenance marks
/// Validate a collection of provenance marks
pub fn validate(marks: Vec<ProvenanceMark>) -> Self {
// Deduplicate exact duplicates
let mut seen = HashSet::new();
let mut deduplicated_marks = Vec::new();
for mark in marks {
if seen.insert(mark.clone()) {
deduplicated_marks.push(mark);
}
}
// Bin marks by chain ID
let mut chain_bins: HashMap<Vec<u8>, Vec<ProvenanceMark>> =
HashMap::new();
for mark in &deduplicated_marks {
chain_bins
.entry(mark.chain_id().to_vec())
.or_default()
.push(mark.clone());
}
// Process each chain
let mut chains = Vec::new();
for (chain_id_bytes, mut chain_marks) in chain_bins {
// Sort by sequence number
chain_marks.sort_by_key(|m| m.seq());
// Check for genesis mark
let has_genesis = chain_marks
.first()
.is_some_and(|m| m.seq() == 0 && m.is_genesis());
// Build sequence bins
let sequences = Self::build_sequence_bins(&chain_marks);
chains.push(ChainReport {
chain_id: chain_id_bytes,
has_genesis,
marks: chain_marks,
sequences,
});
}
// Sort chains by chain ID for consistent output
chains.sort_by(|a, b| a.chain_id.cmp(&b.chain_id));
ValidationReport { marks: deduplicated_marks, chains }
}
fn build_sequence_bins(marks: &[ProvenanceMark]) -> Vec<SequenceReport> {
let mut sequences = Vec::new();
let mut current_sequence: Vec<FlaggedMark> = Vec::new();
for (i, mark) in marks.iter().enumerate() {
if i == 0 {
// First mark starts a sequence
current_sequence.push(FlaggedMark::new(mark.clone()));
} else {
let prev = &marks[i - 1];
// Check if this mark follows the previous one
match prev.precedes_opt(mark) {
Ok(()) => {
// Continues the current sequence
current_sequence.push(FlaggedMark::new(mark.clone()));
}
Err(e) => {
// Breaks the sequence - save current and start new
if !current_sequence.is_empty() {
sequences.push(Self::create_sequence_report(
current_sequence,
));
}
// Start new sequence with this mark, flagged with the
// issue
let issue = match e {
crate::Error::Validation(v) => v,
_ => ValidationIssue::KeyMismatch, // Fallback
};
current_sequence =
vec![FlaggedMark::with_issue(mark.clone(), issue)];
}
}
}
}
// Add the final sequence
if !current_sequence.is_empty() {
sequences.push(Self::create_sequence_report(current_sequence));
}
sequences
}
fn create_sequence_report(marks: Vec<FlaggedMark>) -> SequenceReport {
let start_seq = marks.first().map(|m| m.mark.seq()).unwrap_or(0);
let end_seq = marks.last().map(|m| m.mark.seq()).unwrap_or(0);
SequenceReport { start_seq, end_seq, marks }
}
}
impl ProvenanceMark {
/// Validate a collection of provenance marks
///
/// This method analyzes the provided marks and produces a comprehensive
/// validation report that includes:
/// - Deduplication of exact duplicates
/// - Organization by chain ID
/// - Detection of genesis marks
/// - Identification of contiguous sequences
/// - Flagging of validation issues (hash mismatches, sequence gaps, etc.)
pub fn validate(marks: Vec<ProvenanceMark>) -> ValidationReport {
ValidationReport::validate(marks)
}
}