From b6dddc469f0a0b7501002f1d89d6dc730e73e009 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Mon, 24 Oct 2022 04:43:25 -0400 Subject: [PATCH] Consolidate C and I generics into a TendermintClient trait alias --- substrate/consensus/src/block_import.rs | 24 +++++++------ substrate/consensus/src/import_queue.rs | 23 ++++++------ substrate/consensus/src/lib.rs | 1 - substrate/consensus/src/tendermint.rs | 48 ++++++++++++++++--------- substrate/consensus/src/verifier.rs | 23 +++++++----- 5 files changed, 71 insertions(+), 48 deletions(-) diff --git a/substrate/consensus/src/block_import.rs b/substrate/consensus/src/block_import.rs index 0ae477d7..69d088b6 100644 --- a/substrate/consensus/src/block_import.rs +++ b/substrate/consensus/src/block_import.rs @@ -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 + 'static, - C: Send + Sync + HeaderBackend + Finalizer + ProvideRuntimeApi + 'static, - I: Send + Sync + BlockImport> + 'static, + C: TendermintClient, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, A: Announce, - > BlockImport for TendermintImport + > BlockImport for TendermintImport where - I::Error: Into, TransactionFor: Send + Sync + 'static, + Arc: BlockImport>, + as BlockImport>::Error: Into, { type Error = Error; type Transaction = TransactionFor; @@ -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>, ) -> Result { 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) } } diff --git a/substrate/consensus/src/import_queue.rs b/substrate/consensus/src/import_queue.rs index 20139c7d..64cefaa7 100644 --- a/substrate/consensus/src/import_queue.rs +++ b/substrate/consensus/src/import_queue.rs @@ -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 = BasicQueue; @@ -74,14 +76,12 @@ impl<'a, B: Block, T: Send> Future for ImportFuture<'a, B, T> { pub fn import_queue< B: Block, Be: Backend + 'static, - C: Send + Sync + HeaderBackend + Finalizer + ProvideRuntimeApi + 'static, - I: Send + Sync + BlockImport> + 'static, + C: TendermintClient, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, A: Announce, >( client: Arc, - inner: I, announce: A, providers: Arc, env: E, @@ -89,10 +89,11 @@ pub fn import_queue< registry: Option<&Registry>, ) -> (impl Future, TendermintImportQueue>) where - I::Error: Into, TransactionFor: Send + Sync + 'static, + Arc: BlockImport>, + as BlockImport>::Error: Into, { - 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(); diff --git a/substrate/consensus/src/lib.rs b/substrate/consensus/src/lib.rs index 57c1d27a..99c1f2c8 100644 --- a/substrate/consensus/src/lib.rs +++ b/substrate/consensus/src/lib.rs @@ -56,7 +56,6 @@ pub fn import_queue>( registry: Option<&Registry>, ) -> (impl Future, TendermintImportQueue>) { import_queue::import_queue( - client.clone(), client.clone(), announce, Arc::new(|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) }), diff --git a/substrate/consensus/src/tendermint.rs b/substrate/consensus/src/tendermint.rs index 711a9cfa..4193a255 100644 --- a/substrate/consensus/src/tendermint.rs +++ b/substrate/consensus/src/tendermint.rs @@ -39,11 +39,34 @@ use crate::{ Announce, }; +pub trait TendermintClient + 'static>: + Send + + Sync + + HeaderBackend + + BlockImport> + + Finalizer + + ProvideRuntimeApi + + 'static +{ +} +impl< + B: Send + Sync + Block + 'static, + Be: Send + Sync + Backend + 'static, + C: Send + + Sync + + HeaderBackend + + BlockImport> + + Finalizer + + ProvideRuntimeApi + + 'static, + > TendermintClient for C +{ +} + pub(crate) struct TendermintImport< B: Block, Be: Backend + 'static, - C: Send + Sync + HeaderBackend + Finalizer + ProvideRuntimeApi + 'static, - I: Send + Sync + BlockImport> + 'static, + C: TendermintClient, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, A: Announce, @@ -57,7 +80,6 @@ pub(crate) struct TendermintImport< pub(crate) machine: Arc>>>, pub(crate) client: Arc, - pub(crate) inner: Arc>, announce: A, providers: Arc, @@ -68,12 +90,11 @@ pub(crate) struct TendermintImport< impl< B: Block, Be: Backend + 'static, - C: Send + Sync + HeaderBackend + Finalizer + ProvideRuntimeApi + 'static, - I: Send + Sync + BlockImport> + 'static, + C: TendermintClient, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, A: Announce, - > Clone for TendermintImport + > Clone for TendermintImport where TransactionFor: 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 + 'static, - C: Send + Sync + HeaderBackend + Finalizer + ProvideRuntimeApi + 'static, - I: Send + Sync + BlockImport> + 'static, + C: TendermintClient, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, A: Announce, - > TendermintImport + > TendermintImport where TransactionFor: Send + Sync + 'static, { pub(crate) fn new( client: Arc, - inner: I, announce: A, providers: Arc, env: E, - ) -> TendermintImport { + ) -> TendermintImport { 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 + 'static, - C: Send + Sync + HeaderBackend + Finalizer + ProvideRuntimeApi + 'static, - I: Send + Sync + BlockImport> + 'static, + C: TendermintClient, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, A: Announce, - > Network for TendermintImport + > Network for TendermintImport where TransactionFor: Send + Sync + 'static, { diff --git a/substrate/consensus/src/verifier.rs b/substrate/consensus/src/verifier.rs index 4dae0fc0..d2379196 100644 --- a/substrate/consensus/src/verifier.rs +++ b/substrate/consensus/src/verifier.rs @@ -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 + 'static, - C: Send + Sync + HeaderBackend + Finalizer + ProvideRuntimeApi + 'static, - I: Send + Sync + BlockImport> + 'static, + C: TendermintClient, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, A: Announce, - > Verifier for TendermintImport + > Verifier for TendermintImport where TransactionFor: Send + Sync + 'static, + Arc: BlockImport>, + as BlockImport>::Error: Into, { async fn verify( &mut self,