smoldot_light/json_rpc_service.rs
1// Smoldot
2// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
3// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18//! Background JSON-RPC service.
19//!
20//! # Usage
21//!
22//! Create a new JSON-RPC service by calling [`service()`].
23//! Creating a JSON-RPC service spawns a background task (through [`PlatformRef::spawn_task`])
24//! dedicated to processing JSON-RPC requests.
25//!
26//! In order to process a JSON-RPC request, call [`Frontend::queue_rpc_request`]. Later, the
27//! JSON-RPC service can queue a response or, in the case of subscriptions, a notification. They
28//! can be retrieved by calling [`Frontend::next_json_rpc_response`].
29//!
30//! In the situation where an attacker finds a JSON-RPC request that takes a long time to be
31//! processed and continuously submits this same expensive request over and over again, the queue
32//! of pending requests will start growing and use more and more memory. For this reason, if this
33//! queue grows past [`Config::max_pending_requests`] items, [`Frontend::queue_rpc_request`]
34//! will instead return an error.
35//!
36
37// TODO: doc
38// TODO: re-review this once finished
39
40mod background;
41mod statement;
42
43use crate::{
44 bitswap_service, log, network_service, platform::PlatformRef, runtime_service, sync_service,
45 transactions_service,
46};
47
48use alloc::{
49 borrow::Cow,
50 boxed::Box,
51 format,
52 string::{String, ToString as _},
53 sync::Arc,
54};
55use core::{num::NonZero, pin::Pin};
56use futures_lite::StreamExt as _;
57
58/// Configuration for [`service()`].
59pub struct Config<TPlat: PlatformRef> {
60 /// Access to the platform's capabilities.
61 pub platform: TPlat,
62
63 /// Name of the chain, for logging purposes.
64 ///
65 /// > **Note**: This name will be directly printed out. Any special character should already
66 /// > have been filtered out from this name.
67 pub log_name: String,
68
69 /// Maximum number of JSON-RPC requests that can be added to a queue if it is not ready to be
70 /// processed immediately. Any additional request will be immediately rejected.
71 ///
72 /// This parameter is necessary in order to prevent users from using up too much memory within
73 /// the client.
74 // TODO: unused at the moment
75 #[allow(unused)]
76 pub max_pending_requests: NonZero<u32>,
77
78 /// Maximum number of active subscriptions. Any additional subscription will be immediately
79 /// rejected.
80 ///
81 /// This parameter is necessary in order to prevent users from using up too much memory within
82 /// the client.
83 // TODO: unused at the moment
84 #[allow(unused)]
85 pub max_subscriptions: u32,
86
87 /// Access to the network, and identifier of the chain from the point of view of the network
88 /// service.
89 pub network_service: Arc<network_service::NetworkServiceChain<TPlat>>,
90
91 /// Service responsible for synchronizing the chain.
92 pub sync_service: Arc<sync_service::SyncService<TPlat>>,
93
94 /// Service responsible for emitting transactions and tracking their state.
95 pub transactions_service: Arc<transactions_service::TransactionsService<TPlat>>,
96
97 /// Service that provides a ready-to-be-called runtime for the current best block.
98 pub runtime_service: Arc<runtime_service::RuntimeService<TPlat>>,
99
100 /// Service that fulfills IPFS CID requests.
101 pub bitswap_service: Arc<bitswap_service::BitswapService>,
102
103 /// Name of the chain, as found in the chain specification.
104 pub chain_name: String,
105 /// Type of chain, as found in the chain specification.
106 pub chain_ty: String,
107 /// JSON-encoded properties of the chain, as found in the chain specification.
108 pub chain_properties_json: String,
109 /// Whether the chain is a live network. Found in the chain specification.
110 pub chain_is_live: bool,
111
112 /// Value to return when the `system_name` RPC is called. Should be set to the name of the
113 /// final executable.
114 pub system_name: String,
115
116 /// Value to return when the `system_version` RPC is called. Should be set to the version of
117 /// the final executable.
118 pub system_version: String,
119
120 /// Hash of the genesis block of the chain.
121 pub genesis_block_hash: [u8; 32],
122
123 /// Statement protocol configuration. `None` if the statement protocol is disabled.
124 pub statement_protocol_config: Option<network_service::StatementProtocolConfig>,
125
126 /// Maximum number of seen statement hashes tracked per subscription for dedup.
127 /// `None` if the statement protocol is disabled.
128 pub max_seen_statements: Option<NonZero<usize>>,
129}
130
131/// Creates a new JSON-RPC service with the given configuration.
132///
133/// Returns a handler that allows sending requests and receiving responses.
134///
135/// Destroying the [`Frontend`] automatically shuts down the service.
136pub fn service<TPlat: PlatformRef>(config: Config<TPlat>) -> Frontend<TPlat> {
137 let log_target = format!("json-rpc-{}", config.log_name);
138
139 let (requests_tx, requests_rx) = async_channel::unbounded(); // TODO: capacity?
140 let (responses_tx, responses_rx) = async_channel::bounded(16); // TODO: capacity?
141
142 let frontend = Frontend {
143 platform: config.platform.clone(),
144 log_target: log_target.clone(),
145 responses_rx: Arc::new(async_lock::Mutex::new(Box::pin(responses_rx))),
146 requests_tx,
147 };
148
149 let platform = config.platform.clone();
150 platform.spawn_task(
151 Cow::Owned(log_target.clone()),
152 background::run(
153 log_target,
154 background::Config {
155 platform: config.platform,
156 network_service: config.network_service,
157 sync_service: config.sync_service,
158 transactions_service: config.transactions_service,
159 runtime_service: config.runtime_service,
160 bitswap_service: config.bitswap_service,
161 chain_name: config.chain_name,
162 chain_ty: config.chain_ty,
163 chain_properties_json: config.chain_properties_json,
164 chain_is_live: config.chain_is_live,
165 system_name: config.system_name,
166 system_version: config.system_version,
167 genesis_block_hash: config.genesis_block_hash,
168 statement_protocol_config: config.statement_protocol_config,
169 max_seen_statements: config.max_seen_statements,
170 },
171 requests_rx,
172 responses_tx,
173 ),
174 );
175
176 frontend
177}
178
179/// Handle that allows sending JSON-RPC requests on the service.
180///
181/// The [`Frontend`] can be cloned, in which case the clone will refer to the same JSON-RPC
182/// service.
183///
184/// Destroying all the [`Frontend`]s automatically shuts down the associated service.
185#[derive(Clone)]
186pub struct Frontend<TPlat> {
187 /// See [`Config::platform`].
188 platform: TPlat,
189
190 /// How to send requests to the background task.
191 requests_tx: async_channel::Sender<String>,
192
193 /// How to receive responses coming from the background task.
194 // TODO: we use an Arc so that it's clonable, but that's questionnable
195 responses_rx: Arc<async_lock::Mutex<Pin<Box<async_channel::Receiver<String>>>>>,
196
197 /// Target to use when emitting logs.
198 log_target: String,
199}
200
201impl<TPlat: PlatformRef> Frontend<TPlat> {
202 /// Queues the given JSON-RPC request to be processed in the background.
203 ///
204 /// An error is returned if [`Config::max_pending_requests`] is exceeded, which can happen
205 /// if the requests take a long time to process or if [`Frontend::next_json_rpc_response`]
206 /// isn't called often enough.
207 pub fn queue_rpc_request(&self, json_rpc_request: String) -> Result<(), HandleRpcError> {
208 let log_friendly_request =
209 crate::util::truncated_str(json_rpc_request.chars().filter(|c| !c.is_control()), 250)
210 .to_string();
211
212 match self.requests_tx.try_send(json_rpc_request) {
213 Ok(()) => {
214 log!(
215 &self.platform,
216 Debug,
217 &self.log_target,
218 "json-rpc-request-queued",
219 request = log_friendly_request
220 );
221 Ok(())
222 }
223 Err(err) => Err(HandleRpcError::TooManyPendingRequests {
224 json_rpc_request: err.into_inner(),
225 }),
226 }
227 }
228
229 /// Waits until a JSON-RPC response has been generated, then returns it.
230 ///
231 /// If this function is called multiple times in parallel, the order in which the calls are
232 /// responded to is unspecified.
233 pub async fn next_json_rpc_response(&self) -> String {
234 let message = match self.responses_rx.lock().await.next().await {
235 Some(m) => m,
236 None => unreachable!(),
237 };
238
239 log!(
240 &self.platform,
241 Debug,
242 &self.log_target,
243 "json-rpc-response-yielded",
244 response =
245 crate::util::truncated_str(message.chars().filter(|c| !c.is_control()), 250,)
246 );
247
248 message
249 }
250}
251
252/// Error potentially returned when queuing a JSON-RPC request.
253#[derive(Debug, derive_more::Display, derive_more::Error)]
254pub enum HandleRpcError {
255 /// The JSON-RPC service cannot process this request, as too many requests are already being
256 /// processed.
257 #[display(
258 "The JSON-RPC service cannot process this request, as too many requests are already being processed."
259 )]
260 TooManyPendingRequests {
261 /// Request that was being queued.
262 json_rpc_request: String,
263 },
264}