1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// 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 std::iter;

use super::{
    background::{BackgroundTask, FromSubxt, MethodResponse},
    LightClientRpcError,
};
use serde_json::value::RawValue;
use tokio::sync::{mpsc, mpsc::error::SendError, oneshot};

use super::platform::build_platform;

pub const LOG_TARGET: &str = "subxt-light-client";

/// A raw light-client RPC implementation that can connect to multiple chains.
#[derive(Clone)]
pub struct RawLightClientRpc {
    /// Communicate with the backend task that multiplexes the responses
    /// back to the frontend.
    to_backend: mpsc::UnboundedSender<FromSubxt>,
}

impl RawLightClientRpc {
    /// Construct a [`LightClientRpc`] that can communicated with the provided chain.
    ///
    /// # Note
    ///
    /// This uses the same underlying instance created by [`LightClientRpc::new_from_client`].
    pub fn for_chain(&self, chain_id: smoldot_light::ChainId) -> LightClientRpc {
        LightClientRpc {
            to_backend: self.to_backend.clone(),
            chain_id,
        }
    }
}

/// The light-client RPC implementation that is used to connect with the chain.
#[derive(Clone)]
pub struct LightClientRpc {
    /// Communicate with the backend task that multiplexes the responses
    /// back to the frontend.
    to_backend: mpsc::UnboundedSender<FromSubxt>,
    /// The chain ID to target for requests.
    chain_id: smoldot_light::ChainId,
}

impl LightClientRpc {
    /// Constructs a new [`LightClientRpc`], providing the chain specification.
    ///
    /// The chain specification can be downloaded from a trusted network via
    /// the `sync_state_genSyncSpec` RPC method. This parameter expects the
    /// chain spec in text format (ie not in hex-encoded scale-encoded as RPC methods
    /// will provide).
    ///
    /// ## Panics
    ///
    /// The panic behaviour depends on the feature flag being used:
    ///
    /// ### Native
    ///
    /// Panics when called outside of a `tokio` runtime context.
    ///
    /// ### Web
    ///
    /// If smoldot panics, then the promise created will be leaked. For more details, see
    /// https://docs.rs/wasm-bindgen-futures/latest/wasm_bindgen_futures/fn.future_to_promise.html.
    pub fn new(
        config: smoldot_light::AddChainConfig<
            '_,
            (),
            impl IntoIterator<Item = smoldot_light::ChainId>,
        >,
    ) -> Result<LightClientRpc, LightClientRpcError> {
        tracing::trace!(target: LOG_TARGET, "Create light client");

        let mut client = smoldot_light::Client::new(build_platform());

        let config = smoldot_light::AddChainConfig {
            specification: config.specification,
            json_rpc: config.json_rpc,
            database_content: config.database_content,
            potential_relay_chains: config.potential_relay_chains.into_iter(),
            user_data: config.user_data,
        };

        let smoldot_light::AddChainSuccess {
            chain_id,
            json_rpc_responses,
        } = client
            .add_chain(config)
            .map_err(|err| LightClientRpcError::AddChainError(err.to_string()))?;

        let rpc_responses = json_rpc_responses.expect("Light client RPC configured; qed");

        let raw_client = Self::new_from_client(
            client,
            iter::once(AddedChain {
                chain_id,
                rpc_responses,
            }),
        );
        Ok(raw_client.for_chain(chain_id))
    }

    /// Constructs a new [`RawLightClientRpc`] from the raw smoldot client.
    ///
    /// Receives a list of RPC objects as a result of calling `smoldot_light::Client::add_chain`.
    /// This [`RawLightClientRpc`] can target different chains using [`RawLightClientRpc::for_chain`] method.
    ///
    /// ## Panics
    ///
    /// The panic behaviour depends on the feature flag being used:
    ///
    /// ### Native
    ///
    /// Panics when called outside of a `tokio` runtime context.
    ///
    /// ### Web
    ///
    /// If smoldot panics, then the promise created will be leaked. For more details, see
    /// https://docs.rs/wasm-bindgen-futures/latest/wasm_bindgen_futures/fn.future_to_promise.html.
    pub fn new_from_client<TPlat>(
        client: smoldot_light::Client<TPlat>,
        chains: impl IntoIterator<Item = AddedChain>,
    ) -> RawLightClientRpc
    where
        TPlat: smoldot_light::platform::PlatformRef + Clone,
    {
        let (to_backend, backend) = mpsc::unbounded_channel();
        let chains = chains.into_iter().collect();

        let future = async move {
            let mut task = BackgroundTask::new(client);
            task.start_task(backend, chains).await;
        };

        #[cfg(feature = "native")]
        tokio::spawn(future);
        #[cfg(feature = "web")]
        wasm_bindgen_futures::spawn_local(future);

        RawLightClientRpc { to_backend }
    }

    /// Returns the chain ID of the current light-client.
    pub fn chain_id(&self) -> smoldot_light::ChainId {
        self.chain_id
    }

    /// Submits an RPC method request to the light-client.
    ///
    /// This method sends a request to the light-client to execute an RPC method with the provided parameters.
    /// The parameters are parsed into a valid JSON object in the background.
    pub fn method_request(
        &self,
        method: String,
        params: String,
    ) -> Result<oneshot::Receiver<MethodResponse>, SendError<FromSubxt>> {
        let (sender, receiver) = oneshot::channel();

        self.to_backend.send(FromSubxt::Request {
            method,
            params,
            sender,
            chain_id: self.chain_id,
        })?;

        Ok(receiver)
    }

    /// Makes an RPC subscription call to the light-client.
    ///
    /// This method sends a request to the light-client to establish an RPC subscription with the provided parameters.
    /// The parameters are parsed into a valid JSON object in the background.
    #[allow(clippy::type_complexity)]
    pub fn subscription_request(
        &self,
        method: String,
        params: String,
    ) -> Result<
        (
            oneshot::Receiver<MethodResponse>,
            mpsc::UnboundedReceiver<Box<RawValue>>,
        ),
        SendError<FromSubxt>,
    > {
        let (sub_id, sub_id_rx) = oneshot::channel();
        let (sender, receiver) = mpsc::unbounded_channel();

        self.to_backend.send(FromSubxt::Subscription {
            method,
            params,
            sub_id,
            sender,
            chain_id: self.chain_id,
        })?;

        Ok((sub_id_rx, receiver))
    }
}

/// The added chain of the light-client.
pub struct AddedChain {
    /// The id of the chain.
    pub chain_id: smoldot_light::ChainId,
    /// Producer of RPC responses for the chain.
    pub rpc_responses: smoldot_light::JsonRpcResponses,
}