From 93c0029c75571ef1316867c212dcb00d792f7ff3 Mon Sep 17 00:00:00 2001
From: Pavlo Khrystenko
Date: Wed, 29 May 2024 14:42:11 +0200
Subject: [PATCH 1/3] Ensures poll order wrt subscription ID's
closes #1567
---
Cargo.lock | 30 +++++------
.../backend/unstable/follow_stream_driver.rs | 54 ++++++++++++++-----
subxt/src/backend/unstable/mod.rs | 38 +++++++++++--
subxt/src/backend/utils.rs | 16 ------
4 files changed, 89 insertions(+), 49 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index d7510c044e..aacc922a3d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -327,7 +327,7 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]]
name = "artifacts"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"substrate-runner",
]
@@ -1812,7 +1812,7 @@ dependencies = [
[[package]]
name = "generate-custom-metadata"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"frame-metadata 16.0.0",
"parity-scale-codec",
@@ -2309,7 +2309,7 @@ dependencies = [
[[package]]
name = "integration-tests"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"assert_matches",
"cfg_aliases",
@@ -4745,7 +4745,7 @@ dependencies = [
[[package]]
name = "substrate-runner"
-version = "0.36.1"
+version = "0.37.0"
[[package]]
name = "subtle"
@@ -4755,7 +4755,7 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "subxt"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"assert_matches",
"async-trait",
@@ -4798,7 +4798,7 @@ dependencies = [
[[package]]
name = "subxt-cli"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"clap 4.5.4",
"color-eyre",
@@ -4827,7 +4827,7 @@ dependencies = [
[[package]]
name = "subxt-codegen"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"frame-metadata 16.0.0",
"getrandom",
@@ -4847,7 +4847,7 @@ dependencies = [
[[package]]
name = "subxt-core"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"assert_matches",
"base58",
@@ -4879,7 +4879,7 @@ dependencies = [
[[package]]
name = "subxt-lightclient"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"futures",
"futures-timer",
@@ -4904,7 +4904,7 @@ dependencies = [
[[package]]
name = "subxt-macro"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"darling 0.20.8",
"parity-scale-codec",
@@ -4917,7 +4917,7 @@ dependencies = [
[[package]]
name = "subxt-metadata"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"assert_matches",
"bitvec",
@@ -4931,7 +4931,7 @@ dependencies = [
[[package]]
name = "subxt-signer"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"bip32",
"bip39",
@@ -4958,7 +4958,7 @@ dependencies = [
[[package]]
name = "subxt-test-macro"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"quote",
"syn 2.0.60",
@@ -5021,7 +5021,7 @@ dependencies = [
[[package]]
name = "test-runtime"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"hex",
"impl-serde",
@@ -5446,7 +5446,7 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "ui-tests"
-version = "0.36.1"
+version = "0.37.0"
dependencies = [
"frame-metadata 16.0.0",
"generate-custom-metadata",
diff --git a/subxt/src/backend/unstable/follow_stream_driver.rs b/subxt/src/backend/unstable/follow_stream_driver.rs
index a9bd508990..aab13d521e 100644
--- a/subxt/src/backend/unstable/follow_stream_driver.rs
+++ b/subxt/src/backend/unstable/follow_stream_driver.rs
@@ -72,7 +72,12 @@ pub struct FollowStreamDriverHandle {
impl FollowStreamDriverHandle {
/// Subscribe to follow events.
pub fn subscribe(&self) -> FollowStreamDriverSubscription {
- self.shared.subscribe()
+ self.shared.subscribe(true)
+ }
+
+ /// Returns if Followstream has reconnected
+ pub async fn reconnected(&self) {
+ self.shared.subscribe(false).reconnected().await;
}
}
@@ -137,6 +142,24 @@ impl FollowStreamDriverSubscription {
}
}
+ /// Returns if the backend has reconnected
+ pub async fn reconnected(self) -> bool {
+ let ready_event = self
+ .skip_while(|ev| {
+ std::future::ready(!matches!(
+ ev,
+ FollowStreamMsg::Event(FollowEvent::Initialized(_))
+ ))
+ })
+ .next()
+ .await;
+
+ matches!(
+ ready_event,
+ Some(FollowStreamMsg::Event(FollowEvent::Initialized(_)))
+ )
+ }
+
/// Subscribe to the follow events, ignoring any other messages.
pub fn events(self) -> impl Stream- >> + Send + Sync {
self.filter_map(|ev| std::future::ready(ev.into_event()))
@@ -145,7 +168,7 @@ impl FollowStreamDriverSubscription {
impl Clone for FollowStreamDriverSubscription {
fn clone(&self) -> Self {
- self.shared.subscribe()
+ self.shared.subscribe(true)
}
}
@@ -330,7 +353,10 @@ impl Shared {
}
/// Create a new subscription.
- pub fn subscribe(&self) -> FollowStreamDriverSubscription {
+ pub fn subscribe(
+ &self,
+ insert_subscription_data: bool,
+ ) -> FollowStreamDriverSubscription {
let mut shared = self.0.lock().unwrap();
let id = shared.next_id;
@@ -349,16 +375,18 @@ impl Shared {
// it means the subscription is currently stopped, and we should expect new Ready/Init
// messages anyway once it restarts.
let mut local_items = VecDeque::new();
- if let Some(sub_id) = &shared.current_subscription_id {
- local_items.push_back(FollowStreamMsg::Ready(sub_id.clone()));
- }
- if let Some(init_msg) = &shared.current_init_message {
- local_items.push_back(FollowStreamMsg::Event(FollowEvent::Initialized(
- init_msg.clone(),
- )));
- }
- for ev in &shared.block_events_for_new_subscriptions {
- local_items.push_back(FollowStreamMsg::Event(ev.clone()));
+ if insert_subscription_data {
+ if let Some(sub_id) = &shared.current_subscription_id {
+ local_items.push_back(FollowStreamMsg::Ready(sub_id.clone()));
+ }
+ if let Some(init_msg) = &shared.current_init_message {
+ local_items.push_back(FollowStreamMsg::Event(FollowEvent::Initialized(
+ init_msg.clone(),
+ )));
+ }
+ for ev in &shared.block_events_for_new_subscriptions {
+ local_items.push_back(FollowStreamMsg::Event(ev.clone()));
+ }
}
drop(shared);
diff --git a/subxt/src/backend/unstable/mod.rs b/subxt/src/backend/unstable/mod.rs
index ead0af1d9e..7a6b9ca765 100644
--- a/subxt/src/backend/unstable/mod.rs
+++ b/subxt/src/backend/unstable/mod.rs
@@ -32,7 +32,7 @@ use crate::Config;
use async_trait::async_trait;
use follow_stream_driver::{FollowStreamDriver, FollowStreamDriverHandle};
use futures::future::Either;
-use futures::{Stream, StreamExt};
+use futures::{pin_mut, Future, FutureExt, Stream, StreamExt};
use std::collections::HashMap;
use std::task::Poll;
use storage_items::StorageItems;
@@ -302,7 +302,7 @@ impl Backend for UnstableBackend {
}
async fn block_header(&self, at: T::Hash) -> Result