remove capability based approach

This commit is contained in:
Tadeo hepperle
2024-02-16 17:36:14 +01:00
parent bd0539a259
commit 0a471e2039
4 changed files with 2 additions and 76 deletions
-2
View File
@@ -167,5 +167,3 @@ where
};
Ok(account_nonce)
}
pub struct LatestBlockHeader {}
-72
View File
@@ -1,72 +0,0 @@
// use crate::{backend::Backend, Config};
// use super::OfflineClientT;
use crate::{Config, Error, OfflineClient, OnlineClient};
use core::future::Future;
use super::OfflineClientT;
pub type FetchBlockHeader<Header> =
Box<dyn Future<Output = Result<Header, Error>> + Send + 'static>;
/// An object-safe trait abstracting over capabilities of offline and online-clients.
///
/// Implemented by [`subxt::OfflineClient`], [`subxt::OnlineClient`] and [`subxt::LightClient`] the like.
pub trait ClientCapabilities<T: Config> {
fn latest_block_header(&self) -> Option<FetchBlockHeader<T::Header>>
where
T::Header: Clone;
}
impl<T: Config> ClientCapabilities<T> for OfflineClient<T> {
fn latest_block_header(&self) -> Option<FetchBlockHeader<T::Header>>
where
T::Header: Clone,
{
None
}
}
impl<T: Config> ClientCapabilities<T> for OnlineClient<T> {
fn latest_block_header(&self) -> Option<FetchBlockHeader<T::Header>>
where
T::Header: Clone,
{
let client = self.clone();
Some(Box::new(async move {
let block = client.blocks().at_latest().await?;
let header: T::Header = block.header().clone();
Ok(header)
}))
}
}
crate::macros::cfg_unstable_light_client! {
use super::LightClient;
impl<T: Config> ClientCapabilities<T> for LightClient<T> {
fn latest_block_header(&self) -> Option<FetchBlockHeader<T::Header>>
where
T::Header: Clone,
{
let client = self.clone();
Some(Box::new(async move {
let block = client.blocks().at_latest().await?;
let header: T::Header = block.header().clone();
Ok(header)
}))
}
}
}
#[cfg(test)]
mod tests {
use super::ClientCapabilities;
use crate::PolkadotConfig;
use core::panic;
fn is_object_safe(client: Box<dyn ClientCapabilities<PolkadotConfig>>) {
_ = client.latest_block_header();
unreachable!("Do not call this function, it just needs to compile.")
}
}
-1
View File
@@ -8,7 +8,6 @@
//! require network access. The [`OnlineClient`] requires network
//! access.
mod client_capabilities;
mod offline_client;
mod online_client;
+2 -1
View File
@@ -88,7 +88,7 @@ pub trait DefaultOrFrom<T> {
}
impl<T> DefaultOrFrom<T> for () {
fn default_or_from(_value: Option<&T>) -> () {}
fn default_or_from(_value: Option<&T>) {}
}
macro_rules! impl_default_from_tuples {
@@ -109,6 +109,7 @@ macro_rules! impl_default_from_tuples {
}
}
// Note: these implementations are necessary for AnyOf to work properly where arbitrary tuples are the `OtherParams`.
#[rustfmt::skip]
const _: () = {
impl_default_from_tuples!(A);