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

Use u64 for Subscription ID #656

Merged
merged 3 commits into from
Dec 10, 2024
Merged
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
4 changes: 3 additions & 1 deletion crates/starknet-devnet-server/src/api/json_rpc/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,12 @@ pub struct L1TransactionHashInput {
pub transaction_hash: Hash256,
}

pub type SubscriptionId = u64;

#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct SubscriptionIdInput {
pub subscription_id: i64,
pub subscription_id: SubscriptionId,
}

#[derive(Deserialize, Clone, Debug)]
Expand Down
3 changes: 1 addition & 2 deletions crates/starknet-devnet-server/src/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use starknet_types::rpc::transactions::{TransactionStatus, TransactionWithHash};
use tokio::sync::Mutex;

use crate::api::json_rpc::error::ApiError;
use crate::api::json_rpc::models::SubscriptionId;
use crate::rpc_core::request::Id;

pub type SocketId = u64;

type SubscriptionId = i64;

#[derive(Debug)]
pub struct AddressFilter {
address_container: Vec<ContractAddress>,
Expand Down
12 changes: 7 additions & 5 deletions crates/starknet-devnet/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,15 @@ pub async fn send_binary_rpc_via_ws(
Ok(resp_body)
}

pub type SubscriptionId = u64;

pub async fn subscribe(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
subscription_method: &str,
params: serde_json::Value,
) -> Result<i64, anyhow::Error> {
) -> Result<SubscriptionId, anyhow::Error> {
let subscription_confirmation = send_text_rpc_via_ws(ws, subscription_method, params).await?;
subscription_confirmation["result"].as_i64().ok_or(anyhow::Error::msg(format!(
subscription_confirmation["result"].as_u64().ok_or(anyhow::Error::msg(format!(
"No ID in subscription response: {subscription_confirmation}"
)))
}
Expand All @@ -459,7 +461,7 @@ pub async fn receive_rpc_via_ws(
pub async fn receive_notification(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
method: &str,
expected_subscription_id: i64,
expected_subscription_id: SubscriptionId,
) -> Result<serde_json::Value, anyhow::Error> {
let mut notification = receive_rpc_via_ws(ws).await?;
assert_eq!(notification["jsonrpc"], "2.0");
Expand All @@ -479,13 +481,13 @@ pub async fn assert_no_notifications(ws: &mut WebSocketStream<MaybeTlsStream<Tcp
pub async fn subscribe_new_heads(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
block_specifier: serde_json::Value,
) -> Result<i64, anyhow::Error> {
) -> Result<SubscriptionId, anyhow::Error> {
subscribe(ws, "starknet_subscribeNewHeads", block_specifier).await
}

pub async fn unsubscribe(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
subscription_id: i64,
subscription_id: SubscriptionId,
) -> Result<serde_json::Value, anyhow::Error> {
send_text_rpc_via_ws(ws, "starknet_unsubscribe", json!({ "subscription_id": subscription_id }))
.await
Expand Down
3 changes: 2 additions & 1 deletion crates/starknet-devnet/tests/test_subscription_to_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ mod block_subscription_support {
use crate::common::background_devnet::BackgroundDevnet;
use crate::common::utils::{
assert_no_notifications, receive_notification, subscribe_new_heads, unsubscribe,
SubscriptionId,
};

async fn receive_block(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
subscription_id: i64,
subscription_id: SubscriptionId,
) -> Result<serde_json::Value, anyhow::Error> {
receive_notification(ws, "starknet_subscriptionNewHeads", subscription_id).await
}
Expand Down
6 changes: 3 additions & 3 deletions crates/starknet-devnet/tests/test_subscription_to_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ mod event_subscription_support {
use crate::common::utils::{
assert_no_notifications, declare_v3_deploy_v3,
get_events_contract_in_sierra_and_compiled_class_hash, receive_notification,
receive_rpc_via_ws, subscribe, unsubscribe,
receive_rpc_via_ws, subscribe, unsubscribe, SubscriptionId,
};

async fn subscribe_events(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
params: serde_json::Value,
) -> Result<i64, anyhow::Error> {
) -> Result<SubscriptionId, anyhow::Error> {
subscribe(ws, "starknet_subscribeEvents", params).await
}

async fn receive_event(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
subscription_id: i64,
subscription_id: SubscriptionId,
) -> Result<serde_json::Value, anyhow::Error> {
receive_notification(ws, "starknet_subscriptionEvents", subscription_id).await
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ mod pending_transactions_subscription_support {
use crate::common::utils::{
assert_no_notifications, declare_v3_deploy_v3,
get_simple_contract_in_sierra_and_compiled_class_hash, receive_rpc_via_ws, subscribe,
unsubscribe,
unsubscribe, SubscriptionId,
};

async fn subscribe_pending_txs(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
params: serde_json::Value,
) -> Result<i64, anyhow::Error> {
) -> Result<SubscriptionId, anyhow::Error> {
subscribe(ws, "starknet_subscribePendingTransactions", params).await
}

Expand Down
2 changes: 1 addition & 1 deletion crates/starknet-devnet/tests/test_subscription_to_reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod reorg_subscription_support {
// was received per subscription_id, we extract the IDs from notifications, store them
// in a set, and later assert equality with the set of expected subscription IDs.
let notification_id =
notification["params"]["subscription_id"].take().as_i64().unwrap();
notification["params"]["subscription_id"].take().as_u64().unwrap();
notification_ids.insert(notification_id);

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ mod tx_status_subscription_support {
use crate::common::background_devnet::BackgroundDevnet;
use crate::common::utils::{
assert_no_notifications, receive_rpc_via_ws, subscribe, subscribe_new_heads, unsubscribe,
SubscriptionId,
};

async fn subscribe_tx_status(
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
tx_hash: &Felt,
block_id: Option<BlockId>,
) -> Result<i64, anyhow::Error> {
) -> Result<SubscriptionId, anyhow::Error> {
let mut params = json!({ "transaction_hash": tx_hash });

if let Some(block_id) = block_id {
Expand All @@ -38,7 +39,7 @@ mod tx_status_subscription_support {
fn assert_successful_mint_notification(
notification: serde_json::Value,
tx_hash: Felt,
subscription_id: i64,
subscription_id: SubscriptionId,
) {
assert_eq!(
notification,
Expand Down