From c272dfb6cdbc74b8c6b34b8aa63abd77927a2e11 Mon Sep 17 00:00:00 2001 From: Alexander Shevtsov Date: Mon, 16 Nov 2026 21:29:31 +0100 Subject: [PATCH] fail build without decoded `AddressPool` Previously we continued the build after failing to decode the persisted `AddressPool`, which skipped the pooled derivation indices: they stayed revealed but were never handed out. Now we explicitly fail the build in that case. A missing record still starts a fresh pool, as for wallets that predate the pool. --- src/builder.rs | 2 +- src/wallet/mod.rs | 39 +++++++++++++++++++++++++++++++++++---- src/wallet/persist.rs | 22 ++++++++-------------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 21f15508b4..c0d150314f 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -203,7 +203,7 @@ pub enum BuildError { InvalidNodeAlias, /// An attempt to setup a runtime has failed. RuntimeSetupFailed, - /// We failed to read data from the [`KVStore`]. + /// We failed to read data from the [`KVStore`], or data read from it could not be decoded. /// /// [`KVStore`]: lightning::util::persist::KVStore ReadFailed, diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 77dced3e20..f2608c330e 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -5711,15 +5711,15 @@ mod tests { } #[tokio::test] - async fn loading_survives_an_undecodable_pool_record() { + async fn loading_fails_on_an_undecodable_pool_record() { let store: Arc = Arc::new(DynStoreWrapper(InMemoryStore::new())); { let wallet = new_test_wallet(Arc::clone(&store), false).await; wallet.refill_address_pool().await.unwrap(); } - // Corrupt the record itself: the pool is a reconstructible cache, so an undecodable - // record must not prevent the node from starting. + // Corrupt the record itself: silently starting with an empty pool would reveal fresh + // indices past the old pooled ones, so the read must fail instead. KVStore::write( &*store, BDK_WALLET_ADDRESS_POOL_PRIMARY_NAMESPACE, @@ -5730,9 +5730,40 @@ mod tests { .await .unwrap(); + let logger = Logger::new_log_facade(); + let err = persist::read_address_pool(&*store, &logger).await.unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); + } + + #[tokio::test] + async fn loading_starts_a_fresh_pool_on_a_deleted_pool_record() { + let store: Arc = Arc::new(DynStoreWrapper(InMemoryStore::new())); + let wallet = new_test_wallet(Arc::clone(&store), false).await; + wallet.refill_address_pool().await.unwrap(); + let last_revealed = + wallet.inner.lock().unwrap().derivation_index(KeychainKind::External).unwrap(); + drop(wallet); + + // A missing record is indistinguishable from a wallet that predates the pool, whose + // revealed indices were all handed out, so loading reveals a fresh pool past them. + KVStore::remove( + &*store, + BDK_WALLET_ADDRESS_POOL_PRIMARY_NAMESPACE, + BDK_WALLET_ADDRESS_POOL_SECONDARY_NAMESPACE, + BDK_WALLET_ADDRESS_POOL_KEY, + false, + ) + .await + .unwrap(); + + let logger = Logger::new_log_facade(); + assert!(persist::read_address_pool(&*store, &logger).await.unwrap().is_empty()); + let wallet = new_test_wallet(Arc::clone(&store), true).await; wallet.refill_address_pool().await.unwrap(); - assert_eq!(pooled_indices(&wallet).len(), ADDRESS_POOL_TARGET_SIZE); + let expected: Vec = + (last_revealed + 1..=last_revealed + ADDRESS_POOL_TARGET_SIZE as u32).collect(); + assert_eq!(pooled_indices(&wallet), expected); } /// An in-memory store whose pool-record writes can be made to fail while wallet-changeset diff --git a/src/wallet/persist.rs b/src/wallet/persist.rs index 6384d0fcee..dfeb68fc91 100644 --- a/src/wallet/persist.rs +++ b/src/wallet/persist.rs @@ -29,10 +29,10 @@ use crate::types::DynStore; /// The persisted derivation indices of the wallet's address pool. /// -/// The record is advisory and reconstructible: it is written before the reveals it references -/// are necessarily persisted, so it may briefly lead the persisted [`ChangeSet::indexer`]. -/// Readers must validate each index against the wallet's revealed range before handing out any -/// of the addresses; dropped entries are re-derived to the same indices by the next refill. +/// The record is written before the reveals it references are necessarily persisted, so it may +/// briefly lead the persisted [`ChangeSet::indexer`]. Readers must validate each index against +/// the wallet's revealed range before handing out any of the addresses; dropped entries are +/// re-derived to the same indices by the next refill. /// /// [`ChangeSet::indexer`]: bdk_wallet::ChangeSet::indexer struct AddressPoolRecord { @@ -265,16 +265,10 @@ pub(crate) async fn read_address_pool( Err(e) if e.kind() == lightning::io::ErrorKind::NotFound => return Ok(Vec::new()), Err(e) => return Err(e.into()), }; - let record = match AddressPoolRecord::read(&mut &*reader) { - Ok(record) => record, - Err(e) => { - // The record is a reconstructible cache: a decode failure (corruption, or a - // future version's incompatible encoding) at worst costs the pool's indices, so - // degrade to an empty pool rather than failing the node's startup. - log_error!(logger, "Dropping undecodable address pool: {}", e); - return Ok(Vec::new()); - }, - }; + let record = AddressPoolRecord::read(&mut &*reader).map_err(|e| { + log_error!(logger, "Failed to deserialize address pool: {}", e); + std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to deserialize address pool") + })?; Ok(record.indices) }