chio-store-sqlite 0.1.2

SQLite-backed persistence, query, and report implementations for Chio
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
use super::*;

fn replace_base_checkpoint(
    connection: &Connection,
    replacement: &KernelCheckpoint,
) -> Result<(), ReceiptStoreError> {
    let statement_json = serde_json::to_string(&replacement.body)?;
    let signature = replacement.signature.to_hex();
    connection.execute_batch(
        r#"
        DROP TRIGGER IF EXISTS kernel_checkpoints_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_tree_heads_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_publication_metadata_reject_update;
        "#,
    )?;
    connection.execute(
        "UPDATE kernel_checkpoints
         SET batch_start_seq = ?1, batch_end_seq = ?2, tree_size = ?3,
             merkle_root = ?4, issued_at = ?5, statement_json = ?6,
             signature = ?7, kernel_key = ?8
         WHERE checkpoint_seq = 1",
        rusqlite::params![
            replacement.body.batch_start_seq as i64,
            replacement.body.batch_end_seq as i64,
            replacement.body.tree_size as i64,
            replacement.body.merkle_root.to_hex(),
            replacement.body.issued_at as i64,
            statement_json,
            signature,
            replacement.body.kernel_key.to_hex(),
        ],
    )?;
    connection.execute(
        "UPDATE checkpoint_tree_heads
         SET batch_start_seq = ?1, batch_end_seq = ?2, tree_size = ?3,
             merkle_root = ?4, issued_at = ?5, kernel_key = ?6,
             previous_checkpoint_sha256 = NULL, statement_json = ?7,
             signature = ?8
         WHERE checkpoint_seq = 1",
        rusqlite::params![
            replacement.body.batch_start_seq as i64,
            replacement.body.batch_end_seq as i64,
            replacement.body.tree_size as i64,
            replacement.body.merkle_root.to_hex(),
            replacement.body.issued_at as i64,
            replacement.body.kernel_key.to_hex(),
            statement_json,
            signature,
        ],
    )?;
    connection.execute(
        "UPDATE checkpoint_publication_metadata
         SET merkle_root = ?1, published_at = ?2, kernel_key = ?3,
             log_tree_size = ?4, entry_start_seq = ?5, entry_end_seq = ?6,
             previous_checkpoint_sha256 = NULL
         WHERE checkpoint_seq = 1",
        rusqlite::params![
            replacement.body.merkle_root.to_hex(),
            replacement.body.issued_at as i64,
            replacement.body.kernel_key.to_hex(),
            replacement.body.batch_end_seq as i64,
            replacement.body.batch_start_seq as i64,
            replacement.body.batch_end_seq as i64,
        ],
    )?;
    ensure_checkpoint_transparency_guards(connection)?;
    ensure_transparency_projection_guards(connection)
}

#[test]
fn rejected_checkpoint_commits_restored_guards_but_not_candidate(
) -> Result<(), Box<dyn std::error::Error>> {
    let (temp_dir, path) = temp_db("chio-checkpoint-guard-savepoint")?;
    let keypair = receipt_test_keypair();
    let store = SqliteReceiptStore::open(&path)?;
    for i in 0..4u64 {
        store.append_chio_receipt_returning_seq(&sample_receipt_with_keypair(
            &format!("guard-savepoint-{i}"),
            i + 1,
            &keypair,
        ))?;
    }
    store.flush_receipt_writes()?;
    store.create_next_receipt_checkpoint(2, &keypair)?;
    let checkpoint_one = store
        .load_checkpoint_by_seq(1)?
        .ok_or("checkpoint 1 missing")?;
    let checkpoint_two = build_checkpoint_with_previous(
        2,
        3,
        4,
        &canonical_receipt_bytes(&store, 3, 4),
        &keypair,
        Some(&checkpoint_one),
        &[chio_kernel::checkpoint::checkpoint_chain_leaf_hash(
            &checkpoint_one.body,
        )?],
    )?;

    let connection = store.connection()?;
    connection.execute_batch(
        r#"
        DROP TRIGGER IF EXISTS kernel_checkpoints_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_tree_heads_reject_update;
        DROP TRIGGER IF EXISTS kernel_checkpoints_project_tree_head;
        "#,
    )?;
    drop(connection);

    let error = store
        .store_checkpoint(&checkpoint_two)
        .err()
        .ok_or("missing projection must reject checkpoint 2")?;
    assert!(
        error.to_string().contains("projection") && error.to_string().contains("missing"),
        "unexpected checkpoint rejection: {error}"
    );

    // Read sqlite_master directly. Calling a store API here could restore a
    // missing checkpoint guard and mask a rollback bug.
    let connection = store.connection()?;
    for trigger in [
        "kernel_checkpoints_reject_update",
        "checkpoint_tree_heads_reject_update",
    ] {
        let present: bool = connection.query_row(
            "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'trigger' AND name = ?1)",
            rusqlite::params![trigger],
            |row| row.get(0),
        )?;
        assert!(present, "{trigger} must survive candidate rejection");
    }
    let checkpoint_two_count: i64 = connection.query_row(
        "SELECT COUNT(*) FROM kernel_checkpoints WHERE checkpoint_seq = 2",
        [],
        |row| row.get(0),
    )?;
    let projection_two_count: i64 = connection.query_row(
        "SELECT COUNT(*) FROM checkpoint_tree_heads WHERE checkpoint_seq = 2",
        [],
        |row| row.get(0),
    )?;
    assert_eq!(
        (checkpoint_two_count, projection_two_count),
        (0, 0),
        "the inner savepoint must roll back the rejected candidate"
    );
    let checkpoint_update = connection
        .execute(
            "UPDATE kernel_checkpoints SET issued_at = issued_at WHERE checkpoint_seq = 1",
            [],
        )
        .err()
        .ok_or("checkpoint immutability guard must block UPDATE")?;
    assert!(checkpoint_update
        .to_string()
        .contains("kernel checkpoints are immutable"));
    let projection_update = connection
        .execute(
            "UPDATE checkpoint_tree_heads SET issued_at = issued_at WHERE checkpoint_seq = 1",
            [],
        )
        .err()
        .ok_or("projection immutability guard must block UPDATE")?;
    assert!(projection_update
        .to_string()
        .contains("checkpoint tree heads are immutable"));

    drop(connection);
    drop(store);
    temp_dir.close()?;
    Ok(())
}

