Skip to content

Commit

Permalink
refactor: tce-broadcast config (#444)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Paitrault <[email protected]>
  • Loading branch information
Freyskeyd committed Feb 8, 2024
1 parent edf86ee commit 10c3879
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 165 deletions.
21 changes: 2 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions crates/topos-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ edition = "2021"

[dependencies]
topos-p2p = { path = "../topos-p2p" }
topos-tce-transport = { path = "../topos-tce-transport" }
topos-sequencer = { path = "../topos-sequencer" }
topos-core = { workspace = true, features = ["api"] }
topos-certificate-spammer = { path = "../topos-certificate-spammer" }
topos-tce-broadcast = { path = "../topos-tce-broadcast", optional = true }
topos-core = { path = "../topos-core" }
topos-wallet = { path = "../topos-wallet" }

async-stream.workspace = true
Expand Down Expand Up @@ -48,7 +44,6 @@ openssl = { version = "0.10.61", features = ["vendored"] }

[dev-dependencies]
topos-tce-broadcast = { path = "../topos-tce-broadcast" }
topos-tce-transport = { path = "../topos-tce-transport" }
topos-tce-synchronizer = { path = "../topos-tce-synchronizer" }
topos-tce-gatekeeper = { path = "../topos-tce-gatekeeper" }
topos-tce-api = { path = "../topos-tce-api" }
Expand Down
70 changes: 8 additions & 62 deletions crates/topos-config/src/tce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ use figment::{
use serde::{Deserialize, Serialize};
use topos_core::types::ValidatorId;
use topos_p2p::config::NetworkConfig;
use topos_tce_transport::ReliableBroadcastParams;

use crate::Config;
use topos_p2p::{Multiaddr, PeerId};

use self::broadcast::ReliableBroadcastParams;
use self::p2p::P2PConfig;

pub mod broadcast;
pub mod p2p;

const DEFAULT_IP: std::net::Ipv4Addr = std::net::Ipv4Addr::new(0, 0, 0, 0);

#[derive(Debug)]
Expand Down Expand Up @@ -83,72 +88,13 @@ pub struct TceConfig {
pub network_bootstrap_timeout: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct P2PConfig {
/// List of multiaddresses to listen for incoming connections
#[serde(default = "default_listen_addresses")]
pub listen_addresses: Vec<Multiaddr>,
/// List of multiaddresses to advertise to the network
#[serde(default = "default_public_addresses")]
pub public_addresses: Vec<Multiaddr>,

#[serde(skip)]
pub is_bootnode: bool,
}

impl Default for P2PConfig {
fn default() -> Self {
Self {
listen_addresses: default_listen_addresses(),
public_addresses: default_public_addresses(),
is_bootnode: false,
}
}
}

fn default_db_path() -> PathBuf {
PathBuf::from("./tce_rocksdb")
}

const fn default_network_bootstrap_timeout() -> u64 {
90
}

const fn default_libp2p_api_addr() -> SocketAddr {
SocketAddr::V4(std::net::SocketAddrV4::new(DEFAULT_IP, 9090))
}

fn default_listen_addresses() -> Vec<Multiaddr> {
vec![format!(
"/ip4/{}/tcp/{}",
default_libp2p_api_addr().ip(),
default_libp2p_api_addr().port()
)
.parse()
.expect(
r#"
Listen multiaddresses generation failure.
This is a critical bug that need to be report on `https://github.com/topos-protocol/topos/issues`
"#,
)]
}

fn default_public_addresses() -> Vec<Multiaddr> {
vec![format!(
"/ip4/{}/tcp/{}",
default_libp2p_api_addr().ip(),
default_libp2p_api_addr().port()
)
.parse()
.expect(
r#"
Public multiaddresses generation failure.
This is a critical bug that need to be report on `https://github.com/topos-protocol/topos/issues`
"#,
)]
fn default_db_path() -> PathBuf {
PathBuf::from("./tce_rocksdb")
}

const fn default_minimum_tce_cluster_size() -> usize {
NetworkConfig::MINIMUM_CLUSTER_SIZE
}
Expand Down
24 changes: 24 additions & 0 deletions crates/topos-config/src/tce/broadcast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use serde::{Deserialize, Serialize};

/// Broadcast threshold configurations
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ReliableBroadcastParams {
/// Echo threshold
pub echo_threshold: usize,
/// Ready threshold
pub ready_threshold: usize,
/// Delivery threshold
pub delivery_threshold: usize,
}

impl ReliableBroadcastParams {
pub const fn new(n: usize) -> Self {
let f: usize = n / 3;

Self {
echo_threshold: 1 + (n + f) / 2,
ready_threshold: 1 + f,
delivery_threshold: 2 * f + 1,
}
}
}
64 changes: 64 additions & 0 deletions crates/topos-config/src/tce/p2p.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::net::SocketAddr;

use serde::{Deserialize, Serialize};
use topos_p2p::Multiaddr;

use super::DEFAULT_IP;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct P2PConfig {
/// List of multiaddresses to listen for incoming connections
#[serde(default = "default_listen_addresses")]
pub listen_addresses: Vec<Multiaddr>,
/// List of multiaddresses to advertise to the network
#[serde(default = "default_public_addresses")]
pub public_addresses: Vec<Multiaddr>,

#[serde(skip)]
pub is_bootnode: bool,
}

impl Default for P2PConfig {
fn default() -> Self {
Self {
listen_addresses: default_listen_addresses(),
public_addresses: default_public_addresses(),
is_bootnode: false,
}
}
}

const fn default_libp2p_api_addr() -> SocketAddr {
SocketAddr::V4(std::net::SocketAddrV4::new(DEFAULT_IP, 9090))
}

fn default_listen_addresses() -> Vec<Multiaddr> {
vec![format!(
"/ip4/{}/tcp/{}",
default_libp2p_api_addr().ip(),
default_libp2p_api_addr().port()
)
.parse()
.expect(
r#"
Listen multiaddresses generation failure.
This is a critical bug that need to be report on `https://github.com/topos-protocol/topos/issues`
"#,
)]
}

fn default_public_addresses() -> Vec<Multiaddr> {
vec![format!(
"/ip4/{}/tcp/{}",
default_libp2p_api_addr().ip(),
default_libp2p_api_addr().port()
)
.parse()
.expect(
r#"
Public multiaddresses generation failure.
This is a critical bug that need to be report on `https://github.com/topos-protocol/topos/issues`
"#,
)]
}
2 changes: 1 addition & 1 deletion crates/topos-tce-broadcast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ tokio-stream = { workspace = true, features = ["sync"] }
tokio-util.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
tracing.workspace = true
tce_transport = { package = "topos-tce-transport", path = "../topos-tce-transport"}
topos-core = { workspace = true, features = ["uci"] }
topos-config = { path = "../topos-config/" }
topos-metrics = { path = "../topos-metrics/" }
topos-tce-storage = { path = "../topos-tce-storage/" }
topos-crypto = { path = "../topos-crypto" }
Expand Down
2 changes: 1 addition & 1 deletion crates/topos-tce-broadcast/benches/task_manager.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashSet;
use std::str::FromStr;
use std::sync::Arc;
use tce_transport::ReliableBroadcastParams;
use tokio::sync::{broadcast, mpsc, oneshot};
use topos_config::tce::broadcast::ReliableBroadcastParams;
use topos_core::types::ValidatorId;
use topos_crypto::messages::MessageSigner;
use topos_tce_broadcast::double_echo::DoubleEcho;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::event::ProtocolEvents;
use crate::sampler::SubscriptionsView;
use std::sync::Arc;
use std::{collections::HashSet, time};
use tce_transport::ProtocolEvents;
use tokio::sync::mpsc;
use topos_core::{
types::{
Expand Down
3 changes: 2 additions & 1 deletion crates/topos-tce-broadcast/src/double_echo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
//! be ignored by others. `fullnode` still consumes Echo and Ready coming from
//! validators and use those messages to build their state.

use crate::event::ProtocolEvents;
use crate::{DoubleEchoCommand, SubscriptionsView};
use std::collections::HashSet;
use std::sync::Arc;
use tce_transport::{ProtocolEvents, ReliableBroadcastParams};
use tokio::sync::{broadcast, mpsc, oneshot};
use tokio_util::sync::CancellationToken;
use topos_config::tce::broadcast::ReliableBroadcastParams;
use topos_core::{
types::ValidatorId,
uci::{Certificate, CertificateId},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
//! implementation of Topos Network Transport
//!
use clap::Parser;
use serde::{Deserialize, Serialize};
use topos_core::{
types::ValidatorId,
uci::{Certificate, CertificateId},
};
use topos_crypto::messages::Signature;

#[derive(Parser, Clone, Debug, Default, Deserialize, Serialize)]
#[command(name = "Parameters of the reliable broadcast")]
pub struct ReliableBroadcastParams {
/// Echo threshold
#[arg(long, env = "TCE_ECHO_THRESHOLD", default_value_t = 1)]
pub echo_threshold: usize,
/// Ready threshold
#[arg(long, env = "TCE_READY_THRESHOLD", default_value_t = 1)]
pub ready_threshold: usize,
/// Delivery threshold
#[arg(long, env = "TCE_DELIVERY_THRESHOLD", default_value_t = 1)]
pub delivery_threshold: usize,
}

impl ReliableBroadcastParams {
pub fn new(n: usize) -> Self {
let f: usize = n / 3;

Self {
echo_threshold: 1 + (n + f) / 2,
ready_threshold: 1 + f,
delivery_threshold: 2 * f + 1,
}
}
}

/// Protocol events
#[derive(Clone, Debug)]
pub enum ProtocolEvents {
Expand Down Expand Up @@ -66,7 +36,4 @@ pub enum ProtocolEvents {
},
/// For simulation purpose, for now only caused by ill-formed sampling
Die,

/// Stable Sample
StableSample,
}
Loading

0 comments on commit 10c3879

Please sign in to comment.