Skip to content

Commit

Permalink
Fix doc tests; Make neighbor validator non-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Nov 5, 2021
1 parent 6763d69 commit 58b2f4d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
6 changes: 4 additions & 2 deletions bee-autopeering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand All @@ -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();
Expand All @@ -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).
Expand Down
8 changes: 4 additions & 4 deletions bee-autopeering/examples/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -109,7 +109,7 @@ async fn main() {
local,
peerstore_config,
quit_signal,
Some(neighbor_validator),
neighbor_validator,
)
.await
.expect("initializing autopeering system failed");
Expand Down
2 changes: 1 addition & 1 deletion bee-autopeering/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn init<S, I, Q, V>(
local: Local,
peerstore_config: <S as PeerStore>::Config,
quit_signal: Q,
neighbor_validator: Option<V>,
neighbor_validator: V,
) -> Result<EventRx, Box<dyn error::Error>>
where
S: PeerStore + 'static,
Expand Down
12 changes: 7 additions & 5 deletions bee-autopeering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
//! * 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#"
//! {
//! "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
Expand All @@ -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();
Expand All @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion bee-autopeering/src/peer/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
10 changes: 5 additions & 5 deletions bee-autopeering/src/peering/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) struct NeighborFilter<V: NeighborValidator> {
}

impl<V: NeighborValidator> NeighborFilter<V> {
pub fn new(local_id: PeerId, validator: Option<V>) -> Self {
pub fn new(local_id: PeerId, validator: V) -> Self {
Self {
inner: Arc::new(RwLock::new(NeighborFilterInner::new(local_id, validator))),
}
Expand All @@ -38,15 +38,15 @@ impl<V: NeighborValidator> NeighborFilter<V> {
pub(crate) struct NeighborFilterInner<V: NeighborValidator> {
local_id: PeerId,
rejected: HashSet<PeerId>,
validator: Option<V>,
validator: V,
}

impl<V: NeighborValidator> NeighborFilterInner<V> {
/// Creates a new filter.
///
/// 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<V>) -> Self {
pub(crate) fn new(local_id: PeerId, validator: V) -> Self {
Self {
local_id,
rejected: HashSet::new(),
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<V: NeighborValidator> NeighborFilterInner<V> {
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
Expand Down Expand Up @@ -118,7 +118,7 @@ mod tests {

fn setup_scenario1() -> (NeighborFilter<DummyValidator>, 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);
Expand Down

0 comments on commit 58b2f4d

Please sign in to comment.