From b36b5a01e1ba392ee2fc371f8e29f91ab1f7b174 Mon Sep 17 00:00:00 2001 From: msutton Date: Tue, 26 Jul 2022 15:52:27 +0300 Subject: [PATCH 1/2] Move consensus code to consensus lib, add criterion benchmarking --- Cargo.toml | 16 +---------- consensus/Cargo.toml | 23 ++++++++++++++++ consensus/benches/hash_benchmarks.rs | 18 +++++++++++++ consensus/src/lib.rs | 6 +++++ .../src}/model/api/hash.rs | 27 ------------------- .../src}/model/api/mod.rs | 0 .../consensus => consensus/src}/model/mod.rs | 0 .../src}/model/services/mod.rs | 0 .../src}/model/staging.rs | 0 .../src}/model/stores/errors.rs | 0 .../src}/model/stores/mod.rs | 0 .../src}/model/stores/reachability.rs | 2 +- .../src}/processes/Parallel Processing.md | 0 .../src}/processes/mod.rs | 0 .../src}/processes/reachability/extensions.rs | 2 +- .../src}/processes/reachability/inquirer.rs | 2 +- .../src}/processes/reachability/interval.rs | 0 .../src}/processes/reachability/mod.rs | 2 +- .../src}/processes/reachability/reindex.rs | 6 ++--- consensus/tests/integration_tests.rs | 16 +++++++++++ kaspad/Cargo.toml | 6 ++--- kaspad/src/domain/consensus/mod.rs | 2 -- kaspad/src/domain/mod.rs | 2 +- kaspad/src/main.rs | 21 +++------------ src/main.rs | 3 --- wallet-cli/Cargo.toml | 21 --------------- wallet-cli/src/main.rs | 24 ----------------- 27 files changed, 77 insertions(+), 122 deletions(-) create mode 100644 consensus/Cargo.toml create mode 100644 consensus/benches/hash_benchmarks.rs create mode 100644 consensus/src/lib.rs rename {kaspad/src/domain/consensus => consensus/src}/model/api/hash.rs (82%) rename {kaspad/src/domain/consensus => consensus/src}/model/api/mod.rs (100%) rename {kaspad/src/domain/consensus => consensus/src}/model/mod.rs (100%) rename {kaspad/src/domain/consensus => consensus/src}/model/services/mod.rs (100%) rename {kaspad/src/domain/consensus => consensus/src}/model/staging.rs (100%) rename {kaspad/src/domain/consensus => consensus/src}/model/stores/errors.rs (100%) rename {kaspad/src/domain/consensus => consensus/src}/model/stores/mod.rs (100%) rename {kaspad/src/domain/consensus => consensus/src}/model/stores/reachability.rs (97%) rename {kaspad/src/domain/consensus => consensus/src}/processes/Parallel Processing.md (100%) rename {kaspad/src/domain/consensus => consensus/src}/processes/mod.rs (100%) rename {kaspad/src/domain/consensus => consensus/src}/processes/reachability/extensions.rs (97%) rename {kaspad/src/domain/consensus => consensus/src}/processes/reachability/inquirer.rs (93%) rename {kaspad/src/domain/consensus => consensus/src}/processes/reachability/interval.rs (100%) rename {kaspad/src/domain/consensus => consensus/src}/processes/reachability/mod.rs (84%) rename {kaspad/src/domain/consensus => consensus/src}/processes/reachability/reindex.rs (96%) create mode 100644 consensus/tests/integration_tests.rs delete mode 100644 kaspad/src/domain/consensus/mod.rs delete mode 100644 src/main.rs delete mode 100644 wallet-cli/Cargo.toml delete mode 100644 wallet-cli/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 2bb0cf292..b0a63f0c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,22 +1,8 @@ -[package] -name = "kaspa" -version = "0.1.0" -description = "Kaspa" -edition = "2021" -authors = ["Kaspa Developers"] - [workspace] members = [ "core", "wallet", - "wallet-cli", + "consensus", "kaspad", "wasm", ] - - -[dependencies] -tide = "0.16.0" -async-std = { version = "1.8.0", features = ["attributes"] } -serde = { version = "1.0", features = ["derive"] } -# flow-server = {path="../flow-server"} diff --git a/consensus/Cargo.toml b/consensus/Cargo.toml new file mode 100644 index 000000000..8bda82eae --- /dev/null +++ b/consensus/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "consensus" +version = "0.1.0" +description = "Kaspa consensus lib" +edition = "2021" +authors = ["Kaspa developers"] +include = [ +"src/**/*.rs", +] + +[dependencies] +async-std = "1.11.0" +regex = "1" +hex = "0.4.3" +thiserror = "1.0.31" +kaspa-core = {path="../core"} + +[dev-dependencies] +criterion = "0.3.6" + +[[bench]] +name = "hash_benchmarks" +harness = false diff --git a/consensus/benches/hash_benchmarks.rs b/consensus/benches/hash_benchmarks.rs new file mode 100644 index 000000000..f9662a18c --- /dev/null +++ b/consensus/benches/hash_benchmarks.rs @@ -0,0 +1,18 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use std::str::FromStr; + +extern crate consensus; +use consensus::model::api::hash::Hash; + +/// Placeholder for actual benchmarks +pub fn criterion_benchmark(c: &mut Criterion) { + c.bench_function("Hash::from_str", |b| { + let hash_str = "8e40af02265360d59f4ecf9ae9ebf8f00a3118408f5a9cdcbcc9c0f93642f3af"; + b.iter(|| { + Hash::from_str(black_box(hash_str)).unwrap(); + }) + }); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/consensus/src/lib.rs b/consensus/src/lib.rs new file mode 100644 index 000000000..6a4dcbae7 --- /dev/null +++ b/consensus/src/lib.rs @@ -0,0 +1,6 @@ +// Until the codebase stables up, we will have a lot of these -- ignore for now +// TODO: remove this +#![allow(dead_code, unused_variables)] + +pub mod model; +pub mod processes; diff --git a/kaspad/src/domain/consensus/model/api/hash.rs b/consensus/src/model/api/hash.rs similarity index 82% rename from kaspad/src/domain/consensus/model/api/hash.rs rename to consensus/src/model/api/hash.rs index 0bbac5594..5602705e3 100644 --- a/kaspad/src/domain/consensus/model/api/hash.rs +++ b/consensus/src/model/api/hash.rs @@ -124,30 +124,3 @@ mod tests { // println!("{}", hash.to_string()); } } - -#[cfg(all(test, feature = "bench"))] -mod benches { - extern crate test; - use self::test::{black_box, Bencher}; - use super::*; - - #[bench] - pub fn bench_from_str_slow(bh: &mut Bencher) { - bh.iter(|| { - for _ in 0..1000 { - let hash_str = "8e40af02265360d59f4ecf9ae9ebf8f00a3118408f5a9cdcbcc9c0f93642f3af"; - black_box(Hash::from_str_slow(hash_str).unwrap()); - } - }); - } - - #[bench] - pub fn bench_from_str_fast(bh: &mut Bencher) { - bh.iter(|| { - for _ in 0..1000 { - let hash_str = "8e40af02265360d59f4ecf9ae9ebf8f00a3118408f5a9cdcbcc9c0f93642f3af"; - black_box(Hash::from_str(hash_str).unwrap()); - } - }); - } -} diff --git a/kaspad/src/domain/consensus/model/api/mod.rs b/consensus/src/model/api/mod.rs similarity index 100% rename from kaspad/src/domain/consensus/model/api/mod.rs rename to consensus/src/model/api/mod.rs diff --git a/kaspad/src/domain/consensus/model/mod.rs b/consensus/src/model/mod.rs similarity index 100% rename from kaspad/src/domain/consensus/model/mod.rs rename to consensus/src/model/mod.rs diff --git a/kaspad/src/domain/consensus/model/services/mod.rs b/consensus/src/model/services/mod.rs similarity index 100% rename from kaspad/src/domain/consensus/model/services/mod.rs rename to consensus/src/model/services/mod.rs diff --git a/kaspad/src/domain/consensus/model/staging.rs b/consensus/src/model/staging.rs similarity index 100% rename from kaspad/src/domain/consensus/model/staging.rs rename to consensus/src/model/staging.rs diff --git a/kaspad/src/domain/consensus/model/stores/errors.rs b/consensus/src/model/stores/errors.rs similarity index 100% rename from kaspad/src/domain/consensus/model/stores/errors.rs rename to consensus/src/model/stores/errors.rs diff --git a/kaspad/src/domain/consensus/model/stores/mod.rs b/consensus/src/model/stores/mod.rs similarity index 100% rename from kaspad/src/domain/consensus/model/stores/mod.rs rename to consensus/src/model/stores/mod.rs diff --git a/kaspad/src/domain/consensus/model/stores/reachability.rs b/consensus/src/model/stores/reachability.rs similarity index 97% rename from kaspad/src/domain/consensus/model/stores/reachability.rs rename to consensus/src/model/stores/reachability.rs index 9ccbe71f1..79d878d46 100644 --- a/kaspad/src/domain/consensus/model/stores/reachability.rs +++ b/consensus/src/model/stores/reachability.rs @@ -4,7 +4,7 @@ use std::{ }; use super::errors::StoreError; -use crate::domain::consensus::{model::api::hash::Hash, processes::reachability::interval::Interval}; +use crate::{model::api::hash::Hash, processes::reachability::interval::Interval}; type HashArray = Rc>; diff --git a/kaspad/src/domain/consensus/processes/Parallel Processing.md b/consensus/src/processes/Parallel Processing.md similarity index 100% rename from kaspad/src/domain/consensus/processes/Parallel Processing.md rename to consensus/src/processes/Parallel Processing.md diff --git a/kaspad/src/domain/consensus/processes/mod.rs b/consensus/src/processes/mod.rs similarity index 100% rename from kaspad/src/domain/consensus/processes/mod.rs rename to consensus/src/processes/mod.rs diff --git a/kaspad/src/domain/consensus/processes/reachability/extensions.rs b/consensus/src/processes/reachability/extensions.rs similarity index 97% rename from kaspad/src/domain/consensus/processes/reachability/extensions.rs rename to consensus/src/processes/reachability/extensions.rs index cfee357db..2fdb60e33 100644 --- a/kaspad/src/domain/consensus/processes/reachability/extensions.rs +++ b/consensus/src/processes/reachability/extensions.rs @@ -1,4 +1,4 @@ -use crate::domain::consensus::model::{ +use crate::model::{ api::hash::Hash, stores::{errors::StoreResult, reachability::ReachabilityStore}, }; diff --git a/kaspad/src/domain/consensus/processes/reachability/inquirer.rs b/consensus/src/processes/reachability/inquirer.rs similarity index 93% rename from kaspad/src/domain/consensus/processes/reachability/inquirer.rs rename to consensus/src/processes/reachability/inquirer.rs index f07bea95e..bd791e63e 100644 --- a/kaspad/src/domain/consensus/processes/reachability/inquirer.rs +++ b/consensus/src/processes/reachability/inquirer.rs @@ -1,5 +1,5 @@ use super::*; -use crate::domain::consensus::model::{api::hash::Hash, stores::reachability::ReachabilityStore}; +use crate::model::{api::hash::Hash, stores::reachability::ReachabilityStore}; pub fn init(store: &mut dyn ReachabilityStore) -> Result<()> { todo!() diff --git a/kaspad/src/domain/consensus/processes/reachability/interval.rs b/consensus/src/processes/reachability/interval.rs similarity index 100% rename from kaspad/src/domain/consensus/processes/reachability/interval.rs rename to consensus/src/processes/reachability/interval.rs diff --git a/kaspad/src/domain/consensus/processes/reachability/mod.rs b/consensus/src/processes/reachability/mod.rs similarity index 84% rename from kaspad/src/domain/consensus/processes/reachability/mod.rs rename to consensus/src/processes/reachability/mod.rs index fcf3f0513..7d83f8d7f 100644 --- a/kaspad/src/domain/consensus/processes/reachability/mod.rs +++ b/consensus/src/processes/reachability/mod.rs @@ -5,7 +5,7 @@ mod reindex; use thiserror::Error; -use crate::domain::consensus::model::stores::errors::StoreError; +use crate::model::stores::errors::StoreError; #[derive(Error, Debug)] pub enum ReachabilityError { diff --git a/kaspad/src/domain/consensus/processes/reachability/reindex.rs b/consensus/src/processes/reachability/reindex.rs similarity index 96% rename from kaspad/src/domain/consensus/processes/reachability/reindex.rs rename to consensus/src/processes/reachability/reindex.rs index ffa1f1791..48ddf2c55 100644 --- a/kaspad/src/domain/consensus/processes/reachability/reindex.rs +++ b/consensus/src/processes/reachability/reindex.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, VecDeque}; use super::*; -use crate::domain::consensus::model::{api::hash::Hash, stores::reachability::ReachabilityStore}; +use crate::model::{api::hash::Hash, stores::reachability::ReachabilityStore}; const DEFAULT_REINDEX_DEPTH: u64 = 200; const DEFAULT_REINDEX_SLACK: u64 = 1 << 12; @@ -174,9 +174,7 @@ impl<'a> ReindexOperationContext<'a> { #[cfg(test)] mod tests { use super::*; - use crate::domain::consensus::{ - model::stores::reachability::MemoryReachabilityStore, processes::reachability::interval::Interval, - }; + use crate::{model::stores::reachability::MemoryReachabilityStore, processes::reachability::interval::Interval}; struct StoreBuilder<'a> { store: &'a mut dyn ReachabilityStore, diff --git a/consensus/tests/integration_tests.rs b/consensus/tests/integration_tests.rs new file mode 100644 index 000000000..e219b8a7a --- /dev/null +++ b/consensus/tests/integration_tests.rs @@ -0,0 +1,16 @@ +extern crate consensus; + +use consensus::model::api::hash::Hash; +use consensus::processes::reachability::interval; +use std::str::FromStr; + +/// Placeholder for actual integration tests +#[test] +fn integration_test() { + let interval = interval::Interval::maximal(); + println!("{:?}", interval); + + let hash_str = "8e40af02265360d59f4ecf9ae9ebf8f00a3118408f5a9cdcbcc9c0f93642f3af"; + let hash = Hash::from_str(hash_str).unwrap(); + println!("{:?}", hash); +} diff --git a/kaspad/Cargo.toml b/kaspad/Cargo.toml index 719ceb0e7..78eaaac5c 100644 --- a/kaspad/Cargo.toml +++ b/kaspad/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "kaspad" version = "0.1.0" +description = "Kaspa full node daemon" edition = "2021" +authors = ["Kaspa developers"] include = [ "src/**/*.rs", ] @@ -12,6 +14,4 @@ regex = "1" hex = "0.4.3" thiserror = "1.0.31" kaspa-core = {path="../core"} - -[features] -bench = [] +consensus = {path="../consensus"} diff --git a/kaspad/src/domain/consensus/mod.rs b/kaspad/src/domain/consensus/mod.rs deleted file mode 100644 index 141f9ff2a..000000000 --- a/kaspad/src/domain/consensus/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod model; -pub mod processes; diff --git a/kaspad/src/domain/mod.rs b/kaspad/src/domain/mod.rs index 4021407ba..8b1378917 100644 --- a/kaspad/src/domain/mod.rs +++ b/kaspad/src/domain/mod.rs @@ -1 +1 @@ -pub mod consensus; + diff --git a/kaspad/src/main.rs b/kaspad/src/main.rs index fe0d7de37..198a0a91e 100644 --- a/kaspad/src/main.rs +++ b/kaspad/src/main.rs @@ -1,11 +1,6 @@ -#![cfg_attr(all(test, feature = "bench"), feature(test))] -// Until the codebase stables up, we will have a lot of these -- ignore for now -// TODO: remove this -#![allow(dead_code, unused_variables)] - +extern crate consensus; extern crate core; -use std::str::FromStr; use std::sync::atomic::AtomicU64; use std::sync::Arc; @@ -14,9 +9,6 @@ use kaspa_core::*; mod domain; -use domain::consensus::model::api::hash::Hash; -use domain::consensus::processes::reachability::interval; - const SERVICE_THREADS: usize = 1; // if sleep time is < 0, sleep is skipped const EMITTER_SLEEP_TIME_MSEC: i64 = -1; @@ -25,13 +17,6 @@ const EMITTER_SLEEP_TIME_MSEC: i64 = -1; pub fn main() { trace!("Kaspad starting..."); - let interval = interval::Interval::maximal(); - println!("{:?}", interval); - - let hash_str = "8e40af02265360d59f4ecf9ae9ebf8f00a3118408f5a9cdcbcc9c0f93642f3af"; - let hash = Hash::from_str(hash_str).unwrap(); - println!("{:?}", hash); - let core = Arc::new(Core::new()); let signals = Arc::new(signals::Signals::new(core.clone())); signals.init(); @@ -45,13 +30,13 @@ pub fn main() { // monitor thread dumping message counters let monitor = Arc::new(monitor::Monitor::new(send_count.clone(), recv_count.clone())); - let consumer = Arc::new(test_consumer::TestConsumer::new("consumer", recv_count.clone())); + let consumer = Arc::new(test_consumer::TestConsumer::new("consumer", recv_count)); let service = Arc::new(test_service::TestService::new("service", SERVICE_THREADS, consumer.sender().clone())); let emitter = Arc::new(test_emitter::TestEmitter::new( "emitter", EMITTER_SLEEP_TIME_MSEC, service.sender().clone(), - send_count.clone(), + send_count, )); // signals.bind(&core); diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 41f39b5fe..000000000 --- a/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub fn main() { - println!("Hello World (Main Project)"); -} diff --git a/wallet-cli/Cargo.toml b/wallet-cli/Cargo.toml deleted file mode 100644 index 43d3fec13..000000000 --- a/wallet-cli/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "kaspa-wallet-cli" -version = "0.1.0" -edition = "2021" - -[dependencies] - -rand = "0.7.3" -log = "0.4.16" -console = "0.15.0" -wasm-bindgen = "0.2.79" -wasm-bindgen-futures = "0.4.29" -web-sys = { version = "0.3.56", features = ['console'] } -js-sys = "0.3.56" -bs58 = "0.4.0" -async-std = "1.11.0" -regex = "1" -#derivative = "2.2.0" - - -kaspa-core = {path="../core"} diff --git a/wallet-cli/src/main.rs b/wallet-cli/src/main.rs deleted file mode 100644 index 1496c7630..000000000 --- a/wallet-cli/src/main.rs +++ /dev/null @@ -1,24 +0,0 @@ -// use std::sync::Arc; -use kaspa_core::*; -// use kaspa_core::core::Core; - -pub fn main() { - trace!("Kaspa Wallet starting..."); - - // let core = Arc::new(Core::new()); - // let signals = Arc::new(signals::Signals::new(core.clone())); - // signals.init(); - - // let monitor = Arc::new(monitor::Monitor::new()); - // let test_service_a = Arc::new(test_service::TestService::new("test servivce A")); - // let test_service_b = Arc::new(test_service::TestService::new("test servivce B")); - - // // signals.bind(&core); - // core.bind(monitor.clone()); - // core.bind(test_service_a.clone()); - // core.bind(test_service_b.clone()); - - // core.run(); - - trace!("Kaspa wallet is finished..."); -} From d0974e7c3cfd8cf2e8e0b4dff093f66aa9caca8d Mon Sep 17 00:00:00 2001 From: msutton Date: Tue, 26 Jul 2022 15:58:38 +0300 Subject: [PATCH 2/2] Cleanup --- consensus/src/model/api/hash.rs | 9 --------- consensus/src/processes/reachability/reindex.rs | 2 -- 2 files changed, 11 deletions(-) diff --git a/consensus/src/model/api/hash.rs b/consensus/src/model/api/hash.rs index 5602705e3..d7a1550ea 100644 --- a/consensus/src/model/api/hash.rs +++ b/consensus/src/model/api/hash.rs @@ -1,5 +1,4 @@ use hex; -use std::convert::TryInto; use std::fmt::{Debug, Formatter}; use std::mem::size_of; use std::str::FromStr; @@ -67,14 +66,6 @@ impl Hash { pub fn is_default(&self) -> bool { self.eq(&Self::DEFAULT) } - - #[allow(dead_code)] - pub fn from_str_slow(hash_str: &str) -> Result { - match hex::decode(hash_str)?.try_into() { - Ok(bytes) => Ok(Hash(bytes)), - Err(_) => Err(hex::FromHexError::InvalidStringLength), - } - } } #[cfg(test)] diff --git a/consensus/src/processes/reachability/reindex.rs b/consensus/src/processes/reachability/reindex.rs index 48ddf2c55..87adc8140 100644 --- a/consensus/src/processes/reachability/reindex.rs +++ b/consensus/src/processes/reachability/reindex.rs @@ -66,7 +66,6 @@ impl<'a> ReindexOperationContext<'a> { /// and populates self.subtree_sizes with the results. /// It is equivalent to the following recursive implementation: /// - /// ``` /// fn count_subtrees(&mut self, block: Hash) -> Result { /// let mut subtree_size = 0u64; /// for child in self.store.get_children(block)?.iter().cloned() { @@ -75,7 +74,6 @@ impl<'a> ReindexOperationContext<'a> { /// self.subtree_sizes.insert(block, subtree_size + 1); /// Ok(subtree_size + 1) /// } - /// ``` /// /// However, we are expecting (linearly) deep trees, and so a /// recursive stack-based approach is inefficient and will hit