Parity as a library (#8412)

* Parity as a library

* Fix concerns

* Allow using a null on_client_restart_cb

* Fix more concerns

* Test the C library in test.sh

* Reduce CMake version to 3.5

* Move the clib test before cargo test

* Add println in test
This commit is contained in:
Pierre Krieger
2018-05-09 08:47:21 +02:00
committed by Afri Schoedon
parent 28c731881f
commit ac3de4c5fc
16 changed files with 705 additions and 337 deletions

View File

@@ -92,23 +92,30 @@ pub struct Execute {
pub cmd: Cmd,
}
/// Configuration for the Parity client.
#[derive(Debug, PartialEq)]
pub struct Configuration {
/// Arguments to be interpreted.
pub args: Args,
}
impl Configuration {
pub fn parse<S: AsRef<str>>(command: &[S]) -> Result<Self, ArgsError> {
let args = Args::parse(command)?;
/// Parses a configuration from a list of command line arguments.
///
/// # Example
///
/// ```
/// let _cfg = parity::Configuration::parse_cli(&["--light", "--chain", "koven"]).unwrap();
/// ```
pub fn parse_cli<S: AsRef<str>>(command: &[S]) -> Result<Self, ArgsError> {
let config = Configuration {
args: args,
args: Args::parse(command)?,
};
Ok(config)
}
pub fn into_command(self) -> Result<Execute, String> {
pub(crate) fn into_command(self) -> Result<Execute, String> {
let dirs = self.directories();
let pruning = self.args.arg_pruning.parse()?;
let pruning_history = self.args.arg_pruning_history;
@@ -1843,7 +1850,7 @@ mod tests {
let filename = tempdir.path().join("peers");
File::create(&filename).unwrap().write_all(b" \n\t\n").unwrap();
let args = vec!["parity", "--reserved-peers", filename.to_str().unwrap()];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
assert!(conf.init_reserved_nodes().is_ok());
}
@@ -1853,7 +1860,7 @@ mod tests {
let filename = tempdir.path().join("peers_comments");
File::create(&filename).unwrap().write_all(b"# Sample comment\nenode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@172.0.0.1:30303\n").unwrap();
let args = vec!["parity", "--reserved-peers", filename.to_str().unwrap()];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
let reserved_nodes = conf.init_reserved_nodes();
assert!(reserved_nodes.is_ok());
assert_eq!(reserved_nodes.unwrap().len(), 1);
@@ -1862,7 +1869,7 @@ mod tests {
#[test]
fn test_dev_preset() {
let args = vec!["parity", "--config", "dev"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_settings.chain, "dev");
@@ -1876,7 +1883,7 @@ mod tests {
#[test]
fn test_mining_preset() {
let args = vec!["parity", "--config", "mining"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 50);
@@ -1898,7 +1905,7 @@ mod tests {
#[test]
fn test_non_standard_ports_preset() {
let args = vec!["parity", "--config", "non-standard-ports"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_settings.network_port, 30305);
@@ -1911,7 +1918,7 @@ mod tests {
#[test]
fn test_insecure_preset() {
let args = vec!["parity", "--config", "insecure"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.update_policy.require_consensus, false);
@@ -1931,7 +1938,7 @@ mod tests {
#[test]
fn test_dev_insecure_preset() {
let args = vec!["parity", "--config", "dev-insecure"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_settings.chain, "dev");
@@ -1954,7 +1961,7 @@ mod tests {
#[test]
fn test_override_preset() {
let args = vec!["parity", "--config", "mining", "--min-peers=99"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 99);
@@ -2077,7 +2084,7 @@ mod tests {
#[test]
fn should_respect_only_max_peers_and_default() {
let args = vec!["parity", "--max-peers=50"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 25);
@@ -2090,7 +2097,7 @@ mod tests {
#[test]
fn should_respect_only_max_peers_less_than_default() {
let args = vec!["parity", "--max-peers=5"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 5);
@@ -2103,7 +2110,7 @@ mod tests {
#[test]
fn should_respect_only_min_peers_and_default() {
let args = vec!["parity", "--min-peers=5"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 5);
@@ -2116,7 +2123,7 @@ mod tests {
#[test]
fn should_respect_only_min_peers_and_greater_than_default() {
let args = vec!["parity", "--min-peers=500"];
let conf = Configuration::parse(&args).unwrap();
let conf = Configuration::parse_cli(&args).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 500);