Skip to content

Commit

Permalink
Revert "beefy: Avoid double lock wrt live peers and cache reset"
Browse files Browse the repository at this point in the history
This reverts commit 7c77d4e.

Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Jul 19, 2024
1 parent f3910d4 commit 1d1837c
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,12 @@ impl<B: Block, AuthorityId: AuthorityIdBound> OnDemandJustificationsEngine<B, Au
}
}

/// Returns the next peer from the peers cache, if available.
///
/// If the optional block number if provided, the peers cache is
/// repopulated from the live peers that have voted on higher block numbers.
fn try_next_peer(&mut self, reset_cache: Option<NumberFor<B>>) -> Option<PeerId> {
let live = self.live_peers.lock();

if let Some(reset) = reset_cache {
self.peers_cache = live.further_than(reset);
}
fn reset_peers_cache_for_block(&mut self, block: NumberFor<B>) {
self.peers_cache = self.live_peers.lock().further_than(block);
}

fn try_next_peer(&mut self) -> Option<PeerId> {
let live = self.live_peers.lock();
while let Some(peer) = self.peers_cache.pop_front() {
if live.contains(&peer) {
return Some(peer);
Expand Down Expand Up @@ -147,10 +142,11 @@ impl<B: Block, AuthorityId: AuthorityIdBound> OnDemandJustificationsEngine<B, Au
if matches!(self.state, State::AwaitingResponse(_, _, _)) {
return;
}
self.reset_peers_cache_for_block(block);

// Start the requests engine - each unsuccessful received response will automatically
// trigger a new request to the next peer in the `peers_cache` until there are none left.
if let Some(peer) = self.try_next_peer(Some(block)) {
if let Some(peer) = self.try_next_peer() {
self.request_from_peer(peer, RequestInfo { block, active_set });
} else {
metric_inc!(self.metrics, beefy_on_demand_justification_no_peer_to_request_from);
Expand Down Expand Up @@ -250,7 +246,7 @@ impl<B: Block, AuthorityId: AuthorityIdBound> OnDemandJustificationsEngine<B, Au
match self.process_response(&peer, &req_info, resp) {
Err(err) => {
// No valid justification received, try next peer in our set.
if let Some(peer) = self.try_next_peer(None) {
if let Some(peer) = self.try_next_peer() {
self.request_from_peer(peer, req_info);
} else {
metric_inc!(
Expand Down

0 comments on commit 1d1837c

Please sign in to comment.