From 0b09f60bb4b7f2f9547c45d66bcc04383b6161e3 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Thu, 8 Sep 2022 12:54:30 +0200 Subject: [PATCH] Correct name --- src/discv5.rs | 30 +++++++++++++++--------------- src/discv5/test.rs | 8 ++++---- src/service.rs | 6 +++--- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/discv5.rs b/src/discv5.rs index d2f0dd72b..fbb8e21e4 100644 --- a/src/discv5.rs +++ b/src/discv5.rs @@ -57,11 +57,11 @@ pub static HASH: for<'a> fn(topic: &'a str) -> TopicHash = |topic| { pub(crate) const KBUCKET_PENDING_TIMEOUT: Duration = Duration::from_secs(60); /// Custom ENR keys. -const ENR_KEY_VERSION: &str = "version"; +const ENR_KEY_FEATURES: &str = "features"; pub const ENR_KEY_TOPICS: &str = "topics"; -/// Discv5 versions. -pub enum Version { +/// Discv5 features. +pub enum Features { /// The protocol for advertising and looking up to topics in Discv5 is supported. Topics = 1, } @@ -156,16 +156,16 @@ impl Discv5 { ))); // This node supports topic requests REGTOPIC and TOPICQUERY, and their responses. - if let Err(e) = - local_enr - .write() - .insert(ENR_KEY_VERSION, &[Version::Topics as u8], &enr_key.write()) - { + if let Err(e) = local_enr.write().insert( + ENR_KEY_FEATURES, + &[Features::Topics as u8], + &enr_key.write(), + ) { error!("Failed writing to enr. Error {:?}", e); return Err("Failed to insert field 'version' into local enr"); } - println!("{:?}", local_enr.read().get(ENR_KEY_VERSION).unwrap()); + println!("{:?}", local_enr.read().get(ENR_KEY_FEATURES).unwrap()); // Update the PermitBan list based on initial configuration *PERMIT_BAN_LIST.write() = config.permit_ban_list.clone(); @@ -868,12 +868,12 @@ impl Drop for Discv5 { } } -/// Check if a given peer supports a given version of the Discv5 protocol. -pub fn check_version(peer: &Enr, version: Version) -> bool { - if let Some(supported_versions) = peer.get(ENR_KEY_VERSION) { - if let Some(supported_versions) = supported_versions.first() { - let version_num = version as u8; - supported_versions & version_num == version_num +/// Check if a given peer supports a given feature of the Discv5 protocol. +pub fn supports_feature(peer: &Enr, feature: Features) -> bool { + if let Some(supported_features) = peer.get(ENR_KEY_FEATURES) { + if let Some(supported_features_num) = supported_features.first() { + let feature_num = feature as u8; + supported_features_num & feature_num == feature_num } else { false } diff --git a/src/discv5/test.rs b/src/discv5/test.rs index 073df9dce..ca3dae1f9 100644 --- a/src/discv5/test.rs +++ b/src/discv5/test.rs @@ -1,7 +1,7 @@ #![cfg(test)] use crate::{ - discv5::{check_version, Version, ENR_KEY_VERSION}, + discv5::{supports_feature, Features, ENR_KEY_FEATURES}, kbucket, Discv5, *, }; use enr::{k256, CombinedKey, Enr, EnrBuilder, EnrKey, NodeId}; @@ -639,9 +639,9 @@ fn test_version_check() { .udp4(port) .build(&key) .unwrap(); - let supported_versions = Version::Topics as u8 | 2; - enr.insert(ENR_KEY_VERSION, &[supported_versions], &key) + let supported_versions = Features::Topics as u8 | 2; + enr.insert(ENR_KEY_FEATURES, &[supported_versions], &key) .unwrap(); - assert!(check_version(&enr, Version::Topics)); + assert!(supports_feature(&enr, Features::Topics)); } diff --git a/src/service.rs b/src/service.rs index 5d4050ea0..d53fb2f10 100644 --- a/src/service.rs +++ b/src/service.rs @@ -22,7 +22,7 @@ use crate::{ topic::TopicHash, Ads, AD_LIFETIME, }, - discv5::{check_version, Version, ENR_KEY_TOPICS, KBUCKET_PENDING_TIMEOUT, PERMIT_BAN_LIST}, + discv5::{supports_feature, Features, ENR_KEY_TOPICS, KBUCKET_PENDING_TIMEOUT, PERMIT_BAN_LIST}, error::{RequestError, ResponseError}, handler::{Handler, HandlerIn, HandlerOut}, kbucket::{ @@ -743,7 +743,7 @@ impl Service { let mut discovered_new_peer = false; if let Some(kbuckets_topic) = self.topics_kbuckets.get_mut(&topic_hash) { for enr in found_enrs { - if !check_version(&enr, Version::Topics) { + if !supports_feature(&enr, Features::Topics) { continue; } trace!("Found new peer {} for topic {}", enr, topic_hash); @@ -936,7 +936,7 @@ impl Service { for entry in self.kbuckets.write().iter() { let enr = entry.node.value.clone(); - if !check_version(&enr, Version::Topics) { + if !supports_feature(&enr, Features::Topics) { continue; } match kbuckets.insert_or_update(entry.node.key, enr, entry.status) {