|
| 1 | +//! End-to-end checks that keyset creation, activation, and boot-time |
| 2 | +//! re-activation emit append-only journal events through the journaling |
| 3 | +//! database wrapper. |
| 4 | +//! |
| 5 | +//! These drive [`DbSignatory`] directly (with a fixed seed and a file-backed |
| 6 | +//! sqlite database) so the boot-time re-activation path in `init_keysets` is |
| 7 | +//! exercised deterministically. That path previously wrote no journal event; it |
| 8 | +//! must now, because journaling is a property of the storage layer. |
| 9 | +
|
| 10 | +use std::collections::HashMap; |
| 11 | +use std::sync::Arc; |
| 12 | + |
| 13 | +use cdk_common::database::event_log::{Delta, Entity, Event, Snapshot}; |
| 14 | +use cdk_common::nut02::KeySetVersion; |
| 15 | +use cdk_common::nuts::CurrencyUnit; |
| 16 | +use cdk_signatory::db_signatory::DbSignatory; |
| 17 | +use cdk_signatory::signatory::{RotateKeyArguments, Signatory}; |
| 18 | + |
| 19 | +use crate::test_helpers::mint::read_journal; |
| 20 | + |
| 21 | +const SEED: &[u8] = b"keyset-journal-test-fixed-seed-000000000000"; |
| 22 | + |
| 23 | +fn sat_units() -> HashMap<CurrencyUnit, (u64, Vec<u64>)> { |
| 24 | + let mut units = HashMap::new(); |
| 25 | + units.insert(CurrencyUnit::Sat, (0u64, vec![1, 2, 4, 8])); |
| 26 | + units |
| 27 | +} |
| 28 | + |
| 29 | +fn rotate_args() -> RotateKeyArguments { |
| 30 | + RotateKeyArguments { |
| 31 | + unit: CurrencyUnit::Sat, |
| 32 | + amounts: vec![1, 2, 4, 8], |
| 33 | + input_fee_ppk: 0, |
| 34 | + keyset_id_type: KeySetVersion::Version00, |
| 35 | + final_expiry: None, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +async fn open_db(file: &str) -> Arc<cdk_sqlite::mint::MintSqliteDatabase> { |
| 40 | + Arc::new( |
| 41 | + cdk_sqlite::mint::MintSqliteDatabase::new(file) |
| 42 | + .await |
| 43 | + .expect("file-backed mint db"), |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +/// Rotating a keyset twice journals a snapshot and activation for each keyset, |
| 48 | +/// plus a deactivation of the keyset that the second rotation supersedes. |
| 49 | +/// Re-opening the database with the same seed re-activates the highest-index |
| 50 | +/// keyset at boot, which must append its own journal events. |
| 51 | +#[tokio::test] |
| 52 | +async fn keyset_rotation_and_boot_reactivation_are_journaled() { |
| 53 | + let file = format!( |
| 54 | + "{}/cdk_journal_keyset_test.sqlite", |
| 55 | + std::env::temp_dir().display() |
| 56 | + ); |
| 57 | + let _ = std::fs::remove_file(&file); |
| 58 | + |
| 59 | + // First signatory: create two keysets; the second supersedes the first. |
| 60 | + let (first_id, second_id) = { |
| 61 | + let signatory = DbSignatory::new(open_db(&file).await, SEED, sat_units(), HashMap::new()) |
| 62 | + .await |
| 63 | + .expect("signatory"); |
| 64 | + |
| 65 | + let first = signatory |
| 66 | + .rotate_keyset(rotate_args()) |
| 67 | + .await |
| 68 | + .expect("rotate 1"); |
| 69 | + let second = signatory |
| 70 | + .rotate_keyset(rotate_args()) |
| 71 | + .await |
| 72 | + .expect("rotate 2"); |
| 73 | + (first.id, second.id) |
| 74 | + }; |
| 75 | + |
| 76 | + let after_rotations = read_journal(&file); |
| 77 | + |
| 78 | + // Both keysets have a creation snapshot. |
| 79 | + for id in [first_id, second_id] { |
| 80 | + let record = id.to_string(); |
| 81 | + assert!( |
| 82 | + after_rotations.iter().any(|(entity, r, e)| *entity == Entity::Keyset |
| 83 | + && *r == record |
| 84 | + && matches!(e, Event::Snapshot(s) if matches!(s.as_ref(), Snapshot::Keyset(_)))), |
| 85 | + "missing keyset snapshot for {record}, journal: {after_rotations:?}" |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + // The second rotation deactivates the first keyset. |
| 90 | + let first_record = first_id.to_string(); |
| 91 | + assert!( |
| 92 | + after_rotations |
| 93 | + .iter() |
| 94 | + .any(|(entity, r, e)| *entity == Entity::Keyset |
| 95 | + && *r == first_record |
| 96 | + && matches!(e, Event::Delta(Delta::KeysetActive(false)))), |
| 97 | + "superseded keyset must be journaled as deactivated, journal: {after_rotations:?}" |
| 98 | + ); |
| 99 | + |
| 100 | + // The second keyset is activated. |
| 101 | + let second_record = second_id.to_string(); |
| 102 | + assert!( |
| 103 | + after_rotations |
| 104 | + .iter() |
| 105 | + .any(|(entity, r, e)| *entity == Entity::Keyset |
| 106 | + && *r == second_record |
| 107 | + && matches!(e, Event::Delta(Delta::KeysetActive(true)))), |
| 108 | + "new keyset must be journaled as activated, journal: {after_rotations:?}" |
| 109 | + ); |
| 110 | + |
| 111 | + // Second signatory on the same database and seed: `init_keysets` re-activates |
| 112 | + // the highest-index keyset at boot. This path emitted no journal event before |
| 113 | + // journaling moved into the storage layer; it must now. |
| 114 | + { |
| 115 | + let _signatory = DbSignatory::new(open_db(&file).await, SEED, sat_units(), HashMap::new()) |
| 116 | + .await |
| 117 | + .expect("reboot signatory"); |
| 118 | + } |
| 119 | + |
| 120 | + let after_reboot = read_journal(&file); |
| 121 | + assert!( |
| 122 | + after_reboot.len() > after_rotations.len(), |
| 123 | + "boot-time keyset re-activation must append journal events (was {}, now {})", |
| 124 | + after_rotations.len(), |
| 125 | + after_reboot.len() |
| 126 | + ); |
| 127 | + assert!( |
| 128 | + after_reboot[after_rotations.len()..] |
| 129 | + .iter() |
| 130 | + .all(|(entity, _, _)| *entity == Entity::Keyset), |
| 131 | + "the new boot events must all be keyset entries, tail: {:?}", |
| 132 | + &after_reboot[after_rotations.len()..] |
| 133 | + ); |
| 134 | + |
| 135 | + let _ = std::fs::remove_file(&file); |
| 136 | +} |
| 137 | + |
| 138 | +/// The decorator rejects direct `add_journal` calls on both transaction kinds: |
| 139 | +/// journaling is driven by the entity mutations, so a direct write from outside |
| 140 | +/// the decorator is a programming error and must fail rather than produce an |
| 141 | +/// unmanaged journal row. |
| 142 | +#[tokio::test] |
| 143 | +async fn direct_add_journal_through_wrapper_is_rejected() { |
| 144 | + use cdk_common::database::event_log::{Delta, Event}; |
| 145 | + use cdk_common::database::{ |
| 146 | + Error as DbError, JournaledDatabase, MintDatabase, MintKeysDatabase, |
| 147 | + }; |
| 148 | + |
| 149 | + // Mint transaction path. |
| 150 | + let journaled = JournaledDatabase::new(Arc::new( |
| 151 | + cdk_sqlite::mint::memory::empty().await.expect("mint db"), |
| 152 | + )); |
| 153 | + let mut tx = MintDatabase::begin_transaction(&journaled) |
| 154 | + .await |
| 155 | + .expect("begin mint tx"); |
| 156 | + let err = tx |
| 157 | + .add_journal("record".to_string(), Event::Delta(Delta::ProofRemoved)) |
| 158 | + .await |
| 159 | + .expect_err("direct add_journal must fail"); |
| 160 | + assert!(matches!(err, DbError::JournalNotPermitted), "got {err:?}"); |
| 161 | + tx.rollback().await.expect("rollback mint tx"); |
| 162 | + |
| 163 | + // Keyset transaction path. |
| 164 | + let journaled_keys = JournaledDatabase::new(Arc::new( |
| 165 | + cdk_sqlite::mint::memory::empty().await.expect("keys db"), |
| 166 | + )); |
| 167 | + let mut ktx = MintKeysDatabase::begin_transaction(&journaled_keys) |
| 168 | + .await |
| 169 | + .expect("begin keys tx"); |
| 170 | + let kerr = ktx |
| 171 | + .add_journal( |
| 172 | + "record".to_string(), |
| 173 | + Event::Delta(Delta::KeysetActive(true)), |
| 174 | + ) |
| 175 | + .await |
| 176 | + .expect_err("direct add_journal must fail"); |
| 177 | + assert!(matches!(kerr, DbError::JournalNotPermitted), "got {kerr:?}"); |
| 178 | + ktx.rollback().await.expect("rollback keys tx"); |
| 179 | +} |
0 commit comments