Skip to content

Commit

Permalink
Panic if Zebra exceeds its connection limit
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Oct 27, 2021
1 parent ea49fcf commit deeb503
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions zebra-network/src/peer_set/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ where

// Connect the rx end to a PeerSet, wrapping new peers in load instruments.
let peer_set = PeerSet::new(
&config,
PeakEwmaDiscover::new(
// Discover interprets an error as stream termination,
// so discard any errored connections...
Expand Down
17 changes: 16 additions & 1 deletion zebra-network/src/peer_set/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use crate::{
external::InventoryHash,
internal::{Request, Response},
},
AddressBook, BoxError,
AddressBook, BoxError, Config,
};

/// A signal sent by the [`PeerSet`] when it has no ready peers, and gets a request from Zebra.
Expand Down Expand Up @@ -134,6 +134,8 @@ where
///
/// Used for logging diagnostics.
address_book: Arc<std::sync::Mutex<AddressBook>>,
/// The configured limit for inbound and outbound connections.
peerset_total_connection_limit: usize,
}

impl<D> PeerSet<D>
Expand All @@ -147,6 +149,7 @@ where
{
/// Construct a peerset which uses `discover` internally.
pub fn new(
config: &Config,
discover: D,
demand_signal: mpsc::Sender<MorePeers>,
handle_rx: tokio::sync::oneshot::Receiver<Vec<JoinHandle<Result<(), BoxError>>>>,
Expand All @@ -165,6 +168,7 @@ where
inventory_registry: InventoryRegistry::new(inv_stream),
last_peer_log: None,
address_book,
peerset_total_connection_limit: config.peerset_total_connection_limit(),
}
}

Expand Down Expand Up @@ -432,6 +436,17 @@ where
metrics::gauge!("pool.num_ready", num_ready as f64);
metrics::gauge!("pool.num_unready", num_unready as f64);
metrics::gauge!("zcash.net.peers", num_peers as f64);

// Security: make sure we haven't exceeded the connection limit
if num_peers > self.peerset_total_connection_limit {
let address_metrics = self.address_book.lock().unwrap().address_metrics();
panic!(
"unexpectedly exceeded configured peer set connection limit: \n\
peers: {:?}, ready: {:?}, unready: {:?}, \n\
address_metrics: {:?}",
num_peers, num_ready, num_unready, address_metrics,
);
}
}
}

Expand Down

0 comments on commit deeb503

Please sign in to comment.