diff --git a/bee-autopeering/README.md b/bee-autopeering/README.md index 2f1770ed90..b4f569f79b 100644 --- a/bee-autopeering/README.md +++ b/bee-autopeering/README.md @@ -20,6 +20,8 @@ use bee_autopeering::{ AutopeeringConfig, Event, Local, NeighborValidator, Peer, ServiceProtocol, AUTOPEERING_SERVICE_NAME, }; +const NETWORK: &str = "chrysalis-mainnet"; + // An example autopeering config in JSON format: fn read_config() -> AutopeeringConfig { let config_json = r#" @@ -41,7 +43,6 @@ async fn main() { // Peers will only accept each other as peer if they agree on the protocol version and the // network name. const VERSION: u32 = 1; - const NETWORK: &str = "chrysalis-mainnet"; // Read the config from a JSON file/string. let config = read_config(); @@ -52,8 +53,9 @@ async fn main() { let mut write = l.write(); write.add_service(AUTOPEERING_SERVICE_NAME, ServiceProtocol::Udp, config.bind_addr.port()); write.add_service(NETWORK, ServiceProtocol::Tcp, 15600); + drop(write) l - } + }; // You can choose between the `InMemoryPeerStore` (non-persistent) and the `SledPeerStore` // (persistent). diff --git a/bee-autopeering/examples/node.rs b/bee-autopeering/examples/node.rs index 1844f015e4..1f79b95ab5 100644 --- a/bee-autopeering/examples/node.rs +++ b/bee-autopeering/examples/node.rs @@ -7,7 +7,7 @@ use bee_autopeering::{ init, peerstore::InMemoryPeerStore, peerstore::{SledPeerStore, SledPeerStoreConfig}, - AutopeeringConfig, Event, Local, NeighborValidator, Peer, ServiceTransport, AUTOPEERING_SERVICE_NAME, + AutopeeringConfig, Event, Local, NeighborValidator, Peer, ServiceProtocol, AUTOPEERING_SERVICE_NAME, }; use libp2p_core::identity::ed25519::Keypair; @@ -80,8 +80,8 @@ async fn main() { let mut keypair = hex::decode(BS16_ED25519_PRIVATE_KEY).expect("error decoding keypair"); let local = Local::from_keypair(Keypair::decode(&mut keypair).expect("error decoding keypair")); let mut write = local.write(); - write.add_service(AUTOPEERING_SERVICE_NAME, ServiceTransport::Udp, config.bind_addr.port()); - write.add_service(NETWORK_SERVICE_NAME, ServiceTransport::Tcp, 15600); + write.add_service(AUTOPEERING_SERVICE_NAME, ServiceProtocol::Udp, config.bind_addr.port()); + write.add_service(NETWORK_SERVICE_NAME, ServiceProtocol::Tcp, 15600); drop(write); // Network parameters. @@ -109,7 +109,7 @@ async fn main() { local, peerstore_config, quit_signal, - Some(neighbor_validator), + neighbor_validator, ) .await .expect("initializing autopeering system failed"); diff --git a/bee-autopeering/src/init.rs b/bee-autopeering/src/init.rs index 2ca85b15b1..c3f91824c8 100644 --- a/bee-autopeering/src/init.rs +++ b/bee-autopeering/src/init.rs @@ -67,7 +67,7 @@ pub async fn init( local: Local, peerstore_config: ::Config, quit_signal: Q, - neighbor_validator: Option, + neighbor_validator: V, ) -> Result> where S: PeerStore + 'static, diff --git a/bee-autopeering/src/lib.rs b/bee-autopeering/src/lib.rs index 40a8490677..9bc349b4c2 100644 --- a/bee-autopeering/src/lib.rs +++ b/bee-autopeering/src/lib.rs @@ -12,13 +12,15 @@ //! * a shutdown signal (`Future`); //! * a peer store, e.g. the `InMemoryPeerStore` (non-persistent) or the `SledPeerStore` (persistent), or a custom peer store implementing the `PeerStore` trait; //! -//!```rust +//!```no_run //! use bee_autopeering::{ //! init, //! peerstore::{SledPeerStore, SledPeerStoreConfig}, //! AutopeeringConfig, Event, Local, NeighborValidator, Peer, ServiceProtocol, AUTOPEERING_SERVICE_NAME, //! }; - +//! +//! const NETWORK: &str = "chrysalis-mainnet"; +//! //! // An example autopeering config in JSON format: //! fn read_config() -> AutopeeringConfig { //! let config_json = r#" @@ -26,7 +28,7 @@ //! "bindAddress": "0.0.0.0:14627", //! "entryNodes": [ //! "/dns/entry-hornet-0.h.chrysalis-mainnet.iotaledger.net/udp/14626/autopeering/iotaPHdAn7eueBnXtikZMwhfPXaeGJGXDt4RBuLuGgb", -//! "/dns/entry-hornet-1.h.chrysalis-mainnet.iotaledger.net/udp/14626/autopeering/iotaJJqMd5CQvv1A61coSQCYW9PNT1QKPs7xh2Qg5K2", +//! "/dns/entry-hornet-1.h.chrysalis-mainnet.iotaledger.net/udp/14626/autopeering/iotaJJqMd5CQvv1A61coSQCYW9PNT1QKPs7xh2Qg5K2" //! ], //! "entryNodesPreferIPv6": false, //! "runAsEntryNode": false @@ -40,7 +42,6 @@ //! // Peers will only accept each other as peer if they agree on the protocol version and the //! // network name. //! const VERSION: u32 = 1; -//! const NETWORK: &str = "chrysalis-mainnet"; //! //! // Read the config from a JSON file/string. //! let config = read_config(); @@ -51,8 +52,9 @@ //! let mut write = l.write(); //! write.add_service(AUTOPEERING_SERVICE_NAME, ServiceProtocol::Udp, config.bind_addr.port()); //! write.add_service(NETWORK, ServiceProtocol::Tcp, 15600); +//! drop(write); //! l -//! } +//! }; //! //! // You can choose between the `InMemoryPeerStore` (non-persistent) and the `SledPeerStore` //! // (persistent). diff --git a/bee-autopeering/src/peer/peer_id.rs b/bee-autopeering/src/peer/peer_id.rs index 65932fd373..17460a3c67 100644 --- a/bee-autopeering/src/peer/peer_id.rs +++ b/bee-autopeering/src/peer/peer_id.rs @@ -190,6 +190,6 @@ mod tests { #[test] fn to_libp2p_peer_id() { let peer_id = PeerId::new_static(); - let _ = peer_id.libp2p_peer_id(); + let _ = peer_id.to_libp2p_peer_id(); } } diff --git a/bee-autopeering/src/peering/filter.rs b/bee-autopeering/src/peering/filter.rs index abaa70adc3..47ba8b1b19 100644 --- a/bee-autopeering/src/peering/filter.rs +++ b/bee-autopeering/src/peering/filter.rs @@ -20,7 +20,7 @@ pub(crate) struct NeighborFilter { } impl NeighborFilter { - pub fn new(local_id: PeerId, validator: Option) -> Self { + pub fn new(local_id: PeerId, validator: V) -> Self { Self { inner: Arc::new(RwLock::new(NeighborFilterInner::new(local_id, validator))), } @@ -38,7 +38,7 @@ impl NeighborFilter { pub(crate) struct NeighborFilterInner { local_id: PeerId, rejected: HashSet, - validator: Option, + validator: V, } impl NeighborFilterInner { @@ -46,7 +46,7 @@ impl NeighborFilterInner { /// /// A peer id same as `local_id` will always be rejected. A `validator` can be provided /// to inject another filter criterium. - pub(crate) fn new(local_id: PeerId, validator: Option) -> Self { + pub(crate) fn new(local_id: PeerId, validator: V) -> Self { Self { local_id, rejected: HashSet::new(), @@ -83,7 +83,7 @@ impl NeighborFilterInner { false } else if self.rejected.contains(peer_id) { false - } else if !self.validator.as_ref().map_or(true, |v| v.is_valid(peer)) { + } else if !self.validator.is_valid(peer) { false } else { true @@ -118,7 +118,7 @@ mod tests { fn setup_scenario1() -> (NeighborFilter, Peer, Peer) { let local_id = Peer::new_test_peer(0).into_id(); - let filter = NeighborFilter::new(local_id, Some(DummyValidator {})); + let filter = NeighborFilter::new(local_id, DummyValidator {}); let mut peer1 = Peer::new_test_peer(1); peer1.add_service(AUTOPEERING_SERVICE_NAME, ServiceProtocol::Udp, 6969);