From d6738d1eaf4ee1d6403851cf79cdf6ce694e30b9 Mon Sep 17 00:00:00 2001 From: mjalalzai <33738574+MForensic@users.noreply.github.com> Date: Tue, 17 Oct 2023 11:48:35 -0700 Subject: [PATCH] Merging committees --- carnot/carnot-vote-aggregation.py | 2 +- carnot/merging_committees.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 carnot/merging_committees.py diff --git a/carnot/carnot-vote-aggregation.py b/carnot/carnot-vote-aggregation.py index 945956d..2f9874e 100644 --- a/carnot/carnot-vote-aggregation.py +++ b/carnot/carnot-vote-aggregation.py @@ -401,7 +401,7 @@ class Carnot: # Forward the vote to the parent committee return Send(to=self.overlay.parent_committee, payload=vote) -# ToDo: Is not needed anymore + def forward_timeout_qc(self, msg: TimeoutQc) -> Optional[Event]: # Assertions for input validation assert msg.view == self.current_view, "Received TimeoutQc with correct view" diff --git a/carnot/merging_committees.py b/carnot/merging_committees.py new file mode 100644 index 0000000..b6334d2 --- /dev/null +++ b/carnot/merging_committees.py @@ -0,0 +1,29 @@ +def merge_committees(committees): + num_committees = len(committees) + merged_committees = [] + + # Sort the committees by size + sorted_committees = sorted(committees, key=len) + + # Divide the committees into two groups: smaller and larger + smaller_committees = sorted_committees[:num_committees // 2] + larger_committees = sorted_committees[num_committees // 2:] + + # Merge smaller and larger committees pairwise + for smaller, larger in zip(smaller_committees, larger_committees): + merged_committee = set(smaller) + merged_committee.update(larger) + merged_committees.append(merged_committee) + + # Handle the leftover committee, if it exists + if num_committees % 2 == 1: + leftover_committee = set(sorted_committees[-1]) + num_merged_committees = len(merged_committees) + + # Distribute leftover members evenly among merged committees + for member in leftover_committee: + # Find the index of the merged committee with the smallest size + smallest_idx = min(range(num_merged_committees), key=lambda i: len(merged_committees[i])) + merged_committees[smallest_idx].add(member) + + return merged_committees \ No newline at end of file