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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use super::*;

/// Find all minimal quorums in the FBAS.
pub fn find_minimal_quorums(fbas: &Fbas) -> Vec<NodeIdSet> {
    info!("Starting to look for minimal quorums...");
    let minimal_quorums = find_minimal_sets(fbas, minimal_quorums_finder);
    info!("Found {} minimal quorums.", minimal_quorums.len());
    minimal_quorums
}

/// Find at least two non-intersecting quorums. Use this function if you don't want to enumerate
/// all minimal quorums and/or it is likely that the FBAS lacks quorum intersection and you want to
/// stop early in such cases.
pub fn find_nonintersecting_quorums(fbas: &Fbas) -> Option<Vec<NodeIdSet>> {
    info!("Starting to look for potentially non-intersecting quorums...");
    let quorums = find_sets(fbas, nonintersecting_quorums_finder);
    if quorums.len() < 2 {
        info!("Found no non-intersecting quorums.");
        None
    } else {
        warn!(
            "Found {} non-intersecting quorums (there could more).",
            quorums.len()
        );
        Some(quorums)
    }
}

fn minimal_quorums_finder(consensus_clusters: Vec<NodeIdSet>, fbas: &Fbas) -> Vec<NodeIdSet> {
    let mut found_quorums: Vec<NodeIdSet> = vec![];

    for (i, nodes) in consensus_clusters.into_iter().enumerate() {
        debug!("Finding minimal quorums in cluster {}...", i);

        if let Some(symmetric_cluster) =
            is_symmetric_cluster(&nodes, &fbas.with_standard_form_quorum_sets())
        {
            debug!("Cluster contains a symmetric quorum cluster! Extracting quorums...");
            found_quorums.append(&mut symmetric_cluster.to_minimal_quorums(fbas));
        } else {
            debug!("Sorting nodes by rank...");
            let sorted_candidate_nodes = sort_by_rank(nodes.into_iter().collect(), fbas);
            debug!("Sorted.");

            debug!("Looking for symmetric nodes...");
            let symmetric_nodes = find_symmetric_nodes_in_node_set(&nodes, fbas);
            debug!("Done.");

            let mut found_unexpanded_quorums_in_this_cluster = vec![];

            debug!("Collecting quorums...");
            minimal_quorums_finder_step(
                &mut CandidateValuesMq::new(sorted_candidate_nodes),
                &mut found_unexpanded_quorums_in_this_cluster,
                &FbasValues::new(fbas, &symmetric_nodes),
                true,
            );
            found_quorums
                .append(&mut symmetric_nodes.expand_sets(found_unexpanded_quorums_in_this_cluster))
        }
    }
    found_quorums
}
fn minimal_quorums_finder_step(
    candidates: &mut CandidateValuesMq,
    found_quorums: &mut Vec<NodeIdSet>,
    fbas_values: &FbasValues,
    selection_changed: bool,
) {
    if selection_changed && fbas_values.fbas.is_quorum(&candidates.selection) {
        if is_minimal_for_quorum(&candidates.selection, fbas_values.fbas) {
            found_quorums.push(candidates.selection.clone());
            if found_quorums.len() % 100_000 == 0 {
                debug!("...{} quorums found", found_quorums.len());
            }
        }
    } else if let Some(current_candidate) = candidates.unprocessed.pop_front() {
        // We require that symmetric nodes are used in a fixed order; this way we can omit
        // redundant branches (we expand all combinations of symmetric nodes in the final result
        // sets).
        if fbas_values
            .symmetric_nodes
            .is_non_redundant_next(current_candidate, &candidates.selection)
        {
            candidates.selection.insert(current_candidate);
            minimal_quorums_finder_step(candidates, found_quorums, fbas_values, true);
            candidates.selection.remove(current_candidate);
        }
        candidates.available.remove(current_candidate);

        if selection_satisfiable(
            &candidates.selection,
            &candidates.available,
            fbas_values.fbas,
        ) {
            minimal_quorums_finder_step(candidates, found_quorums, fbas_values, false);
        }
        candidates.unprocessed.push_front(current_candidate);
        candidates.available.insert(current_candidate);
    }
}

