From 5e0da0c3d465c449f3578cbbe1f5d30afb9d9d43 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Thu, 19 Nov 2026 11:33:16 +0100 Subject: [PATCH] Persist wallet birthday checkpoint at build When creating a new on-chain wallet with a bitcoind chain source, we insert the current chain tip as its first checkpoint, but only held it in memory until the wallet's first sync persisted it. If the node was restarted before that, the wallet reloaded at genesis and tried to rescan the whole chain, which never succeeds against a pruned bitcoind. We now persist the wallet right after applying the checkpoint, failing the build if that doesn't succeed. Backport of 5c7817743da713aa809671ac7a089d6be662d88a Reported-by: vnprclanker Co-Authored-By: HAL 9000 --- src/builder.rs | 6 ++++ tests/integration_tests_rust.rs | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/builder.rs b/src/builder.rs index af6eeb5308..62931d01f9 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1342,6 +1342,12 @@ fn build_with_store_internal( log_error!(logger, "Failed to apply checkpoint during wallet setup: {}", e); BuildError::WalletSetupFailed })?; + // Persist the checkpoint right away, as the wallet would otherwise reload at + // genesis if we're restarted before its first sync. + wallet.persist(&mut wallet_persister).map_err(|e| { + log_error!(logger, "Failed to persist checkpoint during wallet setup: {}", e); + BuildError::WalletSetupFailed + })?; } wallet }, diff --git a/tests/integration_tests_rust.rs b/tests/integration_tests_rust.rs index 8ce90c54d2..71b87186c3 100644 --- a/tests/integration_tests_rust.rs +++ b/tests/integration_tests_rust.rs @@ -2449,3 +2449,57 @@ async fn lsps2_lsp_trusts_client_but_client_does_not_claim() { Some(6) ); } + +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +async fn wallet_birthday_survives_restart_before_first_sync() { + let (bitcoind, electrsd) = setup_bitcoind_and_electrsd(); + let chain_source = TestChainSource::BitcoindRpcSync(&bitcoind); + let seed_bytes = vec![42u8; 64]; + + // Learn the wallet's first address via a throwaway node sharing the seed. + let helper_node = setup_node(&chain_source, random_config(true), Some(seed_bytes.clone())); + let addr = helper_node.onchain_payment().new_address().unwrap(); + helper_node.stop().unwrap(); + + // Pay to the address before the node under test is first built, i.e., before its birthday. + let premine_amount_sat = 100_000; + premine_and_distribute_funds( + &bitcoind.client, + &electrsd.client, + vec![addr], + Amount::from_sat(premine_amount_sat), + ) + .await; + generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6).await; + + let config = random_config(true); + let build_node = || { + setup_builder!(builder, config.node_config); + let rpc_host = bitcoind.params.rpc_socket.ip().to_string(); + let rpc_port = bitcoind.params.rpc_socket.port(); + let values = bitcoind.params.get_cookie_values().unwrap().unwrap(); + builder.set_chain_source_bitcoind_rpc(rpc_host, rpc_port, values.user, values.password); + #[cfg(feature = "uniffi")] + builder.set_entropy_seed_bytes(seed_bytes.clone()).unwrap(); + #[cfg(not(feature = "uniffi"))] + { + let mut bytes = [0u8; 64]; + bytes.copy_from_slice(&seed_bytes); + builder.set_entropy_seed_bytes(bytes); + } + builder.build().unwrap() + }; + + // Build the node for the first time, but drop it before it ever syncs. + drop(build_node()); + + generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 1).await; + + let node = build_node(); + node.start().unwrap(); + node.sync_wallets().unwrap(); + + // A wallet that kept its birthday doesn't see the earlier payment. + assert_eq!(node.list_balances().total_onchain_balance_sats, 0); + node.stop().unwrap(); +}