bsv-wallet-cli 0.7.3

Self-hosted BSV wallet CLI and BRC-100 server, wire-compatible with MetaNet Client
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
//! `bsv-wallet reproof`: re-prove stored merkle proofs the chain no longer
//! confirms.
//!
//! After a reorg a wallet can hold `proven_txs` rows anchored to a block that
//! left the chain (2026-09-07: 28 fleet seats kept proofs against the orphan
//! at 965771; every spend touching one was refused with "Invalid merkle
//! root"). The daemon's header, reorg and review tasks repair this on their
//! own; this verb is the same repair on demand, with a table first.
//!
//! For every stored proof in the height window it compares the stored merkle
//! root with the canonical header's, lists the disagreements (a height whose
//! header cannot be read is never stale), and, with `--execute`, runs the
//! toolbox's `reprove_anchor` on each: a provider's validated proof for the
//! canonical block REPLACES the stored one; the providers still naming the
//! stored block, or faulting, RETAIN it (the monitor retries); only positive
//! evidence (the tracker refutes the stored root, two providers answer
//! cleanly "not mined", no provider serves a path) DEMOTES it to the
//! pre-proof state, bytes kept, re-proved by the monitor.
//!
//! The dry run (the default) performs NO storage write: `gather` reads the
//! anchors and the headers and nothing else. `--execute` first raises the
//! persisted proof LAG gate to `tip - 1` when it is 0 or below that (a stated
//! CLI-only divergence from the reference's "survived a cycle": a wallet
//! whose daemon is not running never sees a cycle, and a replacement proof
//! above the gate could not be stored), then re-proves. `--execute` is
//! refused when no chain tracker is configured (`CHAINTRACKS_URL=off`): a
//! heal without a header service cannot refute anything.
//!
//! The default window is the last 288 blocks and INCLUDES the un-aged tip:
//! a replacement proof at the tip is deferred by the gate, not stored, and
//! the monitor re-presents it. `--all` walks every stored proof, one header
//! read per distinct height with no bound; progress is printed every 50
//! heights.

use std::collections::{BTreeMap, HashMap};

use anyhow::{bail, Context, Result};
use bsv_wallet_toolbox::monitor::reorg_ops::{
    block_hash_of_header, merkle_root_of_header, reprove_anchor, stale_anchors_by_root,
    ReproveOutcome, ReproveTally,
};
use bsv_wallet_toolbox::services::WalletServices;
use bsv_wallet_toolbox::storage::{MonitorStorage, ProvenTxAnchor};
use serde::Serialize;

use crate::context::WalletContext;

/// The window below the tip when neither `--since-height` nor `--all` is given.
pub const DEFAULT_WINDOW_BLOCKS: u32 = 288;

/// How often `gather` reports progress over the distinct heights it reads.
pub const PROGRESS_EVERY_HEIGHTS: usize = 50;

/// One stale stored proof, as the table and the JSON report show it.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct StaleRow {
    pub txid: String,
    pub height: u32,
    pub stored_block_hash: String,
    pub canonical_block_hash: String,
    pub stored_merkle_root: String,
    pub canonical_merkle_root: String,
    /// `dry-run` before `--execute`; what `reprove_anchor` did after it.
    pub action: String,
}

/// What the dry run found: reads only.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct Plan {
    pub tip: u32,
    pub min_height: u32,
    pub max_height: u32,
    pub stored_proofs_in_window: usize,
    pub heights_checked: usize,
    pub heights_unreadable: usize,
    /// The persisted proof LAG gate as read (0 = closed).
    pub gate: u32,
    pub stale: Vec<StaleRow>,
}

/// The verb's report: the plan plus what `--execute` did.
#[derive(Debug, Clone, Serialize)]
pub struct Report {
    #[serde(flatten)]
    pub plan: Plan,
    pub executed: bool,
    /// The gate `--execute` raised to `tip - 1`, when it did.
    pub gate_set_to: Option<u32>,
    pub replaced: u32,
    pub demoted: u32,
    pub deferred: u32,
    pub unchanged: u32,
    pub errors: Vec<String>,
}