fn nonintersecting_quorums_finder(
    consensus_clusters: Vec<NodeIdSet>,
    fbas: &Fbas,
) -> Vec<NodeIdSet> {
    if consensus_clusters.len() > 1 {
        debug!("More than one consensus clusters - reducing to maximal quorums.");
        consensus_clusters
            .into_iter()
            .map(|node_set| find_satisfiable_nodes(&node_set, fbas).0)
            .collect()
    } else {
        warn!("There is only one consensus cluster - there might be no non-intersecting quorums and the subsequent search might be slow.");
        let nodes = consensus_clusters.into_iter().next().unwrap_or_default();
        nonintersecting_quorums_finder_using_cluster(&nodes, fbas)
    }
}
fn nonintersecting_quorums_finder_using_cluster(cluster: &NodeIdSet, fbas: &Fbas) -> Vec<BitSet> {
    if let Some(symmetric_cluster) =
        is_symmetric_cluster(cluster, &fbas.with_standard_form_quorum_sets())
    {
        debug!("Cluster contains a symmetric quorum cluster! Extracting using that...");
        if let Some((quorum1, quorum2)) = symmetric_cluster.has_nonintersecting_quorums() {
            vec![quorum1, quorum2]
        } else {
            vec![]
        }
    } else {
        debug!("Sorting nodes by rank...");
        let sorted_nodes = sort_by_rank(cluster.iter().collect(), fbas);
        debug!("Sorted.");

        nonintersecting_quorums_finder_using_sorted_nodes(sorted_nodes, fbas)
    }
}
// This function is used many times in a row in `find_minimal_splitting_sets`, hence we use logging
// more sparingly.
pub(crate) fn nonintersecting_quorums_finder_using_sorted_nodes(
    sorted_nodes: Vec<usize>,
    fbas: &Fbas,
) -> Vec<BitSet> {
    let mut candidates = CandidateValuesNi::new(sorted_nodes);
    let symmetric_nodes = find_symmetric_nodes_in_node_set(&candidates.available, fbas);

    // testing bigger quorums yields no benefit
    let picks_left = candidates.unprocessed.len() / 2;

    if let Some(intersecting_quorums) = nonintersecting_quorums_finder_step(
        &mut candidates,
        &FbasValues::new(fbas, &symmetric_nodes),
        picks_left,
        true,
    ) {
        assert!(intersecting_quorums.iter().all(|x| fbas.is_quorum(x)));
        assert!(intersecting_quorums[0].is_disjoint(&intersecting_quorums[1]));
        intersecting_quorums.to_vec()
    } else {
        assert!(fbas.is_quorum(&candidates.available));
        vec![candidates.available.clone()]
    }
}
fn nonintersecting_quorums_finder_step(
    candidates: &mut CandidateValuesNi,
    fbas_values: &FbasValues,
    picks_left: usize,
    selection_changed: bool,
) -> Option<[NodeIdSet; 2]> {
    debug_assert!(candidates.selection.is_disjoint(&candidates.antiselection));

    if selection_changed && fbas_values.fbas.is_quorum(&candidates.selection) {
        let (potential_complement, _) =
            find_satisfiable_nodes(&candidates.antiselection, fbas_values.fbas);

        if !potential_complement.is_empty() {
            return Some([candidates.selection.clone(), potential_complement]);
        }
    } else if picks_left == 0 {
        return None;
    } else if let Some(current_candidate) = candidates.unprocessed.pop_front() {
        // We require that symmetric nodes are used in a fixed order; this way we can omit
        // redundant branches.
        if fbas_values
            .symmetric_nodes
            .is_non_redundant_next(current_candidate, &candidates.selection)
        {
            candidates.selection.insert(current_candidate);
            candidates.antiselection.remove(current_candidate);

            if let Some(intersecting_quorums) =
                nonintersecting_quorums_finder_step(candidates, fbas_values, picks_left - 1, true)
            {
                return Some(intersecting_quorums);
            }
            candidates.selection.remove(current_candidate);
            candidates.antiselection.insert(current_candidate);
        }
        candidates.available.remove(current_candidate);

        if selection_satisfiable(
            &candidates.selection,
            &candidates.available,
            fbas_values.fbas,
        ) {
            if let Some(intersecting_quorums) =
                nonintersecting_quorums_finder_step(candidates, fbas_values, picks_left, false)
            {
                return Some(intersecting_quorums);
            }
        }
        candidates.unprocessed.push_front(current_candidate);
        candidates.available.insert(current_candidate);
    }
    None
}

