From b350613a436c66b88ac05cfae6b0272ed2312c15 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Fri, 21 Apr 2023 14:48:10 +0900 Subject: [PATCH 01/12] up --- Cargo.lock | 2 + client/consensus/manual-seal/Cargo.toml | 2 + client/consensus/manual-seal/src/lib.rs | 52 ++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7f74a495d2fe..cd1454248d062 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8825,6 +8825,7 @@ dependencies = [ "assert_matches", "async-trait", "futures", + "futures-timer", "jsonrpsee", "log", "parity-scale-codec", @@ -8834,6 +8835,7 @@ dependencies = [ "sc-consensus-aura", "sc-consensus-babe", "sc-consensus-epochs", + "sc-service", "sc-transaction-pool", "sc-transaction-pool-api", "serde", diff --git a/client/consensus/manual-seal/Cargo.toml b/client/consensus/manual-seal/Cargo.toml index 19c4b22247e0f..f962e593c7acf 100644 --- a/client/consensus/manual-seal/Cargo.toml +++ b/client/consensus/manual-seal/Cargo.toml @@ -18,6 +18,7 @@ assert_matches = "1.3.0" async-trait = "0.1.57" codec = { package = "parity-scale-codec", version = "3.2.2" } futures = "0.3.21" +futures-timer = "3.0.1" log = "0.4.17" serde = { version = "1.0", features = ["derive"] } thiserror = "1.0" @@ -29,6 +30,7 @@ sc-consensus-babe = { version = "0.10.0-dev", path = "../../consensus/babe" } sc-consensus-epochs = { version = "0.10.0-dev", path = "../../consensus/epochs" } sc-transaction-pool = { version = "4.0.0-dev", path = "../../transaction-pool" } sc-transaction-pool-api = { version = "4.0.0-dev", path = "../../../client/transaction-pool/api" } +sc-service = { version = "0.10.0-dev", path = "../../../client/service" } sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" } sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" } sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" } diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index b277b34366577..9e4854bdb3f27 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -20,8 +20,12 @@ //! This is suitable for a testing environment. use futures::prelude::*; +use futures_timer::Delay; use prometheus_endpoint::Registry; -use sc_client_api::backend::{Backend as ClientBackend, Finalizer}; +use sc_client_api::{ + backend::{Backend as ClientBackend, Finalizer}, + client::BlockchainEvents, +}; use sc_consensus::{ block_import::{BlockImport, BlockImportParams, ForkChoiceStrategy}, import_queue::{BasicQueue, BoxBlockImport, Verifier}, @@ -30,7 +34,7 @@ use sp_blockchain::HeaderBackend; use sp_consensus::{Environment, Proposer, SelectChain}; use sp_inherents::CreateInherentDataProviders; use sp_runtime::{traits::Block as BlockT, ConsensusEngineId}; -use std::{marker::PhantomData, sync::Arc}; +use std::{marker::PhantomData, sync::Arc, time::Duration}; mod error; mod finalize_block; @@ -136,6 +140,19 @@ pub struct InstantSealParams, TP, SC, pub create_inherent_data_providers: CIDP, } +pub struct DelayedFinalizeParams> { + /// Block import instance for well. importing blocks. + pub client: Arc, + + pub spawn_handle: sc_service::SpawnTaskHandle, + + /// The delay in seconds before a block is finalized. + pub delay_sec: u64, + + /// phantom type to pin the Block type + pub _phantom: PhantomData, +} + /// Creates the background authorship task for the manual seal engine. pub async fn run_manual_seal( ManualSealParams { @@ -303,6 +320,37 @@ pub async fn run_instant_seal_and_finalize( .await } +pub async fn run_delayed_finalize( + DelayedFinalizeParams { + client, + spawn_handle, + delay_sec, + _phantom: PhantomData, + }: DelayedFinalizeParams, +) where + B: BlockT + 'static, + CB: ClientBackend + 'static, + C: HeaderBackend + Finalizer + ProvideRuntimeApi + BlockchainEvents + 'static, +{ + let mut block_import_stream = client.import_notification_stream(); + + while let Some(notification) = block_import_stream.next().await { + let delay = Delay::new(Duration::from_secs(delay_sec)); + let cloned_client = client.clone(); + spawn_handle.spawn("delayed-finalize", None, async move { + delay.await; + finalize_block(FinalizeBlockParams { + hash: notification.hash, + sender: None, + justification: None, + finalizer: cloned_client, + _phantom: PhantomData, + }) + .await + }); + } +} + #[cfg(test)] mod tests { use super::*; From c36b9bce35a0997c5b3acf0bb9f4b11871f34bba Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Mon, 24 Apr 2023 17:25:24 +0900 Subject: [PATCH 02/12] up --- client/consensus/manual-seal/src/lib.rs | 3 ++- client/consensus/manual-seal/src/rpc.rs | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 9e4854bdb3f27..dd0259292d635 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -30,6 +30,7 @@ use sc_consensus::{ block_import::{BlockImport, BlockImportParams, ForkChoiceStrategy}, import_queue::{BasicQueue, BoxBlockImport, Verifier}, }; +use sc_service::SpawnTaskHandle; use sp_blockchain::HeaderBackend; use sp_consensus::{Environment, Proposer, SelectChain}; use sp_inherents::CreateInherentDataProviders; @@ -144,7 +145,7 @@ pub struct DelayedFinalizeParams> { /// Block import instance for well. importing blocks. pub client: Arc, - pub spawn_handle: sc_service::SpawnTaskHandle, + pub spawn_handle: SpawnTaskHandle, /// The delay in seconds before a block is finalized. pub delay_sec: u64, diff --git a/client/consensus/manual-seal/src/rpc.rs b/client/consensus/manual-seal/src/rpc.rs index db92b9fd2981a..b844a5286405f 100644 --- a/client/consensus/manual-seal/src/rpc.rs +++ b/client/consensus/manual-seal/src/rpc.rs @@ -160,10 +160,11 @@ pub fn send_result( } } } else { - // instant seal doesn't report errors over rpc, simply log them. + // Sealing/Finalization with no RPC sender such as instant seal or delayed finalize doesn't + // report errors over rpc, simply log them. match result { - Ok(r) => log::info!("Instant Seal success: {:?}", r), - Err(e) => log::error!("Instant Seal encountered an error: {}", e), + Ok(r) => log::info!("Instant Seal Consensus success: {:?}", r), + Err(e) => log::error!("Instant Seal Consensus encountered an error: {}", e), } } } From a55521887e97891644b6e89ccd21ac06bb60fdf0 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Tue, 25 Apr 2023 15:58:55 +0900 Subject: [PATCH 03/12] added test --- client/consensus/manual-seal/src/lib.rs | 111 ++++++++++++++++++++++-- client/consensus/manual-seal/src/rpc.rs | 4 +- 2 files changed, 106 insertions(+), 9 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index dd0259292d635..86c4d863f7364 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -30,7 +30,7 @@ use sc_consensus::{ block_import::{BlockImport, BlockImportParams, ForkChoiceStrategy}, import_queue::{BasicQueue, BoxBlockImport, Verifier}, }; -use sc_service::SpawnTaskHandle; +use sp_core::traits::SpawnNamed; use sp_blockchain::HeaderBackend; use sp_consensus::{Environment, Proposer, SelectChain}; use sp_inherents::CreateInherentDataProviders; @@ -141,11 +141,11 @@ pub struct InstantSealParams, TP, SC, pub create_inherent_data_providers: CIDP, } -pub struct DelayedFinalizeParams> { +pub struct DelayedFinalizeParams, S: SpawnNamed> { /// Block import instance for well. importing blocks. pub client: Arc, - pub spawn_handle: SpawnTaskHandle, + pub spawn_handle: S, /// The delay in seconds before a block is finalized. pub delay_sec: u64, @@ -321,24 +321,25 @@ pub async fn run_instant_seal_and_finalize( .await } -pub async fn run_delayed_finalize( +pub async fn run_delayed_finalize( DelayedFinalizeParams { client, spawn_handle, delay_sec, _phantom: PhantomData, - }: DelayedFinalizeParams, + }: DelayedFinalizeParams, ) where B: BlockT + 'static, CB: ClientBackend + 'static, C: HeaderBackend + Finalizer + ProvideRuntimeApi + BlockchainEvents + 'static, + S: SpawnNamed, { let mut block_import_stream = client.import_notification_stream(); while let Some(notification) = block_import_stream.next().await { let delay = Delay::new(Duration::from_secs(delay_sec)); let cloned_client = client.clone(); - spawn_handle.spawn("delayed-finalize", None, async move { + spawn_handle.spawn("delayed-finalize", None, Box::pin(async move { delay.await; finalize_block(FinalizeBlockParams { hash: notification.hash, @@ -348,7 +349,7 @@ pub async fn run_delayed_finalize( _phantom: PhantomData, }) .await - }); + })); } } @@ -477,6 +478,102 @@ mod tests { assert_eq!(client.header(created_block.hash).unwrap().unwrap().number, 1) } + #[tokio::test] + async fn instant_seal_delayed_finalize() { + let builder = TestClientBuilder::new(); + let (client, select_chain) = builder.build_with_longest_chain(); + let client = Arc::new(client); + let spawner = sp_core::testing::TaskExecutor::new(); + let genesis_hash = client.info().genesis_hash; + let pool = Arc::new(BasicPool::with_revalidation_type( + Options::default(), + true.into(), + api(), + None, + RevalidationType::Full, + spawner.clone(), + 0, + genesis_hash, + genesis_hash, + )); + let env = ProposerFactory::new(spawner.clone(), client.clone(), pool.clone(), None, None); + // this test checks that blocks are created as soon as transactions are imported into the + // pool. + let (sender, receiver) = futures::channel::oneshot::channel(); + let mut sender = Arc::new(Some(sender)); + let commands_stream = + pool.pool().validated_pool().import_notification_stream().map(move |_| { + // we're only going to submit one tx so this fn will only be called once. + let mut_sender = Arc::get_mut(&mut sender).unwrap(); + let sender = std::mem::take(mut_sender); + EngineCommand::SealNewBlock { + create_empty: false, + // set to `false`, expecting to be finalized by delayed finalize + finalize: false, + parent_hash: None, + sender, + } + }); + + let future_instant_seal = run_manual_seal(ManualSealParams { + block_import: client.clone(), + commands_stream, + env, + client: client.clone(), + pool: pool.clone(), + select_chain, + create_inherent_data_providers: |_, _| async { Ok(()) }, + consensus_data_provider: None, + }); + std::thread::spawn(|| { + let rt = tokio::runtime::Runtime::new().unwrap(); + // spawn the background authorship task + rt.block_on(future_instant_seal); + }); + + let delay_sec = 5; + let future_delayed_finalize = run_delayed_finalize(DelayedFinalizeParams { + client: client.clone(), + delay_sec, + spawn_handle: spawner, + _phantom: PhantomData::default(), + }); + std::thread::spawn(|| { + let rt = tokio::runtime::Runtime::new().unwrap(); + // spawn the background authorship task + rt.block_on(future_delayed_finalize); + }); + + // submit a transaction to pool. + let result = pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Alice, 0)).await; + // assert that it was successfully imported + assert!(result.is_ok()); + // assert that the background task returns ok + let created_block = receiver.await.unwrap().unwrap(); + assert_eq!( + created_block, + CreatedBlock { + hash: created_block.hash, + aux: ImportedAux { + header_only: false, + clear_justification_requests: false, + needs_justification: false, + bad_justification: false, + is_new_best: true, + } + } + ); + // assert that there's a new block in the db. + assert!(client.header(created_block.hash).unwrap().is_some()); + assert_eq!(client.header(created_block.hash).unwrap().unwrap().number, 1); + + assert_eq!(client.info().finalized_hash, client.info().genesis_hash); + // Ensuring run_delayed_finalize's Future is always processed before checking finalized hash + // By adding 1 sec + Delay::new(Duration::from_secs(delay_sec + 1)).await; + assert_eq!(client.info().finalized_hash, created_block.hash); + } + #[tokio::test] async fn manual_seal_and_finalization() { let builder = TestClientBuilder::new(); diff --git a/client/consensus/manual-seal/src/rpc.rs b/client/consensus/manual-seal/src/rpc.rs index b844a5286405f..85abcdc08574b 100644 --- a/client/consensus/manual-seal/src/rpc.rs +++ b/client/consensus/manual-seal/src/rpc.rs @@ -163,8 +163,8 @@ pub fn send_result( // Sealing/Finalization with no RPC sender such as instant seal or delayed finalize doesn't // report errors over rpc, simply log them. match result { - Ok(r) => log::info!("Instant Seal Consensus success: {:?}", r), - Err(e) => log::error!("Instant Seal Consensus encountered an error: {}", e), + Ok(r) => log::info!("Consensus with no RPC sender success: {:?}", r), + Err(e) => log::error!("Consensus with no RPC sender encountered an error: {}", e), } } } From a6c3fdf5b87830a98e6e3d7c9f92a9a2ea7cb393 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Tue, 25 Apr 2023 16:13:23 +0900 Subject: [PATCH 04/12] remove unncessary dep --- Cargo.lock | 1 - client/consensus/manual-seal/Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd1454248d062..fcf7a1af4aedf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8835,7 +8835,6 @@ dependencies = [ "sc-consensus-aura", "sc-consensus-babe", "sc-consensus-epochs", - "sc-service", "sc-transaction-pool", "sc-transaction-pool-api", "serde", diff --git a/client/consensus/manual-seal/Cargo.toml b/client/consensus/manual-seal/Cargo.toml index f962e593c7acf..2ddcf0d772890 100644 --- a/client/consensus/manual-seal/Cargo.toml +++ b/client/consensus/manual-seal/Cargo.toml @@ -30,7 +30,6 @@ sc-consensus-babe = { version = "0.10.0-dev", path = "../../consensus/babe" } sc-consensus-epochs = { version = "0.10.0-dev", path = "../../consensus/epochs" } sc-transaction-pool = { version = "4.0.0-dev", path = "../../transaction-pool" } sc-transaction-pool-api = { version = "4.0.0-dev", path = "../../../client/transaction-pool/api" } -sc-service = { version = "0.10.0-dev", path = "../../../client/service" } sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" } sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" } sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" } From 5efb475e44fc8fbe8303397e14ecb8eee37199b9 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Tue, 25 Apr 2023 16:16:55 +0900 Subject: [PATCH 05/12] cargo fmt --- client/consensus/manual-seal/src/lib.rs | 30 ++++++++++++++----------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 86c4d863f7364..7669bd68c9fa0 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -30,9 +30,9 @@ use sc_consensus::{ block_import::{BlockImport, BlockImportParams, ForkChoiceStrategy}, import_queue::{BasicQueue, BoxBlockImport, Verifier}, }; -use sp_core::traits::SpawnNamed; use sp_blockchain::HeaderBackend; use sp_consensus::{Environment, Proposer, SelectChain}; +use sp_core::traits::SpawnNamed; use sp_inherents::CreateInherentDataProviders; use sp_runtime::{traits::Block as BlockT, ConsensusEngineId}; use std::{marker::PhantomData, sync::Arc, time::Duration}; @@ -339,17 +339,21 @@ pub async fn run_delayed_finalize( while let Some(notification) = block_import_stream.next().await { let delay = Delay::new(Duration::from_secs(delay_sec)); let cloned_client = client.clone(); - spawn_handle.spawn("delayed-finalize", None, Box::pin(async move { - delay.await; - finalize_block(FinalizeBlockParams { - hash: notification.hash, - sender: None, - justification: None, - finalizer: cloned_client, - _phantom: PhantomData, - }) - .await - })); + spawn_handle.spawn( + "delayed-finalize", + None, + Box::pin(async move { + delay.await; + finalize_block(FinalizeBlockParams { + hash: notification.hash, + sender: None, + justification: None, + finalizer: cloned_client, + _phantom: PhantomData, + }) + .await + }), + ); } } @@ -568,7 +572,7 @@ mod tests { assert_eq!(client.header(created_block.hash).unwrap().unwrap().number, 1); assert_eq!(client.info().finalized_hash, client.info().genesis_hash); - // Ensuring run_delayed_finalize's Future is always processed before checking finalized hash + // Ensuring run_delayed_finalize's Future is always processed before checking finalized hash // By adding 1 sec Delay::new(Duration::from_secs(delay_sec + 1)).await; assert_eq!(client.info().finalized_hash, created_block.hash); From 0d1e9e93c8c8fc09491a0f031454207788e10043 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Tue, 25 Apr 2023 16:18:09 +0900 Subject: [PATCH 06/12] cargo fmt --- client/consensus/manual-seal/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 7669bd68c9fa0..27aa13e1213af 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -572,8 +572,8 @@ mod tests { assert_eq!(client.header(created_block.hash).unwrap().unwrap().number, 1); assert_eq!(client.info().finalized_hash, client.info().genesis_hash); - // Ensuring run_delayed_finalize's Future is always processed before checking finalized hash - // By adding 1 sec + // ensuring run_delayed_finalize's Future is always processed before checking finalized hash + // by adding 1 sec Delay::new(Duration::from_secs(delay_sec + 1)).await; assert_eq!(client.info().finalized_hash, created_block.hash); } From 35a5c731dbe36aeffb5bb4623d300781dac38670 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Tue, 25 Apr 2023 17:40:06 +0900 Subject: [PATCH 07/12] up --- client/consensus/manual-seal/src/lib.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 27aa13e1213af..3ff34ac7e5e47 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -141,7 +141,7 @@ pub struct InstantSealParams, TP, SC, pub create_inherent_data_providers: CIDP, } -pub struct DelayedFinalizeParams, S: SpawnNamed> { +pub struct DelayedFinalizeParams { /// Block import instance for well. importing blocks. pub client: Arc, @@ -149,9 +149,6 @@ pub struct DelayedFinalizeParams, S: SpawnNam /// The delay in seconds before a block is finalized. pub delay_sec: u64, - - /// phantom type to pin the Block type - pub _phantom: PhantomData, } /// Creates the background authorship task for the manual seal engine. @@ -321,13 +318,15 @@ pub async fn run_instant_seal_and_finalize( .await } +/// Runs the background finalization task for the manual/instant seal engines. +/// delayed-finalize finalizes blocks configured seconds after blocks are imported. +/// +/// This task is intended to be ran along with a block sealing task, but expects blocks are not +/// finalized when they're sealed. Specifically, `finalize` in `SealNewBlock` command should be set +/// to `false` when it is sent to `commands_stream`. That is, `run_delayed_finalize` is incompatible +/// with `run_instant_seal_and_finalize` due to re-finalize attemption. pub async fn run_delayed_finalize( - DelayedFinalizeParams { - client, - spawn_handle, - delay_sec, - _phantom: PhantomData, - }: DelayedFinalizeParams, + DelayedFinalizeParams { client, spawn_handle, delay_sec }: DelayedFinalizeParams, ) where B: BlockT + 'static, CB: ClientBackend + 'static, @@ -540,7 +539,6 @@ mod tests { client: client.clone(), delay_sec, spawn_handle: spawner, - _phantom: PhantomData::default(), }); std::thread::spawn(|| { let rt = tokio::runtime::Runtime::new().unwrap(); From ca6b9f0a7e58bfc739ef79bc3423817f31439d15 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Wed, 26 Apr 2023 12:22:17 +0900 Subject: [PATCH 08/12] Update client/consensus/manual-seal/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/consensus/manual-seal/src/lib.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 3ff34ac7e5e47..655d10c0b63a7 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -318,13 +318,10 @@ pub async fn run_instant_seal_and_finalize( .await } -/// Runs the background finalization task for the manual/instant seal engines. -/// delayed-finalize finalizes blocks configured seconds after blocks are imported. +/// Creates a future for delayed finalization of manual sealed blocks. /// -/// This task is intended to be ran along with a block sealing task, but expects blocks are not -/// finalized when they're sealed. Specifically, `finalize` in `SealNewBlock` command should be set -/// to `false` when it is sent to `commands_stream`. That is, `run_delayed_finalize` is incompatible -/// with `run_instant_seal_and_finalize` due to re-finalize attemption. +/// The future needs to be spawned in the background alongside the [`run_manual_seal`]/[`run_instant_seal`] future. +/// It is required that [`EngineCommand::SealNewBlock`] is send with `finalize = false` to not finalize blocks directly after building them. This also means that delayed finality can not be used with [`run_instant_seal_and_finalize`]. pub async fn run_delayed_finalize( DelayedFinalizeParams { client, spawn_handle, delay_sec }: DelayedFinalizeParams, ) where From d354172f9b94996ef48c4c0731c3cc64c39ff89e Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Wed, 26 Apr 2023 12:29:26 +0900 Subject: [PATCH 09/12] fix test --- client/consensus/manual-seal/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 655d10c0b63a7..ab24fafd6f4c4 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -543,6 +543,7 @@ mod tests { rt.block_on(future_delayed_finalize); }); + let mut finality_stream = client.finality_notification_stream(); // submit a transaction to pool. let result = pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Alice, 0)).await; // assert that it was successfully imported @@ -567,10 +568,9 @@ mod tests { assert_eq!(client.header(created_block.hash).unwrap().unwrap().number, 1); assert_eq!(client.info().finalized_hash, client.info().genesis_hash); - // ensuring run_delayed_finalize's Future is always processed before checking finalized hash - // by adding 1 sec - Delay::new(Duration::from_secs(delay_sec + 1)).await; - assert_eq!(client.info().finalized_hash, created_block.hash); + + let finalized = finality_stream.select_next_some().await; + assert_eq!(finalized.hash, created_block.hash); } #[tokio::test] From 5ccdf32c3fd418f44a702faaddabb85f61eaa202 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Wed, 26 Apr 2023 12:36:42 +0900 Subject: [PATCH 10/12] cargo fmt --- client/consensus/manual-seal/src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index ab24fafd6f4c4..ffae833b2b663 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -320,8 +320,11 @@ pub async fn run_instant_seal_and_finalize( /// Creates a future for delayed finalization of manual sealed blocks. /// -/// The future needs to be spawned in the background alongside the [`run_manual_seal`]/[`run_instant_seal`] future. -/// It is required that [`EngineCommand::SealNewBlock`] is send with `finalize = false` to not finalize blocks directly after building them. This also means that delayed finality can not be used with [`run_instant_seal_and_finalize`]. +/// The future needs to be spawned in the background alongside the +/// [`run_manual_seal`]/[`run_instant_seal`] future. It is required that +/// [`EngineCommand::SealNewBlock`] is send with `finalize = false` to not finalize blocks directly +/// after building them. This also means that delayed finality can not be used with +/// [`run_instant_seal_and_finalize`]. pub async fn run_delayed_finalize( DelayedFinalizeParams { client, spawn_handle, delay_sec }: DelayedFinalizeParams, ) where From b52b8b9a562a3e349d995b87c2dfa76ba0b5d7c9 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Tue, 2 May 2023 20:38:04 +0900 Subject: [PATCH 11/12] added docs --- client/consensus/manual-seal/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index ffae833b2b663..1d7ad9c3ca730 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -89,7 +89,7 @@ where /// Params required to start the instant sealing authorship task. pub struct ManualSealParams, TP, SC, CS, CIDP, P> { - /// Block import instance for well. importing blocks. + /// Block import instance. pub block_import: BI, /// The environment we are producing blocks for. @@ -141,17 +141,19 @@ pub struct InstantSealParams, TP, SC, pub create_inherent_data_providers: CIDP, } +/// Params required to start the delayed finalization task. pub struct DelayedFinalizeParams { /// Block import instance for well. importing blocks. pub client: Arc, + /// Handle for spawning delayed finalization tasks. pub spawn_handle: S, /// The delay in seconds before a block is finalized. pub delay_sec: u64, } -/// Creates the background authorship task for the manual seal engine. +/// Creates the background authorship task for the manually seal engine. pub async fn run_manual_seal( ManualSealParams { mut block_import, From e4f3d43ff429e79efc2916a7541bac6386773107 Mon Sep 17 00:00:00 2001 From: Shunsuke Watanabe Date: Tue, 2 May 2023 20:54:45 +0900 Subject: [PATCH 12/12] updated doc --- client/consensus/manual-seal/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 1d7ad9c3ca730..03c9418b5c560 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -143,7 +143,7 @@ pub struct InstantSealParams, TP, SC, /// Params required to start the delayed finalization task. pub struct DelayedFinalizeParams { - /// Block import instance for well. importing blocks. + /// Block import instance. pub client: Arc, /// Handle for spawning delayed finalization tasks.