Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit bd3c7c0

Browse files
Cherry picked changelist: a64dd52
this fixed error when running integration tests for node cli
1 parent 48ceaab commit bd3c7c0

10 files changed

+112
-56
lines changed

bin/node/cli/tests/check_block_works.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ mod common;
2626
fn check_block_works() {
2727
let base_path = tempdir().expect("could not create a temp dir");
2828

29-
common::run_command_for_a_while(base_path.path(), false);
29+
common::run_dev_node_for_a_while(base_path.path());
3030

3131
let status = Command::new(cargo_bin("plug"))
32-
.args(&["check-block", "-d"])
32+
.args(&["check-block", "--dev", "--pruning", "archive", "-d"])
3333
.arg(base_path.path())
3434
.arg("1")
3535
.status()

bin/node/cli/tests/common.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,30 @@ use nix::unistd::Pid;
2727
///
2828
/// Returns the `Some(exit status)` or `None` if the process did not finish in the given time.
2929
pub fn wait_for(child: &mut Child, secs: usize) -> Option<ExitStatus> {
30-
for _ in 0..secs {
30+
for i in 0..secs {
3131
match child.try_wait().unwrap() {
32-
Some(status) => return Some(status),
32+
Some(status) => {
33+
if i > 5 {
34+
eprintln!("Child process took {} seconds to exit gracefully", i);
35+
}
36+
return Some(status)
37+
},
3338
None => thread::sleep(Duration::from_secs(1)),
3439
}
3540
}
36-
eprintln!("Took to long to exit. Killing...");
41+
eprintln!("Took too long to exit (> {} seconds). Killing...", secs);
3742
let _ = child.kill();
3843
child.wait().unwrap();
3944

4045
None
4146
}
4247

4348
/// Run the node for a while (30 seconds)
44-
pub fn run_command_for_a_while(base_path: &Path, dev: bool) {
49+
pub fn run_dev_node_for_a_while(base_path: &Path) {
4550
let mut cmd = Command::new(cargo_bin("plug"));
4651

47-
if dev {
48-
cmd.arg("--dev");
49-
}
50-
5152
let mut cmd = cmd
53+
.args(&["--dev"])
5254
.arg("-d")
5355
.arg(base_path)
5456
.spawn()

bin/node/cli/tests/import_export_and_revert_work.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ fn import_export_and_revert_work() {
2727
let base_path = tempdir().expect("could not create a temp dir");
2828
let exported_blocks = base_path.path().join("exported_blocks");
2929

30-
common::run_command_for_a_while(base_path.path(), false);
30+
common::run_dev_node_for_a_while(base_path.path());
3131

3232
let status = Command::new(cargo_bin("plug"))
33-
.args(&["export-blocks", "-d"])
33+
.args(&["export-blocks", "--dev", "--pruning", "archive", "-d"])
3434
.arg(base_path.path())
3535
.arg(&exported_blocks)
3636
.status()
@@ -43,15 +43,15 @@ fn import_export_and_revert_work() {
4343
let _ = fs::remove_dir_all(base_path.path().join("db"));
4444

4545
let status = Command::new(cargo_bin("plug"))
46-
.args(&["import-blocks", "-d"])
46+
.args(&["import-blocks", "--dev", "--pruning", "archive", "-d"])
4747
.arg(base_path.path())
4848
.arg(&exported_blocks)
4949
.status()
5050
.unwrap();
5151
assert!(status.success());
5252

5353
let status = Command::new(cargo_bin("plug"))
54-
.args(&["revert", "-d"])
54+
.args(&["revert", "--dev", "--pruning", "archive", "-d"])
5555
.arg(base_path.path())
5656
.status()
5757
.unwrap();

bin/node/cli/tests/inspect_works.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ mod common;
2626
fn inspect_works() {
2727
let base_path = tempdir().expect("could not create a temp dir");
2828

29-
common::run_command_for_a_while(base_path.path(), false);
29+
common::run_dev_node_for_a_while(base_path.path());
3030

3131
let status = Command::new(cargo_bin("plug"))
32-
.args(&["inspect", "-d"])
32+
.args(&["inspect", "--dev", "--pruning", "archive", "-d"])
3333
.arg(base_path.path())
3434
.args(&["block", "1"])
3535
.status()

bin/node/cli/tests/purge_chain_works.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod common;
2626
fn purge_chain_works() {
2727
let base_path = tempdir().expect("could not create a temp dir");
2828

29-
common::run_command_for_a_while(base_path.path(), true);
29+
common::run_dev_node_for_a_while(base_path.path());
3030

3131
let status = Command::new(cargo_bin("plug"))
3232
.args(&["purge-chain", "--dev", "-d"])

client/cli/src/commands/export_blocks_cmd.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ use log::info;
2222
use structopt::StructOpt;
2323
use sc_service::{
2424
Configuration, ChainSpecExtension, RuntimeGenesis, ServiceBuilderCommand, ChainSpec,
25-
config::DatabaseConfig,
25+
config::DatabaseConfig, Roles,
2626
};
2727
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
2828

2929
use crate::error;
3030
use crate::VersionInfo;
3131
use crate::runtime::run_until_exit;
32-
use crate::params::SharedParams;
33-
use crate::params::BlockNumber;
32+
use crate::params::{SharedParams, BlockNumber, PruningParams};
3433

3534
/// The `export-blocks` command used to export blocks.
3635
#[derive(Debug, StructOpt, Clone)]
@@ -58,6 +57,10 @@ pub struct ExportBlocksCmd {
5857
#[allow(missing_docs)]
5958
#[structopt(flatten)]
6059
pub shared_params: SharedParams,
60+
61+
#[allow(missing_docs)]
62+
#[structopt(flatten)]
63+
pub pruning_params: PruningParams,
6164
}
6265

6366
impl ExportBlocksCmd {
@@ -106,6 +109,7 @@ impl ExportBlocksCmd {
106109
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
107110
{
108111
self.shared_params.update_config(&mut config, spec_factory, version)?;
112+
self.pruning_params.update_config(&mut config, Roles::FULL, true)?;
109113
config.use_in_memory_keystore()?;
110114

111115
Ok(())

client/cli/src/commands/revert_cmd.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
use std::fmt::Debug;
1818
use structopt::StructOpt;
1919
use sc_service::{
20-
Configuration, ChainSpecExtension, RuntimeGenesis, ServiceBuilderCommand, ChainSpec,
20+
Configuration, ChainSpecExtension, RuntimeGenesis, ServiceBuilderCommand, ChainSpec, Roles,
2121
};
2222
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
2323

2424
use crate::error;
2525
use crate::VersionInfo;
26-
use crate::params::BlockNumber;
27-
use crate::params::SharedParams;
26+
use crate::params::{BlockNumber, SharedParams, PruningParams};
2827

2928
/// The `revert` command used revert the chain to a previous state.
3029
#[derive(Debug, StructOpt, Clone)]
@@ -36,6 +35,10 @@ pub struct RevertCmd {
3635
#[allow(missing_docs)]
3736
#[structopt(flatten)]
3837
pub shared_params: SharedParams,
38+
39+
#[allow(missing_docs)]
40+
#[structopt(flatten)]
41+
pub pruning_params: PruningParams,
3942
}
4043

4144
impl RevertCmd {
@@ -72,6 +75,7 @@ impl RevertCmd {
7275
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
7376
{
7477
self.shared_params.update_config(&mut config, spec_factory, version)?;
78+
self.pruning_params.update_config(&mut config, Roles::FULL, true)?;
7579
config.use_in_memory_keystore()?;
7680

7781
Ok(())

client/cli/src/params/import_params.rs

+8-33
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,22 @@
1515
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use structopt::StructOpt;
18-
use sc_service::{
19-
Configuration, RuntimeGenesis,
20-
config::DatabaseConfig, PruningMode,
21-
};
18+
use sc_service::{Configuration, RuntimeGenesis, config::DatabaseConfig};
2219

2320
use crate::error;
2421
use crate::arg_enums::{
2522
WasmExecutionMethod, TracingReceiver, ExecutionStrategy, DEFAULT_EXECUTION_BLOCK_CONSTRUCTION,
2623
DEFAULT_EXECUTION_IMPORT_BLOCK, DEFAULT_EXECUTION_OFFCHAIN_WORKER, DEFAULT_EXECUTION_OTHER,
2724
DEFAULT_EXECUTION_SYNCING
2825
};
26+
use crate::params::PruningParams;
2927

3028
/// Parameters for block import.
3129
#[derive(Debug, StructOpt, Clone)]
3230
pub struct ImportParams {
33-
/// Specify the state pruning mode, a number of blocks to keep or 'archive'.
34-
///
35-
/// Default is to keep all block states if the node is running as a
36-
/// validator (i.e. 'archive'), otherwise state is only kept for the last
37-
/// 256 blocks.
38-
#[structopt(long = "pruning", value_name = "PRUNING_MODE")]
39-
pub pruning: Option<String>,
31+
#[allow(missing_docs)]
32+
#[structopt(flatten)]
33+
pub pruning_params: PruningParams,
4034

4135
/// Force start with unsafe pruning settings.
4236
///
@@ -87,7 +81,7 @@ impl ImportParams {
8781
/// Put block import CLI params into `config` object.
8882
pub fn update_config<G, E>(
8983
&self,
90-
config: &mut Configuration<G, E>,
84+
mut config: &mut Configuration<G, E>,
9185
role: sc_service::Roles,
9286
is_dev: bool,
9387
) -> error::Result<()>
@@ -102,27 +96,7 @@ impl ImportParams {
10296

10397
config.state_cache_size = self.state_cache_size;
10498

105-
// by default we disable pruning if the node is an authority (i.e.
106-
// `ArchiveAll`), otherwise we keep state for the last 256 blocks. if the
107-
// node is an authority and pruning is enabled explicitly, then we error
108-
// unless `unsafe_pruning` is set.
109-
config.pruning = match &self.pruning {
110-
Some(ref s) if s == "archive" => PruningMode::ArchiveAll,
111-
None if role == sc_service::Roles::AUTHORITY => PruningMode::ArchiveAll,
112-
None => PruningMode::default(),
113-
Some(s) => {
114-
if role == sc_service::Roles::AUTHORITY && !self.unsafe_pruning {
115-
return Err(error::Error::Input(
116-
"Validators should run with state pruning disabled (i.e. archive). \
117-
You can ignore this check with `--unsafe-pruning`.".to_string()
118-
));
119-
}
120-
121-
PruningMode::keep_blocks(s.parse()
122-
.map_err(|_| error::Error::Input("Invalid pruning mode specified".to_string()))?
123-
)
124-
},
125-
};
99+
self.pruning_params.update_config(&mut config, role, self.unsafe_pruning)?;
126100

127101
config.wasm_method = self.wasm_method.into();
128102

@@ -144,6 +118,7 @@ impl ImportParams {
144118
exec_all_or(exec.execution_offchain_worker, DEFAULT_EXECUTION_OFFCHAIN_WORKER),
145119
other: exec_all_or(exec.execution_other, DEFAULT_EXECUTION_OTHER),
146120
};
121+
147122
Ok(())
148123
}
149124
}

client/cli/src/params/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod transaction_pool_params;
1919
mod shared_params;
2020
mod node_key_params;
2121
mod network_configuration_params;
22+
mod pruning_params;
2223

2324
use std::str::FromStr;
2425
use std::fmt::Debug;
@@ -28,6 +29,7 @@ pub use crate::params::transaction_pool_params::*;
2829
pub use crate::params::shared_params::*;
2930
pub use crate::params::node_key_params::*;
3031
pub use crate::params::network_configuration_params::*;
32+
pub use crate::params::pruning_params::*;
3133

3234
/// Wrapper type of `String` that holds an unsigned integer of arbitrary size, formatted as a decimal.
3335
#[derive(Debug, Clone)]
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use structopt::StructOpt;
18+
use sc_service::{Configuration, RuntimeGenesis, PruningMode};
19+
20+
use crate::error;
21+
22+
/// Parameters to define the pruning mode
23+
#[derive(Debug, StructOpt, Clone)]
24+
pub struct PruningParams {
25+
/// Specify the state pruning mode, a number of blocks to keep or 'archive'.
26+
///
27+
/// Default is to keep all block states if the node is running as a
28+
/// validator (i.e. 'archive'), otherwise state is only kept for the last
29+
/// 256 blocks.
30+
#[structopt(long = "pruning", value_name = "PRUNING_MODE")]
31+
pub pruning: Option<String>,
32+
}
33+
34+
impl PruningParams {
35+
/// Put block pruning CLI params into `config` object.
36+
pub fn update_config<G, E>(
37+
&self,
38+
mut config: &mut Configuration<G, E>,
39+
role: sc_service::Roles,
40+
unsafe_pruning: bool,
41+
) -> error::Result<()>
42+
where
43+
G: RuntimeGenesis,
44+
{
45+
// by default we disable pruning if the node is an authority (i.e.
46+
// `ArchiveAll`), otherwise we keep state for the last 256 blocks. if the
47+
// node is an authority and pruning is enabled explicitly, then we error
48+
// unless `unsafe_pruning` is set.
49+
config.pruning = match &self.pruning {
50+
Some(ref s) if s == "archive" => PruningMode::ArchiveAll,
51+
None if role == sc_service::Roles::AUTHORITY => PruningMode::ArchiveAll,
52+
None => PruningMode::default(),
53+
Some(s) => {
54+
if role == sc_service::Roles::AUTHORITY && !unsafe_pruning {
55+
return Err(error::Error::Input(
56+
"Validators should run with state pruning disabled (i.e. archive). \
57+
You can ignore this check with `--unsafe-pruning`.".to_string()
58+
));
59+
}
60+
61+
PruningMode::keep_blocks(s.parse()
62+
.map_err(|_| error::Error::Input("Invalid pruning mode specified".to_string()))?
63+
)
64+
},
65+
};
66+
67+
Ok(())
68+
}
69+
}

0 commit comments

Comments
 (0)