cdk-mintd 0.18.0

CDK mint binary
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
//! Persistent storage for the authoritative mintd configuration document.

use std::fmt;
use std::sync::Arc;

use cdk::cdk_database::{self, KVStoreCompareAndSwap};
use serde::{Deserialize, Serialize};
use thiserror::Error;

const PRIMARY_NAMESPACE: &str = "cdk_mintd";
const SECONDARY_NAMESPACE: &str = "config";
const ACTIVE_KEY: &str = "active";
const MAX_CAS_ATTEMPTS: usize = 8;

/// Serialization version for [`ConfigEnvelope`].
pub(crate) const CONFIG_FORMAT_VERSION: u32 = 1;

/// Lifecycle state of the stored configuration document.
///
/// The state machine has two persisted states:
///
/// ```text
///   ┌─────────┐  initialize / replace / rollback  ┌─────────┐
///   │ Pending │ ────────────────────────────────▶ │         │
///   │         │ ◀──────────────────────────────── │ Applied │
///   └─────────┘   (a write always stages Pending) └─────────┘
///        │                                            ▲
///        └────────── completed daemon startup ────────┘
///                 (mark_applied, revision-guarded CAS)
/// ```
///
/// Every document write stages a `Pending` record; only a daemon startup
/// that brought every service up transitions it to `Applied`. A startup
/// that fails midway leaves the record `Pending`, so the next start
/// reconciles the document again.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DocumentState {
    /// Stored but never served by a daemon. The next startup forces the
    /// document's canonical mint info and quote TTL into the database and
    /// commits `Applied` once all services are up.
    Pending,
    /// Served by a completed daemon startup. Later starts preserve
    /// RPC-managed canonical values when management RPC is enabled.
    Applied,
}

/// The single authoritative configuration record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ConfigEnvelope {
    pub(crate) format_version: u32,
    #[serde(default)]
    pub(crate) revision: u64,
    pub(crate) toml: String,
    #[serde(default)]
    pub(crate) previous_applied_toml: Option<String>,
    pub(crate) signing_identity: String,
    pub(crate) applied: bool,
    #[serde(default)]
    pub(crate) allow_new_bdk_wallet: bool,
}

impl ConfigEnvelope {
    pub(crate) fn new(toml: String, signing_identity: String) -> Self {
        Self {
            format_version: CONFIG_FORMAT_VERSION,
            revision: 1,
            toml,
            previous_applied_toml: None,
            signing_identity,
            applied: false,
            allow_new_bdk_wallet: false,
        }
    }

    pub(crate) fn with_new_bdk_wallet_allowed(mut self, allowed: bool) -> Self {
        self.allow_new_bdk_wallet = allowed;
        self
    }

    /// Lifecycle state of the stored document.
    pub(crate) fn state(&self) -> DocumentState {
        if self.applied {
            DocumentState::Applied
        } else {
            DocumentState::Pending
        }
    }

    fn encode(&self) -> Result<Vec<u8>, ConfigStoreError> {
        serde_json::to_vec(self).map_err(|source| ConfigStoreError::Encode { source })
    }

    fn decode(bytes: &[u8]) -> Result<Self, ConfigStoreError> {
        let envelope: Self = serde_json::from_slice(bytes)
            .map_err(|source| ConfigStoreError::CorruptRecord { source })?;
        if envelope.format_version != CONFIG_FORMAT_VERSION {
            return Err(ConfigStoreError::UnsupportedFormatVersion {
                found: envelope.format_version,
                supported: CONFIG_FORMAT_VERSION,
            });
        }
        Ok(envelope)
    }
}

/// Configuration repository failures.
#[derive(Debug, Error)]
pub enum ConfigStoreError {
    /// Configuration already exists.
    #[error("mintd configuration is already initialized")]
    AlreadyInitialized,

