Skip to content

Commit

Permalink
subxt: Use unstable backend for light client
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Feb 29, 2024
1 parent 518aa8e commit ea6f3cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion subxt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
unstable-light-client = ["subxt-lightclient", "tokio-stream", "tokio"]

[dependencies]
async-trait = { workspace = true }
Expand Down Expand Up @@ -117,6 +117,7 @@ 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 }
Expand Down
24 changes: 23 additions & 1 deletion subxt/src/client/light_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,29 @@ impl RawLightClient {
) -> Result<LightClient<TChainConfig>, crate::Error> {
let raw_rpc = self.raw_rpc.for_chain(chain_id);
let rpc_client = RpcClient::new(raw_rpc.clone());
let client = OnlineClient::<TChainConfig>::from_rpc_client(rpc_client).await?;

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.
eprintln!("Error driving unstable backend in tests (will panic): {e}");
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}"))?;

Ok(LightClient { client, chain_id })
}
Expand Down

0 comments on commit ea6f3cc

Please sign in to comment.