#[test]
fn panicked_checkpoint_commits_restored_guards_but_not_candidate(
) -> Result<(), Box<dyn std::error::Error>> {
    let (temp_dir, path) = temp_db("chio-checkpoint-guard-panic")?;
    let keypair = receipt_test_keypair();
    let store = SqliteReceiptStore::open(&path)?;
    let max_batch = test_hooks::PANIC_DURING_CHECKPOINT_BUILD_MARKER_MAX_BATCH;
    for i in 0..(max_batch * 2) {
        store.append_chio_receipt_returning_seq(&sample_receipt_with_keypair(
            &format!("guard-panic-{i}"),
            i + 1,
            &keypair,
        ))?;
    }
    store.flush_receipt_writes()?;
    store.create_next_receipt_checkpoint(max_batch, &keypair)?;
    let checkpoint_one = store
        .load_checkpoint_by_seq(1)?
        .ok_or("checkpoint 1 missing")?;

    let mut connection = store.connection()?;
    let mut head = seed_verified_head(&connection)?;
    head.chain_frontier = None;
    connection.execute_batch(
        r#"
        DROP TRIGGER IF EXISTS kernel_checkpoints_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_tree_heads_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_publication_metadata_reject_update;
        "#,
    )?;

    test_hooks::PANIC_DURING_CHECKPOINT_BUILD.store(true, std::sync::atomic::Ordering::SeqCst);
    let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        build_checkpoint_after_frontier_cache_miss(
            &mut connection,
            &mut head,
            &signer(&keypair, max_batch),
        )
    }));
    test_hooks::PANIC_DURING_CHECKPOINT_BUILD.store(false, std::sync::atomic::Ordering::SeqCst);
    let panic_payload = panic
        .err()
        .ok_or("the injected checkpoint panic must resume")?;
    let panic_message = panic_payload
        .downcast_ref::<&str>()
        .map(|message| (*message).to_string())
        .or_else(|| panic_payload.downcast_ref::<String>().cloned())
        .ok_or("checkpoint panic payload must be a string")?;
    assert_eq!(
        panic_message, "injected test panic during background checkpoint build",
        "the wrapper must resume the original panic payload"
    );
    assert_eq!(
        head.latest_checkpoint.as_ref(),
        Some(&checkpoint_one),
        "a panicked candidate must not publish a new verified head"
    );
    assert!(
        head.chain_frontier.is_none(),
        "a panicked cache-miss build must preserve the live frontier"
    );

    // Inspect the database directly so a store API cannot recreate a missing
    // guard and hide a rollback bug.
    for trigger in [
        "kernel_checkpoints_reject_update",
        "checkpoint_tree_heads_reject_update",
        "checkpoint_publication_metadata_reject_update",
    ] {
        let present: bool = connection.query_row(
            "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'trigger' AND name = ?1)",
            rusqlite::params![trigger],
            |row| row.get(0),
        )?;
        assert!(present, "{trigger} must survive the resumed panic");
    }
    let candidate_rows: (i64, i64) = connection.query_row(
        "SELECT
             (SELECT COUNT(*) FROM kernel_checkpoints WHERE checkpoint_seq = 2),
             (SELECT COUNT(*) FROM checkpoint_tree_heads WHERE checkpoint_seq = 2)",
        [],
        |row| Ok((row.get(0)?, row.get(1)?)),
    )?;
    assert_eq!(
        candidate_rows,
        (0, 0),
        "the panicked candidate savepoint must roll back"
    );
    let checkpoint_update = connection
        .execute(
            "UPDATE kernel_checkpoints SET issued_at = issued_at WHERE checkpoint_seq = 1",
            [],
        )
        .err()
        .ok_or("checkpoint UPDATE must remain guarded after panic")?;
    assert!(checkpoint_update
        .to_string()
        .contains("kernel checkpoints are immutable"));
    let tree_update = connection
        .execute(
            "UPDATE checkpoint_tree_heads SET issued_at = issued_at WHERE checkpoint_seq = 1",
            [],
        )
        .err()
        .ok_or("tree-head UPDATE must remain guarded after panic")?;
    assert!(tree_update
        .to_string()
        .contains("checkpoint tree heads are immutable"));
    let publication_update = connection
        .execute(
            "UPDATE checkpoint_publication_metadata
             SET published_at = published_at
             WHERE checkpoint_seq = 1",
            [],
        )
        .err()
        .ok_or("publication UPDATE must remain guarded after panic")?;
    assert!(publication_update
        .to_string()
        .contains("checkpoint publication metadata is immutable"));

    drop(connection);
    drop(store);
    temp_dir.close()?;
    Ok(())
}

