bitcoindevkit / bdk

A modern, lightweight, descriptor-based wallet library written in Rust!
Other
865 stars 311 forks source link

Retry mechanism broken on WASM #1683

Open darioAnongba opened 2 days ago

darioAnongba commented 2 days ago

Describe the bug
The retry mechanism get_with_retry does not work on the browser (wasm32-unknown-unknown) and fails with the following error:

error output:
        panicked at library/std/src/sys/pal/wasm/../unsupported/time.rs:31:9:
        time not implemented on this platform

The reason is that WebAssembly in browsers doesn’t have direct support for timing mechanisms like std::time::Instant or std::thread::sleep.

To Reproduce
Can run the tests on this repo for sync: https://github.com/darioAnongba/bdk-wasm

Expected behavior

Build environment

Additional context
Using gloo-timers instead ?

use gloo_timers::future::sleep;
use std::time::Duration;

const BASE_BACKOFF_MILLIS: u64 = 100;

async fn get_with_retry(&self, url: &str) -> Result<Response, Error> {
    let mut delay = BASE_BACKOFF_MILLIS;
    let mut attempts = 0;

    loop {
        match self.client.get(url).send().await {
            Ok(resp) if attempts < self.max_retries && is_status_retryable(resp.status()) => {
                sleep(Duration::from_millis(delay)).await;
                attempts += 1;
                delay *= 2;
            }
            Ok(resp) => return Ok(resp),
            Err(e) => return Err(e), // Handle error if send fails
        }
    }
}

or 2 functions

#[cfg(not(target_arch = "wasm32"))]
async fn get_with_retry(...) {
    // Original retry logic with task::sleep
}

#[cfg(target_arch = "wasm32")]
async fn get_with_retry(...) {
    // Non-retrying version for WebAssembly
}
ValuedMammal commented 2 days ago

I think it is also related to bitcoindevkit/rust-esplora-client#102

ValuedMammal commented 2 days ago

@darioAnongba Is it possible to test syncing in bdk-wasm with this branch of bdk based on a proposed change to rust-esplora-client that makes the async client generic over the sleep implementation? https://github.com/ValuedMammal/bdk/tree/deps/esplora-client-103