From d00eae4c2cc80e125a79dcc3e4bf7a72e6378210 Mon Sep 17 00:00:00 2001 From: mjalalzai <33738574+MForensic@users.noreply.github.com> Date: Thu, 21 Sep 2023 19:34:06 -0700 Subject: [PATCH] Safe block --- carnot/carnot-vote-aggregation.py | 36 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/carnot/carnot-vote-aggregation.py b/carnot/carnot-vote-aggregation.py index 38b6945..4ff753e 100644 --- a/carnot/carnot-vote-aggregation.py +++ b/carnot/carnot-vote-aggregation.py @@ -236,21 +236,27 @@ class Carnot: # Return True if both conditions are met return is_view_incremented and is_standard_qc -def latest_committed_view(self) -> View: - return self.latest_committed_block().view + def latest_committed_view(self) -> View: + return self.latest_committed_block().view -# Return a list of blocks received by a node for a specific view. -# More than one block is returned only in case of a malicious leader. -def blocks_in_view(self, view: View) -> List[Block]: - return [block for block in self.safe_blocks.values() if block.view == view] + # Return a list of blocks received by a node for a specific view. + # More than one block is returned only in case of a malicious leader. + def blocks_in_view(self, view: View) -> List[Block]: + return [block for block in self.safe_blocks.values() if block.view == view] -def genesis_block(self) -> Block: - return self.blocks_in_view(0)[0] + def genesis_block(self) -> Block: + return self.blocks_in_view(0)[0] -def latest_committed_block(self) -> Block: - for view in range(self.current_view, 0, -1): - for block in self.blocks_in_view(view): - if self.can_commit_grandparent(block): - return self.safe_blocks.get(self.safe_blocks.get(block.parent()).parent()) - # The genesis block is always considered committed. - return self.genesis_block() + def latest_committed_block(self) -> Block: + for view in range(self.current_view, 0, -1): + for block in self.blocks_in_view(view): + if self.can_commit_grandparent(block): + return self.safe_blocks.get(self.safe_blocks.get(block.parent()).parent()) + # The genesis block is always considered committed. + return self.genesis_block() + + def block_is_safe(self, block: Block) -> bool: + if isinstance(block.qc, (StandardQc,)): + return block.view_num == block.qc.view() + 1 + if isinstance(block.qc, (AggregateQc,)): + return (block.view_num == block.qc.view() + 1) and block.extends(self.latest_committed_block())