Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Enable setting a node address for a dandelion peer #2263

Merged
merged 3 commits into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ fn comments() -> HashMap<String, String> {

# 15 = Bit flags for FULL_NODE
#This structure needs to be changed internally, to make it more configurable

# A prefered dandelion_peer, mainly used for testing dandelion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: preferred

# dandelion_peer = \"10.0.0.1:13144\"

"
.to_string(),
);
Expand Down
40 changes: 27 additions & 13 deletions p2p/src/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,38 @@ impl Peers {
// Update the dandelion relay
pub fn update_dandelion_relay(&self) {
let peers = self.outgoing_connected_peers();

match thread_rng().choose(&peers) {
Some(peer) => {
// Clear the map and add new relay
let dandelion_relay = &self.dandelion_relay;
dandelion_relay.write().clear();
dandelion_relay
.write()
.insert(Utc::now().timestamp(), peer.clone());
debug!(
"Successfully updated Dandelion relay to: {}",
peer.info.addr
);
match &self.config.dandelion_peer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would likely look nicer (and more idiomatic) if you first looked for and assigned the peer your want to set as relay, and then did the set on that. Look for the find method from Iterator.

Some(ip) => {
for peer in &peers {
if peer.info.addr == *ip {
debug!("Found predefined dandelion peer, setting as relay");
self.set_dandelion_relay(peer);
return;
}
}
debug!("Could not find predefined dandelion peer among connected peers, choosing random")
}
None => {}
}
match thread_rng().choose(&peers) {
Some(peer) => self.set_dandelion_relay(peer),
None => debug!("Could not update dandelion relay"),
};
}

fn set_dandelion_relay(&self, peer: &Arc<Peer>) {
// Clear the map and add new relay
let dandelion_relay = &self.dandelion_relay;
dandelion_relay.write().clear();
dandelion_relay
.write()
.insert(Utc::now().timestamp(), peer.clone());
debug!(
"Successfully updated Dandelion relay to: {}",
peer.info.addr
);
}

// Get the dandelion relay
pub fn get_dandelion_relay(&self) -> HashMap<i64, Arc<Peer>> {
self.dandelion_relay.read().clone()
Expand Down
3 changes: 3 additions & 0 deletions p2p/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ pub struct P2PConfig {
pub peer_max_count: Option<u32>,

pub peer_min_preferred_count: Option<u32>,

pub dandelion_peer: Option<SocketAddr>,
}

/// Default address for peer-to-peer connections.
Expand All @@ -142,6 +144,7 @@ impl Default for P2PConfig {
ban_window: None,
peer_max_count: None,
peer_min_preferred_count: None,
dandelion_peer: None
}
}
}
Expand Down