From 00b650255c068e9efd1eef9d87cc15321d096554 Mon Sep 17 00:00:00 2001 From: danielsanchezq Date: Mon, 3 Apr 2023 11:02:58 +0200 Subject: [PATCH] Stylish, adjustments and fixes --- carnot/carnot.py | 33 +++++++++++++++++---------------- carnot/test_happy_path.py | 22 ++++++++++++---------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/carnot/carnot.py b/carnot/carnot.py index f29a0e4..6999441 100644 --- a/carnot/carnot.py +++ b/carnot/carnot.py @@ -66,9 +66,9 @@ class Vote: class TimeoutQc: view: View high_qc: Qc - qc_views: list[View] - SenderIds: Set[Id] - Sender: Id + qc_views: List[View] + sender_ids: Set[Id] + sender: Id # local timeout field is only used by the root committee and its children when they timeout. The timeout_qc is built @@ -148,7 +148,7 @@ class Overlay: def member_of_internal_com(self, _id: Id) -> bool: """ :param _id: - :return: truee if the participant with Id _id is member of internal committees within the committee tree overlay + :return: True if the participant with Id _id is member of internal committees within the committee tree overlay """ pass @@ -201,7 +201,7 @@ def download(view) -> Block: raise NotImplementedError -def build_timeoutqc(msgs) -> TimeoutQc: +def build_timeout_qc(msgs) -> TimeoutQc: pass @@ -304,19 +304,19 @@ class Carnot: def local_timeout(self, new_overlay: Overlay): self.increment_voted_view(self.current_view) - if self.overlay.member_of_leaf_committee(self.id) or self.overlay.is_child_of_root(): - timeout_Msg: Timeout = Timeout( + if self.overlay.member_of_leaf_committee(self.id) or self.overlay.is_child_of_root(self.id): + timeout_msg: Timeout = Timeout( view=self.current_view, high_qc=self.local_high_qc, local_timeout=True, # local_timeout is only true for the root committee or members of its children # root committee or its children can trigger the timeout. timeout_qc=self.last_timeout_view_qc, - sender=self.id() + sender=self.id ) - self.send(timeout_Msg, *self.overlay.root_committee(self.id)) + self.send(timeout_msg, *self.overlay.root_committee()) for child_committee in self.overlay.child_of_root_committee(): - self.send(timeout_Msg, child_committee) + self.send(timeout_msg, child_committee) def timeout(self, msgs: Set["Timeout"]): assert len(msgs) == self.overlay.super_majority_threshold(self.id) @@ -326,7 +326,7 @@ class Carnot: if self.local_high_qc.view < max_msg.high_qc.view: self.update_high_qc(max_msg.high_qc) if self.overlay.member_of_root_committee(self.id) and self.overlay.member_of_leaf_committee(self.id): - timeout_qc = build_timeoutqc(msgs) + timeout_qc = build_timeout_qc(msgs) self.update_timeout_qc(timeout_qc) else: self.update_timeout_qc(msgs.pop().timeout_qc) @@ -334,7 +334,7 @@ class Carnot: def timeout_qc(self,timeout_qc: TimeoutQc): pass - def send(self, vote: Vote, *ids: Id): + def send(self, vote: Vote | Timeout, *ids: Id): pass def broadcast(self, block): @@ -369,14 +369,15 @@ class Carnot: self.current_view = qc.view + 1 return True - def increment_view_timeout_qc(self, timeoutqc: TimeoutQc): - if timeoutqc == None or timeoutqc.view < self.current_view: + def increment_view_timeout_qc(self, timeout_qc: TimeoutQc): + if timeout_qc is None or timeout_qc.view < self.current_view: return - self.last_timeout_view_qc = timeoutqc + self.last_timeout_view_qc = timeout_qc self.current_view = self.last_timeout_view_qc.view + 1 return True - def get_max_timeout(self, timeouts: Set[Timeout]) -> Optional[Timeout]: + @staticmethod + def get_max_timeout(timeouts: Set[Timeout]) -> Optional[Timeout]: if not timeouts: return None return max(timeouts, key=lambda time: time.qc.view) diff --git a/carnot/test_happy_path.py b/carnot/test_happy_path.py index 94275ad..b8e45d5 100644 --- a/carnot/test_happy_path.py +++ b/carnot/test_happy_path.py @@ -173,11 +173,12 @@ class TestCarnotHappyPath(TestCase): self.assertEqual(carnot.latest_committed_view, 3) self.assertEqual(carnot.local_high_qc.view, 4) - # Test cases for vote: - # 1: Votes received should increment highest_voted_view and current_view but should not change - # latest_committed_view and last_timeout_view - + # Test cases for vote: def test_vote_for_received_block(self): + """ + 1: Votes received should increment highest_voted_view and current_view but should not change + latest_committed_view and last_timeout_view + """ class MockOverlay(Overlay): def member_of_root_com(self, _id: Id) -> bool: return False @@ -211,9 +212,10 @@ class TestCarnotHappyPath(TestCase): self.assertEqual(carnot.latest_committed_view, 0) self.assertEqual(carnot.last_timeout_view, None) - # 2 If last_voted_view is incremented after calling vote with votes lower than. - def test_vote_for_received_block_if_threshold_votes_has_not_reached(self): + """ + 2 If last_voted_view is incremented after calling vote with votes lower than. + """ class MockOverlay(Overlay): def member_of_root_com(self, _id: Id) -> bool: return False @@ -243,7 +245,7 @@ class TestCarnotHappyPath(TestCase): ) carnot.vote(block1, votes) - #### The test passes as the assert fails in len(votes) == self.overlay.super_majority_threshold(self.id) - #### when number of votes are < 9 - # self.assertEqual(carnot.highest_voted_view, 1) - # self.assertEqual(carnot.current_view, 1) + # The test passes as asserting fails in len(votes) == self.overlay.super_majority_threshold(self.id) + # when number of votes are < 9 + self.assertEqual(carnot.highest_voted_view, 1) + self.assertEqual(carnot.current_view, 1)