Skip to content

Commit 45c5033

Browse files
committed
[mfp] use more conservative filter for votes
1 parent 7ae4447 commit 45c5033

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

consensus/core/src/commit_finalizer.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,13 @@ impl CommitFinalizer {
339339
}
340340
}
341341
// Initialize the block state.
342-
blocks_map
343-
.entry(block_ref)
344-
.or_insert_with(|| RwLock::new(BlockState::new(block)));
342+
blocks_map.entry(block_ref).or_insert_with(|| {
343+
RwLock::new(BlockState::new(
344+
block,
345+
commit_state.commit.commit_ref.index,
346+
commit_state.commit.leader.round,
347+
))
348+
});
345349
}
346350
}
347351

@@ -592,12 +596,10 @@ impl CommitFinalizer {
592596
let mut finalized_transactions = vec![];
593597
let blocks_map = blocks.read();
594598
// Use BTreeSet to ensure always visit blocks in the earliest round.
595-
let mut to_visit_blocks = blocks_map
596-
.get(&pending_block_ref)
597-
.unwrap()
598-
.read()
599-
.children
600-
.clone();
599+
let (pending_block_commit_index, mut to_visit_blocks) = {
600+
let block_state = blocks_map.get(&pending_block_ref).unwrap().read();
601+
(block_state.commit_index, block_state.children.clone())
602+
};
601603
// Blocks that have been visited.
602604
let mut visited = BTreeSet::new();
603605
// Blocks where votes and origin descendants should be ignored for processing.
@@ -608,15 +610,17 @@ impl CommitFinalizer {
608610
continue;
609611
}
610612
let curr_block_state = blocks_map.get(&curr_block_ref).unwrap_or_else(|| panic!("Block {curr_block_ref} is either incorrectly gc'ed or failed to be recovered after crash.")).read();
611-
// The first ancestor of current block should have the same origin / author as the current block.
612-
// If it is not found in the blocks map but have round higher than the pending block, it might have
613-
// voted on the pending block but have been GC'ed.
613+
// Do not count votes, when there can be GC'ed blocks in the path from the pending block to the current block.
614614
// Because the GC'ed block might have voted on the pending block and rejected some of the pending transactions,
615615
// we cannot assume current block is voting to accept transactions from the pending block.
616-
let curr_origin_ancestor_ref = curr_block_state.block.ancestors().first().unwrap();
617-
let skip_votes = curr_block_ref.author == curr_origin_ancestor_ref.author
618-
&& pending_block_ref.round < curr_origin_ancestor_ref.round
619-
&& !blocks_map.contains_key(curr_origin_ancestor_ref);
616+
//
617+
// This check is more conservative than necessary, because GC round at current block is decided
618+
// by the leader round of the previous commit. But skipping votes unnecessarily is still correct,
619+
// as long as it is deterministic. Also GC depths in most environments
620+
// are large enough to not unnecessarily skip valid accept votes.
621+
let skip_votes = pending_block_commit_index < curr_block_state.commit_index
622+
&& pending_block_ref.round + context.protocol_config.consensus_gc_depth()
623+
<= curr_block_state.leader_round;
620624
// Skip counting votes from the block if it has been marked to be ignored.
621625
if ignored.insert(curr_block_ref) {
622626
// Skip collecting votes from origin descendants of current block.
@@ -800,10 +804,14 @@ struct BlockState {
800804
// Other committed blocks that are origin descendants of this block.
801805
// See the comment above append_origin_descendants_from_last_commit() for more details.
802806
origin_descendants: Vec<BlockRef>,
807+
// Commit which contains this block.
808+
commit_index: CommitIndex,
809+
// Commit leader round.
810+
leader_round: Round,
803811
}
804812

805813
impl BlockState {
806-
fn new(block: VerifiedBlock) -> Self {
814+
fn new(block: VerifiedBlock, commit_index: CommitIndex, leader_round: Round) -> Self {
807815
let reject_votes: BTreeMap<_, _> = block
808816
.transaction_votes()
809817
.iter()
@@ -817,6 +825,8 @@ impl BlockState {
817825
children: BTreeSet::new(),
818826
reject_votes,
819827
origin_descendants,
828+
commit_index,
829+
leader_round,
820830
}
821831
}
822832
}

0 commit comments

Comments
 (0)