csv-adapter-core 0.1.1

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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Consignment: the wire format for CSV contract state transfer
//!
//! A consignment contains the complete provable history of a contract:
//! genesis, all transitions, seal assignments, and anchor proofs.
//! It is the unit of state transfer between peers.

use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::genesis::Genesis;
use crate::hash::Hash;
use crate::seal::AnchorRef;
use crate::state::{Metadata, StateAssignment};
use crate::transition::Transition;

/// Consignment version for forward compatibility
pub const CONSIGNMENT_VERSION: u8 = 1;

/// Anchor proof: links a commitment to an on-chain reference
///
/// An anchor provides cryptographic proof that a commitment was
/// published to a blockchain at a specific location.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Anchor {
    /// Anchor reference (on-chain location of the commitment)
    pub anchor_ref: AnchorRef,
    /// Commitment hash that was anchored
    pub commitment: Hash,
    /// Inclusion proof bytes (chain-specific)
    pub inclusion_proof: Vec<u8>,
    /// Finality proof bytes (chain-specific)
    pub finality_proof: Vec<u8>,
}

impl Anchor {
    /// Create a new anchor from its components
    pub fn new(
        anchor_ref: AnchorRef,
        commitment: Hash,
        inclusion_proof: Vec<u8>,
        finality_proof: Vec<u8>,
    ) -> Self {
        Self {
            anchor_ref,
            commitment,
            inclusion_proof,
            finality_proof,
        }
    }
}

/// Seal assignment record in a consignment
///
/// Records which state was assigned to which seal during a transition.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SealAssignment {
    /// Seal being assigned to
    pub seal_ref: crate::seal::SealRef,
    /// State being assigned to this seal
    pub assignment: StateAssignment,
    /// Metadata for this assignment
    pub metadata: Vec<Metadata>,
}

impl SealAssignment {
    /// Create a new seal assignment
    pub fn new(
        seal_ref: crate::seal::SealRef,
        assignment: StateAssignment,
        metadata: Vec<Metadata>,
    ) -> Self {
        Self {
            seal_ref,
            assignment,
            metadata,
        }
    }
}

/// Complete contract consignment
///
/// This is the wire format for transferring CSV contract state between peers.
/// A valid consignment contains a complete, verifiable chain from genesis
/// to the current state.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Consignment {
    /// Consignment version
    pub version: u8,
    /// Contract genesis
    pub genesis: Genesis,
    /// State transitions in topological order
    pub transitions: Vec<Transition>,
    /// Seal assignments (indexed by transition output)
    pub seal_assignments: Vec<SealAssignment>,
    /// Anchor proofs (on-chain commitment locations)
    pub anchors: Vec<Anchor>,
    /// Schema ID (for validation against contract rules)
    pub schema_id: Hash,
}

impl Consignment {
    /// Create a new consignment
    pub fn new(
        genesis: Genesis,
        transitions: Vec<Transition>,
        seal_assignments: Vec<SealAssignment>,
        anchors: Vec<Anchor>,
        schema_id: Hash,
    ) -> Self {
        Self {
            version: CONSIGNMENT_VERSION,
            genesis,
            transitions,
            seal_assignments,
            anchors,
            schema_id,
        }
    }

    /// Compute the consignment state root hash
    ///
    /// This hash represents the current state of the contract after all
    /// transitions have been applied.
    pub fn state_root(&self) -> Hash {
        let mut hasher = Sha256::new();

        hasher.update(b"CSV-CONSIGNMENT-v1");
        hasher.update(self.version.to_le_bytes());

        // Genesis hash
        hasher.update(self.genesis.hash().as_bytes());

        // Transition hashes in order
        hasher.update((self.transitions.len() as u64).to_le_bytes());
        for transition in &self.transitions {
            hasher.update(transition.hash().as_bytes());
        }

        // Seal assignments
        hasher.update((self.seal_assignments.len() as u64).to_le_bytes());
        for assignment in &self.seal_assignments {
            hasher.update(assignment.seal_ref.to_vec());
            hasher.update(assignment.assignment.seal.to_vec());
            hasher.update(&assignment.assignment.data);
        }

        // Anchors
        hasher.update((self.anchors.len() as u64).to_le_bytes());
        for anchor in &self.anchors {
            hasher.update(anchor.commitment.as_bytes());
            hasher.update(anchor.anchor_ref.to_vec());
        }

        let result = hasher.finalize();
        let mut array = [0u8; 32];
        array.copy_from_slice(&result);
        Hash::new(array)
    }

