Commit ad7f960c authored by Éloïs's avatar Éloïs

[ref] network/lib.rs code breakdown

parent 7d7dafac
......@@ -43,7 +43,10 @@ use dubp_documents::DUBPDocument;
use duniter_dal::dal_event::DALEvent;
use duniter_dal::dal_requests::{DALRequest, DALResponse};
use duniter_module::*;
use duniter_network::{NetworkEvent, NetworkResponse, OldNetworkRequest};
use duniter_network::{
events::NetworkEvent,
requests::{NetworkResponse, OldNetworkRequest},
};
use dup_crypto::hashs::Hash;
use dup_crypto::keys::Sig;
use durs_network_documents::network_endpoint::EndpointEnum;
......
// Copyright (C) 2018 The Duniter Project Developers.
// Copyright (C) 2018 The Durs Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -46,12 +46,14 @@ use dubp_documents::{blockstamp::Blockstamp, BlockHash, BlockId};
use duniter_module::*;
use durs_network_documents::network_endpoint::ApiFeatures;
use durs_network_documents::network_head::NetworkHead;
use durs_network_documents::network_peer::PeerCard;
use durs_network_documents::*;
use std::fmt::Debug;
use std::ops::Deref;
use std::sync::mpsc;
pub mod documents;
pub mod events;
pub mod requests;
/// ApiModule
pub trait ApiModule<DC: DuniterConf, M: ModuleMessage>: DuniterModule<DC, M> {
/// Parsing error
......@@ -99,79 +101,6 @@ pub struct SyncEndpoint {
/// Trait to be implemented by the configuration object of the module managing the inter-node network.
pub trait NetworkConf: Debug + Copy + Clone + PartialEq {}
#[derive(Debug, Clone)]
/// Block v10 in network format (Some events require a blockchain access to reconstitute the corresponding document)
pub struct NetworkBlockV10 {
/// Uncompleted block document
pub uncompleted_block_doc: BlockDocument,
/// revoked
pub revoked: Vec<serde_json::Value>,
/// certifications
pub certifications: Vec<serde_json::Value>,
}
#[derive(Debug, Clone)]
/// Block in network format (Some events require a blockchain access to reconstitute the corresponding document)
pub enum NetworkBlock {
/// Block V1
V10(Box<NetworkBlockV10>),
/// Block V11
V11(),
}
impl NetworkBlock {
/// Return uncompleted block document
pub fn uncompleted_block_doc(&self) -> BlockDocument {
match *self {
NetworkBlock::V10(ref network_block_v10) => {
network_block_v10.deref().uncompleted_block_doc.clone()
}
_ => panic!("Block version not supported !"),
}
}
/// Return blockstamp
pub fn blockstamp(&self) -> Blockstamp {
match *self {
NetworkBlock::V10(ref network_block_v10) => {
network_block_v10.deref().uncompleted_block_doc.blockstamp()
}
_ => panic!("Block version not supported !"),
}
}
/// Return previous blockstamp
pub fn previous_blockstamp(&self) -> Blockstamp {
match *self {
NetworkBlock::V10(ref network_block_v10) => Blockstamp {
id: BlockId(network_block_v10.deref().uncompleted_block_doc.number.0 - 1),
hash: BlockHash(
network_block_v10
.deref()
.uncompleted_block_doc
.previous_hash,
),
},
_ => panic!("Block version not supported !"),
}
}
}
#[derive(Debug, Clone)]
/// Network Document
pub enum BlockchainDocument {
/// Network Block
Block(NetworkBlock),
/// Identity Document
Identity(Box<IdentityDocument>),
/// Membership Document
Membership(Box<MembershipDocument>),
/// Certification Document
Certification(Box<CertificationDocument>),
/// Revocation Document
Revocation(Box<RevocationDocument>),
/// Transaction Document
Transaction(Box<TransactionDocument>),
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
/// Type returned when the network module fails to determine the current network consensus
pub enum NetworkConsensusError {
......@@ -180,100 +109,3 @@ pub enum NetworkConsensusError {
/// The network module does not determine consensus, there is most likely a fork
Fork(),
}
#[derive(Debug, Copy, Clone)]
/// Type containing a request addressed to the network module
pub enum OldNetworkRequest {
/// Get a current block of a specific node
GetCurrent(ModuleReqFullId, NodeFullId),
//GetBlock(NodeFullId, u64),
/// Get a blocks chunk from specified node
GetBlocks(ModuleReqFullId, NodeFullId, u32, u32),
/// Get pending wot documents from specified node
GetRequirementsPending(ModuleReqFullId, NodeFullId, u32),
/// Obtain the current network consensus
GetConsensus(ModuleReqFullId),
/// Getting the heads cache
GetHeadsCache(ModuleReqFullId),
/// Get a list of known endpoints
GetEndpoints(ModuleReqFullId),
}
impl OldNetworkRequest {
/// Get request full identitifier
pub fn get_req_full_id(&self) -> ModuleReqFullId {
match *self {
OldNetworkRequest::GetCurrent(ref req_id, _)
| OldNetworkRequest::GetBlocks(ref req_id, _, _, _)
| OldNetworkRequest::GetRequirementsPending(ref req_id, _, _)
| OldNetworkRequest::GetConsensus(ref req_id)
| OldNetworkRequest::GetHeadsCache(ref req_id)
| OldNetworkRequest::GetEndpoints(ref req_id) => *req_id,
}
}
/// Get request identitifier
pub fn get_req_id(&self) -> ModuleReqId {
self.get_req_full_id().1
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
/// Type returned when the network module does not get a satisfying answer to a request
pub enum OldNetworkRequestError {
/// Receiving an invalid format response
WrongFormat(),
/// Unknow error
UnknowError(),
/// No response received
NoResponse(),
/// Unable to reach the target node
ReceiverUnreachable(),
}
#[derive(Debug, Clone)]
/// Type containing the response to a network request
pub enum NetworkResponse {
/// CurrentBlock
CurrentBlock(ModuleReqFullId, NodeFullId, Box<NetworkBlock>),
/// Block
Block(ModuleReqFullId, NodeFullId, Box<NetworkBlock>),
/// Chunk
Chunk(ModuleReqFullId, NodeFullId, Vec<Box<NetworkBlock>>),
/// PendingDocuments
PendingDocuments(ModuleReqFullId, Vec<BlockchainDocument>),
/// Consensus
Consensus(ModuleReqFullId, Result<Blockstamp, NetworkConsensusError>),
/// HeadsCache
HeadsCache(ModuleReqFullId, Box<NetworkHead>),
}
impl NetworkResponse {
/// Get request full identifier
pub fn get_req_full_id(&self) -> ModuleReqFullId {
match *self {
NetworkResponse::CurrentBlock(ref req_id, _, _)
| NetworkResponse::Block(ref req_id, _, _)
| NetworkResponse::Chunk(ref req_id, _, _)
| NetworkResponse::PendingDocuments(ref req_id, _)
| NetworkResponse::Consensus(ref req_id, _)
| NetworkResponse::HeadsCache(ref req_id, _) => *req_id,
}
}
/// Get request identifier
pub fn get_req_id(&self) -> ModuleReqId {
self.get_req_full_id().1
}
}
#[derive(Debug, Clone)]
/// Type containing a network event, each time a network event occurs it's relayed to all modules
pub enum NetworkEvent {
/// A connection has changed state(`u32` is the new state, `Option<String>` est l'uid du noeud)
ConnectionStateChange(NodeFullId, u32, Option<String>, String),
/// Receiving Pending Documents
ReceiveDocuments(Vec<BlockchainDocument>),
/// Receipt of peer cards
ReceivePeers(Vec<PeerCard>),
/// Receiving heads
ReceiveHeads(Vec<NetworkHead>),
}
......@@ -20,7 +20,7 @@ use dubp_documents::Document;
use dubp_documents::{BlockHash, BlockId, Blockstamp, PreviousBlockstamp};
use duniter_dal::block::DALBlock;
use duniter_dal::*;
use duniter_network::NetworkBlock;
use duniter_network::documents::NetworkBlock;
use dup_crypto::keys::*;
use *;
......
......@@ -75,7 +75,9 @@ use duniter_dal::*;
use duniter_message::*;
use duniter_module::*;
use duniter_network::{
BlockchainDocument, NetworkBlock, NetworkEvent, NetworkResponse, OldNetworkRequest,
documents::{BlockchainDocument, NetworkBlock},
events::NetworkEvent,
requests::{NetworkResponse, OldNetworkRequest},
};
use dup_crypto::keys::*;
use durs_network_documents::NodeFullId;
......
......@@ -24,7 +24,7 @@ use dubp_documents::{BlockHash, BlockId};
use duniter_dal::currency_params::CurrencyParameters;
use duniter_dal::writers::requests::*;
use duniter_dal::ForkId;
use duniter_network::NetworkBlock;
use duniter_network::documents::NetworkBlock;
use dup_crypto::hashs::Hash;
use dup_crypto::keys::*;
use durs_wot::NodeId;
......
......@@ -24,7 +24,7 @@ use dubp_documents::v10::*;
use dubp_documents::CurrencyName;
use dubp_documents::DocumentBuilder;
use dubp_documents::{BlockHash, BlockId, Blockstamp};
use duniter_network::{NetworkBlock, NetworkBlockV10};
use duniter_network::documents::{NetworkBlock, NetworkBlockV10};
use dup_crypto::hashs::Hash;
use dup_crypto::keys::*;
use std::str::FromStr;
......
......@@ -52,7 +52,7 @@ use duniter_conf::DuRsConf;
use duniter_dal::dal_event::DALEvent;
use duniter_message::*;
use duniter_module::*;
use duniter_network::NetworkEvent;
use duniter_network::events::NetworkEvent;
use durs_network_documents::network_head::NetworkHead;
use durs_network_documents::NodeFullId;
use std::collections::HashMap;
......
......@@ -71,6 +71,9 @@ use duniter_dal::dal_event::DALEvent;
use duniter_dal::dal_requests::{DALReqBlockchain, DALRequest, DALResBlockchain, DALResponse};
use duniter_message::*;
use duniter_module::*;
use duniter_network::documents::*;
use duniter_network::events::*;
use duniter_network::requests::*;
use duniter_network::*;
use dup_crypto::keys::*;
use durs_network_documents::network_endpoint::*;
......@@ -930,8 +933,9 @@ mod tests {
use super::*;
use dubp_documents::v10::BlockDocument;
use duniter_module::DuniterModule;
use duniter_network::NetworkBlock;
use duniter_network::documents::NetworkBlock;
use dup_crypto::keys::PublicKey;
use durs_network_documents::network_endpoint::{EndpointEnum, NetworkEndpointApi};
use std::fs;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
......
......@@ -8,7 +8,7 @@ use dubp_documents::v10::membership::*;
use dubp_documents::v10::BlockDocument;
use dubp_documents::CurrencyName;
use dubp_documents::{BlockHash, BlockId};
use duniter_network::{NetworkBlock, NetworkBlockV10};
use duniter_network::documents::{NetworkBlock, NetworkBlockV10};
use dup_crypto::hashs::Hash;
use dup_crypto::keys::*;
use std::str::FromStr;
......
use constants::*;
use duniter_module::ModuleReqId;
use duniter_network::BlockchainDocument;
use duniter_network::documents::BlockchainDocument;
use dup_crypto::keys::*;
use durs_network_documents::network_endpoint::{EndpointV1, NetworkEndpointApi};
use durs_network_documents::NodeId;
......
......@@ -3,7 +3,7 @@ extern crate dup_crypto;
extern crate serde;
extern crate serde_json;
use duniter_network::OldNetworkRequest;
use duniter_network::requests::OldNetworkRequest;
pub fn network_request_to_json(request: &OldNetworkRequest) -> serde_json::Value {
let (request_id, request_type, request_params) = match *request {
......
// Copyright (C) 2018 The Durs Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Defined all network documents
use duniter_documents::v10::block::BlockDocument;
use duniter_documents::v10::certification::CertificationDocument;
use duniter_documents::v10::identity::IdentityDocument;
use duniter_documents::v10::membership::MembershipDocument;
use duniter_documents::v10::revocation::RevocationDocument;
use duniter_documents::v10::transaction::TransactionDocument;
use duniter_documents::Document;
use duniter_documents::{BlockHash, BlockId, Blockstamp};
use serde_json;
use std::ops::Deref;
#[derive(Debug, Clone)]
/// Block v10 in network format (Some events require a blockchain access to reconstitute the corresponding document)
pub struct NetworkBlockV10 {
/// Uncompleted block document
pub uncompleted_block_doc: BlockDocument,
/// revoked
pub revoked: Vec<serde_json::Value>,
/// certifications
pub certifications: Vec<serde_json::Value>,
}
#[derive(Debug, Clone)]
/// Block in network format (Some events require a blockchain access to reconstitute the corresponding document)
pub enum NetworkBlock {
/// Block V1
V10(Box<NetworkBlockV10>),
/// Block V11
V11(),
}
impl NetworkBlock {
/// Return uncompleted block document
pub fn uncompleted_block_doc(&self) -> BlockDocument {
match *self {
NetworkBlock::V10(ref network_block_v10) => {
network_block_v10.deref().uncompleted_block_doc.clone()
}
_ => panic!("Block version not supported !"),
}
}
/// Return blockstamp
pub fn blockstamp(&self) -> Blockstamp {
match *self {
NetworkBlock::V10(ref network_block_v10) => {
network_block_v10.deref().uncompleted_block_doc.blockstamp()
}
_ => panic!("Block version not supported !"),
}
}
/// Return previous blockstamp
pub fn previous_blockstamp(&self) -> Blockstamp {
match *self {
NetworkBlock::V10(ref network_block_v10) => Blockstamp {
id: BlockId(network_block_v10.deref().uncompleted_block_doc.number.0 - 1),
hash: BlockHash(
network_block_v10
.deref()
.uncompleted_block_doc
.previous_hash,
),
},
_ => panic!("Block version not supported !"),
}
}
}
#[derive(Debug, Clone)]
/// Network Document
pub enum BlockchainDocument {
/// Network Block
Block(NetworkBlock),
/// Identity Document
Identity(Box<IdentityDocument>),
/// Membership Document
Membership(Box<MembershipDocument>),
/// Certification Document
Certification(Box<CertificationDocument>),
/// Revocation Document
Revocation(Box<RevocationDocument>),
/// Transaction Document
Transaction(Box<TransactionDocument>),
}
// Copyright (C) 2018 The Durs Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Defined network events.
use documents::*;
use network_head::NetworkHead;
use network_peer::PeerCard;
use NodeFullId;
#[derive(Debug, Clone)]
/// Type containing a network event, each time a network event occurs it's relayed to all modules
pub enum NetworkEvent {
/// A connection has changed state(`u32` is the new state, `Option<String>` est l'uid du noeud)
ConnectionStateChange(NodeFullId, u32, Option<String>, String),
/// Receiving Pending Documents
ReceiveDocuments(Vec<BlockchainDocument>),
/// Receipt of peer cards
ReceivePeers(Vec<PeerCard>),
/// Receiving heads
ReceiveHeads(Vec<NetworkHead>),
}
// Copyright (C) 2018 The Durs Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Defined network requests.
use documents::*;
use duniter_module::*;
use *;
#[derive(Debug, Copy, Clone)]
/// Type containing a request addressed to the network module
pub enum OldNetworkRequest {
/// Get a current block of a specific node
GetCurrent(ModuleReqFullId, NodeFullId),
//GetBlock(NodeFullId, u64),
/// Get a blocks chunk from specified node
GetBlocks(ModuleReqFullId, NodeFullId, u32, u32),
/// Get pending wot documents from specified node
GetRequirementsPending(ModuleReqFullId, NodeFullId, u32),
/// Obtain the current network consensus
GetConsensus(ModuleReqFullId),
/// Getting the heads cache
GetHeadsCache(ModuleReqFullId),
/// Get a list of known endpoints
GetEndpoints(ModuleReqFullId),
}
impl OldNetworkRequest {
/// Get request full identitifier
pub fn get_req_full_id(&self) -> ModuleReqFullId {
match *self {
OldNetworkRequest::GetCurrent(ref req_id, _)
| OldNetworkRequest::GetBlocks(ref req_id, _, _, _)
| OldNetworkRequest::GetRequirementsPending(ref req_id, _, _)
| OldNetworkRequest::GetConsensus(ref req_id)
| OldNetworkRequest::GetHeadsCache(ref req_id)
| OldNetworkRequest::GetEndpoints(ref req_id) => *req_id,
}
}
/// Get request identitifier
pub fn get_req_id(&self) -> ModuleReqId {
self.get_req_full_id().1
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
/// Type returned when the network module does not get a satisfying answer to a request
pub enum OldNetworkRequestError {
/// Receiving an invalid format response
WrongFormat(),
/// Unknow error
UnknowError(),
/// No response received
NoResponse(),
/// Unable to reach the target node
ReceiverUnreachable(),
}
#[derive(Debug, Clone)]
/// Type containing the response to a network request
pub enum NetworkResponse {
/// CurrentBlock
CurrentBlock(ModuleReqFullId, NodeFullId, Box<NetworkBlock>),
/// Block
Block(ModuleReqFullId, NodeFullId, Box<NetworkBlock>),
/// Chunk
Chunk(ModuleReqFullId, NodeFullId, Vec<Box<NetworkBlock>>),
/// PendingDocuments
PendingDocuments(ModuleReqFullId, Vec<BlockchainDocument>),
/// Consensus
Consensus(ModuleReqFullId, Result<Blockstamp, NetworkConsensusError>),
/// HeadsCache
HeadsCache(ModuleReqFullId, Box<NetworkHead>),
}
impl NetworkResponse {
/// Get request full identifier
pub fn get_req_full_id(&self) -> ModuleReqFullId {
match *self {
NetworkResponse::CurrentBlock(ref req_id, _, _)
| NetworkResponse::Block(ref req_id, _, _)
| NetworkResponse::Chunk(ref req_id, _, _)
| NetworkResponse::PendingDocuments(ref req_id, _)
| NetworkResponse::Consensus(ref req_id, _)
| NetworkResponse::HeadsCache(ref req_id, _) => *req_id,
}
}
/// Get request identifier
pub fn get_req_id(&self) -> ModuleReqId {
self.get_req_full_id().1
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment