Skip to content

Commit

Permalink
removed limiting from sync_blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 committed Apr 12, 2024
1 parent c8d06ef commit 96be90b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 46 deletions.
4 changes: 2 additions & 2 deletions node/actors/sync_blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ zksync_consensus_storage.workspace = true
zksync_consensus_utils.workspace = true

anyhow.workspace = true
rand.workspace = true
thiserror.workspace = true
tracing.workspace = true

[dev-dependencies]
assert_matches.workspace = true
async-trait.workspace = true
rand.workspace = true
test-casing.workspace = true
tokio.workspace = true

[lints]
workspace = true
workspace = true
45 changes: 5 additions & 40 deletions node/actors/sync_blocks/src/peers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Peer states tracked by the `SyncBlocks` actor.
#![allow(unused)]
use self::events::PeerStateEvent;
use crate::{io, Config};
use anyhow::Context as _;
Expand Down Expand Up @@ -142,13 +143,11 @@ impl PeerStates {
number: BlockNumber,
) -> ctx::OrCanceled<FinalBlock> {
while ctx.is_active() {
let Some((peer, permit)) = self.try_acquire_peer_permit(number) else {
let sleep_interval = self.config.sleep_interval_for_get_block;
ctx.sleep(sleep_interval).await?;
let Some(peer) = self.select_peer(number) else {
ctx.sleep(self.config.sleep_interval_for_get_block).await?;
continue;
};
let res = self.fetch_block_from_peer(ctx, &peer, number).await;
drop(permit);
match res {
Ok(block) => {
if let Some(send) = &self.events_sender {
Expand Down Expand Up @@ -204,43 +203,9 @@ impl PeerStates {
Ok(block)
}

fn try_acquire_peer_permit(
&self,
block_number: BlockNumber,
) -> Option<(node::PublicKey, sync::OwnedSemaphorePermit)> {
fn select_peer(&self, block_number: BlockNumber) -> Option<node::PublicKey> {
let peers = self.peers.lock().unwrap();
let mut peers_with_no_permits = vec![];
let eligible_peers_info = peers.iter().filter(|(peer_key, state)| {
if !state.state.contains(block_number) {
return false;
}
let available_permits = state.get_block_semaphore.available_permits();
// ^ `available_permits()` provides a lower bound on the actual number of available permits.
// Some permits may be released before acquiring a new permit below, but no other permits
// are acquired since we hold an exclusive lock on `peers`.
if available_permits == 0 {
peers_with_no_permits.push(*peer_key);
}
available_permits > 0
});
let peer_to_query = eligible_peers_info
.max_by_key(|(_, state)| state.get_block_semaphore.available_permits());

if let Some((peer_key, state)) = peer_to_query {
let permit = state
.get_block_semaphore
.clone()
.try_acquire_owned()
.unwrap();
// ^ `unwrap()` is safe for the reasons described in the above comment
Some((peer_key.clone(), permit))
} else {
tracing::debug!(
?peers_with_no_permits,
"No peers to query block #{block_number}"
);
None
}
peers.iter().filter(|(_,s)|s.state.contains(block_number)).next().map(|x|x.0.clone())
}

/// Drops peer state.
Expand Down
3 changes: 1 addition & 2 deletions node/actors/sync_blocks/src/peers/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ async fn test_try_acquire_peer_permit() {
peer_states.update(&peer, s.clone()).unwrap();
for block in b {
let got = peer_states
.try_acquire_peer_permit(block.number())
.map(|p| p.0);
.select_peer(block.number());
if s.first <= block.number()
&& s.last
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions node/actors/sync_blocks/src/peers/tests/multiple_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ impl Test for RequestingBlocksFromTwoPeers {
}
}

#[tokio::test]
/*#[tokio::test]
async fn requesting_blocks_from_two_peers() {
test_peer_states(RequestingBlocksFromTwoPeers).await;
}
}*/

#[derive(Debug, Clone, Copy)]
struct PeerBehavior {
Expand Down

0 comments on commit 96be90b

Please sign in to comment.