From 6f0a30beb64369a26be17386a0255f5134e3431a Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 7 Nov 2023 11:39:17 +0000 Subject: [PATCH 1/3] feat: delayed finalization Based on https://github.com/inkdevhub/swanky-node/pull/61 --- README.md | 8 ++++++++ node/src/cli.rs | 4 ++++ node/src/command.rs | 2 +- node/src/service/dev.rs | 18 ++++++++++++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1d9c5c6..b97d860 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,14 @@ for a production deployment, but a great fit for development and testing:_ [#42](https://github.com/paritytech/substrate-contracts-node/pull/42). Hereby blocks are authored immediately at every transaction, so there is none of the typical six seconds block time associated with `grandpa` or `aura`. + * By default, either manual or instant seal does not result in block finalization unless the `engine_finalizeBlock` + RPC is executed. However, it is possible to configure the finalization of sealed blocks to occur after a certain + amount of time by setting the `--finalize-delay-sec` option to a specific value, which specifies the number of seconds + to delay before finalizing the blocks. Using a value of `0` would lead to instant finalization, with the blocks being + finalized immediately upon being sealed. + ```shell + ./target/release/substrate-contracts-node --finalize-delay-sec 5 + ``` * _If no CLI arguments are passed the node is started in development mode by default._ * A custom logging filter is applied by default that hides block production noise diff --git a/node/src/cli.rs b/node/src/cli.rs index a4efcf6..99c1779 100644 --- a/node/src/cli.rs +++ b/node/src/cli.rs @@ -83,6 +83,10 @@ pub struct Cli { /// Relay chain arguments #[arg(raw = true)] pub relay_chain_args: Vec, + + /// The number of seconds to delay before finalizing blocks. A value of `0` would lead to instant finalization. + #[clap(long)] + pub finalize_delay_sec: Option, } #[derive(Debug)] diff --git a/node/src/command.rs b/node/src/command.rs index 9a8a9a5..00ef680 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -225,7 +225,7 @@ pub fn run() -> Result<()> { runner.run_node_until_exit(|config| async move { if config.chain_spec.name() == "Development" { // TODO - return service::dev::new_full(config).map_err(sc_cli::Error::Service); + return service::dev::new_full(config, cli.finalize_delay_sec).map_err(sc_cli::Error::Service); } let hwbench = (!cli.no_hardware_benchmarks) diff --git a/node/src/service/dev.rs b/node/src/service/dev.rs index bf0689d..737ba63 100644 --- a/node/src/service/dev.rs +++ b/node/src/service/dev.rs @@ -101,7 +101,7 @@ pub fn new_partial( }) } -pub fn new_full(config: Configuration) -> Result { +pub fn new_full(config: Configuration, finalize_delay_sec: Option) -> Result { let sc_service::PartialComponents { client, backend, @@ -195,7 +195,7 @@ pub fn new_full(config: Configuration) -> Result { let params = sc_consensus_manual_seal::InstantSealParams { block_import: client.clone(), env: proposer, - client, + client: client.clone(), pool: transaction_pool, select_chain, consensus_data_provider: None, @@ -206,6 +206,20 @@ pub fn new_full(config: Configuration) -> Result { let authorship_future = sc_consensus_manual_seal::run_instant_seal(params); + if let Some(sec) = finalize_delay_sec { + let delayed_finalize_params = sc_consensus_manual_seal::DelayedFinalizeParams { + client, + spawn_handle: task_manager.spawn_handle(), + delay_sec: sec, + }; + + task_manager.spawn_essential_handle().spawn_blocking( + "delayed_finalize", + None, + sc_consensus_manual_seal::run_delayed_finalize(delayed_finalize_params), + ); + } + task_manager .spawn_essential_handle() .spawn_blocking("instant-seal", None, authorship_future); From 2a5b5bfadeae0b8717f826f2fb3aa62944009304 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 7 Nov 2023 14:08:14 +0000 Subject: [PATCH 2/3] style: fix formatting --- node/src/cli.rs | 3 ++- node/src/service/dev.rs | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/node/src/cli.rs b/node/src/cli.rs index 99c1779..fe20fe5 100644 --- a/node/src/cli.rs +++ b/node/src/cli.rs @@ -84,7 +84,8 @@ pub struct Cli { #[arg(raw = true)] pub relay_chain_args: Vec, - /// The number of seconds to delay before finalizing blocks. A value of `0` would lead to instant finalization. + /// The number of seconds to delay before finalizing blocks. A value of `0` would lead to + /// instant finalization. #[clap(long)] pub finalize_delay_sec: Option, } diff --git a/node/src/service/dev.rs b/node/src/service/dev.rs index 737ba63..f922560 100644 --- a/node/src/service/dev.rs +++ b/node/src/service/dev.rs @@ -101,7 +101,10 @@ pub fn new_partial( }) } -pub fn new_full(config: Configuration, finalize_delay_sec: Option) -> Result { +pub fn new_full( + config: Configuration, + finalize_delay_sec: Option, +) -> Result { let sc_service::PartialComponents { client, backend, From 35f2d580bf0f533f4d36c78e8510fa2b2daf3521 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Wed, 29 Nov 2023 11:55:22 +0000 Subject: [PATCH 3/3] feat: default delayed finalization to 1 second --- README.md | 3 +-- node/src/cli.rs | 7 +++---- node/src/command.rs | 2 +- node/src/service/dev.rs | 29 +++++++++++++---------------- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index b97d860..6c31e66 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,7 @@ for a production deployment, but a great fit for development and testing:_ * By default, either manual or instant seal does not result in block finalization unless the `engine_finalizeBlock` RPC is executed. However, it is possible to configure the finalization of sealed blocks to occur after a certain amount of time by setting the `--finalize-delay-sec` option to a specific value, which specifies the number of seconds - to delay before finalizing the blocks. Using a value of `0` would lead to instant finalization, with the blocks being - finalized immediately upon being sealed. + to delay before finalizing the blocks. The default value is 1 second. ```shell ./target/release/substrate-contracts-node --finalize-delay-sec 5 ``` diff --git a/node/src/cli.rs b/node/src/cli.rs index fe20fe5..522254e 100644 --- a/node/src/cli.rs +++ b/node/src/cli.rs @@ -84,10 +84,9 @@ pub struct Cli { #[arg(raw = true)] pub relay_chain_args: Vec, - /// The number of seconds to delay before finalizing blocks. A value of `0` would lead to - /// instant finalization. - #[clap(long)] - pub finalize_delay_sec: Option, + /// The number of seconds to delay before finalizing blocks. + #[arg(long, default_value_t = 1)] + pub finalize_delay_sec: u8, } #[derive(Debug)] diff --git a/node/src/command.rs b/node/src/command.rs index 00ef680..7be6a9d 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -225,7 +225,7 @@ pub fn run() -> Result<()> { runner.run_node_until_exit(|config| async move { if config.chain_spec.name() == "Development" { // TODO - return service::dev::new_full(config, cli.finalize_delay_sec).map_err(sc_cli::Error::Service); + return service::dev::new_full(config, cli.finalize_delay_sec.into()).map_err(sc_cli::Error::Service); } let hwbench = (!cli.no_hardware_benchmarks) diff --git a/node/src/service/dev.rs b/node/src/service/dev.rs index f922560..274ac49 100644 --- a/node/src/service/dev.rs +++ b/node/src/service/dev.rs @@ -103,7 +103,7 @@ pub fn new_partial( pub fn new_full( config: Configuration, - finalize_delay_sec: Option, + finalize_delay_sec: u64, ) -> Result { let sc_service::PartialComponents { client, @@ -208,24 +208,21 @@ pub fn new_full( }; let authorship_future = sc_consensus_manual_seal::run_instant_seal(params); - - if let Some(sec) = finalize_delay_sec { - let delayed_finalize_params = sc_consensus_manual_seal::DelayedFinalizeParams { - client, - spawn_handle: task_manager.spawn_handle(), - delay_sec: sec, - }; - - task_manager.spawn_essential_handle().spawn_blocking( - "delayed_finalize", - None, - sc_consensus_manual_seal::run_delayed_finalize(delayed_finalize_params), - ); - } - task_manager .spawn_essential_handle() .spawn_blocking("instant-seal", None, authorship_future); + + let delayed_finalize_params = sc_consensus_manual_seal::DelayedFinalizeParams { + client, + spawn_handle: task_manager.spawn_handle(), + delay_sec: finalize_delay_sec, + }; + task_manager.spawn_essential_handle().spawn_blocking( + "delayed_finalize", + None, + sc_consensus_manual_seal::run_delayed_finalize(delayed_finalize_params), + ); + network_starter.start_network(); Ok(task_manager) }