From ace42f7c31f9c2e066bc59697261a19c63a1c532 Mon Sep 17 00:00:00 2001 From: Dior <79525+diorwave@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:13:28 +0100 Subject: [PATCH] wollet: waterfalls: fix last_unused on later pages - The last used index of each page was not offset by the page start, so every page with activity overwrote `last_unused` with a page-relative value. - Wallets with more than 1000 used addresses on a chain could then return an already used address. Fixes https://github.com/Blockstream/lwk/issues/209 --- lwk_wollet/CHANGELOG.md | 1 + lwk_wollet/src/clients/asyncr/esplora.rs | 6 +- .../src/clients/asyncr/esplora/waterfalls.rs | 66 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/lwk_wollet/CHANGELOG.md b/lwk_wollet/CHANGELOG.md index a50bfb6d7..f670b418c 100644 --- a/lwk_wollet/CHANGELOG.md +++ b/lwk_wollet/CHANGELOG.md @@ -6,6 +6,7 @@ * Removed `Amp2::new_testnet()`, use `Amp2::new()`; also removed old AMP2 testnet consts. * Breaking: update the re-exported `age` crate to 0.12. Its `fl!` localization macro dependency emitted arguments in random order, making builds non-deterministic. See the [age 0.12 changelog](https://github.com/str4d/rage/blob/main/age/CHANGELOG.md) for API changes. * Breaking: `AddressResult::new()` takes the full derivation path of the address, `None` if unknown, readable with the new `AddressResult::derivation_path()`. +* Fix Waterfalls scans of wallets with more than 1000 used addresses on a chain: `last_unused` was relative to the last page with activity, so `Wollet::address(None)` could return an already used address. ## 0.20.0 diff --git a/lwk_wollet/src/clients/asyncr/esplora.rs b/lwk_wollet/src/clients/asyncr/esplora.rs index 3bc80ff21..514906748 100644 --- a/lwk_wollet/src/clients/asyncr/esplora.rs +++ b/lwk_wollet/src/clients/asyncr/esplora.rs @@ -762,6 +762,7 @@ impl EsploraClient { // `Data::scripts` will be wrong too, this doesn't seem to impact the correctness of the scan. let chain: Chain = (&desc).try_into().unwrap_or(Chain::External); + let page_offset = page * WATERFALLS_MAX_ADDRESSES as u32; let max = chain_history .iter() .enumerate() @@ -769,11 +770,10 @@ impl EsploraClient { .map(|(i, _)| i as u32) .max(); if let Some(max) = max { - data.last_unused[chain] = max + 1; + data.last_unused[chain] = page_offset + max + 1; } for (i, script_history) in chain_history.iter().enumerate() { - let child = - ChildNumber::from(page * WATERFALLS_MAX_ADDRESSES as u32 + i as u32); + let child = ChildNumber::from(page_offset + i as u32); let (script, blinding_pubkey, cached) = cache.get_or_derive(chain, child)?; if !cached { data.scripts diff --git a/lwk_wollet/src/clients/asyncr/esplora/waterfalls.rs b/lwk_wollet/src/clients/asyncr/esplora/waterfalls.rs index fc3c145e8..8b9470f19 100644 --- a/lwk_wollet/src/clients/asyncr/esplora/waterfalls.rs +++ b/lwk_wollet/src/clients/asyncr/esplora/waterfalls.rs @@ -548,4 +548,70 @@ mod tests { }; assert!(matches!(err, Error::EsploraHttpError { status: 422, .. })); } + + #[tokio::test] + async fn full_scan_last_unused_spans_pages() { + let tx = lwk_test_util::liquid_block_1().txdata.remove(0); + let header = lwk_test_util::liquid_block_header_2_963_520(); + let tip = header.block_hash(); + let external = "elwpkh(tpubDCRMaF33e44pcJj534LXVhFbHibPbJ5vuLhSSPFAw57kYURv4tzXFL6LSnd78bkjqdmE3USedkbpXJUPA1tdzKfuYSL7PianceqAhwL2UkA/0/*)"; + let seen = serde_json::json!([{ "txid": tx.txid(), "height": 0 }]); + + // Index 0 used on the first page, index 3 used on the second page + let mut page0 = vec![serde_json::json!([]); 1_000]; + page0[0] = seen.clone(); + let mut page1 = vec![serde_json::json!([]); 5]; + page1[3] = seen; + let page = |p: u16, history: Vec| { + serde_json::json!({ "txs_seen": { external: history }, "page": p, "tip": tip }) + .to_string() + .into_bytes() + }; + let page0 = page(0, page0); + let page1 = page(1, page1); + let tx_raw = elements::encode::serialize(&tx); + let header_hex = elements::encode::serialize_hex(&header).into_bytes(); + let tip_hex = tip.to_string().into_bytes(); + + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + thread::spawn(move || loop { + let (mut stream, _) = listener.accept().unwrap(); + let mut request = vec![0u8; 8192]; + let len = stream.read(&mut request).unwrap_or(0); + let request = String::from_utf8_lossy(&request[..len]); + let body = if request.starts_with("GET /v2/waterfalls") { + if request.contains("&page=0&") { + &page0 + } else { + &page1 + } + } else if request.starts_with("GET /tx/") { + &tx_raw + } else if request.starts_with("GET /block-height/") { + &tip_hex + } else { + &header_hex + }; + let _ = write!( + stream, + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ); + let _ = stream.write_all(body); + }); + + let mut client = WaterfallsClientBuilder::new(&format!("http://{addr}"), Network::Liquid) + .build() + .unwrap(); + client.avoid_encryption(); + let mut wollet = WolletBuilder::new(Network::Liquid, descriptor()) + .build() + .unwrap(); + + let update = client.full_scan(&wollet).await.unwrap().unwrap(); + wollet.apply_update(update).unwrap(); + + assert_eq!(wollet.address(None).unwrap().index(), 1_004); + } }