    /// Get the contract ID (from genesis)
    pub fn contract_id(&self) -> Hash {
        self.genesis.contract_id
    }

    /// Get the number of transitions
    pub fn transition_count(&self) -> usize {
        self.transitions.len()
    }

    /// Get the number of seal assignments
    pub fn assignment_count(&self) -> usize {
        self.seal_assignments.len()
    }

    /// Get the number of anchors
    pub fn anchor_count(&self) -> usize {
        self.anchors.len()
    }

    /// Get the latest state for a given seal
    ///
    /// Walks transitions in order to find the most recent assignment
    /// to the given seal.
    pub fn latest_state_for_seal(&self, seal: &crate::seal::SealRef) -> Option<&StateAssignment> {
        // Walk assignments in reverse to find the latest for this seal
        self.seal_assignments
            .iter()
            .rev()
            .find(|a| &a.seal_ref == seal)
            .map(|a| &a.assignment)
    }

    /// Get all current seal owners
    ///
    /// Returns the set of seals that have received state but haven't
    /// been consumed by any transition.
    pub fn current_seals(&self) -> alloc::collections::BTreeSet<Vec<u8>> {
        // Start with genesis owned state
        let mut active: alloc::collections::BTreeSet<Vec<u8>> = alloc::collections::BTreeSet::new();

        // Genesis outputs
        for owned in &self.genesis.owned_state {
            active.insert(owned.seal.to_vec());
        }

        // Transition outputs
        for transition in &self.transitions {
            for output in &transition.owned_outputs {
                active.insert(output.seal.to_vec());
            }
        }

        // Note: determining which seals are actually consumed requires
        // resolving StateRef -> SealRef mapping through the transition chain.
        // This is a simplified view; full resolution needs VM execution.
        active
    }

    /// Validate basic consignment structure
    pub fn validate_structure(&self) -> Result<(), ConsignmentError> {
        // Version check
        if self.version != CONSIGNMENT_VERSION {
            return Err(ConsignmentError::VersionMismatch {
                expected: CONSIGNMENT_VERSION,
                actual: self.version,
            });
        }

        // Schema ID consistency
        if self.genesis.schema_id != self.schema_id {
            return Err(ConsignmentError::SchemaMismatch {
                genesis_schema: self.genesis.schema_id,
                consignment_schema: self.schema_id,
            });
        }

        // Contract ID consistency
        if self.genesis.contract_id != self.contract_id() {
            return Err(ConsignmentError::ContractIdMismatch);
        }

        // Transition count vs anchor count (each transition should have an anchor)
        if self.transitions.len() != self.anchors.len() {
            return Err(ConsignmentError::AnchorCountMismatch {
                transitions: self.transitions.len(),
                anchors: self.anchors.len(),
            });
        }

        Ok(())
    }

    /// Serialize consignment to bytes
    pub fn to_bytes(&self) -> Result<Vec<u8>, bincode::Error> {
        bincode::serialize(self)
    }

    /// Deserialize consignment from bytes with size limit (50MB max)
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, bincode::Error> {
        const MAX_SIZE: usize = 50 * 1024 * 1024; // 50MB
        if bytes.len() > MAX_SIZE {
            return Err(bincode::ErrorKind::Custom(format!(
                "Consignment too large: {} bytes (max {})",
                bytes.len(),
                MAX_SIZE
            ))
            .into());
        }

        let consignment: Consignment = bincode::deserialize(bytes)?;

        // Verify version before accepting
        if consignment.version != CONSIGNMENT_VERSION {
            return Err(bincode::ErrorKind::Custom(format!(
                "Unsupported consignment version: {}",
                consignment.version
            ))
            .into());
        }

        Ok(consignment)
    }

