Skip to content

Commit

Permalink
database: check if RawEvent is expired in `DatabaseIndexes::index_r…
Browse files Browse the repository at this point in the history
…aw_event`
  • Loading branch information
yukibtc committed Dec 8, 2023
1 parent ccb0d82 commit 5c8dd37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ precommit:
bench:
RUSTFLAGS='--cfg=bench' cargo +nightly bench -p nostr

indexes-perf:
cd crates/nostr-database/fuzz/perf && make graph

clean:
cargo clean

Expand Down
12 changes: 10 additions & 2 deletions crates/nostr-database/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ impl DatabaseIndexes {
events
.into_iter()
.map(|w| w.raw)
.filter(|raw| !raw.is_expired(&now) && !raw.is_ephemeral())
.filter(|raw| !raw.is_ephemeral())
.for_each(|event| {
let _ = self.index_raw_event(&mut index, &mut deleted, &mut to_discard, event);
let _ =
self.index_raw_event(&mut index, &mut deleted, &mut to_discard, event, &now);
});

// Remove events
Expand All @@ -266,6 +267,7 @@ impl DatabaseIndexes {
deleted: &mut HashSet<EventId>,
to_discard: &mut HashSet<EventId>,
raw: RawEvent,
now: &Timestamp,
) -> Result<(), Error> {
// Parse event ID
let event_id: EventId = EventId::from_slice(&raw.id)?;
Expand All @@ -275,6 +277,12 @@ impl DatabaseIndexes {
return Ok(());
}

// Check if is expired
if raw.is_expired(now) {
to_discard.insert(event_id);
return Ok(());
}

// Compose others fields
let pubkey_prefix: PublicKeyPrefix = PublicKeyPrefix::from(raw.pubkey);
let timestamp = Timestamp::from(raw.created_at);
Expand Down

0 comments on commit 5c8dd37

Please sign in to comment.