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

Make a loop instead of using filter #2071

Closed
wants to merge 3 commits into from
Closed
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
81 changes: 38 additions & 43 deletions core/src/banking_stage/latest_unprocessed_votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,49 +274,44 @@ impl LatestUnprocessedVotes {
bank: Arc<Bank>,
forward_packet_batches_by_accounts: &mut ForwardPacketBatchesByAccounts,
) -> usize {
let mut continue_forwarding = true;
let pubkeys_by_stake = weighted_random_order_by_stake(
&bank,
self.latest_votes_per_pubkey.read().unwrap().keys(),
)
.collect_vec();
pubkeys_by_stake
.into_iter()
.filter(|&pubkey| {
if !continue_forwarding {
return false;
}
if let Some(lock) = self.get_entry(pubkey) {
let mut vote = lock.write().unwrap();
if !vote.is_vote_taken() && !vote.is_forwarded() {
let deserialized_vote_packet = vote.vote.as_ref().unwrap().clone();
if let Some(sanitized_vote_transaction) = deserialized_vote_packet
.build_sanitized_transaction(
bank.vote_only_bank(),
bank.as_ref(),
bank.get_reserved_account_keys(),
)
{
if forward_packet_batches_by_accounts.try_add_packet(
&sanitized_vote_transaction,
deserialized_vote_packet,
&bank.feature_set,
) {
vote.forwarded = true;
} else {
// To match behavior of regular transactions we stop
// forwarding votes as soon as one fails
continue_forwarding = false;
}
return true;
} else {
return false;
}
}
}
false
})
.count()
let binding = self.latest_votes_per_pubkey.read().unwrap();
let pubkeys_by_stake = weighted_random_order_by_stake(&bank, binding.keys());
Comment on lines +277 to +278

Choose a reason for hiding this comment

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

I think this is locking the latest_votes_per_pubkey for longer than necessary.
If the following compiles, it is a slightly better way to do it, as it releases the lock earlier:

Suggested change
let binding = self.latest_votes_per_pubkey.read().unwrap();
let pubkeys_by_stake = weighted_random_order_by_stake(&bank, binding.keys());
let pubkeys_by_stake = {
let binding = self.latest_votes_per_pubkey.read().unwrap();
weighted_random_order_by_stake(&bank, binding.keys())
};

Copy link
Author

Choose a reason for hiding this comment

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

doesn't compile because error: binding does not live long enough

Choose a reason for hiding this comment

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

This is because weighted_random_order_by_stake return type incorrectly captures the lifetime of the second argument.
I've suggested how you can fix it above.

Here is a working code with the lock lifetime reduced and some other fixes I've suggested: #2091

Copy link
Author

Choose a reason for hiding this comment

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

Thanks! I'll abandon this patch then.

let mut count: usize = 0;
for pubkey in pubkeys_by_stake {
let Some(lock) = self.get_entry(pubkey) else {
continue;
};
let mut vote = lock.write().unwrap();
if vote.is_vote_taken() || vote.is_forwarded() {
continue;
}

let deserialized_vote_packet = vote.vote.as_ref().unwrap().clone();
let Some(sanitized_vote_transaction) = deserialized_vote_packet
.build_sanitized_transaction(
bank.vote_only_bank(),
bank.as_ref(),
bank.get_reserved_account_keys(),
)
else {
continue;
};

if !forward_packet_batches_by_accounts.try_add_packet(
&sanitized_vote_transaction,
deserialized_vote_packet,
&bank.feature_set,
) {
// To match behavior of regular transactions we stop
// forwarding votes as soon as one fails. We are
// assuming that failure (try_add_packet) means no more space available.
break;
}

vote.forwarded = true;
count += 1;
}
count
}

/// Drains all votes yet to be processed sorted by a weighted random ordering by stake
Expand Down