    /// Create a consignment with only genesis (no transitions yet)
    pub fn from_genesis(genesis: Genesis) -> Self {
        let schema_id = genesis.schema_id;
        Self::new(genesis, vec![], vec![], vec![], schema_id)
    }
}

/// Consignment validation errors
#[derive(Debug)]
#[allow(missing_docs)]
pub enum ConsignmentError {
    /// Version mismatch between expected and actual consignment
    VersionMismatch { expected: u8, actual: u8 },
    /// Schema ID doesn't match between genesis and consignment
    SchemaMismatch {
        genesis_schema: Hash,
        consignment_schema: Hash,
    },
    /// Contract ID inconsistency between components
    ContractIdMismatch,
    /// Transition count doesn't match anchor count
    AnchorCountMismatch { transitions: usize, anchors: usize },
}

impl core::fmt::Display for ConsignmentError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ConsignmentError::VersionMismatch { expected, actual } => {
                write!(
                    f,
                    "Consignment version mismatch: expected {}, got {}",
                    expected, actual
                )
            }
            ConsignmentError::SchemaMismatch {
                genesis_schema,
                consignment_schema,
            } => {
                write!(
                    f,
                    "Schema mismatch: genesis has {}, consignment has {}",
                    genesis_schema, consignment_schema
                )
            }
            ConsignmentError::ContractIdMismatch => {
                write!(f, "Contract ID inconsistency")
            }
            ConsignmentError::AnchorCountMismatch {
                transitions,
                anchors,
            } => {
                write!(
                    f,
                    "Anchor count mismatch: {} transitions but {} anchors",
                    transitions, anchors
                )
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::genesis::Genesis;
    use crate::seal::SealRef;
    use crate::state::{GlobalState, Metadata, OwnedState};
    use crate::state::{StateAssignment, StateRef};

    fn test_consignment() -> Consignment {
        let genesis = Genesis::new(
            Hash::new([1u8; 32]),
            Hash::new([2u8; 32]),
            vec![
                GlobalState::new(1, 1000u64.to_le_bytes().to_vec()), // total supply
            ],
            vec![OwnedState::new(
                10,
                SealRef::new(vec![0xAA; 16], Some(1)).unwrap(),
                1000u64.to_le_bytes().to_vec(),
            )],
            vec![Metadata::from_string("issuer", "test")],
        );

        let transition = Transition::new(
            1, // transfer
            vec![StateRef::new(10, Hash::new([1u8; 32]), 0)],
            vec![
                StateAssignment::new(
                    10,
                    SealRef::new(vec![0xBB; 16], Some(2)).unwrap(),
                    600u64.to_le_bytes().to_vec(),
                ),
                StateAssignment::new(
                    10,
                    SealRef::new(vec![0xAA; 16], Some(1)).unwrap(),
                    400u64.to_le_bytes().to_vec(),
                ),
            ],
            vec![],
            vec![],
            vec![0x01, 0x02],
            vec![vec![0xAB; 64]],
        );

        let seal_assignment = SealAssignment::new(
            SealRef::new(vec![0xBB; 16], Some(2)).unwrap(),
            StateAssignment::new(
                10,
                SealRef::new(vec![0xBB; 16], Some(2)).unwrap(),
                600u64.to_le_bytes().to_vec(),
            ),
            vec![],
        );

        let anchor = Anchor::new(
            AnchorRef::new(vec![0xCC; 32], 100, vec![]).unwrap(),
            transition.hash(),
            vec![0xDD; 64], // inclusion proof
            vec![0xEE; 32], // finality proof
        );

        Consignment::new(
            genesis,
            vec![transition],
            vec![seal_assignment],
            vec![anchor],
            Hash::new([2u8; 32]),
        )
    }

    #[test]
    fn test_consignment_creation() {
        let c = test_consignment();
        assert_eq!(c.version, CONSIGNMENT_VERSION);
        assert_eq!(c.transition_count(), 1);
        assert_eq!(c.assignment_count(), 1);
        assert_eq!(c.anchor_count(), 1);
    }

    #[test]
    fn test_consignment_state_root() {
        let c = test_consignment();
        let root = c.state_root();
        assert_eq!(root.as_bytes().len(), 32);
    }

    #[test]
    fn test_consignment_state_root_deterministic() {
        let c1 = test_consignment();
        let c2 = test_consignment();
        assert_eq!(c1.state_root(), c2.state_root());
    }

    #[test]
    fn test_consignment_state_root_differs_by_transition() {
        let mut c1 = test_consignment();
        let c2 = test_consignment();
        // Modify transition bytecode
        c1.transitions[0].validation_script = vec![0xFF];
        assert_ne!(c1.state_root(), c2.state_root());
    }

    #[test]
    fn test_contract_id() {
        let c = test_consignment();
        assert_eq!(c.contract_id(), Hash::new([1u8; 32]));
    }

    #[test]
    fn test_latest_state_for_seal() {
        let c = test_consignment();
        let seal = SealRef::new(vec![0xBB; 16], Some(2)).unwrap();
        let state = c.latest_state_for_seal(&seal);
        assert!(state.is_some());
        assert_eq!(state.unwrap().data, 600u64.to_le_bytes().to_vec());
    }

    #[test]
    fn test_latest_state_for_seal_not_found() {
        let c = test_consignment();
        let seal = SealRef::new(vec![0xFF; 16], Some(99)).unwrap();
        let state = c.latest_state_for_seal(&seal);
        assert!(state.is_none());
    }

    #[test]
    fn test_validate_structure_valid() {
        let c = test_consignment();
        assert!(c.validate_structure().is_ok());
    }

    #[test]
    fn test_validate_structure_wrong_version() {
        let mut c = test_consignment();
        c.version = 99;
        assert!(c.validate_structure().is_err());
    }

    #[test]
    fn test_validate_structure_schema_mismatch() {
        let mut c = test_consignment();
        c.schema_id = Hash::new([99u8; 32]); // Different from genesis schema_id
        assert!(c.validate_structure().is_err());
    }

    #[test]
    fn test_validate_structure_anchor_count_mismatch() {
        let mut c = test_consignment();
        c.anchors.push(Anchor::new(
            AnchorRef::new(vec![0xFF; 32], 200, vec![]).unwrap(),
            Hash::zero(),
            vec![],
            vec![],
        ));
        assert!(c.validate_structure().is_err());
    }

    #[test]
    fn test_from_genesis() {
        let genesis = Genesis::new(
            Hash::new([1u8; 32]),
            Hash::new([2u8; 32]),
            vec![],
            vec![],
            vec![],
        );
        let c = Consignment::from_genesis(genesis.clone());
        assert_eq!(c.version, CONSIGNMENT_VERSION);
        assert_eq!(c.transition_count(), 0);
        assert_eq!(c.assignment_count(), 0);
        assert_eq!(c.anchor_count(), 0);
        assert_eq!(c.contract_id(), Hash::new([1u8; 32]));
        assert!(c.validate_structure().is_ok());
    }

    #[test]
    fn test_consignment_serialization_roundtrip() {
        let c = test_consignment();
        let bytes = c.to_bytes().unwrap();
        let restored = Consignment::from_bytes(&bytes).unwrap();
        assert_eq!(c, restored);
        assert_eq!(c.state_root(), restored.state_root());
    }

    #[test]
    fn test_consignment_wrong_version_rejected() {
        let mut c = test_consignment();
        c.version = 99;
        let bytes = bincode::serialize(&c).unwrap();
        let result = Consignment::from_bytes(&bytes);
        assert!(result.is_err());
    }

    #[test]
    fn test_current_seals() {
        let c = test_consignment();
        let seals = c.current_seals();
        // Should include seals from genesis and transition outputs
        assert!(!seals.is_empty());
    }

    #[test]
    fn test_empty_consignment_structure() {
        let genesis = Genesis::new(
            Hash::new([1u8; 32]),
            Hash::new([2u8; 32]),
            vec![],
            vec![],
            vec![],
        );
        let c = Consignment::from_genesis(genesis);
        assert!(c.validate_structure().is_ok());
        assert_eq!(c.state_root().as_bytes().len(), 32);
    }
}