From 53504f5fe0ea93eb09dd8dfae825989a77ff39b9 Mon Sep 17 00:00:00 2001 From: Karol Kokoszka Date: Wed, 13 Dec 2023 22:45:15 +0100 Subject: [PATCH] subxt: update to version 0.32.1 adapt to subxt's API changes --- Cargo.toml | 4 ++-- src/lib.rs | 30 +++++++++++++++++------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2b4d992..f46c0ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blockstats" -version = "0.2.0" +version = "0.3.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.28" +subxt = { version = "0.32.1", features = ["substrate-compat"] } [dev-dependencies] clap = { version = "4", features = ["derive"] } diff --git a/src/lib.rs b/src/lib.rs index 53344d5..af6de16 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,11 +5,11 @@ use core::ops::Add; use futures::{TryStream, TryStreamExt}; -use std::{boxed::Box, fmt, sync::Arc}; +use std::{boxed::Box, fmt}; use subxt::{ ext::{scale_decode, sp_core::H256}, storage::{address::StaticStorageMapKey, address::Yes, Address}, - Error, OnlineClient, PolkadotConfig as DefaultConfig, + Error, OnlineClient, PolkadotConfig as DefaultConfig, error::MetadataError, backend::{legacy::{LegacyRpcMethods}, rpc::RpcClient} }; /// 50% of what is stored in configuration::activeConfig::maxPovSize at the relay chain. @@ -75,28 +75,33 @@ impl fmt::Display for BlockStats { pub async fn subscribe_stats( url: &str, ) -> Result + Unpin, Error> { - let client = OnlineClient::::from_url(url).await?; - subscribe_stats_with_client(client).await + let rpc_client = RpcClient::from_url(url).await?; + subscribe_stats_with_client(rpc_client).await } /// Connect to the specified node and listen for new blocks using OnlineClient. pub async fn subscribe_stats_with_client( - client: OnlineClient, + rpc_client: RpcClient, ) -> Result + Unpin, Error> { - let client = Arc::new(client); - + let client = OnlineClient::::from_rpc_client(rpc_client.clone()).await?; let blocks = client.blocks().subscribe_best().await?; let max_block_weights: BlockWeights = { let metadata = client.metadata(); - let pallet = metadata.pallet("System")?; - let constant = pallet.constant("BlockWeights")?; - codec::Decode::decode(&mut &constant.value[..])? + let pallet = metadata.pallet_by_name_err("System")?; + let constant_name = "BlockWeights"; + let constant = pallet + .constant_by_name(constant_name) + .ok_or_else(|| { + MetadataError::ConstantNameNotFound(constant_name.to_owned()) + })?; + codec::Decode::decode(&mut &constant.value()[..])? }; Ok(Box::pin(blocks.map_err(Into::into).and_then( move |block| { let client = client.clone(); + let rpc_methods = LegacyRpcMethods::::new(rpc_client.clone()); let block_weight_address = Address::, Yes, Yes, ()>::new_static( @@ -107,9 +112,8 @@ pub async fn subscribe_stats_with_client( ) .unvalidated(); async move { - let stats = client - .rpc() - .block_stats(block.hash()) + let stats = rpc_methods + .dev_get_block_stats(block.hash()) .await? .ok_or_else(|| Error::Other("Block not available.".to_string()))?; let weight = client