csv-adapter-core 0.1.0

Chain-agnostic core traits and types for CSV (Client-Side Validation) adapters
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
//! Consignment Validation Pipeline
//!
//! Provides detailed, step-by-step validation of consignments:
//! 1. Fetch state proof chain
//! 2. Verify commitment linkage
//! 3. Verify single consumption of each seal
//! 4. Verify no conflicting state transitions
//! 5. Accept or reject based on full validation
//!
//! ## Validation Pipeline
//!
//! ```text
//! Consignment Received
//!//! [1] Structural Validation
//!   - Version check
//!   - Schema ID consistency  
//!   - Required fields present
//!//! [2] Commitment Chain Validation
//!   - Genesis → Latest chain integrity
//!   - No missing commitments
//!   - No cycles or duplicates
//!//! [3] Seal Consumption Validation  
//!   - Each seal consumed at most once
//!   - Cross-chain double-spend check
//!   - Seal references match transitions
//!//! [4] State Transition Validation
//!   - Inputs satisfied by prior outputs
//!   - State conservation rules
//!   - No conflicting transitions
//!//! [5] Final Acceptance Decision
//!   - All checks pass → Accept
//!   - Any check fails → Reject with reason
//! ```

use alloc::vec::Vec;

use crate::consignment::Consignment;
use crate::hash::Hash;
use crate::seal_registry::{ChainId, CrossChainSealRegistry, SealConsumption, SealStatus};
use crate::state_store::InMemoryStateStore;

/// Detailed validation report.
#[derive(Debug)]
pub struct ValidationReport {
    /// Whether the consignment passed validation
    pub passed: bool,
    /// Individual validation step results
    pub steps: Vec<ValidationStep>,
    /// Summary of findings
    pub summary: String,
}

/// A single validation step result.
#[derive(Debug)]
pub struct ValidationStep {
    /// Name of the validation step
    pub name: String,
    /// Whether this step passed
    pub passed: bool,
    /// Details of the validation (for debugging)
    pub details: String,
}

/// Consignment validator with detailed reporting.
pub struct ConsignmentValidator {
    /// State history store
    store: InMemoryStateStore,
    /// Cross-chain seal registry  
    seal_registry: CrossChainSealRegistry,
    /// Validation report being built
    report: ValidationReport,
}

impl ConsignmentValidator {
    /// Create a new validator.
    pub fn new() -> Self {
        Self {
            store: InMemoryStateStore::new(),
            seal_registry: CrossChainSealRegistry::new(),
            report: ValidationReport {
                passed: true,
                steps: Vec::new(),
                summary: String::new(),
            },
        }
    }

    /// Validate a consignment with detailed reporting.
    pub fn validate_consignment(
        mut self,
        consignment: &Consignment,
        anchor_chain: ChainId,
    ) -> ValidationReport {
        // Step 1: Structural validation
        self.validate_structure(consignment);

        // Step 2: Commitment chain validation
        self.validate_commitment_chain(consignment);

        // Step 3: Seal consumption validation
        self.validate_seal_consumption(consignment, &anchor_chain);

        // Step 4: State transition validation
        self.validate_state_transitions(consignment);

        // Step 5: Generate summary
        self.generate_summary();

        self.report
    }

    /// Step 1: Validate consignment structure.
    fn validate_structure(&mut self, consignment: &Consignment) {
        let result = consignment.validate_structure();
        let passed = result.is_ok();

        self.report.steps.push(ValidationStep {
            name: "Structural Validation".to_string(),
            passed,
            details: if passed {
                "All structural checks passed".to_string()
            } else {
                format!("Structural validation failed: {}", result.unwrap_err())
            },
        });

        if !passed {
            self.report.passed = false;
        }
    }

