Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
chore: remove/refactor some struct in topos-core
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Paitrault <[email protected]>
  • Loading branch information
Freyskeyd committed Jan 22, 2024
1 parent 2838c0a commit 7527f20
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 49 deletions.
30 changes: 15 additions & 15 deletions crates/topos-core/src/api/graphql/certificate.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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),
Expand All @@ -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<CertificateId> for crate::uci::CertificateId {
type Error = uci::Error;

fn try_from(value: CertificateId) -> Result<Self, Self::Error> {
crate::uci::CertificateId::try_from(value.0.as_bytes())
}
}
4 changes: 2 additions & 2 deletions crates/topos-core/src/api/graphql/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CertificateId>,
}

#[derive(Debug, Default, Serialize, Deserialize, InputObject)]
#[derive(InputObject)]
pub struct SourceCheckpoint {
pub source_subnet_ids: Vec<SubnetId>,
pub positions: Vec<SourceStreamPosition>,
Expand Down
21 changes: 11 additions & 10 deletions crates/topos-core/src/api/graphql/subnet.rs
Original file line number Diff line number Diff line change
@@ -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, Self::Error> {
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<crate::uci::SubnetId> 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");
Expand Down
19 changes: 7 additions & 12 deletions crates/topos-tce-api/src/graphql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ impl CertificateQuery for QueryRoot {
store
.get_certificate(
&certificate_id
.value
.as_bytes()
.try_into()
.map_err(|_| GraphQLServerError::ParseCertificateId)?,
)
Expand Down Expand Up @@ -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| {
Expand Down
6 changes: 2 additions & 4 deletions crates/topos-tce-api/src/graphql/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ async fn open_watch_certificate_delivered() {
prevId
proof
signature
sourceSubnetId { value }
sourceSubnetId
stateRoot
targetSubnets {
value
}
targetSubnets
txRootHash
receiptsRootHash
verifier
Expand Down
10 changes: 4 additions & 6 deletions crates/topos-tce-api/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}}
]
Expand All @@ -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
Expand Down

0 comments on commit 7527f20

Please sign in to comment.