mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 04:01:10 +00:00
Metadata V16: Be more dynamic over which hasher is used. (#1974)
* Use DynamicHasher256 to support Blake2 or Keccack depending on chain * remove Config::Hash associated type, replace with HashFor<Config> alias * Fix doc links * fix wasm tests * Don't strip system pallet associated types. check System.Hashing, not Hash. Rename BlockHash trait to Hash * Tweak comment * fmt * fix merge * Fix typo
This commit is contained in:
@@ -6,7 +6,7 @@ use crate::{
|
||||
backend::BlockRef,
|
||||
blocks::Extrinsics,
|
||||
client::{OfflineClientT, OnlineClientT},
|
||||
config::{Config, Header},
|
||||
config::{Config, HashFor, Header},
|
||||
error::{BlockError, DecodeError, Error},
|
||||
events,
|
||||
runtime_api::RuntimeApi,
|
||||
@@ -20,7 +20,7 @@ use std::sync::Arc;
|
||||
/// A representation of a block.
|
||||
pub struct Block<T: Config, C> {
|
||||
header: T::Header,
|
||||
block_ref: BlockRef<T::Hash>,
|
||||
block_ref: BlockRef<HashFor<T>>,
|
||||
client: C,
|
||||
// Since we obtain the same events for every extrinsic, let's
|
||||
// cache them so that we only ever do that once:
|
||||
@@ -36,7 +36,7 @@ where
|
||||
T: Config,
|
||||
C: OfflineClientT<T>,
|
||||
{
|
||||
pub(crate) fn new(header: T::Header, block_ref: BlockRef<T::Hash>, client: C) -> Self {
|
||||
pub(crate) fn new(header: T::Header, block_ref: BlockRef<HashFor<T>>, client: C) -> Self {
|
||||
Block {
|
||||
header,
|
||||
block_ref,
|
||||
@@ -47,12 +47,12 @@ where
|
||||
|
||||
/// Return a reference to the given block. While this reference is kept alive,
|
||||
/// the backend will (if possible) endeavour to keep hold of the block.
|
||||
pub fn reference(&self) -> BlockRef<T::Hash> {
|
||||
pub fn reference(&self) -> BlockRef<HashFor<T>> {
|
||||
self.block_ref.clone()
|
||||
}
|
||||
|
||||
/// Return the block hash.
|
||||
pub fn hash(&self) -> T::Hash {
|
||||
pub fn hash(&self) -> HashFor<T> {
|
||||
self.block_ref.hash()
|
||||
}
|
||||
|
||||
@@ -74,12 +74,12 @@ where
|
||||
{
|
||||
/// Return the events associated with the block, fetching them from the node if necessary.
|
||||
pub async fn events(&self) -> Result<events::Events<T>, Error> {
|
||||
get_events(&self.client, self.header.hash(), &self.cached_events).await
|
||||
get_events(&self.client, self.hash(), &self.cached_events).await
|
||||
}
|
||||
|
||||
/// Fetch and return the extrinsics in the block body.
|
||||
pub async fn extrinsics(&self) -> Result<Extrinsics<T, C>, Error> {
|
||||
let block_hash = self.header.hash();
|
||||
let block_hash = self.hash();
|
||||
let Some(extrinsics) = self.client.backend().block_body(block_hash).await? else {
|
||||
return Err(BlockError::not_found(block_hash).into());
|
||||
};
|
||||
@@ -111,7 +111,7 @@ where
|
||||
// Return Events from the cache, or fetch from the node if needed.
|
||||
pub(crate) async fn get_events<C, T>(
|
||||
client: &C,
|
||||
block_hash: T::Hash,
|
||||
block_hash: HashFor<T>,
|
||||
cached_events: &AsyncMutex<Option<events::Events<T>>>,
|
||||
) -> Result<events::Events<T>, Error>
|
||||
where
|
||||
@@ -140,7 +140,7 @@ where
|
||||
pub(crate) async fn get_account_nonce<C, T>(
|
||||
client: &C,
|
||||
account_id: &T::AccountId,
|
||||
block_hash: T::Hash,
|
||||
block_hash: HashFor<T>,
|
||||
) -> Result<u64, Error>
|
||||
where
|
||||
C: OnlineClientT<T>,
|
||||
|
||||
@@ -6,7 +6,7 @@ use super::Block;
|
||||
use crate::{
|
||||
backend::{BlockRef, StreamOfResults},
|
||||
client::OnlineClientT,
|
||||
config::Config,
|
||||
config::{Config, HashFor},
|
||||
error::{BlockError, Error},
|
||||
utils::PhantomDataSendSync,
|
||||
};
|
||||
@@ -48,7 +48,7 @@ where
|
||||
/// but may run into errors attempting to work with them.
|
||||
pub fn at(
|
||||
&self,
|
||||
block_ref: impl Into<BlockRef<T::Hash>>,
|
||||
block_ref: impl Into<BlockRef<HashFor<T>>>,
|
||||
) -> impl Future<Output = Result<Block<T, Client>, Error>> + Send + 'static {
|
||||
self.at_or_latest(Some(block_ref.into()))
|
||||
}
|
||||
@@ -64,7 +64,7 @@ where
|
||||
/// provided.
|
||||
fn at_or_latest(
|
||||
&self,
|
||||
block_ref: Option<BlockRef<T::Hash>>,
|
||||
block_ref: Option<BlockRef<HashFor<T>>>,
|
||||
) -> impl Future<Output = Result<Block<T, Client>, Error>> + Send + 'static {
|
||||
let client = self.client.clone();
|
||||
async move {
|
||||
@@ -94,8 +94,9 @@ where
|
||||
Client: Send + Sync + 'static,
|
||||
{
|
||||
let client = self.client.clone();
|
||||
let hasher = client.hasher();
|
||||
header_sub_fut_to_block_sub(self.clone(), async move {
|
||||
let stream = client.backend().stream_all_block_headers().await?;
|
||||
let stream = client.backend().stream_all_block_headers(hasher).await?;
|
||||
BlockStreamRes::Ok(stream)
|
||||
})
|
||||
}
|
||||
@@ -111,8 +112,9 @@ where
|
||||
Client: Send + Sync + 'static,
|
||||
{
|
||||
let client = self.client.clone();
|
||||
let hasher = client.hasher();
|
||||
header_sub_fut_to_block_sub(self.clone(), async move {
|
||||
let stream = client.backend().stream_best_block_headers().await?;
|
||||
let stream = client.backend().stream_best_block_headers(hasher).await?;
|
||||
BlockStreamRes::Ok(stream)
|
||||
})
|
||||
}
|
||||
@@ -125,8 +127,12 @@ where
|
||||
Client: Send + Sync + 'static,
|
||||
{
|
||||
let client = self.client.clone();
|
||||
let hasher = client.hasher();
|
||||
header_sub_fut_to_block_sub(self.clone(), async move {
|
||||
let stream = client.backend().stream_finalized_block_headers().await?;
|
||||
let stream = client
|
||||
.backend()
|
||||
.stream_finalized_block_headers(hasher)
|
||||
.await?;
|
||||
BlockStreamRes::Ok(stream)
|
||||
})
|
||||
}
|
||||
@@ -140,7 +146,9 @@ async fn header_sub_fut_to_block_sub<T, Client, S>(
|
||||
) -> Result<BlockStream<Block<T, Client>>, Error>
|
||||
where
|
||||
T: Config,
|
||||
S: Future<Output = Result<BlockStream<(T::Header, BlockRef<T::Hash>)>, Error>> + Send + 'static,
|
||||
S: Future<Output = Result<BlockStream<(T::Header, BlockRef<HashFor<T>>)>, Error>>
|
||||
+ Send
|
||||
+ 'static,
|
||||
Client: OnlineClientT<T> + Send + Sync + 'static,
|
||||
{
|
||||
let sub = sub.await?.then(move |header_and_ref| {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
use crate::{
|
||||
blocks::block_types::{get_events, CachedEvents},
|
||||
client::{OfflineClientT, OnlineClientT},
|
||||
config::Config,
|
||||
config::{Config, HashFor},
|
||||
error::Error,
|
||||
events,
|
||||
};
|
||||
@@ -25,7 +25,7 @@ pub struct Extrinsics<T: Config, C> {
|
||||
inner: CoreExtrinsics<T>,
|
||||
client: C,
|
||||
cached_events: CachedEvents<T>,
|
||||
hash: T::Hash,
|
||||
hash: HashFor<T>,
|
||||
}
|
||||
|
||||
impl<T, C> Extrinsics<T, C>
|
||||
@@ -37,7 +37,7 @@ where
|
||||
client: C,
|
||||
extrinsics: Vec<Vec<u8>>,
|
||||
cached_events: CachedEvents<T>,
|
||||
hash: T::Hash,
|
||||
hash: HashFor<T>,
|
||||
) -> Result<Self, Error> {
|
||||
let inner = CoreExtrinsics::decode_from(extrinsics, client.metadata())?;
|
||||
Ok(Self {
|
||||
@@ -59,7 +59,7 @@ where
|
||||
}
|
||||
|
||||
/// Return the block hash that these extrinsics are from.
|
||||
pub fn block_hash(&self) -> T::Hash {
|
||||
pub fn block_hash(&self) -> HashFor<T> {
|
||||
self.hash
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ where
|
||||
pub struct ExtrinsicDetails<T: Config, C> {
|
||||
inner: CoreExtrinsicDetails<T>,
|
||||
/// The block hash of this extrinsic (needed to fetch events).
|
||||
block_hash: T::Hash,
|
||||
block_hash: HashFor<T>,
|
||||
/// Subxt client.
|
||||
client: C,
|
||||
/// Cached events.
|
||||
@@ -141,7 +141,7 @@ where
|
||||
pub(crate) fn new(
|
||||
inner: CoreExtrinsicDetails<T>,
|
||||
client: C,
|
||||
block_hash: T::Hash,
|
||||
block_hash: HashFor<T>,
|
||||
cached_events: CachedEvents<T>,
|
||||
) -> ExtrinsicDetails<T, C> {
|
||||
ExtrinsicDetails {
|
||||
@@ -153,7 +153,7 @@ where
|
||||
}
|
||||
|
||||
/// See [`subxt_core::blocks::ExtrinsicDetails::hash()`].
|
||||
pub fn hash(&self) -> T::Hash {
|
||||
pub fn hash(&self) -> HashFor<T> {
|
||||
self.inner.hash()
|
||||
}
|
||||
|
||||
@@ -271,7 +271,7 @@ pub struct ExtrinsicEvents<T: Config> {
|
||||
// this type is returned from TxProgress things in the most
|
||||
// basic flows, so it's the only place people can access it
|
||||
// without complicating things for themselves).
|
||||
ext_hash: T::Hash,
|
||||
ext_hash: HashFor<T>,
|
||||
// The index of the extrinsic:
|
||||
idx: u32,
|
||||
// All of the events in the block:
|
||||
@@ -281,7 +281,7 @@ pub struct ExtrinsicEvents<T: Config> {
|
||||
impl<T: Config> ExtrinsicEvents<T> {
|
||||
/// Creates a new instance of `ExtrinsicEvents`.
|
||||
#[doc(hidden)]
|
||||
pub fn new(ext_hash: T::Hash, idx: u32, events: events::Events<T>) -> Self {
|
||||
pub fn new(ext_hash: HashFor<T>, idx: u32, events: events::Events<T>) -> Self {
|
||||
Self {
|
||||
ext_hash,
|
||||
idx,
|
||||
@@ -295,7 +295,7 @@ impl<T: Config> ExtrinsicEvents<T> {
|
||||
}
|
||||
|
||||
/// Return the hash of the extrinsic.
|
||||
pub fn extrinsic_hash(&self) -> T::Hash {
|
||||
pub fn extrinsic_hash(&self) -> HashFor<T> {
|
||||
self.ext_hash
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user