#[test]
fn panicked_savepoint_write_rolls_back_candidate_and_resumes_payload(
) -> Result<(), Box<dyn std::error::Error>> {
    const PANIC_MARKER: &str = "checkpoint candidate write panic marker";

    let (temp_dir, path) = temp_db("chio-checkpoint-savepoint-write-panic")?;
    let keypair = receipt_test_keypair();
    let store = SqliteReceiptStore::open(&path)?;
    for i in 0..4u64 {
        store.append_chio_receipt_returning_seq(&sample_receipt_with_keypair(
            &format!("savepoint-write-panic-{i}"),
            i + 1,
            &keypair,
        ))?;
    }
    store.flush_receipt_writes()?;
    store.create_next_receipt_checkpoint(2, &keypair)?;
    let checkpoint_one = store
        .load_checkpoint_by_seq(1)?
        .ok_or("checkpoint 1 missing")?;
    let checkpoint_two = build_checkpoint_with_previous(
        2,
        3,
        4,
        &canonical_receipt_bytes(&store, 3, 4),
        &keypair,
        Some(&checkpoint_one),
        &[chio_kernel::checkpoint::checkpoint_chain_leaf_hash(
            &checkpoint_one.body,
        )?],
    )?;

    let mut connection = store.connection()?;
    connection.execute_batch(
        r#"
        DROP TRIGGER IF EXISTS kernel_checkpoints_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_tree_heads_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_publication_metadata_reject_update;
        "#,
    )?;
    let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        checkpoint_guarded_immediate::<()>(&mut connection, |tx| {
            let inserted =
                insert_checkpoint_incremental_tx(tx, Some(&checkpoint_one), &checkpoint_two)?;
            if inserted != checkpoint_two {
                return Err(ReceiptStoreError::Conflict(
                    "candidate insert did not persist the expected checkpoint".to_string(),
                ));
            }
            std::panic::panic_any(PANIC_MARKER);
        })
    }));
    let panic_payload = panic
        .err()
        .ok_or("the post-insert savepoint panic must resume")?;
    assert_eq!(
        panic_payload.downcast_ref::<&str>().copied(),
        Some(PANIC_MARKER),
        "the wrapper must resume the original post-write panic payload"
    );

    let candidate_rows: (i64, i64, i64, i64) = connection.query_row(
        "SELECT
             (SELECT COUNT(*) FROM kernel_checkpoints WHERE checkpoint_seq = 2),
             (SELECT COUNT(*) FROM checkpoint_tree_heads WHERE checkpoint_seq = 2),
             (SELECT COUNT(*) FROM checkpoint_predecessor_witnesses
              WHERE witness_checkpoint_seq = 2),
             (SELECT COUNT(*) FROM checkpoint_publication_metadata
              WHERE checkpoint_seq = 2)",
        [],
        |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
    )?;
    assert_eq!(
        candidate_rows,
        (0, 0, 0, 0),
        "every candidate write must roll back with the panicked savepoint"
    );
    for trigger in [
        "kernel_checkpoints_reject_update",
        "checkpoint_tree_heads_reject_update",
        "checkpoint_publication_metadata_reject_update",
    ] {
        let present: bool = connection.query_row(
            "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'trigger' AND name = ?1)",
            rusqlite::params![trigger],
            |row| row.get(0),
        )?;
        assert!(present, "{trigger} must survive the post-write panic");
    }
    let checkpoint_update = connection
        .execute(
            "UPDATE kernel_checkpoints SET issued_at = issued_at WHERE checkpoint_seq = 1",
            [],
        )
        .err()
        .ok_or("checkpoint UPDATE must remain guarded after post-write panic")?;
    assert!(checkpoint_update
        .to_string()
        .contains("kernel checkpoints are immutable"));

    drop(connection);
    drop(store);
    temp_dir.close()?;
    Ok(())
}

#[test]
fn cache_miss_holds_insert_lock_across_interior_projection_audit(
) -> Result<(), Box<dyn std::error::Error>> {
    let (temp_dir, path) = temp_db("chio-checkpoint-cache-miss-interior")?;
    let keypair = receipt_test_keypair();
    let store = SqliteReceiptStore::open(&path)?;
    for i in 0..6u64 {
        store.append_chio_receipt_returning_seq(&sample_receipt_with_keypair(
            &format!("cache-miss-interior-{i}"),
            i + 1,
            &keypair,
        ))?;
    }
    store.flush_receipt_writes()?;
    store.create_next_receipt_checkpoint(2, &keypair)?;
    store.create_next_receipt_checkpoint(2, &keypair)?;

    let mut connection = store.connection()?;
    let mut head = seed_verified_head(&connection)?;
    head.chain_frontier = None;

    // Remove the interior projection guard before the cache-miss operation.
    // The outer transaction restores it, but that DDL is not visible to this
    // peer until commit. If the write lock did not span audit through insert,
    // the peer's exact mid-operation UPDATE would commit in the old gap.
    let peer = store.connection()?;
    peer.busy_timeout(Duration::ZERO)?;
    peer.execute_batch("DROP TRIGGER IF EXISTS checkpoint_publication_metadata_reject_update;")?;
    let mut peer_blocked = false;
    let (frontier, advanced) = build_checkpoint_after_frontier_cache_miss_with_hook(
        &mut connection,
        &mut head,
        &signer(&keypair, 2),
        |_| {
            let error = peer
                .execute(
                    "UPDATE checkpoint_publication_metadata
                     SET published_at = published_at + 1
                     WHERE checkpoint_seq = 1",
                    [],
                )
                .err()
                .ok_or_else(|| {
                    ReceiptStoreError::Conflict(
                        "peer mutated an audited projection before checkpoint insert".to_string(),
                    )
                })?;
            match error {
                rusqlite::Error::SqliteFailure(sqlite_error, _)
                    if matches!(
                        sqlite_error.code,
                        rusqlite::ErrorCode::DatabaseBusy | rusqlite::ErrorCode::DatabaseLocked
                    ) =>
                {
                    peer_blocked = true;
                    Ok(())
                }
                other => Err(ReceiptStoreError::Conflict(format!(
                    "unexpected peer write result during cache-miss audit: {other}"
                ))),
            }
        },
    )?;
    assert!(peer_blocked, "the peer write attempt must reach SQLite");
    assert!(advanced, "the owed checkpoint must commit");
    assert_eq!(frontier.leaf_count(), 3);
    assert_eq!(head.checkpoint_seq(), 3);
    assert!(
        load_persisted_checkpoint_row(&connection, 3)?.is_some(),
        "checkpoint 3 must persist after the locked audit"
    );
    verify_checkpoint_chain_integrity(&connection)?;

    let post_commit_error = peer
        .execute(
            "UPDATE checkpoint_publication_metadata
             SET published_at = published_at + 1
             WHERE checkpoint_seq = 1",
            [],
        )
        .err()
        .ok_or("restored projection guard must reject UPDATE after commit")?;
    assert!(
        post_commit_error
            .to_string()
            .contains("checkpoint publication metadata is immutable"),
        "unexpected post-commit projection update result: {post_commit_error}"
    );

    drop(peer);
    drop(connection);
    drop(store);
    temp_dir.close()?;
    Ok(())
}