#[derive(Debug, Clone)]
struct CandidateValuesMq {
    selection: NodeIdSet,
    unprocessed: NodeIdDeque,
    available: NodeIdSet,
}
impl CandidateValuesMq {
    fn new(sorted_nodes_to_process: Vec<NodeId>) -> Self {
        let selection = bitset![];
        let unprocessed: NodeIdDeque = sorted_nodes_to_process.into();
        let available = unprocessed.iter().copied().collect();
        Self {
            selection,
            unprocessed,
            available,
        }
    }
}

#[derive(Debug, Clone)]
struct CandidateValuesNi {
    selection: NodeIdSet,
    unprocessed: NodeIdDeque,
    available: NodeIdSet,
    antiselection: NodeIdSet,
}
impl CandidateValuesNi {
    fn new(sorted_nodes_to_process: Vec<NodeId>) -> Self {
        let selection = bitset![];
        let unprocessed: NodeIdDeque = sorted_nodes_to_process.into();
        let available: NodeIdSet = unprocessed.iter().copied().collect();
        let antiselection = available.clone();
        Self {
            selection,
            unprocessed,
            available,
            antiselection,
        }
    }
}

#[derive(Debug, Clone)]
struct FbasValues<'a> {
    fbas: &'a Fbas,
    symmetric_nodes: &'a SymmetricNodesMap,
}
impl<'a> FbasValues<'a> {
    fn new(fbas: &'a Fbas, symmetric_nodes: &'a SymmetricNodesMap) -> Self {
        Self {
            fbas,
            symmetric_nodes,
        }
    }
}

impl QuorumSet {
    /// Makes sense if the quorum set represents a symmetric quorum cluster...
    fn to_minimal_quorums(&self, fbas: &Fbas) -> Vec<NodeIdSet> {
        let quorums = self.to_quorum_slices();
        if self.contains_duplicates() {
            remove_non_minimal_x(quorums, is_minimal_for_quorum, fbas)
        } else {
            quorums
        }
    }
    /// Makes sense if the quorum set represents a symmetric quorum cluster...
    pub(crate) fn has_nonintersecting_quorums(&self) -> Option<(NodeIdSet, NodeIdSet)> {
        // make sure we aren't really a 1-node quorum
        if self
            .nonempty_slices_iter(|qset| qset.threshold)
            .take(2)
            .count()
            > 1
        {
            self.has_nonintersecting_quorum_slices()
        } else {
            None
        }
    }
}

fn selection_satisfiable(selection: &NodeIdSet, available: &NodeIdSet, fbas: &Fbas) -> bool {
    selection
        .iter()
        .all(|x| fbas.nodes[x].quorum_set.is_quorum_slice(available))
}

/// Returns `true` if any subset of `node_set` forms a quorum for `fbas`.
pub fn contains_quorum(node_set: &NodeIdSet, fbas: &Fbas) -> bool {
    let mut satisfiable = node_set.clone();

    while let Some(unsatisfiable_node) = satisfiable
        .iter()
        .find(|&x| !fbas.nodes[x].quorum_set.is_quorum_slice(&satisfiable))
    {
        satisfiable.remove(unsatisfiable_node);
    }
    !satisfiable.is_empty()
}

