From 69462e3d4f602557194566126ec9d4df51de42ab Mon Sep 17 00:00:00 2001 From: Uacias Date: Tue, 6 Aug 2024 12:24:04 +0200 Subject: [PATCH 1/7] Add hash to features. --- crates/starknet-types-core/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/starknet-types-core/Cargo.toml b/crates/starknet-types-core/Cargo.toml index 8ebcf93..054ab85 100644 --- a/crates/starknet-types-core/Cargo.toml +++ b/crates/starknet-types-core/Cargo.toml @@ -26,7 +26,7 @@ parity-scale-codec = { version = "3.6.9", default-features = false, optional = t lazy_static = { version = "1.4.0", default-features = false, optional = true } [features] -default = ["std", "serde", "curve", "num-traits"] +default = ["std", "serde", "curve", "num-traits", "hash"] std = [ "lambdaworks-math/std", "num-traits/std", @@ -57,4 +57,3 @@ lazy_static = { version = "1.4.0", default-features = false } [[bench]] name = "criterion_hashes" harness = false - From 9fbfc9908fb6455558b089e3bf66c510ba03fca3 Mon Sep 17 00:00:00 2001 From: Uacias Date: Tue, 6 Aug 2024 12:42:45 +0200 Subject: [PATCH 2/7] Adding blockid, query, syncing status files for certain versions. --- .../starknet-types-rpc/src/custom/block_id.rs | 2 +- crates/starknet-types-rpc/src/custom/query.rs | 2 +- .../src/custom/syncing_status.rs | 2 +- crates/starknet-types-rpc/src/lib.rs | 2 + .../starknet-types-rpc/src/v0_5_0/block_id.rs | 129 ++++++++++++++++++ crates/starknet-types-rpc/src/v0_5_0/mod.rs | 11 +- crates/starknet-types-rpc/src/v0_5_0/query.rs | 48 +++++++ .../src/v0_5_0/starknet_api_openrpc.rs | 10 +- .../src/v0_5_0/syncing_status.rs | 88 ++++++++++++ .../starknet-types-rpc/src/v0_6_0/block_id.rs | 129 ++++++++++++++++++ crates/starknet-types-rpc/src/v0_6_0/mod.rs | 11 +- crates/starknet-types-rpc/src/v0_6_0/query.rs | 48 +++++++ .../src/v0_6_0/syncing_status.rs | 88 ++++++++++++ 13 files changed, 554 insertions(+), 16 deletions(-) create mode 100644 crates/starknet-types-rpc/src/v0_5_0/block_id.rs create mode 100644 crates/starknet-types-rpc/src/v0_5_0/query.rs create mode 100644 crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs create mode 100644 crates/starknet-types-rpc/src/v0_6_0/block_id.rs create mode 100644 crates/starknet-types-rpc/src/v0_6_0/query.rs create mode 100644 crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs diff --git a/crates/starknet-types-rpc/src/custom/block_id.rs b/crates/starknet-types-rpc/src/custom/block_id.rs index e5b92eb..79f53a7 100644 --- a/crates/starknet-types-rpc/src/custom/block_id.rs +++ b/crates/starknet-types-rpc/src/custom/block_id.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Deserializer, Serialize}; -use crate::{BlockHash, BlockNumber, BlockTag}; +use crate::v0_7_1::{BlockHash, BlockNumber, BlockTag}; /// A hexadecimal number. #[derive(Debug, Clone, Eq, Hash, PartialEq)] diff --git a/crates/starknet-types-rpc/src/custom/query.rs b/crates/starknet-types-rpc/src/custom/query.rs index a96e2de..8779101 100644 --- a/crates/starknet-types-rpc/src/custom/query.rs +++ b/crates/starknet-types-rpc/src/custom/query.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; -use crate::{ +use crate::v0_7_1::{ BroadcastedDeclareTxnV1, BroadcastedDeclareTxnV2, BroadcastedDeclareTxnV3, DeployAccountTxnV1, DeployAccountTxnV3, InvokeTxnV0, InvokeTxnV1, InvokeTxnV3, }; diff --git a/crates/starknet-types-rpc/src/custom/syncing_status.rs b/crates/starknet-types-rpc/src/custom/syncing_status.rs index d81b57f..17d87e3 100644 --- a/crates/starknet-types-rpc/src/custom/syncing_status.rs +++ b/crates/starknet-types-rpc/src/custom/syncing_status.rs @@ -2,7 +2,7 @@ use core::marker::PhantomData; use serde::de::Visitor; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::SyncStatus; +use crate::v0_7_1::SyncStatus; /// The syncing status of a node. #[derive(Clone, Debug)] diff --git a/crates/starknet-types-rpc/src/lib.rs b/crates/starknet-types-rpc/src/lib.rs index dfb4ddc..7c6d8c7 100644 --- a/crates/starknet-types-rpc/src/lib.rs +++ b/crates/starknet-types-rpc/src/lib.rs @@ -24,6 +24,8 @@ mod custom_serde; // // Generated files. +pub mod v0_5_0; +pub mod v0_6_0; pub mod v0_7_1; pub use self::v0_7_1::*; diff --git a/crates/starknet-types-rpc/src/v0_5_0/block_id.rs b/crates/starknet-types-rpc/src/v0_5_0/block_id.rs new file mode 100644 index 0000000..c288f96 --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_5_0/block_id.rs @@ -0,0 +1,129 @@ +use serde::{Deserialize, Deserializer, Serialize}; + +use super::{BlockHash, BlockNumber, BlockTag}; + +/// A hexadecimal number. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BlockId { + /// The tag of the block. + Tag(BlockTag), + /// The hash of the block. + Hash(BlockHash), + /// The height of the block. + Number(BlockNumber), +} + +#[derive(Serialize, Deserialize)] +struct BlockHashHelper { + block_hash: BlockHash, +} + +#[derive(Serialize, Deserialize)] +struct BlockNumberHelper { + block_number: BlockNumber, +} + +#[derive(Deserialize)] +#[serde(untagged)] +enum BlockIdHelper { + Tag(BlockTag), + Hash(BlockHashHelper), + Number(BlockNumberHelper), +} + +impl serde::Serialize for BlockId { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match *self { + BlockId::Tag(tag) => tag.serialize(serializer), + BlockId::Hash(block_hash) => { + let helper = BlockHashHelper { block_hash }; + helper.serialize(serializer) + } + BlockId::Number(block_number) => { + let helper = BlockNumberHelper { block_number }; + helper.serialize(serializer) + } + } + } +} + +impl<'de> serde::Deserialize<'de> for BlockId { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let helper = BlockIdHelper::deserialize(deserializer)?; + match helper { + BlockIdHelper::Tag(tag) => Ok(BlockId::Tag(tag)), + BlockIdHelper::Hash(helper) => Ok(BlockId::Hash(helper.block_hash)), + BlockIdHelper::Number(helper) => Ok(BlockId::Number(helper.block_number)), + } + } +} + +#[test] +fn block_id_from_hash() { + use starknet_types_core::felt::Felt; + + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); +} + +#[test] +fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); +} + +#[test] +fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); +} + +#[test] +fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); +} + +#[cfg(test)] +#[test] +fn block_id_to_hash() { + use starknet_types_core::felt::Felt; + + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); +} + +#[cfg(test)] +#[test] +fn block_id_to_number() { + let block_id = BlockId::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); +} + +#[cfg(test)] +#[test] +fn block_id_to_latest() { + let block_id = BlockId::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); +} + +#[cfg(test)] +#[test] +fn block_id_to_pending() { + let block_id = BlockId::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); +} diff --git a/crates/starknet-types-rpc/src/v0_5_0/mod.rs b/crates/starknet-types-rpc/src/v0_5_0/mod.rs index 7a43d1b..55f328d 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/mod.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/mod.rs @@ -2,15 +2,16 @@ pub use starknet_types_core::felt::Felt; -pub use crate::custom::{ - BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, - SyncingStatus, -}; - +mod block_id; +mod query; mod starknet_api_openrpc; mod starknet_trace_api_openrpc; mod starknet_write_api; +mod syncing_status; +pub use self::block_id::*; +pub use self::query::*; pub use self::starknet_api_openrpc::*; pub use self::starknet_trace_api_openrpc::*; pub use self::starknet_write_api::*; +pub use self::syncing_status::*; diff --git a/crates/starknet-types-rpc/src/v0_5_0/query.rs b/crates/starknet-types-rpc/src/v0_5_0/query.rs new file mode 100644 index 0000000..e9dc161 --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_5_0/query.rs @@ -0,0 +1,48 @@ +// query offset: +// 0x0000000000000000000000000000000100000000000000000000000000000000 + +use serde::{Deserialize, Serialize}; + +use super::{ + BroadcastedDeclareTxnV1, BroadcastedDeclareTxnV2, DeployAccountTxnV1, InvokeTxnV0, InvokeTxnV1, +}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedDeclareTxn { + #[serde(rename = "0x1")] + V1(BroadcastedDeclareTxnV1), + #[serde(rename = "0x2")] + V2(BroadcastedDeclareTxnV2), + /// Query-only broadcasted declare transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(BroadcastedDeclareTxnV1), + /// Query-only broadcasted declare transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000002")] + QueryV2(BroadcastedDeclareTxnV2), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedDeployAccountTxn { + #[serde(rename = "0x1")] + V1(DeployAccountTxnV1), + /// Query-only broadcasted deploy account transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(DeployAccountTxnV1), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedInvokeTxn { + #[serde(rename = "0x0")] + V0(InvokeTxnV0), + #[serde(rename = "0x1")] + V1(InvokeTxnV1), + /// Query-only broadcasted invoke transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000000")] + QueryV0(InvokeTxnV0), + /// Query-only broadcasted invoke transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(InvokeTxnV1), +} diff --git a/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs b/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs index f840fa6..2d3cbcd 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs @@ -8,15 +8,19 @@ // https://github.com/nils-mathieu/openrpc-gen // -use super::{ - BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, Felt, -}; +// use super::{ +// BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, Felt, +// }; use crate::custom_serde::NumAsHex; use alloc::string::String; use alloc::vec::Vec; use serde::ser::SerializeMap; use serde::{Deserialize, Serialize}; +use super::{ + BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, Felt, +}; + pub type Address = Felt; #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs b/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs new file mode 100644 index 0000000..7877bfc --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs @@ -0,0 +1,88 @@ +use serde::de::Visitor; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use super::SyncStatus; + +/// The syncing status of a node. +#[derive(Clone, Debug)] +pub enum SyncingStatus { + /// The node is not syncing. + NotSyncing, + /// The node is syncing. + Syncing(SyncStatus), +} + +impl Serialize for SyncingStatus { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match self { + SyncingStatus::NotSyncing => serializer.serialize_bool(false), + SyncingStatus::Syncing(status) => status.serialize(serializer), + } + } +} + +impl<'de> Deserialize<'de> for SyncingStatus { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct SyncingStatusVisitor; + + impl<'de> Visitor<'de> for SyncingStatusVisitor { + type Value = SyncingStatus; + + fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { + writeln!(formatter, "a syncing status") + } + + fn visit_bool(self, v: bool) -> Result + where + E: serde::de::Error, + { + if v { + Err(serde::de::Error::custom("expected a syncing status")) + } else { + Ok(SyncingStatus::NotSyncing) + } + } + + fn visit_map(self, map: A) -> Result + where + A: serde::de::MapAccess<'de>, + { + let status = + SyncStatus::deserialize(serde::de::value::MapAccessDeserializer::new(map))?; + + Ok(SyncingStatus::Syncing(status)) + } + } + + deserializer.deserialize_any(SyncingStatusVisitor) + } +} + +#[cfg(test)] +#[test] +fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); +} + +#[cfg(test)] +#[test] +fn syncing_status_to_false() { + let syncing_status = SyncingStatus::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); +} + +#[cfg(test)] +#[test] +fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::(s).is_err()); +} diff --git a/crates/starknet-types-rpc/src/v0_6_0/block_id.rs b/crates/starknet-types-rpc/src/v0_6_0/block_id.rs new file mode 100644 index 0000000..c288f96 --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_6_0/block_id.rs @@ -0,0 +1,129 @@ +use serde::{Deserialize, Deserializer, Serialize}; + +use super::{BlockHash, BlockNumber, BlockTag}; + +/// A hexadecimal number. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BlockId { + /// The tag of the block. + Tag(BlockTag), + /// The hash of the block. + Hash(BlockHash), + /// The height of the block. + Number(BlockNumber), +} + +#[derive(Serialize, Deserialize)] +struct BlockHashHelper { + block_hash: BlockHash, +} + +#[derive(Serialize, Deserialize)] +struct BlockNumberHelper { + block_number: BlockNumber, +} + +#[derive(Deserialize)] +#[serde(untagged)] +enum BlockIdHelper { + Tag(BlockTag), + Hash(BlockHashHelper), + Number(BlockNumberHelper), +} + +impl serde::Serialize for BlockId { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match *self { + BlockId::Tag(tag) => tag.serialize(serializer), + BlockId::Hash(block_hash) => { + let helper = BlockHashHelper { block_hash }; + helper.serialize(serializer) + } + BlockId::Number(block_number) => { + let helper = BlockNumberHelper { block_number }; + helper.serialize(serializer) + } + } + } +} + +impl<'de> serde::Deserialize<'de> for BlockId { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let helper = BlockIdHelper::deserialize(deserializer)?; + match helper { + BlockIdHelper::Tag(tag) => Ok(BlockId::Tag(tag)), + BlockIdHelper::Hash(helper) => Ok(BlockId::Hash(helper.block_hash)), + BlockIdHelper::Number(helper) => Ok(BlockId::Number(helper.block_number)), + } + } +} + +#[test] +fn block_id_from_hash() { + use starknet_types_core::felt::Felt; + + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); +} + +#[test] +fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); +} + +#[test] +fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); +} + +#[test] +fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); +} + +#[cfg(test)] +#[test] +fn block_id_to_hash() { + use starknet_types_core::felt::Felt; + + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); +} + +#[cfg(test)] +#[test] +fn block_id_to_number() { + let block_id = BlockId::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); +} + +#[cfg(test)] +#[test] +fn block_id_to_latest() { + let block_id = BlockId::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); +} + +#[cfg(test)] +#[test] +fn block_id_to_pending() { + let block_id = BlockId::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); +} diff --git a/crates/starknet-types-rpc/src/v0_6_0/mod.rs b/crates/starknet-types-rpc/src/v0_6_0/mod.rs index 4570a01..b0981ec 100644 --- a/crates/starknet-types-rpc/src/v0_6_0/mod.rs +++ b/crates/starknet-types-rpc/src/v0_6_0/mod.rs @@ -2,15 +2,16 @@ pub use starknet_types_core::felt::Felt; -pub use crate::custom::{ - BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, - SyncingStatus, -}; - +mod block_id; +mod query; mod starknet_api_openrpc; mod starknet_trace_api_openrpc; mod starknet_write_api; +mod syncing_status; +pub use self::block_id::*; +pub use self::query::*; pub use self::starknet_api_openrpc::*; pub use self::starknet_trace_api_openrpc::*; pub use self::starknet_write_api::*; +pub use self::syncing_status::*; diff --git a/crates/starknet-types-rpc/src/v0_6_0/query.rs b/crates/starknet-types-rpc/src/v0_6_0/query.rs new file mode 100644 index 0000000..e9dc161 --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_6_0/query.rs @@ -0,0 +1,48 @@ +// query offset: +// 0x0000000000000000000000000000000100000000000000000000000000000000 + +use serde::{Deserialize, Serialize}; + +use super::{ + BroadcastedDeclareTxnV1, BroadcastedDeclareTxnV2, DeployAccountTxnV1, InvokeTxnV0, InvokeTxnV1, +}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedDeclareTxn { + #[serde(rename = "0x1")] + V1(BroadcastedDeclareTxnV1), + #[serde(rename = "0x2")] + V2(BroadcastedDeclareTxnV2), + /// Query-only broadcasted declare transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(BroadcastedDeclareTxnV1), + /// Query-only broadcasted declare transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000002")] + QueryV2(BroadcastedDeclareTxnV2), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedDeployAccountTxn { + #[serde(rename = "0x1")] + V1(DeployAccountTxnV1), + /// Query-only broadcasted deploy account transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(DeployAccountTxnV1), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedInvokeTxn { + #[serde(rename = "0x0")] + V0(InvokeTxnV0), + #[serde(rename = "0x1")] + V1(InvokeTxnV1), + /// Query-only broadcasted invoke transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000000")] + QueryV0(InvokeTxnV0), + /// Query-only broadcasted invoke transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(InvokeTxnV1), +} diff --git a/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs b/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs new file mode 100644 index 0000000..7877bfc --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs @@ -0,0 +1,88 @@ +use serde::de::Visitor; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use super::SyncStatus; + +/// The syncing status of a node. +#[derive(Clone, Debug)] +pub enum SyncingStatus { + /// The node is not syncing. + NotSyncing, + /// The node is syncing. + Syncing(SyncStatus), +} + +impl Serialize for SyncingStatus { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match self { + SyncingStatus::NotSyncing => serializer.serialize_bool(false), + SyncingStatus::Syncing(status) => status.serialize(serializer), + } + } +} + +impl<'de> Deserialize<'de> for SyncingStatus { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct SyncingStatusVisitor; + + impl<'de> Visitor<'de> for SyncingStatusVisitor { + type Value = SyncingStatus; + + fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { + writeln!(formatter, "a syncing status") + } + + fn visit_bool(self, v: bool) -> Result + where + E: serde::de::Error, + { + if v { + Err(serde::de::Error::custom("expected a syncing status")) + } else { + Ok(SyncingStatus::NotSyncing) + } + } + + fn visit_map(self, map: A) -> Result + where + A: serde::de::MapAccess<'de>, + { + let status = + SyncStatus::deserialize(serde::de::value::MapAccessDeserializer::new(map))?; + + Ok(SyncingStatus::Syncing(status)) + } + } + + deserializer.deserialize_any(SyncingStatusVisitor) + } +} + +#[cfg(test)] +#[test] +fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); +} + +#[cfg(test)] +#[test] +fn syncing_status_to_false() { + let syncing_status = SyncingStatus::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); +} + +#[cfg(test)] +#[test] +fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::(s).is_err()); +} From 7e671e39bb13bb22a4d2ee1cd957ea735d9ee4e5 Mon Sep 17 00:00:00 2001 From: Uacias Date: Tue, 6 Aug 2024 12:45:02 +0200 Subject: [PATCH 3/7] Remove redundant version from imports in custom module. --- crates/starknet-types-rpc/src/custom/block_id.rs | 2 +- crates/starknet-types-rpc/src/custom/query.rs | 2 +- crates/starknet-types-rpc/src/custom/syncing_status.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/starknet-types-rpc/src/custom/block_id.rs b/crates/starknet-types-rpc/src/custom/block_id.rs index 79f53a7..e5b92eb 100644 --- a/crates/starknet-types-rpc/src/custom/block_id.rs +++ b/crates/starknet-types-rpc/src/custom/block_id.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Deserializer, Serialize}; -use crate::v0_7_1::{BlockHash, BlockNumber, BlockTag}; +use crate::{BlockHash, BlockNumber, BlockTag}; /// A hexadecimal number. #[derive(Debug, Clone, Eq, Hash, PartialEq)] diff --git a/crates/starknet-types-rpc/src/custom/query.rs b/crates/starknet-types-rpc/src/custom/query.rs index 8779101..a96e2de 100644 --- a/crates/starknet-types-rpc/src/custom/query.rs +++ b/crates/starknet-types-rpc/src/custom/query.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; -use crate::v0_7_1::{ +use crate::{ BroadcastedDeclareTxnV1, BroadcastedDeclareTxnV2, BroadcastedDeclareTxnV3, DeployAccountTxnV1, DeployAccountTxnV3, InvokeTxnV0, InvokeTxnV1, InvokeTxnV3, }; diff --git a/crates/starknet-types-rpc/src/custom/syncing_status.rs b/crates/starknet-types-rpc/src/custom/syncing_status.rs index 17d87e3..d81b57f 100644 --- a/crates/starknet-types-rpc/src/custom/syncing_status.rs +++ b/crates/starknet-types-rpc/src/custom/syncing_status.rs @@ -2,7 +2,7 @@ use core::marker::PhantomData; use serde::de::Visitor; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::v0_7_1::SyncStatus; +use crate::SyncStatus; /// The syncing status of a node. #[derive(Clone, Debug)] From 5bf54249407282f107cf3735a639f42342dfb030 Mon Sep 17 00:00:00 2001 From: Uacias Date: Wed, 7 Aug 2024 09:29:56 +0200 Subject: [PATCH 4/7] Rm redundant commit --- crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs b/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs index 2d3cbcd..349d985 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs @@ -8,9 +8,6 @@ // https://github.com/nils-mathieu/openrpc-gen // -// use super::{ -// BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, Felt, -// }; use crate::custom_serde::NumAsHex; use alloc::string::String; use alloc::vec::Vec; From 1ba7b0dbcaa6ad0d5fa5ddd07796354b9ba6095b Mon Sep 17 00:00:00 2001 From: Uacias Date: Wed, 7 Aug 2024 10:20:11 +0200 Subject: [PATCH 5/7] Create test mods for block_id and syncing_status. --- .../starknet-types-rpc/src/custom/block_id.rs | 118 ++++++++---------- .../src/custom/syncing_status.rs | 40 +++--- .../starknet-types-rpc/src/v0_5_0/block_id.rs | 104 ++++++++------- .../src/v0_5_0/syncing_status.rs | 38 +++--- .../starknet-types-rpc/src/v0_6_0/block_id.rs | 104 ++++++++------- .../src/v0_6_0/syncing_status.rs | 38 +++--- 6 files changed, 213 insertions(+), 229 deletions(-) diff --git a/crates/starknet-types-rpc/src/custom/block_id.rs b/crates/starknet-types-rpc/src/custom/block_id.rs index e5b92eb..a87954d 100644 --- a/crates/starknet-types-rpc/src/custom/block_id.rs +++ b/crates/starknet-types-rpc/src/custom/block_id.rs @@ -66,78 +66,64 @@ impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId { } } -#[test] -fn block_id_from_hash() { - pub use starknet_types_core::felt::Felt; - - let s = "{\"block_hash\":\"0x123\"}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); -} - -#[test] -fn block_id_from_number() { - pub use starknet_types_core::felt::Felt; - - let s = "{\"block_number\":123}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Number(123)); -} - -#[test] -fn block_id_from_latest() { - pub use starknet_types_core::felt::Felt; - - let s = "\"latest\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); -} - -#[test] -fn block_id_from_pending() { - pub use starknet_types_core::felt::Felt; - - let s = "\"pending\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); -} - #[cfg(test)] -#[test] -fn block_id_to_hash() { - pub use starknet_types_core::felt::Felt; +mod tests { + use super::*; + use starknet_types_core::felt::Felt; + + #[test] + fn block_id_from_hash() { + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); + } - let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_hash\":\"0x123\"}"); -} + #[test] + fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); + } -#[cfg(test)] -#[test] -fn block_id_to_number() { - pub use starknet_types_core::felt::Felt; + #[test] + fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); + } - let block_id = BlockId::::Number(123); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_number\":123}"); -} + #[test] + fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); + } -#[cfg(test)] -#[test] -fn block_id_to_latest() { - pub use starknet_types_core::felt::Felt; + #[test] + fn block_id_to_hash() { + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); + } - let block_id = BlockId::::Tag(BlockTag::Latest); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"latest\""); -} + #[test] + fn block_id_to_number() { + let block_id = BlockId::::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); + } -#[cfg(test)] -#[test] -fn block_id_to_pending() { - pub use starknet_types_core::felt::Felt; + #[test] + fn block_id_to_latest() { + let block_id = BlockId::::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); + } - let block_id = BlockId::::Tag(BlockTag::Pending); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"pending\""); + #[test] + fn block_id_to_pending() { + let block_id = BlockId::::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); + } } diff --git a/crates/starknet-types-rpc/src/custom/syncing_status.rs b/crates/starknet-types-rpc/src/custom/syncing_status.rs index d81b57f..1bc7e01 100644 --- a/crates/starknet-types-rpc/src/custom/syncing_status.rs +++ b/crates/starknet-types-rpc/src/custom/syncing_status.rs @@ -70,29 +70,27 @@ impl<'de, F: Deserialize<'de>> Deserialize<'de> for SyncingStatus { } #[cfg(test)] -#[test] -fn syncing_status_from_false() { +mod tests { + use super::*; pub use starknet_types_core::felt::Felt; - let s = "false"; - let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); - assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); -} - -#[cfg(test)] -#[test] -fn syncing_status_to_false() { - pub use starknet_types_core::felt::Felt; + #[test] + fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); + } - let syncing_status = SyncingStatus::::NotSyncing; - let s = serde_json::to_string(&syncing_status).unwrap(); - assert_eq!(s, "false"); -} + #[test] + fn syncing_status_to_false() { + let syncing_status = SyncingStatus::::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); + } -#[cfg(test)] -#[test] -fn syncing_status_from_true() { - pub use starknet_types_core::felt::Felt; - let s = "true"; - assert!(serde_json::from_str::>(s).is_err()); + #[test] + fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::>(s).is_err()); + } } diff --git a/crates/starknet-types-rpc/src/v0_5_0/block_id.rs b/crates/starknet-types-rpc/src/v0_5_0/block_id.rs index c288f96..f3aba77 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/block_id.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/block_id.rs @@ -64,66 +64,64 @@ impl<'de> serde::Deserialize<'de> for BlockId { } } -#[test] -fn block_id_from_hash() { +#[cfg(test)] +mod tests { + use super::*; use starknet_types_core::felt::Felt; - let s = "{\"block_hash\":\"0x123\"}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); -} - -#[test] -fn block_id_from_number() { - let s = "{\"block_number\":123}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Number(123)); -} + #[test] + fn block_id_from_hash() { + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); + } -#[test] -fn block_id_from_latest() { - let s = "\"latest\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); -} + #[test] + fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); + } -#[test] -fn block_id_from_pending() { - let s = "\"pending\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); -} + #[test] + fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); + } -#[cfg(test)] -#[test] -fn block_id_to_hash() { - use starknet_types_core::felt::Felt; + #[test] + fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); + } - let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_hash\":\"0x123\"}"); -} + #[test] + fn block_id_to_hash() { + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); + } -#[cfg(test)] -#[test] -fn block_id_to_number() { - let block_id = BlockId::Number(123); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_number\":123}"); -} + #[test] + fn block_id_to_number() { + let block_id = BlockId::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); + } -#[cfg(test)] -#[test] -fn block_id_to_latest() { - let block_id = BlockId::Tag(BlockTag::Latest); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"latest\""); -} + #[test] + fn block_id_to_latest() { + let block_id = BlockId::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); + } -#[cfg(test)] -#[test] -fn block_id_to_pending() { - let block_id = BlockId::Tag(BlockTag::Pending); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"pending\""); + #[test] + fn block_id_to_pending() { + let block_id = BlockId::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); + } } diff --git a/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs b/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs index 7877bfc..7901696 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs @@ -65,24 +65,26 @@ impl<'de> Deserialize<'de> for SyncingStatus { } #[cfg(test)] -#[test] -fn syncing_status_from_false() { - let s = "false"; - let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); - assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); -} +mod tests { + use super::*; -#[cfg(test)] -#[test] -fn syncing_status_to_false() { - let syncing_status = SyncingStatus::NotSyncing; - let s = serde_json::to_string(&syncing_status).unwrap(); - assert_eq!(s, "false"); -} + #[test] + fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); + } -#[cfg(test)] -#[test] -fn syncing_status_from_true() { - let s = "true"; - assert!(serde_json::from_str::(s).is_err()); + #[test] + fn syncing_status_to_false() { + let syncing_status = SyncingStatus::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); + } + + #[test] + fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::(s).is_err()); + } } diff --git a/crates/starknet-types-rpc/src/v0_6_0/block_id.rs b/crates/starknet-types-rpc/src/v0_6_0/block_id.rs index c288f96..f3aba77 100644 --- a/crates/starknet-types-rpc/src/v0_6_0/block_id.rs +++ b/crates/starknet-types-rpc/src/v0_6_0/block_id.rs @@ -64,66 +64,64 @@ impl<'de> serde::Deserialize<'de> for BlockId { } } -#[test] -fn block_id_from_hash() { +#[cfg(test)] +mod tests { + use super::*; use starknet_types_core::felt::Felt; - let s = "{\"block_hash\":\"0x123\"}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); -} - -#[test] -fn block_id_from_number() { - let s = "{\"block_number\":123}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Number(123)); -} + #[test] + fn block_id_from_hash() { + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); + } -#[test] -fn block_id_from_latest() { - let s = "\"latest\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); -} + #[test] + fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); + } -#[test] -fn block_id_from_pending() { - let s = "\"pending\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); -} + #[test] + fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); + } -#[cfg(test)] -#[test] -fn block_id_to_hash() { - use starknet_types_core::felt::Felt; + #[test] + fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); + } - let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_hash\":\"0x123\"}"); -} + #[test] + fn block_id_to_hash() { + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); + } -#[cfg(test)] -#[test] -fn block_id_to_number() { - let block_id = BlockId::Number(123); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_number\":123}"); -} + #[test] + fn block_id_to_number() { + let block_id = BlockId::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); + } -#[cfg(test)] -#[test] -fn block_id_to_latest() { - let block_id = BlockId::Tag(BlockTag::Latest); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"latest\""); -} + #[test] + fn block_id_to_latest() { + let block_id = BlockId::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); + } -#[cfg(test)] -#[test] -fn block_id_to_pending() { - let block_id = BlockId::Tag(BlockTag::Pending); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"pending\""); + #[test] + fn block_id_to_pending() { + let block_id = BlockId::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); + } } diff --git a/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs b/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs index 7877bfc..7901696 100644 --- a/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs +++ b/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs @@ -65,24 +65,26 @@ impl<'de> Deserialize<'de> for SyncingStatus { } #[cfg(test)] -#[test] -fn syncing_status_from_false() { - let s = "false"; - let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); - assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); -} +mod tests { + use super::*; -#[cfg(test)] -#[test] -fn syncing_status_to_false() { - let syncing_status = SyncingStatus::NotSyncing; - let s = serde_json::to_string(&syncing_status).unwrap(); - assert_eq!(s, "false"); -} + #[test] + fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); + } -#[cfg(test)] -#[test] -fn syncing_status_from_true() { - let s = "true"; - assert!(serde_json::from_str::(s).is_err()); + #[test] + fn syncing_status_to_false() { + let syncing_status = SyncingStatus::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); + } + + #[test] + fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::(s).is_err()); + } } From 353d7928388a2ae78aad9feeef9d049bf33a61aa Mon Sep 17 00:00:00 2001 From: Uacias Date: Wed, 7 Aug 2024 10:24:06 +0200 Subject: [PATCH 6/7] Fix test mods imports. --- crates/starknet-types-rpc/src/custom/block_id.rs | 2 +- crates/starknet-types-rpc/src/custom/syncing_status.rs | 2 +- crates/starknet-types-rpc/src/v0_5_0/block_id.rs | 2 +- crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs | 2 +- crates/starknet-types-rpc/src/v0_6_0/block_id.rs | 2 +- crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/starknet-types-rpc/src/custom/block_id.rs b/crates/starknet-types-rpc/src/custom/block_id.rs index a87954d..b0945e3 100644 --- a/crates/starknet-types-rpc/src/custom/block_id.rs +++ b/crates/starknet-types-rpc/src/custom/block_id.rs @@ -68,7 +68,7 @@ impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId { #[cfg(test)] mod tests { - use super::*; + use super::{BlockId, BlockTag}; use starknet_types_core::felt::Felt; #[test] diff --git a/crates/starknet-types-rpc/src/custom/syncing_status.rs b/crates/starknet-types-rpc/src/custom/syncing_status.rs index 1bc7e01..db20009 100644 --- a/crates/starknet-types-rpc/src/custom/syncing_status.rs +++ b/crates/starknet-types-rpc/src/custom/syncing_status.rs @@ -71,7 +71,7 @@ impl<'de, F: Deserialize<'de>> Deserialize<'de> for SyncingStatus { #[cfg(test)] mod tests { - use super::*; + use super::SyncingStatus; pub use starknet_types_core::felt::Felt; #[test] diff --git a/crates/starknet-types-rpc/src/v0_5_0/block_id.rs b/crates/starknet-types-rpc/src/v0_5_0/block_id.rs index f3aba77..f1e2a04 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/block_id.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/block_id.rs @@ -66,7 +66,7 @@ impl<'de> serde::Deserialize<'de> for BlockId { #[cfg(test)] mod tests { - use super::*; + use super::{BlockId, BlockTag}; use starknet_types_core::felt::Felt; #[test] diff --git a/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs b/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs index 7901696..2a22ffc 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs @@ -66,7 +66,7 @@ impl<'de> Deserialize<'de> for SyncingStatus { #[cfg(test)] mod tests { - use super::*; + use super::SyncingStatus; #[test] fn syncing_status_from_false() { diff --git a/crates/starknet-types-rpc/src/v0_6_0/block_id.rs b/crates/starknet-types-rpc/src/v0_6_0/block_id.rs index f3aba77..f1e2a04 100644 --- a/crates/starknet-types-rpc/src/v0_6_0/block_id.rs +++ b/crates/starknet-types-rpc/src/v0_6_0/block_id.rs @@ -66,7 +66,7 @@ impl<'de> serde::Deserialize<'de> for BlockId { #[cfg(test)] mod tests { - use super::*; + use super::{BlockId, BlockTag}; use starknet_types_core::felt::Felt; #[test] diff --git a/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs b/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs index 7901696..2a22ffc 100644 --- a/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs +++ b/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs @@ -66,7 +66,7 @@ impl<'de> Deserialize<'de> for SyncingStatus { #[cfg(test)] mod tests { - use super::*; + use super::SyncingStatus; #[test] fn syncing_status_from_false() { From dfe6b727c2bef5b87f280b934a8af286ba45c575 Mon Sep 17 00:00:00 2001 From: Uacias Date: Wed, 7 Aug 2024 10:30:15 +0200 Subject: [PATCH 7/7] Create test mod for num_as_hex. --- .../src/custom_serde/num_as_hex.rs | 110 +++++++++--------- 1 file changed, 52 insertions(+), 58 deletions(-) diff --git a/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs b/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs index a3fe274..41ad462 100644 --- a/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs +++ b/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs @@ -198,72 +198,66 @@ where } #[cfg(test)] -#[derive(serde::Serialize, serde::Deserialize)] -#[serde(transparent)] -struct Helper { - #[serde(with = "NumAsHex")] - num: u64, -} +mod tests { + use super::*; + + #[derive(serde::Serialize, serde::Deserialize)] + #[serde(transparent)] + struct Helper { + #[serde(with = "NumAsHex")] + num: u64, + } -#[cfg(test)] -fn serialize(num: u64) -> serde_json::Result { - let helper = Helper { num }; - serde_json::to_string(&helper) -} + fn serialize(num: u64) -> serde_json::Result { + let helper = Helper { num }; + serde_json::to_string(&helper) + } -#[cfg(test)] -fn deserialize(s: &str) -> serde_json::Result { - let helper: Helper = serde_json::from_str(s)?; - Ok(helper.num) -} + fn deserialize(s: &str) -> serde_json::Result { + let helper: Helper = serde_json::from_str(s)?; + Ok(helper.num) + } -#[test] -#[cfg(test)] -fn serialize_0_hex() { - assert_eq!(serialize(0x0).unwrap(), "\"0x0\""); -} + #[test] + fn serialize_0_hex() { + assert_eq!(serialize(0x0).unwrap(), "\"0x0\""); + } -#[test] -#[cfg(test)] -fn srialize_hex() { - assert_eq!(serialize(0x1234).unwrap(), "\"0x1234\""); -} + #[test] + fn serialize_hex() { + assert_eq!(serialize(0x1234).unwrap(), "\"0x1234\""); + } -#[test] -#[cfg(test)] -fn srialize_max() { - assert_eq!(serialize(u64::MAX).unwrap(), "\"0xffffffffffffffff\""); -} + #[test] + fn serialize_max() { + assert_eq!(serialize(u64::MAX).unwrap(), "\"0xffffffffffffffff\""); + } -#[test] -#[cfg(test)] -fn deserialize_zero() { - assert_eq!(deserialize("\"0x0\"").unwrap(), 0); -} + #[test] + fn deserialize_zero() { + assert_eq!(deserialize("\"0x0\"").unwrap(), 0); + } -#[test] -#[cfg(test)] -fn deserialize_zeros() { - assert_eq!(deserialize("\"0x00000\"").unwrap(), 0); -} + #[test] + fn deserialize_zeros() { + assert_eq!(deserialize("\"0x00000\"").unwrap(), 0); + } -#[test] -#[cfg(test)] -fn deserialize_max() { - assert_eq!(deserialize("\"0xFFFFFFFFFFFFFFFF\"").unwrap(), u64::MAX); -} + #[test] + fn deserialize_max() { + assert_eq!(deserialize("\"0xFFFFFFFFFFFFFFFF\"").unwrap(), u64::MAX); + } -#[test] -#[cfg(test)] -fn deserialize_big_one() { - assert_eq!( - deserialize("\"0x000000000000000000000000000001\"").unwrap(), - 1 - ); -} + #[test] + fn deserialize_big_one() { + assert_eq!( + deserialize("\"0x000000000000000000000000000001\"").unwrap(), + 1 + ); + } -#[test] -#[cfg(test)] -fn deserialize_hex() { - assert_eq!(deserialize("\"0x1234\"").unwrap(), 0x1234); + #[test] + fn deserialize_hex() { + assert_eq!(deserialize("\"0x1234\"").unwrap(), 0x1234); + } }