From 2b18e5307b9878e74efbb9d65e935d797a9fe58c Mon Sep 17 00:00:00 2001 From: draoi Date: Sat, 6 Apr 2024 11:36:14 +0200 Subject: [PATCH] session: cleanup SessionBitFlags Add an additional byte to SessionBitFlags to accommodate SESSION_REFINE and reduce the risk of logic errors. Additionally: * `!SESSION_SEED & !SESSION_REFINE` is now referred to as `SESSION_DEFAULT` * `!SESSION_REFINE` is refered to as `SESSION_NET`. * `SESSION_ALL` has been deleted since it was conceptually out-dated * Binaries have been updated. --- bin/darkfid/src/utils.rs | 8 ++++---- bin/dhtd/dhtd/src/tests.rs | 2 +- bin/fud/fud/src/main.rs | 2 +- bin/genev/genevd/src/main.rs | 4 ++-- bin/tau/taud/src/main.rs | 4 ++-- example/dchat/dchatd/src/main.rs | 2 +- src/event_graph/tests.rs | 4 ++-- src/net/mod.rs | 2 +- src/net/protocol/mod.rs | 6 +++--- src/net/session/mod.rs | 18 ++++++++++-------- 10 files changed, 27 insertions(+), 25 deletions(-) diff --git a/bin/darkfid/src/utils.rs b/bin/darkfid/src/utils.rs index a677ae16d..44c0b3204 100644 --- a/bin/darkfid/src/utils.rs +++ b/bin/darkfid/src/utils.rs @@ -23,7 +23,7 @@ use smol::{fs::read_to_string, Executor}; use structopt_toml::StructOptToml; use darkfi::{ - net::{P2p, P2pPtr, Settings, SESSION_ALL}, + net::{P2p, P2pPtr, Settings, SESSION_NET}, rpc::jsonrpc::JsonSubscriber, util::path::get_config_path, validator::ValidatorPtr, @@ -48,7 +48,7 @@ pub async fn spawn_p2p( let _validator = validator.clone(); registry - .register(SESSION_ALL, move |channel, _p2p| { + .register(SESSION_NET, move |channel, _p2p| { let validator = _validator.clone(); async move { ProtocolSync::init(channel, validator).await.unwrap() } }) @@ -57,7 +57,7 @@ pub async fn spawn_p2p( let _validator = validator.clone(); let _subscriber = subscribers.get("proposals").unwrap().clone(); registry - .register(SESSION_ALL, move |channel, p2p| { + .register(SESSION_NET, move |channel, p2p| { let validator = _validator.clone(); let subscriber = _subscriber.clone(); async move { @@ -71,7 +71,7 @@ pub async fn spawn_p2p( let _validator = validator.clone(); let _subscriber = subscribers.get("txs").unwrap().clone(); registry - .register(SESSION_ALL, move |channel, p2p| { + .register(SESSION_NET, move |channel, p2p| { let validator = _validator.clone(); let subscriber = _subscriber.clone(); async move { ProtocolTx::init(channel, validator, p2p, subscriber).await.unwrap() } diff --git a/bin/dhtd/dhtd/src/tests.rs b/bin/dhtd/dhtd/src/tests.rs index 2faa4b7aa..fb224c334 100644 --- a/bin/dhtd/dhtd/src/tests.rs +++ b/bin/dhtd/dhtd/src/tests.rs @@ -74,7 +74,7 @@ async fn dht_remote_get_insert_real(ex: Arc>) -> Result<()> { let _dhtd = dhtd.clone(); registry - .register(net::SESSION_ALL, move |channel, p2p| { + .register(net::SESSION_NET, move |channel, p2p| { let dhtd = _dhtd.clone(); async move { ProtocolDht::init(channel, p2p, dhtd).await.unwrap() } }) diff --git a/bin/fud/fud/src/main.rs b/bin/fud/fud/src/main.rs index 985e27a49..9e469f176 100644 --- a/bin/fud/fud/src/main.rs +++ b/bin/fud/fud/src/main.rs @@ -604,7 +604,7 @@ async fn realmain(args: Args, ex: Arc>) -> Result<()> { let registry = p2p.protocol_registry(); let fud_ = fud.clone(); registry - .register(net::SESSION_ALL, move |channel, p2p| { + .register(net::SESSION_NET, move |channel, p2p| { let fud_ = fud_.clone(); async move { ProtocolFud::init(fud_, channel, p2p).await.unwrap() } }) diff --git a/bin/genev/genevd/src/main.rs b/bin/genev/genevd/src/main.rs index 35003ceb3..b9ab13c29 100644 --- a/bin/genev/genevd/src/main.rs +++ b/bin/genev/genevd/src/main.rs @@ -21,7 +21,7 @@ use std::sync::{Arc, OnceLock}; use darkfi::{ async_daemonize, cli_desc, event_graph::{proto::ProtocolEventGraph, EventGraph, EventGraphPtr, NULL_ID}, - net::{settings::SettingsOpt, P2p, SESSION_ALL}, + net::{settings::SettingsOpt, P2p, SESSION_NET}, rpc::{ jsonrpc::JsonSubscriber, server::{listen_and_serve, RequestHandler}, @@ -112,7 +112,7 @@ async fn realmain(settings: Args, executor: Arc>) -> Res let event_graph_ = Arc::clone(&event_graph); let registry = p2p.protocol_registry(); registry - .register(SESSION_ALL, move |channel, _| { + .register(SESSION_NET, move |channel, _| { let event_graph_ = event_graph_.clone(); async move { ProtocolEventGraph::init(event_graph_, channel).await.unwrap() } }) diff --git a/bin/tau/taud/src/main.rs b/bin/tau/taud/src/main.rs index 8fa4faf9c..bebf584af 100644 --- a/bin/tau/taud/src/main.rs +++ b/bin/tau/taud/src/main.rs @@ -48,7 +48,7 @@ use darkfi::{ proto::{EventPut, ProtocolEventGraph}, Event, EventGraph, EventGraphPtr, NULL_ID, }, - net::{P2p, P2pPtr, SESSION_ALL}, + net::{P2p, P2pPtr, SESSION_NET}, rpc::{ jsonrpc::JsonSubscriber, server::{listen_and_serve, RequestHandler}, @@ -362,7 +362,7 @@ async fn realmain(settings: Args, executor: Arc>) -> Res let event_graph_ = Arc::clone(&event_graph); let registry = p2p.protocol_registry(); registry - .register(SESSION_ALL, move |channel, _| { + .register(SESSION_NET, move |channel, _| { let event_graph_ = event_graph_.clone(); async move { ProtocolEventGraph::init(event_graph_, channel).await.unwrap() } }) diff --git a/example/dchat/dchatd/src/main.rs b/example/dchat/dchatd/src/main.rs index 63204d488..01870e53e 100644 --- a/example/dchat/dchatd/src/main.rs +++ b/example/dchat/dchatd/src/main.rs @@ -152,7 +152,7 @@ async fn realmain(args: Args, ex: Arc>) -> Result<()> { info!("Registering Dchat protocol"); let registry = p2p.protocol_registry(); registry - .register(!net::session::SESSION_SEED, move |channel, _p2p| { + .register(net::session::SESSION_DEFAULT, move |channel, _p2p| { let msgs_ = msgs.clone(); async move { ProtocolDchat::init(channel, msgs_).await } }) diff --git a/src/event_graph/tests.rs b/src/event_graph/tests.rs index e69134ddb..6993fc29c 100644 --- a/src/event_graph/tests.rs +++ b/src/event_graph/tests.rs @@ -30,7 +30,7 @@ use crate::{ proto::{EventPut, ProtocolEventGraph}, Event, EventGraph, }, - net::{P2p, Settings, SESSION_ALL}, + net::{P2p, Settings, SESSION_NET}, system::sleep, }; @@ -98,7 +98,7 @@ async fn spawn_node( // Register the P2P protocols let registry = p2p.protocol_registry(); registry - .register(SESSION_ALL, move |channel, _| { + .register(SESSION_NET, move |channel, _| { let event_graph_ = event_graph_.clone(); async move { ProtocolEventGraph::init(event_graph_, channel).await.unwrap() } }) diff --git a/src/net/mod.rs b/src/net/mod.rs index a801d6e32..7611e825d 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -104,7 +104,7 @@ pub use protocol::{ /// the first time. Implements the `Session` trait which describes the /// common functions across all sessions. pub mod session; -pub use session::SESSION_ALL; +pub use session::SESSION_NET; /// Handles the acceptance of inbound socket connections. /// Used to start listening on a local socket, to accept incoming connections, diff --git a/src/net/protocol/mod.rs b/src/net/protocol/mod.rs index bcd141341..b0278ca76 100644 --- a/src/net/protocol/mod.rs +++ b/src/net/protocol/mod.rs @@ -18,7 +18,7 @@ use super::{ p2p::P2pPtr, - session::{SESSION_ALL, SESSION_SEED}, + session::{SESSION_DEFAULT, SESSION_NET, SESSION_SEED}, }; /// Manages the tasks for the network protocol. Used by other connection @@ -75,7 +75,7 @@ pub mod protocol_registry; /// Register the default network protocols for a p2p instance. pub async fn register_default_protocols(p2p: P2pPtr) { let registry = p2p.protocol_registry(); - registry.register(SESSION_ALL, ProtocolPing::init).await; - registry.register(!SESSION_SEED, ProtocolAddress::init).await; + registry.register(SESSION_NET, ProtocolPing::init).await; + registry.register(SESSION_DEFAULT, ProtocolAddress::init).await; registry.register(SESSION_SEED, ProtocolSeed::init).await; } diff --git a/src/net/session/mod.rs b/src/net/session/mod.rs index f43b03708..31647bdd3 100644 --- a/src/net/session/mod.rs +++ b/src/net/session/mod.rs @@ -40,15 +40,17 @@ pub mod refine_session; pub use refine_session::{RefineSession, RefineSessionPtr}; /// Bitwise selectors for the `protocol_registry` -// TODO: SESSION_ALL currently does not include SESSION_REFINE, which is -// conceptually wrong. Should we rename SESSION_ALL to SESSION_DEFAULT? pub type SessionBitFlag = u32; -pub const SESSION_INBOUND: SessionBitFlag = 0b0001; -pub const SESSION_OUTBOUND: SessionBitFlag = 0b0010; -pub const SESSION_MANUAL: SessionBitFlag = 0b0100; -pub const SESSION_SEED: SessionBitFlag = 0b1000; -pub const SESSION_REFINE: SessionBitFlag = 0b0000; -pub const SESSION_ALL: SessionBitFlag = 0b1111; +pub const SESSION_INBOUND: SessionBitFlag = 0b00001; +pub const SESSION_OUTBOUND: SessionBitFlag = 0b00010; +pub const SESSION_MANUAL: SessionBitFlag = 0b00100; +pub const SESSION_SEED: SessionBitFlag = 0b01000; +pub const SESSION_REFINE: SessionBitFlag = 0b10000; + +// Equivalent to !SESSION_REFINE. +pub const SESSION_NET: SessionBitFlag = 0b01111; +// Equivalent to !SESSION_REFINE & !SESSION_SEED. +pub const SESSION_DEFAULT: SessionBitFlag = 0b00111; pub type SessionWeakPtr = Weak;