Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nostr: add pow field to Filter #574

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/nostr-lmdb/src/store/types/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::str::FromStr;

use nostr::nips::nip13;
use nostr::{Filter, SingleLetterTag, Timestamp};
use nostr_database::flatbuffers::event_fbs::Fixed32Bytes;

Expand All @@ -18,6 +19,7 @@ pub struct DatabaseFilter {
pub search: Option<String>,
pub since: Option<Timestamp>,
pub until: Option<Timestamp>,
pub pow: Option<u8>,
pub generic_tags: BTreeMap<SingleLetterTag, BTreeSet<String>>,
}

Expand Down Expand Up @@ -78,6 +80,14 @@ impl DatabaseFilter {
}
}

#[inline]
fn pow_match(&self, event: &DatabaseEvent) -> bool {
match self.pow {
Some(difficulty) => nip13::get_leading_zero_bits(event.id.0) >= difficulty,
None => true,
}
}

#[inline]
pub fn match_event(&self, event: &DatabaseEvent) -> bool {
self.ids_match(event)
Expand All @@ -86,6 +96,7 @@ impl DatabaseFilter {
&& self.since.map_or(true, |t| event.created_at >= t)
&& self.until.map_or(true, |t| event.created_at <= t)
&& self.tag_match(event)
&& self.pow_match(event)
&& self.search_match(event)
}
}
Expand Down Expand Up @@ -121,6 +132,7 @@ impl From<Filter> for DatabaseFilter {
}),
since: filter.since,
until: filter.until,
pow: filter.pow,
generic_tags: filter.generic_tags,
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/nostr/src/types/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ pub struct Filter {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub limit: Option<usize>,
/// Minimum POW difficulty
///
/// <https://github.com/nostr-protocol/nips/blob/master/13.md>
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub pow: Option<u8>,
/// Generic tag queries
#[serde(
flatten,
Expand Down Expand Up @@ -768,6 +774,14 @@ impl Filter {
}
}

#[inline]
fn pow_match(&self, event: &Event) -> bool {
match self.pow {
Some(difficulty) => event.id.check_pow(difficulty),
None => true,
}
}

/// Determine if [Filter] match given [Event].
#[inline]
pub fn match_event(&self, event: &Event) -> bool {
Expand All @@ -777,6 +791,7 @@ impl Filter {
&& self.since.map_or(true, |t| event.created_at >= t)
&& self.until.map_or(true, |t| event.created_at <= t)
&& self.tag_match(event)
&& self.pow_match(event)
&& self.search_match(event)
}
}
Expand Down
Loading