From 8cc47c1efeed7f680981054b8a50928bca73da7c Mon Sep 17 00:00:00 2001 From: mjalalzai <33738574+MForensic@users.noreply.github.com> Date: Sun, 29 Oct 2023 18:12:50 -0700 Subject: [PATCH] test_concatenate_standard_qcs --- carnot/carnot_vote_aggregation.py | 21 ++++++++++++--------- carnot/my_carnot.py | 22 +++++++++++++++++++--- carnot/test_carnot_vote_aggregation.py | 21 ++++++++++++++++++++- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/carnot/carnot_vote_aggregation.py b/carnot/carnot_vote_aggregation.py index ab50c21..4dcd614 100644 --- a/carnot/carnot_vote_aggregation.py +++ b/carnot/carnot_vote_aggregation.py @@ -245,29 +245,32 @@ class Carnot2(Carnot): # A node may receive QCs from child committee members. It may also build it's own QC. # These QCs are then concatenated into one before sending to the parent committee. + + def concatenate_standard_qcs(qc_set: Set[StandardQc]) -> StandardQc: + # Convert the set of StandardQc objects into a list + qc_list = list(qc_set) + # Initialize the attributes for the concatenated StandardQc concatenated_block = None concatenated_view = None concatenated_voters = set() - # Iterate through the input set of StandardQc objects - for qc in qc_set: + # Iterate through the input list of StandardQc objects + for qc in qc_list: concatenated_voters.update(qc.voters) - # Choose the block and view values from the first StandardQc in the set - if qc_set: - concatenated_block = qc_set[0].block - concatenated_view = qc_set[0].view + # Choose the block and view values from the first StandardQc in the list + if qc_list: + concatenated_block = qc_list[0].block + concatenated_view = qc_list[0].view # Create the concatenated StandardQc object concatenated_qc = StandardQc(concatenated_block, concatenated_view, concatenated_voters) return concatenated_qc - from carnot import AggregateQc, Qc, StandardQc - -# Similarly aggregated qcs are concatenated after timeout t2: + # Similarly aggregated qcs are concatenated after timeout t2: def concatenate_aggregate_qcs(qc_set: Set[AggregateQc]) -> AggregateQc: # Initialize the attributes for the concatenated AggregateQc concatenated_qcs = [] diff --git a/carnot/my_carnot.py b/carnot/my_carnot.py index d7eb78c..0799b3c 100644 --- a/carnot/my_carnot.py +++ b/carnot/my_carnot.py @@ -48,13 +48,29 @@ def int_to_id(i: int) -> Id: return bytes(str(i), encoding="utf8") -@dataclass(unsafe_hash=True) class StandardQc: block: Id view: View voters: Set[Id] - def get_view(self) -> View: - return self.get_view + + def __init__(self, block: Id, view: View, voters: Set[Id]): + self.block = block + self.view = view + self.voters = voters + + def __hash__(self): + # Customize the hash function based on your requirements + return hash((self.block, self.view, frozenset(self.voters))) + + def __eq__(self, other): + if isinstance(other, StandardQc): + return ( + self.block == other.block and + self.view == other.view and + self.voters == other.voters + ) + return False + @dataclass class AggregateQc: diff --git a/carnot/test_carnot_vote_aggregation.py b/carnot/test_carnot_vote_aggregation.py index cdfe815..22ad062 100644 --- a/carnot/test_carnot_vote_aggregation.py +++ b/carnot/test_carnot_vote_aggregation.py @@ -5,7 +5,7 @@ from carnot import merging_committees from carnot.merging_committees import merge_committees import itertools import unittest - +from carnot.carnot_vote_aggregation import Carnot2,StandardQc class TestMergeCommittees(unittest.TestCase): def assertMergedSetsEqual(self, merged, original): @@ -30,5 +30,24 @@ class TestMergeCommittees(unittest.TestCase): merged = merge_committees(original_sets) self.assertMergedSetsEqual(merged, set.union(*original_sets)) + + +class TestConcatenateStandardQcs(unittest.TestCase): + def test_concatenate_standard_qcs(self): + # Create some StandardQc objects + qc1 = StandardQc(block=1, view=1, voters={1, 2, 3}) + qc2 = StandardQc(block=1, view=1, voters={4, 5, 6}) + qc3 = StandardQc(block=1, view=1, voters={7, 8, 6}) + + # Concatenate the StandardQc objects + concatenated_qc = carnot.carnot_vote_aggregation.Carnot2.concatenate_standard_qcs({qc1, qc2, qc3}) + + # Define the expected concatenated StandardQc + expected_qc = StandardQc(block=1, view=1, voters={1, 2, 3, 4, 5, 6, 7, 8}) + + # Assert that the concatenated StandardQc matches the expected one + self.assertEqual(concatenated_qc, expected_qc) + + if __name__ == '__main__': unittest.main()