    /// Configuration has not been initialized.
    #[error(
        "mintd configuration is not initialized; run `cdk-mintd config init --new-mint --file <path>` for a new mint or `cdk-mintd config init --existing-mint --file <path>` for an existing mint"
    )]
    NotInitialized,

    /// A replacement attempted to change the immutable signer.
    #[error("configured signing identity does not match this mint database")]
    SigningIdentityMismatch,

    /// Too many concurrent writers prevented a configuration update.
    #[error("mintd configuration changed concurrently; retry the operation")]
    ConcurrentModification,

    /// The persisted configuration revision cannot be incremented.
    #[error("mintd configuration revision overflow")]
    RevisionOverflow,

    /// No previously applied document is available.
    #[error("no previously applied mintd configuration is available to roll back to")]
    NoRollbackConfiguration,

    /// The stored envelope uses an unsupported serialization version.
    #[error(
        "unsupported mintd configuration format version {found}; supported version is {supported}"
    )]
    UnsupportedFormatVersion {
        /// Version read from the database.
        found: u32,
        /// Version understood by this binary.
        supported: u32,
    },

    /// Encoding the envelope failed.
    #[error("could not encode mintd configuration: {source}")]
    Encode {
        /// JSON encoding failure.
        #[source]
        source: serde_json::Error,
    },

    /// The stored envelope is malformed.
    #[error("persisted mintd configuration is malformed: {source}")]
    CorruptRecord {
        /// JSON decoding failure.
        #[source]
        source: serde_json::Error,
    },

    /// The underlying key-value database failed.
    #[error(transparent)]
    Database(#[from] cdk_database::Error),
}

/// Repository for the single active configuration envelope.
#[derive(Clone)]
pub(crate) struct ConfigRepository {
    store: Arc<dyn KVStoreCompareAndSwap<Err = cdk_database::Error> + Send + Sync>,
}

impl fmt::Debug for ConfigRepository {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ConfigRepository").finish_non_exhaustive()
    }
}

impl ConfigRepository {
    pub(crate) fn new(
        store: Arc<dyn KVStoreCompareAndSwap<Err = cdk_database::Error> + Send + Sync>,
    ) -> Self {
        Self { store }
    }

    /// Reads the authoritative configuration envelope.
    pub(crate) async fn active(&self) -> Result<ConfigEnvelope, ConfigStoreError> {
        Ok(self.active_record().await?.1)
    }

    async fn active_record(&self) -> Result<(Vec<u8>, ConfigEnvelope), ConfigStoreError> {
        let bytes = self
            .store
            .kv_read(PRIMARY_NAMESPACE, SECONDARY_NAMESPACE, ACTIVE_KEY)
            .await?
            .ok_or(ConfigStoreError::NotInitialized)?;
        let envelope = ConfigEnvelope::decode(&bytes)?;
        Ok((bytes, envelope))
    }

    /// Creates the authoritative record without replacing an existing one.
    ///
    /// Transition: uninitialized → `Pending`.
    pub(crate) async fn initialize(
        &self,
        envelope: ConfigEnvelope,
    ) -> Result<(), ConfigStoreError> {
        let bytes = envelope.encode()?;
        if !self
            .store
            .kv_compare_and_swap(
                PRIMARY_NAMESPACE,
                SECONDARY_NAMESPACE,
                ACTIVE_KEY,
                None,
                &bytes,
            )
            .await?
        {
            return Err(ConfigStoreError::AlreadyInitialized);
        }
        Ok(())
    }

    /// Atomically replaces the document and marks it for next-start application.
    ///
    /// Transition: `Pending | Applied` → `Pending`. The currently applied
    /// document, when there is one, is retained for rollback.
    #[cfg(test)]
    pub(crate) async fn replace(
        &self,
        toml: String,
        signing_identity: &str,
    ) -> Result<(), ConfigStoreError> {
        self.replace_with_bdk_policy(toml, signing_identity, false)
            .await
    }

    pub(crate) async fn replace_with_bdk_policy(
        &self,
        toml: String,
        signing_identity: &str,
        allow_new_bdk_wallet: bool,
    ) -> Result<(), ConfigStoreError> {
        for _ in 0..MAX_CAS_ATTEMPTS {
            let (current_bytes, current) = self.active_record().await?;
            if current.signing_identity != signing_identity {
                return Err(ConfigStoreError::SigningIdentityMismatch);
            }
            let revision = current
                .revision
                .checked_add(1)
                .ok_or(ConfigStoreError::RevisionOverflow)?;
            let previous_applied_toml = if current.state() == DocumentState::Applied {
                Some(current.toml)
            } else {
                current.previous_applied_toml
            };
            let replacement = ConfigEnvelope {
                format_version: CONFIG_FORMAT_VERSION,
                revision,
                toml: toml.clone(),
                previous_applied_toml,
                signing_identity: current.signing_identity,
                applied: false,
                allow_new_bdk_wallet,
            }
            .encode()?;
            if self
                .store
                .kv_compare_and_swap(
                    PRIMARY_NAMESPACE,
                    SECONDARY_NAMESPACE,
                    ACTIVE_KEY,
                    Some(&current_bytes),
                    &replacement,
                )
                .await?
            {
                return Ok(());
            }
        }
        Err(ConfigStoreError::ConcurrentModification)
    }

