From 982e63848df460a46c4cfd4b35c418e68142b40c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 17 Aug 2022 15:34:19 +0100 Subject: [PATCH] Adapt to new subxt API (#15) * Adapt to new subxt API * Unvalidated BlockWeight storage entry * Bump version * Remove redundant error mapping --- Cargo.toml | 4 ++-- src/lib.rs | 45 +++++++++++++++++++-------------------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a4dc075..cfeb75f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blockstats" -version = "0.1.1" +version = "0.2.0" edition = "2021" rust-version = "1.56.1" authors = ["Parity Technologies "] @@ -14,7 +14,7 @@ include = ["src/**/*", "LICENSE", "README.md"] [dependencies] codec = { package = "parity-scale-codec", version = "3" } futures = "0.3" -subxt = "0.22" +subxt = "0.23" [dev-dependencies] clap = { version = "3", features = ["derive"] } diff --git a/src/lib.rs b/src/lib.rs index 89a5a2c..bfbd1c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,10 @@ use futures::{TryStream, TryStreamExt}; use std::{boxed::Box, fmt, sync::Arc}; use subxt::{ - sp_core::H256, sp_runtime::traits::Header, BasicError, Client, ClientBuilder, DefaultConfig, + ext::{sp_core::H256, sp_runtime::traits::Header}, + metadata::DecodeStaticType, + storage::{address::Yes, StaticStorageAddress}, + Error, OnlineClient, PolkadotConfig as DefaultConfig, }; /// 50% of what is stored in configuration::activeConfig::maxPovSize at the relay chain. @@ -69,19 +72,14 @@ impl fmt::Display for BlockStats { /// The `url` needs to be a websocket so that we can subscribe to new blocks. pub async fn subscribe_stats( url: &str, -) -> Result + Unpin, BasicError> { - let client: Client = ClientBuilder::new().set_url(url).build().await?; +) -> Result + Unpin, Error> { + let client = OnlineClient::::from_url(url).await?; let client = Arc::new(client); - let blocks = client - .rpc() - .subscribe_blocks() - .await? - .map_err(BasicError::from); + let blocks = client.rpc().subscribe_blocks().await?; let max_block_weights: BlockWeights = { - let locked_metadata = client.metadata(); - let metadata = locked_metadata.read(); + let metadata = client.metadata(); let pallet = metadata.pallet("System")?; let constant = pallet.constant("BlockWeights")?; codec::Decode::decode(&mut &constant.value[..])? @@ -90,16 +88,23 @@ pub async fn subscribe_stats( Ok(Box::pin(blocks.map_err(Into::into).and_then( move |block| { let client = client.clone(); - let block_weight_storage_entry = BlockWeightStorageEntry; + let block_weight_address = + StaticStorageAddress::>, Yes, Yes, ()>::new( + "System", + "BlockWeight", + vec![], + Default::default(), + ) + .unvalidated(); async move { let stats = client .rpc() .block_stats(block.hash()) .await? - .ok_or_else(|| BasicError::Other("Block not available.".to_string()))?; - let weight = client + .ok_or_else(|| Error::Other("Block not available.".to_string()))?; + let weight: PerDispatchClass = client .storage() - .fetch_or_default(&block_weight_storage_entry, Some(block.hash())) + .fetch_or_default(&block_weight_address, Some(block.hash())) .await?; let pov_len = stats.witness_len + stats.block_len; let total_weight = weight.normal + weight.operational + weight.mandatory; @@ -120,18 +125,6 @@ pub async fn subscribe_stats( ))) } -#[derive(Clone)] -struct BlockWeightStorageEntry; - -impl subxt::StorageEntry for BlockWeightStorageEntry { - const PALLET: &'static str = "System"; - const STORAGE: &'static str = "BlockWeight"; - type Value = PerDispatchClass; - fn key(&self) -> subxt::StorageEntryKey { - subxt::StorageEntryKey::Plain - } -} - #[derive(codec::Encode, codec::Decode)] struct BlockWeights { pub base_block: u64,