Skip to content

Commit

Permalink
Correct name
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Sep 8, 2022
1 parent ad01569 commit 0b09f60
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
30 changes: 15 additions & 15 deletions src/discv5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions src/discv5/test.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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));
}
6 changes: 3 additions & 3 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0b09f60

Please sign in to comment.