Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turbine::broadcast_stage: then_some/ok_or_else => if #1301

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions turbine/src/broadcast_stage/standard_broadcast_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use {
super::{
broadcast_utils::{self, ReceiveResults},
broadcast_utils::{self, get_chained_merkle_root_from_parent, ReceiveResults},
*,
},
crate::cluster_nodes::ClusterNodesCache,
Expand Down Expand Up @@ -207,8 +207,9 @@ impl StandardBroadcastRun {
let mut to_shreds_time = Measure::start("broadcast_to_shreds");
let cluster_type = bank.cluster_type();

// 1) Finish previous slot.
if self.slot != bank.slot() {
// Finish previous slot if it was interrupted.
// Was it interrupted?
if !self.completed {
let shreds = self.finish_prev_slot(
keypair,
Expand Down Expand Up @@ -242,29 +243,30 @@ impl StandardBroadcastRun {
// Refrain from generating shreds for the slot.
return Err(Error::DuplicateSlotBroadcast(bank.slot()));
}

// Reinitialize state for this slot.
let chained_merkle_root = (self.slot == bank.parent_slot())
.then_some(self.chained_merkle_root)
.ok_or_else(|| {
broadcast_utils::get_chained_merkle_root_from_parent(
bank.slot(),
bank.parent_slot(),
blockstore,
)
})

if self.slot != bank.parent_slot() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned on slack, I appreciate you looking into this code, and it is always good to have more eyes on the code and try to spot bugs or inefficiencies.

However this is only a "style" change and people's personal preference on what style is better can be different.
I personally prefer the old code because it avoids this if branch which changes state, whereas the old code uses an "expression". This type of state changing if branches tend to be a source of bugs and errors and I personally rather avoid them when possible.

self.chained_merkle_root = get_chained_merkle_root_from_parent(
bank.slot(),
bank.parent_slot(),
blockstore,
)
.unwrap_or_else(|err| {
error!("Unknown chained Merkle root: {err:?}");
process_stats.err_unknown_chained_merkle_root += 1;
Hash::default()
});
}

self.slot = bank.slot();
self.parent = bank.parent_slot();
self.chained_merkle_root = chained_merkle_root;
self.next_shred_index = 0u32;
self.next_code_index = 0u32;
self.completed = false;
self.slot_broadcast_start = Instant::now();
self.num_batches = 0;

receive_elapsed = Duration::ZERO;
coalesce_elapsed = Duration::ZERO;
}
Expand Down
Loading