Skip to content

Commit

Permalink
make it generic over Felt type
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejka committed Jun 14, 2024
1 parent dd3275f commit c1f9d31
Show file tree
Hide file tree
Showing 7 changed files with 786 additions and 662 deletions.
42 changes: 27 additions & 15 deletions crates/starknet-types-rpc/src/custom/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use crate::{BlockHash, BlockNumber, BlockTag};

/// A hexadecimal number.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BlockId {
pub enum BlockId<F> {
/// The tag of the block.
Tag(BlockTag),
/// The hash of the block.
Hash(BlockHash),
Hash(BlockHash<F>),
/// The height of the block.
Number(BlockNumber),
}

#[derive(Serialize, Deserialize)]
struct BlockHashHelper {
block_hash: BlockHash,
struct BlockHashHelper<F> {
block_hash: BlockHash<F>,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -25,13 +25,13 @@ struct BlockNumberHelper {

#[derive(Deserialize)]
#[serde(untagged)]
enum BlockIdHelper {
enum BlockIdHelper<F> {
Tag(BlockTag),
Hash(BlockHashHelper),
Hash(BlockHashHelper<F>),
Number(BlockNumberHelper),
}

impl serde::Serialize for BlockId {
impl<F: Copy + Serialize> serde::Serialize for BlockId<F> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -50,7 +50,7 @@ impl serde::Serialize for BlockId {
}
}

impl<'de> serde::Deserialize<'de> for BlockId {
impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId<F> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
Expand All @@ -69,28 +69,34 @@ fn block_id_from_hash() {
use crate::Felt;

let s = "{\"block_hash\":\"0x123\"}";
let block_id: BlockId = serde_json::from_str(s).unwrap();
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() {
use crate::Felt;

let s = "{\"block_number\":123}";
let block_id: BlockId = serde_json::from_str(s).unwrap();
let block_id: BlockId<Felt> = serde_json::from_str(s).unwrap();
assert_eq!(block_id, BlockId::Number(123));
}

#[test]
fn block_id_from_latest() {
use crate::Felt;

let s = "\"latest\"";
let block_id: BlockId = serde_json::from_str(s).unwrap();
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() {
use crate::Felt;

let s = "\"pending\"";
let block_id: BlockId = serde_json::from_str(s).unwrap();
let block_id: BlockId<Felt> = serde_json::from_str(s).unwrap();
assert_eq!(block_id, BlockId::Tag(BlockTag::Pending));
}

Expand All @@ -107,23 +113,29 @@ fn block_id_to_hash() {
#[cfg(test)]
#[test]
fn block_id_to_number() {
let block_id = BlockId::Number(123);
use crate::Felt;

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_latest() {
let block_id = BlockId::Tag(BlockTag::Latest);
use crate::Felt;

let block_id = BlockId::<Felt>::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);
use crate::Felt;

let block_id = BlockId::<Felt>::Tag(BlockTag::Pending);
let s = serde_json::to_string(&block_id).unwrap();
assert_eq!(s, "\"pending\"");
}
26 changes: 13 additions & 13 deletions crates/starknet-types-rpc/src/custom/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ use crate::{

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "version")]
pub enum BroadcastedDeclareTxn {
pub enum BroadcastedDeclareTxn<F: Default> {
#[serde(rename = "0x1")]
V1(BroadcastedDeclareTxnV1),
V1(BroadcastedDeclareTxnV1<F>),
#[serde(rename = "0x2")]
V2(BroadcastedDeclareTxnV2),
V2(BroadcastedDeclareTxnV2<F>),
/// Query-only broadcasted declare transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")]
QueryV1(BroadcastedDeclareTxnV1),
QueryV1(BroadcastedDeclareTxnV1<F>),
/// Query-only broadcasted declare transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000002")]
QueryV2(BroadcastedDeclareTxnV2),
QueryV2(BroadcastedDeclareTxnV2<F>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "version")]
pub enum BroadcastedDeployAccountTxn {
pub enum BroadcastedDeployAccountTxn<F> {
#[serde(rename = "0x1")]
V1(DeployAccountTxnV1),
V1(DeployAccountTxnV1<F>),
/// Query-only broadcasted deploy account transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")]
QueryV1(DeployAccountTxnV1),
QueryV1(DeployAccountTxnV1<F>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "version")]
pub enum BroadcastedInvokeTxn {
pub enum BroadcastedInvokeTxn<F> {
#[serde(rename = "0x0")]
V0(InvokeTxnV0),
V0(InvokeTxnV0<F>),
#[serde(rename = "0x1")]
V1(InvokeTxnV1),
V1(InvokeTxnV1<F>),
/// Query-only broadcasted invoke transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000000")]
QueryV0(InvokeTxnV0),
QueryV0(InvokeTxnV0<F>),
/// Query-only broadcasted invoke transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")]
QueryV1(InvokeTxnV1),
QueryV1(InvokeTxnV1<F>),
}
32 changes: 21 additions & 11 deletions crates/starknet-types-rpc/src/custom/syncing_status.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use core::marker::PhantomData;
use serde::de::Visitor;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::SyncStatus;

/// The syncing status of a node.
#[derive(Clone, Debug)]
pub enum SyncingStatus {
pub enum SyncingStatus<F> {
/// The node is not syncing.
NotSyncing,
/// The node is syncing.
Syncing(SyncStatus),
Syncing(SyncStatus<F>),
}

impl Serialize for SyncingStatus {
impl<F: Serialize> Serialize for SyncingStatus<F> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -24,15 +25,17 @@ impl Serialize for SyncingStatus {
}
}

impl<'de> Deserialize<'de> for SyncingStatus {
impl<'de, F: Deserialize<'de>> Deserialize<'de> for SyncingStatus<F> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct SyncingStatusVisitor;
struct SyncingStatusVisitor<F> {
marker: PhantomData<F>,
}

impl<'de> Visitor<'de> for SyncingStatusVisitor {
type Value = SyncingStatus;
impl<'de, F: Deserialize<'de>> Visitor<'de> for SyncingStatusVisitor<F> {
type Value = SyncingStatus<F>;

fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
writeln!(formatter, "a syncing status")
Expand Down Expand Up @@ -60,29 +63,36 @@ impl<'de> Deserialize<'de> for SyncingStatus {
}
}

deserializer.deserialize_any(SyncingStatusVisitor)
deserializer.deserialize_any(SyncingStatusVisitor::<F> {
marker: PhantomData,
})
}
}

#[cfg(test)]
#[test]
fn syncing_status_from_false() {
use crate::Felt;

let s = "false";
let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap();
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() {
let syncing_status = SyncingStatus::NotSyncing;
use crate::Felt;

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() {
use crate::Felt;
let s = "true";
assert!(serde_json::from_str::<SyncingStatus>(s).is_err());
assert!(serde_json::from_str::<SyncingStatus<Felt>>(s).is_err());
}
4 changes: 2 additions & 2 deletions crates/starknet-types-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ mod custom_serde;
//
// Generated files.
//
pub mod v0_5_0;
pub mod v0_6_0;
// pub mod v0_5_0;
// pub mod v0_6_0;
pub mod v0_7_1;

pub use self::v0_7_1::*;
Loading

0 comments on commit c1f9d31

Please sign in to comment.