diff --git a/parity/cli/mod.rs b/parity/cli/mod.rs index 561f54771..6735f9685 100644 --- a/parity/cli/mod.rs +++ b/parity/cli/mod.rs @@ -716,7 +716,9 @@ usage! { "--no-persistent-txqueue", "Don't save pending local transactions to disk to be restored whenever the node restarts.", - FLAG flag_stratum: (bool) = false, or |c: &Config| Some(c.stratum.is_some()), + // For backward compatibility; Stratum should be enabled if the config file + // contains a `[stratum]` section and it is not explicitly disabled (disable = true) + FLAG flag_stratum: (bool) = false, or |c: &Config| Some(c.stratum.as_ref().map(|s| s.disable != Some(true)).unwrap_or(false)), "--stratum", "Run Stratum server for miner push notification.", @@ -1379,6 +1381,7 @@ struct Mining { #[derive(Default, Debug, PartialEq, Deserialize)] #[serde(deny_unknown_fields)] struct Stratum { + disable: Option, interface: Option, port: Option, secret: Option, @@ -1595,6 +1598,83 @@ mod tests { assert_eq!(args.arg_pruning_history, 128); } + #[test] + fn should_disable_stratum() { + // given + let config = toml::from_str(include_str!("./tests/config.stratum_disabled.toml")).unwrap(); + + // when + let args = Args::parse_with_config(&["parity"], config).unwrap(); + + // then + assert_eq!(args.flag_stratum, false); + assert_eq!(args.arg_stratum_interface, "local".to_owned()); + assert_eq!(args.arg_stratum_port, 8008u16); + assert_eq!(args.arg_stratum_secret, None); + } + + #[test] + fn should_disable_stratum_when_missing_section() { + // given + let config = toml::from_str(include_str!("./tests/config.stratum_missing_section.toml")).unwrap(); + + // when + let args = Args::parse_with_config(&["parity"], config).unwrap(); + + // then + assert_eq!(args.flag_stratum, false); + assert_eq!(args.arg_stratum_interface, "local".to_owned()); + assert_eq!(args.arg_stratum_port, 8008u16); + assert_eq!(args.arg_stratum_secret, None); + } + + #[test] + fn should_enable_stratum() { + // given + let config = toml::from_str(include_str!("./tests/config.stratum_enabled.toml")).unwrap(); + + // when + let args = Args::parse_with_config(&["parity"], config).unwrap(); + + // then (with custom configurations) + assert_eq!(args.flag_stratum, true); + assert_eq!(args.arg_stratum_interface, "some_interface".to_owned()); + assert_eq!(args.arg_stratum_port, 8007u16); + assert_eq!(args.arg_stratum_secret, Some("Yellow".to_owned())); + } + + #[test] + fn should_enable_stratum_by_param() { + // given + let config = toml::from_str(include_str!("./tests/config.full.toml")).unwrap(); + + // when + let args = Args::parse_with_config(&["parity", "--stratum"], config).unwrap(); + + // then + assert_eq!(args.flag_stratum, true); + assert_eq!(args.arg_stratum_interface, "local".to_owned()); + assert_eq!(args.arg_stratum_port, 8008u16); + assert_eq!(args.arg_stratum_secret, None); + } + + #[test] + // For backward compatibility; Stratum should be enabled if the config file + // contains a `[stratum]` section and it is not explicitly disabled (disable = true) + fn should_enable_stratum_when_missing_field() { + // given + let config = toml::from_str(include_str!("./tests/config.stratum_missing_field.toml")).unwrap(); + + // when + let args = Args::parse_with_config(&["parity"], config).unwrap(); + + // then + assert_eq!(args.flag_stratum, true); + assert_eq!(args.arg_stratum_interface, "local".to_owned()); + assert_eq!(args.arg_stratum_port, 8008u16); + assert_eq!(args.arg_stratum_secret, None); + } + #[test] fn should_parse_full_config() { // given diff --git a/parity/cli/tests/config.stratum_disabled.toml b/parity/cli/tests/config.stratum_disabled.toml new file mode 100644 index 000000000..b7f99af7e --- /dev/null +++ b/parity/cli/tests/config.stratum_disabled.toml @@ -0,0 +1,2 @@ +[stratum] +disable = true \ No newline at end of file diff --git a/parity/cli/tests/config.stratum_enabled.toml b/parity/cli/tests/config.stratum_enabled.toml new file mode 100644 index 000000000..5b46c0c0c --- /dev/null +++ b/parity/cli/tests/config.stratum_enabled.toml @@ -0,0 +1,5 @@ +[stratum] +disable = false +interface = "some_interface" +port = 8007 +secret = "Yellow" \ No newline at end of file diff --git a/parity/cli/tests/config.stratum_missing_field.toml b/parity/cli/tests/config.stratum_missing_field.toml new file mode 100644 index 000000000..7ceed1815 --- /dev/null +++ b/parity/cli/tests/config.stratum_missing_field.toml @@ -0,0 +1 @@ +[stratum] \ No newline at end of file diff --git a/parity/cli/tests/config.stratum_missing_section.toml b/parity/cli/tests/config.stratum_missing_section.toml new file mode 100644 index 000000000..65efa412c --- /dev/null +++ b/parity/cli/tests/config.stratum_missing_section.toml @@ -0,0 +1 @@ +# No `[stratum]` section \ No newline at end of file