diff --git a/crates/topos-core/src/api/graphql/certificate.rs b/crates/topos-core/src/api/graphql/certificate.rs index c6bfe10c2..665b23445 100644 --- a/crates/topos-core/src/api/graphql/certificate.rs +++ b/crates/topos-core/src/api/graphql/certificate.rs @@ -1,18 +1,18 @@ -use async_graphql::{InputObject, SimpleObject}; +use async_graphql::{InputObject, NewType, Object, SimpleObject}; use serde::{Deserialize, Serialize}; +use crate::uci; + use super::subnet::SubnetId; -#[derive(Debug, Default, Serialize, Deserialize, InputObject)] -pub struct CertificateId { - pub value: String, -} +#[derive(Serialize, Deserialize, Debug, NewType)] +pub struct CertificateId(String); -#[derive(Debug, Default, Serialize, Deserialize, SimpleObject)] +#[derive(Serialize, Deserialize, Debug, SimpleObject)] #[serde(rename_all = "camelCase")] pub struct Certificate { - pub id: String, - pub prev_id: String, + pub id: CertificateId, + pub prev_id: CertificateId, pub proof: String, pub signature: String, pub source_subnet_id: SubnetId, @@ -26,8 +26,8 @@ pub struct Certificate { impl From<&crate::uci::Certificate> for Certificate { fn from(uci_cert: &crate::uci::Certificate) -> Self { Self { - id: uci_cert.id.to_string(), - prev_id: uci_cert.prev_id.to_string(), + id: CertificateId(uci_cert.id.to_string()), + prev_id: CertificateId(uci_cert.prev_id.to_string()), proof: hex::encode(&uci_cert.proof), signature: hex::encode(&uci_cert.signature), source_subnet_id: SubnetId::from(&uci_cert.source_subnet_id), @@ -40,10 +40,10 @@ impl From<&crate::uci::Certificate> for Certificate { } } -impl From<&crate::uci::SubnetId> for SubnetId { - fn from(uci_id: &crate::uci::SubnetId) -> Self { - Self { - value: uci_id.to_string(), - } +impl TryFrom for crate::uci::CertificateId { + type Error = uci::Error; + + fn try_from(value: CertificateId) -> Result { + crate::uci::CertificateId::try_from(value.0.as_bytes()) } } diff --git a/crates/topos-core/src/api/graphql/checkpoint.rs b/crates/topos-core/src/api/graphql/checkpoint.rs index 74adf1e22..519e06afc 100644 --- a/crates/topos-core/src/api/graphql/checkpoint.rs +++ b/crates/topos-core/src/api/graphql/checkpoint.rs @@ -3,14 +3,14 @@ use serde::{Deserialize, Serialize}; use super::{certificate::CertificateId, subnet::SubnetId}; -#[derive(Debug, Default, Serialize, Deserialize, InputObject)] +#[derive(InputObject)] pub struct SourceStreamPosition { pub source_subnet_id: SubnetId, pub position: u64, pub certificate_id: Option, } -#[derive(Debug, Default, Serialize, Deserialize, InputObject)] +#[derive(InputObject)] pub struct SourceCheckpoint { pub source_subnet_ids: Vec, pub positions: Vec, diff --git a/crates/topos-core/src/api/graphql/subnet.rs b/crates/topos-core/src/api/graphql/subnet.rs index 021b26d9b..ed90821a1 100644 --- a/crates/topos-core/src/api/graphql/subnet.rs +++ b/crates/topos-core/src/api/graphql/subnet.rs @@ -1,32 +1,33 @@ -use async_graphql::{InputObject, SimpleObject}; +use async_graphql::{InputObject, NewType, SimpleObject}; use serde::{Deserialize, Serialize}; use std::str::FromStr; use tracing::{error, warn}; use super::errors::GraphQLServerError; -#[derive( - Clone, Debug, Default, Serialize, Deserialize, SimpleObject, InputObject, PartialEq, Eq, -)] -#[graphql(input_name = "SubnetIdInput")] -pub struct SubnetId { - pub value: String, -} +#[derive(Clone, Debug, Serialize, Deserialize, NewType, PartialEq, Eq)] +pub struct SubnetId(pub(crate) String); impl TryFrom<&SubnetId> for crate::uci::SubnetId { type Error = GraphQLServerError; fn try_from(value: &SubnetId) -> Result { - Self::from_str(value.value.as_str()).map_err(|e| { + Self::from_str(value.0.as_str()).map_err(|e| { error!("Failed to convert SubnetId from GraphQL input {e:?}"); GraphQLServerError::ParseDataConnector }) } } +impl From<&crate::uci::SubnetId> for SubnetId { + fn from(uci_id: &crate::uci::SubnetId) -> Self { + Self(uci_id.to_string()) + } +} + impl PartialEq for SubnetId { fn eq(&self, other: &crate::uci::SubnetId) -> bool { - if let Ok(current) = crate::uci::SubnetId::from_str(&self.value) { + if let Ok(current) = crate::uci::SubnetId::from_str(&self.0) { other.as_array().eq(current.as_array()) } else { warn!("Unexpected parsing error for subnet id during comparaison"); diff --git a/crates/topos-tce-api/src/graphql/query.rs b/crates/topos-tce-api/src/graphql/query.rs index b0c5d493d..7ecc77101 100644 --- a/crates/topos-tce-api/src/graphql/query.rs +++ b/crates/topos-tce-api/src/graphql/query.rs @@ -81,8 +81,6 @@ impl CertificateQuery for QueryRoot { store .get_certificate( &certificate_id - .value - .as_bytes() .try_into() .map_err(|_| GraphQLServerError::ParseCertificateId)?, ) @@ -138,16 +136,13 @@ impl SubscriptionRoot { })? .map_err(|e| GraphQLServerError::TransientStream(e.to_string()))?; - let filter: Option<(FilterIs, topos_core::uci::SubnetId)> = - filter - .map(|value| match value { - SubnetFilter::Target(id) => topos_core::uci::SubnetId::from_str(&id.value) - .map(|v| (FilterIs::Target, v)), - SubnetFilter::Source(id) => topos_core::uci::SubnetId::from_str(&id.value) - .map(|v| (FilterIs::Source, v)), - }) - .map_or(Ok(None), |v| v.map(Some)) - .map_err(|_| GraphQLServerError::ParseSubnetId)?; + let filter: Option<(FilterIs, topos_core::uci::SubnetId)> = filter + .map(|value| match value { + SubnetFilter::Target(ref id) => id.try_into().map(|v| (FilterIs::Target, v)), + SubnetFilter::Source(ref id) => id.try_into().map(|v| (FilterIs::Source, v)), + }) + .map_or(Ok(None), |v| v.map(Some)) + .map_err(|_| GraphQLServerError::ParseSubnetId)?; Ok(stream .filter(move |c| { diff --git a/crates/topos-tce-api/src/graphql/tests.rs b/crates/topos-tce-api/src/graphql/tests.rs index 079f11049..847ed3b4f 100644 --- a/crates/topos-tce-api/src/graphql/tests.rs +++ b/crates/topos-tce-api/src/graphql/tests.rs @@ -113,11 +113,9 @@ async fn open_watch_certificate_delivered() { prevId proof signature - sourceSubnetId { value } + sourceSubnetId stateRoot - targetSubnets { - value - } + targetSubnets txRootHash receiptsRootHash verifier diff --git a/crates/topos-tce-api/tests/runtime.rs b/crates/topos-tce-api/tests/runtime.rs index 0357e8811..af4aab73a 100644 --- a/crates/topos-tce-api/tests/runtime.rs +++ b/crates/topos-tce-api/tests/runtime.rs @@ -558,11 +558,11 @@ async fn can_query_graphql_endpoint_for_certificates( certificates( fromSourceCheckpoint: {{ sourceSubnetIds: [ - {{ value: "{SOURCE_SUBNET_ID_1}" }} + "{SOURCE_SUBNET_ID_1}" ], positions: [ {{ - sourceSubnetId: {{ value: "{SOURCE_SUBNET_ID_1}" }}, + sourceSubnetId:"{SOURCE_SUBNET_ID_1}", position: 0, }} ] @@ -573,11 +573,9 @@ async fn can_query_graphql_endpoint_for_certificates( prevId proof signature - sourceSubnetId {{ value }} + sourceSubnetId stateRoot - targetSubnets {{ - value - }} + targetSubnets txRootHash receiptsRootHash verifier