    /// Stages the last applied document for activation on the next restart.
    ///
    /// Transition: `Pending | Applied` → `Pending` with the restored
    /// document. Returns `true` because every restored document must be
    /// activated by a restart. Startup may have partially applied a pending
    /// document before failing, so rollback cannot safely mark the restored
    /// document applied.
    pub(crate) async fn rollback(&self) -> Result<bool, ConfigStoreError> {
        for _ in 0..MAX_CAS_ATTEMPTS {
            let (current_bytes, mut current) = self.active_record().await?;
            let previous = current
                .previous_applied_toml
                .take()
                .ok_or(ConfigStoreError::NoRollbackConfiguration)?;
            current.revision = current
                .revision
                .checked_add(1)
                .ok_or(ConfigStoreError::RevisionOverflow)?;
            if current.state() == DocumentState::Applied {
                current.previous_applied_toml = Some(current.toml);
            }
            current.applied = false;
            current.allow_new_bdk_wallet = false;
            current.toml = previous;
            let replacement = current.encode()?;
            if self
                .store
                .kv_compare_and_swap(
                    PRIMARY_NAMESPACE,
                    SECONDARY_NAMESPACE,
                    ACTIVE_KEY,
                    Some(&current_bytes),
                    &replacement,
                )
                .await?
            {
                return Ok(true);
            }
        }
        Err(ConfigStoreError::ConcurrentModification)
    }

    /// Marks `expected_revision` applied if it is still the current revision.
    ///
    /// Transition: `Pending` → `Applied`. Returns `false` when another apply
    /// replaced the document during startup; the replacement then stays
    /// `Pending` for the next restart.
    pub(crate) async fn mark_applied(
        &self,
        expected_revision: u64,
    ) -> Result<bool, ConfigStoreError> {
        for _ in 0..MAX_CAS_ATTEMPTS {
            let (current_bytes, mut current) = self.active_record().await?;
            if current.revision != expected_revision {
                return Ok(false);
            }
            if current.state() == DocumentState::Applied && !current.allow_new_bdk_wallet {
                return Ok(true);
            }
            current.applied = true;
            current.allow_new_bdk_wallet = false;
            let replacement = current.encode()?;
            if self
                .store
                .kv_compare_and_swap(
                    PRIMARY_NAMESPACE,
                    SECONDARY_NAMESPACE,
                    ACTIVE_KEY,
                    Some(&current_bytes),
                    &replacement,
                )
                .await?
            {
                return Ok(true);
            }
        }
        Err(ConfigStoreError::ConcurrentModification)
    }
}

#[cfg(all(test, feature = "sqlite"))]
mod tests {
    use cdk_sqlite::mint::memory;

    use super::*;

    async fn repository() -> ConfigRepository {
        let database = Arc::new(memory::empty().await.expect("in-memory database"));
        ConfigRepository::new(database)
    }

    async fn write_raw(repository: &ConfigRepository, bytes: &[u8]) {
        assert!(repository
            .store
            .kv_compare_and_swap(
                PRIMARY_NAMESPACE,
                SECONDARY_NAMESPACE,
                ACTIVE_KEY,
                None,
                bytes,
            )
            .await
            .expect("write raw record"));
    }

    #[tokio::test]
    async fn initialize_and_replace_are_single_record_transitions() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("first".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");
        assert!(matches!(
            repository
                .initialize(ConfigEnvelope::new("again".to_owned(), "signer".to_owned()))
                .await,
            Err(ConfigStoreError::AlreadyInitialized)
        ));

        repository
            .replace("second".to_owned(), "signer")
            .await
            .expect("replace configuration");
        let active = repository.active().await.expect("read configuration");
        assert_eq!(active.toml, "second");
        assert!(!active.applied);
        assert_eq!(active.revision, 2);
    }

