From 17abb6d20ab6431d6e93b29735da3b10794a2324 Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Wed, 4 Sep 2019 17:12:16 +0200 Subject: [PATCH] [ref] wot: rename NodeId -> WotId + clean dead code and deps --- Cargo.lock | 3 - lib/dubp/wot/Cargo.toml | 15 +- lib/dubp/wot/data/mod.rs | 56 ++-- lib/dubp/wot/data/rusty.rs | 45 +-- lib/dubp/wot/lib.rs | 289 +++++++----------- lib/dubp/wot/operations/centrality.rs | 20 +- lib/dubp/wot/operations/distance.rs | 4 +- lib/dubp/wot/operations/file.rs | 252 --------------- lib/dubp/wot/operations/mod.rs | 1 - lib/dubp/wot/operations/path.rs | 8 +- lib/dubp/wot/tests/g1_genesis.bin | Bin 1795 -> 3223 bytes .../blockchain-dal/src/entities/block.rs | 4 +- .../blockchain-dal/src/entities/identity.rs | 4 +- .../blockchain/blockchain-dal/src/lib.rs | 6 +- .../blockchain-dal/src/readers/certs.rs | 4 +- .../blockchain-dal/src/readers/identity.rs | 6 +- .../blockchain/blockchain-dal/src/tools.rs | 4 +- .../src/writers/certification.rs | 14 +- .../blockchain-dal/src/writers/identity.rs | 6 +- .../blockchain-dal/src/writers/requests.rs | 14 +- lib/modules/blockchain/blockchain/src/dbex.rs | 8 +- .../blockchain/src/dubp/apply/mod.rs | 12 +- .../blockchain/src/dubp/check/mod.rs | 2 +- .../blockchain/src/fork/revert_block.rs | 8 +- lib/modules/blockchain/blockchain/src/lib.rs | 6 +- .../blockchain/src/sync/apply/mod.rs | 4 +- .../blockchain/blockchain/src/sync/mod.rs | 4 +- 27 files changed, 229 insertions(+), 570 deletions(-) delete mode 100644 lib/dubp/wot/operations/file.rs diff --git a/Cargo.lock b/Cargo.lock index baee1280..789e36cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -754,12 +754,9 @@ name = "durs-wot" version = "0.8.0-a0.9" dependencies = [ "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "durs-common-tools 0.2.0", - "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/lib/dubp/wot/Cargo.toml b/lib/dubp/wot/Cargo.toml index c0625e72..f7f0062e 100644 --- a/lib/dubp/wot/Cargo.toml +++ b/lib/dubp/wot/Cargo.toml @@ -2,10 +2,10 @@ name = "durs-wot" version = "0.8.0-a0.9" authors = ["nanocryk <nanocryk@duniter.org>", "elois <elois@duniter.org>"] -description = "Makes Web of Trust computations for the Duniter project." +description = "Makes Web of Trust computations for the Dunitrust project." repository = "https://git.duniter.org/nodes/rust/duniter-rs" readme = "README.md" -keywords = ["duniter", "wot", "wot", "web", "trust"] +keywords = ["duniter", "dunitrust", "wot", "trust"] license = "AGPL-3.0" edition = "2018" @@ -13,12 +13,11 @@ edition = "2018" path = "lib.rs" [dependencies] -log = "0.4.*" -durs-common-tools = { path = "../../tools/common-tools" } -serde = "1.0.*" -serde_derive = "1.0.*" -bincode = "1.0.*" -byteorder = "1.2.*" rayon = "1.0.*" +serde = { version = "1.0.*", features = ["derive"] } + +[dev-dependencies] +bincode = "1.0.*" +durs-common-tools = { path = "../../tools/common-tools", version = "0.2.0" } [features] \ No newline at end of file diff --git a/lib/dubp/wot/data/mod.rs b/lib/dubp/wot/data/mod.rs index f574e5a2..2180a7ea 100644 --- a/lib/dubp/wot/data/mod.rs +++ b/lib/dubp/wot/data/mod.rs @@ -25,9 +25,9 @@ use std::fmt::{self, Debug}; /// Wrapper for a node id. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct NodeId(pub usize); +pub struct WotId(pub usize); -impl Serialize for NodeId { +impl Serialize for WotId { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, @@ -36,48 +36,48 @@ impl Serialize for NodeId { } } -struct NodeIdVisitor; +struct WotIdVisitor; -impl<'de> Visitor<'de> for NodeIdVisitor { - type Value = NodeId; +impl<'de> Visitor<'de> for WotIdVisitor { + type Value = WotId; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("an integer between -2^31 and 2^31") } - fn visit_u8<E>(self, value: u8) -> Result<NodeId, E> + fn visit_u8<E>(self, value: u8) -> Result<WotId, E> where E: de::Error, { - Ok(NodeId(value as usize)) + Ok(WotId(value as usize)) } - fn visit_u32<E>(self, value: u32) -> Result<NodeId, E> + fn visit_u32<E>(self, value: u32) -> Result<WotId, E> where E: de::Error, { - Ok(NodeId(value as usize)) + Ok(WotId(value as usize)) } - fn visit_u64<E>(self, value: u64) -> Result<NodeId, E> + fn visit_u64<E>(self, value: u64) -> Result<WotId, E> where E: de::Error, { use std::usize; if value >= usize::MIN as u64 && value <= usize::MAX as u64 { - Ok(NodeId(value as usize)) + Ok(WotId(value as usize)) } else { Err(E::custom(format!("u32 out of range: {}", value))) } } } -impl<'de> Deserialize<'de> for NodeId { - fn deserialize<D>(deserializer: D) -> Result<NodeId, D::Error> +impl<'de> Deserialize<'de> for WotId { + fn deserialize<D>(deserializer: D) -> Result<WotId, D::Error> where D: Deserializer<'de>, { - deserializer.deserialize_u32(NodeIdVisitor) + deserializer.deserialize_u32(WotIdVisitor) } } @@ -136,52 +136,52 @@ pub trait WebOfTrust: Clone + Debug + Default + DeserializeOwned + Send + Serial fn set_max_link(&mut self, max_link: usize); /// Add a new node. - fn add_node(&mut self) -> NodeId; + fn add_node(&mut self) -> WotId; /// Remove the last node. /// Returns `None` if the WoT was empty, otherwise new top node id. - fn rem_node(&mut self) -> Option<NodeId>; + fn rem_node(&mut self) -> Option<WotId>; /// Get the size of the WoT. fn size(&self) -> usize; /// Check if given node is enabled. /// Returns `None` if this node doesn't exist. - fn is_enabled(&self, id: NodeId) -> Option<bool>; + fn is_enabled(&self, id: WotId) -> Option<bool>; /// Set the enabled state of given node. /// Returns `Null` if this node doesn't exist, `enabled` otherwise. - fn set_enabled(&mut self, id: NodeId, enabled: bool) -> Option<bool>; + fn set_enabled(&mut self, id: WotId, enabled: bool) -> Option<bool>; /// Get enabled node array. - fn get_enabled(&self) -> Vec<NodeId>; + fn get_enabled(&self) -> Vec<WotId>; /// Get disabled node array. - fn get_disabled(&self) -> Vec<NodeId>; + fn get_disabled(&self) -> Vec<WotId>; /// Try to add a link from the source to the target. - fn add_link(&mut self, source: NodeId, target: NodeId) -> NewLinkResult; + fn add_link(&mut self, source: WotId, target: WotId) -> NewLinkResult; /// Try to remove a link from the source to the target. - fn rem_link(&mut self, source: NodeId, target: NodeId) -> RemLinkResult; + fn rem_link(&mut self, source: WotId, target: WotId) -> RemLinkResult; /// Test if there is a link from the source to the target. - fn has_link(&self, source: NodeId, target: NodeId) -> HasLinkResult; + fn has_link(&self, source: WotId, target: WotId) -> HasLinkResult; /// Get the list of links source for this target. /// Returns `None` if this node doesn't exist. - fn get_links_source(&self, target: NodeId) -> Option<Vec<NodeId>>; + fn get_links_source(&self, target: WotId) -> Option<Vec<WotId>>; /// Get the number of issued links by a node. /// Returns `None` if this node doesn't exist. - fn issued_count(&self, id: NodeId) -> Option<usize>; + fn issued_count(&self, id: WotId) -> Option<usize>; /// Test if a node is a sentry. - fn is_sentry(&self, node: NodeId, sentry_requirement: usize) -> Option<bool>; + fn is_sentry(&self, node: WotId, sentry_requirement: usize) -> Option<bool>; /// Get sentries array. - fn get_sentries(&self, sentry_requirement: usize) -> Vec<NodeId>; + fn get_sentries(&self, sentry_requirement: usize) -> Vec<WotId>; /// Get non sentries array. - fn get_non_sentries(&self, sentry_requirement: usize) -> Vec<NodeId>; + fn get_non_sentries(&self, sentry_requirement: usize) -> Vec<WotId>; } diff --git a/lib/dubp/wot/data/rusty.rs b/lib/dubp/wot/data/rusty.rs index 1928bce6..8d84423e 100644 --- a/lib/dubp/wot/data/rusty.rs +++ b/lib/dubp/wot/data/rusty.rs @@ -16,9 +16,10 @@ //! Experimental implementation of the Web of Trust in a more "rusty" style. use super::{HasLinkResult, NewLinkResult, RemLinkResult}; -use crate::NodeId; use crate::WebOfTrust; +use crate::WotId; use rayon::prelude::*; +use serde::{Deserialize, Serialize}; use std::collections::HashSet; /// A node in the `WoT` graph. @@ -27,7 +28,7 @@ struct Node { /// Is this node enabled ? enabled: bool, /// Set of links this node is the target. - links_source: HashSet<NodeId>, + links_source: HashSet<WotId>, /// Number of links the node issued. issued_count: usize, } @@ -77,16 +78,16 @@ impl WebOfTrust for RustyWebOfTrust { self.max_links = max_links; } - fn add_node(&mut self) -> NodeId { + fn add_node(&mut self) -> WotId { self.nodes.push(Node::new()); - NodeId(self.nodes.len() - 1) + WotId(self.nodes.len() - 1) } - fn rem_node(&mut self) -> Option<NodeId> { + fn rem_node(&mut self) -> Option<WotId> { self.nodes.pop(); if !self.nodes.is_empty() { - Some(NodeId(self.nodes.len() - 1)) + Some(WotId(self.nodes.len() - 1)) } else { None } @@ -96,36 +97,36 @@ impl WebOfTrust for RustyWebOfTrust { self.nodes.len() } - fn is_enabled(&self, id: NodeId) -> Option<bool> { + fn is_enabled(&self, id: WotId) -> Option<bool> { self.nodes.get(id.0).map(|n| n.enabled) } - fn set_enabled(&mut self, id: NodeId, enabled: bool) -> Option<bool> { + fn set_enabled(&mut self, id: WotId, enabled: bool) -> Option<bool> { self.nodes .get_mut(id.0) .map(|n| n.enabled = enabled) .map(|_| enabled) } - fn get_enabled(&self) -> Vec<NodeId> { + fn get_enabled(&self) -> Vec<WotId> { self.nodes .par_iter() .enumerate() .filter(|&(_, n)| n.enabled) - .map(|(i, _)| NodeId(i)) + .map(|(i, _)| WotId(i)) .collect() } - fn get_disabled(&self) -> Vec<NodeId> { + fn get_disabled(&self) -> Vec<WotId> { self.nodes .par_iter() .enumerate() .filter(|&(_, n)| !n.enabled) - .map(|(i, _)| NodeId(i)) + .map(|(i, _)| WotId(i)) .collect() } - fn add_link(&mut self, source: NodeId, target: NodeId) -> NewLinkResult { + fn add_link(&mut self, source: WotId, target: WotId) -> NewLinkResult { if source == target { NewLinkResult::SelfLinkingForbidden() } else if source.0 >= self.size() { @@ -141,7 +142,7 @@ impl WebOfTrust for RustyWebOfTrust { } } - fn rem_link(&mut self, source: NodeId, target: NodeId) -> RemLinkResult { + fn rem_link(&mut self, source: WotId, target: WotId) -> RemLinkResult { if source.0 >= self.size() { RemLinkResult::UnknownSource() } else if target.0 >= self.size() { @@ -155,7 +156,7 @@ impl WebOfTrust for RustyWebOfTrust { } } - fn has_link(&self, source: NodeId, target: NodeId) -> HasLinkResult { + fn has_link(&self, source: WotId, target: WotId) -> HasLinkResult { if source.0 >= self.size() { HasLinkResult::UnknownSource() } else if target.0 >= self.size() { @@ -165,17 +166,17 @@ impl WebOfTrust for RustyWebOfTrust { } } - fn get_links_source(&self, target: NodeId) -> Option<Vec<NodeId>> { + fn get_links_source(&self, target: WotId) -> Option<Vec<WotId>> { self.nodes .get(target.0) .map(|n| n.links_source.iter().cloned().collect()) } - fn issued_count(&self, id: NodeId) -> Option<usize> { + fn issued_count(&self, id: WotId) -> Option<usize> { self.nodes.get(id.0).map(|n| n.issued_count) } - fn is_sentry(&self, node: NodeId, sentry_requirement: usize) -> Option<bool> { + fn is_sentry(&self, node: WotId, sentry_requirement: usize) -> Option<bool> { if node.0 >= self.size() { return None; } @@ -189,7 +190,7 @@ impl WebOfTrust for RustyWebOfTrust { ) } - fn get_sentries(&self, sentry_requirement: usize) -> Vec<NodeId> { + fn get_sentries(&self, sentry_requirement: usize) -> Vec<WotId> { self.nodes .par_iter() .enumerate() @@ -198,11 +199,11 @@ impl WebOfTrust for RustyWebOfTrust { && n.issued_count >= sentry_requirement && n.links_source.len() >= sentry_requirement }) - .map(|(i, _)| NodeId(i)) + .map(|(i, _)| WotId(i)) .collect() } - fn get_non_sentries(&self, sentry_requirement: usize) -> Vec<NodeId> { + fn get_non_sentries(&self, sentry_requirement: usize) -> Vec<WotId> { self.nodes .par_iter() .enumerate() @@ -211,7 +212,7 @@ impl WebOfTrust for RustyWebOfTrust { && (n.issued_count < sentry_requirement || n.links_source.len() < sentry_requirement) }) - .map(|(i, _)| NodeId(i)) + .map(|(i, _)| WotId(i)) .collect() } } diff --git a/lib/dubp/wot/lib.rs b/lib/dubp/wot/lib.rs index 86eeff90..d092b6a3 100644 --- a/lib/dubp/wot/lib.rs +++ b/lib/dubp/wot/lib.rs @@ -38,15 +38,10 @@ unused_qualifications )] -#[macro_use] -extern crate serde_derive; -#[macro_use] -extern crate log; - pub mod data; pub mod operations; -pub use crate::data::{NodeId, WebOfTrust}; +pub use crate::data::{WebOfTrust, WotId}; #[cfg(test)] mod tests { @@ -54,8 +49,8 @@ mod tests { use crate::data::*; use crate::operations::centrality::*; use crate::operations::distance::*; - use crate::operations::file::*; use crate::operations::path::*; + use std::path::Path; /// Test translated from https://github.com/duniter/wot/blob/master/tests/test.js /// @@ -74,126 +69,102 @@ mod tests { assert_eq!(wot.size(), 0); // should return `None()` if testing `is_enabled()` with out-of-bounds node - assert_eq!(wot.is_enabled(NodeId(0)), None); - assert_eq!(wot.is_enabled(NodeId(23)), None); + assert_eq!(wot.is_enabled(WotId(0)), None); + assert_eq!(wot.is_enabled(WotId(23)), None); // should give nomber 0 if we add a node // - add a node - assert_eq!(wot.add_node(), NodeId(0)); + assert_eq!(wot.add_node(), WotId(0)); assert_eq!(wot.size(), 1); assert_eq!(wot.get_disabled().len(), 0); // - add another - assert_eq!(wot.add_node(), NodeId(1)); + assert_eq!(wot.add_node(), WotId(1)); assert_eq!(wot.size(), 2); assert_eq!(wot.get_disabled().len(), 0); // - add 10 nodes for i in 0..10 { - assert_eq!(wot.add_node(), NodeId(i + 2)); + assert_eq!(wot.add_node(), WotId(i + 2)); } assert_eq!(wot.size(), 12); // shouldn't be able to self cert assert_eq!( - wot.add_link(NodeId(0), NodeId(0)), + wot.add_link(WotId(0), WotId(0)), NewLinkResult::SelfLinkingForbidden() ); // should add certs only in the boundaries of max_cert - assert_eq!(wot.add_link(NodeId(0), NodeId(1)), NewLinkResult::Ok(1)); - assert_eq!(wot.add_link(NodeId(0), NodeId(2)), NewLinkResult::Ok(1)); - assert_eq!(wot.add_link(NodeId(0), NodeId(3)), NewLinkResult::Ok(1)); + assert_eq!(wot.add_link(WotId(0), WotId(1)), NewLinkResult::Ok(1)); + assert_eq!(wot.add_link(WotId(0), WotId(2)), NewLinkResult::Ok(1)); + assert_eq!(wot.add_link(WotId(0), WotId(3)), NewLinkResult::Ok(1)); assert_eq!( - wot.add_link(NodeId(0), NodeId(4)), + wot.add_link(WotId(0), WotId(4)), NewLinkResult::AllCertificationsUsed(0) ); assert_eq!(wot.get_max_link(), 3); - assert_eq!( - wot.has_link(NodeId(0), NodeId(1)), - HasLinkResult::Link(true) - ); - assert_eq!( - wot.has_link(NodeId(0), NodeId(2)), - HasLinkResult::Link(true) - ); - assert_eq!( - wot.has_link(NodeId(0), NodeId(3)), - HasLinkResult::Link(true) - ); - assert_eq!( - wot.has_link(NodeId(0), NodeId(4)), - HasLinkResult::Link(false) - ); + assert_eq!(wot.has_link(WotId(0), WotId(1)), HasLinkResult::Link(true)); + assert_eq!(wot.has_link(WotId(0), WotId(2)), HasLinkResult::Link(true)); + assert_eq!(wot.has_link(WotId(0), WotId(3)), HasLinkResult::Link(true)); + assert_eq!(wot.has_link(WotId(0), WotId(4)), HasLinkResult::Link(false)); wot.set_max_link(4); assert_eq!(wot.get_max_link(), 4); - assert_eq!( - wot.has_link(NodeId(0), NodeId(4)), - HasLinkResult::Link(false) - ); - wot.add_link(NodeId(0), NodeId(4)); - assert_eq!( - wot.has_link(NodeId(0), NodeId(4)), - HasLinkResult::Link(true) - ); - wot.rem_link(NodeId(0), NodeId(1)); - wot.rem_link(NodeId(0), NodeId(2)); - wot.rem_link(NodeId(0), NodeId(3)); - wot.rem_link(NodeId(0), NodeId(4)); + assert_eq!(wot.has_link(WotId(0), WotId(4)), HasLinkResult::Link(false)); + wot.add_link(WotId(0), WotId(4)); + assert_eq!(wot.has_link(WotId(0), WotId(4)), HasLinkResult::Link(true)); + wot.rem_link(WotId(0), WotId(1)); + wot.rem_link(WotId(0), WotId(2)); + wot.rem_link(WotId(0), WotId(3)); + wot.rem_link(WotId(0), WotId(4)); // false when not linked + test out of bounds + assert_eq!(wot.has_link(WotId(0), WotId(6)), HasLinkResult::Link(false)); assert_eq!( - wot.has_link(NodeId(0), NodeId(6)), - HasLinkResult::Link(false) - ); - assert_eq!( - wot.has_link(NodeId(23), NodeId(0)), + wot.has_link(WotId(23), WotId(0)), HasLinkResult::UnknownSource() ); assert_eq!( - wot.has_link(NodeId(2), NodeId(53)), + wot.has_link(WotId(2), WotId(53)), HasLinkResult::UnknownTarget() ); // created nodes should be enabled - assert_eq!(wot.is_enabled(NodeId(0)), Some(true)); - assert_eq!(wot.is_enabled(NodeId(1)), Some(true)); - assert_eq!(wot.is_enabled(NodeId(2)), Some(true)); - assert_eq!(wot.is_enabled(NodeId(3)), Some(true)); - assert_eq!(wot.is_enabled(NodeId(11)), Some(true)); + assert_eq!(wot.is_enabled(WotId(0)), Some(true)); + assert_eq!(wot.is_enabled(WotId(1)), Some(true)); + assert_eq!(wot.is_enabled(WotId(2)), Some(true)); + assert_eq!(wot.is_enabled(WotId(3)), Some(true)); + assert_eq!(wot.is_enabled(WotId(11)), Some(true)); // should be able to disable some nodes - assert_eq!(wot.set_enabled(NodeId(0), false), Some(false)); - assert_eq!(wot.set_enabled(NodeId(1), false), Some(false)); - assert_eq!(wot.set_enabled(NodeId(2), false), Some(false)); + assert_eq!(wot.set_enabled(WotId(0), false), Some(false)); + assert_eq!(wot.set_enabled(WotId(1), false), Some(false)); + assert_eq!(wot.set_enabled(WotId(2), false), Some(false)); assert_eq!(wot.get_disabled().len(), 3); - assert_eq!(wot.set_enabled(NodeId(1), true), Some(true)); + assert_eq!(wot.set_enabled(WotId(1), true), Some(true)); // node 0 and 2 should be disabled - assert_eq!(wot.is_enabled(NodeId(0)), Some(false)); - assert_eq!(wot.is_enabled(NodeId(1)), Some(true)); - assert_eq!(wot.is_enabled(NodeId(2)), Some(false)); - assert_eq!(wot.is_enabled(NodeId(3)), Some(true)); + assert_eq!(wot.is_enabled(WotId(0)), Some(false)); + assert_eq!(wot.is_enabled(WotId(1)), Some(true)); + assert_eq!(wot.is_enabled(WotId(2)), Some(false)); + assert_eq!(wot.is_enabled(WotId(3)), Some(true)); // - set enabled again - assert_eq!(wot.set_enabled(NodeId(0), true), Some(true)); - assert_eq!(wot.set_enabled(NodeId(1), true), Some(true)); - assert_eq!(wot.set_enabled(NodeId(2), true), Some(true)); - assert_eq!(wot.set_enabled(NodeId(1), true), Some(true)); + assert_eq!(wot.set_enabled(WotId(0), true), Some(true)); + assert_eq!(wot.set_enabled(WotId(1), true), Some(true)); + assert_eq!(wot.set_enabled(WotId(2), true), Some(true)); + assert_eq!(wot.set_enabled(WotId(1), true), Some(true)); assert_eq!(wot.get_disabled().len(), 0); // should not exist a link from 2 to 0 - assert_eq!( - wot.has_link(NodeId(2), NodeId(0)), - HasLinkResult::Link(false) - ); + assert_eq!(wot.has_link(WotId(2), WotId(0)), HasLinkResult::Link(false)); // should be able to add some links, cert count is returned - assert_eq!(wot.add_link(NodeId(2), NodeId(0)), NewLinkResult::Ok(1)); - assert_eq!(wot.add_link(NodeId(4), NodeId(0)), NewLinkResult::Ok(2)); - assert_eq!(wot.add_link(NodeId(5), NodeId(0)), NewLinkResult::Ok(3)); + assert_eq!(wot.add_link(WotId(2), WotId(0)), NewLinkResult::Ok(1)); + assert_eq!(wot.add_link(WotId(4), WotId(0)), NewLinkResult::Ok(2)); + assert_eq!(wot.add_link(WotId(5), WotId(0)), NewLinkResult::Ok(3)); // should exist new links /* WoT is: @@ -203,28 +174,13 @@ mod tests { * 5 --> 0 */ - assert_eq!( - wot.has_link(NodeId(2), NodeId(0)), - HasLinkResult::Link(true) - ); - assert_eq!( - wot.has_link(NodeId(4), NodeId(0)), - HasLinkResult::Link(true) - ); - assert_eq!( - wot.has_link(NodeId(5), NodeId(0)), - HasLinkResult::Link(true) - ); - assert_eq!( - wot.has_link(NodeId(2), NodeId(1)), - HasLinkResult::Link(false) - ); + assert_eq!(wot.has_link(WotId(2), WotId(0)), HasLinkResult::Link(true)); + assert_eq!(wot.has_link(WotId(4), WotId(0)), HasLinkResult::Link(true)); + assert_eq!(wot.has_link(WotId(5), WotId(0)), HasLinkResult::Link(true)); + assert_eq!(wot.has_link(WotId(2), WotId(1)), HasLinkResult::Link(false)); // should be able to remove some links - assert_eq!( - wot.rem_link(NodeId(4), NodeId(0)), - RemLinkResult::Removed(2) - ); + assert_eq!(wot.rem_link(WotId(4), WotId(0)), RemLinkResult::Removed(2)); /* * WoT is now: * @@ -233,29 +189,17 @@ mod tests { */ // should exist less links - assert_eq!( - wot.has_link(NodeId(2), NodeId(0)), - HasLinkResult::Link(true) - ); - assert_eq!( - wot.has_link(NodeId(4), NodeId(0)), - HasLinkResult::Link(false) - ); - assert_eq!( - wot.has_link(NodeId(5), NodeId(0)), - HasLinkResult::Link(true) - ); - assert_eq!( - wot.has_link(NodeId(2), NodeId(1)), - HasLinkResult::Link(false) - ); + assert_eq!(wot.has_link(WotId(2), WotId(0)), HasLinkResult::Link(true)); + assert_eq!(wot.has_link(WotId(4), WotId(0)), HasLinkResult::Link(false)); + assert_eq!(wot.has_link(WotId(5), WotId(0)), HasLinkResult::Link(true)); + assert_eq!(wot.has_link(WotId(2), WotId(1)), HasLinkResult::Link(false)); // should successfully use distance rule assert_eq!( distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 1, step_max: 1, x_percent: 1.0, @@ -268,7 +212,7 @@ mod tests { distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 2, step_max: 1, x_percent: 1.0, @@ -281,7 +225,7 @@ mod tests { distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 3, step_max: 1, x_percent: 1.0, @@ -292,8 +236,8 @@ mod tests { // => no because no member has issued 3 certifications // - we add links from member 3 - assert_eq!(wot.add_link(NodeId(3), NodeId(1)), NewLinkResult::Ok(1)); - assert_eq!(wot.add_link(NodeId(3), NodeId(2)), NewLinkResult::Ok(1)); + assert_eq!(wot.add_link(WotId(3), WotId(1)), NewLinkResult::Ok(1)); + assert_eq!(wot.add_link(WotId(3), WotId(2)), NewLinkResult::Ok(1)); /* * WoT is now: * @@ -304,29 +248,23 @@ mod tests { */ assert_eq!(wot.size(), 12); assert_eq!(wot.get_sentries(1).len(), 1); - assert_eq!(wot.get_sentries(1)[0], NodeId(2)); + assert_eq!(wot.get_sentries(1)[0], WotId(2)); assert_eq!(wot.get_sentries(2).len(), 0); assert_eq!(wot.get_sentries(3).len(), 0); assert_eq!(wot.get_non_sentries(1).len(), 11); // 12 - 1 assert_eq!(wot.get_non_sentries(2).len(), 12); // 12 - 0 assert_eq!(wot.get_non_sentries(3).len(), 12); // 12 - 0 - assert_eq!( - path_finder.find_paths(&wot, NodeId(3), NodeId(0), 1).len(), - 0 - ); // KO - assert_eq!( - path_finder.find_paths(&wot, NodeId(3), NodeId(0), 2).len(), - 1 - ); // It exists 3 -> 2 -> 0 + assert_eq!(path_finder.find_paths(&wot, WotId(3), WotId(0), 1).len(), 0); // KO + assert_eq!(path_finder.find_paths(&wot, WotId(3), WotId(0), 2).len(), 1); // It exists 3 -> 2 -> 0 assert!(path_finder - .find_paths(&wot, NodeId(3), NodeId(0), 2) - .contains(&vec![NodeId(3), NodeId(2), NodeId(0)])); + .find_paths(&wot, WotId(3), WotId(0), 2) + .contains(&vec![WotId(3), WotId(2), WotId(0)])); assert_eq!( distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 1, step_max: 1, x_percent: 1.0, @@ -338,7 +276,7 @@ mod tests { distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 2, step_max: 1, x_percent: 1.0, @@ -350,7 +288,7 @@ mod tests { distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 3, step_max: 1, x_percent: 1.0, @@ -362,7 +300,7 @@ mod tests { distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 2, step_max: 2, x_percent: 1.0, @@ -371,38 +309,32 @@ mod tests { Some(false) ); // OK : 2 -> 0 - wot.add_link(NodeId(1), NodeId(3)); - wot.add_link(NodeId(2), NodeId(3)); + wot.add_link(WotId(1), WotId(3)); + wot.add_link(WotId(2), WotId(3)); assert_eq!(wot.size(), 12); assert_eq!(wot.get_sentries(1).len(), 3); - assert_eq!(wot.get_sentries(1)[0], NodeId(1)); - assert_eq!(wot.get_sentries(1)[1], NodeId(2)); - assert_eq!(wot.get_sentries(1)[2], NodeId(3)); + assert_eq!(wot.get_sentries(1)[0], WotId(1)); + assert_eq!(wot.get_sentries(1)[1], WotId(2)); + assert_eq!(wot.get_sentries(1)[2], WotId(3)); assert_eq!(wot.get_sentries(2).len(), 1); - assert_eq!(wot.get_sentries(2)[0], NodeId(3)); + assert_eq!(wot.get_sentries(2)[0], WotId(3)); assert_eq!(wot.get_sentries(3).len(), 0); assert_eq!(wot.get_non_sentries(1).len(), 9); // 12 - 3 assert_eq!(wot.get_non_sentries(2).len(), 11); // 12 - 1 assert_eq!(wot.get_non_sentries(3).len(), 12); // 12 - 0 - assert_eq!( - path_finder.find_paths(&wot, NodeId(3), NodeId(0), 1).len(), - 0 - ); // KO - assert_eq!( - path_finder.find_paths(&wot, NodeId(3), NodeId(0), 2).len(), - 1 - ); // It exists 3 -> 2 -> 0 + assert_eq!(path_finder.find_paths(&wot, WotId(3), WotId(0), 1).len(), 0); // KO + assert_eq!(path_finder.find_paths(&wot, WotId(3), WotId(0), 2).len(), 1); // It exists 3 -> 2 -> 0 assert!(path_finder - .find_paths(&wot, NodeId(3), NodeId(0), 2) - .contains(&vec![NodeId(3), NodeId(2), NodeId(0)])); + .find_paths(&wot, WotId(3), WotId(0), 2) + .contains(&vec![WotId(3), WotId(2), WotId(0)])); assert_eq!( distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 1, step_max: 1, x_percent: 1.0, @@ -414,7 +346,7 @@ mod tests { distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 2, step_max: 1, x_percent: 1.0, @@ -426,7 +358,7 @@ mod tests { distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 3, step_max: 1, x_percent: 1.0, @@ -438,7 +370,7 @@ mod tests { distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 2, step_max: 2, x_percent: 1.0, @@ -451,20 +383,20 @@ mod tests { assert_eq!(wot.size(), 12); // delete top node (return new top node id) - assert_eq!(wot.rem_node(), Some(NodeId(10))); + assert_eq!(wot.rem_node(), Some(WotId(10))); // should have 11 nodes assert_eq!(wot.size(), 11); // should work with member 3 disabled // - with member 3 disabled (non-member) - assert_eq!(wot.set_enabled(NodeId(3), false), Some(false)); + assert_eq!(wot.set_enabled(WotId(3), false), Some(false)); assert_eq!(wot.get_disabled().len(), 1); assert_eq!( distance_calculator.is_outdistanced( &wot, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 2, step_max: 1, x_percent: 1.0, @@ -473,28 +405,19 @@ mod tests { Some(false) ); // OK : Disabled - let file_formater = BinaryFileFormater {}; - // Write wot in file - assert_eq!( - file_formater - .to_file( - &wot, - &[0b0000_0000, 0b0000_0001, 0b0000_0001, 0b0000_0000], - "test.wot" - ) - .unwrap(), - () - ); + durs_common_tools::fns::bin_file::write_bin_file( + Path::new("test.wot"), + &bincode::serialize(&wot).expect("fail to serialize wot"), + ) + .expect("fail to write wot file"); - let (wot2, blockstamp2) = file_formater.from_file::<W>("test.wot", 3).unwrap(); + let wot2_bin = durs_common_tools::fns::bin_file::read_bin_file(Path::new("test.wot")) + .expect("fail to read wot file"); + let wot2: W = bincode::deserialize(&wot2_bin).expect("fail to deserialize wot"); // Read wot from file { - assert_eq!( - blockstamp2, - vec![0b0000_0000, 0b0000_0001, 0b0000_0001, 0b0000_0000] - ); assert_eq!(wot.size(), wot2.size()); assert_eq!( wot.get_non_sentries(1).len(), @@ -502,12 +425,12 @@ mod tests { ); assert_eq!(wot.get_disabled().len(), wot2.get_disabled().len()); assert_eq!(wot2.get_disabled().len(), 1); - assert_eq!(wot2.is_enabled(NodeId(3)), Some(false)); + assert_eq!(wot2.is_enabled(WotId(3)), Some(false)); assert_eq!( distance_calculator.is_outdistanced( &wot2, WotDistanceParameters { - node: NodeId(0), + node: WotId(0), sentry_requirement: 2, step_max: 1, x_percent: 1.0, @@ -518,18 +441,10 @@ mod tests { } // Read g1_genesis wot - let (wot3, blockstamp3) = file_formater - .from_file::<W>("tests/g1_genesis.bin", 100) - .unwrap(); - assert_eq!( - blockstamp3, - vec![ - 57, 57, 45, 48, 48, 48, 48, 49, 50, 65, 68, 52, 57, 54, 69, 67, 65, 53, 54, 68, 69, - 48, 66, 56, 69, 53, 68, 54, 70, 55, 52, 57, 66, 55, 67, 66, 69, 55, 56, 53, 53, 51, - 69, 54, 51, 56, 53, 51, 51, 51, 65, 52, 52, 69, 48, 52, 51, 55, 55, 69, 70, 70, 67, - 67, 65, 53, 51, - ] - ); + let wot3_bin = + durs_common_tools::fns::bin_file::read_bin_file(Path::new("tests/g1_genesis.bin")) + .expect("fail to read g1_genesis wot file"); + let wot3: W = bincode::deserialize(&wot3_bin).expect("fail to deserialize g1_genesis wot"); // Check g1_genesis wot members_count let members_count = wot3.get_enabled().len() as u64; @@ -540,7 +455,7 @@ mod tests { distance_calculator.compute_distance( &wot3, WotDistanceParameters { - node: NodeId(37), + node: WotId(37), sentry_requirement: 3, step_max: 5, x_percent: 0.8, diff --git a/lib/dubp/wot/operations/centrality.rs b/lib/dubp/wot/operations/centrality.rs index d15d63de..f1225e11 100644 --- a/lib/dubp/wot/operations/centrality.rs +++ b/lib/dubp/wot/operations/centrality.rs @@ -15,8 +15,8 @@ //! Provide a trait and implementations to find paths between nodes. -use crate::data::NodeId; use crate::data::WebOfTrust; +use crate::data::WotId; use std::collections::{HashMap, VecDeque}; /// Find paths between 2 nodes of a `WebOfTrust`. @@ -41,11 +41,11 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat // The source of any path belongs to enabled_nodes for s in enabled_nodes.clone() { - let mut stack: Vec<NodeId> = Vec::with_capacity(wot_size); - let mut paths: HashMap<NodeId, Vec<NodeId>> = HashMap::with_capacity(wot_size); + let mut stack: Vec<WotId> = Vec::with_capacity(wot_size); + let mut paths: HashMap<WotId, Vec<WotId>> = HashMap::with_capacity(wot_size); let mut sigma = vec![0.0; wot_size]; let mut d: Vec<isize> = vec![-1; wot_size]; - let mut q: VecDeque<NodeId> = VecDeque::with_capacity(wot_size); + let mut q: VecDeque<WotId> = VecDeque::with_capacity(wot_size); sigma[s.0] = 1.0; d[s.0] = 0; @@ -94,11 +94,11 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat // The source of any path belongs to enabled_nodes for s in enabled_nodes.clone() { - let mut stack: Vec<NodeId> = Vec::with_capacity(wot_size); - let mut paths: HashMap<NodeId, Vec<NodeId>> = HashMap::with_capacity(wot_size); + let mut stack: Vec<WotId> = Vec::with_capacity(wot_size); + let mut paths: HashMap<WotId, Vec<WotId>> = HashMap::with_capacity(wot_size); let mut sigma = vec![0.0; wot_size]; let mut d: Vec<isize> = vec![-1; wot_size]; - let mut q: VecDeque<NodeId> = VecDeque::with_capacity(wot_size); + let mut q: VecDeque<WotId> = VecDeque::with_capacity(wot_size); sigma[s.0] = 1.0; d[s.0] = 0; @@ -147,11 +147,11 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat // The source of any path belongs to enabled_nodes for s in enabled_nodes.clone() { - let mut stack: Vec<NodeId> = Vec::with_capacity(wot_size); - let mut paths: HashMap<NodeId, Vec<NodeId>> = HashMap::with_capacity(wot_size); + let mut stack: Vec<WotId> = Vec::with_capacity(wot_size); + let mut paths: HashMap<WotId, Vec<WotId>> = HashMap::with_capacity(wot_size); let mut sigma = vec![0.0; wot_size]; let mut d: Vec<isize> = vec![-1; wot_size]; - let mut q: VecDeque<NodeId> = VecDeque::with_capacity(wot_size); + let mut q: VecDeque<WotId> = VecDeque::with_capacity(wot_size); sigma[s.0] = 1.0; d[s.0] = 0; diff --git a/lib/dubp/wot/operations/distance.rs b/lib/dubp/wot/operations/distance.rs index 6e7c4b23..4fb1dd12 100644 --- a/lib/dubp/wot/operations/distance.rs +++ b/lib/dubp/wot/operations/distance.rs @@ -15,8 +15,8 @@ //! Provide a trait and implementations to compute distances. -use crate::data::NodeId; use crate::data::WebOfTrust; +use crate::data::WotId; use rayon::prelude::*; use std::collections::HashSet; @@ -24,7 +24,7 @@ use std::collections::HashSet; #[derive(Debug, Copy, Clone, PartialEq)] pub struct WotDistanceParameters { /// Node from where distances are calculated. - pub node: NodeId, + pub node: WotId, /// Links count received AND issued to be a sentry. pub sentry_requirement: u32, /// Currency parameter. diff --git a/lib/dubp/wot/operations/file.rs b/lib/dubp/wot/operations/file.rs deleted file mode 100644 index 2f4a87de..00000000 --- a/lib/dubp/wot/operations/file.rs +++ /dev/null @@ -1,252 +0,0 @@ -// Copyright (C) 2017-2019 The AXIOM TEAM Association. -// -// 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/>. - -//! Provide a trait and implementation to read and write `WebOfTrust` to disk. - -use crate::data::NodeId; -use durs_common_tools::fatal_error; -use std::fs; -use std::fs::File; -use std::io; -use std::io::prelude::*; - -use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; - -use crate::data::WebOfTrust; - -/// Results of `WebOfTrust` parsing from binary file. -#[derive(Debug)] -pub enum WotParseError { - /// FailToOpenFile - FailToOpenFile(io::Error), - - /// IOError - IOError(io::Error), -} - -impl From<io::Error> for WotParseError { - fn from(e: io::Error) -> WotParseError { - WotParseError::IOError(e) - } -} - -/// Results of `WebOfTrust` writing to binary file. -#[derive(Debug)] -pub enum WotWriteError { - /// WrongWotSize - WrongWotSize(), - - /// FailToCreateFile - FailToCreateFile(io::Error), - - /// FailToWriteInFile - FailToWriteInFile(io::Error), -} - -impl From<io::Error> for WotWriteError { - fn from(e: io::Error) -> WotWriteError { - WotWriteError::FailToWriteInFile(e) - } -} - -/// Provide Read/Write functions for `WebOfTrust` objects. -pub trait FileFormater { - /// Try to read a `WebOfTrust` from a file. - fn from_file<T: WebOfTrust>( - &self, - path: &str, - max_links: usize, - ) -> Result<(T, Vec<u8>), WotParseError>; - - /// Tru to write a `WebOfTrust` in a file. - fn to_file<T: WebOfTrust>(&self, wot: &T, data: &[u8], path: &str) - -> Result<(), WotWriteError>; -} - -/// Read and write WebOfTrust in a binary format. -#[derive(Debug, Clone, Copy)] -pub struct BinaryFileFormater; - -impl FileFormater for BinaryFileFormater { - /// Try to read a `WebOfTrust` from a file. - fn from_file<T: WebOfTrust>( - &self, - path: &str, - max_links: usize, - ) -> Result<(T, Vec<u8>), WotParseError> { - let mut wot = T::new(max_links); - - let file_size = fs::metadata(path).expect("fail to read wot file !").len(); - let mut file_pointing_to_blockstamp_size: Vec<u8> = vec![0; file_size as usize]; - match File::open(path) { - Ok(mut file) => { - file.read_exact(&mut file_pointing_to_blockstamp_size.as_mut_slice())?; - } - Err(e) => return Err(WotParseError::FailToOpenFile(e)), - }; - // Read up to 4 bytes (blockstamp_size) - let mut file_pointing_to_blockstamp = file_pointing_to_blockstamp_size.split_off(4); - // Get blockstamp size - let mut buf = &file_pointing_to_blockstamp_size[..]; - let blockstamp_size = buf.read_u32::<BigEndian>().unwrap(); - // Read up to blockstamp_size bytes (blockstamp) - let mut file_pointing_to_nodes_count = - file_pointing_to_blockstamp.split_off(blockstamp_size as usize); - // Read up to 4 bytes (nodes_count) - let mut file_pointing_to_nodes_states = file_pointing_to_nodes_count.split_off(4); - // Read nodes_count - let mut buf = &file_pointing_to_nodes_count[..]; - let nodes_count = buf.read_u32::<BigEndian>().unwrap(); - // Calcule nodes_state size - let nodes_states_size = match nodes_count % 8 { - 0 => nodes_count / 8, - _ => (nodes_count / 8) + 1, - }; - // Read up to nodes_states_size bytes (nodes_states) - let file_pointing_to_links = - file_pointing_to_nodes_states.split_off(nodes_states_size as usize); - let count_total_bytes_read = file_pointing_to_links.len() - + nodes_states_size as usize - + 4 - + blockstamp_size as usize - + 4; - if count_total_bytes_read != file_size as usize { - fatal_error!("not read all wot file !"); - } - // Apply nodes state - let mut count_remaining_nodes = nodes_count; - for byte in file_pointing_to_nodes_states { - let mut byte_integer = u8::from_be(byte); - let mut factor: u8 = 128; - for _i in 0..8 { - if count_remaining_nodes > 0 { - wot.add_node(); - if byte_integer >= factor { - byte_integer -= factor; - } else { - let _test = wot.set_enabled( - NodeId((nodes_count - count_remaining_nodes) as usize), - false, - ); - } - count_remaining_nodes -= 1; - } - factor /= 2; - } - } - // Apply links - let mut buffer_3b: Vec<u8> = Vec::with_capacity(3); - let mut count_bytes = 0; - let mut remaining_links: u8 = 0; - let mut target: u32 = 0; - for byte in file_pointing_to_links { - if remaining_links == 0 { - target += 1; - remaining_links = u8::from_be(byte); - count_bytes = 0; - } else { - buffer_3b.push(byte); - if count_bytes % 3 == 2 { - let mut buf = &buffer_3b.clone()[..]; - let source = buf.read_u24::<BigEndian>().expect("fail to parse source"); - wot.add_link(NodeId(source as usize), NodeId((target - 1) as usize)); - remaining_links -= 1; - buffer_3b.clear(); - } - count_bytes += 1; - } - } - if count_bytes % 3 != 0 { - fatal_error!("not read all wot file !"); - } - Ok((wot, file_pointing_to_blockstamp)) - } - - /// Try to write a `WebOfTrust` in a file. - fn to_file<T: WebOfTrust>( - &self, - wot: &T, - data: &[u8], - path: &str, - ) -> Result<(), WotWriteError> { - let mut buffer: Vec<u8> = Vec::new(); - // Write blockstamp size - let blockstamp_size = data.len() as u32; - let mut bytes: Vec<u8> = Vec::with_capacity(4); - bytes.write_u32::<BigEndian>(blockstamp_size).unwrap(); - buffer.append(&mut bytes); - // Write blockstamp - buffer.append(&mut data.to_vec()); - // Write nodes_count - let nodes_count = wot.size() as u32; - let mut bytes: Vec<u8> = Vec::with_capacity(4); - bytes.write_u32::<BigEndian>(nodes_count).unwrap(); - buffer.append(&mut bytes); - // Write enable state by groups of 8 - let mut enable_states: u8 = 0; - let mut factor: u8 = 128; - for n in 0..nodes_count { - match wot.is_enabled(NodeId(n as usize)) { - Some(enable) => { - if enable { - enable_states += factor; - } - } - None => { - return Err(WotWriteError::WrongWotSize()); - } - } - if n % 8 == 7 { - factor = 128; - let mut tmp_buf = Vec::with_capacity(1); - tmp_buf.write_u8(enable_states).unwrap(); - buffer.append(&mut tmp_buf); - enable_states = 0; - } else { - factor /= 2; - } - } - // nodes_states padding - if nodes_count % 8 != 7 { - let mut tmp_buf = Vec::with_capacity(1); - tmp_buf.write_u8(enable_states).unwrap(); - buffer.append(&mut tmp_buf); - } - // Write links - for n in 0..nodes_count { - if let Some(sources) = wot.get_links_source(NodeId(n as usize)) { - // Write sources_counts - let mut bytes = Vec::with_capacity(1); - bytes.write_u8(sources.len() as u8).unwrap(); - buffer.append(&mut bytes); - for source in &sources { - // Write source - let mut bytes: Vec<u8> = Vec::with_capacity(3); - bytes.write_u24::<BigEndian>(source.0 as u32).unwrap(); - buffer.append(&mut bytes); - } - }; - } - // Create or open file - let mut file = match File::create(path) { - Ok(file) => file, - Err(e) => return Err(WotWriteError::FailToCreateFile(e)), - }; - // Write buffer in file - file.write_all(&buffer)?; - - Ok(()) - } -} diff --git a/lib/dubp/wot/operations/mod.rs b/lib/dubp/wot/operations/mod.rs index 4a122046..cbb938ee 100644 --- a/lib/dubp/wot/operations/mod.rs +++ b/lib/dubp/wot/operations/mod.rs @@ -17,5 +17,4 @@ pub mod centrality; pub mod distance; -pub mod file; pub mod path; diff --git a/lib/dubp/wot/operations/path.rs b/lib/dubp/wot/operations/path.rs index 75e22f0a..99a3a8e3 100644 --- a/lib/dubp/wot/operations/path.rs +++ b/lib/dubp/wot/operations/path.rs @@ -15,14 +15,14 @@ //! Provide a trait and implementations to find paths between nodes. -use crate::data::NodeId; use crate::data::WebOfTrust; +use crate::data::WotId; use std::collections::HashSet; /// Find paths between 2 nodes of a `WebOfTrust`. pub trait PathFinder<T: WebOfTrust> { /// Get paths from one node to the other. - fn find_paths(&self, wot: &T, from: NodeId, to: NodeId, k_max: u32) -> Vec<Vec<NodeId>>; + fn find_paths(&self, wot: &T, from: WotId, to: WotId, k_max: u32) -> Vec<Vec<WotId>>; } /// A new "rusty-er" implementation of `WoT` path finding. @@ -30,7 +30,7 @@ pub trait PathFinder<T: WebOfTrust> { pub struct RustyPathFinder; impl<T: WebOfTrust> PathFinder<T> for RustyPathFinder { - fn find_paths(&self, wot: &T, from: NodeId, to: NodeId, k_max: u32) -> Vec<Vec<NodeId>> { + fn find_paths(&self, wot: &T, from: WotId, to: WotId, k_max: u32) -> Vec<Vec<WotId>> { if from.0 >= wot.size() || to.0 >= wot.size() { return vec![]; } @@ -40,7 +40,7 @@ impl<T: WebOfTrust> PathFinder<T> for RustyPathFinder { // Stores for each node its distance to `to` node and its backward links. // By default all nodes are out of range (`k_max + 1`) and links are known. - let mut graph: Vec<(u32, Vec<NodeId>)> = + let mut graph: Vec<(u32, Vec<WotId>)> = (0..wot.size()).map(|_| (k_max + 1, vec![])).collect(); // `to` node is at distance 0, and have no backward links. graph[to.0] = (0, vec![]); diff --git a/lib/dubp/wot/tests/g1_genesis.bin b/lib/dubp/wot/tests/g1_genesis.bin index e7838df9c9efb58d10cbbb7f8a3ba1a2a4407db5..d684f6197a5442ff4860dec581a65b19456a51d4 100644 GIT binary patch literal 3223 zcmcC!fB;5O2+hFDz`y`vGcqtRure?(urM$%urn|)XfQA^$TKi7STQg#C^9fGSTZm$ z@G~$la4|42FflMNa56A3h%+!Sm@qIf@Gvki@G&qj=rS-c$S^Q4h(OJTTLO{<87cv_ z0HhgYILH8yT96}@pca5E(t$e40%{@10+2&M7K=eG0hs~P%P0gQp%|nYWGKi$kgGwi z18D?l0{IN&1CU0LFF-y9ISk}`ki$R@0$Bvo4>FDs<YEv*nSp^p80syMVIT`Z8o3!5 z7}!7x7#JAft^nBpG6iHa$W)M%K?Z>|fD8et2Zb%jr63zYwt-v(G8p7ykZS~>;R{mB z2#ORC17tABFc2GLGsp`tIYvQ{C^Y6kwu4Lu`57b(G99ED6qp>)fCU9M$S{!YAe%w4 z1`2dgn1IxS^fAH=00kz<Ss*Wf6oWK_q8el%$Q2+>FbhEjg1iMX1Y{Y=Vvx5$;RLb- zWGN_EKo)?^1JUq+25ABbfD8ax2(nBP8u%bzf#euLfdgWIOacWcEHFWeK_-JVgIoo2 z3CMPkD?o;U^nfA)<QkC8(jWm)%z*+E#0D7%(gZRD#0P~3NF&H#kU=09fLsXjDu@qK z!w51R!~i9EP&xrAW@cbu0HqOCXqE!G66Al7Iz|`+<U~*!0oe-D1TqNZG?;4`LDqqE zfUE>*0)-yPT987Rvp{|Yg}ydOAv8mR<Uw3UkVX&#<{yw^kQm4|kgXtRf}9L83}i4! zEj)BVagXc*kaIzS12P>H|Dad^83GCnkOiR31u_T}vY_+=OOWsY0vRX;ElWTeK=G;o zO^qPiL0$yA0LB1GsxvS!$T2W5s4y@vfZ_|J38V;w;U<Ip0P+*aKcLV9MFl9xLCys^ z8)hIpL_uj6<Zh5#L7G9i2;_E<-#`HbOC_KH10@2Gp(r+j1V9lCvJqqe%r;PffLsDH z0IUHP3Luj}o(7o?%QYayApgJ=gIoj(MUX2%2?69fPy&G&4ss#bP>>;D2DBsw`440$ z$S_byf}#iHY*2!MMIj?7PC%MKnn5Ok6oJG*wu2OcoDK34C^dmx3o;bsB9I<X)PZPt zJ_RWTITa)bG7x4nC{ci101{^eMFWTd@(V}<C@MgTLH+`n4Du%^3_z{{*#Pn@$R(is z42n9C(_w~zVhH3SP|Sf65y)J)Yd{GDlu$sngIof#Kn+@%fMN<oF(_4mOps+@U;rrw zX#}M%P$~nt5Ud#FG!O&i43M=TCxR4%vKc7gK+XjP9LR|vb?{6LG9Bg|kf|U|AlpFx z1}g;V0x>`mASa+}04WC92vWlc@(73l3PBJXWGyI!K&F906qKSsP6LG|D1<<v334)W zVgTufI}c<!$ax@zAkTta0SW+kD8Z}-DFAs66v?2#0NJR|z`y`k2=fO>1IS4rD?y4t z;vgr0oC>l5#d=Ws0=XNMqCpV^G7XeU;0i$|fOLS|1d0%l2SEx!ZU%)E$o(KUgF+5u zGe|Amb0CvJ84_e7$Ocfx1lb5m(O`ujw}BWSYe60~XJB9eDFSH#X#^Pn@-RpZ+*FWN zAS*%E!<+<iCMZ-;6oQf_NCFg5AX7k^KoNuCPLLv)Bq)7@oCVShcPA(+K}LflK&cIs zO+jG_N_`-45QZmykOGj2phy5I0$B&r0MZQd6j(9Lv7lH1`5fd^P>g|m4zd;$3ZNhZ zX##72IYATJ5Cqu(@(jq`a77@iK&nBp0}2{YP=R6y6jUIEFhk&B3Ca;5MIcE%1_lO@ zLXZYfaDfbhdkUrqWHLx0C_RAu08$K62*U7o5l8_@AxILGMnR^76o4??M37Y=NpvTG zG=UU?FkCUn1W?j5g%+?N--8MlkP|=(K^Sg5ND;^bpl}6AfU*rn5P{+uWCBP5C>%iw zK(@d%!Gj3obC5eh9s;=;6s{l-f;56K+*(k!0OcD{Sq-uhWD+PU!HPf*1~EX10+c{N zg#<_;DE>i-1XMeKVh^MN<|%k{0i*zwKtSa%C|!aK0vQOh4P+3=-Jrw*5{Fl*pu!s@ j3DOMmASf4rf)JFlK(@gQ0BHib1Y`(^&7hI}4nzY0n4me& literal 1795 zcmZQzU~smy)HN_LFfcT7bTP3sb9Ht!HFI$_aI$bUbun`@H?edwcXo0$w=gv|b~Q7$ zFf}$db~G_@H83$YH+OY&b9Qz#HD+L7u>KDL4@4Ol7#SG&7#PGE7}yyYWEdFa85npO z7+4t?bQu^d85p=27(^KuG#D6I7#I{87)%%#I2jmt7#OS=7?>Cs_(d5Q#27%pih)6g zfk6qZSq35p*2B)gU;#FQlYv13tWz9pC?D7+OCd0a2W%BT*e&v4OC-RWd10n=f-M&X zJJX7RfsY;R06qo=VFm^v1_o{h24w~Y5h1WDVFm^Pumczv82G`y5(gX41GW+30a37H zKqfITXt07+bAw&N&%nSS2v*Jx7Uc(9$pQ8?Bgmg1@A82C1o5#9*wdn7V7oZLA!r5m zuLW3w5$pq9uzo(Ur3_$mxfmGOz}Cov-7L<)z{?JHpCs5%R$!|{85lSPz=m1DwDN&9 zDT3W94z^PsY#s;Lqrwaf8r)#J#KGDazyU4+jv7#Gfi+ukFffRKgN>PiK^1HrD>(eD z*uZWv0UOT5z`()4V8z40pba)k930}1Ams$Rlo9L&83qOpPOx?cusR8_923|PRtyZx z0$|fPz`-a0PH8OQG{OM(kr>#4OkkayVAm;efE^(L7Lo#oo+JZ<0s{j-8v}zJ1A_{f zqt3t}&A=eZ0XAG2><4jhG7w>4V1_tb7wiF1u$7kJ1g#7X2S`!^1vbckVCBMKtwLZo zGBGeH3xbWc0LKBuuae;8s0a&W39zY5U=1AL^lHfojtnMn0$>9tWF-a$NpM2Z;RSof z3g&N1urc!BAc6#<5LlZMJ6H!N*e*~)1Se!G5pWopfJ2)PtVt9cQmkO>#lRU=2AoCt z!ASv<b`-(h=VAw&!3PO01_m_-26YAoes*vms(?9i3=Fai3{sHLR00RMDA+EDYs48C zwAsMH4hnFv4k-o(1$MBBQeZwLeq<RKR3PPuC^!b0z+r9ycCZ*Yyd=R<F2evSJ1p72 zew7BhSREYRQfy$3KG?e);6UJH2m6r|9E>vHU=w3tU}FQvmoV5nR*=NZ3FgRwT_p>4 z708ufJ0#e^$&(qJN+1c}iXAKjO)Ow*%^4WDAt{U#93TpCy^wN{1?&+?1_nMh7)uu% z<?IX$maO2st_^k|F9U-(JD4R74n|(EawP@^8Ay7w06SO=oJ54dRx7iD?Xm<b)MQ}L zfVe{n<{6MvAW@*ez@P_-D+UHxcCa^P!SM_c1;r36SYDceK_6_W9y{1K`Y??O;HZ>j z2kU|q$pQ=vreL*9tl*$$0;hXZ1_l;L3B>?TLXu!F@w0&qmIh}<J+RRVtl-#FgXMc3 zPO!Jdz%j=NPT2C`j3>&#pu`ClWdNsHNHNI;wwjZHL4yaJkr==+BMR2R2~IX#V3Q@l HX<i%v+>1>u diff --git a/lib/modules/blockchain/blockchain-dal/src/entities/block.rs b/lib/modules/blockchain/blockchain-dal/src/entities/block.rs index e93bdd72..67e27062 100644 --- a/lib/modules/blockchain/blockchain-dal/src/entities/block.rs +++ b/lib/modules/blockchain/blockchain-dal/src/entities/block.rs @@ -17,7 +17,7 @@ use crate::*; use dubp_block_doc::block::{BlockDocument, BlockDocumentTrait}; use dubp_common_doc::traits::Document; use dubp_common_doc::BlockNumber; -use durs_wot::NodeId; +use durs_wot::WotId; use std::collections::HashMap; #[derive(Clone, Debug, Deserialize, Serialize)] @@ -28,7 +28,7 @@ pub struct DALBlock { /// List of certifications that expire in this block. /// Warning : BlockNumber contain the emission block, not the written block ! /// HashMap<(Source, Target), BlockNumber> - pub expire_certs: Option<HashMap<(NodeId, NodeId), BlockNumber>>, + pub expire_certs: Option<HashMap<(WotId, WotId), BlockNumber>>, } impl DALBlock { diff --git a/lib/modules/blockchain/blockchain-dal/src/entities/identity.rs b/lib/modules/blockchain/blockchain-dal/src/entities/identity.rs index ab1e1e43..934002ff 100644 --- a/lib/modules/blockchain/blockchain-dal/src/entities/identity.rs +++ b/lib/modules/blockchain/blockchain-dal/src/entities/identity.rs @@ -15,7 +15,7 @@ use dubp_common_doc::{BlockNumber, Blockstamp}; use dubp_user_docs::documents::identity::IdentityDocumentV10; -use durs_wot::NodeId; +use durs_wot::WotId; #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)] /// Identity state @@ -48,7 +48,7 @@ pub struct DALIdentity { /// Identity document pub idty_doc: IdentityDocumentV10, /// Identity wot id - pub wot_id: NodeId, + pub wot_id: WotId, /// Membership created block number pub ms_created_block_id: BlockNumber, /// Timestamp from which membership can be renewed diff --git a/lib/modules/blockchain/blockchain-dal/src/lib.rs b/lib/modules/blockchain/blockchain-dal/src/lib.rs index 7b2d1158..9335720f 100644 --- a/lib/modules/blockchain/blockchain-dal/src/lib.rs +++ b/lib/modules/blockchain/blockchain-dal/src/lib.rs @@ -56,7 +56,7 @@ use dubp_user_docs::documents::transaction::*; use dup_crypto::hashs::Hash; use dup_crypto::keys::*; use durs_common_tools::fatal_error; -use durs_wot::data::{rusty::RustyWebOfTrust, NodeId}; +use durs_wot::data::{rusty::RustyWebOfTrust, WotId}; use fnv::FnvHashMap; use rustbreak::backend::{FileBackend, MemoryBackend}; use rustbreak::error::{RustbreakError, RustbreakErrorKind}; @@ -89,9 +89,9 @@ pub type WotDB = RustyWebOfTrust; /// V10 Identities indexed by public key pub type IdentitiesV10Datas = HashMap<PubKey, DALIdentity>; /// Memberships sorted by created block -pub type MsExpirV10Datas = FnvHashMap<BlockNumber, HashSet<NodeId>>; +pub type MsExpirV10Datas = FnvHashMap<BlockNumber, HashSet<WotId>>; /// Certifications sorted by created block -pub type CertsExpirV10Datas = FnvHashMap<BlockNumber, HashSet<(NodeId, NodeId)>>; +pub type CertsExpirV10Datas = FnvHashMap<BlockNumber, HashSet<(WotId, WotId)>>; /// V10 Transactions indexed by their hashs pub type TxV10Datas = HashMap<Hash, DALTxV10>; /// V10 Unused Transaction Output (=sources) diff --git a/lib/modules/blockchain/blockchain-dal/src/readers/certs.rs b/lib/modules/blockchain/blockchain-dal/src/readers/certs.rs index f745d953..2c65e770 100644 --- a/lib/modules/blockchain/blockchain-dal/src/readers/certs.rs +++ b/lib/modules/blockchain/blockchain-dal/src/readers/certs.rs @@ -15,14 +15,14 @@ use crate::{BinDB, CertsExpirV10Datas, DALError}; use dubp_common_doc::BlockNumber; -use durs_wot::NodeId; +use durs_wot::WotId; use std::collections::HashMap; /// Find certifications that emitted in indicated blocks expiring pub fn find_expire_certs( certs_db: &BinDB<CertsExpirV10Datas>, blocks_expiring: Vec<BlockNumber>, -) -> Result<HashMap<(NodeId, NodeId), BlockNumber>, DALError> { +) -> Result<HashMap<(WotId, WotId), BlockNumber>, DALError> { Ok(certs_db.read(|db| { let mut all_expire_certs = HashMap::new(); for expire_block_id in blocks_expiring { diff --git a/lib/modules/blockchain/blockchain-dal/src/readers/identity.rs b/lib/modules/blockchain/blockchain-dal/src/readers/identity.rs index 0e931916..b6eb745a 100644 --- a/lib/modules/blockchain/blockchain-dal/src/readers/identity.rs +++ b/lib/modules/blockchain/blockchain-dal/src/readers/identity.rs @@ -19,7 +19,7 @@ use crate::{BinDB, DALError, IdentitiesV10Datas}; use dubp_common_doc::traits::Document; use dubp_common_doc::BlockNumber; use dup_crypto::keys::*; -use durs_wot::NodeId; +use durs_wot::WotId; use std::collections::HashMap; /// Get identities in databases @@ -106,7 +106,7 @@ pub fn get_pubkey_from_uid( /// Get wot_id index pub fn get_wot_index( identities_db: &BinDB<IdentitiesV10Datas>, -) -> Result<HashMap<PubKey, NodeId>, DALError> { +) -> Result<HashMap<PubKey, WotId>, DALError> { Ok(identities_db.read(|db| { db.iter() .map(|(pubkey, member_datas)| (*pubkey, member_datas.wot_id)) @@ -136,7 +136,7 @@ mod test { pubkey, created_block_id, ), - wot_id: NodeId(0), + wot_id: WotId(0), ms_created_block_id: BlockNumber(0), ms_chainable_on: vec![], cert_chainable_on: vec![], diff --git a/lib/modules/blockchain/blockchain-dal/src/tools.rs b/lib/modules/blockchain/blockchain-dal/src/tools.rs index c3e3286a..507a31fe 100644 --- a/lib/modules/blockchain/blockchain-dal/src/tools.rs +++ b/lib/modules/blockchain/blockchain-dal/src/tools.rs @@ -21,7 +21,7 @@ use durs_wot::operations::centrality::{CentralitiesCalculator, UlrikBrandesCentr use durs_wot::operations::distance::{ DistanceCalculator, RustyDistanceCalculator, WotDistance, WotDistanceParameters, }; -use durs_wot::{NodeId, WebOfTrust}; +use durs_wot::{WebOfTrust, WotId}; use std::collections::HashMap; /// CENTRALITY_CALCULATOR @@ -109,7 +109,7 @@ pub fn compute_distances<T: WebOfTrust + Sync>( .compute_distance( wot, WotDistanceParameters { - node: NodeId(i), + node: WotId(i), sentry_requirement, step_max, x_percent, diff --git a/lib/modules/blockchain/blockchain-dal/src/writers/certification.rs b/lib/modules/blockchain/blockchain-dal/src/writers/certification.rs index 63cd60e4..730c9942 100644 --- a/lib/modules/blockchain/blockchain-dal/src/writers/certification.rs +++ b/lib/modules/blockchain/blockchain-dal/src/writers/certification.rs @@ -18,7 +18,7 @@ use dubp_common_doc::BlockNumber; use dubp_currency_params::CurrencyParameters; use dubp_user_docs::documents::certification::CompactCertificationDocumentV10; use dup_crypto::keys::*; -use durs_wot::NodeId; +use durs_wot::WotId; /// Apply "certification" event in databases pub fn write_certification( @@ -26,8 +26,8 @@ pub fn write_certification( identities_db: &BinDB<IdentitiesV10Datas>, certs_db: &BinDB<CertsExpirV10Datas>, source_pubkey: PubKey, - source: NodeId, - target: NodeId, + source: WotId, + target: WotId, created_block_id: BlockNumber, written_timestamp: u64, ) -> Result<(), DALError> { @@ -59,8 +59,8 @@ pub fn revert_write_cert( identities_db: &BinDB<IdentitiesV10Datas>, certs_db: &BinDB<CertsExpirV10Datas>, compact_doc: CompactCertificationDocumentV10, - source: NodeId, - target: NodeId, + source: WotId, + target: WotId, ) -> Result<(), DALError> { // Remove CertsExpirV10Datas entry certs_db.write(|db| { @@ -84,8 +84,8 @@ pub fn revert_write_cert( /// Revert "certification expiry" event in databases pub fn revert_expire_cert( certs_db: &BinDB<CertsExpirV10Datas>, - source: NodeId, - target: NodeId, + source: WotId, + target: WotId, created_block_id: BlockNumber, ) -> Result<(), DALError> { // Reinsert CertsExpirV10Datas entry diff --git a/lib/modules/blockchain/blockchain-dal/src/writers/identity.rs b/lib/modules/blockchain/blockchain-dal/src/writers/identity.rs index b9ed9899..2e8cb65d 100644 --- a/lib/modules/blockchain/blockchain-dal/src/writers/identity.rs +++ b/lib/modules/blockchain/blockchain-dal/src/writers/identity.rs @@ -21,7 +21,7 @@ use dubp_currency_params::CurrencyParameters; use dubp_user_docs::documents::identity::IdentityDocumentV10; use dup_crypto::keys::PubKey; use durs_common_tools::fatal_error; -use durs_wot::NodeId; +use durs_wot::WotId; /// Remove identity from databases pub fn revert_create_identity( @@ -57,7 +57,7 @@ pub fn create_identity( ms_db: &BinDB<MsExpirV10Datas>, idty_doc: &IdentityDocumentV10, ms_created_block_id: BlockNumber, - wot_id: NodeId, + wot_id: WotId, current_blockstamp: Blockstamp, current_bc_time: u64, ) -> Result<(), DALError> { @@ -181,7 +181,7 @@ pub fn renewal_identity( identities_db: &BinDB<IdentitiesV10Datas>, ms_db: &BinDB<MsExpirV10Datas>, pubkey: &PubKey, - idty_wot_id: NodeId, + idty_wot_id: WotId, renewal_timestamp: u64, ms_created_block_id: BlockNumber, revert: bool, diff --git a/lib/modules/blockchain/blockchain-dal/src/writers/requests.rs b/lib/modules/blockchain/blockchain-dal/src/writers/requests.rs index 7ece8baf..b79c7980 100644 --- a/lib/modules/blockchain/blockchain-dal/src/writers/requests.rs +++ b/lib/modules/blockchain/blockchain-dal/src/writers/requests.rs @@ -23,7 +23,7 @@ use dubp_currency_params::CurrencyParameters; use dubp_user_docs::documents::certification::CompactCertificationDocumentV10; use dubp_user_docs::documents::identity::IdentityDocumentV10; use dup_crypto::keys::PubKey; -use durs_wot::NodeId; +use durs_wot::WotId; use std::ops::Deref; #[derive(Debug, Clone)] @@ -96,7 +96,7 @@ impl BlocksDBsWriteQuery { pub enum WotsDBsWriteQuery { /// Newcomer (wot_id, blockstamp, current_bc_time, idty_doc, ms_created_block_id) CreateIdentity( - NodeId, + WotId, Blockstamp, u64, Box<IdentityDocumentV10>, @@ -105,9 +105,9 @@ pub enum WotsDBsWriteQuery { /// Revert newcomer event (wot_id, blockstamp, current_bc_time, idty_doc, ms_created_block_id) RevertCreateIdentity(PubKey), /// Active (pubKey, idty_wot_id, current_bc_time, ms_created_block_id) - RenewalIdentity(PubKey, NodeId, u64, BlockNumber), + RenewalIdentity(PubKey, WotId, u64, BlockNumber), /// Revert active (pubKey, idty_wot_id, current_bc_time, ms_created_block_id) - RevertRenewalIdentity(PubKey, NodeId, u64, BlockNumber), + RevertRenewalIdentity(PubKey, WotId, u64, BlockNumber), /// Excluded ExcludeIdentity(PubKey, Blockstamp), /// Revert exclusion @@ -117,13 +117,13 @@ pub enum WotsDBsWriteQuery { /// Revert revocation RevertRevokeIdentity(PubKey, Blockstamp, bool), /// Certification (source_pubkey, source, target, created_block_id, median_time) - CreateCert(PubKey, NodeId, NodeId, BlockNumber, u64), + CreateCert(PubKey, WotId, WotId, BlockNumber, u64), /// Revert certification (source_pubkey, source, target, created_block_id, median_time) - RevertCert(CompactCertificationDocumentV10, NodeId, NodeId), + RevertCert(CompactCertificationDocumentV10, WotId, WotId), /// Certification expiry (source, target, created_block_id) ExpireCerts(BlockNumber), /// Revert certification expiry event (source, target, created_block_id) - RevertExpireCert(NodeId, NodeId, BlockNumber), + RevertExpireCert(WotId, WotId, BlockNumber), } impl WotsDBsWriteQuery { diff --git a/lib/modules/blockchain/blockchain/src/dbex.rs b/lib/modules/blockchain/blockchain/src/dbex.rs index cfd00ab2..fe31cd62 100644 --- a/lib/modules/blockchain/blockchain/src/dbex.rs +++ b/lib/modules/blockchain/blockchain/src/dbex.rs @@ -289,11 +289,11 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { readers::identity::get_wot_index(&wot_databases.identities_db).expect("DALError"); // get wot_reverse_index - let wot_reverse_index: HashMap<NodeId, &PubKey> = + let wot_reverse_index: HashMap<WotId, &PubKey> = wot_index.iter().map(|(p, id)| (*id, p)).collect(); // get wot uid index - let wot_uid_index: HashMap<NodeId, String> = wot_databases + let wot_uid_index: HashMap<WotId, String> = wot_databases .identities_db .read(|db| { db.iter() @@ -320,7 +320,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { DbExWotQuery::AllDistances(ref reverse) => { println!("compute distances..."); let compute_distances_begin = SystemTime::now(); - let mut distances_datas: Vec<(NodeId, WotDistance)> = wot_db + let mut distances_datas: Vec<(WotId, WotDistance)> = wot_db .read(|db| { db.get_enabled() .iter() @@ -389,7 +389,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { .expect("Fail to read blockchain db"); // Get expire_dates let min_created_ms_time = current_bc_time - currency_params.ms_validity; - let mut expire_dates: Vec<(NodeId, u64)> = wot_databases + let mut expire_dates: Vec<(WotId, u64)> = wot_databases .ms_db .read(|db| { let mut expire_dates = Vec::new(); diff --git a/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs b/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs index 1ecda1f8..b7047a04 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs @@ -26,7 +26,7 @@ use durs_blockchain_dal::writers::requests::*; use durs_blockchain_dal::BinDB; use durs_common_tools::fatal_error; use durs_wot::data::NewLinkResult; -use durs_wot::{NodeId, WebOfTrust}; +use durs_wot::{WebOfTrust, WotId}; use std::collections::{HashMap, HashSet}; #[derive(Debug, Clone)] @@ -48,9 +48,9 @@ pub enum ApplyValidBlockError { #[inline] pub fn apply_valid_block<W: WebOfTrust>( block: BlockDocument, - wot_index: &mut HashMap<PubKey, NodeId>, + wot_index: &mut HashMap<PubKey, WotId>, wot_db: &BinDB<W>, - expire_certs: &HashMap<(NodeId, NodeId), BlockNumber>, + expire_certs: &HashMap<(WotId, WotId), BlockNumber>, ) -> Result<ValidBlockApplyReqs, ApplyValidBlockError> { match block { BlockDocument::V10(block_v10) => { @@ -61,9 +61,9 @@ pub fn apply_valid_block<W: WebOfTrust>( pub fn apply_valid_block_v10<W: WebOfTrust>( mut block: BlockDocumentV10, - wot_index: &mut HashMap<PubKey, NodeId>, + wot_index: &mut HashMap<PubKey, WotId>, wot_db: &BinDB<W>, - expire_certs: &HashMap<(NodeId, NodeId), BlockNumber>, + expire_certs: &HashMap<(WotId, WotId), BlockNumber>, ) -> Result<ValidBlockApplyReqs, ApplyValidBlockError> { debug!( "BlockchainModule : apply_valid_block({})", @@ -80,7 +80,7 @@ pub fn apply_valid_block_v10<W: WebOfTrust>( let pubkey = joiner.issuers()[0]; if let Some(idty_doc) = identities.get(&pubkey) { // Newcomer - let wot_id = NodeId( + let wot_id = WotId( wot_db .read(WebOfTrust::size) .expect("Fatal error : fail to read WotDB !"), diff --git a/lib/modules/blockchain/blockchain/src/dubp/check/mod.rs b/lib/modules/blockchain/blockchain/src/dubp/check/mod.rs index 4ad8c269..9d13dd06 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/check/mod.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/check/mod.rs @@ -36,7 +36,7 @@ pub fn verify_block_validity<W: WebOfTrust>( block: &BlockDocument, blockchain_db: &BinDB<LocalBlockchainV10Datas>, _certs_db: &BinDB<CertsExpirV10Datas>, - _wot_index: &HashMap<PubKey, NodeId>, + _wot_index: &HashMap<PubKey, WotId>, _wot_db: &BinDB<W>, ) -> Result<(), BlockError> { // Rules that do not concern genesis block diff --git a/lib/modules/blockchain/blockchain/src/fork/revert_block.rs b/lib/modules/blockchain/blockchain/src/fork/revert_block.rs index 2f1da648..16a457d8 100644 --- a/lib/modules/blockchain/blockchain/src/fork/revert_block.rs +++ b/lib/modules/blockchain/blockchain/src/fork/revert_block.rs @@ -28,7 +28,7 @@ use durs_blockchain_dal::writers::transaction::DALTxV10; use durs_blockchain_dal::{BinDB, DALError, TxV10Datas}; use durs_common_tools::fatal_error; use durs_wot::data::{NewLinkResult, RemLinkResult}; -use durs_wot::{NodeId, WebOfTrust}; +use durs_wot::{WebOfTrust, WotId}; use std::collections::HashMap; use unwrap::unwrap; @@ -57,7 +57,7 @@ impl From<DALError> for RevertValidBlockError { pub fn revert_block<W: WebOfTrust>( dal_block: DALBlock, - wot_index: &mut HashMap<PubKey, NodeId>, + wot_index: &mut HashMap<PubKey, WotId>, wot_db: &BinDB<W>, txs_db: &BinDB<TxV10Datas>, ) -> Result<ValidBlockRevertReqs, RevertValidBlockError> { @@ -74,8 +74,8 @@ pub fn revert_block<W: WebOfTrust>( pub fn revert_block_v10<W: WebOfTrust>( mut block: BlockDocumentV10, - expire_certs: HashMap<(NodeId, NodeId), BlockNumber>, - wot_index: &mut HashMap<PubKey, NodeId>, + expire_certs: HashMap<(WotId, WotId), BlockNumber>, + wot_index: &mut HashMap<PubKey, WotId>, wot_db: &BinDB<W>, txs_db: &BinDB<TxV10Datas>, ) -> Result<ValidBlockRevertReqs, RevertValidBlockError> { diff --git a/lib/modules/blockchain/blockchain/src/lib.rs b/lib/modules/blockchain/blockchain/src/lib.rs index 9662998b..3b08651b 100644 --- a/lib/modules/blockchain/blockchain/src/lib.rs +++ b/lib/modules/blockchain/blockchain/src/lib.rs @@ -76,7 +76,7 @@ use durs_network::{ }; // use durs_wot::data::rusty::RustyWebOfTrust; use durs_wot::operations::distance::RustyDistanceCalculator; -use durs_wot::NodeId; +use durs_wot::WotId; use failure::Error; /// The blocks are requested by packet groups. This constant sets the block packet size. @@ -102,7 +102,7 @@ pub struct BlockchainModule { /// Forks Databases pub forks_dbs: ForksDBs, /// Wot index - pub wot_index: HashMap<PubKey, NodeId>, + pub wot_index: HashMap<PubKey, WotId>, /// Wots Databases pub wot_databases: WotsV10DBs, /// Currency databases @@ -214,7 +214,7 @@ impl BlockchainModule { }; // Get wot index - let wot_index: HashMap<PubKey, NodeId> = + let wot_index: HashMap<PubKey, WotId> = readers::identity::get_wot_index(&wot_databases.identities_db) .expect("Fatal eror : get_wot_index : Fail to read blockchain databases"); diff --git a/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs b/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs index 449301d3..d21117ca 100644 --- a/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs +++ b/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs @@ -31,7 +31,7 @@ use durs_blockchain_dal::{BinDB, CertsExpirV10Datas, WotsV10DBs}; use durs_common_tools::fatal_error; use durs_network_documents::url::Url; use durs_wot::data::rusty::RustyWebOfTrust; -use durs_wot::data::NodeId; +use durs_wot::data::WotId; use std::collections::{HashMap, VecDeque}; use std::path::PathBuf; use std::sync::mpsc; @@ -57,7 +57,7 @@ pub struct BlockApplicator { pub blocks_not_expiring: VecDeque<u64>, pub last_block_expiring: isize, // databases - pub wot_index: HashMap<PubKey, NodeId>, + pub wot_index: HashMap<PubKey, WotId>, pub wot_databases: WotsV10DBs, pub certs_db: BinDB<CertsExpirV10Datas>, // time measurement diff --git a/lib/modules/blockchain/blockchain/src/sync/mod.rs b/lib/modules/blockchain/blockchain/src/sync/mod.rs index d63ded78..a5ddfa8f 100644 --- a/lib/modules/blockchain/blockchain/src/sync/mod.rs +++ b/lib/modules/blockchain/blockchain/src/sync/mod.rs @@ -26,7 +26,7 @@ use dup_crypto::keys::*; use durs_blockchain_dal::writers::requests::*; use durs_blockchain_dal::{open_memory_db, CertsExpirV10Datas}; use durs_common_tools::fatal_error; -use durs_wot::NodeId; +use durs_wot::WotId; use failure::Fail; use pbr::ProgressBar; use std::collections::{HashMap, VecDeque}; @@ -198,7 +198,7 @@ pub fn local_sync<DC: DursConfTrait>( } // Get wot index - let wot_index: HashMap<PubKey, NodeId> = + let wot_index: HashMap<PubKey, WotId> = readers::identity::get_wot_index(&wot_databases.identities_db) .expect("Fatal eror : get_wot_index : Fail to read blockchain databases"); -- GitLab