Skip to content

Commit 728db3f

Browse files
committed
gossip: move flags module from nostr-gossip-memory
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 0c96e0f commit 728db3f

File tree

6 files changed

+86
-69
lines changed

6 files changed

+86
-69
lines changed

gossip/nostr-gossip-memory/src/flags.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

gossip/nostr-gossip-memory/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010
#![warn(clippy::large_futures)]
1111

1212
mod constant;
13-
mod flags;
1413
pub mod prelude;
1514
pub mod store;

gossip/nostr-gossip-memory/src/store.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ use nostr::nips::nip65::{self, RelayMetadata};
1212
use nostr::util::BoxedFuture;
1313
use nostr::{Event, Kind, PublicKey, RelayUrl, TagKind, TagStandard, Timestamp};
1414
use nostr_gossip::error::GossipError;
15+
use nostr_gossip::flags::GossipFlags;
1516
use nostr_gossip::{BestRelaySelection, GossipListKind, GossipPublicKeyStatus, NostrGossip};
1617
use tokio::sync::Mutex;
1718

1819
use crate::constant::{MAX_NIP17_SIZE, MAX_NIP65_SIZE, PUBKEY_METADATA_OUTDATED_AFTER};
19-
use crate::flags::Flags;
2020

2121
#[derive(Default)]
2222
struct PkRelayData {
23-
bitflags: Flags,
23+
bitflags: GossipFlags,
2424
received_events: u64,
2525
last_received_event: Option<Timestamp>,
2626
}
@@ -73,19 +73,19 @@ impl NostrGossipMemory {
7373

7474
for (relay_url, metadata) in nip65::extract_relay_list(event).take(MAX_NIP65_SIZE) {
7575
// New bitflag for the relay
76-
let bitflag: Flags = match metadata {
77-
Some(RelayMetadata::Read) => Flags::READ,
78-
Some(RelayMetadata::Write) => Flags::WRITE,
76+
let bitflag: GossipFlags = match metadata {
77+
Some(RelayMetadata::Read) => GossipFlags::READ,
78+
Some(RelayMetadata::Write) => GossipFlags::WRITE,
7979
None => {
80-
let mut f = Flags::READ;
81-
f.add(Flags::WRITE);
80+
let mut f = GossipFlags::READ;
81+
f.add(GossipFlags::WRITE);
8282
f
8383
}
8484
};
8585

8686
// Create a mask for READ and WRITE flags
87-
let mut read_write_mask: Flags = Flags::READ;
88-
read_write_mask.add(Flags::WRITE);
87+
let mut read_write_mask: GossipFlags = GossipFlags::READ;
88+
read_write_mask.add(GossipFlags::WRITE);
8989

9090
match pk_data.relays.get_mut(relay_url) {
9191
Some(relay_data) => {
@@ -110,11 +110,11 @@ impl NostrGossipMemory {
110110
for relay_url in nip17::extract_relay_list(event).take(MAX_NIP17_SIZE) {
111111
match pk_data.relays.get_mut(relay_url) {
112112
Some(relay_data) => {
113-
relay_data.bitflags.add(Flags::PRIVATE_MESSAGE);
113+
relay_data.bitflags.add(GossipFlags::PRIVATE_MESSAGE);
114114
}
115115
None => {
116116
let mut relay_data = PkRelayData::default();
117-
relay_data.bitflags.add(Flags::PRIVATE_MESSAGE);
117+
relay_data.bitflags.add(GossipFlags::PRIVATE_MESSAGE);
118118

119119
pk_data.relays.insert(relay_url.clone(), relay_data);
120120
}
@@ -132,15 +132,15 @@ impl NostrGossipMemory {
132132
{
133133
let pk_data: &mut PkData =
134134
public_keys.get_or_insert_mut(*public_key, PkData::default);
135-
update_relay_per_user(pk_data, relay_url.clone(), Flags::HINT);
135+
update_relay_per_user(pk_data, relay_url.clone(), GossipFlags::HINT);
136136
}
137137
}
138138
}
139139
}
140140

141141
if let Some(relay_url) = relay_url {
142142
let pk_data: &mut PkData = public_keys.get_or_insert_mut(event.pubkey, PkData::default);
143-
update_relay_per_user(pk_data, relay_url.clone(), Flags::RECEIVED);
143+
update_relay_per_user(pk_data, relay_url.clone(), GossipFlags::RECEIVED);
144144
}
145145
}
146146

@@ -207,69 +207,74 @@ impl NostrGossipMemory {
207207
most_received,
208208
} => {
209209
// Get read relays
210-
relays.extend(self.get_relays_by_flag(&public_keys, public_key, Flags::READ, read));
210+
relays.extend(self.get_relays_by_flag(
211+
&public_keys,
212+
public_key,
213+
GossipFlags::READ,
214+
read,
215+
));
211216

212217
// Get write relays
213218
relays.extend(self.get_relays_by_flag(
214219
&public_keys,
215220
public_key,
216-
Flags::WRITE,
221+
GossipFlags::WRITE,
217222
write,
218223
));
219224

220225
// Get hint relays
221226
relays.extend(self.get_relays_by_flag(
222227
&public_keys,
223228
public_key,
224-
Flags::HINT,
229+
GossipFlags::HINT,
225230
hints,
226231
));
227232

228233
// Get most received relays
229234
relays.extend(self.get_relays_by_flag(
230235
&public_keys,
231236
public_key,
232-
Flags::RECEIVED,
237+
GossipFlags::RECEIVED,
233238
most_received,
234239
));
235240
}
236241
BestRelaySelection::Read { limit } => {
237242
relays.extend(self.get_relays_by_flag(
238243
&public_keys,
239244
public_key,
240-
Flags::READ,
245+
GossipFlags::READ,
241246
limit,
242247
));
243248
}
244249
BestRelaySelection::Write { limit } => {
245250
relays.extend(self.get_relays_by_flag(
246251
&public_keys,
247252
public_key,
248-
Flags::WRITE,
253+
GossipFlags::WRITE,
249254
limit,
250255
));
251256
}
252257
BestRelaySelection::PrivateMessage { limit } => {
253258
relays.extend(self.get_relays_by_flag(
254259
&public_keys,
255260
public_key,
256-
Flags::PRIVATE_MESSAGE,
261+
GossipFlags::PRIVATE_MESSAGE,
257262
limit,
258263
));
259264
}
260265
BestRelaySelection::Hints { limit } => {
261266
relays.extend(self.get_relays_by_flag(
262267
&public_keys,
263268
public_key,
264-
Flags::HINT,
269+
GossipFlags::HINT,
265270
limit,
266271
));
267272
}
268273
BestRelaySelection::MostReceived { limit } => {
269274
relays.extend(self.get_relays_by_flag(
270275
&public_keys,
271276
public_key,
272-
Flags::RECEIVED,
277+
GossipFlags::RECEIVED,
273278
limit,
274279
));
275280
}
@@ -282,7 +287,7 @@ impl NostrGossipMemory {
282287
&self,
283288
tx: &LruCache<PublicKey, PkData>,
284289
public_key: &PublicKey,
285-
flag: Flags,
290+
flag: GossipFlags,
286291
limit: usize,
287292
) -> impl Iterator<Item = RelayUrl> + '_ {
288293
let mut relays: Vec<(RelayUrl, u64, Option<Timestamp>)> = Vec::new();
@@ -312,7 +317,7 @@ impl NostrGossipMemory {
312317
}
313318

314319
/// Add relay per user or update the received events and bitflags.
315-
fn update_relay_per_user(pk_data: &mut PkData, relay_url: RelayUrl, flags: Flags) {
320+
fn update_relay_per_user(pk_data: &mut PkData, relay_url: RelayUrl, flags: GossipFlags) {
316321
match pk_data.relays.get_mut(&relay_url) {
317322
Some(relay_data) => {
318323
relay_data.bitflags.add(flags);

gossip/nostr-gossip/src/flags.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Gossip flags
2+
3+
/// Gossip flags
4+
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
5+
pub struct GossipFlags(u16);
6+
7+
impl GossipFlags {
8+
/// Empty flags.
9+
pub const NONE: Self = Self(0); // 0
10+
11+
/// Read flag.
12+
pub const READ: Self = Self(1 << 0); // 1
13+
14+
/// Write flag.
15+
pub const WRITE: Self = Self(1 << 1); // 2
16+
17+
/// Private message (NIP-17) flag.
18+
pub const PRIVATE_MESSAGE: Self = Self(1 << 2); // 4
19+
20+
/// Hint flag.
21+
pub const HINT: Self = Self(1 << 3); // 8
22+
23+
/// Received flag.
24+
pub const RECEIVED: Self = Self(1 << 4); // 16
25+
26+
/// New empty flags.
27+
#[inline]
28+
pub const fn new() -> Self {
29+
Self::NONE
30+
}
31+
32+
/// Add flag.
33+
#[inline]
34+
pub const fn add(&mut self, other: Self) {
35+
self.0 |= other.0;
36+
}
37+
38+
/// Remove flag.
39+
#[inline]
40+
pub const fn remove(&mut self, other: Self) {
41+
self.0 ^= other.0;
42+
}
43+
44+
/// Check if has flag.
45+
#[inline]
46+
pub const fn has(&self, other: Self) -> bool {
47+
self.0 & other.0 != 0
48+
}
49+
50+
/// Get flags as [`u16`].
51+
#[inline]
52+
pub const fn as_u16(&self) -> u16 {
53+
self.0
54+
}
55+
}

gossip/nostr-gossip/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::fmt::Debug;
1616
use nostr::prelude::*;
1717

1818
pub mod error;
19+
pub mod flags;
1920
pub mod prelude;
2021

2122
use self::error::GossipError;

gossip/nostr-gossip/src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
pub use nostr::prelude::*;
1212

1313
pub use crate::error::*;
14+
pub use crate::flags::*;
1415
pub use crate::*;

0 commit comments

Comments
 (0)