    /// Step 2: Validate commitment chain integrity.
    fn validate_commitment_chain(&mut self, consignment: &Consignment) {
        // Verify that the anchors form a valid commitment chain.
        // Each anchor contains a commitment hash and an inclusion proof.
        // We verify that:
        // 1. The genesis commitment is consistent with the consignment
        // 2. Each transition's hash is linked to its anchor's commitment
        // 3. The commitment chain forms an unbroken sequence

        if consignment.anchors.is_empty() {
            // No anchors means no on-chain commitments to verify
            // This is valid for a genesis-only consignment
            self.report.steps.push(ValidationStep {
                name: "Commitment Chain Validation".to_string(),
                passed: true,
                details: "No anchors — genesis-only consignment".to_string(),
            });
            return;
        }

        // Verify anchor count matches transition count
        if consignment.anchors.len() != consignment.transitions.len() {
            self.report.steps.push(ValidationStep {
                name: "Commitment Chain Validation".to_string(),
                passed: false,
                details: format!(
                    "Anchor count mismatch: {} anchors but {} transitions",
                    consignment.anchors.len(),
                    consignment.transitions.len(),
                ),
            });
            self.report.passed = false;
            return;
        }

        // Verify each transition's hash is anchored
        let mut all_valid = true;
        let mut details = Vec::new();

        for (i, (transition, anchor)) in consignment
            .transitions
            .iter()
            .zip(consignment.anchors.iter())
            .enumerate()
        {
            let tx_hash = transition.hash();
            if tx_hash != anchor.commitment {
                all_valid = false;
                details.push(format!(
                    "Transition {} hash {} not anchored (got {})",
                    i,
                    hex::encode(tx_hash.as_bytes()),
                    hex::encode(anchor.commitment.as_bytes()),
                ));
            }
        }

        // Verify inclusion proofs are non-empty (basic check; full MPT/Merkle
        // verification is done by chain-specific verifiers)
        for (i, anchor) in consignment.anchors.iter().enumerate() {
            if anchor.inclusion_proof.is_empty() {
                all_valid = false;
                details.push(format!("Anchor {} has empty inclusion proof", i));
            }
            if anchor.finality_proof.is_empty() {
                all_valid = false;
                details.push(format!("Anchor {} has empty finality proof", i));
            }
        }

        self.report.steps.push(ValidationStep {
            name: "Commitment Chain Validation".to_string(),
            passed: all_valid,
            details: if all_valid {
                format!(
                    "Verified {} commitment(s) anchored on-chain",
                    consignment.anchors.len(),
                )
            } else {
                details.join("; ")
            },
        });

        if !all_valid {
            self.report.passed = false;
        }
    }

    /// Step 3: Validate seal consumption (no double-spends).
    fn validate_seal_consumption(&mut self, consignment: &Consignment, anchor_chain: &ChainId) {
        let mut all_passed = true;
        let mut details = Vec::new();

        for seal_assignment in &consignment.seal_assignments {
            match self
                .seal_registry
                .check_seal_status(&seal_assignment.seal_ref)
            {
                SealStatus::Unconsumed => {
                    // Create a synthetic Right ID for tracking
                    let right_id = crate::right::RightId(Hash::new(
                        seal_assignment
                            .seal_ref
                            .seal_id
                            .clone()
                            .try_into()
                            .unwrap_or([0u8; 32]),
                    ));

                    let consumption = SealConsumption {
                        chain: anchor_chain.clone(),
                        seal_ref: seal_assignment.seal_ref.clone(),
                        right_id,
                        block_height: 0,
                        tx_hash: Hash::new([0u8; 32]),
                        recorded_at: 0,
                    };

                    if let Err(e) = self.seal_registry.record_consumption(consumption) {
                        all_passed = false;
                        details.push(format!("Double-spend: {:?}", e));
                    }
                }
                SealStatus::ConsumedOnChain { chain, .. } => {
                    all_passed = false;
                    details.push(format!("Seal already consumed on {:?}", chain));
                }
                SealStatus::DoubleSpent { consumptions } => {
                    all_passed = false;
                    details.push(format!(
                        "Seal double-spent across {} chains",
                        consumptions.len()
                    ));
                }
            }
        }

        self.report.steps.push(ValidationStep {
            name: "Seal Consumption Validation".to_string(),
            passed: all_passed,
            details: if all_passed {
                format!(
                    "All {} seals validated successfully",
                    consignment.seal_assignments.len()
                )
            } else {
                details.join("; ")
            },
        });

        if !all_passed {
            self.report.passed = false;
        }
    }

