Skip to content

Commit

Permalink
Merge pull request #946 from input-output-hk/make-trusted-peer-more-r…
Browse files Browse the repository at this point in the history
…andom

make the trusted peer list more random
  • Loading branch information
NicolasDP authored Oct 14, 2019
2 parents 3fc9c86 + 7da3966 commit 012e6a3
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions jormungandr/src/network/p2p/topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,29 @@ pub mod modules {
// trust
BTreeMap::new()
}
fn view(&self, _: &BTreeMap<Id, NodeData>, view: &mut BTreeMap<Id, NodeData>) {
fn view(&self, known_nodes: &BTreeMap<Id, NodeData>, view: &mut BTreeMap<Id, NodeData>) {
const MAX_TRUSTED_PEER_VIEW: usize = 4;
let count = std::cmp::max(
MAX_TRUSTED_PEER_VIEW,
self.peers.len() - std::cmp::min(self.peers.len(), view.len()),
);
view.extend(
self.peers
.iter()
.take(count)
.map(|node| (*node.id(), node.clone())),
)
use rand::seq::SliceRandom;

let mut rng = rand::thread_rng();

let mut peers = self.peers.clone();
peers.shuffle(&mut rng);

for peer in peers.into_iter().take(MAX_TRUSTED_PEER_VIEW) {
// if we received a gossip from the node, then prefer taking the node
// with the appropriate node data as it may have updated its IP address
// as received from a gossip
let peer = if let Some(known_peer) = known_nodes.get(peer.id()) {
known_peer.clone()
} else {
// otherwise use the peer from the trusted list
peer
};

// insert only if not already present
view.entry(*peer.id()).or_insert(peer);
}
}
}
}

0 comments on commit 012e6a3

Please sign in to comment.