Skip to content

Commit

Permalink
make heartbeat config fields configurable and expose them to CLI (#998)
Browse files Browse the repository at this point in the history
currently our Block heartbeat config fields were hardcoded, this PR
makes them configurable and exposes them in the CLI

---------

Co-authored-by: Brandon Kite <brandonkite92@gmail.com>
  • Loading branch information
leviathanbeak and Voxelot committed Feb 7, 2023
1 parent d73cff7 commit fb4e932
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
35 changes: 31 additions & 4 deletions bin/fuel-core/src/cli/run/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use fuel_core::{
NotInitialized,
},
gossipsub_config::default_gossipsub_builder,
HeartbeatConfig,
Multiaddr,
},
types::{
Expand All @@ -20,6 +21,7 @@ use std::{
IpAddr,
Ipv4Addr,
},
num::NonZeroU32,
path::PathBuf,
str::FromStr,
time::Duration,
Expand Down Expand Up @@ -130,9 +132,9 @@ pub struct P2PArgs {
#[clap(long = "history_gossip", default_value = "3", env)]
pub history_gossip: usize,

/// Time between each heartbeat
#[clap(long = "heartbeat_interval", default_value = "1", env)]
pub heartbeat_interval: u64,
/// Time between each gossipsub heartbeat
#[clap(long = "gossip_heartbeat_interval", default_value = "1", env)]
pub gossip_heartbeat_interval: u64,

/// The maximum byte size for each gossip
#[clap(long = "max_transmit_size", default_value = "2048", env)]
Expand All @@ -145,6 +147,20 @@ pub struct P2PArgs {
/// Choose how long RequestResponse protocol connections will live if idle
#[clap(long = "connection_keep_alive", default_value = "20", env)]
pub connection_keep_alive: u64,

/// Sending of `BlockHeight` should not take longer than this duration, in seconds.
#[clap(long = "heartbeat_send_duration", default_value = "2", env)]
pub heartbeat_send_duration: u64,

/// Idle time in seconds before sending next `BlockHeight`
#[clap(long = "heartbeat_idle_duration", default_value = "1", env)]
pub heartbeat_idle_duration: u64,

/// Max failures allowed at `Heartbeat` protocol.
/// If reached, the protocol will request disconnect.
/// Cannot be zero.
#[clap(long = "heartbeat_max_failures", default_value = "5", env)]
pub heartbeat_max_failures: NonZeroU32,
}

#[derive(Debug, Clone, Args)]
Expand Down Expand Up @@ -224,7 +240,7 @@ impl P2PArgs {
.mesh_n_high(self.max_mesh_size)
.history_length(self.history_length)
.history_gossip(self.history_gossip)
.heartbeat_interval(Duration::from_secs(self.heartbeat_interval))
.heartbeat_interval(Duration::from_secs(self.gossip_heartbeat_interval))
.max_transmit_size(self.max_transmit_size)
.build()
.expect("valid gossipsub configuration");
Expand All @@ -235,6 +251,16 @@ impl P2PArgs {
Some(Duration::from_secs(self.random_walk))
};

let heartbeat_config = {
let send_duration = Duration::from_secs(self.heartbeat_send_duration);
let idle_duration = Duration::from_secs(self.heartbeat_idle_duration);
HeartbeatConfig::new(
send_duration,
idle_duration,
self.heartbeat_max_failures,
)
};

Ok(Config {
keypair: local_keypair,
network_name: self.network,
Expand All @@ -258,6 +284,7 @@ impl P2PArgs {
)),
topics: self.topics,
gossipsub_config,
heartbeat_config,
set_request_timeout: Duration::from_secs(self.request_timeout),
set_connection_keep_alive: Duration::from_secs(self.connection_keep_alive),
info_interval: Some(Duration::from_secs(self.info_interval)),
Expand Down
5 changes: 5 additions & 0 deletions crates/services/p2p/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
NEW_TX_GOSSIP_TOPIC,
},
},
heartbeat::HeartbeatConfig,
peer_manager::ConnectionState,
};
use fuel_core_types::blockchain::consensus::Genesis;
Expand Down Expand Up @@ -123,6 +124,8 @@ pub struct Config<State = Initialized> {
pub gossipsub_config: GossipsubConfig,
pub topics: Vec<String>,

pub heartbeat_config: HeartbeatConfig,

// RequestResponse related fields
/// Sets the timeout for inbound and outbound requests.
pub set_request_timeout: Duration,
Expand Down Expand Up @@ -171,6 +174,7 @@ impl Config<NotInitialized> {
info_interval: self.info_interval,
gossipsub_config: self.gossipsub_config,
topics: self.topics,
heartbeat_config: self.heartbeat_config,
set_request_timeout: self.set_request_timeout,
set_connection_keep_alive: self.set_connection_keep_alive,
metrics: self.metrics,
Expand Down Expand Up @@ -216,6 +220,7 @@ impl Config<NotInitialized> {
CON_VOTE_GOSSIP_TOPIC.into(),
],
gossipsub_config: default_gossipsub_config(),
heartbeat_config: HeartbeatConfig::default(),
set_request_timeout: REQ_RES_TIMEOUT,
set_connection_keep_alive: REQ_RES_TIMEOUT,
info_interval: Some(Duration::from_secs(3)),
Expand Down
20 changes: 14 additions & 6 deletions crates/services/p2p/src/heartbeat/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,31 @@ pub struct HeartbeatConfig {
/// Idle time before sending next `BlockHeight`
idle_timeout: Duration,
/// Max failures allowed.
/// If reached `HeartbeatHandler` will request closing of the connection.
/// If reached `HeartbeatHandler` will request closing of the connection.
max_failures: NonZeroU32,
}

impl HeartbeatConfig {
pub fn new() -> Self {
pub fn new(
send_timeout: Duration,
idle_timeout: Duration,
max_failures: NonZeroU32,
) -> Self {
Self {
send_timeout: Duration::from_secs(5),
idle_timeout: Duration::from_secs(10),
max_failures: NonZeroU32::new(5).expect("5 != 0"),
send_timeout,
idle_timeout,
max_failures,
}
}
}

impl Default for HeartbeatConfig {
fn default() -> Self {
Self::new()
Self::new(
Duration::from_secs(2),
Duration::from_secs(1),
NonZeroU32::new(5).expect("5 != 0"),
)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/services/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod request_response;
pub mod service;

pub use gossipsub::config as gossipsub_config;
pub use heartbeat::HeartbeatConfig;

pub use libp2p::{
multiaddr::Protocol,
Expand Down
4 changes: 2 additions & 2 deletions crates/services/p2p/src/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
config::Config,
heartbeat::{
Heartbeat,
HeartbeatConfig,
HeartbeatEvent,
},
};
Expand Down Expand Up @@ -111,7 +110,8 @@ impl PeerManagerBehaviour {
}
};

let heartbeat = Heartbeat::new(HeartbeatConfig::new(), BlockHeight::default());
let heartbeat =
Heartbeat::new(config.heartbeat_config.clone(), BlockHeight::default());

let reserved_peers: HashSet<PeerId> = config
.reserved_nodes
Expand Down

0 comments on commit fb4e932

Please sign in to comment.