Consolidate C and I generics into a TendermintClient trait alias

This commit is contained in:
Luke Parker 2022-10-24 04:43:25 -04:00
parent 4859e8c27e
commit b6dddc469f
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
5 changed files with 71 additions and 48 deletions

View file

@ -1,32 +1,34 @@
use std::collections::HashMap;
use std::{sync::Arc, collections::HashMap};
use async_trait::async_trait;
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::Block;
use sp_blockchain::HeaderBackend;
use sp_api::{TransactionFor, ProvideRuntimeApi};
use sp_api::TransactionFor;
use sp_consensus::{Error, CacheKeyId, Environment};
use sc_consensus::{BlockCheckParams, BlockImportParams, ImportResult, BlockImport};
use sc_client_api::{Backend, Finalizer};
use sc_client_api::Backend;
use crate::{tendermint::TendermintImport, Announce};
use crate::{
tendermint::{TendermintClient, TendermintImport},
Announce,
};
#[async_trait]
impl<
B: Block,
Be: Backend<B> + 'static,
C: Send + Sync + HeaderBackend<B> + Finalizer<B, Be> + ProvideRuntimeApi<B> + 'static,
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
C: TendermintClient<B, Be>,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
> BlockImport<B> for TendermintImport<B, Be, C, I, CIDP, E, A>
> BlockImport<B> for TendermintImport<B, Be, C, CIDP, E, A>
where
I::Error: Into<Error>,
TransactionFor<C, B>: Send + Sync + 'static,
Arc<C>: BlockImport<B, Transaction = TransactionFor<C, B>>,
<Arc<C> as BlockImport<B>>::Error: Into<Error>,
{
type Error = Error;
type Transaction = TransactionFor<C, B>;
@ -45,7 +47,7 @@ where
block.allow_missing_state = false;
block.allow_missing_parent = false;
self.inner.write().await.check_block(block).await.map_err(Into::into)
self.client.check_block(block).await.map_err(Into::into)
}
async fn import_block(
@ -54,6 +56,6 @@ where
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
self.check(&mut block).await?;
self.inner.write().await.import_block(block, new_cache).await.map_err(Into::into)
self.client.import_block(block, new_cache).await.map_err(Into::into)
}
}

View file

