Merge pull request #5 from vacp2p/Pravdyvy/networking-and-consensus-mocks

Consensus and Networking mocks
This commit is contained in:
Moudy 2024-10-07 13:47:44 +02:00 committed by GitHub
commit 80c656f780
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 127 additions and 2 deletions

1
Cargo.lock generated
View File

@ -429,6 +429,7 @@ dependencies = [
"networking",
"serde",
"serde_json",
"tokio",
]
[[package]]

View File

@ -9,6 +9,7 @@ serde_json.workspace = true
env_logger.workspace = true
log.workspace = true
serde.workspace = true
tokio.workspace = true
[dependencies.networking]
path = "../networking"

View File

@ -1 +1,22 @@
//ToDo: Add consensus module
use std::sync::Arc;
use networking::peer_manager::PeerManager;
use tokio::sync::Mutex;
#[derive(Debug)]
///Entrypoint to consensus.
/// Manages consensus protocol.
pub struct ConsensusManager {
pub peer_manager: Arc<Mutex<PeerManager>>,
}
impl ConsensusManager {
pub fn new(peer_manager: Arc<Mutex<PeerManager>>) -> Self {
Self { peer_manager }
}
//ToDo: change block from generic value into struct, when data block will be defined
pub fn vote(&self, _block: serde_json::Value) -> bool {
todo!()
}
}

View File

@ -1 +1,5 @@
//ToDo: Add networking module
pub mod network_protocol;
pub mod peer;
pub mod peer_manager;
pub mod rate_limiter;
pub mod tcp;

View File

@ -0,0 +1,19 @@
#[derive(Debug)]
pub enum MessageKind {}
pub type PeerId = u64;
pub type PeerDistance = u32;
#[derive(Debug)]
pub struct PeerAddr {
pub id: PeerId,
//Probably will be socket address in the future
pub addr: String,
}
#[derive(Debug)]
///Structure, which contains all necessary fields for handshake
pub struct Handshake {}
#[derive(Debug)]
pub enum HandshakeFailedReason {}

18
networking/src/peer.rs Normal file
View File

@ -0,0 +1,18 @@
use crate::{
network_protocol::{HandshakeFailedReason, PeerAddr},
tcp::Connection,
};
#[derive(Debug)]
/// Structure, which stores all of the peer interaction data.
/// Created at per-peer connection basis at `PeerManager`
pub struct Peer {
pub connection: Connection,
pub peer_addr: PeerAddr,
}
impl Peer {
pub fn handshake(&mut self) -> Result<(), HandshakeFailedReason> {
todo!();
}
}

View File

@ -0,0 +1,20 @@
use anyhow::Result;
use crate::{network_protocol::PeerId, peer::Peer};
#[derive(Debug)]
///Entrypoint to network module.
/// Manages connections with peers in network
pub struct PeerManager {
pub my_peer_id: PeerId,
}
impl PeerManager {
pub async fn start_peer_manager(_num_threads: u8, my_peer_id: PeerId) -> Result<Self> {
Ok(Self { my_peer_id })
}
pub async fn connect(&self, _peer_id: PeerId) -> Peer {
todo!()
}
}

View File

@ -0,0 +1,16 @@
use std::collections::HashMap;
use crate::network_protocol::MessageKind;
#[derive(Debug)]
/// Object responsible to manage the rate limits of all network messages
/// for a single connection/peer.
pub struct RateLimiter {
pub limits: HashMap<MessageKind, u64>,
}
impl RateLimiter {
pub fn is_allowed(&self, _message: MessageKind) -> bool {
todo!();
}
}

11
networking/src/tcp.rs Normal file
View File

@ -0,0 +1,11 @@
use crate::network_protocol::PeerAddr;
#[derive(Debug)]
///Structure, representing peer connection
pub struct Connection {}
#[derive(Debug)]
pub enum ConnectionType {
Inbound { conn: Connection },
Outbound { conn: Connection, peer: PeerAddr },
}

View File

@ -1,7 +1,12 @@
use std::sync::Arc;
use anyhow::Result;
use consensus::ConsensusManager;
use log::info;
use networking::peer_manager::PeerManager;
use node_rpc::new_http_server;
use rpc_primitives::RpcConfig;
use tokio::sync::Mutex;
pub async fn main_runner() -> Result<()> {
env_logger::init();
@ -11,6 +16,14 @@ pub async fn main_runner() -> Result<()> {
let _http_server_handle = http_server.handle();
tokio::spawn(http_server);
let peer_manager = PeerManager::start_peer_manager(4, 0).await?;
info!("Peer manager mock started");
let peer_manager_shared = Arc::new(Mutex::new(peer_manager));
let _consensus_manager = ConsensusManager::new(peer_manager_shared.clone());
info!("Consensus manger mock started");
#[allow(clippy::empty_loop)]
loop {
//ToDo: Insert activity into main loop

View File

@ -28,6 +28,7 @@ impl serde::Serialize for Version {
impl<'de> serde::Deserialize<'de> for Version {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct VersionVisitor;
#[allow(clippy::needless_lifetimes)]
impl<'de> Visitor<'de> for VersionVisitor {
type Value = Version;