pub mod jsonrpc_types;
mod parser;
mod util;
use crate::lotus_json::HasLotusJson;
use self::{jsonrpc_types::RequestParameters, util::Optional as _};
use super::error::ServerError as Error;
use anyhow::Context as _;
use fvm_ipld_blockstore::Blockstore;
use itertools::{Either, Itertools as _};
use jsonrpsee::RpcModule;
use openrpc_types::{ContentDescriptor, Method, ParamStructure, ReferenceOr};
use parser::Parser;
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{
de::{Error as _, Unexpected},
Deserialize,
};
use std::{future::Future, iter, sync::Arc};
pub type Ctx<T> = Arc<crate::rpc::RPCState<T>>;
pub trait RpcMethod<const ARITY: usize> {
const N_REQUIRED_PARAMS: usize = ARITY;
const NAME: &'static str;
const NAME_ALIAS: Option<&'static str> = None;
const PARAM_NAMES: [&'static str; ARITY];
const API_PATHS: ApiPaths;
const PERMISSION: Permission;
const SUMMARY: Option<&'static str> = None;
const DESCRIPTION: Option<&'static str> = None;
type Params: Params<ARITY>;
type Ok: HasLotusJson;
fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
params: Self::Params,
) -> impl Future<Output = Result<Self::Ok, Error>> + Send;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, displaydoc::Display)]
pub enum Permission {
Admin,
Sign,
Write,
Read,
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum ApiPaths {
V0,
V1,
#[allow(dead_code)]
Both,
}
impl ApiPaths {
fn iter(&self) -> impl Iterator<Item = ApiPath> {
match self {
ApiPaths::V0 => Either::Left(iter::once(ApiPath::V0)),
ApiPaths::V1 => Either::Left(iter::once(ApiPath::V1)),
ApiPaths::Both => Either::Right([ApiPath::V0, ApiPath::V1].into_iter()),
}
}
pub fn max(&self) -> ApiPath {
self.iter().max().expect("cannot create an empty ApiPaths")
}
pub fn contains(&self, path: ApiPath) -> bool {
self.iter().contains(&path)
}
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, clap::ValueEnum)]
pub enum ApiPath {
V0,
V1,
}
pub trait RpcMethodExt<const ARITY: usize>: RpcMethod<ARITY> {
fn build_params(
params: Self::Params,
calling_convention: ConcreteCallingConvention,
) -> Result<RequestParameters, serde_json::Error> {
let args = params.unparse()?;
match calling_convention {
ConcreteCallingConvention::ByPosition => {
Ok(RequestParameters::ByPosition(Vec::from(args)))
}
ConcreteCallingConvention::ByName => Ok(RequestParameters::ByName(
itertools::zip_eq(Self::PARAM_NAMES.into_iter().map(String::from), args).collect(),
)),
}
}
fn openrpc<'de>(gen: &mut SchemaGenerator, calling_convention: ParamStructure) -> Method
where
<Self::Ok as HasLotusJson>::LotusJson: JsonSchema + Deserialize<'de>,
{
Method {
name: String::from(Self::NAME),
params: itertools::zip_eq(Self::PARAM_NAMES, Self::Params::schemas(gen))
.enumerate()
.map(|(pos, (name, (schema, nullable)))| {
let required = pos <= Self::N_REQUIRED_PARAMS;
if !required && !nullable {
panic!(
"Optional parameter at position {pos} should be of an optional type. method={}, param_name={name}", Self::NAME
);
}
ReferenceOr::Item(ContentDescriptor {
name: String::from(name),
schema,
required: Some(required),
..Default::default()
})
})
.collect(),
param_structure: Some(calling_convention),
result: Some(ReferenceOr::Item(ContentDescriptor {
name: format!("{}.Result", Self::NAME),
schema: gen.subschema_for::<<Self::Ok as HasLotusJson>::LotusJson>(),
required: Some(!<Self::Ok as HasLotusJson>::LotusJson::optional()),
..Default::default()
})),
summary: Self::SUMMARY.map(Into::into),
description: Self::DESCRIPTION.map(Into::into),
..Default::default()
}
}
fn register_alias(
module: &mut RpcModule<crate::rpc::RPCState<impl Blockstore + Send + Sync + 'static>>,
) -> Result<(), jsonrpsee::core::RegisterMethodError> {
if let Some(alias) = Self::NAME_ALIAS {
module.register_alias(alias, Self::NAME)?
}
Ok(())
}
fn register(
module: &mut RpcModule<crate::rpc::RPCState<impl Blockstore + Send + Sync + 'static>>,
calling_convention: ParamStructure,
) -> Result<&mut jsonrpsee::MethodCallback, jsonrpsee::core::RegisterMethodError>
where
<Self::Ok as HasLotusJson>::LotusJson: Clone + 'static,
{
assert!(
Self::N_REQUIRED_PARAMS <= ARITY,
"N_REQUIRED_PARAMS({}) can not be greater than ARITY({ARITY}) in {}",
Self::N_REQUIRED_PARAMS,
Self::NAME
);
module.register_async_method(Self::NAME, move |params, ctx, _extensions| async move {
let raw = params
.as_str()
.map(serde_json::from_str)
.transpose()
.map_err(|e| Error::invalid_params(e, None))?;
let params = Self::Params::parse(
raw,
Self::PARAM_NAMES,
calling_convention,
Self::N_REQUIRED_PARAMS,
)?;
let ok = Self::handle(ctx, params).await?;
Result::<_, jsonrpsee::types::ErrorObjectOwned>::Ok(ok.into_lotus_json())
})
}
fn request(params: Self::Params) -> Result<crate::rpc::Request<Self::Ok>, serde_json::Error> {
let params = Self::request_params(params)?;
Ok(crate::rpc::Request {
method_name: Self::NAME,
params,
result_type: std::marker::PhantomData,
api_paths: Self::API_PATHS,
timeout: *crate::rpc::DEFAULT_REQUEST_TIMEOUT,
})
}
fn request_params(params: Self::Params) -> Result<serde_json::Value, serde_json::Error> {
Ok(
match Self::build_params(params, ConcreteCallingConvention::ByPosition)? {
RequestParameters::ByPosition(mut it) => {
while Self::N_REQUIRED_PARAMS < it.len() {
match it.last() {
Some(last) if last.is_null() => it.pop(),
_ => break,
};
}
serde_json::Value::Array(it)
}
RequestParameters::ByName(it) => serde_json::Value::Object(it),
},
)
}
fn request_with_alias(
params: Self::Params,
use_alias: bool,
) -> anyhow::Result<crate::rpc::Request<Self::Ok>> {
let params = Self::request_params(params)?;
let name = if use_alias {
Self::NAME_ALIAS.context("alias is None")?
} else {
Self::NAME
};
Ok(crate::rpc::Request {
method_name: name,
params,
result_type: std::marker::PhantomData,
api_paths: Self::API_PATHS,
timeout: *crate::rpc::DEFAULT_REQUEST_TIMEOUT,
})
}
fn call_raw(
client: &crate::rpc::client::Client,
params: Self::Params,
) -> impl Future<Output = Result<<Self::Ok as HasLotusJson>::LotusJson, jsonrpsee::core::ClientError>>
{
async {
let json = client.call(Self::request(params)?.map_ty()).await?;
Ok(serde_json::from_value(json)?)
}
}
fn call(
client: &crate::rpc::client::Client,
params: Self::Params,
) -> impl Future<Output = Result<Self::Ok, jsonrpsee::core::ClientError>> {
async {
Self::call_raw(client, params)
.await
.map(Self::Ok::from_lotus_json)
}
}
}
impl<const ARITY: usize, T> RpcMethodExt<ARITY> for T where T: RpcMethod<ARITY> {}
pub trait Params<const ARITY: usize>: HasLotusJson {
fn schemas(gen: &mut SchemaGenerator) -> [(Schema, bool); ARITY];
fn parse(
raw: Option<RequestParameters>,
names: [&str; ARITY],
calling_convention: ParamStructure,
n_required: usize,
) -> Result<Self, Error>
where
Self: Sized;
fn unparse(self) -> Result<[serde_json::Value; ARITY], serde_json::Error> {
match self.into_lotus_json_value() {
Ok(serde_json::Value::Array(args)) => match args.try_into() {
Ok(it) => Ok(it),
Err(_) => Err(serde_json::Error::custom("ARITY mismatch")),
},
Ok(serde_json::Value::Null) if ARITY == 0 => {
Ok(std::array::from_fn(|_ix| Default::default()))
}
Ok(it) => Err(serde_json::Error::invalid_type(
unexpected(&it),
&"a Vec with an item for each argument",
)),
Err(e) => Err(e),
}
}
}
fn unexpected(v: &serde_json::Value) -> Unexpected<'_> {
match v {
serde_json::Value::Null => Unexpected::Unit,
serde_json::Value::Bool(it) => Unexpected::Bool(*it),
serde_json::Value::Number(it) => match (it.as_f64(), it.as_i64(), it.as_u64()) {
(None, None, None) => Unexpected::Other("Number"),
(Some(it), _, _) => Unexpected::Float(it),
(_, Some(it), _) => Unexpected::Signed(it),
(_, _, Some(it)) => Unexpected::Unsigned(it),
},
serde_json::Value::String(it) => Unexpected::Str(it),
serde_json::Value::Array(_) => Unexpected::Seq,
serde_json::Value::Object(_) => Unexpected::Map,
}
}
macro_rules! do_impls {
($arity:literal $(, $arg:ident)* $(,)?) => {
const _: () = {
let _assert: [&str; $arity] = [$(stringify!($arg)),*];
};
impl<$($arg),*> Params<$arity> for ($($arg,)*)
where
$($arg: HasLotusJson + Clone, <$arg as HasLotusJson>::LotusJson: JsonSchema, )*
{
fn parse(
raw: Option<RequestParameters>,
arg_names: [&str; $arity],
calling_convention: ParamStructure,
n_required: usize,
) -> Result<Self, Error> {
let mut _parser = Parser::new(raw, &arg_names, calling_convention, n_required)?;
Ok(($(_parser.parse::<crate::lotus_json::LotusJson<$arg>>()?.into_inner(),)*))
}
fn schemas(_gen: &mut SchemaGenerator) -> [(Schema, bool); $arity] {
[$((_gen.subschema_for::<$arg::LotusJson>(), $arg::LotusJson::optional())),*]
}
}
};
}
do_impls!(0);
do_impls!(1, T0);
do_impls!(2, T0, T1);
do_impls!(3, T0, T1, T2);
do_impls!(4, T0, T1, T2, T3);
pub enum ConcreteCallingConvention {
ByPosition,
#[allow(unused)] ByName,
}