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

[types]: ID type instead of serde_json::RawValue #325

Merged
merged 19 commits into from
May 19, 2021

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented May 18, 2021

Similar should be done on Params and JsonRpcNotifParams to work properly but this PR is large enough as it is.

Further info see #292 (comment)

@@ -157,16 +159,17 @@ impl From<SubscriptionId> for JsonValue {
#[derive(Debug, PartialEq, Clone, Hash, Eq, Deserialize, Serialize)]
Copy link
Member Author

@niklasad1 niklasad1 May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clone should be cheap as deserialize just borrows the String AFAIU.

Then in the client code we just use Id::Number for now.

Thus, should never allocate in server at least?!

@niklasad1 niklasad1 changed the title [types]: typed ID instead of serde_json::RawValue [types]: ID type instead of serde_json::RawValue May 18, 2021
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good cleanup here, and good riddance of those *Alloc types. 👍

@@ -141,12 +141,12 @@ async fn background_task(
let execute = move |tx: &MethodSink, req: JsonRpcRequest| {
if let Some(method) = methods.get(&*req.method) {
let params = RpcParams::new(req.params.map(|params| params.get()));
if let Err(err) = (method)(req.id, params, &tx, conn_id) {
if let Err(err) = (method)(req.id.clone(), params, &tx, conn_id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these clones are cheap then you say?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes 🙏

let err: JsonRpcErrorAlloc = serde_json::from_slice(&body).map_err(Error::ParseError)?;
return Err(Error::Request(err));
let err: JsonRpcError = serde_json::from_slice(&body).map_err(Error::ParseError)?;
return Err(Error::Request(err.to_string()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I changed the error type to a String in Error::Request to get rid of the alloc type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I noticed that. I don't think there much else we can do right?

@@ -211,23 +212,27 @@ impl Server {
// Our [issue](https://github.com/paritytech/jsonrpsee/issues/296).
if let Ok(req) = serde_json::from_slice::<JsonRpcRequest>(&body) {
execute(&tx, req);
} else if let Ok(_req) = serde_json::from_slice::<JsonRpcNotification>(&body) {
return Ok::<_, HyperError>(response::ok_response("".into()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send empty response to notification,

currently we don't care if the method is registered or not (that API is not exposed)

}
} else if let Ok(_batch) = serde_json::from_slice::<Vec<JsonRpcNotification>>(&body) {
return Ok::<_, HyperError>(response::ok_response("".into()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send empty response to batch notification,

currently we don't care if the method is registered or not (that API is not exposed)

/// Method
pub method: &'a str,
/// Params.
pub params: JsonRpcNotificationParams<'a>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In another PR; merge JsonRpcNotificationParams and JsonRpcNotificationParamsNotification

pub struct JsonRpcNotification<'a> {
/// JSON-RPC version.
pub jsonrpc: TwoPointZero,
/// Name of the method to be invoked.
pub method: &'a str,
/// Parameter values of the request.
pub params: JsonRpcNotificationParams<'a>,
#[serde(borrow)]
pub params: Option<&'a RawValue>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In another PR, hopefully merge JsonRpcParams/RpcParams and replace RawValue here,

That's the reason why some de-serialize tests still fail

@niklasad1 niklasad1 marked this pull request as ready for review May 18, 2021 19:22
@niklasad1 niklasad1 requested a review from TarikGul May 19, 2021 12:25
let response = http_request(req.into(), uri).await.unwrap();
assert_eq!(response.status, StatusCode::OK);
// Note: this is *not* according to spec. Response should be the empty string, `""`.
assert_eq!(response.body, r#"[{"jsonrpc":"2.0","result":"","id":null},{"jsonrpc":"2.0","result":"","id":null}]"#);
// Note: on HTTP we ack the notification with an empty response.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does ack mean here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK - acknowledge, maybe it's a bad term ^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh makes sense, yea I wouldn't have been able to deduce that. But now that I know what it means I like it. For the general user, it would probably make sense to write out the whole word though in this case.

Copy link
Member

@TarikGul TarikGul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, After a brief explanation from @niklasad1 I was able to follow along the type changes, and understand the direction of the PR. 👍

@niklasad1 niklasad1 merged commit 4e95f43 into master May 19, 2021
@niklasad1 niklasad1 deleted the na-jsonrpc-types-id-and-params branch May 19, 2021 15:20
@dvdplm dvdplm mentioned this pull request Jun 15, 2021
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants