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/rpc request serde #1539

Merged
merged 5 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ version = "0.14"
features = [ "http1", "runtime", "server", "tcp" ]

[dependencies.json-rpc-types]
version = "1.0"
version = "1.0.2"

[dependencies.jsonrpc-core]
version = "18"
Expand Down
34 changes: 23 additions & 11 deletions src/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,30 @@ async fn handle_rpc<N: Network, E: Environment>(
};

// Deserialize the JSON-RPC request.
let req: jrt::Request<Params> = match serde_json::from_slice(&data) {
Ok(req) => req,
Err(_) => {
let resp = jrt::Response::<(), ()>::error(
jrt::Version::V2,
jrt::Error::with_custom_msg(jrt::ErrorCode::ParseError, "Couldn't parse the RPC body"),
None,
);
let body = serde_json::to_vec(&resp).unwrap_or_default();

return Ok(hyper::Response::new(body.into()));
let req = if let Ok(req_data) = serde_json::from_slice::<serde_json::Value>(&data) {
match serde_json::from_value::<jrt::Request<Params>>(req_data) {
Ok(req) => req,
Err(e) => {
debug!("Request Parsing Error: {:?}", e);
let resp = jrt::Response::<(), ()>::error(
jrt::Version::V2,
jrt::Error::with_custom_msg(jrt::ErrorCode::ParseError, "Couldn't parse the RPC body"),
None,
);
let body = serde_json::to_vec(&resp).unwrap_or_default();

return Ok(hyper::Response::new(body.into()));
}
}
} else {
let resp = jrt::Response::<(), ()>::error(
jrt::Version::V2,
jrt::Error::with_custom_msg(jrt::ErrorCode::ParseError, "Deserialize to Value failed"),
None,
);
let body = serde_json::to_vec(&resp).unwrap_or_default();

return Ok(hyper::Response::new(body.into()));
};

debug!("Received '{}' RPC request from {}: {:?}", &*req.method, caller, headers);
Expand Down