Skip to content

Commit

Permalink
Merge pull request #3802 from doitian/bugfix/filter-alerts
Browse files Browse the repository at this point in the history
fix: p2p alerts are not filterred in block chain info
  • Loading branch information
doitian authored Jan 17, 2023
2 parents ff0186f + 8723ba2 commit 5c92ec7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion util/launcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Launcher {

let alert_signature_config = self.args.config.alert_signature.clone().unwrap_or_default();
let alert_relayer = AlertRelayer::new(
self.version.to_string(),
self.version.short(),
shared.notify_controller().clone(),
alert_signature_config,
);
Expand Down
35 changes: 28 additions & 7 deletions util/network-alert/src/notifier.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! notifier module
use ckb_logger::debug;
use ckb_logger::{debug, error};
use ckb_notify::NotifyController;
use ckb_types::{packed::Alert, prelude::*};
use lru::LruCache;
use semver::Version;
use std::collections::HashMap;

const CANCEL_FILTER_SIZE: usize = 128;
Expand All @@ -15,25 +16,35 @@ pub struct Notifier {
received_alerts: HashMap<u32, Alert>,
/// alerts that self node should notice
noticed_alerts: Vec<Alert>,
client_version: String,
client_version: Option<Version>,
notify_controller: NotifyController,
}

impl Notifier {
/// Init
pub fn new(client_version: String, notify_controller: NotifyController) -> Self {
let parsed_client_version = match Version::parse(&client_version) {
Ok(version) => Some(version),
Err(err) => {
error!(
"Invalid version {} for alert notifier: {}",
client_version, err
);
None
}
};

Notifier {
cancel_filter: LruCache::new(CANCEL_FILTER_SIZE),
received_alerts: Default::default(),
noticed_alerts: Vec::new(),
client_version,
client_version: parsed_client_version,
notify_controller,
}
}

fn is_version_effective(&self, alert: &Alert) -> bool {
use semver::Version;
if let Ok(client_version) = Version::parse(&self.client_version) {
if let Some(client_version) = &self.client_version {
let test_min_ver_failed = alert
.as_reader()
.raw()
Expand All @@ -42,7 +53,12 @@ impl Notifier {
.and_then(|v| {
v.as_utf8()
.ok()
.and_then(|v| Version::parse(v).map(|min_v| client_version < min_v).ok())
.and_then(|v| {
Version::parse(v)
.as_ref()
.map(|min_v| client_version < min_v)
.ok()
})
.or(Some(true))
})
.unwrap_or(false);
Expand All @@ -57,7 +73,12 @@ impl Notifier {
.and_then(|v| {
v.as_utf8()
.ok()
.and_then(|v| Version::parse(v).map(|max_v| client_version > max_v).ok())
.and_then(|v| {
Version::parse(v)
.as_ref()
.map(|max_v| client_version > max_v)
.ok()
})
.or(Some(true))
})
.unwrap_or(false);
Expand Down

0 comments on commit 5c92ec7

Please sign in to comment.