/// The height window: `--all` is everything, `--since-height` a floor, else
/// the last `DEFAULT_WINDOW_BLOCKS` below the tip. The tip itself is
/// included (the gate defers a replacement there; the monitor re-presents it).
pub fn window(tip: u32, since_height: Option<u32>, all: bool) -> (u32, u32) {
    if all {
        (0, tip)
    } else {
        (
            since_height.unwrap_or(tip.saturating_sub(DEFAULT_WINDOW_BLOCKS)),
            tip,
        )
    }
}

/// Pure: the stale rows, sorted by height then txid. An anchor at a height
/// with no canonical root is never stale.
pub fn plan_rows(
    anchors: &[ProvenTxAnchor],
    canonical_roots: &HashMap<u32, String>,
    canonical_hashes: &HashMap<u32, String>,
) -> Vec<StaleRow> {
    let mut rows: BTreeMap<(u32, String), StaleRow> = BTreeMap::new();
    for a in stale_anchors_by_root(anchors, canonical_roots) {
        rows.insert(
            (a.height, a.txid.clone()),
            StaleRow {
                txid: a.txid.clone(),
                height: a.height,
                stored_block_hash: a.block_hash.clone(),
                canonical_block_hash: canonical_hashes.get(&a.height).cloned().unwrap_or_default(),
                stored_merkle_root: a.merkle_root.clone(),
                canonical_merkle_root: canonical_roots.get(&a.height).cloned().unwrap_or_default(),
                action: "dry-run".to_string(),
            },
        );
    }
    rows.into_values().collect()
}

/// The chain tip: the header service's tip header when there is one, else
/// the height services.
async fn read_tip<V: WalletServices + ?Sized>(services: &V) -> Result<u32> {
    match services.get_chain_tip_header().await {
        Ok(header) => Ok(header.height),
        Err(_) => services.get_height().await.context("chain tip unavailable"),
    }
}

/// READ-ONLY: the stored anchors in the window, the canonical header per
/// distinct height, the stale rows. Performs no storage write. `progress`
/// is called every [`PROGRESS_EVERY_HEIGHTS`] heights with (done, total).
pub async fn gather<S, V>(
    storage: &S,
    services: &V,
    since_height: Option<u32>,
    all: bool,
    progress: &mut dyn FnMut(usize, usize),
) -> Result<Plan>
where
    S: MonitorStorage + ?Sized,
    V: WalletServices + ?Sized,
{
    let tip = read_tip(services).await?;
    let (min_height, max_height) = window(tip, since_height, all);
    let gate = storage
        .max_acceptable_proof_height()
        .await
        .context("reading the proof gate")?;
    let anchors: Vec<ProvenTxAnchor> = storage
        .find_proven_txs_in_heights(min_height, max_height)
        .await
        .context("reading stored proofs")?;

    // One header read per distinct height; an unreadable header leaves that
    // height out of the verdict (never stale by default).
    let mut heights: Vec<u32> = anchors.iter().map(|a| a.height).collect();
    heights.sort_unstable();
    heights.dedup();
    let total = heights.len();
    let mut canonical_roots: HashMap<u32, String> = HashMap::new();
    let mut canonical_hashes: HashMap<u32, String> = HashMap::new();
    let mut unreadable = 0usize;
    for (done, h) in heights.iter().enumerate() {
        match services.get_header_for_height(*h).await {
            Ok(bytes) => {
                if let Some(root) = merkle_root_of_header(&bytes) {
                    canonical_roots.insert(*h, root);
                }
                if let Some(hash) = block_hash_of_header(&bytes) {
                    canonical_hashes.insert(*h, hash);
                }
            }
            Err(e) => {
                unreadable += 1;
                eprintln!("height {}: header unreadable ({}); skipped", h, e);
            }
        }
        if (done + 1) % PROGRESS_EVERY_HEIGHTS == 0 {
            progress(done + 1, total);
        }
    }

    Ok(Plan {
        tip,
        min_height,
        max_height,
        stored_proofs_in_window: anchors.len(),
        heights_checked: canonical_roots.len(),
        heights_unreadable: unreadable,
        gate,
        stale: plan_rows(&anchors, &canonical_roots, &canonical_hashes),
    })
}

/// Refuse the heal when no chain tracker is configured: without a header
/// service nothing can be refuted, and a re-prove that cannot refute cannot
/// demote or replace safely.
pub async fn refuse_execute_without_tracker<V: WalletServices + ?Sized>(
    services: &V,
) -> Result<()> {
    if services.get_chain_tracker().await.is_err() {
        bail!(
            "reproof --execute refused: no chain tracker is configured (CHAINTRACKS_URL=off); \
             a heal without a header service cannot refute anything. Run the dry run, or set CHAINTRACKS_URL."
        );
    }
    Ok(())
}

/// The CLI-only gate rule: raise the persisted proof LAG gate to `tip - 1`
/// when it is 0 or below that, so a replacement proof at a buried height
/// can be stored now instead of after a monitor cycle this wallet may never
/// run. Returns the height it was set to, or `None` when it already stood
/// at or above `tip - 1`.
pub async fn raise_gate_for_heal<S: MonitorStorage + ?Sized>(
    storage: &S,
    tip: u32,
) -> Result<Option<u32>> {
    let target = tip.saturating_sub(1);
    let gate = storage
        .max_acceptable_proof_height()
        .await
        .context("reading the proof gate")?;
    if gate == 0 || gate < target {
        storage
            .set_max_acceptable_proof_height(target)
            .await
            .context("raising the proof gate")?;
        Ok(Some(target))
    } else {
        Ok(None)
    }
}

/// The heal: raise the gate, then re-prove every stale row. Returns the
/// report; each row's `action` names what `reprove_anchor` did.
pub async fn apply<S, V>(storage: &S, services: &V, plan: Plan) -> Result<Report>
where
    S: MonitorStorage + ?Sized,
    V: WalletServices + ?Sized,
{
    let gate_set_to = raise_gate_for_heal(storage, plan.tip).await?;
    let mut plan = plan;
    let mut tally = ReproveTally::default();
    for row in plan.stale.iter_mut() {
        let anchor = ProvenTxAnchor {
            txid: row.txid.clone(),
            height: row.height,
            block_hash: row.stored_block_hash.clone(),
            merkle_root: row.stored_merkle_root.clone(),
        };
        let outcome = reprove_anchor(storage, services, &anchor).await;
        tally.record(&anchor.txid, &outcome);
        row.action = match &outcome {
            ReproveOutcome::Replaced { height, block_hash } => {
                format!("replaced by {} {}", height, short(block_hash))
            }
            ReproveOutcome::Unchanged => {
                "unchanged (the providers still name the stored block, or the tracker refutes their path); retained, the monitor retries".into()
            }
            ReproveOutcome::Deferred { height } => {
                format!("deferred (height {} is above the proof gate); the monitor re-presents it", height)
            }
            ReproveOutcome::Demoted => {
                "demoted to unproven (positive evidence); run `bsv-wallet tick` twice to re-prove".into()
            }
            ReproveOutcome::TransientError(e) => format!("retained: {}", e),
        };
    }
    Ok(Report {
        plan,
        executed: true,
        gate_set_to,
        replaced: tally.replaced,
        demoted: tally.demoted,
        deferred: tally.deferred,
        unchanged: tally.unchanged,
        errors: tally.errors,
    })
}