    /// Step 4: Validate state transitions.
    fn validate_state_transitions(&mut self, consignment: &Consignment) {
        // Verify state transitions are valid:
        // 1. Each transition's validation script is non-empty
        // 2. Each transition's input references point to valid commitments
        // 3. Seal assignments are consistent with transition outputs
        let mut all_valid = true;
        let mut details = Vec::new();

        // Track available commitments from genesis and transition outputs
        let mut available_commitments: alloc::collections::BTreeSet<Hash> =
            alloc::collections::BTreeSet::new();

        // Genesis outputs are initially available (indexed by their commitment hash)
        for _owned in &consignment.genesis.owned_state {
            available_commitments.insert(consignment.genesis.hash());
        }

        for (i, transition) in consignment.transitions.iter().enumerate() {
            // Check validation script is non-empty
            if transition.validation_script.is_empty() {
                all_valid = false;
                details.push(format!("Transition {} has empty validation script", i));
            }

            // Verify input references point to known commitments
            for input in &transition.owned_inputs {
                if !available_commitments.contains(&input.commitment) {
                    all_valid = false;
                    details.push(format!(
                        "Transition {} references unknown commitment {}",
                        i,
                        hex::encode(input.commitment.as_bytes()),
                    ));
                }
            }

            // Track transition outputs as available for subsequent transitions
            available_commitments.insert(transition.hash());
        }

        // Verify seal assignments reference valid transition outputs
        for (i, assignment) in consignment.seal_assignments.iter().enumerate() {
            // The assignment should correspond to a transition output
            // Check that the assignment's metadata is well-formed
            if assignment.assignment.data.is_empty() {
                details.push(format!("Seal assignment {} has empty data", i));
            }
        }

        self.report.steps.push(ValidationStep {
            name: "State Transition Validation".to_string(),
            passed: all_valid,
            details: if all_valid {
                format!(
                    "Validated {} transitions, {} commitments tracked",
                    consignment.transitions.len(),
                    available_commitments.len(),
                )
            } else {
                details.join("; ")
            },
        });

        if !all_valid {
            self.report.passed = false;
        }
    }

    /// Generate final summary.
    fn generate_summary(&mut self) {
        let passed_count = self.report.steps.iter().filter(|s| s.passed).count();
        let total_count = self.report.steps.len();

        self.report.summary = if self.report.passed {
            format!(
                "Consignment accepted: {}/{} validation steps passed",
                passed_count, total_count
            )
        } else {
            let failed: Vec<&str> = self
                .report
                .steps
                .iter()
                .filter(|s| !s.passed)
                .map(|s| s.name.as_str())
                .collect();
            format!(
                "Consignment rejected: {} steps failed: {}",
                failed.len(),
                failed.join(", ")
            )
        };
    }

    /// Get access to the state store.
    pub fn store(&self) -> &InMemoryStateStore {
        &self.store
    }

    /// Get access to the seal registry.
    pub fn seal_registry(&self) -> &CrossChainSealRegistry {
        &self.seal_registry
    }
}

impl Default for ConsignmentValidator {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::consignment::Consignment;
    use crate::genesis::Genesis;
    use crate::state_store::StateHistoryStore;

    fn make_test_consignment() -> Consignment {
        let genesis = Genesis::new(
            Hash::new([0xAB; 32]),
            Hash::new([0x01; 32]),
            vec![],
            vec![],
            vec![],
        );
        Consignment::new(genesis, vec![], vec![], vec![], Hash::new([0x01; 32]))
    }

    #[test]
    fn test_validator_creation() {
        let validator = ConsignmentValidator::new();
        assert_eq!(validator.store().list_contracts().unwrap().len(), 0);
    }

    #[test]
    fn test_validate_simple_consignment() {
        let validator = ConsignmentValidator::new();
        let consignment = make_test_consignment();

        let report = validator.validate_consignment(&consignment, ChainId::Bitcoin);

        // Should have validation steps
        assert!(!report.steps.is_empty());

        // All steps should pass for a simple valid consignment
        for step in &report.steps {
            assert!(step.passed, "Step '{}' failed: {}", step.name, step.details);
        }
    }

    #[test]
    fn test_validation_report_structure() {
        let validator = ConsignmentValidator::new();
        let consignment = make_test_consignment();

        let report = validator.validate_consignment(&consignment, ChainId::Bitcoin);

        // Report should have meaningful content
        assert!(!report.summary.is_empty());
        assert!(report.steps.len() >= 3); // At least structural, seal, and transition validation
    }

    #[test]
    fn test_validation_steps_are_sequential() {
        let validator = ConsignmentValidator::new();
        let consignment = make_test_consignment();

        let report = validator.validate_consignment(&consignment, ChainId::Bitcoin);

        // Steps should be in expected order
        let step_names: Vec<&str> = report.steps.iter().map(|s| s.name.as_str()).collect();

        assert!(step_names.contains(&"Structural Validation"));
        assert!(step_names.contains(&"Seal Consumption Validation"));
        assert!(step_names.contains(&"State Transition Validation"));
    }
}