Skip to main content

forest/rpc/reflect/
mod.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4//! Forest wishes to provide [OpenRPC](http://open-rpc.org) definitions for
5//! Filecoin APIs.
6//! To do this, it needs:
7//! - [JSON Schema](https://json-schema.org/) definitions for all the argument
8//!   and return types.
9//! - The number of arguments ([arity](https://en.wikipedia.org/wiki/Arity)) and
10//!   names of those arguments for each RPC method.
11//!
12//! As a secondary objective, we wish to provide an RPC client for our CLI, and
13//! internal tests against Lotus.
14//!
15//! The [`RpcMethod`] trait encapsulates all the above at a single site.
16//! - [`schemars::JsonSchema`] provides schema definitions,
17//! - [`RpcMethod`] defining arity and actually dispatching the function calls.
18
19pub mod jsonrpc_types;
20
21mod parser;
22mod util;
23
24use crate::lotus_json::HasLotusJson;
25
26use self::{jsonrpc_types::RequestParameters, util::Optional as _};
27use super::error::ServerError as Error;
28use ahash::HashMap;
29use anyhow::Context as _;
30use enumflags2::{BitFlags, bitflags, make_bitflags};
31use fvm_ipld_blockstore::Blockstore;
32use http::Uri;
33use jsonrpsee::RpcModule;
34use openrpc_types::{ContentDescriptor, Method, ParamStructure, ReferenceOr};
35use parser::Parser;
36use schemars::{JsonSchema, Schema, SchemaGenerator};
37use serde::{
38    Deserialize, Serialize,
39    de::{Error as _, Unexpected},
40};
41use std::{future::Future, str::FromStr, sync::Arc};
42use strum::EnumString;
43
44/// Type to be used by [`RpcMethod::handle`].
45pub type Ctx<T> = Arc<crate::rpc::RPCState<T>>;
46
47/// A definition of an RPC method handler which:
48/// - can be [registered](RpcMethodExt::register) with an [`RpcModule`].
49/// - can describe itself in OpenRPC.
50///
51/// Note, an earlier draft of this trait had an additional type parameter for `Ctx`
52/// for generality.
53/// However, fixing it as [`Ctx<...>`] saves on complexity/confusion for implementors,
54/// at the expense of handler flexibility, which could come back to bite us.
55/// - All handlers accept the same type.
56/// - All `Ctx`s must be `Send + Sync + 'static` due to bounds on [`RpcModule`].
57/// - Handlers don't specialize on top of the given bounds, but they MAY relax them.
58pub trait RpcMethod<const ARITY: usize> {
59    /// Number of required parameters, defaults to `ARITY`.
60    const N_REQUIRED_PARAMS: usize = ARITY;
61    /// Method name.
62    const NAME: &'static str;
63    /// Alias for `NAME`. Note that currently this is not reflected in the OpenRPC spec.
64    const NAME_ALIAS: Option<&'static str> = None;
65    /// Name of each argument, MUST be unique.
66    const PARAM_NAMES: [&'static str; ARITY];
67    /// See [`ApiPaths`].
68    const API_PATHS: BitFlags<ApiPaths>;
69    /// See [`Permission`]
70    const PERMISSION: Permission;
71    /// Becomes [`openrpc_types::Method::summary`].
72    const SUMMARY: Option<&'static str> = None;
73    /// Becomes [`openrpc_types::Method::description`].
74    const DESCRIPTION: Option<&'static str> = None;
75    /// Types of each argument. [`Option`]-al arguments MUST follow mandatory ones.
76    type Params: Params<ARITY>;
77    /// Return value of this method.
78    type Ok: HasLotusJson;
79    /// Logic for this method.
80    fn handle(
81        ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
82        params: Self::Params,
83    ) -> impl Future<Output = Result<Self::Ok, Error>> + Send;
84    /// If it a subscription method. Defaults to false.
85    const SUBSCRIPTION: bool = false;
86}
87
88/// The permission required to call an RPC method.
89#[derive(
90    Debug,
91    Clone,
92    Copy,
93    PartialEq,
94    Eq,
95    PartialOrd,
96    Ord,
97    Hash,
98    derive_more::Display,
99    Serialize,
100    Deserialize,
101)]
102#[serde(rename_all = "snake_case")]
103pub enum Permission {
104    /// admin
105    Admin,
106    /// sign
107    Sign,
108    /// write
109    Write,
110    /// read
111    Read,
112}
113
114/// Which paths should this method be exposed on?
115///
116/// This information is important when using [`crate::rpc::client`].
117#[bitflags]
118#[repr(u8)]
119#[derive(
120    Debug,
121    Default,
122    Clone,
123    Copy,
124    Hash,
125    Eq,
126    PartialEq,
127    Ord,
128    PartialOrd,
129    clap::ValueEnum,
130    EnumString,
131    Deserialize,
132    Serialize,
133)]
134pub enum ApiPaths {
135    /// Only expose this method on `/rpc/v0`
136    #[strum(ascii_case_insensitive)]
137    V0 = 0b00000001,
138    /// Only expose this method on `/rpc/v1`
139    #[strum(ascii_case_insensitive)]
140    #[default]
141    V1 = 0b00000010,
142    /// Only expose this method on `/rpc/v2`
143    #[strum(ascii_case_insensitive)]
144    V2 = 0b00000100,
145}
146
147impl ApiPaths {
148    pub const fn all() -> BitFlags<Self> {
149        // Not containing V2 until it's released in Lotus.
150        make_bitflags!(Self::{ V0 | V1 })
151    }
152
153    // Remove this helper once all RPC methods are migrated to V2.
154    // We're incrementally migrating methods to V2 support. Once complete,
155    // update all() to include V2 and remove this temporary helper.
156    pub const fn all_with_v2() -> BitFlags<Self> {
157        Self::all().union_c(make_bitflags!(Self::{ V2 }))
158    }
159
160    pub fn from_uri(uri: &Uri) -> anyhow::Result<Self> {
161        Ok(Self::from_str(uri.path().trim_start_matches("/rpc/"))?)
162    }
163
164    pub fn path(&self) -> &'static str {
165        match self {
166            Self::V0 => "rpc/v0",
167            Self::V1 => "rpc/v1",
168            Self::V2 => "rpc/v2",
169        }
170    }
171}
172
173/// Utility methods, defined as an extension trait to avoid having to specify
174/// `ARITY` in user code.
175pub trait RpcMethodExt<const ARITY: usize>: RpcMethod<ARITY> {
176    /// Convert from typed handler parameters to un-typed JSON-RPC parameters.
177    ///
178    /// Exposes errors from [`Params::unparse`]
179    fn build_params(
180        params: Self::Params,
181        calling_convention: ConcreteCallingConvention,
182    ) -> Result<RequestParameters, serde_json::Error> {
183        let args = params.unparse()?;
184        match calling_convention {
185            ConcreteCallingConvention::ByPosition => {
186                Ok(RequestParameters::ByPosition(Vec::from(args)))
187            }
188            ConcreteCallingConvention::ByName => Ok(RequestParameters::ByName(
189                itertools::zip_eq(Self::PARAM_NAMES.into_iter().map(String::from), args).collect(),
190            )),
191        }
192    }
193
194    fn parse_params(
195        params_raw: Option<impl AsRef<str>>,
196        calling_convention: ParamStructure,
197    ) -> anyhow::Result<Self::Params> {
198        Ok(Self::Params::parse(
199            params_raw
200                .map(|s| serde_json::from_str(s.as_ref()))
201                .transpose()?,
202            Self::PARAM_NAMES,
203            calling_convention,
204            Self::N_REQUIRED_PARAMS,
205        )?)
206    }
207
208    /// Generate a full `OpenRPC` method definition for this endpoint.
209    fn openrpc<'de>(
210        g: &mut SchemaGenerator,
211        calling_convention: ParamStructure,
212        method_name: &'static str,
213    ) -> Method
214    where
215        <Self::Ok as HasLotusJson>::LotusJson: JsonSchema + Deserialize<'de>,
216    {
217        Method {
218            name: String::from(method_name),
219            params: itertools::zip_eq(Self::PARAM_NAMES, Self::Params::schemas(g))
220                .enumerate()
221                .map(|(pos, (name, (schema, nullable)))| {
222                    let required = pos <= Self::N_REQUIRED_PARAMS;
223                    if !required && !nullable {
224                        panic!("Optional parameter at position {pos} should be of an optional type. method={method_name}, param_name={name}");
225                    }
226                    ReferenceOr::Item(ContentDescriptor {
227                        name: String::from(name),
228                        schema,
229                        required: Some(required),
230                        ..Default::default()
231                    })
232                })
233                .collect(),
234            param_structure: Some(calling_convention),
235            result: Some(ReferenceOr::Item(ContentDescriptor {
236                name: format!("{method_name}.Result"),
237                schema: g.subschema_for::<<Self::Ok as HasLotusJson>::LotusJson>(),
238                required: Some(!<Self::Ok as HasLotusJson>::LotusJson::optional()),
239                ..Default::default()
240            })),
241            summary: Self::SUMMARY.map(Into::into),
242            description: Self::DESCRIPTION.map(Into::into),
243            ..Default::default()
244        }
245    }
246
247    /// Register a method with an [`RpcModule`].
248    fn register(
249        modules: &mut HashMap<
250            ApiPaths,
251            RpcModule<crate::rpc::RPCState<impl Blockstore + Send + Sync + 'static>>,
252        >,
253        calling_convention: ParamStructure,
254    ) -> Result<(), jsonrpsee::core::RegisterMethodError>
255    where
256        <Self::Ok as HasLotusJson>::LotusJson: Clone + 'static,
257    {
258        use clap::ValueEnum as _;
259
260        assert!(
261            Self::N_REQUIRED_PARAMS <= ARITY,
262            "N_REQUIRED_PARAMS({}) can not be greater than ARITY({ARITY}) in {}",
263            Self::N_REQUIRED_PARAMS,
264            Self::NAME
265        );
266
267        for api_version in ApiPaths::value_variants() {
268            if Self::API_PATHS.contains(*api_version)
269                && let Some(module) = modules.get_mut(api_version)
270            {
271                module.register_async_method(
272                    Self::NAME,
273                    move |params, ctx, _extensions| async move {
274                        let params = Self::parse_params(params.as_str(), calling_convention)
275                            .map_err(|e| Error::invalid_params(e, None))?;
276                        let ok = Self::handle(ctx, params).await?;
277                        Result::<_, jsonrpsee::types::ErrorObjectOwned>::Ok(ok.into_lotus_json())
278                    },
279                )?;
280                if let Some(alias) = Self::NAME_ALIAS {
281                    module.register_alias(alias, Self::NAME)?
282                }
283            }
284        }
285        Ok(())
286    }
287    /// Returns [`Err`] if any of the parameters fail to serialize.
288    fn request(params: Self::Params) -> Result<crate::rpc::Request<Self::Ok>, serde_json::Error> {
289        // hardcode calling convention because lotus is by-position only
290        let params = Self::request_params(params)?;
291        Ok(crate::rpc::Request {
292            method_name: Self::NAME.into(),
293            params,
294            result_type: std::marker::PhantomData,
295            api_paths: Self::API_PATHS,
296            timeout: *crate::rpc::DEFAULT_REQUEST_TIMEOUT,
297        })
298    }
299
300    fn request_params(params: Self::Params) -> Result<serde_json::Value, serde_json::Error> {
301        // hardcode calling convention because lotus is by-position only
302        Ok(
303            match Self::build_params(params, ConcreteCallingConvention::ByPosition)? {
304                RequestParameters::ByPosition(mut it) => {
305                    // Omit optional parameters when they are null
306                    // This can be refactored into using `while pop_if`
307                    // when the API is stablized.
308                    while Self::N_REQUIRED_PARAMS < it.len() {
309                        match it.last() {
310                            Some(last) if last.is_null() => it.pop(),
311                            _ => break,
312                        };
313                    }
314                    serde_json::Value::Array(it)
315                }
316                RequestParameters::ByName(it) => serde_json::Value::Object(it),
317            },
318        )
319    }
320
321    /// Creates a request, using the alias method name if `use_alias` is `true`.
322    fn request_with_alias(
323        params: Self::Params,
324        use_alias: bool,
325    ) -> anyhow::Result<crate::rpc::Request<Self::Ok>> {
326        let params = Self::request_params(params)?;
327        let name = if use_alias {
328            Self::NAME_ALIAS.context("alias is None")?
329        } else {
330            Self::NAME
331        };
332
333        Ok(crate::rpc::Request {
334            method_name: name.into(),
335            params,
336            result_type: std::marker::PhantomData,
337            api_paths: Self::API_PATHS,
338            timeout: *crate::rpc::DEFAULT_REQUEST_TIMEOUT,
339        })
340    }
341    fn call_raw(
342        client: &crate::rpc::client::Client,
343        params: Self::Params,
344    ) -> impl Future<Output = Result<<Self::Ok as HasLotusJson>::LotusJson, jsonrpsee::core::ClientError>>
345    {
346        async {
347            // TODO(forest): https://github.com/ChainSafe/forest/issues/4032
348            //               Client::call has an inappropriate HasLotusJson
349            //               bound, work around it for now.
350            let json = client.call(Self::request(params)?.map_ty()).await?;
351            Ok(serde_json::from_value(json)?)
352        }
353    }
354    fn call(
355        client: &crate::rpc::client::Client,
356        params: Self::Params,
357    ) -> impl Future<Output = Result<Self::Ok, jsonrpsee::core::ClientError>> {
358        async {
359            Self::call_raw(client, params)
360                .await
361                .map(Self::Ok::from_lotus_json)
362        }
363    }
364}
365impl<const ARITY: usize, T> RpcMethodExt<ARITY> for T where T: RpcMethod<ARITY> {}
366
367/// A tuple of `ARITY` arguments.
368///
369/// This should NOT be manually implemented.
370pub trait Params<const ARITY: usize>: HasLotusJson {
371    /// A [`Schema`] and [`Optional::optional`](`util::Optional::optional`)
372    /// schema-nullable pair for argument, in-order.
373    fn schemas(g: &mut SchemaGenerator) -> [(Schema, bool); ARITY];
374    /// Convert from raw request parameters, to the argument tuple required by
375    /// [`RpcMethod::handle`]
376    fn parse(
377        raw: Option<RequestParameters>,
378        names: [&str; ARITY],
379        calling_convention: ParamStructure,
380        n_required: usize,
381    ) -> Result<Self, Error>
382    where
383        Self: Sized;
384    /// Convert from an argument tuple to un-typed JSON.
385    ///
386    /// Exposes de-serialization errors, or mis-implementation of this trait.
387    fn unparse(self) -> Result<[serde_json::Value; ARITY], serde_json::Error> {
388        match self.into_lotus_json_value() {
389            Ok(serde_json::Value::Array(args)) => match args.try_into() {
390                Ok(it) => Ok(it),
391                Err(_) => Err(serde_json::Error::custom("ARITY mismatch")),
392            },
393            Ok(serde_json::Value::Null) if ARITY == 0 => {
394                Ok(std::array::from_fn(|_ix| Default::default()))
395            }
396            Ok(it) => Err(serde_json::Error::invalid_type(
397                unexpected(&it),
398                &"a Vec with an item for each argument",
399            )),
400            Err(e) => Err(e),
401        }
402    }
403}
404
405fn unexpected(v: &serde_json::Value) -> Unexpected<'_> {
406    match v {
407        serde_json::Value::Null => Unexpected::Unit,
408        serde_json::Value::Bool(it) => Unexpected::Bool(*it),
409        serde_json::Value::Number(it) => match (it.as_f64(), it.as_i64(), it.as_u64()) {
410            (None, None, None) => Unexpected::Other("Number"),
411            (Some(it), _, _) => Unexpected::Float(it),
412            (_, Some(it), _) => Unexpected::Signed(it),
413            (_, _, Some(it)) => Unexpected::Unsigned(it),
414        },
415        serde_json::Value::String(it) => Unexpected::Str(it),
416        serde_json::Value::Array(_) => Unexpected::Seq,
417        serde_json::Value::Object(_) => Unexpected::Map,
418    }
419}
420
421macro_rules! do_impls {
422    ($arity:literal $(, $arg:ident)* $(,)?) => {
423        const _: () = {
424            let _assert: [&str; $arity] = [$(stringify!($arg)),*];
425        };
426
427        impl<$($arg),*> Params<$arity> for ($($arg,)*)
428        where
429            $($arg: HasLotusJson + Clone, <$arg as HasLotusJson>::LotusJson: JsonSchema, )*
430        {
431            fn parse(
432                raw: Option<RequestParameters>,
433                arg_names: [&str; $arity],
434                calling_convention: ParamStructure,
435                n_required: usize,
436            ) -> Result<Self, Error> {
437                let mut _parser = Parser::new(raw, &arg_names, calling_convention, n_required)?;
438                Ok(($(_parser.parse::<crate::lotus_json::LotusJson<$arg>>()?.into_inner(),)*))
439            }
440            fn schemas(_gen: &mut SchemaGenerator) -> [(Schema, bool); $arity] {
441                [$((_gen.subschema_for::<$arg::LotusJson>(), $arg::LotusJson::optional())),*]
442            }
443        }
444    };
445}
446
447do_impls!(0);
448do_impls!(1, T0);
449do_impls!(2, T0, T1);
450do_impls!(3, T0, T1, T2);
451do_impls!(4, T0, T1, T2, T3);
452// do_impls!(5, T0, T1, T2, T3, T4);
453// do_impls!(6, T0, T1, T2, T3, T4, T5);
454// do_impls!(7, T0, T1, T2, T3, T4, T5, T6);
455// do_impls!(8, T0, T1, T2, T3, T4, T5, T6, T7);
456// do_impls!(9, T0, T1, T2, T3, T4, T5, T6, T7, T8);
457// do_impls!(10, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9);
458
459/// [`openrpc_types::ParamStructure`] describes accepted param format.
460/// This is an actual param format, used to decide how to construct arguments.
461pub enum ConcreteCallingConvention {
462    ByPosition,
463    #[allow(unused)] // included for completeness
464    ByName,
465}
466
467#[cfg(test)]
468mod tests {
469    use super::*;
470
471    #[test]
472    fn test_api_paths_from_uri() {
473        let v0 = ApiPaths::from_uri(&"http://127.0.0.1:2345/rpc/v0".parse().unwrap()).unwrap();
474        assert_eq!(v0, ApiPaths::V0);
475        let v1 = ApiPaths::from_uri(&"http://127.0.0.1:2345/rpc/v1".parse().unwrap()).unwrap();
476        assert_eq!(v1, ApiPaths::V1);
477        let v2 = ApiPaths::from_uri(&"http://127.0.0.1:2345/rpc/v2".parse().unwrap()).unwrap();
478        assert_eq!(v2, ApiPaths::V2);
479
480        ApiPaths::from_uri(&"http://127.0.0.1:2345/rpc/v3".parse().unwrap()).unwrap_err();
481    }
482}