Skip to content
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
49 changes: 46 additions & 3 deletions core-primitives/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,69 @@ impl RpcReturnValue {
}
}

#[derive(Clone, Encode, Decode, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Id {
Number(u32),
Text(String),
}

#[derive(Clone, Encode, Decode, Debug, Serialize, Deserialize)]
pub struct RpcResponse {
pub jsonrpc: String,
pub result: String, // hex encoded RpcReturnValue
pub id: u32,
pub id: Id,
}

#[derive(Clone, Encode, Decode, Serialize, Deserialize)]
pub struct RpcRequest {
pub jsonrpc: String,
pub method: String,
pub params: Vec<String>,
pub id: i32,
pub id: Id,
}

impl RpcRequest {
pub fn compose_jsonrpc_call(
method: String,
params: Vec<String>,
) -> Result<String, serde_json::Error> {
serde_json::to_string(&RpcRequest { jsonrpc: "2.0".to_owned(), method, params, id: 1 })
serde_json::to_string(&RpcRequest {
jsonrpc: "2.0".to_owned(),
method,
params,
id: Id::Number(1),
})
}
}

#[cfg(test)]
pub mod tests {
use crate::Id;

#[test]
pub fn deserialize_string_id() {
let id: Id = serde_json::from_str(r#""1""#).unwrap();
assert!(matches!(id, Id::Text(t) if t == "1"))
}

#[test]
pub fn deserialize_number_id() {
let id: Id = serde_json::from_str(r#"1"#).unwrap();
assert!(matches!(id, Id::Number(t) if t == 1))
}

#[test]
pub fn serialize_string_id() {
let id = Id::Text("1".to_string());
let serialized = serde_json::to_string(&id).unwrap();
assert_eq!(serialized, r#""1""#)
}

#[test]
pub fn serialize_number_id() {
let id = Id::Number(1);
let serialized = serde_json::to_string(&id).unwrap();
assert_eq!(serialized, r#"1"#)
}
}
4 changes: 2 additions & 2 deletions core/direct-rpc-server/src/builders/rpc_response_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

use crate::builders::rpc_return_value_builder::RpcReturnValueBuilder;
use itp_rpc::{RpcResponse, RpcReturnValue};
use itp_rpc::{Id, RpcResponse, RpcReturnValue};
use itp_utils::ToHexPrefixed;

/// builder pattern for RpcResponse
Expand Down Expand Up @@ -52,7 +52,7 @@ impl RpcResponseBuilder {

#[allow(unused)]
pub fn build(self) -> RpcResponse {
let id = self.maybe_id.unwrap_or(1u32);
let id = Id::Number(self.maybe_id.unwrap_or(1u32));
let json_rpc = self.maybe_json_rpc.unwrap_or(String::from("json_rpc"));
let result = self
.maybe_result
Expand Down
3 changes: 2 additions & 1 deletion core/direct-rpc-server/src/rpc_connection_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ where
#[cfg(test)]
pub mod tests {
use super::*;
use itp_rpc::Id;

type TestRegistry = ConnectionRegistry<String, u64>;

Expand Down Expand Up @@ -119,6 +120,6 @@ pub mod tests {
}

fn dummy_rpc_response() -> RpcResponse {
RpcResponse { jsonrpc: String::new(), result: Default::default(), id: 1u32 }
RpcResponse { jsonrpc: String::new(), result: Default::default(), id: Id::Number(1u32) }
}
}
8 changes: 6 additions & 2 deletions core/direct-rpc-server/src/rpc_watch_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ pub mod tests {
rpc_response_builder::RpcResponseBuilder, rpc_return_value_builder::RpcReturnValueBuilder,
};
use codec::Encode;
use itp_rpc::Id;
use itp_types::TrustedOperationStatus;

#[test]
fn invalid_rpc_response_returns_error() {
let watch_extractor = RpcWatchExtractor::<String>::new();
let rpc_response =
RpcResponse { id: 1u32, jsonrpc: String::from("json"), result: "hello".to_string() };
let rpc_response = RpcResponse {
id: Id::Number(1u32),
jsonrpc: String::from("json"),
result: "hello".to_string(),
};

assert!(watch_extractor.must_be_watched(&rpc_response).is_err());
}
Expand Down
9 changes: 7 additions & 2 deletions core/rpc-server/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

use itp_enclave_api::{direct_request::DirectRequest, EnclaveResult};
use itp_rpc::RpcResponse;
use itp_rpc::{Id, RpcResponse};
use itp_utils::ToHexPrefixed;
use its_primitives::{
traits::ShardIdentifierFor,
Expand All @@ -29,7 +29,12 @@ pub struct TestEnclave;

impl DirectRequest for TestEnclave {
fn rpc(&self, _request: Vec<u8>) -> EnclaveResult<Vec<u8>> {
Ok(RpcResponse { jsonrpc: "mock_response".into(), result: "null".to_hex(), id: 1 }.encode())
Ok(RpcResponse {
jsonrpc: "mock_response".into(),
result: "null".to_hex(),
id: Id::Number(1),
}
.encode())
}
}

Expand Down