pub async fn run(
    ctx: &WalletContext,
    since_height: Option<u32>,
    all: bool,
    execute: bool,
) -> Result<()> {
    let storage = ctx.wallet.storage();
    let services = ctx.wallet.services();
    if execute {
        refuse_execute_without_tracker(services).await?;
    }

    let mut progress = |done: usize, total: usize| {
        eprintln!("reproof: {} of {} heights read", done, total);
    };
    let plan = gather(storage, services, since_height, all, &mut progress).await?;
    let report = if execute {
        apply(storage, services, plan).await?
    } else {
        Report {
            plan,
            executed: false,
            gate_set_to: None,
            replaced: 0,
            demoted: 0,
            deferred: 0,
            unchanged: 0,
            errors: vec![],
        }
    };

    if ctx.json_output {
        println!("{}", serde_json::to_string_pretty(&report)?);
        return Ok(());
    }

    let p = &report.plan;
    println!(
        "reproof: tip {} | window {}..{} | {} stored proof(s) | {} height(s) checked | {} unreadable | proof gate {}",
        p.tip,
        p.min_height,
        p.max_height,
        p.stored_proofs_in_window,
        p.heights_checked,
        p.heights_unreadable,
        if p.gate == 0 { "closed".to_string() } else { p.gate.to_string() }
    );
    if p.stale.is_empty() {
        println!("every stored proof in the window matches the canonical header");
    } else {
        println!("txid             height  stored block       canonical block    action");
        for r in &p.stale {
            println!(
                "{:<14} {:>8}  {:<18} {:<18} {}",
                &r.txid[..12],
                r.height,
                short(&r.stored_block_hash),
                short(&r.canonical_block_hash),
                r.action
            );
        }
    }
    if report.executed {
        if let Some(h) = report.gate_set_to {
            println!("proof gate raised to {} (tip - 1) for the heal", h);
        }
        println!(
            "executed: {} replaced | {} demoted | {} deferred | {} unchanged | {} retained on error",
            report.replaced,
            report.demoted,
            report.deferred,
            report.unchanged,
            report.errors.len()
        );
        for e in &report.errors {
            println!("  retained: {}", e);
        }
        if report.demoted > 0 {
            println!("demoted proofs are re-proved by the monitor: `bsv-wallet tick` twice (the first run queues the header, the second processes it)");
        }
    } else if !p.stale.is_empty() {
        println!(
            "dry run (no storage write); re-run with --execute to replace, retain or demote the {} stale proof(s)",
            p.stale.len()
        );
    }
    Ok(())
}

