Skip to content

Commit

Permalink
Flatten request type to avoid option on message field.
Browse files Browse the repository at this point in the history
  • Loading branch information
aterentic-ethernal committed Sep 13, 2023
1 parent b91b344 commit 6f42b7e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/api/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,8 @@ mod tests {
Uuid::try_parse(uuid).unwrap()
}

#[test_case(r#"{"type":"submit","request_id":"16b24956-2e01-4ba8-bad5-456c561c87d7","message":{"data":""}}"#, None, Some("16b24956-2e01-4ba8-bad5-456c561c87d7"), "Submit feature is not configured" ; "No submitter")]
#[test_case(r#"{"type":"submit","request_id":"16b24956-2e01-4ba8-bad5-456c561c87d7","message":{"data":""}}"#, None, Some("16b24956-2e01-4ba8-bad5-456c561c87d7"), "Submit is not configured" ; "No submitter")]
#[test_case(r#"{"type":"submit","request_id":"537a3c39-c029-4283-9612-17465bf7cfd1","message":{"data":"dHJhbnNhY3Rpb24K"}}"#, Some(false), Some("537a3c39-c029-4283-9612-17465bf7cfd1"), "Signer is not configured" ; "No signer")]
#[test_case(r#"{"type":"submit","request_id":"533e9c37-2bea-4fd1-9365-c77a0dae9211","message":null}"#, Some(false), Some("533e9c37-2bea-4fd1-9365-c77a0dae9211"), "Message is empty" ; "Empty message")]
#[test_case(r#"{"type":"submit","request_id":"36bc1f28-e093-422f-964b-1cb1b3882baf","message":{"extrinsic":""}}"#, Some(false), Some("36bc1f28-e093-422f-964b-1cb1b3882baf"), "Transaction is empty" ; "Empty extrinsic")]
#[test_case(r#"{"type":"submit","request_id":"cc60b2f3-d9ff-4c73-9632-d21d07f7b620","message":{"data":""}}"#, Some(true), Some("cc60b2f3-d9ff-4c73-9632-d21d07f7b620"), "Transaction is empty" ; "Empty data")]
#[test_case(r#"{"type":"submit","request_id":"9181df86-22f0-42a1-a965-60adb9fc6bdc","message":{"extrinsic":"bad"}}"#, Some(false), None, "Failed to parse request" ; "Bad extrinsic")]
Expand Down
11 changes: 5 additions & 6 deletions src/api/v2/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,18 @@ impl Reply for SubscriptionId {
}

#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RequestType {
#[serde(tag = "type", content = "message", rename_all = "kebab-case")]
pub enum Payload {
Version,
Status,
Submit,
Submit(Transaction),
}

#[derive(Deserialize)]
pub struct Request {
#[serde(rename = "type")]
pub request_type: RequestType,
#[serde(flatten)]
pub payload: Payload,
pub request_id: Uuid,
pub message: Option<Transaction>,
}

#[derive(Serialize, Deserialize)]
Expand Down
19 changes: 7 additions & 12 deletions src/api/v2/ws.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
transactions,
types::{Clients, Request, RequestType, Response, Status, Transaction, Version, WsResponse},
types::{Clients, Payload, Request, Response, Status, Transaction, Version, WsResponse},
};
use crate::{
api::v2::types::Error,
Expand Down Expand Up @@ -106,21 +106,16 @@ async fn handle_request(
state: Arc<Mutex<State>>,
) -> anyhow::Result<WsResponse> {
let request_id = request.request_id;
match request.request_type {
RequestType::Version => Ok(Response::new(request_id, version.clone()).into()),
RequestType::Status => {
let state = state.lock().expect("Cannot acquire state lock");

match request.payload {
Payload::Version => Ok(Response::new(request_id, version.clone()).into()),
Payload::Status => {
let state = state.lock().expect("State lock can be acquired");
let status = Status::new(config, node, &state);
Ok(Response::new(request_id, status).into())
},
RequestType::Submit => {
Payload::Submit(transaction) => {
let Some(submitter) = submitter else {
let message = "Submit feature is not configured.";
return Ok(Error::bad_request(request_id, message).into());
};
let Some(transaction) = request.message else {
return Ok(Error::bad_request(request_id, "Message is empty.").into());
return Ok(Error::bad_request(request_id, "Submit is not configured.").into());
};
if transaction.is_empty() {
return Ok(Error::bad_request(request_id, "Transaction is empty.").into());
Expand Down

0 comments on commit 6f42b7e

Please sign in to comment.