Files
pezkuwi-subxt/lightclient/src/shared_client.rs
T
James Wilson b069c4425a Rework light client (#1475)
* WIP second pass over light client code for simpler API

* First pass new light client

* pub(crate) LightClientRpc::new_raw(), and fmt

* Update examples and add back a way to configure boot nodes and fetch chainspec from a URL

* Fix light client examples

* remove unused deps and tidy lightclient feature flags

* fix wasm error

* LightClientRpc can be cloned

* update light client tests

* Other small fixes

* exclude mod unless jsonrpsee

* Fix wasm-lightclient-tests

* add back docsrs bit and web+native feature flag compile error

* update book and light client example names

* fix docs
2024-03-15 15:21:06 +00:00

48 lines
1.5 KiB
Rust

// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use smoldot_light as sl;
use std::sync::{Arc, Mutex};
/// This wraps [`smoldot_light::Client`] so that it can be cloned and shared.
#[derive(Clone)]
pub struct SharedClient<TPlat: sl::platform::PlatformRef, TChain = ()> {
client: Arc<Mutex<sl::Client<TPlat, TChain>>>,
}
impl<TPlat: sl::platform::PlatformRef, TChain> From<sl::Client<TPlat, TChain>>
for SharedClient<TPlat, TChain>
{
fn from(client: sl::Client<TPlat, TChain>) -> Self {
SharedClient {
client: Arc::new(Mutex::new(client)),
}
}
}
impl<TPlat: sl::platform::PlatformRef, TChain> SharedClient<TPlat, TChain> {
/// Delegates to [`smoldot_light::Client::json_rpc_request()`].
pub(crate) fn json_rpc_request(
&self,
json_rpc_request: impl Into<String>,
chain_id: sl::ChainId,
) -> Result<(), sl::HandleRpcError> {
self.client
.lock()
.expect("mutex should not be poisoned")
.json_rpc_request(json_rpc_request, chain_id)
}
/// Delegates to [`smoldot_light::Client::add_chain()`].
pub(crate) fn add_chain(
&self,
config: sl::AddChainConfig<'_, TChain, impl Iterator<Item = sl::ChainId>>,
) -> Result<sl::AddChainSuccess, sl::AddChainError> {
self.client
.lock()
.expect("mutex should not be poisoned")
.add_chain(config)
}
}