1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use std::cmp::Ordering;
use std::fmt::{self, Debug, Display, Formatter};
use std::str::FromStr;

use anyhow::{format_err, Error, Result};
use semver::Version;

pub use bellperson::groth16::aggregate::AggregateVersion;

/// The ApiVersion enum is used for mandatory changes that the network
/// must use and recognize.
///
/// New versions always require new network behaviour.
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum ApiVersion {
    V1_0_0,
    V1_1_0,
    V1_2_0,
}

impl Ord for ApiVersion {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_semver().cmp(&other.as_semver())
    }
}

impl PartialOrd for ApiVersion {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl ApiVersion {
    pub fn as_semver(&self) -> Version {
        match self {
            ApiVersion::V1_0_0 => Version::new(1, 0, 0),
            ApiVersion::V1_1_0 => Version::new(1, 1, 0),
            ApiVersion::V1_2_0 => Version::new(1, 2, 0),
        }
    }

    #[inline]
    pub fn supports_feature(&self, feat: &ApiFeature) -> bool {
        self >= &feat.first_supported_version()
            && feat
                .last_supported_version()
                .map(|v_last| self <= &v_last)
                .unwrap_or(true)
    }

    #[inline]
    pub fn supports_features(&self, feats: &[ApiFeature]) -> bool {
        feats.iter().all(|feat| self.supports_feature(feat))
    }
}

impl Debug for ApiVersion {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let semver = self.as_semver();
        write!(f, "{}.{}.{}", semver.major, semver.minor, semver.patch)
    }
}

impl Display for ApiVersion {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let semver = self.as_semver();
        write!(f, "{}.{}.{}", semver.major, semver.minor, semver.patch)
    }
}

impl FromStr for ApiVersion {
    type Err = Error;
    fn from_str(api_version_str: &str) -> Result<Self> {
        let api_version = Version::parse(api_version_str)?;
        match (api_version.major, api_version.minor, api_version.patch) {
            (1, 0, 0) => Ok(ApiVersion::V1_0_0),
            (1, 1, 0) => Ok(ApiVersion::V1_1_0),
            (1, 2, 0) => Ok(ApiVersion::V1_2_0),
            (1, 0, _) | (1, 1, _) | (1, 2, _) => Err(format_err!(
                "Could not parse API Version from string (patch)"
            )),
            (1, _, _) => Err(format_err!(
                "Could not parse API Version from string (minor)"
            )),
            _ => Err(format_err!(
                "Could not parse API Version from string (major)"
            )),
        }
    }
}

/// The ApiFeature enum is used for optional features that the network
/// can use and recognize, but in no way is required to be used.
///
/// New features always require new network behaviour (i.e. for proper
/// validation of others, even if not actively using)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ApiFeature {
    SyntheticPoRep,
    NonInteractivePoRep,
}

impl ApiFeature {
    #[inline]
    pub fn first_supported_version(&self) -> ApiVersion {
        match self {
            ApiFeature::SyntheticPoRep | ApiFeature::NonInteractivePoRep => ApiVersion::V1_2_0,
        }
    }

    #[inline]
    pub fn last_supported_version(&self) -> Option<ApiVersion> {
        match self {
            ApiFeature::SyntheticPoRep | ApiFeature::NonInteractivePoRep => None,
        }
    }

    /// Return the features that are in conflict with the current one.
    pub fn conflicting_features(&self) -> &[ApiFeature] {
        match self {
            ApiFeature::SyntheticPoRep => &[ApiFeature::NonInteractivePoRep],
            ApiFeature::NonInteractivePoRep => &[ApiFeature::SyntheticPoRep],
        }
    }
}

impl Display for ApiFeature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let api_feature_str = match self {
            Self::SyntheticPoRep => "synthetic-porep",
            Self::NonInteractivePoRep => "non-interactive-porep",
        };
        write!(f, "{}", api_feature_str)
    }
}

impl FromStr for ApiFeature {
    type Err = Error;
    fn from_str(api_feature_str: &str) -> Result<Self> {
        match api_feature_str {
            "synthetic-porep" => Ok(ApiFeature::SyntheticPoRep),
            "non-interactive-porep" => Ok(ApiFeature::NonInteractivePoRep),
            _ => Err(format_err!(
                "'{}' cannot be parsed as valid API feature",
                api_feature_str
            )),
        }
    }
}

#[test]
fn test_fmt() {
    assert_eq!(format!("{}", ApiVersion::V1_0_0), "1.0.0");
    assert_eq!(format!("{}", ApiVersion::V1_1_0), "1.1.0");
    assert_eq!(format!("{}", ApiVersion::V1_2_0), "1.2.0");
}

#[test]
fn test_as_semver() {
    assert_eq!(ApiVersion::V1_0_0.as_semver().major, 1);
    assert_eq!(ApiVersion::V1_1_0.as_semver().major, 1);
    assert_eq!(ApiVersion::V1_2_0.as_semver().major, 1);
    assert_eq!(ApiVersion::V1_0_0.as_semver().minor, 0);
    assert_eq!(ApiVersion::V1_1_0.as_semver().minor, 1);
    assert_eq!(ApiVersion::V1_2_0.as_semver().minor, 2);
    assert_eq!(ApiVersion::V1_0_0.as_semver().patch, 0);
    assert_eq!(ApiVersion::V1_1_0.as_semver().patch, 0);
    assert_eq!(ApiVersion::V1_2_0.as_semver().patch, 0);
}

#[test]
fn test_api_version_order() {
    assert!(ApiVersion::V1_0_0 < ApiVersion::V1_1_0 && ApiVersion::V1_1_0 < ApiVersion::V1_2_0);
    assert!(ApiVersion::V1_1_0 > ApiVersion::V1_0_0 && ApiVersion::V1_2_0 > ApiVersion::V1_1_0);
}

#[test]
fn test_api_feature_synthetic_porep() {
    let feature = ApiFeature::SyntheticPoRep;

    assert_eq!(format!("{}", feature), "synthetic-porep");
    assert_eq!(
        ApiFeature::from_str("synthetic-porep").expect("can be parsed"),
        feature
    );

    assert!(feature.first_supported_version() == ApiVersion::V1_2_0);
    assert!(feature.last_supported_version().is_none());
}

#[test]
fn test_api_feature_non_interactive_porep() {
    let feature = ApiFeature::NonInteractivePoRep;
    assert!(feature.first_supported_version() == ApiVersion::V1_2_0);
    assert!(feature.last_supported_version().is_none());
}