Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
!fixup Use slice instead of Vec for request_backoff.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo committed Jun 7, 2018
1 parent ff11cfc commit 364c8e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions util/network-devp2p/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ const PING_TIMEOUT: Duration = Duration::from_millis(300);
const FIND_NODE_TIMEOUT: Duration = Duration::from_secs(2);
const EXPIRY_TIME: Duration = Duration::from_secs(60);
const MAX_NODES_PING: usize = 32; // Max nodes to add/ping at once
const REQUEST_BACKOFF: [u64; 4] = [1, 4, 16, 64];
const REQUEST_BACKOFF: [Duration; 4] = [
Duration::from_secs(1),
Duration::from_secs(4),
Duration::from_secs(16),
Duration::from_secs(64)
];

#[derive(Clone, Debug)]
pub struct NodeEntry {
Expand Down Expand Up @@ -110,7 +115,7 @@ struct Datagramm {
address: SocketAddr,
}

pub struct Discovery {
pub struct Discovery<'a> {
id: NodeId,
id_hash: H256,
secret: Secret,
Expand All @@ -128,16 +133,16 @@ pub struct Discovery {
check_timestamps: bool,
adding_nodes: Vec<NodeEntry>,
ip_filter: IpFilter,
request_backoff: Vec<Duration>,
request_backoff: &'a [Duration],
}

pub struct TableUpdates {
pub added: HashMap<NodeId, NodeEntry>,
pub removed: HashSet<NodeId>,
}

impl Discovery {
pub fn new(key: &KeyPair, listen: SocketAddr, public: NodeEndpoint, token: StreamToken, ip_filter: IpFilter) -> Discovery {
impl<'a> Discovery<'a> {
pub fn new(key: &KeyPair, listen: SocketAddr, public: NodeEndpoint, token: StreamToken, ip_filter: IpFilter) -> Discovery<'static> {
let socket = UdpSocket::bind(&listen).expect("Error binding UDP socket");
Discovery {
id: key.public().clone(),
Expand All @@ -157,7 +162,7 @@ impl Discovery {
check_timestamps: true,
adding_nodes: Vec::new(),
ip_filter: ip_filter,
request_backoff: REQUEST_BACKOFF.iter().map(|s| Duration::from_secs(*s)).collect(),
request_backoff: &REQUEST_BACKOFF,
}
}

Expand Down Expand Up @@ -857,8 +862,9 @@ mod tests {
fn removes_expired() {
let key = Random.generate().unwrap();
let ep = NodeEndpoint { address: SocketAddr::from_str("127.0.0.1:40446").unwrap(), udp_port: 40447 };
let mut discovery = Discovery::new(&key, ep.address.clone(), ep.clone(), 0, IpFilter::default());
discovery.request_backoff = vec![];
let discovery = Discovery::new(&key, ep.address.clone(), ep.clone(), 0, IpFilter::default());

let mut discovery = Discovery { request_backoff: &[], ..discovery };

let total_bucket_nodes = |node_buckets: &Vec<NodeBucket>| -> usize {
node_buckets.iter().map(|bucket| bucket.nodes.len()).sum()
Expand Down Expand Up @@ -914,7 +920,8 @@ mod tests {
assert_eq!(removed, 0);

// Test bucket evictions with retries.
discovery.request_backoff = vec![Duration::new(0, 0); 2];
let request_backoff = [Duration::new(0, 0); 2];
let mut discovery = Discovery { request_backoff: &request_backoff, ..discovery };

for _ in 0..2 {
discovery.ping(&node_entries[101]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion util/network-devp2p/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub struct Host {
pub info: RwLock<HostInfo>,
tcp_listener: Mutex<TcpListener>,
sessions: Arc<RwLock<Slab<SharedSession>>>,
discovery: Mutex<Option<Discovery>>,
discovery: Mutex<Option<Discovery<'static>>>,
nodes: RwLock<NodeTable>,
handlers: RwLock<HashMap<ProtocolId, Arc<NetworkProtocolHandler + Sync>>>,
timers: RwLock<HashMap<TimerToken, ProtocolTimer>>,
Expand Down

0 comments on commit 364c8e1

Please sign in to comment.