Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/semver client #92

Merged
merged 2 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/topology/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde = { version = "1.0.104", features = ["derive"] }

## internal
addressing = {path = "../addressing"}
version-checker = {path = "../version-checker" }

## will be moved to proper dependencies once released
sphinx = { git = "https://github.com/nymtech/sphinx", rev="1d8cefcb6a0cb8e87d00d89eb1ccf2839e92aa1f" }
19 changes: 13 additions & 6 deletions common/topology/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sphinx::route::{Node as SphinxNode, NodeAddressBytes};
use std::cmp::max;
use std::collections::HashMap;
use std::net::SocketAddr;
use version_checker;

#[derive(Debug, Clone)]
pub struct MixNode {
Expand Down Expand Up @@ -167,27 +168,33 @@ pub trait NymTopology: Sized {

fn filter_node_versions(
&self,
mix_version: &str,
provider_version: &str,
coco_version: &str,
expected_mix_version: &str,
expected_provider_version: &str,
expected_coco_version: &str,
) -> Self {
let filtered_mixes = self
.get_mix_nodes()
.iter()
.filter(|mix_node| {
version_checker::is_compatible(&mix_node.version, expected_mix_version)
})
.cloned()
.filter(|mix_node| mix_node.version == mix_version)
.collect();
let filtered_providers = self
.get_mix_provider_nodes()
.iter()
.filter(|provider_node| {
version_checker::is_compatible(&provider_node.version, expected_provider_version)
})
.cloned()
.filter(|provider_node| provider_node.version == provider_version)
.collect();
let filtered_coco_nodes = self
.get_coco_nodes()
.iter()
.filter(|coco_node| {
version_checker::is_compatible(&coco_node.version, expected_coco_version)
})
.cloned()
.filter(|coco_node| coco_node.version == coco_version)
.collect();

Self::new_from_nodes(filtered_mixes, filtered_providers, filtered_coco_nodes)
Expand Down
3 changes: 3 additions & 0 deletions common/version-checker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock
10 changes: 10 additions & 0 deletions common/version-checker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "version-checker"
version = "0.1.0"
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
semver = "0.9.0"
51 changes: 51 additions & 0 deletions common/version-checker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use semver::Version;
use semver::VersionReq;

/// Checks whether given `version` is compatible with a given semantic
/// version requirement `req` according to major-minor semver rules.
/// The semantic version requirement can be passed as a full, concrete version
/// number, because that's what we'll have in our Cargo.toml files (e.g. 0.3.2).
/// The patch number in the requirement will be dropped and replaced with a wildcard (0.3.*) as all minor versions should be compatible with each other.
pub fn is_compatible(version: &str, req: &str) -> bool {
let version = Version::parse(version).unwrap();
let tmp = Version::parse(req).unwrap();
let wildcard = format!("{}.{}.*", tmp.major, tmp.minor).to_string();
let semver_requirement = VersionReq::parse(&wildcard).unwrap();

semver_requirement.matches(&version)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn version_0_3_0_is_compatible_with_requirement_0_3_x() {
assert!(is_compatible("0.3.0", "0.3.2"));
}

#[test]
fn version_0_3_1_is_compatible_with_minimum_requirement_0_3_x() {
assert!(is_compatible("0.3.1", "0.3.2"));
}

#[test]
fn version_0_3_2_is_compatible_with_minimum_requirement_0_3_x() {
assert!(is_compatible("0.3.2", "0.3.0"));
}

#[test]
fn version_0_2_0_is_not_compatible_with_requirement_0_3_x() {
assert!(!is_compatible("0.2.0", "0.3.2"));
}

#[test]
fn version_0_4_0_is_not_compatible_with_requirement_0_3_x() {
assert!(!is_compatible("0.4.0", "0.3.2"));
}

#[test]
fn version_1_3_2_is_not_compatible_with_requirement_0_3_x() {
assert!(!is_compatible("1.3.2", "0.3.2"));
}
}