Skip to content

Commit

Permalink
Add known peers for p2p network connection monitor (#19815)
Browse files Browse the repository at this point in the history
## Description 

This will enable quinn stats for p2p network

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
arun-koshy authored Oct 16, 2024
1 parent f8e33f5 commit 8fec5f8
Showing 1 changed file with 48 additions and 27 deletions.
75 changes: 48 additions & 27 deletions crates/sui-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use anemo::Network;
use anemo::PeerId;
use anemo_tower::callback::CallbackLayer;
use anemo_tower::trace::DefaultMakeSpan;
use anemo_tower::trace::DefaultOnFailure;
Expand Down Expand Up @@ -156,11 +157,19 @@ pub struct ValidatorComponents {
checkpoint_metrics: Arc<CheckpointMetrics>,
sui_tx_validator_metrics: Arc<SuiTxValidatorMetrics>,
}
pub struct P2pComponents {
p2p_network: Network,
known_peers: HashMap<PeerId, String>,
discovery_handle: discovery::Handle,
state_sync_handle: state_sync::Handle,
randomness_handle: randomness::Handle,
}

#[cfg(msim)]
mod simulator {
use super::*;
use std::sync::atomic::AtomicBool;

use super::*;
pub(super) struct SimState {
pub sim_node: sui_simulator::runtime::NodeHandle,
pub sim_safe_mode_expected: AtomicBool,
Expand Down Expand Up @@ -208,14 +217,14 @@ mod simulator {
}
}

#[cfg(msim)]
use simulator::*;

#[cfg(msim)]
pub use simulator::set_jwk_injector;
use sui_core::consensus_handler::ConsensusHandlerInitializer;
use sui_core::safe_client::SafeClientMetricsBase;
use sui_core::validator_tx_finalizer::ValidatorTxFinalizer;
#[cfg(msim)]
use simulator::*;
use sui_core::{
consensus_handler::ConsensusHandlerInitializer, safe_client::SafeClientMetricsBase,
validator_tx_finalizer::ValidatorTxFinalizer,
};
use sui_types::execution_config_utils::to_binary_config;

pub struct SuiNode {
Expand Down Expand Up @@ -612,16 +621,21 @@ impl SuiNode {
.unwrap_or_default()
.mailbox_capacity(),
);
let (p2p_network, discovery_handle, state_sync_handle, randomness_handle) =
Self::create_p2p_network(
&config,
state_sync_store.clone(),
chain_identifier,
trusted_peer_change_rx,
archive_readers.clone(),
randomness_tx,
&prometheus_registry,
)?;
let P2pComponents {
p2p_network,
known_peers,
discovery_handle,
state_sync_handle,
randomness_handle,
} = Self::create_p2p_network(
&config,
state_sync_store.clone(),
chain_identifier,
trusted_peer_change_rx,
archive_readers.clone(),
randomness_tx,
&prometheus_registry,
)?;

// We must explicitly send this instead of relying on the initial value to trigger
// watch value change, so that state-sync is able to process it.
Expand Down Expand Up @@ -778,8 +792,7 @@ impl SuiNode {
let connection_monitor_handle = consensus_core::AnemoConnectionMonitor::spawn(
p2p_network.downgrade(),
Arc::new(network_connection_metrics),
// TODO: add known seed peers via discovery so metrics will update.
HashMap::new(),
known_peers,
);

let connection_monitor_status = ConnectionMonitorStatus {
Expand Down Expand Up @@ -1026,12 +1039,7 @@ impl SuiNode {
archive_readers: ArchiveReaderBalancer,
randomness_tx: mpsc::Sender<(EpochId, RandomnessRound, Vec<u8>)>,
prometheus_registry: &Registry,
) -> Result<(
Network,
discovery::Handle,
state_sync::Handle,
randomness::Handle,
)> {
) -> Result<P2pComponents> {
let (state_sync, state_sync_server) = state_sync::Builder::new()
.config(config.p2p_config.state_sync.clone().unwrap_or_default())
.store(state_sync_store)
Expand All @@ -1043,6 +1051,18 @@ impl SuiNode {
.config(config.p2p_config.clone())
.build();

let discovery_config = config.p2p_config.discovery.clone().unwrap_or_default();
let known_peers: HashMap<PeerId, String> = discovery_config
.allowlisted_peers
.clone()
.into_iter()
.map(|ap| (ap.peer_id, "allowlisted_peer".to_string()))
.chain(config.p2p_config.seed_peers.iter().filter_map(|peer| {
peer.peer_id
.map(|peer_id| (peer_id, "seed_peer".to_string()))
}))
.collect();

let (randomness, randomness_router) =
randomness::Builder::new(config.protocol_public_key(), randomness_tx)
.config(config.p2p_config.randomness.clone().unwrap_or_default())
Expand Down Expand Up @@ -1153,12 +1173,13 @@ impl SuiNode {
let state_sync_handle = state_sync.start(p2p_network.clone());
let randomness_handle = randomness.start(p2p_network.clone());

Ok((
Ok(P2pComponents {
p2p_network,
known_peers,
discovery_handle,
state_sync_handle,
randomness_handle,
))
})
}

async fn construct_validator_components(
Expand Down

0 comments on commit 8fec5f8

Please sign in to comment.