Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/1 mods #88

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -57,4 +57,3 @@ lazy_static = { version = "1.4.0", default-features = false }
[[bench]]
name = "criterion_hashes"
harness = false

118 changes: 52 additions & 66 deletions crates/starknet-types-rpc/src/custom/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,78 +66,64 @@ impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId<F> {
}
}

#[test]
fn block_id_from_hash() {
pub use starknet_types_core::felt::Felt;

let s = "{\"block_hash\":\"0x123\"}";
let block_id: BlockId<Felt> = 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<Felt> = 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<Felt> = 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<Felt> = 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::{BlockId, BlockTag};
use starknet_types_core::felt::Felt;

#[test]
fn block_id_from_hash() {
let s = "{\"block_hash\":\"0x123\"}";
let block_id: BlockId<Felt> = 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<Felt> = 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<Felt> = serde_json::from_str(s).unwrap();
assert_eq!(block_id, BlockId::Tag(BlockTag::Latest));
}

let block_id = BlockId::<Felt>::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<Felt> = 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::<Felt>::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::<Felt>::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::<Felt>::Tag(BlockTag::Latest);
let s = serde_json::to_string(&block_id).unwrap();
assert_eq!(s, "\"latest\"");
}

let block_id = BlockId::<Felt>::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::<Felt>::Tag(BlockTag::Pending);
let s = serde_json::to_string(&block_id).unwrap();
assert_eq!(s, "\"pending\"");
}
}
40 changes: 19 additions & 21 deletions crates/starknet-types-rpc/src/custom/syncing_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,27 @@ impl<'de, F: Deserialize<'de>> Deserialize<'de> for SyncingStatus<F> {
}

#[cfg(test)]
#[test]
fn syncing_status_from_false() {
mod tests {
use super::SyncingStatus;
pub use starknet_types_core::felt::Felt;

let s = "false";
let syncing_status: SyncingStatus<Felt> = 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<Felt> = serde_json::from_str(s).unwrap();
assert!(matches!(syncing_status, SyncingStatus::NotSyncing));
}

let syncing_status = SyncingStatus::<Felt>::NotSyncing;
let s = serde_json::to_string(&syncing_status).unwrap();
assert_eq!(s, "false");
}
#[test]
fn syncing_status_to_false() {
let syncing_status = SyncingStatus::<Felt>::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::<SyncingStatus<Felt>>(s).is_err());
#[test]
fn syncing_status_from_true() {
let s = "true";
assert!(serde_json::from_str::<SyncingStatus<Felt>>(s).is_err());
}
}
110 changes: 52 additions & 58 deletions crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<alloc::string::String> {
let helper = Helper { num };
serde_json::to_string(&helper)
}
fn serialize(num: u64) -> serde_json::Result<alloc::string::String> {
let helper = Helper { num };
serde_json::to_string(&helper)
}

#[cfg(test)]
fn deserialize(s: &str) -> serde_json::Result<u64> {
let helper: Helper = serde_json::from_str(s)?;
Ok(helper.num)
}
fn deserialize(s: &str) -> serde_json::Result<u64> {
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);
}
}
2 changes: 2 additions & 0 deletions crates/starknet-types-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Loading