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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use crate::sources;

use log::{debug, error};
use rand::seq::SliceRandom;
use std::collections::HashMap;
use std::net::IpAddr;
use std::option::Option;
use std::vec::Vec;

use crate::sources::Family;

/// Type alias for easier usage of the library
pub type Sources = Vec<Box<dyn sources::Source>>;

use std::default::Default;

/// Policies for Consensus resolution
#[derive(Debug, Copy, Clone, Default)]
pub enum Policy {
    /// Requires all sources to be queried, it will ignore the sources returning errors but and it
    /// will return the IP with the most replies as the result.
    All,
    /// Will test the sources one by one in order until there's one success and will return it as
    /// the result.
    First,
    /// Will test the sources one by one in random order until there's one success and will return
    /// it as the result.
    #[default]
    Random,
}

/// Consensus system that aggregates the various sources of information and returns the most common
/// reply
pub struct Consensus {
    voters: Sources,
    policy: Policy,
    family: Family,
}

/// Consensus builder
pub struct ConsensusBuilder {
    voters: Sources,
    policy: Policy,
    family: Family,
}

impl Default for ConsensusBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ConsensusBuilder {
    pub fn new() -> ConsensusBuilder {
        ConsensusBuilder {
            voters: vec![],
            policy: Policy::default(),
            family: Family::default(),
        }
    }

    /// Adds sources to the builder
    ///
    /// # Arguments
    ///
    /// * `source` - Iterable of sources to add
    pub fn add_sources<T>(mut self, source: T) -> ConsensusBuilder
    where
        T: IntoIterator<Item = Box<dyn sources::Source>>,
    {
        self.voters.extend(source);
        self
    }

    pub fn policy(mut self, policy: Policy) -> ConsensusBuilder {
        self.policy = policy;
        self
    }

    pub fn family(mut self, family: Family) -> ConsensusBuilder {
        self.family = family;
        self
    }

    /// Returns the configured consensus struct from the builder
    pub fn build(self) -> Consensus {
        Consensus {
            voters: self.voters,
            policy: self.policy,
            family: self.family,
        }
    }
}

impl Consensus {
    /// Returns the IP address it found or None if no source worked.
    pub async fn get_consensus(&self) -> Option<IpAddr> {
        match self.policy {
            Policy::All => self.all().await,
            Policy::First => self.first().await,
            Policy::Random => self.random().await,
        }
    }

    async fn all(&self) -> Option<IpAddr> {
        let results =
            futures::future::join_all(self.voters.iter().map(|voter| voter.get_ip(self.family)))
                .await;

        debug!("Results {:?}", results);
        let mut accumulate = HashMap::new();
        for (pos, result) in results.into_iter().enumerate() {
            match result {
                Ok(result) => {
                    accumulate
                        .entry(result)
                        .and_modify(|c| *c += 1)
                        .or_insert(1);
                }
                Err(err) => error!("Source {} failed {:?}", self.voters[pos], err),
            };
        }

        let mut ordered_output: Vec<_> = accumulate.iter().collect();
        ordered_output.sort_unstable_by(|(_, a), (_, b)| a.cmp(b));
        debug!("Sorted results {:?}", ordered_output);

        ordered_output.pop().map(|x| *x.0)
    }

    async fn first(&self) -> Option<IpAddr> {
        for voter in &self.voters {
            let result = voter.get_ip(self.family).await;
            debug!("Results {:?}", result);
            if result.is_ok() {
                return result.ok();
            }
        }
        debug!("Tried all sources");
        None
    }