/// Block hashes begin with sixteen or more zeros; the TAIL tells them apart.
fn short(h: &str) -> String {
    if h.is_empty() {
        "(none)".into()
    } else if h.len() > 16 {
        format!("..{}", &h[h.len() - 16..])
    } else {
        h.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bsv_wallet_toolbox::services::mock::MockWalletServices;
    use bsv_wallet_toolbox::services::{BlockHeader, Services, ServicesOptions};
    use bsv_wallet_toolbox::{Chain, StorageSqlx, WalletStorageWriter};

    fn anchor(txid: &str, height: u32, hash: &str, root: &str) -> ProvenTxAnchor {
        ProvenTxAnchor {
            txid: txid.into(),
            height,
            block_hash: hash.into(),
            merkle_root: root.into(),
        }
    }

    fn header(height: u32, root: &str) -> BlockHeader {
        BlockHeader {
            version: 1,
            previous_hash: "00".repeat(32),
            merkle_root: root.to_string(),
            time: 0,
            bits: 0,
            nonce: height,
            hash: String::new(),
            height,
        }
    }

    async fn storage() -> StorageSqlx {
        let s = StorageSqlx::in_memory().await.unwrap();
        s.migrate("reproof-test", &("02".to_string() + &"ab".repeat(32)))
            .await
            .unwrap();
        s.make_available().await.unwrap();
        s
    }

    async fn seed_proven(s: &StorageSqlx, txid: &str, height: u32, hash: &str, root: &str) {
        sqlx::query(
            "INSERT INTO proven_txs (txid, height, idx, block_hash, merkle_root, merkle_path, raw_tx) VALUES (?, ?, 0, ?, ?, X'00', X'00')",
        )
        .bind(txid)
        .bind(height as i64)
        .bind(hash)
        .bind(root)
        .execute(s.pool())
        .await
        .unwrap();
    }

    /// Every row count and the newest `updated_at` of every table a heal
    /// could touch: equal before and after means no write.
    async fn snapshot(s: &StorageSqlx) -> Vec<(String, i64, Option<String>)> {
        let mut out = Vec::new();
        for (table, stamp) in [
            ("proven_txs", "updated_at"),
            ("proven_tx_reqs", "updated_at"),
            ("transactions", "updated_at"),
            ("broadcast_seen", "seen_at"),
            ("monitor_state", "updated_at"),
        ] {
            let (count, newest): (i64, Option<String>) =
                sqlx::query_as(&format!("SELECT COUNT(*), MAX({stamp}) FROM {table}"))
                    .fetch_one(s.pool())
                    .await
                    .unwrap();
            out.push((table.to_string(), count, newest));
        }
        out
    }

    #[test]
    fn the_window_defaults_to_the_last_288_blocks_and_all_covers_everything() {
        assert_eq!(window(1000, None, false), (712, 1000));
        assert_eq!(window(100, None, false), (0, 100));
        assert_eq!(window(1000, Some(950), false), (950, 1000));
        assert_eq!(window(1000, Some(950), true), (0, 1000));
    }

    #[test]
    fn plan_rows_names_only_the_anchors_whose_root_disagrees_and_skips_unknown_heights() {
        let anchors = vec![
            anchor("t1", 100, "h1", "r-old"),
            anchor("t2", 100, "h2", "R-NEW"),
            anchor("t3", 101, "", "r-any"),
        ];
        let roots: HashMap<u32, String> = [(100u32, "r-new".to_string())].into();
        let hashes: HashMap<u32, String> = [(100u32, "h-new".to_string())].into();
        let rows = plan_rows(&anchors, &roots, &hashes);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].txid, "t1");
        assert_eq!(rows[0].canonical_block_hash, "h-new");
        assert_eq!(rows[0].canonical_merkle_root, "r-new");
        assert_eq!(rows[0].action, "dry-run");
    }

    /// F14: the dry run is read-only. Every table a heal could touch is
    /// byte-for-byte the same before and after, and the gate stays closed.
    #[tokio::test]
    async fn the_dry_run_performs_no_storage_writes() {
        let s = storage().await;
        seed_proven(&s, &"a".repeat(64), 100, &"11".repeat(32), &"aa".repeat(32)).await;
        seed_proven(&s, &"b".repeat(64), 100, &"11".repeat(32), &"22".repeat(32)).await;
        let services = MockWalletServices::builder().height(101).build();
        services.set_header_for_height(header(100, &"22".repeat(32)));
        let before = snapshot(&s).await;

        let mut ticks = 0usize;
        let plan = gather(&s, &services, None, false, &mut |_, _| ticks += 1)
            .await
            .unwrap();

        assert_eq!(plan.tip, 101);
        assert_eq!((plan.min_height, plan.max_height), (0, 101));
        assert_eq!(plan.stored_proofs_in_window, 2);
        assert_eq!(plan.heights_checked, 1);
        assert_eq!(plan.gate, 0, "closed, and left closed");
        assert_eq!(plan.stale.len(), 1);
        assert_eq!(plan.stale[0].txid, "a".repeat(64));
        assert_eq!(plan.stale[0].canonical_merkle_root, "22".repeat(32));
        assert_eq!(ticks, 0, "no progress line under 50 heights");
        assert_eq!(snapshot(&s).await, before, "the dry run wrote nothing");
        assert_eq!(
            MonitorStorage::max_acceptable_proof_height(&s)
                .await
                .unwrap(),
            0
        );
    }

    /// The CLI-only gate rule: `--execute` raises a closed or low gate to
    /// `tip - 1`, and leaves a gate at or above that alone.
    #[tokio::test]
    async fn execute_raises_the_gate_to_tip_minus_one_only_when_it_is_below() {
        let s = storage().await;
        assert_eq!(raise_gate_for_heal(&s, 101).await.unwrap(), Some(100));
        assert_eq!(
            MonitorStorage::max_acceptable_proof_height(&s)
                .await
                .unwrap(),
            100
        );
        assert_eq!(raise_gate_for_heal(&s, 101).await.unwrap(), None);
        MonitorStorage::set_max_acceptable_proof_height(&s, 500)
            .await
            .unwrap();
        assert_eq!(raise_gate_for_heal(&s, 101).await.unwrap(), None);
        assert_eq!(
            MonitorStorage::max_acceptable_proof_height(&s)
                .await
                .unwrap(),
            500
        );
    }

    /// `--execute` is refused without a chain tracker; the mock (which has
    /// one) passes.
    #[tokio::test]
    async fn execute_is_refused_without_a_chain_tracker() {
        let off = Services::with_options(Chain::Main, ServicesOptions::mainnet()).unwrap();
        let err = refuse_execute_without_tracker(&off).await.unwrap_err();
        assert!(err.to_string().contains("CHAINTRACKS_URL=off"), "{err}");
        let mock = MockWalletServices::new();
        assert!(refuse_execute_without_tracker(&mock).await.is_ok());
    }

    /// Progress is reported every 50 distinct heights.
    #[tokio::test]
    async fn progress_is_reported_every_fifty_heights() {
        let s = storage().await;
        for h in 1..=120u32 {
            seed_proven(&s, &format!("{:064x}", h), h, "", &"22".repeat(32)).await;
        }
        let services = MockWalletServices::builder().height(200).build();
        let mut seen = Vec::new();
        let plan = gather(&s, &services, None, true, &mut |done, total| {
            seen.push((done, total))
        })
        .await
        .unwrap();
        assert_eq!(plan.stored_proofs_in_window, 120);
        assert_eq!(seen, vec![(50, 120), (100, 120)]);
    }

    /// The JSON report's documented shape.
    #[test]
    fn the_json_report_has_the_documented_shape() {
        let report = Report {
            plan: Plan {
                tip: 101,
                min_height: 0,
                max_height: 101,
                stored_proofs_in_window: 1,
                heights_checked: 1,
                heights_unreadable: 0,
                gate: 0,
                stale: vec![StaleRow {
                    txid: "t".into(),
                    height: 100,
                    stored_block_hash: "s".into(),
                    canonical_block_hash: "c".into(),
                    stored_merkle_root: "r1".into(),
                    canonical_merkle_root: "r2".into(),
                    action: "dry-run".into(),
                }],
            },
            executed: false,
            gate_set_to: None,
            replaced: 0,
            demoted: 0,
            deferred: 0,
            unchanged: 0,
            errors: vec![],
        };
        let value = serde_json::to_value(&report).unwrap();
        let object = value.as_object().unwrap();
        let mut keys: Vec<&str> = object.keys().map(|k| k.as_str()).collect();
        keys.sort_unstable();
        assert_eq!(
            keys,
            vec![
                "deferred",
                "demoted",
                "errors",
                "executed",
                "gate",
                "gate_set_to",
                "heights_checked",
                "heights_unreadable",
                "max_height",
                "min_height",
                "replaced",
                "stale",
                "stored_proofs_in_window",
                "tip",
                "unchanged",
            ]
        );
        let row = &value["stale"][0];
        for key in [
            "txid",
            "height",
            "stored_block_hash",
            "canonical_block_hash",
            "stored_merkle_root",
            "canonical_merkle_root",
            "action",
        ] {
            assert!(row.get(key).is_some(), "missing {key}");
        }
    }
}