From e7192f350a7a1b3aa2bf7bac09a622a02cefe437 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 30 Nov 2022 13:09:52 +0000 Subject: [PATCH] rpc: Fetch chainHead genesis hash Signed-off-by: Alexandru Vasile --- examples/examples/chainhead_subscription.rs | 3 +++ subxt/src/blocks/block_types.rs | 6 ------ subxt/src/rpc/rpc.rs | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/examples/examples/chainhead_subscription.rs b/examples/examples/chainhead_subscription.rs index 530bbe7460..ded14edf68 100644 --- a/examples/examples/chainhead_subscription.rs +++ b/examples/examples/chainhead_subscription.rs @@ -28,6 +28,9 @@ async fn main() -> Result<(), Box> { // Create a client to use: let api = OnlineClient::::new().await?; + let genesis = api.rpc().get_chainhead_genesis_hash().await?; + println!("Genesis: {:?}", genesis); + let mut follow_sub = api.blocks().subscribe_chainhead_finalized(true).await?; // Handle all subscriptions from the `chainHead_follow`. while let Some(block) = follow_sub.next().await { diff --git a/subxt/src/blocks/block_types.rs b/subxt/src/blocks/block_types.rs index 0c407c8449..42bae3c196 100644 --- a/subxt/src/blocks/block_types.rs +++ b/subxt/src/blocks/block_types.rs @@ -81,8 +81,6 @@ where if let Some(event) = sub.next().await { let event = event?; - println!("Got event: {:?}", event); - return match event { ChainHeadEvent::Done(ChainHeadResult { result }) => { let bytes = hex::decode(result.trim_start_matches("0x")) @@ -148,8 +146,6 @@ where if let Some(event) = sub.next().await { let event = event?; - println!("Got event: {:?}", event); - return match event { ChainHeadEvent::Done(ChainHeadResult { result }) => { let result = match result { @@ -195,8 +191,6 @@ where if let Some(event) = sub.next().await { let event = event?; - println!("Got event: {:?}", event); - return match event { ChainHeadEvent::Done(ChainHeadResult { result }) => { let bytes = hex::decode(result.trim_start_matches("0x")) diff --git a/subxt/src/rpc/rpc.rs b/subxt/src/rpc/rpc.rs index e787adbbf6..38c5c8c350 100644 --- a/subxt/src/rpc/rpc.rs +++ b/subxt/src/rpc/rpc.rs @@ -662,6 +662,21 @@ impl Rpc { Ok(header) } + /// Get the chain head genesis hash. + pub async fn get_chainhead_genesis_hash( + &self, + ) -> Result { + let hash = self + .client + .request( + "chainHead_unstable_genesisHash", + rpc_params![], + ) + .await?; + + Ok(hash) + } + /// Subscribe to the chain head storage. pub async fn subscribe_chainhead_storage( &self,