exochain-dag 0.2.0-beta

EXOCHAIN append-only DAG with BFT consensus and Merkle structures
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
// Copyright 2026 Exochain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Checkpoint finality aggregation for the EXOCHAIN DAG.
//!
//! Aggregates MMR event root, SMT state root, finalized height, frontier tip
//! set, and multi-validator signatures into a single checkpoint payload.
//! Provides the canonical CBOR signing preimage (Spec 9.4) with domain
//! separation tag `EXOCHAIN-CHECKPOINT-v1`.

use exo_core::{Did, Hash256, Signature};
use serde::{Deserialize, Serialize};

use crate::error::{DagError, Result};

/// A checkpoint payload aggregating finality proof components.
///
/// Validators collectively attest to a finalized DAG state by signing
/// the canonical preimage computed by [`checkpoint_signing_preimage`].
#[derive(Clone, Serialize, Deserialize)]
pub struct CheckpointPayload {
    /// MMR root over finalized event IDs.
    pub event_root: Hash256,

    /// SMT root over derived state.
    pub state_root: Hash256,

    /// Height (sequence number) of this checkpoint.
    pub height: u64,

    /// Count of finalized events covered by this checkpoint.
    pub finalized_events: u64,

    /// Frontier hashes (DAG tips at checkpoint time).
    pub frontier: Vec<Hash256>,

    /// Validator signatures attesting to this checkpoint.
    pub validator_sigs: Vec<ValidatorSignature>,
}

impl std::fmt::Debug for CheckpointPayload {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CheckpointPayload")
            .field("event_root", &self.event_root)
            .field("state_root", &self.state_root)
            .field("height", &self.height)
            .field("finalized_events", &self.finalized_events)
            .field("frontier", &self.frontier)
            .field("validator_sig_count", &self.validator_sigs.len())
            .finish()
    }
}

/// A single validator's attestation to a checkpoint.
#[derive(Clone, Serialize, Deserialize)]
pub struct ValidatorSignature {
    /// DID of the validating entity.
    pub validator_did: Did,
    /// Key version used for signing.
    pub key_version: u64,
    /// Cryptographic signature over the checkpoint preimage.
    pub signature: Signature,
}

impl std::fmt::Debug for ValidatorSignature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ValidatorSignature")
            .field("validator_did", &self.validator_did)
            .field("key_version", &self.key_version)
            .field("signature", &"<redacted>")
            .finish()
    }
}

/// Domain separation tag for checkpoint signing (EXOCHAIN Specification v2.2 §9.4).
pub const CHECKPOINT_SIGNING_DOMAIN: &str = "EXOCHAIN-CHECKPOINT-v1";

/// Legacy byte view of the checkpoint signing domain tag.
pub const CHECKPOINT_DOMAIN_SEP: &[u8] = CHECKPOINT_SIGNING_DOMAIN.as_bytes();

const CHECKPOINT_SIGNING_SCHEMA_VERSION: u16 = 1;

#[derive(Serialize)]
struct CheckpointSigningPayload<'a> {
    domain: &'static str,
    schema_version: u16,
    event_root: &'a Hash256,
    state_root: &'a Hash256,
    height: u64,
    finalized_events: u64,
    frontier: &'a [Hash256],
}

