diff --git a/examples/examples/chainhead_subscription.rs b/examples/examples/chainhead_subscription.rs index 2e9c8e4113..fd15c649fe 100644 --- a/examples/examples/chainhead_subscription.rs +++ b/examples/examples/chainhead_subscription.rs @@ -13,6 +13,7 @@ use codec::Encode; use futures::StreamExt; use sp_keyring::AccountKeyring; +use std::time::Duration; use subxt::{ OnlineClient, PolkadotConfig, @@ -31,8 +32,14 @@ async fn main() -> Result<(), Box> { let genesis = api.rpc().chainhead_genesis_hash().await?; println!("Genesis: {:?}", genesis); - let mut follow_sub = api.blocks().subscribe_chainhead_finalized(false).await?; + // TODO: spec v2 no longer uses the `pipe_from_stream`, therefore we need some + // time between two sequencial `chainHead_follow` subscription to propagate + // the `chainHead_unfollow` from the first subscription. + tokio::time::sleep(Duration::from_secs(5)).await; + + let mut follow_sub = api.blocks().subscribe_chainhead_finalized(true).await?; println!("Following subscription..."); + // Handle all subscriptions from the `chainHead_follow`. while let Some(block) = follow_sub.next().await { let block = block?; @@ -56,7 +63,7 @@ async fn main() -> Result<(), Box> { let call = block .call("AccountNonceApi_account_nonce".into(), Some(&call_params)) .await?; - // println!("[hash={:?}] call={:?}", block.hash(), call); + println!("[hash={:?}] call={:?}", block.hash(), call); } // Subscribe to the `chainHead_follow` method. diff --git a/subxt/src/rpc/rpc.rs b/subxt/src/rpc/rpc.rs index bd6cc764d6..b555686f57 100644 --- a/subxt/src/rpc/rpc.rs +++ b/subxt/src/rpc/rpc.rs @@ -891,8 +891,7 @@ impl Rpc { .await?; println!("Dropping subscription"); - // Manually cancel the susbcription - // {"id": 1, "method": "eth_unsubscribe", "params": ["0xcd0c3e8af590364c09d0fa6a1210faf5"]} + drop(sub); let result: bool = self .client @@ -902,7 +901,6 @@ impl Rpc { ) .await?; println!("Subscription dropped with result={:?}", result); - drop(sub); (bytes, event) };