Nomos core (#28)

* Extract block to core crate

* Added linking flag for waku

* Cleanup imports

* Add missing core files

* Create more base mods
This commit is contained in:
Daniel Sanchez 2022-12-13 15:35:11 +01:00 committed by GitHub
parent ae97db3e0f
commit fb3fd6f3b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 99 additions and 48 deletions

4
.cargo/config.toml Normal file
View File

@ -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"]

View File

@ -1,6 +1,7 @@
[workspace] [workspace]
members = [ members = [
"nomos-core",
"nomos-services/log", "nomos-services/log",
"nomos-services/network", "nomos-services/network",
"nomos-services/storage", "nomos-services/storage",

11
nomos-core/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "nomos-core"
version = "0.1.0"
edition = "2021"
authors = [
"Daniel Sanchez Quiros <danielsq@status.im>"
]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

21
nomos-core/src/block.rs Normal file
View File

@ -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<const SIZE: usize>(self) -> [BlockChunk; SIZE] {
// TODO: this is a completely temporary and fake implementation
(0..SIZE)
.map(|i| BlockChunk { index: i as u8 })
.collect::<Vec<_>>()
.try_into()
.expect("This should not fail unless chunking exceed memory limits")
}
}

2
nomos-core/src/crypto.rs Normal file
View File

@ -0,0 +1,2 @@
pub type PublicKey = [u8; 32];
pub type PrivateKey = [u8; 32];

3
nomos-core/src/lib.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod block;
pub mod crypto;
pub mod staking;

View File

@ -0,0 +1 @@
pub type Stake = u64;

View File

@ -12,6 +12,7 @@ rand = "0.8"
overwatch-rs = { git = "https://github.com/logos-co/Overwatch", branch = "main" } overwatch-rs = { git = "https://github.com/logos-co/Overwatch", branch = "main" }
async-trait = "0.1" async-trait = "0.1"
nomos-network = { path = "../network" } nomos-network = { path = "../network" }
nomos-core = { path = "../../nomos-core" }
tokio = { version = "1", features = ["sync"] } tokio = { version = "1", features = ["sync"] }
tokio-stream = "0.1" tokio-stream = "0.1"
futures = "0.3" futures = "0.3"

View File

@ -7,7 +7,17 @@
mod network; mod network;
pub mod overlay; 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 overlay::{Member, Overlay};
use overwatch_rs::services::relay::{OutboundRelay, Relay};
use overwatch_rs::services::{ use overwatch_rs::services::{
handle::ServiceStateHandle, handle::ServiceStateHandle,
relay::NoMessage, relay::NoMessage,
@ -16,15 +26,9 @@ use overwatch_rs::services::{
}; };
// Raw bytes for now, could be a ed25519 public key // 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 // Random seed for each round provided by the protocol
pub type Seed = [u8; 32]; 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; 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<const SIZE: usize>(self) -> [BlockChunk; SIZE] {
// TODO: this is a completely temporary and fake implementation
(0..SIZE)
.map(|i| BlockChunk { index: i as u8 })
.collect::<Vec<_>>()
.try_into()
.expect("This should not fail unless chunking exceed memory limits")
}
}
#[derive(Hash, Eq, PartialEq)] #[derive(Hash, Eq, PartialEq)]
pub struct Approval; pub struct Approval;

View File

@ -8,7 +8,8 @@ use crate::network::{
messages::{ApprovalMsg, ProposalChunkMsg}, messages::{ApprovalMsg, ProposalChunkMsg},
NetworkAdapter, NetworkAdapter,
}; };
use crate::{Approval, BlockChunk, View}; use crate::{Approval, View};
use nomos_core::block::BlockChunk;
use nomos_network::{ use nomos_network::{
backends::waku::{EventKind, NetworkEvent, Waku, WakuBackendMessage}, backends::waku::{EventKind, NetworkEvent, Waku, WakuBackendMessage},
NetworkMsg, NetworkService, NetworkMsg, NetworkService,

View File

@ -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 struct ProposalChunkMsg {
pub chunk: BlockChunk, pub chunk: BlockChunk,

View File

@ -1,9 +1,14 @@
pub mod adapters; pub mod adapters;
mod messages; mod messages;
use crate::network::messages::{ApprovalMsg, ProposalChunkMsg}; // std
use crate::{Approval, BlockChunk, View};
// crates
use futures::Stream; 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::backends::NetworkBackend;
use nomos_network::NetworkService; use nomos_network::NetworkService;
use overwatch_rs::services::relay::OutboundRelay; use overwatch_rs::services::relay::OutboundRelay;

View File

@ -1,6 +1,10 @@
// std
// crates
use rand::{seq::SliceRandom, SeedableRng};
// internal
use super::*; use super::*;
use crate::network::NetworkAdapter; use crate::network::NetworkAdapter;
use rand::{seq::SliceRandom, SeedableRng};
/// View of the tree overlay centered around a specific member /// View of the tree overlay centered around a specific member
pub struct Member<'view, const C: usize> { pub struct Member<'view, const C: usize> {

View File

@ -1,10 +1,13 @@
use super::{Approval, Block, NodeId, View};
#[allow(unused)] #[allow(unused)]
mod committees; mod committees;
// std
// crates
// internal
use super::{Approval, NodeId, View};
use crate::network::NetworkAdapter; use crate::network::NetworkAdapter;
pub use committees::Member; pub use committees::Member;
use nomos_core::block::Block;
// Dissamination overlay, tied to a specific view // Dissamination overlay, tied to a specific view
#[async_trait::async_trait] #[async_trait::async_trait]

View File

@ -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::{ use overwatch_rs::services::{
handle::ServiceStateHandle, handle::ServiceStateHandle,
relay::NoMessage, relay::NoMessage,
state::{NoOperator, NoState}, state::{NoOperator, NoState},
ServiceCore, ServiceData, 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<WorkerGuard>); pub struct Logger(Option<WorkerGuard>);

View File

@ -1,6 +1,13 @@
pub mod backends; pub mod backends;
// std
use std::fmt::{self, Debug};
// crates
use async_trait::async_trait; use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tokio::sync::broadcast;
use tokio::sync::oneshot;
// internal
use backends::NetworkBackend; use backends::NetworkBackend;
use overwatch_rs::services::{ use overwatch_rs::services::{
handle::ServiceStateHandle, handle::ServiceStateHandle,
@ -8,10 +15,6 @@ use overwatch_rs::services::{
state::{NoOperator, ServiceState}, state::{NoOperator, ServiceState},
ServiceCore, ServiceData, ServiceId, ServiceCore, ServiceData, ServiceId,
}; };
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug};
use tokio::sync::broadcast;
use tokio::sync::oneshot;
pub enum NetworkMsg<B: NetworkBackend> { pub enum NetworkMsg<B: NetworkBackend> {
Process(B::Message), Process(B::Message),

View File

@ -1,6 +1,6 @@
// std
use std::collections::HashMap; use std::collections::HashMap;
use std::marker::PhantomData; use std::marker::PhantomData;
// std
// crates // crates
use async_trait::async_trait; use async_trait::async_trait;
use bytes::Bytes; use bytes::Bytes;

View File

@ -8,13 +8,13 @@ use async_trait::async_trait;
use bytes::Bytes; use bytes::Bytes;
use overwatch_rs::services::handle::ServiceStateHandle; use overwatch_rs::services::handle::ServiceStateHandle;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize;
// internal // internal
use backends::StorageBackend; use backends::StorageBackend;
use backends::{StorageSerde, StorageTransaction}; use backends::{StorageSerde, StorageTransaction};
use overwatch_rs::services::relay::RelayMessage; use overwatch_rs::services::relay::RelayMessage;
use overwatch_rs::services::state::{NoOperator, NoState}; use overwatch_rs::services::state::{NoOperator, NoState};
use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId}; use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId};
use serde::Serialize;
/// Storage message that maps to [`StorageBackend`] trait /// Storage message that maps to [`StorageBackend`] trait
pub enum StorageMsg<Backend: StorageBackend> { pub enum StorageMsg<Backend: StorageBackend> {