diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..5bc05eb0 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,4 @@ +[target.'cfg(target_os = "macos")'] +# when using osx, we need to link against some golang libraries, it did just work with this missing flags +# from: https://github.com/golang/go/issues/42459 +rustflags = ["-C", "link-args=-framework CoreFoundation -framework Security"] \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 73a0edfd..1b81d9d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ + "nomos-core", "nomos-services/log", "nomos-services/network", "nomos-services/storage", diff --git a/nomos-core/Cargo.toml b/nomos-core/Cargo.toml new file mode 100644 index 00000000..ea193c62 --- /dev/null +++ b/nomos-core/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "nomos-core" +version = "0.1.0" +edition = "2021" +authors = [ + "Daniel Sanchez Quiros " +] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/nomos-core/src/block.rs b/nomos-core/src/block.rs new file mode 100644 index 00000000..52b3af66 --- /dev/null +++ b/nomos-core/src/block.rs @@ -0,0 +1,21 @@ +/// A block +#[derive(Clone)] +pub struct Block; + +/// A block chunk, N pieces are necessary to reconstruct the full block +#[derive(Clone, Copy, Debug)] +pub struct BlockChunk { + pub index: u8, +} + +impl Block { + /// Fake implementation of erasure coding protocol + pub fn chunk(self) -> [BlockChunk; SIZE] { + // TODO: this is a completely temporary and fake implementation + (0..SIZE) + .map(|i| BlockChunk { index: i as u8 }) + .collect::>() + .try_into() + .expect("This should not fail unless chunking exceed memory limits") + } +} diff --git a/nomos-core/src/crypto.rs b/nomos-core/src/crypto.rs new file mode 100644 index 00000000..416ea347 --- /dev/null +++ b/nomos-core/src/crypto.rs @@ -0,0 +1,2 @@ +pub type PublicKey = [u8; 32]; +pub type PrivateKey = [u8; 32]; diff --git a/nomos-core/src/lib.rs b/nomos-core/src/lib.rs new file mode 100644 index 00000000..c4198d78 --- /dev/null +++ b/nomos-core/src/lib.rs @@ -0,0 +1,3 @@ +pub mod block; +pub mod crypto; +pub mod staking; diff --git a/nomos-core/src/staking.rs b/nomos-core/src/staking.rs new file mode 100644 index 00000000..0a41fe40 --- /dev/null +++ b/nomos-core/src/staking.rs @@ -0,0 +1 @@ +pub type Stake = u64; diff --git a/nomos-services/consensus/Cargo.toml b/nomos-services/consensus/Cargo.toml index ed4ffa9c..c4fd7262 100644 --- a/nomos-services/consensus/Cargo.toml +++ b/nomos-services/consensus/Cargo.toml @@ -12,6 +12,7 @@ rand = "0.8" overwatch-rs = { git = "https://github.com/logos-co/Overwatch", branch = "main" } async-trait = "0.1" nomos-network = { path = "../network" } +nomos-core = { path = "../../nomos-core" } tokio = { version = "1", features = ["sync"] } tokio-stream = "0.1" futures = "0.3" diff --git a/nomos-services/consensus/src/lib.rs b/nomos-services/consensus/src/lib.rs index 0e5c34b0..e95b4bbc 100644 --- a/nomos-services/consensus/src/lib.rs +++ b/nomos-services/consensus/src/lib.rs @@ -7,7 +7,17 @@ mod network; pub mod overlay; +// std +use std::collections::{BTreeMap, HashSet}; +// crates +// internal +use crate::network::NetworkAdapter; +use nomos_core::block::Block; +use nomos_core::crypto::PublicKey; +use nomos_core::staking::Stake; +use nomos_network::NetworkService; use overlay::{Member, Overlay}; +use overwatch_rs::services::relay::{OutboundRelay, Relay}; use overwatch_rs::services::{ handle::ServiceStateHandle, relay::NoMessage, @@ -16,15 +26,9 @@ use overwatch_rs::services::{ }; // Raw bytes for now, could be a ed25519 public key -pub type NodeId = [u8; 32]; +pub type NodeId = PublicKey; // Random seed for each round provided by the protocol pub type Seed = [u8; 32]; -pub type Stake = u64; - -use crate::network::NetworkAdapter; -use nomos_network::NetworkService; -use overwatch_rs::services::relay::{OutboundRelay, Relay}; -use std::collections::{BTreeMap, HashSet}; const COMMITTEE_SIZE: usize = 1; @@ -107,28 +111,6 @@ impl ViewGenerator { } } -/// A block -#[derive(Clone)] -pub struct Block; - -/// A block chunk, N pieces are necessary to reconstruct the full block -#[derive(Clone, Copy, Debug)] -pub struct BlockChunk { - index: u8, -} - -impl Block { - /// Fake implementation of erasure coding protocol - pub fn chunk(self) -> [BlockChunk; SIZE] { - // TODO: this is a completely temporary and fake implementation - (0..SIZE) - .map(|i| BlockChunk { index: i as u8 }) - .collect::>() - .try_into() - .expect("This should not fail unless chunking exceed memory limits") - } -} - #[derive(Hash, Eq, PartialEq)] pub struct Approval; diff --git a/nomos-services/consensus/src/network/adapters/waku.rs b/nomos-services/consensus/src/network/adapters/waku.rs index 0164a30d..8a827e20 100644 --- a/nomos-services/consensus/src/network/adapters/waku.rs +++ b/nomos-services/consensus/src/network/adapters/waku.rs @@ -8,7 +8,8 @@ use crate::network::{ messages::{ApprovalMsg, ProposalChunkMsg}, NetworkAdapter, }; -use crate::{Approval, BlockChunk, View}; +use crate::{Approval, View}; +use nomos_core::block::BlockChunk; use nomos_network::{ backends::waku::{EventKind, NetworkEvent, Waku, WakuBackendMessage}, NetworkMsg, NetworkService, diff --git a/nomos-services/consensus/src/network/messages.rs b/nomos-services/consensus/src/network/messages.rs index db35a74d..c3d5b21e 100644 --- a/nomos-services/consensus/src/network/messages.rs +++ b/nomos-services/consensus/src/network/messages.rs @@ -1,4 +1,10 @@ -use crate::{Approval, BlockChunk, NodeId}; +// std + +// crates + +// internal +use crate::{Approval, NodeId}; +use nomos_core::block::BlockChunk; pub struct ProposalChunkMsg { pub chunk: BlockChunk, diff --git a/nomos-services/consensus/src/network/mod.rs b/nomos-services/consensus/src/network/mod.rs index 31bdc8dc..27df1cd1 100644 --- a/nomos-services/consensus/src/network/mod.rs +++ b/nomos-services/consensus/src/network/mod.rs @@ -1,9 +1,14 @@ pub mod adapters; mod messages; -use crate::network::messages::{ApprovalMsg, ProposalChunkMsg}; -use crate::{Approval, BlockChunk, View}; +// std + +// crates use futures::Stream; +// internal +use crate::network::messages::{ApprovalMsg, ProposalChunkMsg}; +use crate::{Approval, View}; +use nomos_core::block::BlockChunk; use nomos_network::backends::NetworkBackend; use nomos_network::NetworkService; use overwatch_rs::services::relay::OutboundRelay; diff --git a/nomos-services/consensus/src/overlay/committees.rs b/nomos-services/consensus/src/overlay/committees.rs index 8b933f0e..32ff322d 100644 --- a/nomos-services/consensus/src/overlay/committees.rs +++ b/nomos-services/consensus/src/overlay/committees.rs @@ -1,6 +1,10 @@ +// std + +// crates +use rand::{seq::SliceRandom, SeedableRng}; +// internal use super::*; use crate::network::NetworkAdapter; -use rand::{seq::SliceRandom, SeedableRng}; /// View of the tree overlay centered around a specific member pub struct Member<'view, const C: usize> { diff --git a/nomos-services/consensus/src/overlay/mod.rs b/nomos-services/consensus/src/overlay/mod.rs index 6a45c8b3..dfe01135 100644 --- a/nomos-services/consensus/src/overlay/mod.rs +++ b/nomos-services/consensus/src/overlay/mod.rs @@ -1,10 +1,13 @@ -use super::{Approval, Block, NodeId, View}; - #[allow(unused)] mod committees; +// std +// crates +// internal +use super::{Approval, NodeId, View}; use crate::network::NetworkAdapter; pub use committees::Member; +use nomos_core::block::Block; // Dissamination overlay, tied to a specific view #[async_trait::async_trait] diff --git a/nomos-services/log/src/lib.rs b/nomos-services/log/src/lib.rs index 7ba7583d..eb303cb2 100644 --- a/nomos-services/log/src/lib.rs +++ b/nomos-services/log/src/lib.rs @@ -1,15 +1,18 @@ +// std +use std::net::SocketAddr; +use std::path::PathBuf; +// crates +use serde::{Deserialize, Serialize}; +use tracing::Level; +use tracing_appender::non_blocking::WorkerGuard; +use tracing_subscriber::{filter::LevelFilter, prelude::*}; +// internal use overwatch_rs::services::{ handle::ServiceStateHandle, relay::NoMessage, state::{NoOperator, NoState}, ServiceCore, ServiceData, }; -use serde::{Deserialize, Serialize}; -use std::net::SocketAddr; -use std::path::PathBuf; -use tracing::Level; -use tracing_appender::non_blocking::WorkerGuard; -use tracing_subscriber::{filter::LevelFilter, prelude::*}; pub struct Logger(Option); diff --git a/nomos-services/network/src/lib.rs b/nomos-services/network/src/lib.rs index c6f5c6fd..e4aae349 100644 --- a/nomos-services/network/src/lib.rs +++ b/nomos-services/network/src/lib.rs @@ -1,6 +1,13 @@ pub mod backends; +// std +use std::fmt::{self, Debug}; +// crates use async_trait::async_trait; +use serde::{Deserialize, Serialize}; +use tokio::sync::broadcast; +use tokio::sync::oneshot; +// internal use backends::NetworkBackend; use overwatch_rs::services::{ handle::ServiceStateHandle, @@ -8,10 +15,6 @@ use overwatch_rs::services::{ state::{NoOperator, ServiceState}, ServiceCore, ServiceData, ServiceId, }; -use serde::{Deserialize, Serialize}; -use std::fmt::{self, Debug}; -use tokio::sync::broadcast; -use tokio::sync::oneshot; pub enum NetworkMsg { Process(B::Message), diff --git a/nomos-services/storage/src/backends/mock.rs b/nomos-services/storage/src/backends/mock.rs index 5d142d56..368d841d 100644 --- a/nomos-services/storage/src/backends/mock.rs +++ b/nomos-services/storage/src/backends/mock.rs @@ -1,6 +1,6 @@ +// std use std::collections::HashMap; use std::marker::PhantomData; -// std // crates use async_trait::async_trait; use bytes::Bytes; diff --git a/nomos-services/storage/src/lib.rs b/nomos-services/storage/src/lib.rs index 5888e427..53b1a099 100644 --- a/nomos-services/storage/src/lib.rs +++ b/nomos-services/storage/src/lib.rs @@ -8,13 +8,13 @@ use async_trait::async_trait; use bytes::Bytes; use overwatch_rs::services::handle::ServiceStateHandle; use serde::de::DeserializeOwned; +use serde::Serialize; // internal use backends::StorageBackend; use backends::{StorageSerde, StorageTransaction}; use overwatch_rs::services::relay::RelayMessage; use overwatch_rs::services::state::{NoOperator, NoState}; use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId}; -use serde::Serialize; /// Storage message that maps to [`StorageBackend`] trait pub enum StorageMsg {