    async fn random(&self) -> Option<IpAddr> {
        let mut rng = rand::thread_rng();
        for voter in self.voters.choose_multiple(&mut rng, self.voters.len()) {
            let result = voter.get_ip(self.family).await;
            debug!("Results {:?}", result);
            if result.is_ok() {
                return result.ok();
            }
        }
        debug!("Tried all sources");
        None
    }
}

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

    use crate::sources::MockSource;
    use mockall::predicate::eq;
    use std::net::Ipv4Addr;
    use tokio_test::block_on;

    const IP0: IpAddr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0));

    fn make_success(ip: IpAddr) -> Box<dyn sources::Source> {
        let mut mock = MockSource::new();
        mock.expect_get_ip()
            .with(eq(Family::Any))
            .times(1)
            .returning(move |_| Box::pin(futures::future::ready(Ok(ip))));
        Box::new(mock)
    }

    fn make_fail() -> Box<dyn sources::Source> {
        let mut mock = MockSource::new();
        mock.expect_get_ip()
            .with(eq(Family::Any))
            .times(1)
            .returning(move |_| {
                let invalid_ip: Result<IpAddr, std::net::AddrParseError> = "x.0.0.0".parse();
                Box::pin(futures::future::ready(Err(sources::Error::InvalidAddress(
                    invalid_ip.err().unwrap(),
                ))))
            });
        Box::new(mock)
    }

    fn make_untouched() -> Box<dyn sources::Source> {
        let mut mock = MockSource::new();
        mock.expect_get_ip().with(eq(Family::Any)).times(0);
        Box::new(mock)
    }

    #[test]
    fn test_success() {
        let sources: Sources = vec![make_success(IP0)];
        let consensus = ConsensusBuilder::new().add_sources(sources).build();
        let result = consensus.get_consensus();
        let value = block_on(result);
        assert_eq!(Some(IP0), value);
    }

    #[test]
    fn test_all_success_multiple_same() {
        let consensus = ConsensusBuilder::new()
            .add_sources(vec![make_success(IP0), make_success(IP0)])
            .policy(Policy::All)
            .build();

        let result = consensus.get_consensus();
        let value = block_on(result);
        assert_eq!(Some(IP0), value);
    }

    #[test]
    fn test_all_success_multiple_same_diff() {
        let ip2 = "0.0.0.1".parse().expect("valid ip");
        let consensus = ConsensusBuilder::new()
            .policy(Policy::All)
            .add_sources(vec![
                make_success(IP0),
                make_success(IP0),
                make_success(ip2),
            ])
            .build();

        let result = consensus.get_consensus();
        let value = block_on(result);
        assert_eq!(Some(IP0), value);
    }

    #[test]
    fn test_all_success_multiple_with_fails() {
        let consensus = ConsensusBuilder::new()
            .add_sources(vec![make_success(IP0), make_fail()])
            .policy(Policy::All)
            .build();
        let result = consensus.get_consensus();
        let value = block_on(result);
        assert_eq!(Some(IP0), value);
    }

    #[test]
    fn test_only_failures() {
        for policy in [Policy::All, Policy::Random, Policy::First].iter() {
            let consensus = ConsensusBuilder::new()
                .add_sources(vec![make_fail()])
                .policy(*policy)
                .build();
            let result = consensus.get_consensus();
            let value = block_on(result);
            assert_eq!(None, value);
        }
    }

    #[test]
    fn test_add_sources_multiple_times() {
        let consensus = ConsensusBuilder::new()
            .add_sources(vec![make_fail()])
            .add_sources(vec![make_success(IP0)])
            .policy(Policy::All)
            .build();
        let result = consensus.get_consensus();
        let value = block_on(result);
        assert_eq!(Some(IP0), value);
    }

    #[test]
    fn test_first_success_multiple_with_fails() {
        let consensus = ConsensusBuilder::new()
            .add_sources(vec![make_fail(), make_success(IP0)])
            .policy(Policy::First)
            .build();
        let result = consensus.get_consensus();
        let value = block_on(result);
        assert_eq!(Some(IP0), value);
    }

    #[test]
    fn test_first_success_with_first_success() {
        let consensus = ConsensusBuilder::new()
            .add_sources(vec![make_success(IP0), make_untouched()])
            .policy(Policy::First)
            .build();
        let result = consensus.get_consensus();
        let value = block_on(result);
        assert_eq!(Some(IP0), value);
    }
}