#[test]
fn cache_miss_uses_locked_legacy_replacement_frontier_when_no_build_is_due(
) -> Result<(), Box<dyn std::error::Error>> {
    let (temp_dir, path) = temp_db("chio-checkpoint-cache-miss-legacy")?;
    let keypair = receipt_test_keypair();
    let store = SqliteReceiptStore::open(&path)?;
    for i in 0..2u64 {
        store.append_chio_receipt_returning_seq(&sample_receipt_with_keypair(
            &format!("cache-miss-legacy-{i}"),
            i + 1,
            &keypair,
        ))?;
    }
    store.flush_receipt_writes()?;

    let connection = store.connection()?;
    let mut head = seed_verified_head(&connection)?;
    drop(connection);
    let mut checkpoint_a =
        build_checkpoint(1, 1, 1, &canonical_receipt_bytes(&store, 1, 1), &keypair)?;
    checkpoint_a.body.schema = chio_kernel::checkpoint::CHECKPOINT_SCHEMA_V1.to_string();
    checkpoint_a.body.chain_root = None;
    checkpoint_a.signature = keypair.sign(&canonical_json_bytes(&checkpoint_a.body)?);
    insert_checkpoint_row(&store, &checkpoint_a, checkpoint_a.body.batch_end_seq);

    let mut connection = store.connection()?;
    let stale_frontier = rebuild_checkpoint_frontier(&mut connection, None)?;
    let mut checkpoint_b =
        build_checkpoint(1, 1, 2, &canonical_receipt_bytes(&store, 1, 2), &keypair)?;
    checkpoint_b.body.schema = chio_kernel::checkpoint::CHECKPOINT_SCHEMA_V1.to_string();
    checkpoint_b.body.chain_root = None;
    checkpoint_b.signature = keypair.sign(&canonical_json_bytes(&checkpoint_b.body)?);
    assert_ne!(
        chio_kernel::checkpoint::checkpoint_chain_leaf_hash(&checkpoint_a.body)?,
        chio_kernel::checkpoint::checkpoint_chain_leaf_hash(&checkpoint_b.body)?,
        "the replacement must change a chain-leaf-bound field"
    );
    replace_base_checkpoint(&connection, &checkpoint_b)?;
    assert_eq!(
        verify_checkpoint_chain_integrity(&connection)?.as_ref(),
        Some(&checkpoint_b)
    );

    head.chain_frontier = None;
    let advanced = maybe_build_checkpoint(&mut connection, &mut head, &signer(&keypair, 1))?;
    assert!(advanced, "the locked audit must adopt checkpoint B");
    assert_eq!(head.latest_checkpoint.as_ref(), Some(&checkpoint_b));
    let adopted_frontier = head
        .chain_frontier
        .as_ref()
        .ok_or("adopted legacy frontier missing")?;
    assert_ne!(
        adopted_frontier.root(),
        stale_frontier.root(),
        "the stale A frontier must not overwrite the caught-up B frontier"
    );
    let expected_frontier = CheckpointChainFrontier::from_leaves(&[
        chio_kernel::checkpoint::checkpoint_chain_leaf_hash(&checkpoint_b.body)?,
    ]);
    assert_eq!(adopted_frontier.root(), expected_frontier.root());
    assert!(
        load_persisted_checkpoint_row(&connection, 2)?.is_none(),
        "checkpoint B covers every receipt, so no successor is due"
    );

    drop(connection);
    drop(store);
    temp_dir.close()?;
    Ok(())
}

#[test]
fn cache_hit_adopts_valid_different_batch_winner_frontier() -> Result<(), Box<dyn std::error::Error>>
{
    let (temp_dir, path) = temp_db("chio-checkpoint-different-batch-hit")?;
    let keypair = receipt_test_keypair();
    let store = SqliteReceiptStore::open(&path)?;
    for i in 0..5u64 {
        store.append_chio_receipt_returning_seq(&sample_receipt_with_keypair(
            &format!("different-batch-hit-{i}"),
            i + 1,
            &keypair,
        ))?;
    }
    store.flush_receipt_writes()?;
    store.create_next_receipt_checkpoint(2, &keypair)?;
    let checkpoint_one = store
        .load_checkpoint_by_seq(1)?
        .ok_or("checkpoint 1 missing")?;
    let chain_leaves = [chio_kernel::checkpoint::checkpoint_chain_leaf_hash(
        &checkpoint_one.body,
    )?];
    let prior_frontier = CheckpointChainFrontier::from_leaves(&chain_leaves);
    let loser = build_checkpoint_with_previous(
        2,
        3,
        4,
        &canonical_receipt_bytes(&store, 3, 4),
        &keypair,
        Some(&checkpoint_one),
        &chain_leaves,
    )?;
    let winner = build_checkpoint_with_previous(
        2,
        3,
        5,
        &canonical_receipt_bytes(&store, 3, 5),
        &keypair,
        Some(&checkpoint_one),
        &chain_leaves,
    )?;
    assert_ne!(
        loser.body.chain_root, winner.body.chain_root,
        "different valid batch lengths must produce different chain roots"
    );
    store.store_checkpoint(&winner)?;

    let mut connection = store.connection()?;
    let (adopted, adopted_frontier) = insert_background_checkpoint_guarded(
        &mut connection,
        Some(&checkpoint_one),
        &prior_frontier,
        &loser,
    )?;
    assert_eq!(adopted, winner);
    assert_eq!(adopted_frontier.root(), winner.body.chain_root);
    assert_ne!(adopted_frontier.root(), loser.body.chain_root);
    assert_eq!(
        parse_persisted_checkpoint_row(
            load_persisted_checkpoint_row(&connection, 2)?
                .ok_or("persisted checkpoint 2 missing")?,
        )?,
        winner
    );
    assert!(load_persisted_checkpoint_row(&connection, 3)?.is_none());
    verify_checkpoint_chain_integrity(&connection)?;

    drop(connection);
    drop(store);
    temp_dir.close()?;
    Ok(())
}

