diff --git a/Cargo.lock b/Cargo.lock index d830a09f7c..3b7cefb4ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4547,6 +4547,7 @@ dependencies = [ "tracing", "tracing-subscriber 0.3.18", "url", + "wasm-bindgen-futures", ] [[package]] diff --git a/subxt/Cargo.toml b/subxt/Cargo.toml index f06bd9dd63..9b927ed1db 100644 --- a/subxt/Cargo.toml +++ b/subxt/Cargo.toml @@ -61,7 +61,7 @@ unstable-metadata = [] # Activate this to expose the Light Client functionality. # Note that this feature is experimental and things may break or not work as expected. -unstable-light-client = ["subxt-lightclient", "tokio-stream", "tokio"] +unstable-light-client = ["subxt-lightclient", "tokio-stream"] [dependencies] async-trait = { workspace = true } @@ -117,7 +117,6 @@ getrandom = { workspace = true, optional = true } # Included if "native" feature is enabled tokio-util = { workspace = true, features = ["compat"], optional = true } -tokio = { workspace = true, features = ["macros", "time", "rt-multi-thread"], optional = true } [dev-dependencies] bitvec = { workspace = true } @@ -129,6 +128,7 @@ sp-keyring = { workspace = true } sp-runtime = { workspace = true } assert_matches = { workspace = true } subxt-signer = { path = "../signer" } +wasm-bindgen-futures = { workspace = true } # Tracing subscriber is useful for light-client examples to ensure that # the `bootNodes` and chain spec are configured correctly. If all is fine, then # the light-client wlll emit INFO logs with diff --git a/subxt/src/client/light_client/mod.rs b/subxt/src/client/light_client/mod.rs index 121fadcdab..4eeb58da18 100644 --- a/subxt/src/client/light_client/mod.rs +++ b/subxt/src/client/light_client/mod.rs @@ -94,27 +94,43 @@ impl RawLightClient { let raw_rpc = self.raw_rpc.for_chain(chain_id); let rpc_client = RpcClient::new(raw_rpc.clone()); - use crate::backend::unstable::UnstableBackend; - use std::sync::Arc; - let (backend, mut driver) = UnstableBackend::builder().build(rpc_client); - let future = async move { - use futures::StreamExt; - while let Some(val) = driver.next().await { - if let Err(e) = val { - // This is a test; bail if something does wrong and try to - // ensure that the message makes it to some logs. - panic!("Error driving unstable backend in tests: {e}"); + // Production code should use the legacy backend. + #[cfg(not(test))] + async fn build_online_client( + rpc_client: RpcClient, + ) -> Result, crate::Error> { + OnlineClient::::from_rpc_client(rpc_client).await + } + + // For testing we are only interested in using the unstable-backend with + // the lightclient. + #[cfg(test)] + async fn build_online_client( + rpc_client: RpcClient, + ) -> Result, crate::Error> { + use crate::backend::unstable::UnstableBackend; + use std::sync::Arc; + let (backend, mut driver) = UnstableBackend::builder().build(rpc_client); + let future = async move { + use futures::StreamExt; + while let Some(val) = driver.next().await { + if let Err(e) = val { + // This is a test; bail if something does wrong and try to + // ensure that the message makes it to some logs. + panic!("Error driving unstable backend in tests: {e}"); + } } - } - }; - // The unstable backend needs driving: - #[cfg(feature = "native")] - tokio::spawn(future); - #[cfg(feature = "web")] - wasm_bindgen_futures::spawn_local(future); - let client = OnlineClient::from_backend(Arc::new(backend)) - .await - .map_err(|e| format!("Cannot construct OnlineClient from backend: {e}"))?; + }; + // The unstable backend needs driving: + #[cfg(feature = "native")] + tokio::spawn(future); + #[cfg(feature = "web")] + wasm_bindgen_futures::spawn_local(future); + + OnlineClient::from_backend(Arc::new(backend)).await + } + + let client = build_online_client(rpc_client).await?; Ok(LightClient { client, chain_id }) }