    #[tokio::test]
    async fn successful_startup_clears_new_bdk_wallet_permission() {
        let repository = repository().await;
        repository
            .initialize(
                ConfigEnvelope::new("first".to_owned(), "signer".to_owned())
                    .with_new_bdk_wallet_allowed(true),
            )
            .await
            .expect("initialize configuration");
        let pending = repository
            .active()
            .await
            .expect("read pending configuration");
        assert!(pending.allow_new_bdk_wallet);

        assert!(repository
            .mark_applied(pending.revision)
            .await
            .expect("mark configuration applied"));
        let applied = repository
            .active()
            .await
            .expect("read applied configuration");
        assert!(applied.applied);
        assert!(!applied.allow_new_bdk_wallet);
    }

    #[tokio::test]
    async fn older_startup_cannot_mark_replacement_applied() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("first".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");
        let first_revision = repository
            .active()
            .await
            .expect("read first revision")
            .revision;
        repository
            .replace("second".to_owned(), "signer")
            .await
            .expect("replace configuration");
        let second_revision = repository
            .active()
            .await
            .expect("read second revision")
            .revision;

        assert!(!repository
            .mark_applied(first_revision)
            .await
            .expect("compare configuration"));
        assert!(
            !repository
                .active()
                .await
                .expect("read configuration")
                .applied
        );
        assert!(repository
            .mark_applied(second_revision)
            .await
            .expect("mark current configuration"));
        assert!(
            repository
                .active()
                .await
                .expect("read configuration")
                .applied
        );
    }

    #[tokio::test]
    async fn corrupt_record_is_rejected() {
        let repository = repository().await;
        write_raw(&repository, b"{").await;

        assert!(matches!(
            repository.active().await,
            Err(ConfigStoreError::CorruptRecord { .. })
        ));
    }

    #[tokio::test]
    async fn unsupported_format_version_is_rejected() {
        let repository = repository().await;
        let unsupported = serde_json::to_vec(&ConfigEnvelope {
            format_version: CONFIG_FORMAT_VERSION + 1,
            revision: 1,
            toml: "document".to_owned(),
            previous_applied_toml: None,
            signing_identity: "signer".to_owned(),
            applied: false,
            allow_new_bdk_wallet: false,
        })
        .expect("encode test record");
        write_raw(&repository, &unsupported).await;

        assert!(matches!(
            repository.active().await,
            Err(ConfigStoreError::UnsupportedFormatVersion { .. })
        ));
    }

