From 54b172d7dc3263123ec0b7a3135f196b69df527c Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 22 Jan 2024 14:56:42 +0100 Subject: [PATCH] Update `sudo_network_unstable_watch` to latest JSON-RPC draft (#1597) * Update `sudo_network_unstable_watch` to latest JSON-RPC draft * Rustfmt --- lib/src/json_rpc/methods.rs | 109 ++++++------------ lib/src/json_rpc/service/client_main_task.rs | 16 +-- light-base/src/json_rpc_service/background.rs | 10 +- 3 files changed, 46 insertions(+), 89 deletions(-) diff --git a/lib/src/json_rpc/methods.rs b/lib/src/json_rpc/methods.rs index 5904771075..9c76d77765 100644 --- a/lib/src/json_rpc/methods.rs +++ b/lib/src/json_rpc/methods.rs @@ -504,8 +504,8 @@ define_methods! { // These functions are a custom addition in smoldot. As of the writing of this comment, there // is no plan to standardize them. See and // . - network_unstable_subscribeEvents() -> Cow<'a, str>, - network_unstable_unsubscribeEvents(subscription: Cow<'a, str>) -> (), + sudo_network_unstable_watch() -> Cow<'a, str>, + sudo_network_unstable_unwatch(subscription: Cow<'a, str>) -> (), chainHead_unstable_finalizedDatabase(#[rename = "maxSizeBytes"] max_size_bytes: Option) -> Cow<'a, str>, } @@ -525,7 +525,7 @@ define_methods! { // This function is a custom addition in smoldot. As of the writing of this comment, there is // no plan to standardize it. See https://github.com/paritytech/smoldot/issues/2245. - network_unstable_event(subscription: Cow<'a, str>, result: NetworkEvent<'a>) -> (), + sudo_networkState_event(subscription: Cow<'a, str>, result: NetworkEvent) -> (), } #[derive(Clone, PartialEq, Eq, Hash)] @@ -849,90 +849,51 @@ pub struct TransactionWatchEventBlock { /// See . #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[serde(tag = "event")] -pub enum NetworkEvent<'a> { - #[serde(rename = "startConnect")] - StartConnect { - when: u64, - #[serde(rename = "connectionId")] - connection_id: u32, - multiaddr: Cow<'a, str>, - }, - #[serde(rename = "connected")] - Connected { - when: u64, +pub enum NetworkEvent { + #[serde(rename = "connectionState")] + ConnectionState { #[serde(rename = "connectionId")] connection_id: u32, - }, - #[serde(rename = "handshakeFinished")] - HandshakeFinished { + #[serde(rename = "targetPeerId", skip_serializing_if = "Option::is_none")] + target_peer_id: Option, + #[serde(rename = "targetMultiaddr")] + target_multiaddr: String, + status: NetworkEventStatus, + direction: NetworkEventDirection, when: u64, - #[serde(rename = "connectionId")] - connection_id: u32, - #[serde(rename = "peerId")] - peer_id: Cow<'a, str>, }, - #[serde(rename = "stop")] - Stop { - when: u64, - #[serde(rename = "connectionId")] - connection_id: u32, - reason: Cow<'a, str>, - }, - #[serde(rename = "out-slot-assign")] - OutSlotAssign { - when: u64, - #[serde(rename = "peerId")] - peer_id: Cow<'a, str>, - }, - #[serde(rename = "out-slot-unassign")] - OutSlotUnassign { - when: u64, - #[serde(rename = "peerId")] - peer_id: Cow<'a, str>, - }, - #[serde(rename = "in-slot-assign")] - InSlotAssign { - when: u64, - #[serde(rename = "peerId")] - peer_id: Cow<'a, str>, - }, - #[serde(rename = "in-slot-unassign")] - InSlotUnassign { - when: u64, - #[serde(rename = "peerId")] - peer_id: Cow<'a, str>, - }, - #[serde(rename = "in-slot-to-out-slot")] - InSlotToOutSlot { - when: u64, - #[serde(rename = "peerId")] - peer_id: Cow<'a, str>, - }, - #[serde(rename = "substream-out-open")] - SubstreamOutOpen { - when: u64, + #[serde(rename = "substreamState")] + SubstreamState { #[serde(rename = "connectionId")] connection_id: u32, #[serde(rename = "substreamId")] substream_id: u32, + status: NetworkEventStatus, #[serde(rename = "protocolName")] - protocol_name: Cow<'a, str>, - }, - #[serde(rename = "substream-out-accept")] - SubstreamOutAccept { - when: u64, - #[serde(rename = "substreamId")] - substream_id: u32, - }, - #[serde(rename = "substream-out-stop")] - SubstreamOutStop { + protocol_name: String, + direction: NetworkEventDirection, when: u64, - #[serde(rename = "substreamId")] - substream_id: u32, - reason: Cow<'a, str>, }, } +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub enum NetworkEventStatus { + #[serde(rename = "connecting")] + Connecting, + #[serde(rename = "open")] + Open, + #[serde(rename = "closed")] + Close, +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub enum NetworkEventDirection { + #[serde(rename = "in")] + In, + #[serde(rename = "out")] + Out, +} + #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct Header { #[serde(rename = "parentHash")] diff --git a/lib/src/json_rpc/service/client_main_task.rs b/lib/src/json_rpc/service/client_main_task.rs index f752f6ab36..e1bfc3c77a 100644 --- a/lib/src/json_rpc/service/client_main_task.rs +++ b/lib/src/json_rpc/service/client_main_task.rs @@ -471,7 +471,7 @@ impl ClientMainTask { | methods::MethodCall::state_subscribeRuntimeVersion { .. } | methods::MethodCall::state_subscribeStorage { .. } | methods::MethodCall::transaction_unstable_submitAndWatch { .. } - | methods::MethodCall::network_unstable_subscribeEvents { .. } + | methods::MethodCall::sudo_network_unstable_watch { .. } | methods::MethodCall::chainHead_unstable_follow { .. } => { // Subscription starting requests. @@ -541,9 +541,7 @@ impl ClientMainTask { | methods::MethodCall::state_unsubscribeRuntimeVersion { subscription, .. } | methods::MethodCall::state_unsubscribeStorage { subscription, .. } | methods::MethodCall::transaction_unstable_unwatch { subscription, .. } - | methods::MethodCall::network_unstable_unsubscribeEvents { - subscription, .. - } + | methods::MethodCall::sudo_network_unstable_unwatch { subscription, .. } | methods::MethodCall::chainHead_unstable_unfollow { follow_subscription: subscription, .. @@ -568,9 +566,9 @@ impl ClientMainTask { methods::MethodCall::transaction_unstable_unwatch { .. } => methods::Response::transaction_unstable_unwatch(()), - methods::MethodCall::network_unstable_unsubscribeEvents { + methods::MethodCall::sudo_network_unstable_unwatch { .. - } => methods::Response::network_unstable_unsubscribeEvents(()), + } => methods::Response::sudo_network_unstable_unwatch(()), methods::MethodCall::chainHead_unstable_unfollow { .. } => { methods::Response::chainHead_unstable_unfollow(()) } @@ -1164,10 +1162,8 @@ impl SubscriptionStartProcess { &self.subscription_id, )) } - methods::MethodCall::network_unstable_subscribeEvents { .. } => { - methods::Response::network_unstable_subscribeEvents(Cow::Borrowed( - &self.subscription_id, - )) + methods::MethodCall::sudo_network_unstable_watch { .. } => { + methods::Response::sudo_network_unstable_watch(Cow::Borrowed(&self.subscription_id)) } methods::MethodCall::chainHead_unstable_follow { .. } => { methods::Response::chainHead_unstable_follow(Cow::Borrowed(&self.subscription_id)) diff --git a/light-base/src/json_rpc_service/background.rs b/light-base/src/json_rpc_service/background.rs index 1eb0646acc..6d0f9d54f1 100644 --- a/light-base/src/json_rpc_service/background.rs +++ b/light-base/src/json_rpc_service/background.rs @@ -358,8 +358,8 @@ impl Background { | methods::MethodCall::sudo_unstable_version { .. } | methods::MethodCall::transaction_unstable_submitAndWatch { .. } | methods::MethodCall::transaction_unstable_unwatch { .. } - | methods::MethodCall::network_unstable_subscribeEvents { .. } - | methods::MethodCall::network_unstable_unsubscribeEvents { .. } + | methods::MethodCall::sudo_network_unstable_watch { .. } + | methods::MethodCall::sudo_network_unstable_unwatch { .. } | methods::MethodCall::chainHead_unstable_finalizedDatabase { .. } => {} } @@ -629,8 +629,8 @@ impl Background { | methods::MethodCall::sudo_unstable_version { .. } | methods::MethodCall::transaction_unstable_submitAndWatch { .. } | methods::MethodCall::transaction_unstable_unwatch { .. } - | methods::MethodCall::network_unstable_subscribeEvents { .. } - | methods::MethodCall::network_unstable_unsubscribeEvents { .. } + | methods::MethodCall::sudo_network_unstable_watch { .. } + | methods::MethodCall::sudo_network_unstable_unwatch { .. } | methods::MethodCall::chainHead_unstable_finalizedDatabase { .. } => {} } @@ -654,7 +654,7 @@ impl Background { self.submit_and_watch_transaction(request).await } - _method @ methods::MethodCall::network_unstable_subscribeEvents { .. } => { + _method @ methods::MethodCall::sudo_network_unstable_watch { .. } => { // TODO: implement the ones that make sense to implement ^ log!( &self.platform,