@ -1,27 +1,29 @@
use std::{
pin::Pin,
sync::{Arc, RwLock},
task::{Poll, /* Wake, Waker, */ Context},
task::{Poll, Context},
future::Future,
time::SystemTime,
};
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::{Header, Block};
use sp_blockchain::HeaderBackend;
use sp_api::{BlockId, TransactionFor, ProvideRuntimeApi};
use sp_api::{BlockId, TransactionFor};
use sp_consensus::{Error, Environment};
use sc_consensus::{BlockImport, BlockImportStatus, BlockImportError, Link, BasicQueue};
use sc_consensus::{BlockImportStatus, BlockImportError, BlockImport, Link, BasicQueue};
use sc_service::ImportQueue;
use sc_client_api::{Backend, Finalizer};
use sc_client_api::Backend;
use substrate_prometheus_endpoint::Registry;
use tendermint_machine::{ext::BlockNumber, TendermintMachine};
use crate::{tendermint::TendermintImport, Announce};
use crate::{
tendermint::{TendermintClient, TendermintImport},
Announce,
};
pub type TendermintImportQueue<Block, Transaction> = BasicQueue<Block, Transaction>;
@ -74,14 +76,12 @@ impl<'a, B: Block, T: Send> Future for ImportFuture<'a, B, T> {
pub fn import_queue<
B: Block,
Be: Backend<B> + 'static,
C: Send + Sync + HeaderBackend<B> + Finalizer<B, Be> + ProvideRuntimeApi<B> + 'static,
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
C: TendermintClient<B, Be>,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
>(
client: Arc<C>,
inner: I,
announce: A,
providers: Arc<CIDP>,
env: E,
@ -89,10 +89,11 @@ pub fn import_queue<
registry: Option<&Registry>,
) -> (impl Future<Output = ()>, TendermintImportQueue<B, TransactionFor<C, B>>)
where
I::Error: Into<Error>,
TransactionFor<C, B>: Send + Sync + 'static,
Arc<C>: BlockImport<B, Transaction = TransactionFor<C, B>>,
<Arc<C> as BlockImport<B>>::Error: Into<Error>,
{
let import = TendermintImport::new(client, inner, announce, providers, env);
let import = TendermintImport::new(client, announce, providers, env);
let authority = {
let machine_clone = import.machine.clone();

View file

@ -56,7 +56,6 @@ pub fn import_queue<A: Announce<Block>>(
registry: Option<&Registry>,
) -> (impl Future<Output = ()>, TendermintImportQueue<Block, TransactionFor<FullClient, Block>>) {
import_queue::import_queue(
client.clone(),
client.clone(),
announce,
Arc::new(|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) }),

View file

@ -39,11 +39,34 @@ use crate::{
Announce,
};
pub trait TendermintClient<B: Block, Be: Backend<B> + 'static>:
Send
+ Sync
+ HeaderBackend<B>
+ BlockImport<B, Transaction = TransactionFor<Self, B>>
+ Finalizer<B, Be>
+ ProvideRuntimeApi<B>
+ 'static
{
}
impl<
B: Send + Sync + Block + 'static,
Be: Send + Sync + Backend<B> + 'static,
C: Send
+ Sync
+ HeaderBackend<B>
+ BlockImport<B, Transaction = TransactionFor<C, B>>
+ Finalizer<B, Be>
+ ProvideRuntimeApi<B>
+ 'static,
> TendermintClient<B, Be> for C
{
}
pub(crate) struct TendermintImport<
B: Block,
Be: Backend<B> + 'static,
C: Send + Sync + HeaderBackend<B> + Finalizer<B, Be> + ProvideRuntimeApi<B> + 'static,
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
C: TendermintClient<B, Be>,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
@ -57,7 +80,6 @@ pub(crate) struct TendermintImport<
pub(crate) machine: Arc<RwLock<Option<TendermintHandle<Self>>>>,
pub(crate) client: Arc<C>,
pub(crate) inner: Arc<AsyncRwLock<I>>,
announce: A,
providers: Arc<CIDP>,
@ -68,12 +90,11 @@ pub(crate) struct TendermintImport<
impl<
B: Block,
Be: Backend<B> + 'static,
C: Send + Sync + HeaderBackend<B> + Finalizer<B, Be> + ProvideRuntimeApi<B> + 'static,
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
C: TendermintClient<B, Be>,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
> Clone for TendermintImport<B, Be, C, I, CIDP, E, A>
> Clone for TendermintImport<B, Be, C, CIDP, E, A>
where
TransactionFor<C, B>: Send + Sync + 'static,
{
@ -86,7 +107,6 @@ where
machine: self.machine.clone(),
client: self.client.clone(),
inner: self.inner.clone(),
announce: self.announce.clone(),
providers: self.providers.clone(),
@ -99,22 +119,20 @@ where
impl<
B: Block,
Be: Backend<B> + 'static,
C: Send + Sync + HeaderBackend<B> + Finalizer<B, Be> + ProvideRuntimeApi<B> + 'static,
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
C: TendermintClient<B, Be>,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
> TendermintImport<B, Be, C, I, CIDP, E, A>
> TendermintImport<B, Be, C, CIDP, E, A>
where
TransactionFor<C, B>: Send + Sync + 'static,
{
pub(crate) fn new(
client: Arc<C>,
inner: I,
announce: A,
providers: Arc<CIDP>,
env: E,
) -> TendermintImport<B, Be, C, I, CIDP, E, A> {
) -> TendermintImport<B, Be, C, CIDP, E, A> {
TendermintImport {
_block: PhantomData,
_backend: PhantomData,
@ -123,7 +141,6 @@ where
machine: Arc::new(RwLock::new(None)),
client,
inner: Arc::new(AsyncRwLock::new(inner)),
announce,
providers,
@ -284,12 +301,11 @@ where
impl<
B: Block,
Be: Backend<B> + 'static,
C: Send + Sync + HeaderBackend<B> + Finalizer<B, Be> + ProvideRuntimeApi<B> + 'static,
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
C: TendermintClient<B, Be>,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
> Network for TendermintImport<B, Be, C, I, CIDP, E, A>
> Network for TendermintImport<B, Be, C, CIDP, E, A>
where
TransactionFor<C, B>: Send + Sync + 'static,
{

View file

@ -1,29 +1,34 @@
use std::sync::Arc;
use async_trait::async_trait;
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::Block;
use sp_blockchain::HeaderBackend;
use sp_api::{TransactionFor, ProvideRuntimeApi};
use sp_api::TransactionFor;
use sp_consensus::{CacheKeyId, Environment};
use sc_consensus::{BlockImportParams, Verifier, BlockImport};
use sp_consensus::{Error, CacheKeyId, Environment};
use sc_consensus::{BlockImportParams, BlockImport, Verifier};
use sc_client_api::{Backend, Finalizer};
use sc_client_api::Backend;
use crate::{tendermint::TendermintImport, Announce};
use crate::{
tendermint::{TendermintClient, TendermintImport},
Announce,
};
#[async_trait]
impl<
B: Block,
Be: Backend<B> + 'static,
C: Send + Sync + HeaderBackend<B> + Finalizer<B, Be> + ProvideRuntimeApi<B> + 'static,
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
C: TendermintClient<B, Be>,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
> Verifier<B> for TendermintImport<B, Be, C, I, CIDP, E, A>
> Verifier<B> for TendermintImport<B, Be, C, CIDP, E, A>
where
TransactionFor<C, B>: Send + Sync + 'static,
Arc<C>: BlockImport<B, Transaction = TransactionFor<C, B>>,
<Arc<C> as BlockImport<B>>::Error: Into<Error>,
{
async fn verify(
&mut self,