From 8ed0f1f1cfa0a0a56c352a9c3b75eace574c17e8 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sat, 22 Oct 2022 00:48:09 -0400 Subject: [PATCH] Trigger block importing Doesn't wait for the response yet, which it needs to. --- substrate/consensus/src/import_queue.rs | 50 +++++++++++++++++++++---- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/substrate/consensus/src/import_queue.rs b/substrate/consensus/src/import_queue.rs index 48c3ec00..2be10e4c 100644 --- a/substrate/consensus/src/import_queue.rs +++ b/substrate/consensus/src/import_queue.rs @@ -34,7 +34,7 @@ use sp_runtime::{ use sp_blockchain::HeaderBackend; use sp_api::{BlockId, TransactionFor, ProvideRuntimeApi}; -use sp_consensus::{Error, CacheKeyId, Proposer, Environment}; +use sp_consensus::{Error, CacheKeyId, BlockOrigin, Proposer, Environment}; #[rustfmt::skip] // rustfmt doesn't know how to handle this line use sc_consensus::{ ForkChoiceStrategy, @@ -44,9 +44,11 @@ use sc_consensus::{ ImportResult, BlockImport, JustificationImport, + import_queue::IncomingBlock, BasicQueue, }; +use sc_service::ImportQueue; use sc_client_api::{Backend, Finalizer}; use substrate_prometheus_endpoint::Registry; @@ -60,6 +62,8 @@ use crate::{signature_scheme::TendermintSigner, weights::TendermintWeights}; const CONSENSUS_ID: [u8; 4] = *b"tend"; +pub type TendermintImportQueue = BasicQueue; + struct TendermintImport< B: Block, Be: Backend + 'static, @@ -78,6 +82,7 @@ struct TendermintImport< providers: Arc, env: Arc>, + queue: Arc>>>>, } impl< @@ -101,6 +106,7 @@ impl< providers: self.providers.clone(), env: self.env.clone(), + queue: self.queue.clone(), } } } @@ -113,6 +119,8 @@ impl< CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, > TendermintImport +where + TransactionFor: Send + Sync + 'static, { async fn check_inherents( &self, @@ -286,6 +294,7 @@ impl< > BlockImport for TendermintImport where I::Error: Into, + TransactionFor: Send + Sync + 'static, { type Error = Error; type Transaction = TransactionFor; @@ -326,6 +335,8 @@ impl< CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, > JustificationImport for TendermintImport +where + TransactionFor: Send + Sync + 'static, { type Error = Error; @@ -352,6 +363,8 @@ impl< CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, > Verifier for TendermintImport +where + TransactionFor: Send + Sync + 'static, { async fn verify( &mut self, @@ -371,6 +384,8 @@ impl< CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, > Network for TendermintImport +where + TransactionFor: Send + Sync + 'static, { type ValidatorId = u16; type SignatureScheme = TendermintSigner; @@ -396,10 +411,27 @@ impl< } fn validate(&mut self, block: &B) -> Result<(), BlockError> { + let hash = block.hash(); + let (header, body) = block.clone().deconstruct(); + *self.importing_block.write().unwrap() = Some(hash); + self.queue.write().unwrap().as_mut().unwrap().import_blocks( + BlockOrigin::NetworkBroadcast, + vec![IncomingBlock { + hash, + header: Some(header), + body: Some(body), + indexed_body: None, + justifications: None, + origin: None, + allow_missing_state: false, + skip_execution: false, + // TODO: Only set to true if block was rejected due to its inherents + import_existing: true, + state: None, + }], + ); todo!() - // self.check_block().map_err(|_| BlockError::Temporal)?; - // self.import_block().map_err(|_| BlockError::Temporal)?; - // Ok(()) + // self.queue.poll_actions } async fn add_block(&mut self, block: B, commit: Commit) -> B { @@ -408,8 +440,6 @@ impl< } } -pub type TendermintImportQueue = BasicQueue; - pub fn import_queue< B: Block, Be: Backend + 'static, @@ -427,6 +457,7 @@ pub fn import_queue< ) -> TendermintImportQueue> where I::Error: Into, + TransactionFor: Send + Sync + 'static, { let import = TendermintImport { _block: PhantomData, @@ -439,9 +470,12 @@ where providers, env: Arc::new(AsyncRwLock::new(env)), + queue: Arc::new(RwLock::new(None)), }; let boxed = Box::new(import.clone()); - // TODO: Fully read BasicQueue in otder to understand it - BasicQueue::new(import, boxed.clone(), Some(boxed), spawner, registry) + let queue = + || BasicQueue::new(import.clone(), boxed.clone(), Some(boxed.clone()), spawner, registry); + *import.queue.write().unwrap() = Some(queue()); + queue() }