#[test]
fn cache_miss_adopts_valid_different_batch_winner_frontier(
) -> Result<(), Box<dyn std::error::Error>> {
    let (temp_dir, path) = temp_db("chio-checkpoint-different-batch-miss")?;
    let keypair = receipt_test_keypair();
    let store = SqliteReceiptStore::open(&path)?;
    for i in 0..5u64 {
        store.append_chio_receipt_returning_seq(&sample_receipt_with_keypair(
            &format!("different-batch-miss-{i}"),
            i + 1,
            &keypair,
        ))?;
    }
    store.flush_receipt_writes()?;
    store.create_next_receipt_checkpoint(2, &keypair)?;
    let checkpoint_one = store
        .load_checkpoint_by_seq(1)?
        .ok_or("checkpoint 1 missing")?;
    let chain_leaves = [chio_kernel::checkpoint::checkpoint_chain_leaf_hash(
        &checkpoint_one.body,
    )?];
    let loser = build_checkpoint_with_previous(
        2,
        3,
        4,
        &canonical_receipt_bytes(&store, 3, 4),
        &keypair,
        Some(&checkpoint_one),
        &chain_leaves,
    )?;
    let winner = build_checkpoint_with_previous(
        2,
        3,
        5,
        &canonical_receipt_bytes(&store, 3, 5),
        &keypair,
        Some(&checkpoint_one),
        &chain_leaves,
    )?;
    assert_ne!(loser.body.chain_root, winner.body.chain_root);

    let mut connection = store.connection()?;
    let mut head = seed_verified_head(&connection)?;
    head.chain_frontier = None;
    let (frontier, advanced) = build_checkpoint_after_frontier_cache_miss_with_hook(
        &mut connection,
        &mut head,
        &signer(&keypair, 2),
        |tx| {
            let inserted = insert_checkpoint_incremental_tx(tx, Some(&checkpoint_one), &winner)?;
            if inserted != winner {
                return Err(ReceiptStoreError::Conflict(
                    "different-batch winner injection diverged".to_string(),
                ));
            }
            Ok(())
        },
    )?;
    assert!(advanced);
    assert_eq!(frontier.leaf_count(), 2);
    assert_eq!(frontier.root(), winner.body.chain_root);
    assert_ne!(frontier.root(), loser.body.chain_root);
    assert_eq!(head.latest_checkpoint.as_ref(), Some(&winner));
    assert_eq!(head.chain_frontier.as_ref(), Some(&frontier));
    assert_eq!(head.checkpointed_entry_seq(), 5);
    assert_eq!(
        parse_persisted_checkpoint_row(
            load_persisted_checkpoint_row(&connection, 2)?
                .ok_or("persisted checkpoint 2 missing")?,
        )?,
        winner
    );
    assert!(load_persisted_checkpoint_row(&connection, 3)?.is_none());
    verify_checkpoint_chain_integrity(&connection)?;

    drop(connection);
    drop(store);
    temp_dir.close()?;
    Ok(())
}

