diff --git a/CHANGELOG.md b/CHANGELOG.md index cb4e2aaa..6ba0e98a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details. - Fixed `key generate` silently producing a 24-word mnemonic for any `--entropy` value other than 12; values other than 12 and 24 are now rejected - Gated `full_scan`'s `--scan-stop-gap` to the Electrum and Esplora backends that use it; RPC and CBF wallets now reject the flag instead of silently ignoring it, while a `STOP_GAP` environment variable keeps working for supported backends and no longer breaks unsupported ones - Changed `payjoin history` to return JSON instead of a rendered table +- Fixed REPL mode not persisting wallet changes, so a new session resumed from stale state (e.g. reusing already-handed-out addresses) and lost transactions, UTXO locks and scan results ## [4.0.0] diff --git a/src/main.rs b/src/main.rs index 7ed73d46..a7ef4f95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,6 +202,10 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> { .await .map_err(Error::Generic)?; + // Persist after every command so the session's changes survive an exit, + // a crash, or Ctrl-C, matching the non-REPL path. + wallet.persist()?; + if should_exit { break; } diff --git a/tests/integration/offline.rs b/tests/integration/offline.rs index c4be0a21..2a4f8422 100644 --- a/tests/integration/offline.rs +++ b/tests/integration/offline.rs @@ -491,3 +491,78 @@ mod multipath_tests { .failure(); } } + +#[cfg(all( + feature = "repl", + feature = "sqlite", + not(any( + feature = "electrum", + feature = "esplora", + feature = "rpc", + feature = "cbf" + )) +))] +mod repl_persist_tests { + use crate::common::BdkCli; + use serde_json::Value; + use tempfile::TempDir; + + const WALLET_NAME: &str = "repl_persist_wallet"; + + fn setup_repl_wallet() -> (BdkCli, TempDir) { + let temp = TempDir::new().unwrap(); + let cli = BdkCli::new("regtest", Some(temp.path().to_path_buf())); + + let desc = cli.cmd("descriptor", &["--type", "tr"]).output().unwrap(); + let v: Value = serde_json::from_slice(&desc.stdout).unwrap(); + let ext = v["private_descriptors"]["external"].as_str().unwrap(); + let int = v["private_descriptors"]["internal"].as_str().unwrap(); + + cli.build_base_cmd() + .args(["wallet", "--wallet", WALLET_NAME, "config"]) + .args(["--ext-descriptor", ext, "--int-descriptor", int]) + .args(["--database-type", "sqlite"]) + .assert() + .success(); + + (cli, temp) + } + + /// Runs `wallet new_address` in a fresh REPL process and returns the index it printed. + fn repl_new_address_index(cli: &BdkCli) -> u64 { + let output = cli + .build_base_cmd() + .args(["repl", "--wallet", WALLET_NAME]) + .write_stdin("wallet new_address\nexit\n") + .output() + .expect("failed to run repl"); + + assert!( + output.status.success(), + "repl exited non-zero: {}", + String::from_utf8_lossy(&output.stderr) + ); + + let stdout = String::from_utf8_lossy(&output.stdout); + let marker = "\"index\": "; + let start = stdout + .find(marker) + .unwrap_or_else(|| panic!("new_address output missing index:\n{stdout}")) + + marker.len(); + stdout[start..] + .chars() + .take_while(|c| c.is_ascii_digit()) + .collect::() + .parse() + .expect("index was not a number") + } + + #[test] + fn repl_persists_address_index_across_sessions() { + let (cli, _temp) = setup_repl_wallet(); + + assert_eq!(repl_new_address_index(&cli), 0); + // A second, separate REPL process must see the index persisted by the first one. + assert_eq!(repl_new_address_index(&cli), 1); + } +}