Skip to content

Commit

Permalink
Improve forest-tool shed openrpc output (#4943)
Browse files Browse the repository at this point in the history
  • Loading branch information
elmattic authored Oct 28, 2024
1 parent 454db3b commit 9f76c45
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
- [#4706](https://github.com/ChainSafe/forest/issues/4706) Add support for the
`Filecoin.EthSendRawTransaction` RPC method.

- [#4943](https://github.com/ChainSafe/forest/pull/4943) Add generation of
method aliases for `forest-tool shed openrpc` subcommand and sort all methods
in lexicographic order.

### Changed

### Removed
Expand Down
36 changes: 28 additions & 8 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,36 @@ pub fn openrpc(path: ApiPath, include: Option<&[&str]>) -> openrpc_types::OpenRP
if <$ty>::API_PATHS.contains(path) {
match include {
Some(include) => match include.contains(&<$ty>::NAME) {
true => methods.push(openrpc_types::ReferenceOr::Item(<$ty>::openrpc(
&mut gen,
ParamStructure::ByPosition,
))),
true => {
methods.push(openrpc_types::ReferenceOr::Item(<$ty>::openrpc(
&mut gen,
ParamStructure::ByPosition,
&<$ty>::NAME,
)));
if let Some(alias) = &<$ty>::NAME_ALIAS {
methods.push(openrpc_types::ReferenceOr::Item(<$ty>::openrpc(
&mut gen,
ParamStructure::ByPosition,
&alias,
)));
}
}
false => {}
},
None => methods.push(openrpc_types::ReferenceOr::Item(<$ty>::openrpc(
&mut gen,
ParamStructure::ByPosition,
))),
None => {
methods.push(openrpc_types::ReferenceOr::Item(<$ty>::openrpc(
&mut gen,
ParamStructure::ByPosition,
&<$ty>::NAME,
)));
if let Some(alias) = &<$ty>::NAME_ALIAS {
methods.push(openrpc_types::ReferenceOr::Item(<$ty>::openrpc(
&mut gen,
ParamStructure::ByPosition,
&alias,
)));
}
}
}
}
};
Expand Down
14 changes: 8 additions & 6 deletions src/rpc/reflect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,22 @@ pub trait RpcMethodExt<const ARITY: usize>: RpcMethod<ARITY> {
}
}
/// Generate a full `OpenRPC` method definition for this endpoint.
fn openrpc<'de>(gen: &mut SchemaGenerator, calling_convention: ParamStructure) -> Method
fn openrpc<'de>(
gen: &mut SchemaGenerator,
calling_convention: ParamStructure,
method_name: &'static str,
) -> Method
where
<Self::Ok as HasLotusJson>::LotusJson: JsonSchema + Deserialize<'de>,
{
Method {
name: String::from(Self::NAME),
name: String::from(method_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
);
panic!("Optional parameter at position {pos} should be of an optional type. method={method_name}, param_name={name}");
}
ReferenceOr::Item(ContentDescriptor {
name: String::from(name),
Expand All @@ -175,7 +177,7 @@ pub trait RpcMethodExt<const ARITY: usize>: RpcMethod<ARITY> {
.collect(),
param_structure: Some(calling_convention),
result: Some(ReferenceOr::Item(ContentDescriptor {
name: format!("{}.Result", Self::NAME),
name: format!("{}.Result", method_name),
schema: gen.subschema_for::<<Self::Ok as HasLotusJson>::LotusJson>(),
required: Some(!<Self::Ok as HasLotusJson>::LotusJson::optional()),
..Default::default()
Expand Down
24 changes: 14 additions & 10 deletions src/tool/subcommands/shed_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use anyhow::Context as _;
use base64::{prelude::BASE64_STANDARD, Engine};
use clap::Subcommand;
use futures::{StreamExt as _, TryFutureExt as _, TryStreamExt as _};
use openrpc_types::ReferenceOr;

#[derive(Subcommand)]
pub enum ShedCommands {
Expand Down Expand Up @@ -123,17 +124,20 @@ impl ShedCommands {
}
ShedCommands::Openrpc { include, path } => {
let include = include.iter().map(String::as_str).collect::<Vec<_>>();
println!(
"{}",
serde_json::to_string_pretty(&crate::rpc::openrpc(
path,
match include.is_empty() {
true => None,
false => Some(&include),
}
))
.unwrap()

let mut openrpc_doc = crate::rpc::openrpc(
path,
match include.is_empty() {
true => None,
false => Some(&include),
},
);
openrpc_doc.methods.sort_by(|a, b| match (a, b) {
(ReferenceOr::Item(a), ReferenceOr::Item(b)) => a.name.cmp(&b.name),
_ => std::cmp::Ordering::Equal,
});

println!("{}", serde_json::to_string_pretty(&openrpc_doc).unwrap());
}
}
Ok(())
Expand Down

0 comments on commit 9f76c45

Please sign in to comment.