Skip to content

Commit

Permalink
Configuration change (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
sh3ll3x3c authored Nov 23, 2023
1 parent 9bb2450 commit b1141a5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/bin/avail-light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,16 @@ async fn run(error_sender: Sender<anyhow::Error>) -> Result<()> {
.context("Listening on UDP not to fail.")?;
info!("Listening for QUIC on port: {port}");

let tcp_port = port + 1;
p2p_client
.start_listening(
Multiaddr::empty()
.with(Protocol::from(Ipv4Addr::UNSPECIFIED))
.with(Protocol::Tcp(port + 1)),
.with(Protocol::Tcp(tcp_port)),
)
.await
.context("Listening on TCP not to fail.")?;
info!("Listening for TCP on port: {port}");
info!("Listening for TCP on port: {tcp_port}");

let p2p_clone = p2p_client.to_owned();
let cfg_clone = cfg.to_owned();
Expand All @@ -183,7 +184,7 @@ async fn run(error_sender: Sender<anyhow::Error>) -> Result<()> {
info!("Bootstrap done.");
},
Err(e) => {
error!("Error in the bootstrap process: {e:?}.");
warn!("Bootstrap process: {e:?}.");
},
}
});
Expand Down
7 changes: 5 additions & 2 deletions src/network/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{Context, Result};
use kad_mem_store::{MemoryStore, MemoryStoreConfig};
use libp2p::{
autonat, dcutr, identify, identity,
kad::{self, Mode, PeerRecord, QueryId},
kad::{self, PeerRecord, QueryId},
mdns, noise, ping, relay,
swarm::NetworkBehaviour,
tcp, upnp, yamux, PeerId, Swarm, SwarmBuilder,
Expand Down Expand Up @@ -191,7 +191,10 @@ pub fn init(
.build();

if is_fat_client {
swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server));
swarm
.behaviour_mut()
.kademlia
.set_mode(Some(cfg.kademlia_mode.to_kad_mode()));
}

// create sender channel for Event Loop Commands
Expand Down
45 changes: 40 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use kate_recovery::{
commitments,
matrix::{Dimensions, Partition},
};
use libp2p::kad::Mode as KadMode;
use libp2p::{Multiaddr, PeerId};
use serde::{de::Error, Deserialize, Serialize};
use sp_core::crypto::Ss58Codec;
Expand Down Expand Up @@ -98,11 +99,41 @@ impl TryFrom<(DaHeader, Option<f64>)> for BlockVerified {
}
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
#[serde(try_from = "String")]
pub enum KademliaMode {
Client,
Server,
}

impl KademliaMode {
pub fn to_kad_mode(self) -> KadMode {
match self {
KademliaMode::Client => KadMode::Client,
KademliaMode::Server => KadMode::Server,
}
}
}

impl TryFrom<String> for KademliaMode {
type Error = anyhow::Error;

fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
match value.to_lowercase().as_str() {
"client" => Ok(KademliaMode::Client),
"server" => Ok(KademliaMode::Server),
_ => Err(anyhow!(
"Wrong Kademlia mode. Expecting 'client' or 'server'."
)),
}
}
}

/// Client mode
///
/// * `LightClient` - light client is running
/// * `AppClient` - app client is running alongside the light client
#[derive(Serialize, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Mode {
LightClient,
AppClient(u32),
Expand Down Expand Up @@ -233,6 +264,7 @@ pub struct RuntimeConfig {
pub bootstraps: Vec<MultiaddrConfig>,
/// Defines a period of time in which periodic bootstraps will be repeated. (default: 300 sec)
pub bootstrap_period: u64,
pub operation_mode: KademliaMode,
/// Vector of Relay nodes, which are used for hole punching
pub relays: Vec<MultiaddrConfig>,
/// WebSocket endpoint of full node for subscribing to latest header, etc (default: [ws://127.0.0.1:9944]).
Expand Down Expand Up @@ -366,6 +398,7 @@ pub struct LibP2PConfig {
pub relays: Vec<(PeerId, Multiaddr)>,
pub bootstrap_interval: Duration,
pub connection_idle_timeout: Duration,
pub kademlia_mode: KademliaMode,
}

impl From<&RuntimeConfig> for LibP2PConfig {
Expand All @@ -379,6 +412,7 @@ impl From<&RuntimeConfig> for LibP2PConfig {
relays: val.relays.iter().map(Into::into).collect(),
bootstrap_interval: Duration::from_secs(val.bootstrap_period),
connection_idle_timeout: Duration::from_secs(val.connection_idle_timeout),
kademlia_mode: val.operation_mode,
}
}
}
Expand Down Expand Up @@ -508,7 +542,7 @@ impl Default for RuntimeConfig {
relays: Vec::new(),
full_node_ws: vec!["ws://127.0.0.1:9944".to_owned()],
app_id: None,
confidence: 92.0,
confidence: 99.9,
avail_path: "avail_path".to_owned(),
log_level: "INFO".to_owned(),
log_format_json: false,
Expand All @@ -517,18 +551,18 @@ impl Default for RuntimeConfig {
dht_parallelization_limit: 20,
put_batch_size: 1000,
query_proof_rpc_parallel_tasks: 8,
block_processing_delay: None,
block_processing_delay: Some(10),
block_matrix_partition: None,
sync_start_block: None,
sync_finality_enable: true,
max_cells_per_rpc: Some(30),
kad_record_ttl: 24 * 60 * 60,
threshold: 5000,
replication_factor: 10,
replication_factor: 5,
publication_interval: 12 * 60 * 60,
replication_interval: 3 * 60 * 60,
connection_idle_timeout: 30,
query_timeout: 60,
query_timeout: 10,
query_parallelism: 3,
caching_max_peers: 1,
disjoint_query_paths: false,
Expand All @@ -538,6 +572,7 @@ impl Default for RuntimeConfig {
#[cfg(feature = "crawl")]
crawl: crate::crawl_client::CrawlConfig::default(),
origin: "external".to_string(),
operation_mode: KademliaMode::Server,
}
}
}
Expand Down

0 comments on commit b1141a5

Please sign in to comment.