mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 17:21:08 +00:00
Add a method to return the block header at a given block
This commit is contained in:
@@ -4,6 +4,7 @@ mod online_client;
|
|||||||
use crate::config::{Config, HashFor};
|
use crate::config::{Config, HashFor};
|
||||||
use crate::constants::ConstantsClient;
|
use crate::constants::ConstantsClient;
|
||||||
use crate::custom_values::CustomValuesClient;
|
use crate::custom_values::CustomValuesClient;
|
||||||
|
use crate::error::BlockError;
|
||||||
use crate::events::EventsClient;
|
use crate::events::EventsClient;
|
||||||
use crate::extrinsics::ExtrinsicsClient;
|
use crate::extrinsics::ExtrinsicsClient;
|
||||||
use crate::runtime_apis::RuntimeApisClient;
|
use crate::runtime_apis::RuntimeApisClient;
|
||||||
@@ -105,4 +106,22 @@ where
|
|||||||
pub fn block_hash(&self) -> HashFor<T> {
|
pub fn block_hash(&self) -> HashFor<T> {
|
||||||
self.client.block_hash()
|
self.client.block_hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The header for this block.
|
||||||
|
pub async fn block_header(&self) -> Result<T::Header, BlockError> {
|
||||||
|
let block_hash = self.block_hash();
|
||||||
|
let header = self
|
||||||
|
.client
|
||||||
|
.backend()
|
||||||
|
.block_header(block_hash)
|
||||||
|
.await
|
||||||
|
.map_err(|e| BlockError::CouldNotDownloadBlockHeader {
|
||||||
|
block_hash: block_hash.into(),
|
||||||
|
reason: e,
|
||||||
|
})?
|
||||||
|
.ok_or_else(|| BlockError::BlockNotFound {
|
||||||
|
block_hash: block_hash.into(),
|
||||||
|
})?;
|
||||||
|
Ok(header)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ pub enum Error {
|
|||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
ExtrinsicDecodeErrorAt(#[from] ExtrinsicDecodeErrorAt),
|
ExtrinsicDecodeErrorAt(#[from] ExtrinsicDecodeErrorAt),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
|
BlockError(#[from] BlockError),
|
||||||
|
#[error(transparent)]
|
||||||
ConstantError(#[from] ConstantError),
|
ConstantError(#[from] ConstantError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
CustomValueError(#[from] CustomValueError),
|
CustomValueError(#[from] CustomValueError),
|
||||||
@@ -147,6 +149,7 @@ impl Error {
|
|||||||
Error::OnlineClientError(e) => e.backend_error(),
|
Error::OnlineClientError(e) => e.backend_error(),
|
||||||
Error::RuntimeApiError(e) => e.backend_error(),
|
Error::RuntimeApiError(e) => e.backend_error(),
|
||||||
Error::EventsError(e) => e.backend_error(),
|
Error::EventsError(e) => e.backend_error(),
|
||||||
|
Error::BlockError(e) => e.backend_error(),
|
||||||
Error::ExtrinsicError(e) => e.backend_error(),
|
Error::ExtrinsicError(e) => e.backend_error(),
|
||||||
Error::ViewFunctionError(e) => e.backend_error(),
|
Error::ViewFunctionError(e) => e.backend_error(),
|
||||||
Error::TransactionProgressError(e) => e.backend_error(),
|
Error::TransactionProgressError(e) => e.backend_error(),
|
||||||
@@ -384,6 +387,28 @@ impl OnlineClientAtBlockError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
pub enum BlockError {
|
||||||
|
#[error("Could not find the block with hash {block_hash}")]
|
||||||
|
BlockNotFound { block_hash: Hex },
|
||||||
|
#[error("Could not download the block header with hash {block_hash}: {reason}")]
|
||||||
|
CouldNotDownloadBlockHeader {
|
||||||
|
block_hash: Hex,
|
||||||
|
reason: BackendError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BlockError {
|
||||||
|
fn backend_error(&self) -> Option<&BackendError> {
|
||||||
|
match self {
|
||||||
|
BlockError::CouldNotDownloadBlockHeader { reason, .. } => Some(reason),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
|
|||||||
Reference in New Issue
Block a user