From fa93009a5734802875004276b0594c16d73229d7 Mon Sep 17 00:00:00 2001 From: parazyd Date: Fri, 28 Jan 2022 11:51:27 +0100 Subject: [PATCH 01/38] Remove zkvm feature and compile it directly with crypto feature. --- Cargo.toml | 10 ++-------- Makefile | 14 +++++++++----- src/zk/mod.rs | 5 +---- src/zk/vm_stack.rs | 2 +- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a3d777701..6594c21d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -223,12 +223,6 @@ zkas = [ "util", ] -zkvm = [ - "crypto", - "zkas", -] - - [[example]] name = "net" path = "example/net.rs" @@ -252,9 +246,9 @@ required-features = ["crypto"] [[example]] name = "mint" path = "proof/mint.rs" -required-features = ["cli", "zkvm"] +required-features = ["cli", "crypto", "zkas"] [[example]] name = "burn" path = "proof/burn.rs" -required-features = ["cli", "zkvm"] +required-features = ["cli", "crypto", "zkas"] diff --git a/Makefile b/Makefile index d4fccc733..fa813fe31 100644 --- a/Makefile +++ b/Makefile @@ -39,11 +39,15 @@ test: test-vm test-tx test-tx: $(CARGO) run --release --features=node --example tx -test-vm: zkas - ./zkas proof/mint.zk - $(CARGO) run --release --features=cli,zkvm --example mint - ./zkas proof/burn.zk - $(CARGO) run --release --features=cli,zkvm --example burn +VM_SRC = proof/mint.zk proof/burn.zk +VM_BIN = $(VM_SRC:=.bin) + +$(VM_BIN): $(VM_SRC) + ./zkas $(basename $@) -o $@ + +test-vm: + $(CARGO) run --release --features=cli,crypto,zkas --example mint + $(CARGO) run --release --features=cli,crypto,zkas --example burn clean: rm -f $(BINS) diff --git a/src/zk/mod.rs b/src/zk/mod.rs index a68994c2b..dc6405089 100644 --- a/src/zk/mod.rs +++ b/src/zk/mod.rs @@ -1,7 +1,4 @@ pub mod circuit; - -#[cfg(feature = "zkvm")] +/// Halo2 zkas virtual machine pub mod vm; - -#[cfg(feature = "zkvm")] mod vm_stack; diff --git a/src/zk/vm_stack.rs b/src/zk/vm_stack.rs index 67e0881c4..555ab5987 100644 --- a/src/zk/vm_stack.rs +++ b/src/zk/vm_stack.rs @@ -1,4 +1,4 @@ -//! Stack type abstractions +//! VM stack type abstractions use halo2_gadgets::{ ecc::{chip::EccChip, FixedPoint, Point}, utilities::CellValue, From fdb11f1fad8b64f0fc667f33fee16d6ac352b351 Mon Sep 17 00:00:00 2001 From: parazyd Date: Fri, 28 Jan 2022 12:00:58 +0100 Subject: [PATCH 02/38] tests: Implement variable logging in zkas VM examples. --- proof/burn.rs | 14 ++++++++++---- proof/mint.rs | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/proof/burn.rs b/proof/burn.rs index b164f0c18..7f6abdf45 100644 --- a/proof/burn.rs +++ b/proof/burn.rs @@ -15,16 +15,22 @@ use halo2_gadgets::primitives::{ poseidon::{ConstantLength, P128Pow5T3}, }; use incrementalmerkletree::{bridgetree::BridgeTree, Frontier, Tree}; +use log::info; use pasta_curves::{ arithmetic::{CurveAffine, Field}, group::Curve, pallas, }; use rand::rngs::OsRng; -use simplelog::{ColorChoice::Auto, Config, LevelFilter::Debug, TermLogger, TerminalMode::Mixed}; +use simplelog::{ColorChoice::Auto, Config, LevelFilter, TermLogger, TerminalMode::Mixed}; fn main() -> Result<()> { - TermLogger::init(Debug, Config::default(), Mixed, Auto)?; + let loglevel = match option_env!("RUST_LOG") { + Some("debug") => LevelFilter::Debug, + Some("trace") => LevelFilter::Trace, + Some(_) | None => LevelFilter::Info, + }; + TermLogger::init(loglevel, Config::default(), Mixed, Auto)?; /* ANCHOR: main */ let bincode = include_bytes!("burn.zk.bin"); @@ -114,7 +120,7 @@ fn main() -> Result<()> { // Create the circuit let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone()); - // Build the proving key and create the zero-knowledge proof + info!(target: "PROVER", "Building proving key and creating the zero-knowledge proof"); let proving_key = ProvingKey::build(11, &circuit); let proof = Proof::create(&proving_key, &[circuit], &public_inputs)?; @@ -139,7 +145,7 @@ fn main() -> Result<()> { // Create the circuit let circuit = ZkCircuit::new(verifier_witnesses, zkbin); - // Build the verifying key and verify the zero-knowledge proof + info!(target: "VERIFIER", "Building verifying key and verifying the zero-knowledge proof"); let verifying_key = VerifyingKey::build(11, &circuit); proof.verify(&verifying_key, &public_inputs)?; /* ANCHOR_END: main */ diff --git a/proof/mint.rs b/proof/mint.rs index 05f04512b..d776a3c21 100644 --- a/proof/mint.rs +++ b/proof/mint.rs @@ -13,16 +13,22 @@ use halo2_gadgets::primitives::{ poseidon, poseidon::{ConstantLength, P128Pow5T3}, }; +use log::info; use pasta_curves::{ arithmetic::{CurveAffine, Field}, group::Curve, pallas, }; use rand::rngs::OsRng; -use simplelog::{ColorChoice::Auto, Config, LevelFilter::Debug, TermLogger, TerminalMode::Mixed}; +use simplelog::{ColorChoice::Auto, Config, LevelFilter, TermLogger, TerminalMode::Mixed}; fn main() -> Result<()> { - TermLogger::init(Debug, Config::default(), Mixed, Auto)?; + let loglevel = match option_env!("RUST_LOG") { + Some("debug") => LevelFilter::Debug, + Some("trace") => LevelFilter::Trace, + Some(_) | None => LevelFilter::Info, + }; + TermLogger::init(loglevel, Config::default(), Mixed, Auto)?; /* ANCHOR: main */ let bincode = include_bytes!("mint.zk.bin"); @@ -69,7 +75,7 @@ fn main() -> Result<()> { // Create the circuit let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone()); - // Build the proving key and create the zero-knowledge proof + info!(target: "PROVER", "Building proving key and creating the zero-knowledge proof"); let proving_key = ProvingKey::build(11, &circuit); let proof = Proof::create(&proving_key, &[circuit], &public_inputs)?; @@ -92,7 +98,7 @@ fn main() -> Result<()> { // Create the circuit let circuit = ZkCircuit::new(verifier_witnesses, zkbin); - // Build the verifying key and verify the zero-knowledge proof + info!(target: "VERIFIER", "Building verifying key and verifying the zero-knowledge proof"); let verifying_key = VerifyingKey::build(11, &circuit); proof.verify(&verifying_key, &public_inputs)?; /* ANCHOR_END: main */ From 5ff0b12de30b09e2978ba478f666c613554ae482 Mon Sep 17 00:00:00 2001 From: Dastan-glitch <75807342+Dastan-glitch@users.noreply.github.com> Date: Mon, 31 Jan 2022 04:41:02 -0500 Subject: [PATCH 03/38] book: fixing url (#57) --- book/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/src/tutorial.md b/book/src/tutorial.md index 517b7b49e..400050145 100644 --- a/book/src/tutorial.md +++ b/book/src/tutorial.md @@ -107,7 +107,7 @@ tutorial](https://bitzuma.com/posts/a-beginners-guide-to-the-electrum-bitcoin-wa for more details. For Solana, you can either install the Solana command-line suite or use -[sollet](sollet.io). +[sollet](https://www.sollet.io). Follow [this tutorial](https://docs.solana.com/cli) for the Solana command-line. For sollet.io, switch the network to testnet and click From 46985baca587288072a9c08bf127d379360b15c7 Mon Sep 17 00:00:00 2001 From: parazyd Date: Mon, 31 Jan 2022 10:42:20 +0100 Subject: [PATCH 04/38] Fix VM tests. --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index fa813fe31..c8817c7ca 100644 --- a/Makefile +++ b/Makefile @@ -37,15 +37,15 @@ test: test-vm test-tx $(CARGO) test --release --all-features --all test-tx: - $(CARGO) run --release --features=node --example tx + $(CARGO) run --release --features=node,zkas --example tx VM_SRC = proof/mint.zk proof/burn.zk VM_BIN = $(VM_SRC:=.bin) -$(VM_BIN): $(VM_SRC) +$(VM_BIN): zkas $(VM_SRC) ./zkas $(basename $@) -o $@ -test-vm: +test-vm: $(VM_BIN) $(CARGO) run --release --features=cli,crypto,zkas --example mint $(CARGO) run --release --features=cli,crypto,zkas --example burn From 73e34b339e48b7b7b149471f224a9ea8eba6201d Mon Sep 17 00:00:00 2001 From: parazyd Date: Mon, 31 Jan 2022 10:57:54 +0100 Subject: [PATCH 05/38] Make crypto feature depend on zkas. --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 6594c21d5..b5f36d50c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -193,6 +193,7 @@ crypto = [ "bs58", "util", + "zkas", ] wallet = [ From c32b56d5adc489eed923b769d1c67a0d6bfdc50f Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 08:59:58 +0100 Subject: [PATCH 06/38] map: removed redudant async function on call in start() --- bin/map/src/main.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index aa0657c82..e777d85f2 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -78,6 +78,10 @@ async fn main() -> Result<()> { terminal.clear()?; + // let current_state = get_current_state.await; + // let mut app = app.lock(); + // app.current_state = current_state; + // let app = Arc::new(Mutex::new(App::new())); let app = App::new(); let nthreads = num_cpus::get(); @@ -104,10 +108,7 @@ async fn main() -> Result<()> { async fn start(ex: Arc>, app: App) -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); - ex.spawn(async { - let _ = poll(client, app).await; - }) - .detach(); + ex.spawn(poll(client, app)).detach(); Ok(()) } @@ -134,7 +135,7 @@ async fn update(app: App, reply: Value) -> Result<()> { id: node1["id"].to_string(), connections: node1["connections"].as_u64().unwrap() as usize, is_active: node2["is_active"].as_bool().unwrap(), - last_message: "message".to_string(), + last_message: node3["message"].to_string(), }, //NodeInfo { // id: node2["id"].to_string(), @@ -177,6 +178,9 @@ async fn run_app(terminal: &mut Terminal, mut app: App) -> io::Re app.node_info.index = 0; + // acquire the mutex + // let mut app = app.lock(); + loop { terminal.draw(|f| ui::ui(f, &mut app))?; From a04660c7b32cbd9d26311c39cb20fa2ed79bdd17 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 09:00:57 +0100 Subject: [PATCH 07/38] map: prepared App for non-initialized values --- bin/map/src/app.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/bin/map/src/app.rs b/bin/map/src/app.rs index c9dc34ac0..c79797d7c 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/app.rs @@ -16,7 +16,6 @@ pub struct App { impl App { pub fn new() -> App { - // append to vector of node info let infos = vec![ NodeInfo { id: "0385048034sodisofjhosd1111q3434".to_string(), @@ -61,6 +60,12 @@ impl App { ]; let node_list = NodeIdList::new(ids); + + //let infos = Vec::new(); + //let ids = Vec::new(); + + //let node_info = NodeInfoView::new(infos); + //let node_list = NodeIdList::new(ids); App { node_list, node_info } } @@ -69,12 +74,20 @@ impl App { // Timer::after(dur).await; //} - pub async fn update(self, node_vec: Vec) -> App { - let node_info = NodeInfoView::new(node_vec.clone()); + pub async fn update(mut self, node_vec: Vec) -> App { + let ids = vec![node_vec[0].id.clone()]; - let ids = vec![node_vec[0].id.clone(), node_vec[1].id.clone(), node_vec[2].id.clone()]; + for id in ids { + self.node_list.node_id.push(id); + } + + let node_list = self.node_list; + + for info in node_vec { + self.node_info.infos.push(info); + } + let node_info = self.node_info; - let node_list = NodeIdList::new(ids); App { node_list, node_info } } } From fc6dad30ad2760b5685d33e81fd08ea764e6242d Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 09:13:59 +0100 Subject: [PATCH 08/38] map: wrapped App in Mutex --- bin/map/src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index e777d85f2..13b173904 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -8,7 +8,7 @@ use darkfi::{ util::async_util, }; -use async_std::sync::Arc; +use async_std::sync::{Arc, Mutex}; use easy_parallel::Parallel; use log::debug; use serde_json::{json, Value}; @@ -81,8 +81,8 @@ async fn main() -> Result<()> { // let current_state = get_current_state.await; // let mut app = app.lock(); // app.current_state = current_state; - // let app = Arc::new(Mutex::new(App::new())); - let app = App::new(); + let app = Arc::new(Mutex::new(App::new())); + //let app = App::new(); let nthreads = num_cpus::get(); let (signal, shutdown) = async_channel::unbounded::<()>(); @@ -95,8 +95,8 @@ async fn main() -> Result<()> { // Run the main future on the current thread. .finish(|| { smol::future::block_on(async move { - start(ex2.clone(), app.clone()).await?; - run_app(&mut terminal, app).await?; + start(ex2.clone(), app.lock().await.clone()).await?; + run_app(&mut terminal, app.lock().await.clone()).await?; drop(signal); Ok::<(), darkfi::Error>(()) }) @@ -128,7 +128,7 @@ async fn update(app: App, reply: Value) -> Result<()> { let node1 = &nodes[0]; let node2 = &nodes[1]; - let _node3 = &nodes[2]; + let node3 = &nodes[2]; let infos = vec![ NodeInfo { From 0f07155950a9473cf9f6cff3927e8e4ad24d36b2 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 10:32:14 +0100 Subject: [PATCH 09/38] map: cleaned up start and poll functions --- bin/map/src/main.rs | 76 +++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 51 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 13b173904..5667ea014 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -95,7 +95,7 @@ async fn main() -> Result<()> { // Run the main future on the current thread. .finish(|| { smol::future::block_on(async move { - start(ex2.clone(), app.lock().await.clone()).await?; + listen(ex2.clone(), app.lock().await.clone()).await?; run_app(&mut terminal, app.lock().await.clone()).await?; drop(signal); Ok::<(), darkfi::Error>(()) @@ -105,7 +105,7 @@ async fn main() -> Result<()> { result } -async fn start(ex: Arc>, app: App) -> Result<()> { +async fn listen(ex: Arc>, app: App) -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); ex.spawn(poll(client, app)).detach(); @@ -116,59 +116,33 @@ async fn start(ex: Arc>, app: App) -> Result<()> { async fn poll(client: Map, app: App) -> Result<()> { loop { let reply = client.get_info().await?; - update(app.clone(), reply).await?; + + if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() { + let nodes = reply.as_object().unwrap().get("nodes").unwrap(); + + let node1 = &nodes[0]; + let node2 = &nodes[1]; + let node3 = &nodes[2]; + + let infos = vec![ + NodeInfo { + id: node1["id"].to_string(), + connections: node1["connections"].as_u64().unwrap() as usize, + is_active: node2["is_active"].as_bool().unwrap(), + last_message: node3["message"].to_string(), + }, + ]; + + app.clone().update(infos).await; + } else { + // TODO: error handling + println!("Reply is an error"); + } + async_util::sleep(1).await; } } -async fn update(app: App, reply: Value) -> Result<()> { - if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() { - //let args = params.as_array(); - let nodes = reply.as_object().unwrap().get("nodes").unwrap(); - - let node1 = &nodes[0]; - let node2 = &nodes[1]; - let node3 = &nodes[2]; - - let infos = vec![ - NodeInfo { - id: node1["id"].to_string(), - connections: node1["connections"].as_u64().unwrap() as usize, - is_active: node2["is_active"].as_bool().unwrap(), - last_message: node3["message"].to_string(), - }, - //NodeInfo { - // id: node2["id"].to_string(), - // connections: node2["connections"].as_u64().unwrap() as usize, - // is_active: node2["is_active"].as_bool().unwrap(), - // last_message: node2["message"].to_string(), - //}, - //NodeInfo { - // id: node3["id"].to_string(), - // connections: node3["connections"].as_u64().unwrap() as usize, - // is_active: node3["is_active"].as_bool().unwrap(), - // last_message: node3["message"].to_string(), - //}, - ]; - - //app.node_info( - //let node_info = NodeInfoView::new(infos.clone()); - - //let ids = vec![node1["id"].to_string(), node2["id"].to_string(), node3["id"].to_string()]; - - // mutex - app.update(infos).await; - //let node_list = NodeIdList::new(ids); - //println!("{}", test); - // do something - } else { - // TODO: error handling - println!("Reply is an error"); - } - - Ok(()) -} - async fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<()> { let mut asi = async_stdin(); From 533ee4a63cba8c5eac2ed292f8bfaf30e904655f Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 10:59:19 +0100 Subject: [PATCH 10/38] map: renamed NodeInfoView and NodeIdList to InfoPanel and IdPanel --- bin/map/src/app.rs | 8 ++++---- bin/map/src/list.rs | 8 ++++---- bin/map/src/main.rs | 4 ---- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/bin/map/src/app.rs b/bin/map/src/app.rs index c79797d7c..5070fc98b 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/app.rs @@ -1,5 +1,5 @@ use crate::{ - list::NodeIdList, + list::IdPanel, node_info::{NodeInfo, NodeInfoView}, }; //use smol::Timer; @@ -10,7 +10,7 @@ use crate::{ // arc reference #[derive(Clone)] pub struct App { - pub node_list: NodeIdList, + pub node_list: IdPanel, pub node_info: NodeInfoView, } @@ -59,13 +59,13 @@ impl App { infos[4].id.clone(), ]; - let node_list = NodeIdList::new(ids); + let node_list = IdPanel::new(ids); //let infos = Vec::new(); //let ids = Vec::new(); //let node_info = NodeInfoView::new(infos); - //let node_list = NodeIdList::new(ids); + //let node_list = IdPanel::new(ids); App { node_list, node_info } } diff --git a/bin/map/src/list.rs b/bin/map/src/list.rs index 44ab29321..8cd42dceb 100644 --- a/bin/map/src/list.rs +++ b/bin/map/src/list.rs @@ -2,14 +2,14 @@ use tui::widgets::ListState; #[derive(Clone)] -pub struct NodeIdList { +pub struct IdPanel { pub state: ListState, pub node_id: Vec, } -impl NodeIdList { - pub fn new(node_id: Vec) -> NodeIdList { - NodeIdList { state: ListState::default(), node_id } +impl IdPanel { + pub fn new(node_id: Vec) -> IdPanel { + IdPanel { state: ListState::default(), node_id } } pub fn next(&mut self) { diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 5667ea014..e93faf4da 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -78,11 +78,7 @@ async fn main() -> Result<()> { terminal.clear()?; - // let current_state = get_current_state.await; - // let mut app = app.lock(); - // app.current_state = current_state; let app = Arc::new(Mutex::new(App::new())); - //let app = App::new(); let nthreads = num_cpus::get(); let (signal, shutdown) = async_channel::unbounded::<()>(); From 23a692378c46e3722aeba8bc1e365a3b10fab3c0 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 11:37:09 +0100 Subject: [PATCH 11/38] map: renamed Info/IdPanel to Info/IdList --- bin/map/src/app.rs | 16 ++++++++-------- bin/map/src/list.rs | 8 ++++---- bin/map/src/node_info.rs | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/bin/map/src/app.rs b/bin/map/src/app.rs index 5070fc98b..c9dae905a 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/app.rs @@ -1,6 +1,6 @@ use crate::{ - list::IdPanel, - node_info::{NodeInfo, NodeInfoView}, + list::IdList, + node_info::{NodeInfo, InfoList}, }; //use smol::Timer; //use std::{collections::HashMap, time::Duration}; @@ -10,8 +10,8 @@ use crate::{ // arc reference #[derive(Clone)] pub struct App { - pub node_list: IdPanel, - pub node_info: NodeInfoView, + pub node_list: IdList, + pub node_info: InfoList, } impl App { @@ -49,7 +49,7 @@ impl App { }, ]; - let node_info = NodeInfoView::new(infos.clone()); + let node_info = InfoList::new(infos.clone()); let ids = vec![ infos[0].id.clone(), @@ -59,13 +59,13 @@ impl App { infos[4].id.clone(), ]; - let node_list = IdPanel::new(ids); + let node_list = IdList::new(ids); //let infos = Vec::new(); //let ids = Vec::new(); - //let node_info = NodeInfoView::new(infos); - //let node_list = IdPanel::new(ids); + //let node_info = InfoList::new(infos); + //let node_list = IdList::new(ids); App { node_list, node_info } } diff --git a/bin/map/src/list.rs b/bin/map/src/list.rs index 8cd42dceb..ee3cf5146 100644 --- a/bin/map/src/list.rs +++ b/bin/map/src/list.rs @@ -2,14 +2,14 @@ use tui::widgets::ListState; #[derive(Clone)] -pub struct IdPanel { +pub struct IdList { pub state: ListState, pub node_id: Vec, } -impl IdPanel { - pub fn new(node_id: Vec) -> IdPanel { - IdPanel { state: ListState::default(), node_id } +impl IdList { + pub fn new(node_id: Vec) -> IdList { + IdList { state: ListState::default(), node_id } } pub fn next(&mut self) { diff --git a/bin/map/src/node_info.rs b/bin/map/src/node_info.rs index f6a1f7137..0d94dcc3e 100644 --- a/bin/map/src/node_info.rs +++ b/bin/map/src/node_info.rs @@ -1,14 +1,14 @@ #[derive(Clone)] -pub struct NodeInfoView { +pub struct InfoList { pub index: usize, pub infos: Vec, } -impl NodeInfoView { - pub fn new(infos: Vec) -> NodeInfoView { +impl InfoList { + pub fn new(infos: Vec) -> InfoList { let index = 0; - NodeInfoView { index, infos } + InfoList { index, infos } } pub fn next(&mut self) { From a52f61cb64b644f6385c84809c688fca54eebf82 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 11:40:29 +0100 Subject: [PATCH 12/38] map: renamed id/info list files --- bin/map/src/{list.rs => id_list.rs} | 0 bin/map/src/{node_info.rs => info_list.rs} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename bin/map/src/{list.rs => id_list.rs} (100%) rename bin/map/src/{node_info.rs => info_list.rs} (100%) diff --git a/bin/map/src/list.rs b/bin/map/src/id_list.rs similarity index 100% rename from bin/map/src/list.rs rename to bin/map/src/id_list.rs diff --git a/bin/map/src/node_info.rs b/bin/map/src/info_list.rs similarity index 100% rename from bin/map/src/node_info.rs rename to bin/map/src/info_list.rs From 391a22593c82958e75081aa6ec26d6a22e06d48a Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 11:50:38 +0100 Subject: [PATCH 13/38] map: fixed import errors --- bin/map/src/app.rs | 4 ++-- bin/map/src/lib.rs | 4 ++-- bin/map/src/main.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/map/src/app.rs b/bin/map/src/app.rs index c9dae905a..ba0d2f6f8 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/app.rs @@ -1,6 +1,6 @@ use crate::{ - list::IdList, - node_info::{NodeInfo, InfoList}, + id_list::IdList, + info_list::{NodeInfo, InfoList}, }; //use smol::Timer; //use std::{collections::HashMap, time::Duration}; diff --git a/bin/map/src/lib.rs b/bin/map/src/lib.rs index d05977f03..a983a4482 100644 --- a/bin/map/src/lib.rs +++ b/bin/map/src/lib.rs @@ -1,6 +1,6 @@ pub mod app; -pub mod list; -pub mod node_info; +pub mod id_list; +pub mod info_list; pub mod types; pub mod ui; diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index e93faf4da..f24433eb6 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -20,7 +20,7 @@ use tui::{ Terminal, }; -use map::{node_info::NodeInfo, ui, App}; +use map::{info_list::NodeInfo, ui, App}; struct Map { url: String, From 32c44c0439a7b005c1cd8d434b124cb41c17be42 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 11:53:23 +0100 Subject: [PATCH 14/38] map: changed node info/id to list across project --- bin/map/src/app.rs | 24 ++++++++++++------------ bin/map/src/main.rs | 12 ++++++------ bin/map/src/ui.rs | 14 +++++++------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/bin/map/src/app.rs b/bin/map/src/app.rs index ba0d2f6f8..ac778f9ff 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/app.rs @@ -10,8 +10,8 @@ use crate::{ // arc reference #[derive(Clone)] pub struct App { - pub node_list: IdList, - pub node_info: InfoList, + pub id_list: IdList, + pub info_list: InfoList, } impl App { @@ -49,7 +49,7 @@ impl App { }, ]; - let node_info = InfoList::new(infos.clone()); + let info_list = InfoList::new(infos.clone()); let ids = vec![ infos[0].id.clone(), @@ -59,14 +59,14 @@ impl App { infos[4].id.clone(), ]; - let node_list = IdList::new(ids); + let id_list = IdList::new(ids); //let infos = Vec::new(); //let ids = Vec::new(); - //let node_info = InfoList::new(infos); - //let node_list = IdList::new(ids); - App { node_list, node_info } + //let info_list = InfoList::new(infos); + //let id_list = IdList::new(ids); + App { id_list, info_list } } // TODO: implement this @@ -78,17 +78,17 @@ impl App { let ids = vec![node_vec[0].id.clone()]; for id in ids { - self.node_list.node_id.push(id); + self.id_list.node_id.push(id); } - let node_list = self.node_list; + let id_list = self.id_list; for info in node_vec { - self.node_info.infos.push(info); + self.info_list.infos.push(info); } - let node_info = self.node_info; + let info_list = self.info_list; - App { node_list, node_info } + App { id_list, info_list } } } diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index f24433eb6..f800aa7b6 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -144,9 +144,9 @@ async fn run_app(terminal: &mut Terminal, mut app: App) -> io::Re terminal.clear()?; - app.node_list.state.select(Some(0)); + app.id_list.state.select(Some(0)); - app.node_info.index = 0; + app.info_list.index = 0; // acquire the mutex // let mut app = app.lock(); @@ -161,12 +161,12 @@ async fn run_app(terminal: &mut Terminal, mut app: App) -> io::Re return Ok(()) } Key::Char('j') => { - app.node_list.next(); - app.node_info.next(); + app.id_list.next(); + app.info_list.next(); } Key::Char('k') => { - app.node_list.previous(); - app.node_info.previous(); + app.id_list.previous(); + app.info_list.previous(); } _ => (), } diff --git a/bin/map/src/ui.rs b/bin/map/src/ui.rs index 83829e9ff..d9d3b0d65 100644 --- a/bin/map/src/ui.rs +++ b/bin/map/src/ui.rs @@ -16,7 +16,7 @@ pub fn ui(f: &mut Frame, app: &mut App) { .split(f.size()); let nodes: Vec = app - .node_list + .id_list .node_id .iter() .map(|id| { @@ -29,18 +29,18 @@ pub fn ui(f: &mut Frame, app: &mut App) { .block(Block::default().borders(Borders::ALL)) .highlight_style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD)); - f.render_stateful_widget(nodes, slice[0], &mut app.node_list.state); + f.render_stateful_widget(nodes, slice[0], &mut app.id_list.state); - let index = app.node_info.index; + let index = app.info_list.index; render_info(app, f, index, slice); } fn render_info(app: &mut App, f: &mut Frame, index: usize, slice: Vec) { - let id = &app.node_info.infos[index].id; - let connections = app.node_info.infos[index].connections; - let is_active = app.node_info.infos[index].is_active; - let message = &app.node_info.infos[index].last_message; + let id = &app.info_list.infos[index].id; + let connections = app.info_list.infos[index].connections; + let is_active = app.info_list.infos[index].is_active; + let message = &app.info_list.infos[index].last_message; let span = vec![ Spans::from(format!("NodeId: {}", id)), Spans::from(format!("Number of connections: {}", connections)), From 7c84dcb8f8adce2c92daac9344113b20ebd975ed Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 2 Feb 2022 12:00:34 +0100 Subject: [PATCH 15/38] map: made NodeInfo seperate crate --- bin/map/src/app.rs | 5 +---- bin/map/src/info_list.rs | 28 ++-------------------------- bin/map/src/lib.rs | 1 + bin/map/src/main.rs | 16 +++++++--------- 4 files changed, 11 insertions(+), 39 deletions(-) diff --git a/bin/map/src/app.rs b/bin/map/src/app.rs index ac778f9ff..1133ecd71 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/app.rs @@ -1,7 +1,4 @@ -use crate::{ - id_list::IdList, - info_list::{NodeInfo, InfoList}, -}; +use crate::{id_list::IdList, info_list::InfoList, node_info::NodeInfo}; //use smol::Timer; //use std::{collections::HashMap, time::Duration}; diff --git a/bin/map/src/info_list.rs b/bin/map/src/info_list.rs index 0d94dcc3e..90d7ecf3d 100644 --- a/bin/map/src/info_list.rs +++ b/bin/map/src/info_list.rs @@ -1,3 +1,5 @@ +use crate::node_info::NodeInfo; + #[derive(Clone)] pub struct InfoList { pub index: usize, @@ -23,29 +25,3 @@ impl InfoList { } } } - -#[derive(Clone)] -pub struct NodeInfo { - pub id: String, - pub connections: usize, - pub is_active: bool, - pub last_message: String, -} - -impl NodeInfo { - pub fn new() -> NodeInfo { - let connections = 0; - let is_active = false; - NodeInfo { id: String::new(), connections, is_active, last_message: String::new() } - } -} - -impl Default for NodeInfo { - fn default() -> Self { - Self::new() - } -} - -//pub async fn add_seen(&self, id: u32) { -// self.privmsg_ids.lock().await.insert(id); -//} diff --git a/bin/map/src/lib.rs b/bin/map/src/lib.rs index a983a4482..8bb213488 100644 --- a/bin/map/src/lib.rs +++ b/bin/map/src/lib.rs @@ -1,6 +1,7 @@ pub mod app; pub mod id_list; pub mod info_list; +pub mod node_info; pub mod types; pub mod ui; diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index f800aa7b6..e7df376c0 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -20,7 +20,7 @@ use tui::{ Terminal, }; -use map::{info_list::NodeInfo, ui, App}; +use map::{node_info::NodeInfo, ui, App}; struct Map { url: String, @@ -120,14 +120,12 @@ async fn poll(client: Map, app: App) -> Result<()> { let node2 = &nodes[1]; let node3 = &nodes[2]; - let infos = vec![ - NodeInfo { - id: node1["id"].to_string(), - connections: node1["connections"].as_u64().unwrap() as usize, - is_active: node2["is_active"].as_bool().unwrap(), - last_message: node3["message"].to_string(), - }, - ]; + let infos = vec![NodeInfo { + id: node1["id"].to_string(), + connections: node1["connections"].as_u64().unwrap() as usize, + is_active: node2["is_active"].as_bool().unwrap(), + last_message: node3["message"].to_string(), + }]; app.clone().update(infos).await; } else { From d1d184c0eaa00c4ab9a8acb4e4f2e8fdf4665fd0 Mon Sep 17 00:00:00 2001 From: narodnik Date: Thu, 3 Feb 2022 08:09:58 +0100 Subject: [PATCH 16/38] Add a protocol registry which properly handles creating the protocols in a suspended sleep state, then activating them once the channel is operating. --- bin/ircd/src/main.rs | 2 +- src/net/channel.rs | 1 + src/net/p2p.rs | 18 ++++++-- src/net/protocols/mod.rs | 14 ++++++ src/net/protocols/protocol_address.rs | 61 +++++++++++++++++++------- src/net/protocols/protocol_base.rs | 10 +++++ src/net/protocols/protocol_ping.rs | 43 ++++++++++++------ src/net/protocols/protocol_registry.rs | 48 ++++++++++++++++++++ src/net/sessions/inbound_session.rs | 5 +-- src/net/sessions/manual_session.rs | 5 +-- src/net/sessions/outbound_session.rs | 5 +-- src/net/sessions/seed_session.rs | 4 +- src/net/sessions/session.rs | 23 +++++++++- 13 files changed, 194 insertions(+), 45 deletions(-) create mode 100644 src/net/protocols/protocol_base.rs create mode 100644 src/net/protocols/protocol_registry.rs diff --git a/bin/ircd/src/main.rs b/bin/ircd/src/main.rs index fbc3c8dab..e4240b917 100644 --- a/bin/ircd/src/main.rs +++ b/bin/ircd/src/main.rs @@ -145,7 +145,7 @@ async fn start(executor: Arc>, options: ProgramOptions) -> Result<( let seen_privmsg_ids = SeenPrivMsgIds::new(); - let p2p = net::P2p::new(options.network_settings); + let p2p = net::P2p::new(options.network_settings).await; // Performs seed session p2p.clone().start(executor.clone()).await?; // Actual main p2p session diff --git a/src/net/channel.rs b/src/net/channel.rs index 532aa9934..dc53501fb 100644 --- a/src/net/channel.rs +++ b/src/net/channel.rs @@ -18,6 +18,7 @@ use crate::{ net::{ message_subscriber::{MessageSubscription, MessageSubsystem}, messages, + protocols::{ProtocolBase, ProtocolBasePtr}, }, system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription}, }; diff --git a/src/net/p2p.rs b/src/net/p2p.rs index b8eedb733..686240ef4 100644 --- a/src/net/p2p.rs +++ b/src/net/p2p.rs @@ -11,6 +11,7 @@ use crate::{ error::{Error, Result}, net::{ messages::Message, + protocols::{register_default_protocols, ProtocolRegistry}, sessions::{InboundSession, ManualSession, OutboundSession, SeedSession}, Channel, ChannelPtr, Hosts, HostsPtr, Settings, SettingsPtr, }, @@ -32,21 +33,28 @@ pub struct P2p { // Used both internally and externally stop_subscriber: SubscriberPtr, hosts: HostsPtr, + protocol_registry: ProtocolRegistry, settings: SettingsPtr, } impl P2p { /// Create a new p2p network. - pub fn new(settings: Settings) -> Arc { + pub async fn new(settings: Settings) -> Arc { let settings = Arc::new(settings); - Arc::new(Self { + + let self_ = Arc::new(Self { pending: Mutex::new(HashSet::new()), channels: Mutex::new(HashMap::new()), channel_subscriber: Subscriber::new(), stop_subscriber: Subscriber::new(), hosts: Hosts::new(), + protocol_registry: ProtocolRegistry::new(), settings, - }) + }); + + register_default_protocols(self_.clone()).await; + + self_ } /// Invoke startup and seeding sequence. Call from constructing thread. @@ -140,6 +148,10 @@ impl P2p { self.hosts.clone() } + pub fn protocol_registry(&self) -> &ProtocolRegistry { + &self.protocol_registry + } + /// Subscribe to a channel. pub async fn subscribe_channel(&self) -> Subscription> { self.channel_subscriber.clone().subscribe().await diff --git a/src/net/protocols/mod.rs b/src/net/protocols/mod.rs index d395ff560..0d7308a44 100644 --- a/src/net/protocols/mod.rs +++ b/src/net/protocols/mod.rs @@ -46,8 +46,22 @@ pub mod protocol_seed; /// other node and sending the version acknowledgement. pub mod protocol_version; +pub mod protocol_base; +pub mod protocol_registry; + pub use protocol_address::ProtocolAddress; pub use protocol_jobs_manager::{ProtocolJobsManager, ProtocolJobsManagerPtr}; pub use protocol_ping::ProtocolPing; pub use protocol_seed::ProtocolSeed; pub use protocol_version::ProtocolVersion; + +pub use protocol_base::{ProtocolBase, ProtocolBasePtr}; +pub use protocol_registry::ProtocolRegistry; + +use crate::net::P2pPtr; + +pub async fn register_default_protocols(p2p: P2pPtr) { + let registry = p2p.protocol_registry(); + registry.register(ProtocolPing::new2).await; + registry.register(ProtocolAddress::new2).await; +} diff --git a/src/net/protocols/protocol_address.rs b/src/net/protocols/protocol_address.rs index 2dc8bd386..b6de1fd15 100644 --- a/src/net/protocols/protocol_address.rs +++ b/src/net/protocols/protocol_address.rs @@ -1,14 +1,15 @@ -use log::*; +use log::{error, debug}; use smol::Executor; use std::sync::Arc; +use async_trait::async_trait; use crate::{ error::Result, net::{ message_subscriber::MessageSubscription, messages, - protocols::{ProtocolJobsManager, ProtocolJobsManagerPtr}, - ChannelPtr, HostsPtr, + protocols::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr}, + ChannelPtr, HostsPtr, P2pPtr, }, }; @@ -48,19 +49,30 @@ impl ProtocolAddress { }) } - /// Starts the address protocol. Runs receive address and get address - /// protocols on the protocol task manager. Then sends get-address - /// message. - pub async fn start(self: Arc, executor: Arc>) { - debug!(target: "net", "ProtocolAddress::start() [START]"); - self.jobsman.clone().start(executor.clone()); - self.jobsman.clone().spawn(self.clone().handle_receive_addrs(), executor.clone()).await; - self.jobsman.clone().spawn(self.clone().handle_receive_get_addrs(), executor).await; + pub async fn new2(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { + let hosts = p2p.hosts(); - // Send get_address message. - let get_addrs = messages::GetAddrsMessage {}; - let _ = self.channel.clone().send(get_addrs).await; - debug!(target: "net", "ProtocolAddress::start() [END]"); + // Creates a subscription to address message. + let addrs_sub = channel + .clone() + .subscribe_msg::() + .await + .expect("Missing addrs dispatcher!"); + + // Creates a subscription to get-address message. + let get_addrs_sub = channel + .clone() + .subscribe_msg::() + .await + .expect("Missing getaddrs dispatcher!"); + + Arc::new(Self { + channel: channel.clone(), + addrs_sub, + get_addrs_sub, + hosts, + jobsman: ProtocolJobsManager::new("ProtocolAddress", channel), + }) } /// Handles receiving the address message. Loops to continually recieve @@ -107,3 +119,22 @@ impl ProtocolAddress { } } } + +#[async_trait] +impl ProtocolBase for ProtocolAddress { + /// Starts the address protocol. Runs receive address and get address + /// protocols on the protocol task manager. Then sends get-address + /// message. + async fn start(self: Arc, executor: Arc>) { + debug!(target: "net", "ProtocolAddress::start() [START]"); + self.jobsman.clone().start(executor.clone()); + self.jobsman.clone().spawn(self.clone().handle_receive_addrs(), executor.clone()).await; + self.jobsman.clone().spawn(self.clone().handle_receive_get_addrs(), executor).await; + + // Send get_address message. + let get_addrs = messages::GetAddrsMessage {}; + let _ = self.channel.clone().send(get_addrs).await; + debug!(target: "net", "ProtocolAddress::start() [END]"); + } +} + diff --git a/src/net/protocols/protocol_base.rs b/src/net/protocols/protocol_base.rs new file mode 100644 index 000000000..bb83bc3fd --- /dev/null +++ b/src/net/protocols/protocol_base.rs @@ -0,0 +1,10 @@ +use std::sync::Arc; +use async_trait::async_trait; +use smol::Executor; + +pub type ProtocolBasePtr = Arc; + +#[async_trait] +pub trait ProtocolBase { + async fn start(self: Arc, executor: Arc>); +} diff --git a/src/net/protocols/protocol_ping.rs b/src/net/protocols/protocol_ping.rs index aad16efbe..9af624586 100644 --- a/src/net/protocols/protocol_ping.rs +++ b/src/net/protocols/protocol_ping.rs @@ -1,14 +1,15 @@ -use log::*; +use log::{error, debug}; use rand::Rng; use smol::Executor; use std::{sync::Arc, time::Instant}; +use async_trait::async_trait; use crate::{ error::{Error, Result}, net::{ messages, - protocols::{ProtocolJobsManager, ProtocolJobsManagerPtr}, - ChannelPtr, SettingsPtr, + protocols::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr}, + ChannelPtr, P2pPtr, SettingsPtr, }, util::sleep, }; @@ -22,7 +23,9 @@ pub struct ProtocolPing { impl ProtocolPing { /// Create a new ping-pong protocol. - pub fn new(channel: ChannelPtr, settings: SettingsPtr) -> Arc { + pub fn new(channel: ChannelPtr, p2p: P2pPtr) -> Arc { + let settings = p2p.settings(); + Arc::new(Self { channel: channel.clone(), settings, @@ -30,15 +33,14 @@ impl ProtocolPing { }) } - /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the - /// protocol task manager, then queues the reply. Sends out a ping and - /// waits for pong reply. Waits for ping and replies with a pong. - pub async fn start(self: Arc, executor: Arc>) { - debug!(target: "net", "ProtocolPing::start() [START]"); - self.jobsman.clone().start(executor.clone()); - self.jobsman.clone().spawn(self.clone().run_ping_pong(), executor.clone()).await; - self.jobsman.clone().spawn(self.reply_to_ping(), executor).await; - debug!(target: "net", "ProtocolPing::start() [END]"); + pub async fn new2(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { + let settings = p2p.settings(); + + Arc::new(Self { + channel: channel.clone(), + settings, + jobsman: ProtocolJobsManager::new("ProtocolPing", channel), + }) } /// Runs ping-pong protocol. Creates a subscription to pong, then starts a @@ -110,3 +112,18 @@ impl ProtocolPing { rng.gen() } } + +#[async_trait] +impl ProtocolBase for ProtocolPing { + /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the + /// protocol task manager, then queues the reply. Sends out a ping and + /// waits for pong reply. Waits for ping and replies with a pong. + async fn start(self: Arc, executor: Arc>) { + debug!(target: "net", "ProtocolPing::start() [START]"); + self.jobsman.clone().start(executor.clone()); + self.jobsman.clone().spawn(self.clone().run_ping_pong(), executor.clone()).await; + self.jobsman.clone().spawn(self.reply_to_ping(), executor).await; + debug!(target: "net", "ProtocolPing::start() [END]"); + } +} + diff --git a/src/net/protocols/protocol_registry.rs b/src/net/protocols/protocol_registry.rs new file mode 100644 index 000000000..c294a6df6 --- /dev/null +++ b/src/net/protocols/protocol_registry.rs @@ -0,0 +1,48 @@ +use async_std::sync::Mutex; +use futures::future::BoxFuture; +use std::future::Future; + +use super::protocol_base::ProtocolBase; +use std::sync::Arc; + +use super::protocol_base::ProtocolBasePtr; +use crate::net::{ChannelPtr, P2pPtr}; + +type Constructor = + Box BoxFuture<'static, + Arc + > + Send + Sync>; + +pub struct ProtocolRegistry { + protocol_constructors: Mutex>, +} + +impl ProtocolRegistry { + pub fn new() -> Self { + Self { + protocol_constructors: Mutex::new(Vec::new()), + } + } + + // add_protocol()? + pub async fn register(&self, constructor: C) + where + C: 'static + Fn(ChannelPtr, P2pPtr) -> F + Send + Sync, + F: 'static + Future> + Send, + { + let constructor = move |channel, p2p| { + Box::pin(constructor(channel, p2p)) as BoxFuture<'static, ProtocolBasePtr> + }; + self.protocol_constructors.lock().await.push(Box::new(constructor)); + } + + pub async fn attach(&self, channel: ChannelPtr, p2p: P2pPtr) -> Vec { + let mut protocols: Vec> = Vec::new(); + for construct in self.protocol_constructors.lock().await.iter() { + let protocol: Arc = + construct(channel.clone(), p2p.clone()).await; + protocols.push(protocol) + } + protocols + } +} diff --git a/src/net/sessions/inbound_session.rs b/src/net/sessions/inbound_session.rs index 9f977e22a..4102a068e 100644 --- a/src/net/sessions/inbound_session.rs +++ b/src/net/sessions/inbound_session.rs @@ -8,7 +8,7 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocols::{ProtocolAddress, ProtocolPing}, + protocols::{ProtocolAddress, ProtocolPing, ProtocolBase}, sessions::Session, Acceptor, AcceptorPtr, ChannelPtr, P2p, }, @@ -105,10 +105,9 @@ impl InboundSession { channel: ChannelPtr, executor: Arc>, ) -> Result<()> { - let settings = self.p2p().settings().clone(); let hosts = self.p2p().hosts(); - let protocol_ping = ProtocolPing::new(channel.clone(), settings.clone()); + let protocol_ping = ProtocolPing::new(channel.clone(), self.p2p()); let protocol_addr = ProtocolAddress::new(channel, hosts).await; protocol_ping.start(executor.clone()).await; diff --git a/src/net/sessions/manual_session.rs b/src/net/sessions/manual_session.rs index e625340dd..31237000f 100644 --- a/src/net/sessions/manual_session.rs +++ b/src/net/sessions/manual_session.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocols::{ProtocolAddress, ProtocolPing}, + protocols::{ProtocolAddress, ProtocolPing, ProtocolBase}, sessions::Session, ChannelPtr, Connector, P2p, }, @@ -118,10 +118,9 @@ impl ManualSession { channel: ChannelPtr, executor: Arc>, ) -> Result<()> { - let settings = self.p2p().settings().clone(); let hosts = self.p2p().hosts(); - let protocol_ping = ProtocolPing::new(channel.clone(), settings.clone()); + let protocol_ping = ProtocolPing::new(channel.clone(), self.p2p()); let protocol_addr = ProtocolAddress::new(channel, hosts).await; protocol_ping.start(executor.clone()).await; diff --git a/src/net/sessions/outbound_session.rs b/src/net/sessions/outbound_session.rs index c7561c5af..b17423982 100644 --- a/src/net/sessions/outbound_session.rs +++ b/src/net/sessions/outbound_session.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocols::{ProtocolAddress, ProtocolPing}, + protocols::{ProtocolAddress, ProtocolPing, ProtocolBase}, sessions::Session, ChannelPtr, Connector, P2p, }, @@ -157,10 +157,9 @@ impl OutboundSession { channel: ChannelPtr, executor: Arc>, ) -> Result<()> { - let settings = self.p2p().settings().clone(); let hosts = self.p2p().hosts(); - let protocol_ping = ProtocolPing::new(channel.clone(), settings.clone()); + let protocol_ping = ProtocolPing::new(channel.clone(), self.p2p()); let protocol_addr = ProtocolAddress::new(channel, hosts).await; protocol_ping.start(executor.clone()).await; diff --git a/src/net/sessions/seed_session.rs b/src/net/sessions/seed_session.rs index f54b5153e..5e7939fd0 100644 --- a/src/net/sessions/seed_session.rs +++ b/src/net/sessions/seed_session.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocols::{ProtocolPing, ProtocolSeed}, + protocols::{ProtocolPing, ProtocolSeed, ProtocolBase}, sessions::Session, ChannelPtr, Connector, HostsPtr, P2p, SettingsPtr, }, @@ -120,7 +120,7 @@ impl SeedSession { settings: SettingsPtr, executor: Arc>, ) -> Result<()> { - let protocol_ping = ProtocolPing::new(channel.clone(), settings.clone()); + let protocol_ping = ProtocolPing::new(channel.clone(), self.p2p()); protocol_ping.start(executor.clone()).await; let protocol_seed = ProtocolSeed::new(channel.clone(), hosts, settings.clone()); diff --git a/src/net/sessions/session.rs b/src/net/sessions/session.rs index 01ae07c14..48624410d 100644 --- a/src/net/sessions/session.rs +++ b/src/net/sessions/session.rs @@ -37,15 +37,34 @@ pub trait Session: Sync { ) -> Result<()> { debug!(target: "net", "Session::register_channel() [START]"); + // Protocols should all be initialized but not started + // We do this so that the protocols can begin receiving and buffering messages + // while the handshake protocol is ongoing. + // They are currently in sleep mode. + let p2p = self.p2p(); + let protocols = p2p.protocol_registry().attach(channel.clone(), p2p.clone()).await; + + // Perform the handshake protocol let protocol_version = ProtocolVersion::new(channel.clone(), self.p2p().settings()).await; let handshake_task = self.perform_handshake_protocols(protocol_version, channel.clone(), executor.clone()); - // start channel - channel.start(executor); + // Switch on the channel + channel.start(executor.clone()); + // Wait for handshake to finish. handshake_task.await?; + // Now the channel is ready + + // Now start all the protocols + // They are responsible for managing their own lifetimes and + // correctly self destructing when the channel ends. + for protocol in protocols { + // Activate protocol + protocol.start(executor.clone()).await; + } + debug!(target: "net", "Session::register_channel() [END]"); Ok(()) } From eb23a3cda5624814857cd44b27f46ab07474ab83 Mon Sep 17 00:00:00 2001 From: narodnik Date: Thu, 3 Feb 2022 08:23:15 +0100 Subject: [PATCH 17/38] remove unecessary plurals: protocols -> protocol, sessions -> session, messages -> message --- src/net/channel.rs | 28 +++++++++---------- src/net/{messages.rs => message.rs} | 0 src/net/message_subscriber.rs | 2 +- src/net/mod.rs | 10 +++---- src/net/p2p.rs | 8 +++--- src/net/{protocols => protocol}/mod.rs | 0 .../protocol_address.rs | 20 ++++++------- .../{protocols => protocol}/protocol_base.rs | 0 .../protocol_jobs_manager.rs | 0 .../{protocols => protocol}/protocol_ping.rs | 12 ++++---- .../protocol_registry.rs | 0 .../{protocols => protocol}/protocol_seed.rs | 8 +++--- .../protocol_version.rs | 14 +++++----- .../{sessions => session}/inbound_session.rs | 4 +-- .../{sessions => session}/manual_session.rs | 4 +-- src/net/{sessions => session}/mod.rs | 0 .../{sessions => session}/outbound_session.rs | 4 +-- src/net/{sessions => session}/seed_session.rs | 4 +-- src/net/{sessions => session}/session.rs | 2 +- 19 files changed, 60 insertions(+), 60 deletions(-) rename src/net/{messages.rs => message.rs} (100%) rename src/net/{protocols => protocol}/mod.rs (100%) rename src/net/{protocols => protocol}/protocol_address.rs (88%) rename src/net/{protocols => protocol}/protocol_base.rs (100%) rename src/net/{protocols => protocol}/protocol_jobs_manager.rs (100%) rename src/net/{protocols => protocol}/protocol_ping.rs (92%) rename src/net/{protocols => protocol}/protocol_registry.rs (100%) rename src/net/{protocols => protocol}/protocol_seed.rs (89%) rename src/net/{protocols => protocol}/protocol_version.rs (88%) rename src/net/{sessions => session}/inbound_session.rs (97%) rename src/net/{sessions => session}/manual_session.rs (97%) rename src/net/{sessions => session}/mod.rs (100%) rename src/net/{sessions => session}/outbound_session.rs (98%) rename src/net/{sessions => session}/seed_session.rs (97%) rename src/net/{sessions => session}/session.rs (98%) diff --git a/src/net/channel.rs b/src/net/channel.rs index dc53501fb..64f28ce78 100644 --- a/src/net/channel.rs +++ b/src/net/channel.rs @@ -17,8 +17,8 @@ use crate::{ error::{Error, Result}, net::{ message_subscriber::{MessageSubscription, MessageSubsystem}, - messages, - protocols::{ProtocolBase, ProtocolBasePtr}, + message, + protocol::{ProtocolBase, ProtocolBasePtr}, }, system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription}, }; @@ -108,7 +108,7 @@ impl Channel { /// Sends a message across a channel. Calls function 'send_message' that /// creates a new payload and sends it over the TCP connection as a /// packet. Returns an error if something goes wrong. - pub async fn send(&self, message: M) -> Result<()> { + pub async fn send(&self, message: M) -> Result<()> { debug!(target: "net", "Channel::send() [START, command={:?}, address={}]", M::name(), @@ -139,17 +139,17 @@ impl Channel { /// it. Then creates a message packet- the base type of the network- and /// copies the payload into it. Then we send the packet over the TCP /// stream. - async fn send_message(&self, message: M) -> Result<()> { + async fn send_message(&self, message: M) -> Result<()> { let mut payload = Vec::new(); message.encode(&mut payload)?; - let packet = messages::Packet { command: String::from(M::name()), payload }; + let packet = message::Packet { command: String::from(M::name()), payload }; let stream = &mut *self.writer.lock().await; - messages::send_packet(stream, packet).await + message::send_packet(stream, packet).await } /// Subscribe to a messages on the message subsystem. - pub async fn subscribe_msg(&self) -> Result> { + pub async fn subscribe_msg(&self) -> Result> { debug!(target: "net", "Channel::subscribe_msg() [START, command={:?}, address={}]", M::name(), @@ -179,12 +179,12 @@ impl Channel { /// Perform network handshake for message subsystem dispatchers. async fn setup_dispatchers(message_subsystem: &MessageSubsystem) { - message_subsystem.add_dispatch::().await; - message_subsystem.add_dispatch::().await; - message_subsystem.add_dispatch::().await; - message_subsystem.add_dispatch::().await; - message_subsystem.add_dispatch::().await; - message_subsystem.add_dispatch::().await; + message_subsystem.add_dispatch::().await; + message_subsystem.add_dispatch::().await; + message_subsystem.add_dispatch::().await; + message_subsystem.add_dispatch::().await; + message_subsystem.add_dispatch::().await; + message_subsystem.add_dispatch::().await; } /// Convenience function that returns the Message Subsystem. @@ -203,7 +203,7 @@ impl Channel { let reader = &mut *self.reader.lock().await; loop { - let packet = match messages::read_packet(reader).await { + let packet = match message::read_packet(reader).await { Ok(packet) => packet, Err(err) => { if Self::is_eof_error(err.clone()) { diff --git a/src/net/messages.rs b/src/net/message.rs similarity index 100% rename from src/net/messages.rs rename to src/net/message.rs diff --git a/src/net/message_subscriber.rs b/src/net/message_subscriber.rs index b14356348..a9db4d52e 100644 --- a/src/net/message_subscriber.rs +++ b/src/net/message_subscriber.rs @@ -5,7 +5,7 @@ use rand::Rng; use std::{any::Any, collections::HashMap, io, io::Cursor, sync::Arc}; use crate::{ - net::messages::Message, + net::message::Message, util::serial::{Decodable, Encodable}, Error, Result, }; diff --git a/src/net/mod.rs b/src/net/mod.rs index 20203b654..e58a7c070 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -46,7 +46,7 @@ pub mod message_subscriber; /// /// Implements a type called Packet which is the base message type. Packets are /// converted into messages and passed to an event loop. -pub mod messages; +pub mod message; /// P2P provides all core functionality to interact with the peer-to-peer /// network. @@ -72,7 +72,7 @@ pub mod p2p; /// /// Protocol submodule also implements a jobs manager than handles the /// asynchronous execution of the protocols. -pub mod protocols; +pub mod protocol; /// Defines the interaction between nodes during a connection. Consists of an /// inbound session, which describes how to set up an incoming connection, and @@ -80,7 +80,7 @@ pub mod protocols; /// describes the seed session, which is the type of connection used when a node /// connects to the network for the first time. Implements the session trait /// which describes the common functions across all sessions. -pub mod sessions; +pub mod session; /// Network configuration settings. pub mod settings; @@ -90,7 +90,7 @@ pub use channel::{Channel, ChannelPtr}; pub use connector::Connector; pub use hosts::{Hosts, HostsPtr}; pub use message_subscriber::MessageSubscription; -pub use messages::Message; +pub use message::Message; pub use p2p::{P2p, P2pPtr}; -pub use protocols::{ProtocolJobsManager, ProtocolJobsManagerPtr}; +pub use protocol::{ProtocolJobsManager, ProtocolJobsManagerPtr}; pub use settings::{Settings, SettingsPtr}; diff --git a/src/net/p2p.rs b/src/net/p2p.rs index 686240ef4..9da49b09a 100644 --- a/src/net/p2p.rs +++ b/src/net/p2p.rs @@ -1,6 +1,6 @@ use async_executor::Executor; use async_std::sync::Mutex; -use log::*; +use log::debug; use std::{ collections::{HashMap, HashSet}, net::SocketAddr, @@ -10,9 +10,9 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - messages::Message, - protocols::{register_default_protocols, ProtocolRegistry}, - sessions::{InboundSession, ManualSession, OutboundSession, SeedSession}, + message::Message, + protocol::{register_default_protocols, ProtocolRegistry}, + session::{InboundSession, ManualSession, OutboundSession, SeedSession}, Channel, ChannelPtr, Hosts, HostsPtr, Settings, SettingsPtr, }, system::{Subscriber, SubscriberPtr, Subscription}, diff --git a/src/net/protocols/mod.rs b/src/net/protocol/mod.rs similarity index 100% rename from src/net/protocols/mod.rs rename to src/net/protocol/mod.rs diff --git a/src/net/protocols/protocol_address.rs b/src/net/protocol/protocol_address.rs similarity index 88% rename from src/net/protocols/protocol_address.rs rename to src/net/protocol/protocol_address.rs index b6de1fd15..bca99f32c 100644 --- a/src/net/protocols/protocol_address.rs +++ b/src/net/protocol/protocol_address.rs @@ -7,8 +7,8 @@ use crate::{ error::Result, net::{ message_subscriber::MessageSubscription, - messages, - protocols::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr}, + message, + protocol::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr}, ChannelPtr, HostsPtr, P2pPtr, }, }; @@ -16,8 +16,8 @@ use crate::{ /// Defines address and get-address messages. pub struct ProtocolAddress { channel: ChannelPtr, - addrs_sub: MessageSubscription, - get_addrs_sub: MessageSubscription, + addrs_sub: MessageSubscription, + get_addrs_sub: MessageSubscription, hosts: HostsPtr, jobsman: ProtocolJobsManagerPtr, } @@ -29,14 +29,14 @@ impl ProtocolAddress { // Creates a subscription to address message. let addrs_sub = channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing addrs dispatcher!"); // Creates a subscription to get-address message. let get_addrs_sub = channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing getaddrs dispatcher!"); @@ -55,14 +55,14 @@ impl ProtocolAddress { // Creates a subscription to address message. let addrs_sub = channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing addrs dispatcher!"); // Creates a subscription to get-address message. let get_addrs_sub = channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing getaddrs dispatcher!"); @@ -113,7 +113,7 @@ impl ProtocolAddress { addrs.len() ); // Creates an address messages containing host address. - let addrs_msg = messages::AddrsMessage { addrs }; + let addrs_msg = message::AddrsMessage { addrs }; // Sends the address message across the channel. self.channel.clone().send(addrs_msg).await?; } @@ -132,7 +132,7 @@ impl ProtocolBase for ProtocolAddress { self.jobsman.clone().spawn(self.clone().handle_receive_get_addrs(), executor).await; // Send get_address message. - let get_addrs = messages::GetAddrsMessage {}; + let get_addrs = message::GetAddrsMessage {}; let _ = self.channel.clone().send(get_addrs).await; debug!(target: "net", "ProtocolAddress::start() [END]"); } diff --git a/src/net/protocols/protocol_base.rs b/src/net/protocol/protocol_base.rs similarity index 100% rename from src/net/protocols/protocol_base.rs rename to src/net/protocol/protocol_base.rs diff --git a/src/net/protocols/protocol_jobs_manager.rs b/src/net/protocol/protocol_jobs_manager.rs similarity index 100% rename from src/net/protocols/protocol_jobs_manager.rs rename to src/net/protocol/protocol_jobs_manager.rs diff --git a/src/net/protocols/protocol_ping.rs b/src/net/protocol/protocol_ping.rs similarity index 92% rename from src/net/protocols/protocol_ping.rs rename to src/net/protocol/protocol_ping.rs index 9af624586..b20b9c969 100644 --- a/src/net/protocols/protocol_ping.rs +++ b/src/net/protocol/protocol_ping.rs @@ -7,8 +7,8 @@ use async_trait::async_trait; use crate::{ error::{Error, Result}, net::{ - messages, - protocols::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr}, + message, + protocol::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr}, ChannelPtr, P2pPtr, SettingsPtr, }, util::sleep, @@ -53,7 +53,7 @@ impl ProtocolPing { let pong_sub = self .channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing pong dispatcher!"); @@ -65,7 +65,7 @@ impl ProtocolPing { let nonce = Self::random_nonce(); // Send ping message. - let ping = messages::PingMessage { nonce }; + let ping = message::PingMessage { nonce }; self.channel.clone().send(ping).await?; debug!(target: "net", "ProtocolPing::run_ping_pong() send Ping message"); // Start the timer for ping timer. @@ -91,7 +91,7 @@ impl ProtocolPing { let ping_sub = self .channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing ping dispatcher!"); @@ -101,7 +101,7 @@ impl ProtocolPing { debug!(target: "net", "ProtocolPing::reply_to_ping() received Ping message"); // Send pong message. - let pong = messages::PongMessage { nonce: ping.nonce }; + let pong = message::PongMessage { nonce: ping.nonce }; self.channel.clone().send(pong).await?; debug!(target: "net", "ProtocolPing::reply_to_ping() sent Pong reply"); } diff --git a/src/net/protocols/protocol_registry.rs b/src/net/protocol/protocol_registry.rs similarity index 100% rename from src/net/protocols/protocol_registry.rs rename to src/net/protocol/protocol_registry.rs diff --git a/src/net/protocols/protocol_seed.rs b/src/net/protocol/protocol_seed.rs similarity index 89% rename from src/net/protocols/protocol_seed.rs rename to src/net/protocol/protocol_seed.rs index 6eca990f7..e27e267c0 100644 --- a/src/net/protocols/protocol_seed.rs +++ b/src/net/protocol/protocol_seed.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use crate::{ error::Result, - net::{messages, ChannelPtr, HostsPtr, SettingsPtr}, + net::{message, ChannelPtr, HostsPtr, SettingsPtr}, }; /// Implements the seed protocol. @@ -29,7 +29,7 @@ impl ProtocolSeed { let addr_sub = self .channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing addrs dispatcher!"); @@ -37,7 +37,7 @@ impl ProtocolSeed { self.send_self_address().await?; // Send get address message. - let get_addr = messages::GetAddrsMessage {}; + let get_addr = message::GetAddrsMessage {}; self.channel.clone().send(get_addr).await?; // Receive addresses. @@ -56,7 +56,7 @@ impl ProtocolSeed { match self.settings.external_addr { Some(addr) => { debug!(target: "net", "ProtocolSeed::send_own_address() addr={}", addr); - let addr = messages::AddrsMessage { addrs: vec![addr] }; + let addr = message::AddrsMessage { addrs: vec![addr] }; Ok(self.channel.clone().send(addr).await?) } // Do nothing if external address is not configured diff --git a/src/net/protocols/protocol_version.rs b/src/net/protocol/protocol_version.rs similarity index 88% rename from src/net/protocols/protocol_version.rs rename to src/net/protocol/protocol_version.rs index e63de528f..11bccee49 100644 --- a/src/net/protocols/protocol_version.rs +++ b/src/net/protocol/protocol_version.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use crate::{ error::{Error, Result}, - net::{message_subscriber::MessageSubscription, messages, ChannelPtr, SettingsPtr}, + net::{message_subscriber::MessageSubscription, message, ChannelPtr, SettingsPtr}, util::sleep, }; @@ -13,8 +13,8 @@ use crate::{ /// of a connection. pub struct ProtocolVersion { channel: ChannelPtr, - version_sub: MessageSubscription, - verack_sub: MessageSubscription, + version_sub: MessageSubscription, + verack_sub: MessageSubscription, settings: SettingsPtr, } @@ -26,14 +26,14 @@ impl ProtocolVersion { // Creates a version subscription. let version_sub = channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing version dispatcher!"); // Creates a version acknowledgement subscription. let verack_sub = channel .clone() - .subscribe_msg::() + .subscribe_msg::() .await .expect("Missing verack dispatcher!"); @@ -71,7 +71,7 @@ impl ProtocolVersion { /// Send version info and wait for version acknowledgement. async fn send_version(self: Arc) -> Result<()> { debug!(target: "net", "ProtocolVersion::send_version() [START]"); - let version = messages::VersionMessage {}; + let version = message::VersionMessage {}; self.channel.clone().send(version).await?; // Wait for version acknowledgement @@ -90,7 +90,7 @@ impl ProtocolVersion { // Check the message is OK // Send version acknowledgement - let verack = messages::VerackMessage {}; + let verack = message::VerackMessage {}; self.channel.clone().send(verack).await?; debug!(target: "net", "ProtocolVersion::recv_version() [END]"); diff --git a/src/net/sessions/inbound_session.rs b/src/net/session/inbound_session.rs similarity index 97% rename from src/net/sessions/inbound_session.rs rename to src/net/session/inbound_session.rs index 4102a068e..533cc5d28 100644 --- a/src/net/sessions/inbound_session.rs +++ b/src/net/session/inbound_session.rs @@ -8,8 +8,8 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocols::{ProtocolAddress, ProtocolPing, ProtocolBase}, - sessions::Session, + protocol::{ProtocolAddress, ProtocolPing, ProtocolBase}, + session::Session, Acceptor, AcceptorPtr, ChannelPtr, P2p, }, system::{StoppableTask, StoppableTaskPtr}, diff --git a/src/net/sessions/manual_session.rs b/src/net/session/manual_session.rs similarity index 97% rename from src/net/sessions/manual_session.rs rename to src/net/session/manual_session.rs index 31237000f..ed812a163 100644 --- a/src/net/sessions/manual_session.rs +++ b/src/net/session/manual_session.rs @@ -9,8 +9,8 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocols::{ProtocolAddress, ProtocolPing, ProtocolBase}, - sessions::Session, + protocol::{ProtocolAddress, ProtocolPing, ProtocolBase}, + session::Session, ChannelPtr, Connector, P2p, }, system::{StoppableTask, StoppableTaskPtr}, diff --git a/src/net/sessions/mod.rs b/src/net/session/mod.rs similarity index 100% rename from src/net/sessions/mod.rs rename to src/net/session/mod.rs diff --git a/src/net/sessions/outbound_session.rs b/src/net/session/outbound_session.rs similarity index 98% rename from src/net/sessions/outbound_session.rs rename to src/net/session/outbound_session.rs index b17423982..d2a5c8537 100644 --- a/src/net/sessions/outbound_session.rs +++ b/src/net/session/outbound_session.rs @@ -9,8 +9,8 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocols::{ProtocolAddress, ProtocolPing, ProtocolBase}, - sessions::Session, + protocol::{ProtocolAddress, ProtocolPing, ProtocolBase}, + session::Session, ChannelPtr, Connector, P2p, }, system::{StoppableTask, StoppableTaskPtr}, diff --git a/src/net/sessions/seed_session.rs b/src/net/session/seed_session.rs similarity index 97% rename from src/net/sessions/seed_session.rs rename to src/net/session/seed_session.rs index 5e7939fd0..a4a7fe2b1 100644 --- a/src/net/sessions/seed_session.rs +++ b/src/net/session/seed_session.rs @@ -9,8 +9,8 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocols::{ProtocolPing, ProtocolSeed, ProtocolBase}, - sessions::Session, + protocol::{ProtocolPing, ProtocolSeed, ProtocolBase}, + session::Session, ChannelPtr, Connector, HostsPtr, P2p, SettingsPtr, }, util::sleep, diff --git a/src/net/sessions/session.rs b/src/net/session/session.rs similarity index 98% rename from src/net/sessions/session.rs rename to src/net/session/session.rs index 48624410d..5f6b615f1 100644 --- a/src/net/sessions/session.rs +++ b/src/net/session/session.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use crate::{ error::Result, - net::{p2p::P2pPtr, protocols::ProtocolVersion, ChannelPtr}, + net::{p2p::P2pPtr, protocol::ProtocolVersion, ChannelPtr}, }; /// Removes channel from the list of connected channels when a stop signal is From 6fb1304c000891cff0d9999b2d6a8663aa1cf338 Mon Sep 17 00:00:00 2001 From: narodnik Date: Thu, 3 Feb 2022 08:26:55 +0100 Subject: [PATCH 18/38] cargo fmt --- bin/map/src/main.rs | 14 ++++++-------- src/net/channel.rs | 2 +- src/net/mod.rs | 2 +- src/net/protocol/protocol_address.rs | 7 +++---- src/net/protocol/protocol_base.rs | 2 +- src/net/protocol/protocol_ping.rs | 5 ++--- src/net/protocol/protocol_registry.rs | 13 ++++++------- src/net/protocol/protocol_version.rs | 2 +- src/net/session/inbound_session.rs | 2 +- src/net/session/manual_session.rs | 2 +- src/net/session/outbound_session.rs | 2 +- src/net/session/seed_session.rs | 2 +- 12 files changed, 25 insertions(+), 30 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index e93faf4da..5b0be4624 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -120,14 +120,12 @@ async fn poll(client: Map, app: App) -> Result<()> { let node2 = &nodes[1]; let node3 = &nodes[2]; - let infos = vec![ - NodeInfo { - id: node1["id"].to_string(), - connections: node1["connections"].as_u64().unwrap() as usize, - is_active: node2["is_active"].as_bool().unwrap(), - last_message: node3["message"].to_string(), - }, - ]; + let infos = vec![NodeInfo { + id: node1["id"].to_string(), + connections: node1["connections"].as_u64().unwrap() as usize, + is_active: node2["is_active"].as_bool().unwrap(), + last_message: node3["message"].to_string(), + }]; app.clone().update(infos).await; } else { diff --git a/src/net/channel.rs b/src/net/channel.rs index 64f28ce78..fbbc1eb08 100644 --- a/src/net/channel.rs +++ b/src/net/channel.rs @@ -16,8 +16,8 @@ use std::sync::{ use crate::{ error::{Error, Result}, net::{ - message_subscriber::{MessageSubscription, MessageSubsystem}, message, + message_subscriber::{MessageSubscription, MessageSubsystem}, protocol::{ProtocolBase, ProtocolBasePtr}, }, system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription}, diff --git a/src/net/mod.rs b/src/net/mod.rs index e58a7c070..37b1d2d7a 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -89,8 +89,8 @@ pub use acceptor::{Acceptor, AcceptorPtr}; pub use channel::{Channel, ChannelPtr}; pub use connector::Connector; pub use hosts::{Hosts, HostsPtr}; -pub use message_subscriber::MessageSubscription; pub use message::Message; +pub use message_subscriber::MessageSubscription; pub use p2p::{P2p, P2pPtr}; pub use protocol::{ProtocolJobsManager, ProtocolJobsManagerPtr}; pub use settings::{Settings, SettingsPtr}; diff --git a/src/net/protocol/protocol_address.rs b/src/net/protocol/protocol_address.rs index bca99f32c..d1e91f60a 100644 --- a/src/net/protocol/protocol_address.rs +++ b/src/net/protocol/protocol_address.rs @@ -1,13 +1,13 @@ -use log::{error, debug}; +use async_trait::async_trait; +use log::{debug, error}; use smol::Executor; use std::sync::Arc; -use async_trait::async_trait; use crate::{ error::Result, net::{ - message_subscriber::MessageSubscription, message, + message_subscriber::MessageSubscription, protocol::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr}, ChannelPtr, HostsPtr, P2pPtr, }, @@ -137,4 +137,3 @@ impl ProtocolBase for ProtocolAddress { debug!(target: "net", "ProtocolAddress::start() [END]"); } } - diff --git a/src/net/protocol/protocol_base.rs b/src/net/protocol/protocol_base.rs index bb83bc3fd..990b5a677 100644 --- a/src/net/protocol/protocol_base.rs +++ b/src/net/protocol/protocol_base.rs @@ -1,6 +1,6 @@ -use std::sync::Arc; use async_trait::async_trait; use smol::Executor; +use std::sync::Arc; pub type ProtocolBasePtr = Arc; diff --git a/src/net/protocol/protocol_ping.rs b/src/net/protocol/protocol_ping.rs index b20b9c969..3a15b7315 100644 --- a/src/net/protocol/protocol_ping.rs +++ b/src/net/protocol/protocol_ping.rs @@ -1,8 +1,8 @@ -use log::{error, debug}; +use async_trait::async_trait; +use log::{debug, error}; use rand::Rng; use smol::Executor; use std::{sync::Arc, time::Instant}; -use async_trait::async_trait; use crate::{ error::{Error, Result}, @@ -126,4 +126,3 @@ impl ProtocolBase for ProtocolPing { debug!(target: "net", "ProtocolPing::start() [END]"); } } - diff --git a/src/net/protocol/protocol_registry.rs b/src/net/protocol/protocol_registry.rs index c294a6df6..57ad86724 100644 --- a/src/net/protocol/protocol_registry.rs +++ b/src/net/protocol/protocol_registry.rs @@ -8,10 +8,11 @@ use std::sync::Arc; use super::protocol_base::ProtocolBasePtr; use crate::net::{ChannelPtr, P2pPtr}; -type Constructor = - Box BoxFuture<'static, - Arc - > + Send + Sync>; +type Constructor = Box< + dyn Fn(ChannelPtr, P2pPtr) -> BoxFuture<'static, Arc> + + Send + + Sync, +>; pub struct ProtocolRegistry { protocol_constructors: Mutex>, @@ -19,9 +20,7 @@ pub struct ProtocolRegistry { impl ProtocolRegistry { pub fn new() -> Self { - Self { - protocol_constructors: Mutex::new(Vec::new()), - } + Self { protocol_constructors: Mutex::new(Vec::new()) } } // add_protocol()? diff --git a/src/net/protocol/protocol_version.rs b/src/net/protocol/protocol_version.rs index 11bccee49..81151bf6f 100644 --- a/src/net/protocol/protocol_version.rs +++ b/src/net/protocol/protocol_version.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use crate::{ error::{Error, Result}, - net::{message_subscriber::MessageSubscription, message, ChannelPtr, SettingsPtr}, + net::{message, message_subscriber::MessageSubscription, ChannelPtr, SettingsPtr}, util::sleep, }; diff --git a/src/net/session/inbound_session.rs b/src/net/session/inbound_session.rs index 533cc5d28..946d07db1 100644 --- a/src/net/session/inbound_session.rs +++ b/src/net/session/inbound_session.rs @@ -8,7 +8,7 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocol::{ProtocolAddress, ProtocolPing, ProtocolBase}, + protocol::{ProtocolAddress, ProtocolBase, ProtocolPing}, session::Session, Acceptor, AcceptorPtr, ChannelPtr, P2p, }, diff --git a/src/net/session/manual_session.rs b/src/net/session/manual_session.rs index ed812a163..713ebe751 100644 --- a/src/net/session/manual_session.rs +++ b/src/net/session/manual_session.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocol::{ProtocolAddress, ProtocolPing, ProtocolBase}, + protocol::{ProtocolAddress, ProtocolBase, ProtocolPing}, session::Session, ChannelPtr, Connector, P2p, }, diff --git a/src/net/session/outbound_session.rs b/src/net/session/outbound_session.rs index d2a5c8537..654e228d5 100644 --- a/src/net/session/outbound_session.rs +++ b/src/net/session/outbound_session.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocol::{ProtocolAddress, ProtocolPing, ProtocolBase}, + protocol::{ProtocolAddress, ProtocolBase, ProtocolPing}, session::Session, ChannelPtr, Connector, P2p, }, diff --git a/src/net/session/seed_session.rs b/src/net/session/seed_session.rs index a4a7fe2b1..96daf4566 100644 --- a/src/net/session/seed_session.rs +++ b/src/net/session/seed_session.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ error::{Error, Result}, net::{ - protocol::{ProtocolPing, ProtocolSeed, ProtocolBase}, + protocol::{ProtocolBase, ProtocolPing, ProtocolSeed}, session::Session, ChannelPtr, Connector, HostsPtr, P2p, SettingsPtr, }, From f4026bf8a204ba6bb7c8fa71293c169533a8ab0c Mon Sep 17 00:00:00 2001 From: narodnik Date: Sat, 5 Feb 2022 07:46:40 +0100 Subject: [PATCH 19/38] remove uneeded 'static declarations --- src/net/protocol/protocol_registry.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/net/protocol/protocol_registry.rs b/src/net/protocol/protocol_registry.rs index 57ad86724..d1f8f00f3 100644 --- a/src/net/protocol/protocol_registry.rs +++ b/src/net/protocol/protocol_registry.rs @@ -5,11 +5,13 @@ use std::future::Future; use super::protocol_base::ProtocolBase; use std::sync::Arc; -use super::protocol_base::ProtocolBasePtr; +//use super::protocol_base::ProtocolBasePtr; use crate::net::{ChannelPtr, P2pPtr}; +type ProtocolBasePtr = Arc; + type Constructor = Box< - dyn Fn(ChannelPtr, P2pPtr) -> BoxFuture<'static, Arc> + dyn Fn(ChannelPtr, P2pPtr) -> BoxFuture<'static, Arc> + Send + Sync, >; @@ -27,18 +29,18 @@ impl ProtocolRegistry { pub async fn register(&self, constructor: C) where C: 'static + Fn(ChannelPtr, P2pPtr) -> F + Send + Sync, - F: 'static + Future> + Send, + F: 'static + Future> + Send, { let constructor = move |channel, p2p| { - Box::pin(constructor(channel, p2p)) as BoxFuture<'static, ProtocolBasePtr> + Box::pin(constructor(channel, p2p)) as BoxFuture<'static, Arc> }; self.protocol_constructors.lock().await.push(Box::new(constructor)); } - pub async fn attach(&self, channel: ChannelPtr, p2p: P2pPtr) -> Vec { - let mut protocols: Vec> = Vec::new(); + pub async fn attach(&self, channel: ChannelPtr, p2p: P2pPtr) -> Vec> { + let mut protocols: Vec> = Vec::new(); for construct in self.protocol_constructors.lock().await.iter() { - let protocol: Arc = + let protocol: Arc = construct(channel.clone(), p2p.clone()).await; protocols.push(protocol) } From 220e4c9adc5dcabd2b552cdcc1d42ef83db61068 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 5 Feb 2022 09:52:41 +0100 Subject: [PATCH 20/38] map: wrapped App in a Mutex and initialized App objects in main() --- bin/map/src/app.rs | 59 ++++------------------------ bin/map/src/id_list.rs | 3 +- bin/map/src/info_list.rs | 5 +-- bin/map/src/main.rs | 84 ++++++++++++++++++++++++++++++---------- bin/map/src/ui.rs | 27 ++++++++----- 5 files changed, 92 insertions(+), 86 deletions(-) diff --git a/bin/map/src/app.rs b/bin/map/src/app.rs index 1133ecd71..c718572b4 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/app.rs @@ -1,63 +1,18 @@ use crate::{id_list::IdList, info_list::InfoList, node_info::NodeInfo}; +//use async_std::sync::Mutex; //use smol::Timer; //use std::{collections::HashMap, time::Duration}; // make a structure to be able to modify and read them // protect using a mutex // arc reference -#[derive(Clone)] pub struct App { pub id_list: IdList, pub info_list: InfoList, } impl App { - pub fn new() -> App { - let infos = vec![ - NodeInfo { - id: "0385048034sodisofjhosd1111q3434".to_string(), - connections: 10, - is_active: true, - last_message: "hey how are you?".to_string(), - }, - NodeInfo { - id: "09w30we9wsnfksdfkdjflsjkdfjdfsd".to_string(), - connections: 5, - is_active: false, - last_message: "lmao".to_string(), - }, - NodeInfo { - id: "038043325alsdlasjfrsdfsdfsdjsdf".to_string(), - connections: 7, - is_active: true, - last_message: "gm".to_string(), - }, - NodeInfo { - id: "04985034953ldflsdjflsdjflsdjfii".to_string(), - connections: 2, - is_active: true, - last_message: "hihi".to_string(), - }, - NodeInfo { - id: "09850249352asdjapsdikalskasdkas".to_string(), - connections: 10, - is_active: true, - last_message: "wtf".to_string(), - }, - ]; - - let info_list = InfoList::new(infos.clone()); - - let ids = vec![ - infos[0].id.clone(), - infos[1].id.clone(), - infos[2].id.clone(), - infos[3].id.clone(), - infos[4].id.clone(), - ]; - - let id_list = IdList::new(ids); - + pub fn new(id_list: IdList, info_list: InfoList) -> App { //let infos = Vec::new(); //let ids = Vec::new(); @@ -89,8 +44,8 @@ impl App { } } -impl Default for App { - fn default() -> Self { - Self::new() - } -} +//impl Default for App { +// fn default() -> Self { +// Self::new() +// } +//} diff --git a/bin/map/src/id_list.rs b/bin/map/src/id_list.rs index ee3cf5146..43333d233 100644 --- a/bin/map/src/id_list.rs +++ b/bin/map/src/id_list.rs @@ -1,4 +1,5 @@ -//use std::collections::HashMap; +//TODO: made node_id into a hashset across project +//use std::collections::HashSet use tui::widgets::ListState; #[derive(Clone)] diff --git a/bin/map/src/info_list.rs b/bin/map/src/info_list.rs index 90d7ecf3d..4b99ece6b 100644 --- a/bin/map/src/info_list.rs +++ b/bin/map/src/info_list.rs @@ -1,6 +1,5 @@ use crate::node_info::NodeInfo; -#[derive(Clone)] pub struct InfoList { pub index: usize, pub infos: Vec, @@ -13,11 +12,11 @@ impl InfoList { InfoList { index, infos } } - pub fn next(&mut self) { + pub async fn next(&mut self) { self.index = (self.index + 1) % self.infos.len(); } - pub fn previous(&mut self) { + pub async fn previous(&mut self) { if self.index > 0 { self.index -= 1; } else { diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index e7df376c0..e8fb212a8 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -1,7 +1,3 @@ -// select each connection and show log of traffic -// use rpc to get some info from the ircd network -// ircd::logger keeps track of network info -// map rpc polls logger for info about nodes, etc use darkfi::{ error::{Error, Result}, rpc::{jsonrpc, jsonrpc::JsonResult}, @@ -20,7 +16,7 @@ use tui::{ Terminal, }; -use map::{node_info::NodeInfo, ui, App}; +use map::{id_list::IdList, info_list::InfoList, node_info::NodeInfo, ui, App}; struct Map { url: String, @@ -78,7 +74,52 @@ async fn main() -> Result<()> { terminal.clear()?; - let app = Arc::new(Mutex::new(App::new())); + let infos = vec![ + NodeInfo { + id: "0385048034sodisofjhosd1111q3434".to_string(), + connections: 10, + is_active: true, + last_message: "hey how are you?".to_string(), + }, + NodeInfo { + id: "09w30we9wsnfksdfkdjflsjkdfjdfsd".to_string(), + connections: 5, + is_active: false, + last_message: "lmao".to_string(), + }, + NodeInfo { + id: "038043325alsdlasjfrsdfsdfsdjsdf".to_string(), + connections: 7, + is_active: true, + last_message: "gm".to_string(), + }, + NodeInfo { + id: "04985034953ldflsdjflsdjflsdjfii".to_string(), + connections: 2, + is_active: true, + last_message: "hihi".to_string(), + }, + NodeInfo { + id: "09850249352asdjapsdikalskasdkas".to_string(), + connections: 10, + is_active: true, + last_message: "wtf".to_string(), + }, + ]; + + let info_list = InfoList::new(infos.clone()); + + let ids = vec![ + infos[0].id.clone(), + infos[1].id.clone(), + infos[2].id.clone(), + infos[3].id.clone(), + infos[4].id.clone(), + ]; + + let id_list = IdList::new(ids); + + let app = Arc::new(Mutex::new(App::new(id_list, info_list))); let nthreads = num_cpus::get(); let (signal, shutdown) = async_channel::unbounded::<()>(); @@ -91,8 +132,8 @@ async fn main() -> Result<()> { // Run the main future on the current thread. .finish(|| { smol::future::block_on(async move { - listen(ex2.clone(), app.lock().await.clone()).await?; - run_app(&mut terminal, app.lock().await.clone()).await?; + listen(ex2.clone(), app.clone()).await?; + run_app(&mut terminal, app.clone()).await?; drop(signal); Ok::<(), darkfi::Error>(()) }) @@ -101,7 +142,7 @@ async fn main() -> Result<()> { result } -async fn listen(ex: Arc>, app: App) -> Result<()> { +async fn listen(ex: Arc>, app: Arc>) -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); ex.spawn(poll(client, app)).detach(); @@ -109,7 +150,7 @@ async fn listen(ex: Arc>, app: App) -> Result<()> { Ok(()) } -async fn poll(client: Map, app: App) -> Result<()> { +async fn poll(client: Map, app: Arc>) -> Result<()> { loop { let reply = client.get_info().await?; @@ -127,7 +168,7 @@ async fn poll(client: Map, app: App) -> Result<()> { last_message: node3["message"].to_string(), }]; - app.clone().update(infos).await; + //app.update(infos).await; } else { // TODO: error handling println!("Reply is an error"); @@ -137,34 +178,35 @@ async fn poll(client: Map, app: App) -> Result<()> { } } -async fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<()> { +async fn run_app(terminal: &mut Terminal, app: Arc>) -> io::Result<()> { let mut asi = async_stdin(); terminal.clear()?; - app.id_list.state.select(Some(0)); + app.lock().await.id_list.state.select(Some(0)); - app.info_list.index = 0; + app.lock().await.info_list.index = 0; // acquire the mutex // let mut app = app.lock(); loop { - terminal.draw(|f| ui::ui(f, &mut app))?; - + terminal.draw(|f| { + ui::ui(f, app.clone()); + })?; for k in asi.by_ref().keys() { match k.unwrap() { Key::Char('q') => { terminal.clear()?; - return Ok(()) + return Ok(()); } Key::Char('j') => { - app.id_list.next(); - app.info_list.next(); + app.lock().await.id_list.next(); + app.lock().await.info_list.next(); } Key::Char('k') => { - app.id_list.previous(); - app.info_list.previous(); + app.lock().await.id_list.previous(); + app.lock().await.info_list.previous(); } _ => (), } diff --git a/bin/map/src/ui.rs b/bin/map/src/ui.rs index d9d3b0d65..8bf058b68 100644 --- a/bin/map/src/ui.rs +++ b/bin/map/src/ui.rs @@ -1,4 +1,5 @@ use crate::app::App; +use async_std::sync::{Arc, Mutex}; use tui::{ backend::Backend, layout::{Constraint, Direction, Layout, Rect}, @@ -8,7 +9,7 @@ use tui::{ Frame, }; -pub fn ui(f: &mut Frame, app: &mut App) { +pub async fn ui(f: &mut Frame<'_, B>, app: Arc>) { let slice = Layout::default() .direction(Direction::Horizontal) .margin(2) @@ -16,6 +17,8 @@ pub fn ui(f: &mut Frame, app: &mut App) { .split(f.size()); let nodes: Vec = app + .lock() + .await .id_list .node_id .iter() @@ -29,18 +32,24 @@ pub fn ui(f: &mut Frame, app: &mut App) { .block(Block::default().borders(Borders::ALL)) .highlight_style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD)); - f.render_stateful_widget(nodes, slice[0], &mut app.id_list.state); + f.render_stateful_widget(nodes, slice[0], &mut app.lock().await.id_list.state); - let index = app.info_list.index; + let index = app.lock().await.info_list.index; - render_info(app, f, index, slice); + render_info(app, f, index, slice).await; } -fn render_info(app: &mut App, f: &mut Frame, index: usize, slice: Vec) { - let id = &app.info_list.infos[index].id; - let connections = app.info_list.infos[index].connections; - let is_active = app.info_list.infos[index].is_active; - let message = &app.info_list.infos[index].last_message; +async fn render_info( + app: Arc>, + f: &mut Frame<'_, B>, + index: usize, + slice: Vec, +) { + let info = &app.lock().await.info_list.infos; + let id = &info[index].id; + let connections = info[index].connections; + let is_active = info[index].is_active; + let message = &info[index].last_message; let span = vec![ Spans::from(format!("NodeId: {}", id)), Spans::from(format!("Number of connections: {}", connections)), From a9a714aa73cc8f0c5f450af183477fb2c698ae44 Mon Sep 17 00:00:00 2001 From: narodnik Date: Sat, 5 Feb 2022 09:56:34 +0100 Subject: [PATCH 21/38] net: when registering protocols, use a bitwise selector to specify sessions it will be activated for. registry.register(SESSION_INBOUND | SESSION_OUTBOUND, ProtocolFoo::new) The constructors accept a channel and p2p instance. For an example of passing additional arguments, you have to use an async closure: let foo2 = foo.clone(); // registry.register( !net::SESSION_SEED, move |channel, p2p| { let foo = foo2.clone(); async move { ProtocolFoo::new(channel, p2p, foo).await } }).await; --- src/net/mod.rs | 3 +- src/net/protocol/mod.rs | 10 ++++-- src/net/protocol/protocol_address.rs | 3 +- src/net/protocol/protocol_base.rs | 6 ++-- src/net/protocol/protocol_ping.rs | 3 +- src/net/protocol/protocol_registry.rs | 25 +++++++++---- src/net/protocol/protocol_seed.rs | 51 +++++++++++++++++---------- src/net/session/inbound_session.rs | 15 +++++--- src/net/session/manual_session.rs | 14 +++++--- src/net/session/mod.rs | 8 +++++ src/net/session/outbound_session.rs | 14 +++++--- src/net/session/seed_session.rs | 14 +++++--- src/net/session/session.rs | 7 ++-- 13 files changed, 118 insertions(+), 55 deletions(-) diff --git a/src/net/mod.rs b/src/net/mod.rs index 37b1d2d7a..61491796f 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -92,5 +92,6 @@ pub use hosts::{Hosts, HostsPtr}; pub use message::Message; pub use message_subscriber::MessageSubscription; pub use p2p::{P2p, P2pPtr}; -pub use protocol::{ProtocolJobsManager, ProtocolJobsManagerPtr}; +pub use protocol::{ProtocolJobsManager, ProtocolJobsManagerPtr, ProtocolBasePtr, ProtocolBase}; +pub use session::{SESSION_ALL, SESSION_INBOUND, SESSION_MANUAL, SESSION_OUTBOUND, SESSION_SEED}; pub use settings::{Settings, SettingsPtr}; diff --git a/src/net/protocol/mod.rs b/src/net/protocol/mod.rs index 0d7308a44..d109de207 100644 --- a/src/net/protocol/mod.rs +++ b/src/net/protocol/mod.rs @@ -58,10 +58,14 @@ pub use protocol_version::ProtocolVersion; pub use protocol_base::{ProtocolBase, ProtocolBasePtr}; pub use protocol_registry::ProtocolRegistry; -use crate::net::P2pPtr; +use crate::net::{ + session::{SESSION_ALL, SESSION_SEED}, + P2pPtr, +}; pub async fn register_default_protocols(p2p: P2pPtr) { let registry = p2p.protocol_registry(); - registry.register(ProtocolPing::new2).await; - registry.register(ProtocolAddress::new2).await; + registry.register(SESSION_ALL, ProtocolPing::new2).await; + registry.register(!SESSION_SEED, ProtocolAddress::new2).await; + registry.register(SESSION_SEED, ProtocolSeed::new2).await; } diff --git a/src/net/protocol/protocol_address.rs b/src/net/protocol/protocol_address.rs index d1e91f60a..f43b0ceab 100644 --- a/src/net/protocol/protocol_address.rs +++ b/src/net/protocol/protocol_address.rs @@ -125,7 +125,7 @@ impl ProtocolBase for ProtocolAddress { /// Starts the address protocol. Runs receive address and get address /// protocols on the protocol task manager. Then sends get-address /// message. - async fn start(self: Arc, executor: Arc>) { + async fn start(self: Arc, executor: Arc>) -> Result<()> { debug!(target: "net", "ProtocolAddress::start() [START]"); self.jobsman.clone().start(executor.clone()); self.jobsman.clone().spawn(self.clone().handle_receive_addrs(), executor.clone()).await; @@ -135,5 +135,6 @@ impl ProtocolBase for ProtocolAddress { let get_addrs = message::GetAddrsMessage {}; let _ = self.channel.clone().send(get_addrs).await; debug!(target: "net", "ProtocolAddress::start() [END]"); + Ok(()) } } diff --git a/src/net/protocol/protocol_base.rs b/src/net/protocol/protocol_base.rs index 990b5a677..bcc4e5830 100644 --- a/src/net/protocol/protocol_base.rs +++ b/src/net/protocol/protocol_base.rs @@ -2,9 +2,11 @@ use async_trait::async_trait; use smol::Executor; use std::sync::Arc; -pub type ProtocolBasePtr = Arc; +use crate::error::Result; + +pub type ProtocolBasePtr = Arc; #[async_trait] pub trait ProtocolBase { - async fn start(self: Arc, executor: Arc>); + async fn start(self: Arc, executor: Arc>) -> Result<()>; } diff --git a/src/net/protocol/protocol_ping.rs b/src/net/protocol/protocol_ping.rs index 3a15b7315..15462f6ec 100644 --- a/src/net/protocol/protocol_ping.rs +++ b/src/net/protocol/protocol_ping.rs @@ -118,11 +118,12 @@ impl ProtocolBase for ProtocolPing { /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the /// protocol task manager, then queues the reply. Sends out a ping and /// waits for pong reply. Waits for ping and replies with a pong. - async fn start(self: Arc, executor: Arc>) { + async fn start(self: Arc, executor: Arc>) -> Result<()> { debug!(target: "net", "ProtocolPing::start() [START]"); self.jobsman.clone().start(executor.clone()); self.jobsman.clone().spawn(self.clone().run_ping_pong(), executor.clone()).await; self.jobsman.clone().spawn(self.reply_to_ping(), executor).await; debug!(target: "net", "ProtocolPing::start() [END]"); + Ok(()) } } diff --git a/src/net/protocol/protocol_registry.rs b/src/net/protocol/protocol_registry.rs index d1f8f00f3..2a956c654 100644 --- a/src/net/protocol/protocol_registry.rs +++ b/src/net/protocol/protocol_registry.rs @@ -6,7 +6,7 @@ use super::protocol_base::ProtocolBase; use std::sync::Arc; //use super::protocol_base::ProtocolBasePtr; -use crate::net::{ChannelPtr, P2pPtr}; +use crate::net::{session::SessionBitflag, ChannelPtr, P2pPtr}; type ProtocolBasePtr = Arc; @@ -17,7 +17,7 @@ type Constructor = Box< >; pub struct ProtocolRegistry { - protocol_constructors: Mutex>, + protocol_constructors: Mutex>, } impl ProtocolRegistry { @@ -26,20 +26,31 @@ impl ProtocolRegistry { } // add_protocol()? - pub async fn register(&self, constructor: C) + pub async fn register(&self, session_flags: SessionBitflag, constructor: C) where C: 'static + Fn(ChannelPtr, P2pPtr) -> F + Send + Sync, F: 'static + Future> + Send, { let constructor = move |channel, p2p| { - Box::pin(constructor(channel, p2p)) as BoxFuture<'static, Arc> + Box::pin(constructor(channel, p2p)) + as BoxFuture<'static, Arc> }; - self.protocol_constructors.lock().await.push(Box::new(constructor)); + self.protocol_constructors.lock().await.push((session_flags, Box::new(constructor))); } - pub async fn attach(&self, channel: ChannelPtr, p2p: P2pPtr) -> Vec> { + pub async fn attach( + &self, + selector_id: SessionBitflag, + channel: ChannelPtr, + p2p: P2pPtr, + ) -> Vec> { let mut protocols: Vec> = Vec::new(); - for construct in self.protocol_constructors.lock().await.iter() { + for (session_flags, construct) in self.protocol_constructors.lock().await.iter() { + // Skip protocols that are not registered for this session + if selector_id & session_flags == 0 { + continue + } + let protocol: Arc = construct(channel.clone(), p2p.clone()).await; protocols.push(protocol) diff --git a/src/net/protocol/protocol_seed.rs b/src/net/protocol/protocol_seed.rs index e27e267c0..b033c1963 100644 --- a/src/net/protocol/protocol_seed.rs +++ b/src/net/protocol/protocol_seed.rs @@ -1,10 +1,15 @@ -use log::*; +use async_trait::async_trait; +use log::debug; use smol::Executor; use std::sync::Arc; use crate::{ error::Result, - net::{message, ChannelPtr, HostsPtr, SettingsPtr}, + net::{ + message, + protocol::{ProtocolBase, ProtocolBasePtr}, + ChannelPtr, HostsPtr, P2pPtr, SettingsPtr, + }, }; /// Implements the seed protocol. @@ -20,10 +25,35 @@ impl ProtocolSeed { Arc::new(Self { channel, hosts, settings }) } + pub async fn new2(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { + let hosts = p2p.hosts(); + let settings = p2p.settings(); + + Arc::new(Self { channel, hosts, settings }) + } + + /// Sends own external address over a channel. Imports own external address + /// from settings, then adds that address to an address message and + /// sends it out over the channel. + pub async fn send_self_address(&self) -> Result<()> { + match self.settings.external_addr { + Some(addr) => { + debug!(target: "net", "ProtocolSeed::send_own_address() addr={}", addr); + let addr = message::AddrsMessage { addrs: vec![addr] }; + Ok(self.channel.clone().send(addr).await?) + } + // Do nothing if external address is not configured + None => Ok(()), + } + } +} + +#[async_trait] +impl ProtocolBase for ProtocolSeed { /// Starts the seed protocol. Creates a subscription to the address message, /// then sends our address to the seed server. Sends a get-address /// message and receives an address message. - pub async fn start(self: Arc, _executor: Arc>) -> Result<()> { + async fn start(self: Arc, _executor: Arc>) -> Result<()> { debug!(target: "net", "ProtocolSeed::start() [START]"); // Create a subscription to address message. let addr_sub = self @@ -48,19 +78,4 @@ impl ProtocolSeed { debug!(target: "net", "ProtocolSeed::start() [END]"); Ok(()) } - - /// Sends own external address over a channel. Imports own external address - /// from settings, then adds that address to an address message and - /// sends it out over the channel. - pub async fn send_self_address(&self) -> Result<()> { - match self.settings.external_addr { - Some(addr) => { - debug!(target: "net", "ProtocolSeed::send_own_address() addr={}", addr); - let addr = message::AddrsMessage { addrs: vec![addr] }; - Ok(self.channel.clone().send(addr).await?) - } - // Do nothing if external address is not configured - None => Ok(()), - } - } } diff --git a/src/net/session/inbound_session.rs b/src/net/session/inbound_session.rs index 946d07db1..667486e69 100644 --- a/src/net/session/inbound_session.rs +++ b/src/net/session/inbound_session.rs @@ -9,7 +9,7 @@ use crate::{ error::{Error, Result}, net::{ protocol::{ProtocolAddress, ProtocolBase, ProtocolPing}, - session::Session, + session::{Session, SessionBitflag, SESSION_INBOUND}, Acceptor, AcceptorPtr, ChannelPtr, P2p, }, system::{StoppableTask, StoppableTaskPtr}, @@ -96,11 +96,12 @@ impl InboundSession { self.clone().register_channel(channel.clone(), executor.clone()).await?; - self.attach_protocols(channel, executor).await + //self.attach_protocols(channel, executor).await + Ok(()) } - /// Starts sending keep-alive and address messages across the channels. - async fn attach_protocols( + // Starts sending keep-alive and address messages across the channels. + /*async fn attach_protocols( self: Arc, channel: ChannelPtr, executor: Arc>, @@ -114,11 +115,15 @@ impl InboundSession { protocol_addr.start(executor).await; Ok(()) - } + }*/ } impl Session for InboundSession { fn p2p(&self) -> Arc { self.p2p.upgrade().unwrap() } + + fn selector_id(&self) -> SessionBitflag { + SESSION_INBOUND + } } diff --git a/src/net/session/manual_session.rs b/src/net/session/manual_session.rs index 713ebe751..b10f9155c 100644 --- a/src/net/session/manual_session.rs +++ b/src/net/session/manual_session.rs @@ -10,7 +10,7 @@ use crate::{ error::{Error, Result}, net::{ protocol::{ProtocolAddress, ProtocolBase, ProtocolPing}, - session::Session, + session::{Session, SessionBitflag, SESSION_MANUAL}, ChannelPtr, Connector, P2p, }, system::{StoppableTask, StoppableTaskPtr}, @@ -89,7 +89,7 @@ impl ManualSession { // Remove pending lock since register_channel will add the channel to p2p self.p2p().remove_pending(&addr).await; - self.clone().attach_protocols(channel, executor.clone()).await?; + //self.clone().attach_protocols(channel, executor.clone()).await?; // Wait for channel to close stop_sub.receive().await; @@ -112,8 +112,8 @@ impl ManualSession { Ok(()) } - /// Starts sending keep-alive and address messages across the channels. - async fn attach_protocols( + // Starts sending keep-alive and address messages across the channels. + /*async fn attach_protocols( self: Arc, channel: ChannelPtr, executor: Arc>, @@ -127,11 +127,15 @@ impl ManualSession { protocol_addr.start(executor).await; Ok(()) - } + }*/ } impl Session for ManualSession { fn p2p(&self) -> Arc { self.p2p.upgrade().unwrap() } + + fn selector_id(&self) -> SessionBitflag { + SESSION_MANUAL + } } diff --git a/src/net/session/mod.rs b/src/net/session/mod.rs index 9dcf93238..1cad1ffef 100644 --- a/src/net/session/mod.rs +++ b/src/net/session/mod.rs @@ -27,6 +27,14 @@ pub mod outbound_session; /// channel and initializing the channel by performing a network handshake. pub mod session; +// bitwise selectors for the protocol_registry +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_ALL: SessionBitflag = 0b1111; + pub use inbound_session::InboundSession; pub use manual_session::ManualSession; pub use outbound_session::OutboundSession; diff --git a/src/net/session/outbound_session.rs b/src/net/session/outbound_session.rs index 654e228d5..8c0f91716 100644 --- a/src/net/session/outbound_session.rs +++ b/src/net/session/outbound_session.rs @@ -10,7 +10,7 @@ use crate::{ error::{Error, Result}, net::{ protocol::{ProtocolAddress, ProtocolBase, ProtocolPing}, - session::Session, + session::{Session, SessionBitflag, SESSION_OUTBOUND}, ChannelPtr, Connector, P2p, }, system::{StoppableTask, StoppableTaskPtr}, @@ -91,7 +91,7 @@ impl OutboundSession { // Remove pending lock since register_channel will add the channel to p2p self.p2p().remove_pending(&addr).await; - self.clone().attach_protocols(channel, executor.clone()).await?; + //self.clone().attach_protocols(channel, executor.clone()).await?; // Wait for channel to close stop_sub.receive().await; @@ -151,8 +151,8 @@ impl OutboundSession { } } - /// Starts sending keep-alive and address messages across the channels. - async fn attach_protocols( + // Starts sending keep-alive and address messages across the channels. + /*async fn attach_protocols( self: Arc, channel: ChannelPtr, executor: Arc>, @@ -166,11 +166,15 @@ impl OutboundSession { protocol_addr.start(executor).await; Ok(()) - } + }*/ } impl Session for OutboundSession { fn p2p(&self) -> Arc { self.p2p.upgrade().unwrap() } + + fn selector_id(&self) -> SessionBitflag { + SESSION_OUTBOUND + } } diff --git a/src/net/session/seed_session.rs b/src/net/session/seed_session.rs index 96daf4566..403d7b0f7 100644 --- a/src/net/session/seed_session.rs +++ b/src/net/session/seed_session.rs @@ -10,7 +10,7 @@ use crate::{ error::{Error, Result}, net::{ protocol::{ProtocolBase, ProtocolPing, ProtocolSeed}, - session::Session, + session::{Session, SessionBitflag, SESSION_SEED}, ChannelPtr, Connector, HostsPtr, P2p, SettingsPtr, }, util::sleep, @@ -100,7 +100,7 @@ impl SeedSession { self.clone().register_channel(channel.clone(), executor.clone()).await?; - self.attach_protocols(channel, hosts, settings, executor).await?; + //self.attach_protocols(channel, hosts, settings, executor).await?; debug!(target: "net", "SeedSession::start_seed(i={}) [END]", seed_index); Ok(()) @@ -112,8 +112,8 @@ impl SeedSession { } } - /// Starts keep-alive messages and seed protocol. - async fn attach_protocols( + // Starts keep-alive messages and seed protocol. + /*async fn attach_protocols( self: Arc, channel: ChannelPtr, hosts: HostsPtr, @@ -130,11 +130,15 @@ impl SeedSession { channel.stop().await; Ok(()) - } + }*/ } impl Session for SeedSession { fn p2p(&self) -> Arc { self.p2p.upgrade().unwrap() } + + fn selector_id(&self) -> SessionBitflag { + SESSION_SEED + } } diff --git a/src/net/session/session.rs b/src/net/session/session.rs index 5f6b615f1..56c55b073 100644 --- a/src/net/session/session.rs +++ b/src/net/session/session.rs @@ -42,7 +42,8 @@ pub trait Session: Sync { // while the handshake protocol is ongoing. // They are currently in sleep mode. let p2p = self.p2p(); - let protocols = p2p.protocol_registry().attach(channel.clone(), p2p.clone()).await; + let protocols = + p2p.protocol_registry().attach(self.selector_id(), channel.clone(), p2p.clone()).await; // Perform the handshake protocol let protocol_version = ProtocolVersion::new(channel.clone(), self.p2p().settings()).await; @@ -62,7 +63,7 @@ pub trait Session: Sync { // correctly self destructing when the channel ends. for protocol in protocols { // Activate protocol - protocol.start(executor.clone()).await; + protocol.start(executor.clone()).await?; } debug!(target: "net", "Session::register_channel() [END]"); @@ -95,4 +96,6 @@ pub trait Session: Sync { /// Returns a pointer to the p2p network interface. fn p2p(&self) -> P2pPtr; + + fn selector_id(&self) -> u32; } From 3b914bfe36ff2b4b464b12ab3f9628bff2aba1d1 Mon Sep 17 00:00:00 2001 From: narodnik Date: Sat, 5 Feb 2022 10:15:01 +0100 Subject: [PATCH 22/38] Switch IRCd to using net protocol registry --- bin/ircd/src/main.rs | 53 ++++++++++++++++---------------- bin/ircd/src/protocol_privmsg.rs | 24 ++++++++++----- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/bin/ircd/src/main.rs b/bin/ircd/src/main.rs index e4240b917..58ee66e3a 100644 --- a/bin/ircd/src/main.rs +++ b/bin/ircd/src/main.rs @@ -98,25 +98,6 @@ async fn process_user_input( Ok(()) } -async fn channel_loop( - p2p: net::P2pPtr, - sender: async_channel::Sender>, - seen_privmsg_ids: SeenPrivMsgIdsPtr, - executor: Arc>, -) -> Result<()> { - let new_channel_sub = p2p.subscribe_channel().await; - - loop { - let channel = new_channel_sub.receive().await?; - - let protocol_privmsg = - ProtocolPrivMsg::new(channel, sender.clone(), seen_privmsg_ids.clone(), p2p.clone()) - .await; - - protocol_privmsg.start(executor.clone()).await; - } -} - async fn start(executor: Arc>, options: ProgramOptions) -> Result<()> { let listener = match Async::::bind(options.irc_accept_addr) { Ok(listener) => listener, @@ -145,7 +126,28 @@ async fn start(executor: Arc>, options: ProgramOptions) -> Result<( let seen_privmsg_ids = SeenPrivMsgIds::new(); + // + // PrivMsg protocol + // let p2p = net::P2p::new(options.network_settings).await; + let registry = p2p.protocol_registry(); + + let (sender, recvr) = async_channel::unbounded(); + let seen_privmsg_ids2 = seen_privmsg_ids.clone(); + let sender2 = sender.clone(); + registry.register( + !net::SESSION_SEED, + move |channel, p2p| { + let sender = sender2.clone(); + let seen_privmsg_ids = seen_privmsg_ids2.clone(); + async move { + ProtocolPrivMsg::new(channel, sender, seen_privmsg_ids, p2p).await + } + }).await; + + // + // p2p network main instance + // // Performs seed session p2p.clone().start(executor.clone()).await?; // Actual main p2p session @@ -159,13 +161,9 @@ async fn start(executor: Arc>, options: ProgramOptions) -> Result<( }) .detach(); - let (sender, recvr) = async_channel::unbounded(); - // for now the p2p and channel sub sessions just run forever - // so detach them as background processes. - executor - .spawn(channel_loop(p2p.clone(), sender, seen_privmsg_ids.clone(), executor.clone())) - .detach(); - + // + // RPC interface + // let ex2 = executor.clone(); let ex3 = ex2.clone(); let rpc_interface = Arc::new(JsonRpcInterface {}); @@ -173,6 +171,9 @@ async fn start(executor: Arc>, options: ProgramOptions) -> Result<( .spawn(async move { listen_and_serve(server_config, rpc_interface, ex3).await }) .detach(); + // + // IRC instance + // loop { let (stream, peer_addr) = match listener.accept().await { Ok((s, a)) => (s, a), diff --git a/bin/ircd/src/protocol_privmsg.rs b/bin/ircd/src/protocol_privmsg.rs index 315042ac8..336e1eb6a 100644 --- a/bin/ircd/src/protocol_privmsg.rs +++ b/bin/ircd/src/protocol_privmsg.rs @@ -1,3 +1,4 @@ +use async_trait::async_trait; use async_executor::Executor; use darkfi::{net, Result}; @@ -20,7 +21,7 @@ impl ProtocolPrivMsg { notify_queue_sender: async_channel::Sender>, seen_privmsg_ids: SeenPrivMsgIdsPtr, p2p: net::P2pPtr, - ) -> Arc { + ) -> net::ProtocolBasePtr { let message_subsytem = channel.get_message_subsystem(); message_subsytem.add_dispatch::().await; @@ -38,13 +39,6 @@ impl ProtocolPrivMsg { }) } - pub async fn start(self: Arc, executor: Arc>) { - debug!(target: "ircd", "ProtocolPrivMsg::start() [START]"); - self.jobsman.clone().start(executor.clone()); - self.jobsman.clone().spawn(self.clone().handle_receive_privmsg(), executor.clone()).await; - debug!(target: "ircd", "ProtocolPrivMsg::start() [END]"); - } - async fn handle_receive_privmsg(self: Arc) -> Result<()> { debug!(target: "ircd", "ProtocolAddress::handle_receive_privmsg() [START]"); loop { @@ -71,3 +65,17 @@ impl ProtocolPrivMsg { } } } + +#[async_trait] +impl net::ProtocolBase for ProtocolPrivMsg { + /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the + /// protocol task manager, then queues the reply. Sends out a ping and + /// waits for pong reply. Waits for ping and replies with a pong. + async fn start(self: Arc, executor: Arc>) -> Result<()> { + debug!(target: "ircd", "ProtocolPrivMsg::start() [START]"); + self.jobsman.clone().start(executor.clone()); + self.jobsman.clone().spawn(self.clone().handle_receive_privmsg(), executor.clone()).await; + debug!(target: "ircd", "ProtocolPrivMsg::start() [END]"); + Ok(()) + } +} From 89371962fe64288b1cc2df808ceb66751947149c Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 5 Feb 2022 10:40:42 +0100 Subject: [PATCH 23/38] map: backtracked and removed Mutex due to async rendering error ui() needs a mutable reference or Mutex but cannot be async due to synchronous capture: terminal.draw(|f| { ui::ui(f, app.clone()); })?; --- bin/map/src/app.rs | 2 ++ bin/map/src/info_list.rs | 1 + bin/map/src/main.rs | 26 ++++++++++++++------------ bin/map/src/ui.rs | 19 ++++++------------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/bin/map/src/app.rs b/bin/map/src/app.rs index c718572b4..7c7b64898 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/app.rs @@ -6,6 +6,8 @@ use crate::{id_list::IdList, info_list::InfoList, node_info::NodeInfo}; // make a structure to be able to modify and read them // protect using a mutex // arc reference + +#[derive(Clone)] pub struct App { pub id_list: IdList, pub info_list: InfoList, diff --git a/bin/map/src/info_list.rs b/bin/map/src/info_list.rs index 4b99ece6b..9da3039de 100644 --- a/bin/map/src/info_list.rs +++ b/bin/map/src/info_list.rs @@ -1,5 +1,6 @@ use crate::node_info::NodeInfo; +#[derive(Clone)] pub struct InfoList { pub index: usize, pub infos: Vec, diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index e8fb212a8..90065b0fe 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -119,7 +119,8 @@ async fn main() -> Result<()> { let id_list = IdList::new(ids); - let app = Arc::new(Mutex::new(App::new(id_list, info_list))); + //let app = Arc::new(App::new(id_list, info_list)); + let app = App::new(id_list, info_list); let nthreads = num_cpus::get(); let (signal, shutdown) = async_channel::unbounded::<()>(); @@ -142,7 +143,7 @@ async fn main() -> Result<()> { result } -async fn listen(ex: Arc>, app: Arc>) -> Result<()> { +async fn listen(ex: Arc>, app: App) -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); ex.spawn(poll(client, app)).detach(); @@ -150,7 +151,7 @@ async fn listen(ex: Arc>, app: Arc>) -> Result<()> { Ok(()) } -async fn poll(client: Map, app: Arc>) -> Result<()> { +async fn poll(client: Map, _app: App) -> Result<()> { loop { let reply = client.get_info().await?; @@ -161,14 +162,14 @@ async fn poll(client: Map, app: Arc>) -> Result<()> { let node2 = &nodes[1]; let node3 = &nodes[2]; - let infos = vec![NodeInfo { + let _infos = vec![NodeInfo { id: node1["id"].to_string(), connections: node1["connections"].as_u64().unwrap() as usize, is_active: node2["is_active"].as_bool().unwrap(), last_message: node3["message"].to_string(), }]; - //app.update(infos).await; + //app.lock().await.update(infos).await; } else { // TODO: error handling println!("Reply is an error"); @@ -178,19 +179,20 @@ async fn poll(client: Map, app: Arc>) -> Result<()> { } } -async fn run_app(terminal: &mut Terminal, app: Arc>) -> io::Result<()> { +async fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<()> { let mut asi = async_stdin(); terminal.clear()?; - app.lock().await.id_list.state.select(Some(0)); + app.id_list.state.select(Some(0)); - app.lock().await.info_list.index = 0; + app.info_list.index = 0; // acquire the mutex // let mut app = app.lock(); loop { + // clone everything terminal.draw(|f| { ui::ui(f, app.clone()); })?; @@ -201,12 +203,12 @@ async fn run_app(terminal: &mut Terminal, app: Arc>) - return Ok(()); } Key::Char('j') => { - app.lock().await.id_list.next(); - app.lock().await.info_list.next(); + app.id_list.next(); + app.info_list.next().await; } Key::Char('k') => { - app.lock().await.id_list.previous(); - app.lock().await.info_list.previous(); + app.id_list.previous(); + app.info_list.previous().await; } _ => (), } diff --git a/bin/map/src/ui.rs b/bin/map/src/ui.rs index 8bf058b68..1d16b182c 100644 --- a/bin/map/src/ui.rs +++ b/bin/map/src/ui.rs @@ -9,7 +9,7 @@ use tui::{ Frame, }; -pub async fn ui(f: &mut Frame<'_, B>, app: Arc>) { +pub fn ui(f: &mut Frame<'_, B>, mut app: App) { let slice = Layout::default() .direction(Direction::Horizontal) .margin(2) @@ -17,8 +17,6 @@ pub async fn ui(f: &mut Frame<'_, B>, app: Arc>) { .split(f.size()); let nodes: Vec = app - .lock() - .await .id_list .node_id .iter() @@ -32,20 +30,15 @@ pub async fn ui(f: &mut Frame<'_, B>, app: Arc>) { .block(Block::default().borders(Borders::ALL)) .highlight_style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD)); - f.render_stateful_widget(nodes, slice[0], &mut app.lock().await.id_list.state); + f.render_stateful_widget(nodes, slice[0], &mut app.id_list.state); - let index = app.lock().await.info_list.index; + let index = app.info_list.index; - render_info(app, f, index, slice).await; + render_info(app, f, index, slice); } -async fn render_info( - app: Arc>, - f: &mut Frame<'_, B>, - index: usize, - slice: Vec, -) { - let info = &app.lock().await.info_list.infos; +fn render_info(app: App, f: &mut Frame<'_, B>, index: usize, slice: Vec) { + let info = &app.info_list.infos; let id = &info[index].id; let connections = info[index].connections; let is_active = info[index].is_active; From 828535a8121dcfd122e5620a6a480c033941bc6e Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 5 Feb 2022 10:45:07 +0100 Subject: [PATCH 24/38] map: moved NodeInfo to own crate --- bin/map/src/node_info.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 bin/map/src/node_info.rs diff --git a/bin/map/src/node_info.rs b/bin/map/src/node_info.rs new file mode 100644 index 000000000..06ef3eb42 --- /dev/null +++ b/bin/map/src/node_info.rs @@ -0,0 +1,30 @@ +//TODO: made node_id into a HashSet(u32) +// wrap NodeInfo and NodeId in a Mutex + +pub type NodeId = u32; + +#[derive(Clone)] +pub struct NodeInfo { + pub id: String, + pub connections: usize, + pub is_active: bool, + pub last_message: String, +} + +impl NodeInfo { + pub fn new() -> NodeInfo { + let connections = 0; + let is_active = false; + NodeInfo { id: String::new(), connections, is_active, last_message: String::new() } + } +} + +impl Default for NodeInfo { + fn default() -> Self { + Self::new() + } +} + +//pub async fn add_seen(&self, id: u32) { +// self.privmsg_ids.lock().await.insert(id); +//} From c51187dead9824ed5537a0b50d340be28782ad5f Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 5 Feb 2022 10:48:06 +0100 Subject: [PATCH 25/38] map: renamed listen() to run_rpc() --- bin/map/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 90065b0fe..27fb96d45 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -133,7 +133,7 @@ async fn main() -> Result<()> { // Run the main future on the current thread. .finish(|| { smol::future::block_on(async move { - listen(ex2.clone(), app.clone()).await?; + run_rpc(ex2.clone(), app.clone()).await?; run_app(&mut terminal, app.clone()).await?; drop(signal); Ok::<(), darkfi::Error>(()) @@ -143,7 +143,7 @@ async fn main() -> Result<()> { result } -async fn listen(ex: Arc>, app: App) -> Result<()> { +async fn run_rpc(ex: Arc>, app: App) -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); ex.spawn(poll(client, app)).detach(); From 7a8a10aaac06e169c768c4bc7f6b9d13a2b03db0 Mon Sep 17 00:00:00 2001 From: narodnik Date: Sat, 5 Feb 2022 10:48:56 +0100 Subject: [PATCH 26/38] add protocol names --- bin/ircd/src/protocol_privmsg.rs | 8 +++++--- src/net/message.rs | 4 ++-- src/net/message_subscriber.rs | 4 +++- src/net/protocol/protocol_address.rs | 4 ++++ src/net/protocol/protocol_base.rs | 2 ++ src/net/protocol/protocol_ping.rs | 4 ++++ src/net/protocol/protocol_registry.rs | 2 ++ src/net/protocol/protocol_seed.rs | 4 ++++ src/net/session/session.rs | 3 ++- 9 files changed, 28 insertions(+), 7 deletions(-) diff --git a/bin/ircd/src/protocol_privmsg.rs b/bin/ircd/src/protocol_privmsg.rs index 336e1eb6a..3d78ebded 100644 --- a/bin/ircd/src/protocol_privmsg.rs +++ b/bin/ircd/src/protocol_privmsg.rs @@ -25,8 +25,6 @@ impl ProtocolPrivMsg { let message_subsytem = channel.get_message_subsystem(); message_subsytem.add_dispatch::().await; - debug!("ADDED DISPATCH"); - let privmsg_sub = channel.subscribe_msg::().await.expect("Missing PrivMsg dispatcher!"); @@ -40,7 +38,7 @@ impl ProtocolPrivMsg { } async fn handle_receive_privmsg(self: Arc) -> Result<()> { - debug!(target: "ircd", "ProtocolAddress::handle_receive_privmsg() [START]"); + debug!(target: "ircd", "ProtocolPrivMsg::handle_receive_privmsg() [START]"); loop { let privmsg = self.privmsg_sub.receive().await?; @@ -78,4 +76,8 @@ impl net::ProtocolBase for ProtocolPrivMsg { debug!(target: "ircd", "ProtocolPrivMsg::start() [END]"); Ok(()) } + + fn name(&self) -> &'static str { + "ProtocolPrivMsg" + } } diff --git a/src/net/message.rs b/src/net/message.rs index 1c42a7abb..6ab8737f5 100644 --- a/src/net/message.rs +++ b/src/net/message.rs @@ -1,5 +1,5 @@ -use futures::prelude::*; -use log::*; +use futures::{AsyncRead, AsyncWrite, AsyncReadExt, AsyncWriteExt}; +use log::debug; use std::{io, net::SocketAddr}; use crate::{ diff --git a/src/net/message_subscriber.rs b/src/net/message_subscriber.rs index a9db4d52e..b47627e1a 100644 --- a/src/net/message_subscriber.rs +++ b/src/net/message_subscriber.rs @@ -1,6 +1,6 @@ use async_std::sync::Mutex; use async_trait::async_trait; -use log::*; +use log::{debug, error, warn}; use rand::Rng; use std::{any::Any, collections::HashMap, io, io::Cursor, sync::Arc}; @@ -87,6 +87,7 @@ impl MessageDispatcher { /// channels. async fn trigger_all(&self, message: MessageResult) { debug!( + target: "net", "MessageDispatcher::trigger_all({}) [START, subs={}]", M::name(), if message.is_ok() { "msg" } else { "err" }, @@ -109,6 +110,7 @@ impl MessageDispatcher { self.collect_garbage(garbage_ids).await; debug!( + target: "net", "MessageDispatcher::trigger_all({}) [END, subs={}]", M::name(), if message.is_ok() { "msg" } else { "err" }, diff --git a/src/net/protocol/protocol_address.rs b/src/net/protocol/protocol_address.rs index f43b0ceab..cbbfe1060 100644 --- a/src/net/protocol/protocol_address.rs +++ b/src/net/protocol/protocol_address.rs @@ -137,4 +137,8 @@ impl ProtocolBase for ProtocolAddress { debug!(target: "net", "ProtocolAddress::start() [END]"); Ok(()) } + + fn name(&self) -> &'static str { + "ProtocolAddress" + } } diff --git a/src/net/protocol/protocol_base.rs b/src/net/protocol/protocol_base.rs index bcc4e5830..54857bbb9 100644 --- a/src/net/protocol/protocol_base.rs +++ b/src/net/protocol/protocol_base.rs @@ -9,4 +9,6 @@ pub type ProtocolBasePtr = Arc; #[async_trait] pub trait ProtocolBase { async fn start(self: Arc, executor: Arc>) -> Result<()>; + + fn name(&self) -> &'static str; } diff --git a/src/net/protocol/protocol_ping.rs b/src/net/protocol/protocol_ping.rs index 15462f6ec..728d2379e 100644 --- a/src/net/protocol/protocol_ping.rs +++ b/src/net/protocol/protocol_ping.rs @@ -126,4 +126,8 @@ impl ProtocolBase for ProtocolPing { debug!(target: "net", "ProtocolPing::start() [END]"); Ok(()) } + + fn name(&self) -> &'static str { + "ProtocolPing" + } } diff --git a/src/net/protocol/protocol_registry.rs b/src/net/protocol/protocol_registry.rs index 2a956c654..69cab868b 100644 --- a/src/net/protocol/protocol_registry.rs +++ b/src/net/protocol/protocol_registry.rs @@ -1,6 +1,7 @@ use async_std::sync::Mutex; use futures::future::BoxFuture; use std::future::Future; +use log::debug; use super::protocol_base::ProtocolBase; use std::sync::Arc; @@ -53,6 +54,7 @@ impl ProtocolRegistry { let protocol: Arc = construct(channel.clone(), p2p.clone()).await; + debug!(target: "net", "Attached {}", protocol.name()); protocols.push(protocol) } protocols diff --git a/src/net/protocol/protocol_seed.rs b/src/net/protocol/protocol_seed.rs index b033c1963..477e6047b 100644 --- a/src/net/protocol/protocol_seed.rs +++ b/src/net/protocol/protocol_seed.rs @@ -78,4 +78,8 @@ impl ProtocolBase for ProtocolSeed { debug!(target: "net", "ProtocolSeed::start() [END]"); Ok(()) } + + fn name(&self) -> &'static str { + "ProtocolSeed" + } } diff --git a/src/net/session/session.rs b/src/net/session/session.rs index 56c55b073..e16fddca7 100644 --- a/src/net/session/session.rs +++ b/src/net/session/session.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use log::*; +use log::debug; use smol::Executor; use std::sync::Arc; @@ -57,6 +57,7 @@ pub trait Session: Sync { handshake_task.await?; // Now the channel is ready + debug!(target: "net", "Session handshake complete. Activating remaining protocols"); // Now start all the protocols // They are responsible for managing their own lifetimes and From 7343c0be83d639d9334e0368fd1d386da0f3b502 Mon Sep 17 00:00:00 2001 From: narodnik Date: Sat, 5 Feb 2022 10:51:46 +0100 Subject: [PATCH 27/38] delete old new() functions and rename new2() to new() --- src/net/protocol/mod.rs | 6 +++--- src/net/protocol/protocol_address.rs | 26 +------------------------- src/net/protocol/protocol_ping.rs | 12 +----------- src/net/protocol/protocol_seed.rs | 6 +----- 4 files changed, 6 insertions(+), 44 deletions(-) diff --git a/src/net/protocol/mod.rs b/src/net/protocol/mod.rs index d109de207..1d4216d20 100644 --- a/src/net/protocol/mod.rs +++ b/src/net/protocol/mod.rs @@ -65,7 +65,7 @@ use crate::net::{ pub async fn register_default_protocols(p2p: P2pPtr) { let registry = p2p.protocol_registry(); - registry.register(SESSION_ALL, ProtocolPing::new2).await; - registry.register(!SESSION_SEED, ProtocolAddress::new2).await; - registry.register(SESSION_SEED, ProtocolSeed::new2).await; + registry.register(SESSION_ALL, ProtocolPing::new).await; + registry.register(!SESSION_SEED, ProtocolAddress::new).await; + registry.register(SESSION_SEED, ProtocolSeed::new).await; } diff --git a/src/net/protocol/protocol_address.rs b/src/net/protocol/protocol_address.rs index cbbfe1060..88d692884 100644 --- a/src/net/protocol/protocol_address.rs +++ b/src/net/protocol/protocol_address.rs @@ -25,31 +25,7 @@ pub struct ProtocolAddress { impl ProtocolAddress { /// Create a new address protocol. Makes an address and get-address /// subscription and adds them to the address protocol instance. - pub async fn new(channel: ChannelPtr, hosts: HostsPtr) -> Arc { - // Creates a subscription to address message. - let addrs_sub = channel - .clone() - .subscribe_msg::() - .await - .expect("Missing addrs dispatcher!"); - - // Creates a subscription to get-address message. - let get_addrs_sub = channel - .clone() - .subscribe_msg::() - .await - .expect("Missing getaddrs dispatcher!"); - - Arc::new(Self { - channel: channel.clone(), - addrs_sub, - get_addrs_sub, - hosts, - jobsman: ProtocolJobsManager::new("ProtocolAddress", channel), - }) - } - - pub async fn new2(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { + pub async fn new(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { let hosts = p2p.hosts(); // Creates a subscription to address message. diff --git a/src/net/protocol/protocol_ping.rs b/src/net/protocol/protocol_ping.rs index 728d2379e..efc50eb0f 100644 --- a/src/net/protocol/protocol_ping.rs +++ b/src/net/protocol/protocol_ping.rs @@ -23,17 +23,7 @@ pub struct ProtocolPing { impl ProtocolPing { /// Create a new ping-pong protocol. - pub fn new(channel: ChannelPtr, p2p: P2pPtr) -> Arc { - let settings = p2p.settings(); - - Arc::new(Self { - channel: channel.clone(), - settings, - jobsman: ProtocolJobsManager::new("ProtocolPing", channel), - }) - } - - pub async fn new2(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { + pub async fn new(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { let settings = p2p.settings(); Arc::new(Self { diff --git a/src/net/protocol/protocol_seed.rs b/src/net/protocol/protocol_seed.rs index 477e6047b..d2de384a8 100644 --- a/src/net/protocol/protocol_seed.rs +++ b/src/net/protocol/protocol_seed.rs @@ -21,11 +21,7 @@ pub struct ProtocolSeed { impl ProtocolSeed { /// Create a new seed protocol. - pub fn new(channel: ChannelPtr, hosts: HostsPtr, settings: SettingsPtr) -> Arc { - Arc::new(Self { channel, hosts, settings }) - } - - pub async fn new2(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { + pub async fn new(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { let hosts = p2p.hosts(); let settings = p2p.settings(); From cd74b6aa0b6ae39dea8e9ad0c08e4c7018febf6b Mon Sep 17 00:00:00 2001 From: narodnik Date: Sat, 5 Feb 2022 11:04:19 +0100 Subject: [PATCH 28/38] from now on all subscribers in protocols must be created in the constructor. move subscribers in ProtocolPing to the constructor. --- src/net/protocol/protocol_ping.rs | 39 +++++++++++++++++-------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/net/protocol/protocol_ping.rs b/src/net/protocol/protocol_ping.rs index efc50eb0f..020ec8568 100644 --- a/src/net/protocol/protocol_ping.rs +++ b/src/net/protocol/protocol_ping.rs @@ -8,6 +8,7 @@ use crate::{ error::{Error, Result}, net::{ message, + message_subscriber::MessageSubscription, protocol::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr}, ChannelPtr, P2pPtr, SettingsPtr, }, @@ -17,6 +18,8 @@ use crate::{ /// Defines ping and pong messages. pub struct ProtocolPing { channel: ChannelPtr, + ping_sub: MessageSubscription, + pong_sub: MessageSubscription, settings: SettingsPtr, jobsman: ProtocolJobsManagerPtr, } @@ -26,8 +29,24 @@ impl ProtocolPing { pub async fn new(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr { let settings = p2p.settings(); + // Creates a subscription to ping message. + let ping_sub = channel + .clone() + .subscribe_msg::() + .await + .expect("Missing ping dispatcher!"); + + // Creates a subscription to pong message. + let pong_sub = channel + .clone() + .subscribe_msg::() + .await + .expect("Missing pong dispatcher!"); + Arc::new(Self { channel: channel.clone(), + ping_sub, + pong_sub, settings, jobsman: ProtocolJobsManager::new("ProtocolPing", channel), }) @@ -39,14 +58,6 @@ impl ProtocolPing { /// for the pong reply and insures the nonce is the same. async fn run_ping_pong(self: Arc) -> Result<()> { debug!(target: "net", "ProtocolPing::run_ping_pong() [START]"); - // Creates a subscription to pong message. - let pong_sub = self - .channel - .clone() - .subscribe_msg::() - .await - .expect("Missing pong dispatcher!"); - loop { // Wait channel_heartbeat amount of time. sleep(self.settings.channel_heartbeat_seconds).await; @@ -62,7 +73,7 @@ impl ProtocolPing { let start = Instant::now(); // Wait for pong, check nonce matches. - let pong_msg = pong_sub.receive().await?; + let pong_msg = self.pong_sub.receive().await?; if pong_msg.nonce != nonce { error!("Wrong nonce for ping reply. Disconnecting from channel."); self.channel.stop().await; @@ -77,17 +88,9 @@ impl ProtocolPing { /// pong reply. async fn reply_to_ping(self: Arc) -> Result<()> { debug!(target: "net", "ProtocolPing::reply_to_ping() [START]"); - // Creates a subscription to ping message. - let ping_sub = self - .channel - .clone() - .subscribe_msg::() - .await - .expect("Missing ping dispatcher!"); - loop { // Wait for ping, reply with pong that has a matching nonce. - let ping = ping_sub.receive().await?; + let ping = self.ping_sub.receive().await?; debug!(target: "net", "ProtocolPing::reply_to_ping() received Ping message"); // Send pong message. From 05a8e04bc8ed42ec3c63e28e1d1bb1893dfd0465 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 5 Feb 2022 11:39:13 +0100 Subject: [PATCH 29/38] map: renamed App to Model --- bin/map/src/id_list.rs | 1 + bin/map/src/lib.rs | 4 ++-- bin/map/src/main.rs | 12 ++++++------ bin/map/src/{app.rs => model.rs} | 14 +++++++------- bin/map/src/ui.rs | 7 ++++--- 5 files changed, 20 insertions(+), 18 deletions(-) rename bin/map/src/{app.rs => model.rs} (78%) diff --git a/bin/map/src/id_list.rs b/bin/map/src/id_list.rs index 43333d233..87e27fe1d 100644 --- a/bin/map/src/id_list.rs +++ b/bin/map/src/id_list.rs @@ -5,6 +5,7 @@ use tui::widgets::ListState; #[derive(Clone)] pub struct IdList { pub state: ListState, + // todo: mutex pub node_id: Vec, } diff --git a/bin/map/src/lib.rs b/bin/map/src/lib.rs index 8bb213488..f2031c668 100644 --- a/bin/map/src/lib.rs +++ b/bin/map/src/lib.rs @@ -1,9 +1,9 @@ -pub mod app; pub mod id_list; pub mod info_list; +pub mod model; pub mod node_info; pub mod types; pub mod ui; -pub use app::App; +pub use model::Model; pub use ui::ui; diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 27fb96d45..2fe9e9eaf 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -16,7 +16,7 @@ use tui::{ Terminal, }; -use map::{id_list::IdList, info_list::InfoList, node_info::NodeInfo, ui, App}; +use map::{id_list::IdList, info_list::InfoList, node_info::NodeInfo, ui, Model}; struct Map { url: String, @@ -119,8 +119,8 @@ async fn main() -> Result<()> { let id_list = IdList::new(ids); - //let app = Arc::new(App::new(id_list, info_list)); - let app = App::new(id_list, info_list); + //let app = Arc::new(Model::new(id_list, info_list)); + let app = Model::new(id_list, info_list); let nthreads = num_cpus::get(); let (signal, shutdown) = async_channel::unbounded::<()>(); @@ -143,7 +143,7 @@ async fn main() -> Result<()> { result } -async fn run_rpc(ex: Arc>, app: App) -> Result<()> { +async fn run_rpc(ex: Arc>, app: Model) -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); ex.spawn(poll(client, app)).detach(); @@ -151,7 +151,7 @@ async fn run_rpc(ex: Arc>, app: App) -> Result<()> { Ok(()) } -async fn poll(client: Map, _app: App) -> Result<()> { +async fn poll(client: Map, _app: Model) -> Result<()> { loop { let reply = client.get_info().await?; @@ -179,7 +179,7 @@ async fn poll(client: Map, _app: App) -> Result<()> { } } -async fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<()> { +async fn run_app(terminal: &mut Terminal, mut app: Model) -> io::Result<()> { let mut asi = async_stdin(); terminal.clear()?; diff --git a/bin/map/src/app.rs b/bin/map/src/model.rs similarity index 78% rename from bin/map/src/app.rs rename to bin/map/src/model.rs index 7c7b64898..77d9e33d3 100644 --- a/bin/map/src/app.rs +++ b/bin/map/src/model.rs @@ -8,19 +8,19 @@ use crate::{id_list::IdList, info_list::InfoList, node_info::NodeInfo}; // arc reference #[derive(Clone)] -pub struct App { +pub struct Model { pub id_list: IdList, pub info_list: InfoList, } -impl App { - pub fn new(id_list: IdList, info_list: InfoList) -> App { +impl Model { + pub fn new(id_list: IdList, info_list: InfoList) -> Model { //let infos = Vec::new(); //let ids = Vec::new(); //let info_list = InfoList::new(infos); //let id_list = IdList::new(ids); - App { id_list, info_list } + Model { id_list, info_list } } // TODO: implement this @@ -28,7 +28,7 @@ impl App { // Timer::after(dur).await; //} - pub async fn update(mut self, node_vec: Vec) -> App { + pub async fn update(mut self, node_vec: Vec) -> Model { let ids = vec![node_vec[0].id.clone()]; for id in ids { @@ -42,11 +42,11 @@ impl App { } let info_list = self.info_list; - App { id_list, info_list } + Model { id_list, info_list } } } -//impl Default for App { +//impl Default for Model { // fn default() -> Self { // Self::new() // } diff --git a/bin/map/src/ui.rs b/bin/map/src/ui.rs index 1d16b182c..31607613e 100644 --- a/bin/map/src/ui.rs +++ b/bin/map/src/ui.rs @@ -1,4 +1,4 @@ -use crate::app::App; +use crate::model::Model; use async_std::sync::{Arc, Mutex}; use tui::{ backend::Backend, @@ -9,7 +9,7 @@ use tui::{ Frame, }; -pub fn ui(f: &mut Frame<'_, B>, mut app: App) { +pub fn ui(f: &mut Frame<'_, B>, mut app: Model) { let slice = Layout::default() .direction(Direction::Horizontal) .margin(2) @@ -30,6 +30,7 @@ pub fn ui(f: &mut Frame<'_, B>, mut app: App) { .block(Block::default().borders(Borders::ALL)) .highlight_style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD)); + // needs to be mutable. could f.render_stateful_widget(nodes, slice[0], &mut app.id_list.state); let index = app.info_list.index; @@ -37,7 +38,7 @@ pub fn ui(f: &mut Frame<'_, B>, mut app: App) { render_info(app, f, index, slice); } -fn render_info(app: App, f: &mut Frame<'_, B>, index: usize, slice: Vec) { +fn render_info(app: Model, f: &mut Frame<'_, B>, index: usize, slice: Vec) { let info = &app.info_list.infos; let id = &info[index].id; let connections = info[index].connections; From f63783baae1792b441daf947c2c2e784ef78d3b9 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 5 Feb 2022 11:54:55 +0100 Subject: [PATCH 30/38] map: moved id_list, info_list, node_info into model.rs --- bin/map/src/id_list.rs | 52 --------------- bin/map/src/info_list.rs | 27 -------- bin/map/src/lib.rs | 5 +- bin/map/src/main.rs | 41 ++++++------ bin/map/src/model.rs | 103 ++++++++++++++++++++++++++++- bin/map/src/{ => old}/node_info.rs | 0 6 files changed, 125 insertions(+), 103 deletions(-) delete mode 100644 bin/map/src/id_list.rs delete mode 100644 bin/map/src/info_list.rs rename bin/map/src/{ => old}/node_info.rs (100%) diff --git a/bin/map/src/id_list.rs b/bin/map/src/id_list.rs deleted file mode 100644 index 87e27fe1d..000000000 --- a/bin/map/src/id_list.rs +++ /dev/null @@ -1,52 +0,0 @@ -//TODO: made node_id into a hashset across project -//use std::collections::HashSet -use tui::widgets::ListState; - -#[derive(Clone)] -pub struct IdList { - pub state: ListState, - // todo: mutex - pub node_id: Vec, -} - -impl IdList { - pub fn new(node_id: Vec) -> IdList { - IdList { state: ListState::default(), node_id } - } - - pub fn next(&mut self) { - let i = match self.state.selected() { - Some(i) => { - if i >= self.node_id.len() - 1 { - 0 - } else { - i + 1 - } - } - None => 0, - }; - self.state.select(Some(i)); - } - - pub fn previous(&mut self) { - let i = match self.state.selected() { - Some(i) => { - if i == 0 { - self.node_id.len() - 1 - } else { - i - 1 - } - } - None => 0, - }; - self.state.select(Some(i)); - } - - pub fn unselect(&mut self) { - self.state.select(None); - } -} - -//pub async fn add_seen(&self, id: u32) { -// self.privmsg_ids.lock().await.insert(id); -//} diff --git a/bin/map/src/info_list.rs b/bin/map/src/info_list.rs deleted file mode 100644 index 9da3039de..000000000 --- a/bin/map/src/info_list.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::node_info::NodeInfo; - -#[derive(Clone)] -pub struct InfoList { - pub index: usize, - pub infos: Vec, -} - -impl InfoList { - pub fn new(infos: Vec) -> InfoList { - let index = 0; - - InfoList { index, infos } - } - - pub async fn next(&mut self) { - self.index = (self.index + 1) % self.infos.len(); - } - - pub async fn previous(&mut self) { - if self.index > 0 { - self.index -= 1; - } else { - self.index = self.infos.len() - 1; - } - } -} diff --git a/bin/map/src/lib.rs b/bin/map/src/lib.rs index f2031c668..200af0967 100644 --- a/bin/map/src/lib.rs +++ b/bin/map/src/lib.rs @@ -1,9 +1,6 @@ -pub mod id_list; -pub mod info_list; pub mod model; -pub mod node_info; pub mod types; pub mod ui; -pub use model::Model; +pub use model::{IdList, InfoList, Model, NodeInfo}; pub use ui::ui; diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 2fe9e9eaf..3685566eb 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -16,7 +16,10 @@ use tui::{ Terminal, }; -use map::{id_list::IdList, info_list::InfoList, node_info::NodeInfo, ui, Model}; +use map::{ + model::{IdList, InfoList, NodeInfo}, + ui, Model, +}; struct Map { url: String, @@ -119,8 +122,8 @@ async fn main() -> Result<()> { let id_list = IdList::new(ids); - //let app = Arc::new(Model::new(id_list, info_list)); - let app = Model::new(id_list, info_list); + //let model = Arc::new(Model::new(id_list, info_list)); + let model = Model::new(id_list, info_list); let nthreads = num_cpus::get(); let (signal, shutdown) = async_channel::unbounded::<()>(); @@ -133,8 +136,8 @@ async fn main() -> Result<()> { // Run the main future on the current thread. .finish(|| { smol::future::block_on(async move { - run_rpc(ex2.clone(), app.clone()).await?; - run_app(&mut terminal, app.clone()).await?; + run_rpc(ex2.clone(), model.clone()).await?; + run_model(&mut terminal, model.clone()).await?; drop(signal); Ok::<(), darkfi::Error>(()) }) @@ -143,15 +146,15 @@ async fn main() -> Result<()> { result } -async fn run_rpc(ex: Arc>, app: Model) -> Result<()> { +async fn run_rpc(ex: Arc>, model: Model) -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); - ex.spawn(poll(client, app)).detach(); + ex.spawn(poll(client, model)).detach(); Ok(()) } -async fn poll(client: Map, _app: Model) -> Result<()> { +async fn poll(client: Map, _model: Model) -> Result<()> { loop { let reply = client.get_info().await?; @@ -169,7 +172,7 @@ async fn poll(client: Map, _app: Model) -> Result<()> { last_message: node3["message"].to_string(), }]; - //app.lock().await.update(infos).await; + //model.lock().await.update(infos).await; } else { // TODO: error handling println!("Reply is an error"); @@ -179,36 +182,36 @@ async fn poll(client: Map, _app: Model) -> Result<()> { } } -async fn run_app(terminal: &mut Terminal, mut app: Model) -> io::Result<()> { +async fn run_model(terminal: &mut Terminal, mut model: Model) -> io::Result<()> { let mut asi = async_stdin(); terminal.clear()?; - app.id_list.state.select(Some(0)); + model.id_list.state.select(Some(0)); - app.info_list.index = 0; + model.info_list.index = 0; // acquire the mutex - // let mut app = app.lock(); + // let mut model = model.lock(); loop { // clone everything terminal.draw(|f| { - ui::ui(f, app.clone()); + ui::ui(f, model.clone()); })?; for k in asi.by_ref().keys() { match k.unwrap() { Key::Char('q') => { terminal.clear()?; - return Ok(()); + return Ok(()) } Key::Char('j') => { - app.id_list.next(); - app.info_list.next().await; + model.id_list.next(); + model.info_list.next().await; } Key::Char('k') => { - app.id_list.previous(); - app.info_list.previous().await; + model.id_list.previous(); + model.info_list.previous().await; } _ => (), } diff --git a/bin/map/src/model.rs b/bin/map/src/model.rs index 77d9e33d3..cd1400dc5 100644 --- a/bin/map/src/model.rs +++ b/bin/map/src/model.rs @@ -1,4 +1,4 @@ -use crate::{id_list::IdList, info_list::InfoList, node_info::NodeInfo}; +use tui::widgets::ListState; //use async_std::sync::Mutex; //use smol::Timer; //use std::{collections::HashMap, time::Duration}; @@ -46,8 +46,109 @@ impl Model { } } +#[derive(Clone)] +pub struct IdList { + pub state: ListState, + pub node_id: Vec, +} + +impl IdList { + pub fn new(node_id: Vec) -> IdList { + IdList { state: ListState::default(), node_id } + } + + pub fn next(&mut self) { + let i = match self.state.selected() { + Some(i) => { + if i >= self.node_id.len() - 1 { + 0 + } else { + i + 1 + } + } + None => 0, + }; + self.state.select(Some(i)); + } + + pub fn previous(&mut self) { + let i = match self.state.selected() { + Some(i) => { + if i == 0 { + self.node_id.len() - 1 + } else { + i - 1 + } + } + None => 0, + }; + self.state.select(Some(i)); + } + + pub fn unselect(&mut self) { + self.state.select(None); + } +} + +#[derive(Clone)] +pub struct InfoList { + pub index: usize, + pub infos: Vec, +} + +impl InfoList { + pub fn new(infos: Vec) -> InfoList { + let index = 0; + + InfoList { index, infos } + } + + pub async fn next(&mut self) { + self.index = (self.index + 1) % self.infos.len(); + } + + pub async fn previous(&mut self) { + if self.index > 0 { + self.index -= 1; + } else { + self.index = self.infos.len() - 1; + } + } +} + //impl Default for Model { // fn default() -> Self { // Self::new() // } //} + +//TODO: made node_id into a HashSet(u32) +// wrap NodeInfo and NodeId in a Mutex + +pub type NodeId = u32; + +#[derive(Clone)] +pub struct NodeInfo { + pub id: String, + pub connections: usize, + pub is_active: bool, + pub last_message: String, +} + +impl NodeInfo { + pub fn new() -> NodeInfo { + let connections = 0; + let is_active = false; + NodeInfo { id: String::new(), connections, is_active, last_message: String::new() } + } +} + +impl Default for NodeInfo { + fn default() -> Self { + Self::new() + } +} + +//pub async fn add_seen(&self, id: u32) { +// self.privmsg_ids.lock().await.insert(id); +//} diff --git a/bin/map/src/node_info.rs b/bin/map/src/old/node_info.rs similarity index 100% rename from bin/map/src/node_info.rs rename to bin/map/src/old/node_info.rs From cabbe3c8eaf0bef73054c904e63a10996ce30a1d Mon Sep 17 00:00:00 2001 From: parazyd Date: Thu, 3 Feb 2022 14:17:15 +0100 Subject: [PATCH 31/38] zkas: Unify error functions into an error emitter. --- src/zkas/analyzer.rs | 62 +++++++--------------------- src/zkas/compiler.rs | 45 +++++--------------- src/zkas/error.rs | 41 ++++++++++++++++++ src/zkas/lexer.rs | 54 +++++++++--------------- src/zkas/mod.rs | 2 + src/zkas/parser.rs | 98 ++++++++++++++++++-------------------------- 6 files changed, 127 insertions(+), 175 deletions(-) create mode 100644 src/zkas/error.rs diff --git a/src/zkas/analyzer.rs b/src/zkas/analyzer.rs index 1f823b1d0..0d11971b7 100644 --- a/src/zkas/analyzer.rs +++ b/src/zkas/analyzer.rs @@ -1,27 +1,23 @@ use std::{ - io, io::{stdin, stdout, Read, Write}, - process, str::Chars, }; -use termion::{color, style}; - use super::{ ast::{ Constant, Constants, StatementType, Statements, Var, Variable, Variables, Witness, Witnesses, }, + error::ErrorEmitter, types::Type, }; pub struct Analyzer { - file: String, - lines: Vec, pub constants: Constants, pub witnesses: Witnesses, pub statements: Statements, pub stack: Variables, + error: ErrorEmitter, } impl Analyzer { @@ -34,15 +30,10 @@ impl Analyzer { ) -> Self { // For nice error reporting, we'll load everything into a string // vector so we have references to lines. - let lines = source.as_str().lines().map(|x| x.to_string()).collect(); - Analyzer { - file: filename.to_string(), - lines, - constants, - witnesses, - statements, - stack: vec![], - } + let lines: Vec = source.as_str().lines().map(|x| x.to_string()).collect(); + let error = ErrorEmitter::new("Semantic", filename, lines.clone()); + + Analyzer { constants, witnesses, statements, stack: vec![], error } } pub fn analyze_types(&mut self) { @@ -62,7 +53,7 @@ impl Analyzer { // It's kinda ugly. if arg_types[0] == Type::BaseArray || arg_types[0] == Type::ScalarArray { if statement.args.is_empty() { - self.error( + self.error.emit( format!( "Passed no arguments to `{:?}` call. Expected at least 1.", statement.opcode @@ -81,7 +72,7 @@ impl Analyzer { }; if arg_types[0] == Type::BaseArray && var_type != Type::Base { - self.error( + self.error.emit( format!( "Incorrect argument type. Expected `{:?}`, got `{:?}`", arg_types[0], @@ -93,7 +84,7 @@ impl Analyzer { } if arg_types[0] == Type::ScalarArray && var_type != Type::Scalar { - self.error( + self.error.emit( format!( "Incorrect argument type. Expected `{:?}`, got `{:?}`", arg_types[0], @@ -108,7 +99,7 @@ impl Analyzer { arg.typ = var_type; args.push(arg); } else { - self.error( + self.error.emit( format!("Unknown argument reference `{}`.", i.name), i.line, i.column, @@ -117,7 +108,7 @@ impl Analyzer { } } else { if statement.args.len() != arg_types.len() { - self.error( + self.error.emit( format!( "Incorrent number of args to `{:?}` call. Expected {}, got {}", statement.opcode, @@ -138,7 +129,7 @@ impl Analyzer { }; if var_type != arg_types[idx] { - self.error( + self.error.emit( format!( "Incorrect argument type. Expected `{:?}`, got `{:?}`", arg_types[idx], var_type, @@ -152,7 +143,7 @@ impl Analyzer { arg.typ = var_type; args.push(arg); } else { - self.error( + self.error.emit( format!("Unknown argument reference `{}`.", i.name), i.line, i.column, @@ -213,7 +204,7 @@ impl Analyzer { if let Some(index) = stack.iter().position(|&r| r == &arg.name) { println!("Found at stack index {}", index); } else { - self.error( + self.error.emit( format!("Could not find `{}` on the stack", arg.name), arg.line, arg.column, @@ -283,31 +274,6 @@ impl Analyzer { None } - fn error(&self, msg: String, ln: usize, col: usize) { - let err_msg = format!("{} (line {}, column {})", msg, ln, col); - let dbg_msg = format!("{}:{}:{}: {}", self.file, ln, col, self.lines[ln - 1]); - let pad = dbg_msg.split(": ").next().unwrap().len() + col + 2; - let caret = format!("{:width$}^", "", width = pad); - let msg = format!("{}\n{}\n{}\n", err_msg, dbg_msg, caret); - Analyzer::abort(&msg); - } - - fn abort(msg: &str) { - let stderr = io::stderr(); - let mut handle = stderr.lock(); - write!( - handle, - "{}{}Semantic error:{} {}", - style::Bold, - color::Fg(color::Red), - style::Reset, - msg, - ) - .unwrap(); - handle.flush().unwrap(); - process::exit(1); - } - fn pause() { let msg = b"[Press Enter to continue]\r"; let mut stdout = stdout(); diff --git a/src/zkas/compiler.rs b/src/zkas/compiler.rs index c462ef113..73180bb9c 100644 --- a/src/zkas/compiler.rs +++ b/src/zkas/compiler.rs @@ -1,8 +1,9 @@ -use std::{io, io::Write, process, str::Chars}; +use std::str::Chars; -use termion::{color, style}; - -use super::ast::{Constants, StatementType, Statements, Witnesses}; +use super::{ + ast::{Constants, StatementType, Statements, Witnesses}, + error::ErrorEmitter, +}; use crate::util::serial::{serialize, VarInt}; /// Version of the binary @@ -11,12 +12,11 @@ pub const BINARY_VERSION: u8 = 1; pub const MAGIC_BYTES: [u8; 4] = [0x0b, 0x00, 0xb1, 0x35]; pub struct Compiler { - file: String, - lines: Vec, constants: Constants, witnesses: Witnesses, statements: Statements, debug_info: bool, + error: ErrorEmitter, } impl Compiler { @@ -30,8 +30,10 @@ impl Compiler { ) -> Self { // For nice error reporting, we'll load everything into a string // vector so we have references to lines. - let lines = source.as_str().lines().map(|x| x.to_string()).collect(); - Compiler { file: filename.to_string(), lines, constants, witnesses, statements, debug_info } + let lines: Vec = source.as_str().lines().map(|x| x.to_string()).collect(); + let error = ErrorEmitter::new("Compiler", filename, lines.clone()); + + Compiler { constants, witnesses, statements, debug_info, error } } pub fn compile(&self) -> Vec { @@ -77,7 +79,7 @@ impl Compiler { continue } - self.error( + self.error.emit( format!("Failed finding a stack reference for `{}`", arg.name), arg.line, arg.column, @@ -104,29 +106,4 @@ impl Compiler { None } - - fn error(&self, msg: String, ln: usize, col: usize) { - let err_msg = format!("{} (line {}, column {})", msg, ln, col); - let dbg_msg = format!("{}:{}:{}: {}", self.file, ln, col, self.lines[ln - 1]); - let pad = dbg_msg.split(": ").next().unwrap().len() + col + 2; - let caret = format!("{:width$}^", "", width = pad); - let msg = format!("{}\n{}\n{}\n", err_msg, dbg_msg, caret); - Compiler::abort(&msg); - } - - fn abort(msg: &str) { - let stderr = io::stderr(); - let mut handle = stderr.lock(); - write!( - handle, - "{}{}Compiler error:{} {}", - style::Bold, - color::Fg(color::Red), - style::Reset, - msg, - ) - .unwrap(); - handle.flush().unwrap(); - process::exit(1); - } } diff --git a/src/zkas/error.rs b/src/zkas/error.rs new file mode 100644 index 000000000..eed3f6947 --- /dev/null +++ b/src/zkas/error.rs @@ -0,0 +1,41 @@ +use std::{io, io::Write, process}; + +use termion::{color, style}; + +pub(super) struct ErrorEmitter { + namespace: String, + file: String, + lines: Vec, +} + +impl ErrorEmitter { + pub fn new(namespace: &str, file: &str, lines: Vec) -> Self { + Self { namespace: namespace.to_string(), file: file.to_string(), lines } + } + + pub fn emit(&self, msg: String, ln: usize, col: usize) { + let err_msg = format!("{} (line{}, column {})", msg, ln, col); + let dbg_msg = format!("{}:{}:{}: {}", self.file, ln, col, self.lines[ln - 1]); + let pad = dbg_msg.split(": ").next().unwrap().len() + col + 2; + let caret = format!("{:width$}^", "", width = pad); + let msg = format!("{}\n{}\n{}\n", err_msg, dbg_msg, caret); + self.abort(&msg); + } + + fn abort(&self, msg: &str) { + let stderr = io::stderr(); + let mut handle = stderr.lock(); + write!( + handle, + "{}{}{} error:{} {}", + style::Bold, + color::Fg(color::Red), + self.namespace, + style::Reset, + msg, + ) + .unwrap(); + handle.flush().unwrap(); + process::exit(1); + } +} diff --git a/src/zkas/lexer.rs b/src/zkas/lexer.rs index 50a18bfbd..1106292c8 100644 --- a/src/zkas/lexer.rs +++ b/src/zkas/lexer.rs @@ -1,6 +1,6 @@ -use std::{io, io::Write, process, str::Chars}; +use std::str::Chars; -use termion::{color, style}; +use super::error::ErrorEmitter; #[derive(Hash, Eq, PartialEq, Clone, Debug)] pub enum TokenType { @@ -33,9 +33,8 @@ impl Token { } pub struct Lexer<'a> { - file: String, - lines: Vec, source: Chars<'a>, + error: ErrorEmitter, } impl<'a> Lexer<'a> { @@ -43,7 +42,9 @@ impl<'a> Lexer<'a> { // For nice error reporting, we'll load everything into a string // vector so we have references to lines. let lines: Vec = source.as_str().lines().map(|x| x.to_string()).collect(); - Lexer { file: filename.to_string(), lines, source } + let error = ErrorEmitter::new("Lexer", filename, lines.clone()); + + Self { source, error } } pub fn lex(self) -> Vec { @@ -78,7 +79,11 @@ impl<'a> Lexer<'a> { if in_string { // TODO: Allow newlines in strings? - self.error(format!("Invalid ending in string `{}`", &strbuf), lineno, column); + self.error.emit( + format!("Invalid ending in string `{}`", &strbuf), + lineno, + column, + ); } in_comment = false; @@ -141,7 +146,7 @@ impl<'a> Lexer<'a> { if c == '"' && !in_string { if in_symbol { - self.error(format!("Illegal char `{}` for symbol", c), lineno, column); + self.error.emit(format!("Illegal char `{}` for symbol", c), lineno, column); } in_string = true; continue @@ -149,7 +154,11 @@ impl<'a> Lexer<'a> { if c == '"' && in_string { if strbuf.is_empty() { - self.error(format!("Invalid ending in string `{}`", &strbuf), lineno, column); + self.error.emit( + format!("Invalid ending in string `{}`", &strbuf), + lineno, + column, + ); } in_string = false; @@ -229,41 +238,16 @@ impl<'a> Lexer<'a> { tokens.push(Token::new("=".to_string(), TokenType::Assign, lineno, column)); continue } - _ => self.error(format!("Invalid token `{}`", c), lineno, column - 1), + _ => self.error.emit(format!("Invalid token `{}`", c), lineno, column - 1), } continue } - self.error(format!("Invalid token `{}`", c), lineno, column - 1); + self.error.emit(format!("Invalid token `{}`", c), lineno, column - 1); } tokens } - - fn error(&self, msg: String, ln: usize, col: usize) { - let err_msg = format!("{} (line {}, column {})", msg, ln, col); - let dbg_msg = format!("{}:{}:{}: {}", self.file, ln, col, self.lines[ln - 1]); - let pad = dbg_msg.split(": ").next().unwrap().len() + col + 2; - let caret = format!("{:width$}^", "", width = pad); - let msg = format!("{}\n{}\n{}\n", err_msg, dbg_msg, caret); - Lexer::abort(&msg); - } - - fn abort(msg: &str) { - let stderr = io::stderr(); - let mut handle = stderr.lock(); - write!( - handle, - "{}{}Lexer error:{} {}", - style::Bold, - color::Fg(color::Red), - style::Reset, - msg, - ) - .unwrap(); - handle.flush().unwrap(); - process::exit(1); - } } fn is_letter(ch: char) -> bool { diff --git a/src/zkas/mod.rs b/src/zkas/mod.rs index e86d9a487..c05e4b267 100644 --- a/src/zkas/mod.rs +++ b/src/zkas/mod.rs @@ -6,6 +6,8 @@ pub mod ast; pub mod compiler; /// Binary decoder pub mod decoder; +/// Error emitter +mod error; /// Lexer module pub mod lexer; /// Language opcodes diff --git a/src/zkas/parser.rs b/src/zkas/parser.rs index ff2c13625..3ac3e455c 100644 --- a/src/zkas/parser.rs +++ b/src/zkas/parser.rs @@ -1,31 +1,32 @@ -use std::{io, io::Write, iter::Peekable, process, str::Chars}; +use std::{iter::Peekable, str::Chars}; use indexmap::IndexMap; use itertools::Itertools; -use termion::{color, style}; use super::{ ast::{ Constant, Constants, Statement, StatementType, Statements, UnparsedConstants, UnparsedWitnesses, Variable, Witness, Witnesses, }, + error::ErrorEmitter, lexer::{Token, TokenType}, opcode::Opcode, types::Type, }; pub struct Parser { - file: String, - lines: Vec, tokens: Vec, + error: ErrorEmitter, } impl Parser { pub fn new(filename: &str, source: Chars, tokens: Vec) -> Self { // For nice error reporting, we'll load everything into a string // vector so we have references to lines. - let lines = source.as_str().lines().map(|x| x.to_string()).collect(); - Parser { file: filename.to_string(), lines, tokens } + let lines: Vec = source.as_str().lines().map(|x| x.to_string()).collect(); + let error = ErrorEmitter::new("Parser", filename, lines.clone()); + + Parser { tokens, error } } pub fn parse(self) -> (Constants, Witnesses, Statements) { @@ -93,7 +94,9 @@ impl Parser { } } - x => self.error(format!("Unknown `{}` proof section", x), t.line, t.column), + x => { + self.error.emit(format!("Unknown `{}` proof section", x), t.line, t.column) + } } } @@ -114,7 +117,7 @@ impl Parser { // TODO: Do we need this? if namespace_found && namespace != constant_tokens[0].token { - self.error( + self.error.emit( format!( "Found `{}` namespace. Expected `{}`.", constant_tokens[0].token, namespace @@ -134,7 +137,7 @@ impl Parser { while let Some((typ, name, comma)) = constants_inner.next_tuple() { if comma.token_type != TokenType::Comma { - self.error( + self.error.emit( "Separator is not a comma".to_string(), comma.line, comma.column, @@ -142,7 +145,7 @@ impl Parser { } if constants_map.contains_key(name.token.as_str()) { - self.error( + self.error.emit( format!( "Section `constant` already contains the token `{}`.", &name.token @@ -164,7 +167,7 @@ impl Parser { // TODO: Do we need this? if namespace_found && namespace != contract_tokens[0].token { - self.error( + self.error.emit( format!( "Found `{}` namespace. Expected `{}`.", contract_tokens[0].token, namespace @@ -184,7 +187,7 @@ impl Parser { while let Some((typ, name, comma)) = contract_inner.next_tuple() { if comma.token_type != TokenType::Comma { - self.error( + self.error.emit( "Separator is not a comma".to_string(), comma.line, comma.column, @@ -192,7 +195,7 @@ impl Parser { } if contract_map.contains_key(name.token.as_str()) { - self.error( + self.error.emit( format!( "Section `contract` already contains the token `{}`.", &name.token @@ -213,7 +216,7 @@ impl Parser { self.check_section_structure("circuit", contract_tokens.clone()); if circuit_tokens[circuit_tokens.len() - 2].token_type != TokenType::Semicolon { - self.error( + self.error.emit( "Circuit section does not end with a semicolon. Would never finish parsing.".to_string(), circuit_tokens[circuit_tokens.len()-2].line, circuit_tokens[circuit_tokens.len()-2].column @@ -222,7 +225,7 @@ impl Parser { // TODO: Do we need this? if namespace_found && namespace != circuit_tokens[0].token { - self.error( + self.error.emit( format!( "Found `{}` namespace. Expected `{}`.", circuit_tokens[0].token, namespace @@ -271,7 +274,7 @@ impl Parser { fn check_section_structure(&self, section: &str, tokens: Vec) { if tokens[0].token_type != TokenType::String { - self.error( + self.error.emit( format!("{} section declaration must start with a naming string.", section), tokens[0].line, tokens[0].column, @@ -279,7 +282,7 @@ impl Parser { } if tokens[1].token_type != TokenType::LeftBrace { - self.error( + self.error.emit( format!( "{} section opening is not correct. Must be opened with a left brace `{{`", section @@ -290,7 +293,7 @@ impl Parser { } if tokens[tokens.len() - 1].token_type != TokenType::RightBrace { - self.error( + self.error.emit( format!( "{} section closing is not correct. Must be closed with a right brace `}}`", section @@ -303,7 +306,7 @@ impl Parser { if (section == "constant" || section == "contract") && tokens[2..tokens.len() - 1].len() % 3 != 0 { - self.error( + self.error.emit( format!( "Invalid number of elements in `{}` section. Must be pairs of `type:name` separated with a comma `,`", section @@ -319,7 +322,7 @@ impl Parser { for (k, v) in ast { if &v.0.token != k { - self.error( + self.error.emit( format!("Constant name `{}` doesn't match token `{}`.", v.0.token, k), v.0.line, v.0.column, @@ -327,7 +330,7 @@ impl Parser { } if v.0.token_type != TokenType::Symbol { - self.error( + self.error.emit( format!("Constant name `{}` is not a symbol.", v.0.token), v.0.line, v.0.column, @@ -335,7 +338,7 @@ impl Parser { } if v.1.token_type != TokenType::Symbol { - self.error( + self.error.emit( format!("Constant type `{}` is not a symbol.", v.1.token), v.1.line, v.1.column, @@ -353,7 +356,7 @@ impl Parser { } x => { - self.error( + self.error.emit( format!("`{}` is an illegal constant type", x), v.1.line, v.1.column, @@ -370,7 +373,7 @@ impl Parser { for (k, v) in ast { if &v.0.token != k { - self.error( + self.error.emit( format!("Witness name `{}` doesn't match token `{}`.", v.0.token, k), v.0.line, v.0.column, @@ -378,7 +381,7 @@ impl Parser { } if v.0.token_type != TokenType::Symbol { - self.error( + self.error.emit( format!("Witness name `{}` is not a symbol.", v.0.token), v.0.line, v.0.column, @@ -386,7 +389,7 @@ impl Parser { } if v.1.token_type != TokenType::Symbol { - self.error( + self.error.emit( format!("Witness type `{}` is not a symbol.", v.1.token), v.1.line, v.1.column, @@ -440,7 +443,11 @@ impl Parser { } x => { - self.error(format!("`{}` is an illegal witness type", x), v.1.line, v.1.column); + self.error.emit( + format!("`{}` is an illegal witness type", x), + v.1.line, + v.1.column, + ); } } } @@ -461,7 +468,7 @@ impl Parser { } } if left_paren != right_paren { - self.error( + self.error.emit( "Incorrect number of left and right parenthesis for statement.".to_string(), statement[0].line, statement[0].column, @@ -508,7 +515,7 @@ impl Parser { } if !parsing { - self.error( + self.error.emit( format!("Illegal token `{}`", next_token.token), next_token.line, next_token.column, @@ -614,7 +621,7 @@ impl Parser { } x => { - self.error( + self.error.emit( format!("Unimplemented function call `{}`", x), token.line, token.column, @@ -635,7 +642,7 @@ impl Parser { ) -> Vec { if let Some(next_token) = iter.peek() { if next_token.token_type != TokenType::LeftParen { - self.error( + self.error.emit( "Invalid function call opening. Must start with a `(`".to_string(), next_token.line, next_token.column, @@ -644,7 +651,7 @@ impl Parser { // Skip the opening parenthesis iter.next(); } else { - self.error("Premature ending of statement".to_string(), token.line, token.column); + self.error.emit("Premature ending of statement".to_string(), token.line, token.column); } // Eat up function arguments @@ -663,7 +670,7 @@ impl Parser { } if sep.token_type != TokenType::Comma { - self.error( + self.error.emit( "Argument separator is not a comma (`,`)".to_string(), sep.line, sep.column, @@ -673,29 +680,4 @@ impl Parser { args } - - fn error(&self, msg: String, ln: usize, col: usize) { - let err_msg = format!("{} (line {}, column {})", msg, ln, col); - let dbg_msg = format!("{}:{}:{}: {}", self.file, ln, col, self.lines[ln - 1]); - let pad = dbg_msg.split(": ").next().unwrap().len() + col + 2; - let caret = format!("{:width$}^", "", width = pad); - let msg = format!("{}\n{}\n{}\n", err_msg, dbg_msg, caret); - Parser::abort(&msg); - } - - fn abort(msg: &str) { - let stderr = io::stderr(); - let mut handle = stderr.lock(); - write!( - handle, - "{}{}Parser error:{} {}", - style::Bold, - color::Fg(color::Red), - style::Reset, - msg, - ) - .unwrap(); - handle.flush().unwrap(); - process::exit(1); - } } From a64820132a2ce261c7c1b734097613c1f0592e76 Mon Sep 17 00:00:00 2001 From: narodnik Date: Sat, 5 Feb 2022 22:27:26 +0100 Subject: [PATCH 32/38] cleanup ProtocolRegistry --- src/net/protocol/protocol_registry.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/net/protocol/protocol_registry.rs b/src/net/protocol/protocol_registry.rs index 69cab868b..8ba1a579d 100644 --- a/src/net/protocol/protocol_registry.rs +++ b/src/net/protocol/protocol_registry.rs @@ -3,16 +3,10 @@ use futures::future::BoxFuture; use std::future::Future; use log::debug; -use super::protocol_base::ProtocolBase; -use std::sync::Arc; - -//use super::protocol_base::ProtocolBasePtr; -use crate::net::{session::SessionBitflag, ChannelPtr, P2pPtr}; - -type ProtocolBasePtr = Arc; +use crate::net::{session::SessionBitflag, ChannelPtr, P2pPtr, protocol::ProtocolBasePtr}; type Constructor = Box< - dyn Fn(ChannelPtr, P2pPtr) -> BoxFuture<'static, Arc> + dyn Fn(ChannelPtr, P2pPtr) -> BoxFuture<'static, ProtocolBasePtr> + Send + Sync, >; @@ -30,11 +24,11 @@ impl ProtocolRegistry { pub async fn register(&self, session_flags: SessionBitflag, constructor: C) where C: 'static + Fn(ChannelPtr, P2pPtr) -> F + Send + Sync, - F: 'static + Future> + Send, + F: 'static + Future + Send, { let constructor = move |channel, p2p| { Box::pin(constructor(channel, p2p)) - as BoxFuture<'static, Arc> + as BoxFuture<'static, ProtocolBasePtr> }; self.protocol_constructors.lock().await.push((session_flags, Box::new(constructor))); } @@ -44,15 +38,15 @@ impl ProtocolRegistry { selector_id: SessionBitflag, channel: ChannelPtr, p2p: P2pPtr, - ) -> Vec> { - let mut protocols: Vec> = Vec::new(); + ) -> Vec { + let mut protocols: Vec = Vec::new(); for (session_flags, construct) in self.protocol_constructors.lock().await.iter() { // Skip protocols that are not registered for this session if selector_id & session_flags == 0 { continue } - let protocol: Arc = + let protocol: ProtocolBasePtr = construct(channel.clone(), p2p.clone()).await; debug!(target: "net", "Attached {}", protocol.name()); protocols.push(protocol) From 714552c660aebc0a4dbc09bf455fd9a74f1d25e8 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 5 Feb 2022 12:54:12 +0100 Subject: [PATCH 33/38] map: fixed rename error on run_app --- bin/map/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 3685566eb..33ac07f9d 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -137,7 +137,7 @@ async fn main() -> Result<()> { .finish(|| { smol::future::block_on(async move { run_rpc(ex2.clone(), model.clone()).await?; - run_model(&mut terminal, model.clone()).await?; + run_app(&mut terminal, model.clone()).await?; drop(signal); Ok::<(), darkfi::Error>(()) }) @@ -182,7 +182,7 @@ async fn poll(client: Map, _model: Model) -> Result<()> { } } -async fn run_model(terminal: &mut Terminal, mut model: Model) -> io::Result<()> { +async fn run_app(terminal: &mut Terminal, mut model: Model) -> io::Result<()> { let mut asi = async_stdin(); terminal.clear()?; @@ -203,7 +203,7 @@ async fn run_model(terminal: &mut Terminal, mut model: Model) -> match k.unwrap() { Key::Char('q') => { terminal.clear()?; - return Ok(()) + return Ok(()); } Key::Char('j') => { model.id_list.next(); From 7244fb850a9cb9bf2e0f73ccb2d6ecd5fb01c63d Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 5 Feb 2022 22:39:30 +0100 Subject: [PATCH 34/38] map: clean up --- bin/map/src/lib.rs | 1 - bin/map/src/main.rs | 2 +- bin/map/src/model.rs | 30 ------------------------------ bin/map/src/types.rs | 2 -- 4 files changed, 1 insertion(+), 34 deletions(-) delete mode 100644 bin/map/src/types.rs diff --git a/bin/map/src/lib.rs b/bin/map/src/lib.rs index 200af0967..c501b6a88 100644 --- a/bin/map/src/lib.rs +++ b/bin/map/src/lib.rs @@ -1,5 +1,4 @@ pub mod model; -pub mod types; pub mod ui; pub use model::{IdList, InfoList, Model, NodeInfo}; diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 33ac07f9d..89d621932 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -203,7 +203,7 @@ async fn run_app(terminal: &mut Terminal, mut model: Model) -> io match k.unwrap() { Key::Char('q') => { terminal.clear()?; - return Ok(()); + return Ok(()) } Key::Char('j') => { model.id_list.next(); diff --git a/bin/map/src/model.rs b/bin/map/src/model.rs index cd1400dc5..d1940962f 100644 --- a/bin/map/src/model.rs +++ b/bin/map/src/model.rs @@ -1,11 +1,4 @@ use tui::widgets::ListState; -//use async_std::sync::Mutex; -//use smol::Timer; -//use std::{collections::HashMap, time::Duration}; - -// make a structure to be able to modify and read them -// protect using a mutex -// arc reference #[derive(Clone)] pub struct Model { @@ -15,19 +8,9 @@ pub struct Model { impl Model { pub fn new(id_list: IdList, info_list: InfoList) -> Model { - //let infos = Vec::new(); - //let ids = Vec::new(); - - //let info_list = InfoList::new(infos); - //let id_list = IdList::new(ids); Model { id_list, info_list } } - // TODO: implement this - //async fn sleep(self, dur: Duration) { - // Timer::after(dur).await; - //} - pub async fn update(mut self, node_vec: Vec) -> Model { let ids = vec![node_vec[0].id.clone()]; @@ -116,15 +99,6 @@ impl InfoList { } } -//impl Default for Model { -// fn default() -> Self { -// Self::new() -// } -//} - -//TODO: made node_id into a HashSet(u32) -// wrap NodeInfo and NodeId in a Mutex - pub type NodeId = u32; #[derive(Clone)] @@ -148,7 +122,3 @@ impl Default for NodeInfo { Self::new() } } - -//pub async fn add_seen(&self, id: u32) { -// self.privmsg_ids.lock().await.insert(id); -//} diff --git a/bin/map/src/types.rs b/bin/map/src/types.rs deleted file mode 100644 index 1ad737b1b..000000000 --- a/bin/map/src/types.rs +++ /dev/null @@ -1,2 +0,0 @@ -//pub type NodeId = String; -//pub type NodeInfo = String; From cc895b2580547462df7309399519030db8dc152a Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sun, 6 Feb 2022 08:14:51 +0100 Subject: [PATCH 35/38] map: made View data structure and cleaned up --- bin/map/src/lib.rs | 2 ++ bin/map/src/main.rs | 39 +++++++++++++++++++------ bin/map/src/notes/list.rs | 56 ------------------------------------ bin/map/src/old/node_info.rs | 30 ------------------- bin/map/src/ui.rs | 16 +++++------ 5 files changed, 40 insertions(+), 103 deletions(-) delete mode 100644 bin/map/src/notes/list.rs delete mode 100644 bin/map/src/old/node_info.rs diff --git a/bin/map/src/lib.rs b/bin/map/src/lib.rs index c501b6a88..abe92bb36 100644 --- a/bin/map/src/lib.rs +++ b/bin/map/src/lib.rs @@ -1,5 +1,7 @@ pub mod model; pub mod ui; +pub mod view; pub use model::{IdList, InfoList, Model, NodeInfo}; pub use ui::ui; +pub use view::{IdListView, InfoListView, View}; diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 89d621932..b7c278fc2 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -18,7 +18,9 @@ use tui::{ use map::{ model::{IdList, InfoList, NodeInfo}, - ui, Model, + ui, + view::{IdListView, InfoListView}, + Model, View, }; struct Map { @@ -187,17 +189,36 @@ async fn run_app(terminal: &mut Terminal, mut model: Model) -> io terminal.clear()?; - model.id_list.state.select(Some(0)); + //model.id_list.state.select(Some(0)); - model.info_list.index = 0; + //model.info_list.index = 0; + + let mut info_vec = Vec::new(); + + for info in model.info_list.infos.clone() { + info_vec.push(info) + } + + let mut id_vec = Vec::new(); + + for id in model.id_list.node_id.clone() { + id_vec.push(id) + } + + let id_list = IdListView::new(id_vec); + let info_list = InfoListView::new(info_vec); + let mut view = View::new(id_list, info_list); + + view.id_list.state.select(Some(0)); + + view.info_list.index = 0; // acquire the mutex // let mut model = model.lock(); loop { - // clone everything terminal.draw(|f| { - ui::ui(f, model.clone()); + ui::ui(f, view.clone()); })?; for k in asi.by_ref().keys() { match k.unwrap() { @@ -206,12 +227,12 @@ async fn run_app(terminal: &mut Terminal, mut model: Model) -> io return Ok(()) } Key::Char('j') => { - model.id_list.next(); - model.info_list.next().await; + view.id_list.next(); + view.info_list.next().await; } Key::Char('k') => { - model.id_list.previous(); - model.info_list.previous().await; + view.id_list.previous(); + view.info_list.previous().await; } _ => (), } diff --git a/bin/map/src/notes/list.rs b/bin/map/src/notes/list.rs deleted file mode 100644 index 6b74dc506..000000000 --- a/bin/map/src/notes/list.rs +++ /dev/null @@ -1,56 +0,0 @@ -use crate::{ - info::{NodeId, NodeInfo}, - ui::render_selected, -}; -use std::collections::HashMap; -use tui::widgets::ListState; - -// TODO: make this just a list -// hashmaps are owned by App -#[derive(Clone)] -pub struct StatefulList { - pub state: ListState, - pub nodes: NodeId, - //pub nodes: HashMap, - //pub node_info: NodeInfo, - //pub index: HashMap, - //pub node_info: InfoScreen, -} - -impl StatefulList { - pub fn new(nodes: NodeId) -> StatefulList { - StatefulList { state: ListState::default(), nodes } - } - - pub fn next(&mut self) { - let i = match self.state.selected() { - Some(i) => { - if i >= self.nodes.id.len() - 1 { - 0 - } else { - i + 1 - } - } - None => 0, - }; - self.state.select(Some(i)); - } - - pub fn previous(&mut self) { - let i = match self.state.selected() { - Some(i) => { - if i == 0 { - self.nodes.id.len() - 1 - } else { - i - 1 - } - } - None => 0, - }; - self.state.select(Some(i)); - } - - pub fn unselect(&mut self) { - self.state.select(None); - } -} diff --git a/bin/map/src/old/node_info.rs b/bin/map/src/old/node_info.rs deleted file mode 100644 index 06ef3eb42..000000000 --- a/bin/map/src/old/node_info.rs +++ /dev/null @@ -1,30 +0,0 @@ -//TODO: made node_id into a HashSet(u32) -// wrap NodeInfo and NodeId in a Mutex - -pub type NodeId = u32; - -#[derive(Clone)] -pub struct NodeInfo { - pub id: String, - pub connections: usize, - pub is_active: bool, - pub last_message: String, -} - -impl NodeInfo { - pub fn new() -> NodeInfo { - let connections = 0; - let is_active = false; - NodeInfo { id: String::new(), connections, is_active, last_message: String::new() } - } -} - -impl Default for NodeInfo { - fn default() -> Self { - Self::new() - } -} - -//pub async fn add_seen(&self, id: u32) { -// self.privmsg_ids.lock().await.insert(id); -//} diff --git a/bin/map/src/ui.rs b/bin/map/src/ui.rs index 31607613e..5006dc184 100644 --- a/bin/map/src/ui.rs +++ b/bin/map/src/ui.rs @@ -1,4 +1,4 @@ -use crate::model::Model; +use crate::view::View; use async_std::sync::{Arc, Mutex}; use tui::{ backend::Backend, @@ -9,14 +9,14 @@ use tui::{ Frame, }; -pub fn ui(f: &mut Frame<'_, B>, mut app: Model) { +pub fn ui(f: &mut Frame<'_, B>, mut view: View) { let slice = Layout::default() .direction(Direction::Horizontal) .margin(2) .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .split(f.size()); - let nodes: Vec = app + let nodes: Vec = view .id_list .node_id .iter() @@ -31,15 +31,15 @@ pub fn ui(f: &mut Frame<'_, B>, mut app: Model) { .highlight_style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD)); // needs to be mutable. could - f.render_stateful_widget(nodes, slice[0], &mut app.id_list.state); + f.render_stateful_widget(nodes, slice[0], &mut view.id_list.state); - let index = app.info_list.index; + let index = view.info_list.index; - render_info(app, f, index, slice); + render_info(view, f, index, slice); } -fn render_info(app: Model, f: &mut Frame<'_, B>, index: usize, slice: Vec) { - let info = &app.info_list.infos; +fn render_info(view: View, f: &mut Frame<'_, B>, index: usize, slice: Vec) { + let info = &view.info_list.infos; let id = &info[index].id; let connections = info[index].connections; let is_active = info[index].is_active; From aa80910d05558d484f0e2a035fd6647040a04e9b Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sun, 6 Feb 2022 08:34:42 +0100 Subject: [PATCH 36/38] map: renamed run_app to render --- bin/map/src/main.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index b7c278fc2..4425136ef 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -139,7 +139,7 @@ async fn main() -> Result<()> { .finish(|| { smol::future::block_on(async move { run_rpc(ex2.clone(), model.clone()).await?; - run_app(&mut terminal, model.clone()).await?; + render(&mut terminal, model.clone()).await?; drop(signal); Ok::<(), darkfi::Error>(()) }) @@ -184,15 +184,11 @@ async fn poll(client: Map, _model: Model) -> Result<()> { } } -async fn run_app(terminal: &mut Terminal, mut model: Model) -> io::Result<()> { +async fn render(terminal: &mut Terminal, model: Model) -> io::Result<()> { let mut asi = async_stdin(); terminal.clear()?; - //model.id_list.state.select(Some(0)); - - //model.info_list.index = 0; - let mut info_vec = Vec::new(); for info in model.info_list.infos.clone() { @@ -206,16 +202,15 @@ async fn run_app(terminal: &mut Terminal, mut model: Model) -> io } let id_list = IdListView::new(id_vec); + let info_list = InfoListView::new(info_vec); + let mut view = View::new(id_list, info_list); view.id_list.state.select(Some(0)); view.info_list.index = 0; - // acquire the mutex - // let mut model = model.lock(); - loop { terminal.draw(|f| { ui::ui(f, view.clone()); @@ -224,7 +219,7 @@ async fn run_app(terminal: &mut Terminal, mut model: Model) -> io match k.unwrap() { Key::Char('q') => { terminal.clear()?; - return Ok(()) + return Ok(()); } Key::Char('j') => { view.id_list.next(); From 1800aa14472f0404506fb025888b31e49e84309b Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sun, 6 Feb 2022 08:57:37 +0100 Subject: [PATCH 37/38] map: wrapped Model objects in a Mutex --- bin/map/src/main.rs | 14 +++--- bin/map/src/model.rs | 102 ++++++++++++++++++++++--------------------- bin/map/src/ui.rs | 1 - 3 files changed, 59 insertions(+), 58 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 4425136ef..9840a2163 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -124,8 +124,8 @@ async fn main() -> Result<()> { let id_list = IdList::new(ids); - //let model = Arc::new(Model::new(id_list, info_list)); - let model = Model::new(id_list, info_list); + let model = Arc::new(Model::new(id_list, info_list)); + //let model = Model::new(id_list, info_list); let nthreads = num_cpus::get(); let (signal, shutdown) = async_channel::unbounded::<()>(); @@ -148,7 +148,7 @@ async fn main() -> Result<()> { result } -async fn run_rpc(ex: Arc>, model: Model) -> Result<()> { +async fn run_rpc(ex: Arc>, model: Arc) -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); ex.spawn(poll(client, model)).detach(); @@ -156,7 +156,7 @@ async fn run_rpc(ex: Arc>, model: Model) -> Result<()> { Ok(()) } -async fn poll(client: Map, _model: Model) -> Result<()> { +async fn poll(client: Map, _model: Arc) -> Result<()> { loop { let reply = client.get_info().await?; @@ -184,20 +184,20 @@ async fn poll(client: Map, _model: Model) -> Result<()> { } } -async fn render(terminal: &mut Terminal, model: Model) -> io::Result<()> { +async fn render(terminal: &mut Terminal, model: Arc) -> io::Result<()> { let mut asi = async_stdin(); terminal.clear()?; let mut info_vec = Vec::new(); - for info in model.info_list.infos.clone() { + for info in model.info_list.infos.lock().await.clone() { info_vec.push(info) } let mut id_vec = Vec::new(); - for id in model.id_list.node_id.clone() { + for id in model.id_list.node_id.lock().await.clone() { id_vec.push(id) } diff --git a/bin/map/src/model.rs b/bin/map/src/model.rs index d1940962f..3407864f1 100644 --- a/bin/map/src/model.rs +++ b/bin/map/src/model.rs @@ -1,6 +1,6 @@ +use async_std::sync::Mutex; use tui::widgets::ListState; -#[derive(Clone)] pub struct Model { pub id_list: IdList, pub info_list: InfoList, @@ -11,17 +11,17 @@ impl Model { Model { id_list, info_list } } - pub async fn update(mut self, node_vec: Vec) -> Model { + pub async fn update(self, node_vec: Vec) -> Model { let ids = vec![node_vec[0].id.clone()]; for id in ids { - self.id_list.node_id.push(id); + self.id_list.node_id.lock().await.push(id); } let id_list = self.id_list; for info in node_vec { - self.info_list.infos.push(info); + self.info_list.infos.lock().await.push(info); } let info_list = self.info_list; @@ -29,74 +29,76 @@ impl Model { } } -#[derive(Clone)] pub struct IdList { - pub state: ListState, - pub node_id: Vec, + pub state: Mutex, + pub node_id: Mutex>, } impl IdList { pub fn new(node_id: Vec) -> IdList { - IdList { state: ListState::default(), node_id } + let node_id = Mutex::new(node_id); + IdList { state: Mutex::new(ListState::default()), node_id } } - pub fn next(&mut self) { - let i = match self.state.selected() { - Some(i) => { - if i >= self.node_id.len() - 1 { - 0 - } else { - i + 1 - } - } - None => 0, - }; - self.state.select(Some(i)); - } + //pub async fn next(&mut self) { + // let i = match self.state.lock().await.selected() { + // Some(i) => { + // if i >= self.node_id.lock().await.len() - 1 { + // 0 + // } else { + // i + 1 + // } + // } + // None => 0, + // }; + // self.state.lock().await.select(Some(i)); + //} - pub fn previous(&mut self) { - let i = match self.state.selected() { - Some(i) => { - if i == 0 { - self.node_id.len() - 1 - } else { - i - 1 - } - } - None => 0, - }; - self.state.select(Some(i)); - } + //pub async fn previous(&mut self) { + // let i = match self.state.lock().await.selected() { + // Some(i) => { + // if i == 0 { + // self.node_id.lock().await.len() - 1 + // } else { + // i - 1 + // } + // } + // None => 0, + // }; + // self.state.lock().await.select(Some(i)); + //} - pub fn unselect(&mut self) { - self.state.select(None); - } + //pub async fn unselect(&mut self) { + // self.state.lock().await.select(None); + //} } -#[derive(Clone)] pub struct InfoList { - pub index: usize, - pub infos: Vec, + pub index: Mutex, + pub infos: Mutex>, } impl InfoList { pub fn new(infos: Vec) -> InfoList { let index = 0; + let index = Mutex::new(index); + let infos = Mutex::new(infos); InfoList { index, infos } } - pub async fn next(&mut self) { - self.index = (self.index + 1) % self.infos.len(); - } + //pub async fn next(&mut self) { + // let index = self.index.lock().await; + // index = (index + 1) % index.len(); + //} - pub async fn previous(&mut self) { - if self.index > 0 { - self.index -= 1; - } else { - self.index = self.infos.len() - 1; - } - } + //pub async fn previous(&mut self) { + // if self.index.lock().await > 0 { + // self.index.lock().await -= 1; + // } else { + // self.index.lock().await = self.infos.lock().await.len() - 1; + // } + //} } pub type NodeId = u32; diff --git a/bin/map/src/ui.rs b/bin/map/src/ui.rs index 5006dc184..764ac496c 100644 --- a/bin/map/src/ui.rs +++ b/bin/map/src/ui.rs @@ -1,5 +1,4 @@ use crate::view::View; -use async_std::sync::{Arc, Mutex}; use tui::{ backend::Backend, layout::{Constraint, Direction, Layout, Rect}, From 3f8768ee69023592275eeef9b1617f2ac3a59369 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sun, 6 Feb 2022 08:58:42 +0100 Subject: [PATCH 38/38] map: cleanup --- bin/map/src/model.rs | 45 -------------------------------------------- bin/map/src/ui.rs | 1 - 2 files changed, 46 deletions(-) diff --git a/bin/map/src/model.rs b/bin/map/src/model.rs index 3407864f1..7321ab700 100644 --- a/bin/map/src/model.rs +++ b/bin/map/src/model.rs @@ -39,38 +39,6 @@ impl IdList { let node_id = Mutex::new(node_id); IdList { state: Mutex::new(ListState::default()), node_id } } - - //pub async fn next(&mut self) { - // let i = match self.state.lock().await.selected() { - // Some(i) => { - // if i >= self.node_id.lock().await.len() - 1 { - // 0 - // } else { - // i + 1 - // } - // } - // None => 0, - // }; - // self.state.lock().await.select(Some(i)); - //} - - //pub async fn previous(&mut self) { - // let i = match self.state.lock().await.selected() { - // Some(i) => { - // if i == 0 { - // self.node_id.lock().await.len() - 1 - // } else { - // i - 1 - // } - // } - // None => 0, - // }; - // self.state.lock().await.select(Some(i)); - //} - - //pub async fn unselect(&mut self) { - // self.state.lock().await.select(None); - //} } pub struct InfoList { @@ -86,19 +54,6 @@ impl InfoList { InfoList { index, infos } } - - //pub async fn next(&mut self) { - // let index = self.index.lock().await; - // index = (index + 1) % index.len(); - //} - - //pub async fn previous(&mut self) { - // if self.index.lock().await > 0 { - // self.index.lock().await -= 1; - // } else { - // self.index.lock().await = self.infos.lock().await.len() - 1; - // } - //} } pub type NodeId = u32; diff --git a/bin/map/src/ui.rs b/bin/map/src/ui.rs index 764ac496c..ab40f4a0b 100644 --- a/bin/map/src/ui.rs +++ b/bin/map/src/ui.rs @@ -29,7 +29,6 @@ pub fn ui(f: &mut Frame<'_, B>, mut view: View) { .block(Block::default().borders(Borders::ALL)) .highlight_style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD)); - // needs to be mutable. could f.render_stateful_widget(nodes, slice[0], &mut view.id_list.state); let index = view.info_list.index;