/// Compute the normative checkpoint signing preimage (EXOCHAIN Specification v2.2 §9.4).
///
/// Validators sign this domain-separated, versioned canonical CBOR payload to
/// attest to the checkpoint.
///
/// # Errors
///
/// Returns [`DagError::Serialization`] if the canonical signing payload cannot
/// be serialized.
pub fn checkpoint_signing_preimage(cp: &CheckpointPayload) -> Result<Vec<u8>> {
    let payload = CheckpointSigningPayload {
        domain: CHECKPOINT_SIGNING_DOMAIN,
        schema_version: CHECKPOINT_SIGNING_SCHEMA_VERSION,
        event_root: &cp.event_root,
        state_root: &cp.state_root,
        height: cp.height,
        finalized_events: cp.finalized_events,
        frontier: &cp.frontier,
    };
    let mut preimage = Vec::new();
    ciborium::ser::into_writer(&payload, &mut preimage).map_err(|e| {
        DagError::Serialization(format!(
            "checkpoint signing payload canonical CBOR serialization failed: {e}"
        ))
    })?;
    Ok(preimage)
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
#[allow(clippy::expect_used)]
mod proptests {
    use proptest::prelude::*;
    use serde::Deserialize;

    use super::*;

    #[derive(Debug, Deserialize)]
    struct CheckpointSigningPayloadForTest {
        domain: String,
        schema_version: u16,
        event_root: Hash256,
        state_root: Hash256,
        height: u64,
        finalized_events: u64,
        frontier: Vec<Hash256>,
    }

    fn arb_hash256() -> impl Strategy<Value = Hash256> {
        any::<[u8; 32]>().prop_map(Hash256::from_bytes)
    }

    fn arb_checkpoint() -> impl Strategy<Value = CheckpointPayload> {
        (
            arb_hash256(),
            arb_hash256(),
            any::<u64>(),
            any::<u64>(),
            prop::collection::vec(arb_hash256(), 0..=8usize),
        )
            .prop_map(
                |(event_root, state_root, height, finalized_events, frontier)| CheckpointPayload {
                    event_root,
                    state_root,
                    height,
                    finalized_events,
                    frontier,
                    validator_sigs: vec![],
                },
            )
    }

    fn preimage(cp: &CheckpointPayload) -> Vec<u8> {
        checkpoint_signing_preimage(cp).expect("test checkpoint preimage must encode")
    }

    fn decoded_preimage(cp: &CheckpointPayload) -> CheckpointSigningPayloadForTest {
        ciborium::from_reader(&preimage(cp)[..])
            .expect("checkpoint signing preimage must decode as CBOR")
    }

    proptest! {
        #![proptest_config(proptest::test_runner::Config::with_cases(100))]

        /// The preimage function must be pure: same input → same bytes.
        #[test]
        fn preimage_is_deterministic(cp in arb_checkpoint()) {
            let p1 = preimage(&cp);
            let p2 = preimage(&cp);
            prop_assert_eq!(p1, p2);
        }

        /// Domain separation tag must always be carried inside the CBOR payload.
        #[test]
        fn preimage_carries_domain_sep(cp in arb_checkpoint()) {
            let decoded = decoded_preimage(&cp);
            prop_assert_eq!(decoded.domain.as_bytes(), CHECKPOINT_DOMAIN_SEP);
            prop_assert_eq!(decoded.schema_version, CHECKPOINT_SIGNING_SCHEMA_VERSION);
        }

        /// The CBOR envelope must preserve every checkpoint field exactly.
        #[test]
        fn preimage_cbor_envelope_preserves_all_fields(cp in arb_checkpoint()) {
            let decoded = decoded_preimage(&cp);
            prop_assert_eq!(decoded.event_root, cp.event_root);
            prop_assert_eq!(decoded.state_root, cp.state_root);
            prop_assert_eq!(decoded.height, cp.height);
            prop_assert_eq!(decoded.finalized_events, cp.finalized_events);
            prop_assert_eq!(decoded.frontier, cp.frontier);
        }

        /// Any change to `height` must change the preimage.
        #[test]
        fn different_heights_produce_different_preimages(
            mut cp in arb_checkpoint(),
            alt_height in any::<u64>(),
        ) {
            prop_assume!(cp.height != alt_height);
            let p1 = preimage(&cp);
            cp.height = alt_height;
            let p2 = preimage(&cp);
            prop_assert_ne!(p1, p2);
        }

        /// Any change to `event_root` must change the preimage.
        #[test]
        fn different_event_roots_produce_different_preimages(
            mut cp in arb_checkpoint(),
            alt_root in arb_hash256(),
        ) {
            prop_assume!(cp.event_root != alt_root);
            let p1 = preimage(&cp);
            cp.event_root = alt_root;
            let p2 = preimage(&cp);
            prop_assert_ne!(p1, p2);
        }

        /// Any change to `state_root` must change the preimage.
        #[test]
        fn different_state_roots_produce_different_preimages(
            mut cp in arb_checkpoint(),
            alt_root in arb_hash256(),
        ) {
            prop_assume!(cp.state_root != alt_root);
            let p1 = preimage(&cp);
            cp.state_root = alt_root;
            let p2 = preimage(&cp);
            prop_assert_ne!(p1, p2);
        }

        /// Any change to `finalized_events` must change the preimage.
        #[test]
        fn different_finalized_events_produce_different_preimages(
            mut cp in arb_checkpoint(),
            alt_count in any::<u64>(),
        ) {
            prop_assume!(cp.finalized_events != alt_count);
            let p1 = preimage(&cp);
            cp.finalized_events = alt_count;
            let p2 = preimage(&cp);
            prop_assert_ne!(p1, p2);
        }
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use serde::Deserialize;

    use super::*;

    #[derive(Debug, Deserialize)]
    struct CheckpointSigningPayloadForTest {
        domain: String,
        schema_version: u16,
        event_root: Hash256,
        state_root: Hash256,
        height: u64,
        finalized_events: u64,
        frontier: Vec<Hash256>,
    }

    fn test_did() -> Did {
        Did::new("did:exo:validator1").expect("valid")
    }

    fn preimage(cp: &CheckpointPayload) -> Vec<u8> {
        checkpoint_signing_preimage(cp).expect("test checkpoint preimage must encode")
    }

    fn decode_signing_payload(preimage: &[u8]) -> CheckpointSigningPayloadForTest {
        ciborium::from_reader(preimage)
            .expect("checkpoint signing preimage must be a canonical CBOR payload")
    }

    #[test]
    fn preimage_deterministic() {
        let cp = CheckpointPayload {
            event_root: Hash256::digest(b"events"),
            state_root: Hash256::digest(b"state"),
            height: 42,
            finalized_events: 100,
            frontier: vec![Hash256::digest(b"tip1"), Hash256::digest(b"tip2")],
            validator_sigs: vec![],
        };

        let p1 = preimage(&cp);
        let p2 = preimage(&cp);
        assert_eq!(p1, p2, "preimage must be deterministic");
    }

    #[test]
    fn preimage_is_domain_separated_versioned_cbor() {
        let event_root = Hash256::digest(b"events");
        let state_root = Hash256::digest(b"state");
        let frontier = vec![Hash256::digest(b"tip1"), Hash256::digest(b"tip2")];
        let cp = CheckpointPayload {
            event_root,
            state_root,
            height: 42,
            finalized_events: 100,
            frontier: frontier.clone(),
            validator_sigs: vec![],
        };

        let decoded = decode_signing_payload(&preimage(&cp));

        assert_eq!(
            decoded.domain.as_bytes(),
            CHECKPOINT_DOMAIN_SEP,
            "checkpoint signing payload must carry the checkpoint domain tag"
        );
        assert_eq!(
            decoded.schema_version, 1,
            "checkpoint signing payload must carry an explicit schema version"
        );
        assert_eq!(decoded.event_root, event_root);
        assert_eq!(decoded.state_root, state_root);
        assert_eq!(decoded.height, 42);
        assert_eq!(decoded.finalized_events, 100);
        assert_eq!(decoded.frontier, frontier);
    }

    #[test]
    fn preimage_carries_domain_sep() {
        let cp = CheckpointPayload {
            event_root: Hash256::ZERO,
            state_root: Hash256::ZERO,
            height: 0,
            finalized_events: 0,
            frontier: vec![],
            validator_sigs: vec![],
        };

        let decoded = decode_signing_payload(&preimage(&cp));
        assert_eq!(decoded.domain.as_bytes(), CHECKPOINT_DOMAIN_SEP);
        assert_eq!(decoded.schema_version, CHECKPOINT_SIGNING_SCHEMA_VERSION);
    }

    #[test]
    fn preimage_includes_frontier() {
        let tip = Hash256::digest(b"frontier-tip");
        let cp_with = CheckpointPayload {
            event_root: Hash256::ZERO,
            state_root: Hash256::ZERO,
            height: 1,
            finalized_events: 1,
            frontier: vec![tip],
            validator_sigs: vec![],
        };
        let cp_without = CheckpointPayload {
            event_root: Hash256::ZERO,
            state_root: Hash256::ZERO,
            height: 1,
            finalized_events: 1,
            frontier: vec![],
            validator_sigs: vec![],
        };

        assert_ne!(
            preimage(&cp_with),
            preimage(&cp_without),
            "frontier must affect preimage"
        );
    }

    #[test]
    fn preimage_decodes_frontier_hashes_in_order() {
        let tip1 = Hash256::digest(b"frontier-tip-1");
        let tip2 = Hash256::digest(b"frontier-tip-2");
        let cp = CheckpointPayload {
            event_root: Hash256::digest(b"events"),
            state_root: Hash256::digest(b"state"),
            height: 7,
            finalized_events: 11,
            frontier: vec![tip1, tip2],
            validator_sigs: vec![],
        };

        let decoded = decode_signing_payload(&preimage(&cp));

        assert_eq!(decoded.frontier, vec![tip1, tip2]);
    }

    #[test]
    fn checkpoint_preimage_uses_cbor_instead_of_raw_concatenation() {
        let production = include_str!("checkpoint.rs");
        let preimage_section = production
            .split("pub fn checkpoint_signing_preimage")
            .nth(1)
            .expect("checkpoint_signing_preimage function must exist")
            .split("// ===========================================================================")
            .next()
            .expect("test separator must follow checkpoint_signing_preimage");

        assert!(
            !preimage_section.contains("unwrap_or(u64::MAX)"),
            "checkpoint signing preimage must fail closed instead of saturating frontier length"
        );
        assert!(
            preimage_section.contains("ciborium::ser::into_writer"),
            "checkpoint signing preimage must use canonical CBOR serialization"
        );
        assert!(
            !preimage_section.contains("extend_from_slice"),
            "checkpoint signing preimage must not use ad hoc byte concatenation"
        );
        assert!(
            !preimage_section.contains("to_le_bytes"),
            "checkpoint signing preimage must not hand-roll integer byte layouts"
        );
    }

    #[test]
    fn checkpoint_debug_redacts_validator_signature_material() {
        let checkpoint = CheckpointPayload {
            event_root: Hash256::digest(b"events"),
            state_root: Hash256::digest(b"state"),
            height: 7,
            finalized_events: 11,
            frontier: vec![Hash256::digest(b"frontier-tip")],
            validator_sigs: vec![ValidatorSignature {
                validator_did: test_did(),
                key_version: 1,
                signature: Signature::from_bytes([0xAB; 64]),
            }],
        };

        let checkpoint_debug = format!("{checkpoint:?}");
        let validator_debug = format!("{:?}", checkpoint.validator_sigs[0]);

        assert!(
            checkpoint_debug.contains("validator_sig_count: 1"),
            "Checkpoint Debug output should expose signature count, not signature bodies"
        );
        assert!(
            validator_debug.contains("signature: \"<redacted>\""),
            "ValidatorSignature Debug output must explicitly redact signature material"
        );
        assert!(
            !checkpoint_debug.contains("Signature::Ed25519")
                && !validator_debug.contains("Signature::Ed25519"),
            "Debug output must not delegate to Signature Debug for checkpoint signatures"
        );
        assert!(
            !checkpoint_debug.contains("abab") && !validator_debug.contains("abab"),
            "Debug output must not expose validator signature byte prefixes"
        );
    }

    #[test]
    fn validator_signature_construction() {
        let sig = ValidatorSignature {
            validator_did: test_did(),
            key_version: 1,
            signature: Signature::Empty,
        };
        assert_eq!(sig.key_version, 1);
    }
}