add onlineclient.at_block_hash_and_number for the event that you know both.

This commit is contained in:
James Wilson
2025-12-03 11:10:42 +00:00
parent 0a617eb2a9
commit c1ebceb965
3 changed files with 20 additions and 9 deletions
+2 -7
View File
@@ -6,13 +6,8 @@ use crate::transactions::Transactions;
use core::marker::PhantomData;
use subxt_metadata::Metadata;
// We keep these traits internal, so that we can mess with them later if needed,
// and instead only the concrete types are public which wrap these trait impls.
pub(crate) use offline_client::OfflineClientAtBlockT;
pub(crate) use online_client::OnlineClientAtBlockT;
pub use offline_client::{OfflineClient, OfflineClientAtBlock};
pub use online_client::{OnlineClient, OnlineClientAtBlock};
pub use offline_client::{OfflineClient, OfflineClientAtBlock, OfflineClientAtBlockT};
pub use online_client::{OnlineClient, OnlineClientAtBlock, OnlineClientAtBlockT};
/// This represents a client at a specific block number.
#[derive(Clone, Debug)]
+17 -1
View File
@@ -9,8 +9,8 @@ use crate::error::{BlocksError, OnlineClientAtBlockError};
use blocks::Blocks;
use codec::{Compact, Decode, Encode};
use core::marker::PhantomData;
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
use frame_decode::helpers::ToTypeRegistry;
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
use scale_info_legacy::TypeRegistrySet;
use std::sync::Arc;
use subxt_metadata::Metadata;
@@ -260,6 +260,22 @@ impl<T: Config> OnlineClient<T> {
(block_ref, block_number)
}
};
self.at_block_hash_and_number(block_ref, block_number).await
}
/// Instantiate a client for working at a specific block. This takes a block hash/ref _and_ the
/// corresponding block number. When both are available, this saves an RPC call to obtain one from
/// the other.
///
/// **Warning:** If the block hash and number do not align, then things will go wrong. Prefer to
/// use [`Self::at_block`] if in any doubt.
pub async fn at_block_hash_and_number(
&self,
block_ref: impl Into<BlockRef<HashFor<T>>>,
block_number: u64,
) -> Result<ClientAtBlock<OnlineClientAtBlock<T>, T>, OnlineClientAtBlockError> {
let block_ref = block_ref.into();
let block_hash = block_ref.hash();
// Obtain the spec version so that we know which metadata to use at this block.
+1 -1
View File
@@ -362,7 +362,7 @@ pub enum OnlineClientAtBlockError {
metadata_version: u32,
/// Reason the conversion failed.
reason: subxt_metadata::TryFromError,
},
},
#[error(
"Cannot construct OnlineClientAtBlock: cannot inject types from metadata: failure to parse a type found in the metadata: {parse_error}"
)]