pub(crate) fn complement_contains_quorum(node_set: &NodeIdSet, fbas: &Fbas) -> bool {
    let mut complement = fbas.all_nodes();
    complement.difference_with(node_set);
    contains_quorum(&complement, fbas)
}

fn is_minimal_for_quorum(quorum: &NodeIdSet, fbas: &Fbas) -> bool {
    let mut tester = quorum.clone();

    for node_id in quorum.iter() {
        tester.remove(node_id);
        if contains_quorum(&tester, fbas) {
            return false;
        }
        tester.insert(node_id);
    }
    true
}

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

    #[test]
    fn minimal_quorums_in_correct_trivial() {
        let fbas = Fbas::from_json_file(Path::new("test_data/correct_trivial.json"));

        let expected = vec![bitset![0, 1], bitset![0, 2], bitset![1, 2]];
        let actual = find_minimal_quorums(&fbas);

        assert_eq!(expected, actual);
    }

    #[test]
    fn minimal_quorums_in_broken_trivial() {
        let fbas = Fbas::from_json_file(Path::new("test_data/broken_trivial.json"));

        let expected = vec![bitset![0], bitset![1, 2]];
        let actual = find_minimal_quorums(&fbas);

        assert_eq!(expected, actual);
    }

    #[test]
    fn minimal_quorums_in_broken_trivial_reversed_node_ids() {
        let mut fbas = Fbas::from_json_file(Path::new("test_data/broken_trivial.json"));
        fbas.nodes.reverse();

        let expected = vec![bitset![2], bitset![0, 1]];
        let actual = find_minimal_quorums(&fbas);

        assert_eq!(expected, actual);
    }

    #[test]
    fn minimal_quorums_when_naive_remove_non_minimal_optimization_doesnt_work() {
        let fbas = Fbas::from_json_str(
            r#"[
            {
                "publicKey": "n0",
                "quorumSet": { "threshold": 2, "validators": ["n0", "n3"] }
            },
            {
                "publicKey": "n1",
                "quorumSet": { "threshold": 2, "validators": ["n1", "n2"] }
            },
            {
                "publicKey": "n2",
                "quorumSet": { "threshold": 2, "validators": ["n1", "n2"] }
            },
            {
                "publicKey": "n3",
                "quorumSet": { "threshold": 2, "validators": ["n0", "n3"] }
            }
        ]"#,
        );
        let expected = vec![bitset![0, 3], bitset![1, 2]];
        let actual = find_minimal_quorums(&fbas);
        assert_eq!(expected, actual);
    }

    #[test]
    fn nonintersecting_quorums_in_half_half() {
        let fbas = Fbas::from_json_str(
            r#"[
            {
                "publicKey": "n0",
                "quorumSet": { "threshold": 2, "validators": ["n0", "n1"] }
            },
            {
                "publicKey": "n1",
                "quorumSet": { "threshold": 2, "validators": ["n0", "n1", "n2"] }
            },
            {
                "publicKey": "n2",
                "quorumSet": { "threshold": 2, "validators": ["n1", "n2", "n3"] }
            },
            {
                "publicKey": "n3",
                "quorumSet": { "threshold": 2, "validators": ["n2", "n3"] }
            }
        ]"#,
        );

        let expected = Some(vec![bitset![0, 1], bitset![2, 3]]);
        let actual = find_nonintersecting_quorums(&fbas);

        assert_eq!(expected, actual);
    }

    #[test]
    fn nonintersecting_quorums_in_broken() {
        let fbas = Fbas::from_json_file(Path::new("test_data/broken.json"));

        let expected = Some(vec![bitset![3, 10], bitset![4, 6]]);
        let actual = find_nonintersecting_quorums(&fbas);

        assert_eq!(expected, actual);
    }
}