Skip to content

Commit

Permalink
Update error handling and remove unnecessary membership check
Browse files Browse the repository at this point in the history
  • Loading branch information
shenkeyao committed Jan 10, 2025
1 parent c55f630 commit ee17af5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 81 deletions.
30 changes: 14 additions & 16 deletions crates/task-impls/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,13 @@ pub async fn decide_from_proposal_2<TYPES: NodeType>(
let epoch_height = consensus_reader.epoch_height;
drop(consensus_reader);

// `leaf_views.last()` is never none if we've reached a new decide, so this is safe to
// unwrap.
decide_epoch_root(
&res.leaf_views.last().unwrap().leaf,
epoch_height,
membership,
)
.await;
if let Some(decided_leaf_info) = res.leaf_views.last() {
decide_epoch_root(&decided_leaf_info.leaf, epoch_height, membership).await;
} else {
tracing::warn!(
"No decided leaf while a view has been decided, which should be impossible."
);
}
}

res
Expand Down Expand Up @@ -490,14 +489,13 @@ pub async fn decide_from_proposal<TYPES: NodeType>(
let epoch_height = consensus_reader.epoch_height;
drop(consensus_reader);

// `leaf_views.last()` is never none if we've reached a new decide, so this is safe to
// unwrap.
decide_epoch_root(
&res.leaf_views.last().unwrap().leaf,
epoch_height,
membership,
)
.await;
if let Some(decided_leaf_info) = res.leaf_views.last() {
decide_epoch_root(&decided_leaf_info.leaf, epoch_height, membership).await;
} else {
tracing::warn!(
"No decided leaf while a view has been decided, which should be impossible."
);
}
}

res
Expand Down
122 changes: 57 additions & 65 deletions crates/task-impls/src/quorum_vote/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,76 +233,68 @@ async fn store_drb_seed_and_result<TYPES: NodeType, I: NodeImplementation<TYPES>
task_state: &mut QuorumVoteTaskState<TYPES, I, V>,
decided_leaf: &Leaf2<TYPES>,
) -> Result<()> {
let decided_block_number = decided_leaf.block_header().block_number();

if task_state.epoch_height != 0 {
let current_epoch_number = TYPES::Epoch::new(epoch_from_block_number(
decided_block_number,
task_state.epoch_height,
));
if task_state.epoch_height == 0 {
tracing::info!("Epoch height is 0, skipping DRB storage.");
return Ok(());
}

// Skip storing the seed and computed result if this is not the epoch root.
if is_epoch_root(decided_block_number, task_state.epoch_height) {
// Cancel old DRB computation tasks.
let mut consensus_writer = task_state.consensus.write().await;
consensus_writer
.drb_seeds_and_results
.garbage_collect(current_epoch_number);
drop(consensus_writer);
let decided_block_number = decided_leaf.block_header().block_number();
let current_epoch_number = TYPES::Epoch::new(epoch_from_block_number(
decided_block_number,
task_state.epoch_height,
));

// Store the DRB result for the next epoch, which will be used by the proposal task to
// include in the proposal in the last block of this epoch.
store_and_get_computed_drb_result(current_epoch_number + 1, task_state).await?;
// Skip storing the seed and computed result if this is not the epoch root.
if is_epoch_root(decided_block_number, task_state.epoch_height) {
// Cancel old DRB computation tasks.
let mut consensus_writer = task_state.consensus.write().await;
consensus_writer
.drb_seeds_and_results
.garbage_collect(current_epoch_number);
drop(consensus_writer);

// Skip storing the seed if we are not in the committee of the next epoch.
if task_state
.membership
.read()
.await
.has_stake(&task_state.public_key, current_epoch_number + 1)
{
let Ok(drb_seed_input_vec) =
bincode::serialize(&decided_leaf.justify_qc().signatures)
else {
bail!("Failed to serialize the QC signature.");
};
let Ok(drb_seed_input) = drb_seed_input_vec.try_into() else {
bail!("Failed to convert the serialized QC signature into a DRB seed input.");
};

// Store the DRB seed input for the epoch after the next one.
task_state
.consensus
.write()
.await
.drb_seeds_and_results
.store_seed(current_epoch_number + 2, drb_seed_input);
}
// Store the DRB result for the next epoch, which will be used by the proposal task to
// include in the proposal in the last block of this epoch.
store_and_get_computed_drb_result(current_epoch_number + 1, task_state).await?;

// Store the DRB seed input for the epoch after the next one.
let Ok(drb_seed_input_vec) = bincode::serialize(&decided_leaf.justify_qc().signatures)
else {
bail!("Failed to serialize the QC signature.");
};
let Ok(drb_seed_input) = drb_seed_input_vec.try_into() else {
bail!("Failed to convert the serialized QC signature into a DRB seed input.");
};
task_state
.consensus
.write()
.await
.drb_seeds_and_results
.store_seed(current_epoch_number + 2, drb_seed_input);
}
// Skip storing the received result if this is not the last block.
else if is_last_block_in_epoch(decided_block_number, task_state.epoch_height) {
// Skip if we are not in the committee of the next epoch.
if !task_state
.membership
.read()
.await
.has_stake(&task_state.public_key, current_epoch_number + 1)
{
return Ok(());
}
// Skip storing the received result if this is not the last block.
else if is_last_block_in_epoch(decided_block_number, task_state.epoch_height) {
// Skip if we are not in the committee of the next epoch.
if !task_state
.membership
.read()
if let Some(result) = decided_leaf.next_drb_result {
// We don't need to check value existence and consistency because it should be
// impossible to decide on a block with different DRB results.
task_state
.consensus
.write()
.await
.has_stake(&task_state.public_key, current_epoch_number + 1)
{
return Ok(());
}
if let Some(result) = decided_leaf.next_drb_result {
// We don't need to check value existence and consistency because it should be
// impossible to decide on a block with different DRB results.
task_state
.consensus
.write()
.await
.drb_seeds_and_results
.results
.insert(current_epoch_number + 1, result);
} else {
bail!("The last block of the epoch is decided but doesn't contain a DRB result.");
}
.drb_seeds_and_results
.results
.insert(current_epoch_number + 1, result);
} else {
bail!("The last block of the epoch is decided but doesn't contain a DRB result.");
}
}
Ok(())
Expand Down

0 comments on commit ee17af5

Please sign in to comment.