Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve error handling in DelayedResolver::into_rpc_module #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions crates/node/src/delayed_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,26 @@ impl DelayedResolver {
}

/// Converts this type into a new [`RpcModule`] that delegates the get payload call.
pub fn into_rpc_module(self) -> RpcModule<()> {
///
/// # Errors
/// Returns error if failed to register the RPC method.
pub fn into_rpc_module(self) -> Result<RpcModule<()>, jsonrpsee::core::Error> {
let mut module = RpcModule::new(());
module
.register_async_method(GET_PAYLOAD_V3, move |params, _ctx, _| {
let value = self.clone();
async move {
value.call(params).await.map_err(|err| match err {
MethodsError::JsonRpc(err) => err,
err => ErrorObject::owned(
INVALID_PARAMS_CODE,
format!("invalid payload call: {:?}", err),
None::<()>,
),
})
}
})
.unwrap();
module.register_async_method(GET_PAYLOAD_V3, move |params, _ctx, _| {
let value = self.clone();
async move {
value.call(params).await.map_err(|err| match err {
MethodsError::JsonRpc(err) => err,
err => ErrorObject::owned(
INVALID_PARAMS_CODE,
format!("invalid payload call: {:?}", err),
None::<()>,
),
})
}
})?;

module
Ok(module)
}
}

Expand Down Expand Up @@ -125,22 +126,23 @@ mod tests {
}

#[tokio::test]
async fn test_delayed_forward() {
async fn test_delayed_forward() -> Result<(), Box<dyn std::error::Error>> {
use jsonrpsee::{core::RpcResult, RpcModule};

let mut module = RpcModule::new(());
module
.register_method::<RpcResult<Payload>, _>(GET_PAYLOAD_V3, |params, _, _| {
params.one::<PayloadId>()?;
Ok(Payload::default())
})
.unwrap();
})?;

let id = PayloadId::default();

let _echo: Payload = module.call(GET_PAYLOAD_V3, [id]).await.unwrap();
let _echo: Payload = module.call(GET_PAYLOAD_V3, [id]).await?;

let delayer = DelayedResolver::new(module, MAX_DELAY_INTO_SLOT).into_rpc_module();
let _echo: Payload = delayer.call(GET_PAYLOAD_V3, [id]).await.unwrap();
let delayer = DelayedResolver::new(module, MAX_DELAY_INTO_SLOT).into_rpc_module()?;
let _echo: Payload = delayer.call(GET_PAYLOAD_V3, [id]).await?;

Ok(())
}
}