Skip to content
Open
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
17 changes: 17 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,23 @@ fn build_with_store_internal(
let bdk_wallet = match wallet_opt {
Some(wallet) => wallet,
None => {
// Guard against silently setting the wallet birthday to genesis on a fresh node:
// if we are creating a new wallet but failed to learn the current chain tip from
// a Bitcoin Core RPC/REST backend, we'd otherwise persist fresh wallet state
// pinned at height 0 and force a full-history rescan once the backend comes back.
// Abort cleanly instead so the misconfiguration surfaces on the first startup.
// Esplora/Electrum backends currently never return a tip at build time, so they
// retain their existing behavior.
let is_bitcoind_source =
matches!(chain_data_source_config, Some(ChainDataSourceConfig::Bitcoind { .. }));
if chain_tip_opt.is_none() && is_bitcoind_source {
log_error!(
logger,
"Failed to determine chain tip on first startup. Aborting to avoid pinning the wallet birthday to genesis."
);
return Err(BuildError::WalletSetupFailed);
}

// We hold off on persisting the wallet until its initial checkpoint is in place, as
// a wallet persisted without it would reload at genesis.
let mut wallet = BdkWallet::create(descriptor.clone(), change_descriptor.clone())
Expand Down
24 changes: 23 additions & 1 deletion tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use ldk_node::payment::{
ConfirmationStatus, PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus,
QrPaymentResult,
};
use ldk_node::{Builder, DynStore, Event, NodeError};
use ldk_node::{BuildError, Builder, DynStore, Event, NodeError};
use lightning::ln::channelmanager::PaymentId;
use lightning::routing::gossip::{NodeAlias, NodeId};
use lightning::routing::router::RouteParametersConfig;
Expand Down Expand Up @@ -739,6 +739,28 @@ async fn onchain_wallet_recovery() {
);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn build_aborts_on_first_startup_bitcoind_tip_fetch_failure() {
// A fresh node pointed at an unreachable bitcoind RPC endpoint must not silently
// fall back to genesis as the wallet birthday. The build must abort cleanly so the
// misconfiguration surfaces immediately.
let config = random_config(false);

setup_builder!(builder, config.node_config);
// Pick a localhost port that is extremely unlikely to be bound. The kernel will
// refuse the connection immediately so the test does not have to wait for the
// chain-polling timeout.
let unreachable_port: u16 = 1;
builder.set_chain_source_bitcoind_rpc(
"127.0.0.1".to_string(),
unreachable_port,
"user".to_string(),
"password".to_string(),
);

assert_eq!(builder.build().map(|_| ()), Err(BuildError::WalletSetupFailed));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_rbf_via_mempool() {
run_rbf_test(false).await;
Expand Down
Loading