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.
This commit is contained in:
draoi
2024-04-06 11:36:14 +02:00
parent 9d64403407
commit 2b18e5307b
10 changed files with 27 additions and 25 deletions

View File

@@ -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() }

View File

@@ -74,7 +74,7 @@ async fn dht_remote_get_insert_real(ex: Arc<Executor<'_>>) -> 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() }
})

View File

@@ -604,7 +604,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> 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() }
})

View File

@@ -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<smol::Executor<'static>>) -> 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() }
})

View File

@@ -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<smol::Executor<'static>>) -> 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() }
})

View File

@@ -152,7 +152,7 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> 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 }
})

View File

@@ -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() }
})

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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<dyn Session + Send + Sync + 'static>;