    #[tokio::test]
    async fn not_initialized_and_signing_identity_mismatch_are_rejected() {
        let repository = repository().await;
        assert!(matches!(
            repository.active().await,
            Err(ConfigStoreError::NotInitialized)
        ));
        assert!(matches!(
            repository.replace("next".to_owned(), "signer").await,
            Err(ConfigStoreError::NotInitialized)
        ));
        assert!(matches!(
            repository.mark_applied(1).await,
            Err(ConfigStoreError::NotInitialized)
        ));

        repository
            .initialize(ConfigEnvelope::new("first".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");
        assert!(matches!(
            repository
                .replace("second".to_owned(), "other-signer")
                .await,
            Err(ConfigStoreError::SigningIdentityMismatch)
        ));
        assert_eq!(
            repository.active().await.expect("read configuration").toml,
            "first"
        );
    }

    #[tokio::test]
    async fn mark_applied_is_idempotent_for_current_document() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("doc".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");
        let revision = repository
            .active()
            .await
            .expect("read configuration")
            .revision;

        assert!(repository
            .mark_applied(revision)
            .await
            .expect("mark applied once"));
        assert!(
            repository
                .active()
                .await
                .expect("read configuration")
                .applied
        );
        assert!(repository
            .mark_applied(revision)
            .await
            .expect("mark applied twice"));
        assert!(
            repository
                .active()
                .await
                .expect("read configuration")
                .applied
        );

        let debug = format!("{repository:?}");
        assert!(debug.contains("ConfigRepository"));
        assert!(!debug.contains("store:"));
    }

    #[tokio::test]
    async fn rollback_of_pending_document_stages_previous_applied_document() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("first".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");
        assert!(repository
            .mark_applied(1)
            .await
            .expect("mark first document applied"));
        repository
            .replace("second".to_owned(), "signer")
            .await
            .expect("stage second document");

        let pending = repository.active().await.expect("read pending document");
        assert_eq!(pending.previous_applied_toml.as_deref(), Some("first"));
        assert!(repository
            .rollback()
            .await
            .expect("stage previous applied document"));

        let restored = repository.active().await.expect("read restored document");
        assert_eq!(restored.toml, "first");
        assert!(!restored.applied);
        assert_eq!(restored.revision, 3);
        assert!(restored.previous_applied_toml.is_none());
    }

    #[tokio::test]
    async fn rollback_of_applied_document_stages_previous_applied_document() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("first".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");
        assert!(repository
            .mark_applied(1)
            .await
            .expect("mark first document applied"));
        repository
            .replace("second".to_owned(), "signer")
            .await
            .expect("stage second document");
        assert!(repository
            .mark_applied(2)
            .await
            .expect("mark second document applied"));

        assert!(repository
            .rollback()
            .await
            .expect("stage previous applied document"));
        let restored = repository.active().await.expect("read rollback document");
        assert_eq!(restored.toml, "first");
        assert!(!restored.applied);
        assert_eq!(restored.revision, 3);
        assert_eq!(restored.previous_applied_toml.as_deref(), Some("second"));
    }

    #[tokio::test]
    async fn rollback_requires_a_previous_applied_document() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("first".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");

        assert!(matches!(
            repository.rollback().await,
            Err(ConfigStoreError::NoRollbackConfiguration)
        ));
    }

    #[tokio::test]
    async fn reapplying_same_document_advances_revision_and_stays_pending() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("doc".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");
        let startup_revision = repository
            .active()
            .await
            .expect("read startup configuration")
            .revision;

        repository
            .replace("doc".to_owned(), "signer")
            .await
            .expect("reapply configuration");

        assert!(!repository
            .mark_applied(startup_revision)
            .await
            .expect("compare revisions"));
        let active = repository.active().await.expect("read replacement");
        assert_eq!(active.toml, "doc");
        assert_eq!(active.revision, startup_revision + 1);
        assert!(!active.applied);
    }

    #[tokio::test]
    async fn concurrent_initialization_has_one_winner() {
        let repository = repository().await;
        let left =
            repository.initialize(ConfigEnvelope::new("left".to_owned(), "signer".to_owned()));
        let right =
            repository.initialize(ConfigEnvelope::new("right".to_owned(), "signer".to_owned()));
        let (left, right) = tokio::join!(left, right);

        assert_ne!(left.is_ok(), right.is_ok());
        let loser = if left.is_err() { left } else { right };
        assert!(matches!(loser, Err(ConfigStoreError::AlreadyInitialized)));
    }

    #[tokio::test]
    async fn concurrent_applies_both_succeed_with_monotonic_revisions() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("first".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");

        let left = repository.replace("left".to_owned(), "signer");
        let right = repository.replace("right".to_owned(), "signer");
        let (left, right) = tokio::join!(left, right);
        left.expect("apply left configuration");
        right.expect("apply right configuration");

        let active = repository.active().await.expect("read final configuration");
        assert!(matches!(active.toml.as_str(), "left" | "right"));
        assert_eq!(active.revision, 3);
        assert!(!active.applied);
    }

    #[tokio::test]
    async fn concurrent_apply_and_startup_mark_leave_replacement_pending() {
        let repository = repository().await;
        repository
            .initialize(ConfigEnvelope::new("first".to_owned(), "signer".to_owned()))
            .await
            .expect("initialize configuration");
        let startup_revision = repository
            .active()
            .await
            .expect("read startup configuration")
            .revision;

        let apply = repository.replace("second".to_owned(), "signer");
        let mark = repository.mark_applied(startup_revision);
        let (apply, mark) = tokio::join!(apply, mark);
        apply.expect("apply replacement");
        mark.expect("mark startup revision");

        let active = repository.active().await.expect("read replacement");
        assert_eq!(active.toml, "second");
        assert_eq!(active.revision, startup_revision + 1);
        assert!(!active.applied);
    }

    #[tokio::test]
    async fn legacy_envelope_without_revision_defaults_to_zero() {
        let repository = repository().await;
        write_raw(
            &repository,
            br#"{"format_version":1,"toml":"legacy","signing_identity":"signer","applied":false}"#,
        )
        .await;

        let active = repository.active().await.expect("decode legacy record");
        assert_eq!(active.revision, 0);
        assert!(active.previous_applied_toml.is_none());
        assert!(!active.allow_new_bdk_wallet);
        assert!(repository
            .mark_applied(0)
            .await
            .expect("mark legacy revision"));
    }
}