Skip to content

wollet: waterfalls: fix last_unused on later pages - #212

Open
diorwave wants to merge 1 commit into
Blockstream:masterfrom
diorwave:waterfalls-last-unused
Open

diorwave wants to merge 1 commit into
Blockstream:masterfrom
diorwave:waterfalls-last-unused

Conversation

@diorwave

Copy link
Copy Markdown

Waterfalls scans computed last_unused relative to the page, so wallets with more than 1000 used addresses on a chain could return an already used address.

The test serves a two page scan from a local stub, it takes about 1s in debug builds because the first page derives 1000 addresses.

Fixes #209

- 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 Blockstream#209
assert!(matches!(err, Error::EsploraHttpError { status: 422, .. }));
}

#[tokio::test]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convert this to a e2e test, showing that the bug can happen with a real waterfalls server

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eg

--- a/lwk_wollet/tests/e2e.rs
+++ b/lwk_wollet/tests/e2e.rs
@@ -2199,6 +2199,42 @@ fn test_clients() {
     assert_eq!(esplora_waterfalls_client.capabilities().len(), 1);
 }
 
+#[cfg(feature = "esplora")]
+#[test]
+fn waterfalls_last_unused_spans_pages() {
+    let env = TestEnvBuilder::from_env().with_waterfalls().build();
+    let network = Network::default_regtest();
+    let mut client =
+        clients::blocking::WaterfallsClient::new(&env.waterfalls_url(), network).unwrap();
+
+    let view_key = generate_view_key();
+    let signer = generate_signer();
+    let desc = format!("ct({},elwpkh({}/<0;1>/*))", view_key, signer.xpub());
+    let desc = WolletDescriptor::from_str(&desc).unwrap();
+    let mut wollet = WolletBuilder::new(network, desc).build().unwrap();
+
+    // Index 1003 is on the second Waterfalls page (each page has 1000 addresses)
+    let index = 1_003;
+    let address = wollet.address(Some(index)).unwrap();
+    let txid = env.elementsd_sendtoaddress(address.address(), 1_000, None);
+
+    // Wait until waterfalls processes and returns the tx
+    for _ in 0..50 {
+        if let Some(update) = client.full_scan_to_index(&wollet, index).unwrap() {
+            wollet.apply_update(update).unwrap();
+        }
+        if wollet.transaction(&txid).unwrap().is_some() {
+            break;
+        }
+        std::thread::sleep(std::time::Duration::from_millis(500));
+    }
+
+    assert!(wollet.transaction(&txid).unwrap().is_some());
+    assert_eq!(wollet.address(None).unwrap().index(), index + 1);
+}
+

@LeoComandini

Copy link
Copy Markdown
Contributor

lgtm with test converted

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wollet: Waterfalls scan sets last_unused to a page-relative index past the first page

2 participants