/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2026 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
use sled_overlay::sled;
use smol::lock::Mutex;
use std::{collections::HashSet, path::PathBuf};
use darkfi::{
event_graph::EventGraphPtr, net::P2pPtr, rpc::jsonrpc::JsonSubscriber, system::StoppableTaskPtr,
};
use darkfi_serial::{async_trait, SerialDecodable, SerialEncodable};
/// IRC server and client handler implementation
pub mod irc;
use irc::server::IrcServer;
use crate::irc::server::MAX_NICK_LEN;
/// Cryptography utilities
pub mod crypto;
/// Pregenerated DarkIRC RLN identity commitments.
pub mod genesis_commits;
/// JSON-RPC methods
pub mod rpc;
/// Settings utilities
pub mod settings;
/// IRC PRIVMSG
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct Privmsg {
pub version: u8,
pub msg_type: u8,
pub channel: String,
pub nick: String,
pub msg: String,
}
pub struct DarkIrc {
/// P2P network pointer
p2p: P2pPtr,
/// Sled DB (also used in event_graph and for RLN)
sled: sled::Db,
/// Event Graph instance
event_graph: EventGraphPtr,
/// JSON-RPC connection tracker
rpc_connections: Mutex>,
/// dnet JSON-RPC subscriber
dnet_sub: JsonSubscriber,
/// deg JSON-RPC subscriber
deg_sub: JsonSubscriber,
/// Gource visualization JSON-RPC subscriber
gource_sub: JsonSubscriber,
/// Replay logs (DB) path
replay_datastore: PathBuf,
}
impl DarkIrc {
pub fn new(
p2p: P2pPtr,
sled: sled::Db,
event_graph: EventGraphPtr,
dnet_sub: JsonSubscriber,
deg_sub: JsonSubscriber,
gource_sub: JsonSubscriber,
replay_datastore: PathBuf,
) -> Self {
Self {
p2p,
sled,
event_graph,
rpc_connections: Mutex::new(HashSet::new()),
dnet_sub,
deg_sub,
gource_sub,
replay_datastore,
}
}
}
pub fn pad(string: &str) -> Vec {
let mut bytes = string.as_bytes().to_vec();
bytes.resize(MAX_NICK_LEN, 0x00);
bytes
}
pub fn unpad(vec: &mut Vec) {
if let Some(i) = vec.iter().rposition(|x| *x != 0) {
let new_len = i + 1;
vec.truncate(new_len);
}
}
#[cfg(test)]
mod tests {
use std::{
collections::HashMap,
sync::{
atomic::{AtomicU16, Ordering},
Arc,
},
};
use darkfi::{
event_graph::{
proto::{ProtocolEventGraph, RangeCursor, SyncDirection},
Event, EventGraph, EventGraphConfig, EventGraphPtr, Header, NULL_PARENTS,
},
net::{
session::SESSION_DEFAULT,
settings::{NetworkProfile, Settings},
P2p, P2pPtr,
},
system::sleep,
};
use darkfi_serial::{deserialize_async_partial, serialize_async};
use easy_parallel::Parallel;
use sled_overlay::sled;
use smol::{channel, future, Executor};
use url::Url;
use super::Privmsg;
struct HistoryNode {
p2p: P2pPtr,
event_graph: EventGraphPtr,
}
fn alloc_port_base() -> u16 {
static NEXT: AtomicU16 = AtomicU16::new(24_400);
NEXT.fetch_add(2, Ordering::SeqCst)
}
fn history_test_config() -> EventGraphConfig {
EventGraphConfig {
initial_genesis: 1_704_067_200_000,
hours_rotation: 1,
genesis_contents: b"darkirc-mobile-history-test".to_vec(),
rln_enabled: false,
pregenerated_identity_commitments: Vec::new(),
max_dags: Some(5),
}
}
async fn spawn_history_node(
port_base: u16,
port_offset: u16,
peer_offsets: &[u16],
ex: Arc>,
) -> HistoryNode {
let mut profiles = HashMap::new();
profiles.insert(
"tcp".to_string(),
NetworkProfile { outbound_connect_timeout: 2, ..Default::default() },
);
let inbound =
vec![Url::parse(&format!("tcp://127.0.0.1:{}", port_base + port_offset)).unwrap()];
let peers = peer_offsets
.iter()
.map(|offset| Url::parse(&format!("tcp://127.0.0.1:{}", port_base + *offset)).unwrap())
.collect();
let settings = Settings {
localnet: true,
inbound_addrs: inbound,
outbound_connections: 0,
inbound_connections: usize::MAX,
peers,
active_profiles: vec!["tcp".to_string()],
profiles,
..Default::default()
};
let p2p = P2p::new(settings, ex.clone()).await.unwrap();
let sled_db = sled::Config::new().temporary(true).open().unwrap();
let event_graph =
EventGraph::new(p2p.clone(), sled_db, "/tmp".into(), false, history_test_config(), ex)
.await
.unwrap();
event_graph.synced.store(true, Ordering::Release);
let event_graph_weak = Arc::downgrade(&event_graph);
p2p.protocol_registry()
.register(SESSION_DEFAULT, move |channel, _| {
let event_graph_weak = event_graph_weak.clone();
async move {
let event_graph = event_graph_weak
.upgrade()
.expect("EventGraph dropped before protocol factory invoked");
ProtocolEventGraph::init(event_graph, channel).await.unwrap()
}
})
.await;
HistoryNode { p2p, event_graph }
}
async fn make_history_network(ex: Arc>) -> Vec {
let port_base = alloc_port_base();
let nodes = vec![
spawn_history_node(port_base, 0, &[1], ex.clone()).await,
spawn_history_node(port_base, 1, &[0], ex).await,
];
for node in &nodes {
node.p2p.clone().start().await.unwrap();
}
sleep(5).await;
nodes
}
async fn shutdown_history_network(nodes: &[HistoryNode]) {
for node in nodes {
node.p2p.stop().await;
}
}
fn run_history_test(body: F)
where
F: FnOnce(Arc>) -> Fut,
Fut: std::future::Future