Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 35 additions & 4 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Comment thread
randomlogin marked this conversation as resolved.
Resolved
let store: Arc<DynStore> = 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,
Expand All @@ -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<DynStore> = 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<u32> =
(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
Expand Down
22 changes: 8 additions & 14 deletions src/wallet/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
Loading