// 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 { client: Arc>>, } impl From> for SharedClient { fn from(client: sl::Client) -> Self { SharedClient { client: Arc::new(Mutex::new(client)), } } } impl SharedClient { /// Delegates to [`smoldot_light::Client::json_rpc_request()`]. pub(crate) fn json_rpc_request( &self, json_rpc_request: impl Into, 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>, ) -> Result, sl::AddChainError> { self.client .lock() .expect("mutex should not be poisoned") .add_chain(config) } }