#[test]
fn archived_peer_checkpoint_winner_is_authenticated_before_adoption(
) -> Result<(), Box<dyn std::error::Error>> {
    let (temp_dir, path) = temp_db("chio-checkpoint-archived-winner")?;
    let archive = unique_db_path("chio-checkpoint-archived-winner-archive");
    let archive_path = archive.to_str().ok_or("archive path invalid")?;
    let keypair = receipt_test_keypair();
    let store = SqliteReceiptStore::open(&path)?;
    for i in 0..5u64 {
        let timestamp = if i < 4 { 100 } else { 500 };
        store.append_chio_receipt_returning_seq(&sample_receipt_with_keypair_and_timestamp(
            &format!("archived-winner-{i}"),
            i + 1,
            timestamp,
            &keypair,
        ))?;
    }
    store.flush_receipt_writes()?;
    store.create_next_receipt_checkpoint(2, &keypair)?;
    let checkpoint_one = store
        .load_checkpoint_by_seq(1)?
        .ok_or("checkpoint 1 missing")?;
    let chain_leaves = [chio_kernel::checkpoint::checkpoint_chain_leaf_hash(
        &checkpoint_one.body,
    )?];
    let loser = build_checkpoint_with_previous(
        2,
        3,
        4,
        &canonical_receipt_bytes(&store, 3, 4),
        &keypair,
        Some(&checkpoint_one),
        &chain_leaves,
    )?;
    let mut winner = loser.clone();
    winner.body.issued_at = loser.body.issued_at.saturating_add(1_000);
    winner.signature = keypair.sign(&canonical_json_bytes(&winner.body)?);
    assert_ne!(loser, winner);
    assert_eq!(loser.body.chain_root, winner.body.chain_root);

    let peer = SqliteReceiptStore::open_existing(&path)?;
    peer.store_checkpoint(&winner)?;
    let archived = peer.archive_receipts_before(150, archive_path)?;
    assert_eq!(archived, 4, "the winner's complete prefix must rotate");
    let peer_connection = peer.connection()?;
    assert_eq!(trusted_retention_watermark(&peer_connection)?, 4);
    let live_range: (i64, i64) = peer_connection.query_row(
        "SELECT COUNT(*), COALESCE(MIN(entry_seq), 0)
         FROM claim_receipt_log_entries",
        [],
        |row| Ok((row.get(0)?, row.get(1)?)),
    )?;
    assert_eq!(live_range, (1, 5), "only receipt 5 must remain live");
    drop(peer_connection);

    let prior_frontier = CheckpointChainFrontier::from_leaves(&chain_leaves);
    let mut connection = store.connection()?;
    let (adopted, adopted_frontier) = insert_background_checkpoint_guarded(
        &mut connection,
        Some(&checkpoint_one),
        &prior_frontier,
        &loser,
    )?;
    assert_eq!(
        adopted, winner,
        "the loser must adopt the archived peer winner"
    );
    assert_eq!(adopted_frontier.root(), winner.body.chain_root);

    // Diverge only the live surrogate row id. Checkpoint parsing, receipt
    // signatures, signer binding, and Merkle validation cannot observe this
    // field, so withdrawing trust here specifically proves exact archive-row
    // identity is required.
    let live_winner_row =
        load_persisted_checkpoint_row(&connection, 2)?.ok_or("live winner row missing")?;
    let divergent_id = live_winner_row
        .id
        .checked_add(1_000)
        .ok_or("checkpoint row id overflow")?;
    connection.execute_batch("DROP TRIGGER IF EXISTS kernel_checkpoints_reject_update;")?;
    assert_eq!(
        connection.execute(
            "UPDATE kernel_checkpoints SET id = ?1 WHERE checkpoint_seq = 2",
            rusqlite::params![sqlite_i64(divergent_id, "divergent checkpoint id")?],
        )?,
        1
    );
    assert_eq!(
        trusted_retention_watermark(&connection)?,
        0,
        "an id-only live/archive row mismatch must withdraw trust"
    );
    assert_eq!(
        connection.execute(
            "UPDATE kernel_checkpoints SET id = ?1 WHERE checkpoint_seq = 2",
            rusqlite::params![sqlite_i64(live_winner_row.id, "restored checkpoint id")?],
        )?,
        1
    );
    assert_eq!(
        trusted_retention_watermark(&connection)?,
        4,
        "restoring exact row identity must restore archive trust"
    );

    // Replace the persisted winner and all of its live projections with a
    // checkpoint signed by an attacker. Its range, receipt Merkle root,
    // predecessor link, and chain root remain authentic, so only archived
    // checkpoint identity and receipt-signer binding distinguish it.
    let attacker = Keypair::from_seed(&[0x5d; 32]);
    let mut forged = winner.clone();
    forged.body.kernel_key = attacker.public_key();
    forged.signature = attacker.sign(&canonical_json_bytes(&forged.body)?);
    chio_kernel::checkpoint::validate_checkpoint(&forged)?;
    chio_kernel::checkpoint::validate_checkpoint_predecessor(&checkpoint_one, &forged)?;
    assert_eq!(forged.body.batch_start_seq, winner.body.batch_start_seq);
    assert_eq!(forged.body.batch_end_seq, winner.body.batch_end_seq);
    assert_eq!(forged.body.merkle_root, winner.body.merkle_root);
    assert_eq!(forged.body.chain_root, winner.body.chain_root);
    assert_ne!(forged.body.kernel_key, winner.body.kernel_key);

    let forged_json = serde_json::to_string(&forged.body)?;
    let forged_signature = forged.signature.to_hex();
    let forged_key = forged.body.kernel_key.to_hex();
    connection.execute_batch(
        r#"
        DROP TRIGGER IF EXISTS kernel_checkpoints_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_tree_heads_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_predecessor_witnesses_reject_update;
        DROP TRIGGER IF EXISTS checkpoint_publication_metadata_reject_update;
        "#,
    )?;
    assert_eq!(
        connection.execute(
            "UPDATE kernel_checkpoints
             SET statement_json = ?1, signature = ?2, kernel_key = ?3
             WHERE checkpoint_seq = 2",
            rusqlite::params![&forged_json, &forged_signature, &forged_key],
        )?,
        1
    );
    assert_eq!(
        connection.execute(
            "UPDATE checkpoint_tree_heads
             SET kernel_key = ?1, statement_json = ?2, signature = ?3
             WHERE checkpoint_seq = 2",
            rusqlite::params![&forged_key, &forged_json, &forged_signature],
        )?,
        1
    );
    assert_eq!(
        connection.execute(
            "UPDATE checkpoint_predecessor_witnesses
             SET witness_statement_json = ?1
             WHERE witness_checkpoint_seq = 2",
            rusqlite::params![&forged_json],
        )?,
        1
    );
    assert_eq!(
        connection.execute(
            "UPDATE checkpoint_publication_metadata
             SET kernel_key = ?1
             WHERE checkpoint_seq = 2",
            rusqlite::params![&forged_key],
        )?,
        1
    );
    let live_forged_row =
        load_persisted_checkpoint_row(&connection, 2)?.ok_or("forged checkpoint 2 missing")?;
    assert_eq!(
        parse_persisted_checkpoint_row(live_forged_row.clone())?,
        forged
    );
    validate_checkpoint_projection_rows(&connection, &live_forged_row, &forged)?;
    assert_eq!(
        trusted_retention_watermark(&connection)?,
        0,
        "live/archive checkpoint identity mismatch must withdraw trust"
    );

    // Replace the archived checkpoint row too. Exact row identity and the
    // authentic Merkle root now pass, but the untouched archived receipts are
    // still signed by the original key. Full archived claim-log validation
    // must therefore keep the watermark untrusted.
    let archive_connection = rusqlite::Connection::open(&archive)?;
    assert_eq!(
        archive_connection.execute(
            "UPDATE kernel_checkpoints
             SET statement_json = ?1, signature = ?2, kernel_key = ?3
             WHERE checkpoint_seq = 2",
            rusqlite::params![&forged_json, &forged_signature, &forged_key],
        )?,
        1
    );
    let archived_forged_row = load_persisted_checkpoint_row(&archive_connection, 2)?
        .ok_or("archived forged checkpoint 2 missing")?;
    assert_eq!(archived_forged_row, live_forged_row);
    drop(archive_connection);
    assert_eq!(
        trusted_retention_watermark(&connection)?,
        0,
        "attacker-signed checkpoint must not authenticate original-key archived receipts"
    );

    let error = insert_background_checkpoint_guarded(
        &mut connection,
        Some(&checkpoint_one),
        &prior_frontier,
        &loser,
    )
    .err()
    .ok_or("attacker-signed archived winner must be rejected")?;
    assert!(
        matches!(error, ReceiptStoreError::Conflict(_))
            && error
                .to_string()
                .contains("gap in checkpoint signer binding"),
        "unexpected archived-winner rejection: {error:?}"
    );
    assert_eq!(
        parse_persisted_checkpoint_row(
            load_persisted_checkpoint_row(&connection, 2)?
                .ok_or("persisted forged checkpoint 2 missing after rejection")?,
        )?,
        forged
    );
    for trigger in [
        "kernel_checkpoints_reject_update",
        "checkpoint_tree_heads_reject_update",
        "checkpoint_predecessor_witnesses_reject_update",
        "checkpoint_publication_metadata_reject_update",
    ] {
        let present: bool = connection.query_row(
            "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'trigger' AND name = ?1)",
            rusqlite::params![trigger],
            |row| row.get(0),
        )?;
        assert!(present, "{trigger} must be restored after rejection");
    }

    drop(connection);
    drop(peer);
    drop(store);
    temp_dir.close()?;
    let _ = fs::remove_file(archive);
    Ok(())
}