Enable setting a fixed dandelion peer

This commit is contained in:
Yoni 2018-12-31 18:49:05 +02:00
parent 738d49d560
commit b2cc5ba7e1
3 changed files with 34 additions and 13 deletions

View file

@ -230,6 +230,10 @@ fn comments() -> HashMap<String, String> {
# 15 = Bit flags for FULL_NODE # 15 = Bit flags for FULL_NODE
#This structure needs to be changed internally, to make it more configurable #This structure needs to be changed internally, to make it more configurable
# A prefered dandelion_peer, mainly used for testing dandelion
# dandelion_peer = \"10.0.0.1:13144\"
" "
.to_string(), .to_string(),
); );

View file

@ -83,24 +83,38 @@ impl Peers {
// Update the dandelion relay // Update the dandelion relay
pub fn update_dandelion_relay(&self) { pub fn update_dandelion_relay(&self) {
let peers = self.outgoing_connected_peers(); let peers = self.outgoing_connected_peers();
match &self.config.dandelion_peer {
match thread_rng().choose(&peers) { Some(ip) => {
Some(peer) => { for peer in &peers {
// Clear the map and add new relay if peer.info.addr == *ip {
let dandelion_relay = &self.dandelion_relay; debug!("Found predefined dandelion peer, setting as relay");
dandelion_relay.write().clear(); self.set_dandelion_relay(peer);
dandelion_relay return;
.write() }
.insert(Utc::now().timestamp(), peer.clone()); }
debug!( debug!("Could not find predefined dandelion peer among connected peers, choosing random")
"Successfully updated Dandelion relay to: {}",
peer.info.addr
);
} }
None => {}
}
match thread_rng().choose(&peers) {
Some(peer) => self.set_dandelion_relay(peer),
None => debug!("Could not update dandelion relay"), 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 // Get the dandelion relay
pub fn get_dandelion_relay(&self) -> HashMap<i64, Arc<Peer>> { pub fn get_dandelion_relay(&self) -> HashMap<i64, Arc<Peer>> {
self.dandelion_relay.read().clone() self.dandelion_relay.read().clone()

View file

@ -124,6 +124,8 @@ pub struct P2PConfig {
pub peer_max_count: Option<u32>, pub peer_max_count: Option<u32>,
pub peer_min_preferred_count: Option<u32>, pub peer_min_preferred_count: Option<u32>,
pub dandelion_peer: Option<SocketAddr>,
} }
/// Default address for peer-to-peer connections. /// Default address for peer-to-peer connections.
@ -142,6 +144,7 @@ impl Default for P2PConfig {
ban_window: None, ban_window: None,
peer_max_count: None, peer_max_count: None,
peer_min_preferred_count: None, peer_min_preferred_count: None,
dandelion_peer: None
} }
} }
} }