Skip to content

Commit

Permalink
BFT-464: Rename to Splits
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed May 23, 2024
1 parent 225b04c commit 841da8a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
14 changes: 8 additions & 6 deletions node/actors/bft/src/testonly/twins/partition.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::cmp::min;

/// A group of nodes that can talk to each other.
///
/// This is in line with the language used in the paper, e.g. "we can split the nodes into two partitions".
pub type Partition<'a, T> = Vec<&'a T>;
/// A division of nodes into disjunct partitions, with no communication between different groups.
pub type Partitioning<'a, T> = Vec<Partition<'a, T>>;
pub type Split<'a, T> = Vec<Partition<'a, T>>;

/// Generate all possible partitioning of `items` into `num_partitions` groups.
/// Generate all possible splits of `items` into `num_partitions` partitions.
///
/// The idea is to fill out a table such as this:
/// ```text
Expand All @@ -21,7 +23,7 @@ pub type Partitioning<'a, T> = Vec<Partition<'a, T>>;
/// partition `[A, B, C]` into two groups, we want `[{A, B}, {C}]` to appear in the
/// results, but not `[{C}, {A, B}]`, or `[{B, A}, {C}]` as they are the same, but
/// we do want `[{A, C}, {B}]` because they have different labels.
pub fn partitions<T>(items: &[T], num_partitions: usize) -> Vec<Partitioning<T>> {
pub fn splits<T>(items: &[T], num_partitions: usize) -> Vec<Split<T>> {
Partitioner::generate(items.iter().collect(), num_partitions)
}

Expand All @@ -31,14 +33,14 @@ struct Partitioner<'a, T> {
num_items: usize,
num_partitions: usize,
// All collected complete partitionings
output: Vec<Partitioning<'a, T>>,
output: Vec<Split<'a, T>>,
// Partially complete partitioning currently being built
acc: Partitioning<'a, T>,
acc: Split<'a, T>,
}

impl<'a, T> Partitioner<'a, T> {
/// Generate all possible partitioning.
fn generate(items: Partition<'a, T>, num_partitions: usize) -> Vec<Partitioning<'a, T>> {
fn generate(items: Partition<'a, T>, num_partitions: usize) -> Vec<Split<'a, T>> {
if num_partitions == 0 || items.len() < num_partitions {
// Impossible to partition.
return Vec::new();
Expand Down
16 changes: 8 additions & 8 deletions node/actors/bft/src/testonly/twins/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeSet;

use rand::{seq::SliceRandom, Rng};

use super::{partitions, HasKey, Partitioning, Twin};
use super::{splits, HasKey, Split, Twin};

/// A cluster holds all the nodes in the simulation, some of which are twins.
pub struct Cluster<T> {
Expand Down Expand Up @@ -93,7 +93,7 @@ where
/// The configuration for a single round.
pub struct RoundConfig<'a, T: HasKey> {
pub leader: &'a T::Key,
pub partitions: Partitioning<'a, T>,
pub partitions: Split<'a, T>,
}

/// Configuration for a number of rounds.
Expand All @@ -109,8 +109,8 @@ where
num_rounds: usize,
/// Unique leader keys.
keys: Vec<&'a T::Key>,
/// All partitionings of various sizes we can choose in a round.
partitions: Vec<Partitioning<'a, T>>,
/// All splits of various sizes we can choose in a round.
splits: Vec<Split<'a, T>>,
}

impl<'a, T> ScenarioGenerator<'a, T>
Expand All @@ -125,11 +125,11 @@ where
let keys = cluster.replicas().iter().map(|r| r.key()).collect();

// Create all possible partitionings; the paper considers 2 or 3 partitions to be enough.
let partitions = (1..=max_partitions).flat_map(|np| partitions(cluster.nodes(), np));
let splits = (1..=max_partitions).flat_map(|np| splits(cluster.nodes(), np));

// Prune partitionings so that all of them have at least one where a quorum is possible.
// Alternatively we could keep all and make sure every scenario has eventually one with a quorum, for liveness.
let partitions = partitions
let splits = splits
.filter(|ps| {
ps.iter()
.any(|p| unique_key_count(p) >= cluster.quorum_size())
Expand All @@ -139,7 +139,7 @@ where
Self {
num_rounds,
keys,
partitions,
splits,
}
}

Expand All @@ -154,7 +154,7 @@ where
for _ in 0..self.num_rounds {
rounds.push(RoundConfig {
leader: self.keys.choose(rng).cloned().unwrap(),
partitions: self.partitions.choose(rng).cloned().unwrap(),
partitions: self.splits.choose(rng).cloned().unwrap(),
})
}

Expand Down
16 changes: 8 additions & 8 deletions node/actors/bft/src/testonly/twins/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use zksync_concurrency::ctx;

use crate::testonly::twins::unique_key_count;

use super::{partitions, Cluster, HasKey, Partitioning, ScenarioGenerator, Twin};
use super::{splits, Cluster, HasKey, ScenarioGenerator, Split, Twin};

#[test]
fn test_partitions() {
let got = partitions(&["foo", "bar", "baz"], 2);
let want: HashSet<Partitioning<_>> = [
fn test_splits() {
let got = splits(&["foo", "bar", "baz"], 2);
let want: HashSet<Split<_>> = [
vec![vec![&"foo"], vec![&"bar", &"baz"]],
vec![vec![&"foo", &"bar"], vec![&"baz"]],
vec![vec![&"foo", &"baz"], vec![&"bar"]],
Expand All @@ -25,7 +25,7 @@ fn test_partitions() {
}

#[test]
fn prop_partitions() {
fn prop_splits() {
let rng = &mut ctx::test_root(&ctx::RealClock).rng();
for _ in 0..100 {
let num_partitions = rng.gen_range(0..=3);
Expand All @@ -36,7 +36,7 @@ fn prop_partitions() {
items.push(i);
}

let got = partitions(&items, num_partitions);
let got = splits(&items, num_partitions);
let got_len = got.len();
let got = BTreeSet::from_iter(got.into_iter().map(|ps| {
BTreeSet::from_iter(
Expand All @@ -45,7 +45,7 @@ fn prop_partitions() {
)
}));

let want = partitions_naive(items, num_partitions);
let want = splits_naive(items, num_partitions);

assert_eq!(
got_len,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl Twin for TestNode {
}

/// Naive implementation of the partitioning to test against.
fn partitions_naive<T: Ord + Eq + Clone + Debug>(
fn splits_naive<T: Ord + Eq + Clone + Debug>(
items: Vec<T>,
num_partitions: usize,
) -> BTreeSet<BTreeSet<BTreeSet<T>>> {
Expand Down

0 comments on commit 841da8a

Please sign in to comment.