Skip to content

Commit 5da0b97

Browse files
committed
database: remove NostrDatabase::has_coordinate_been_deleted
Pull-Request: #917 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 06dd1b6 commit 5da0b97

File tree

8 files changed

+46
-133
lines changed

8 files changed

+46
-133
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- nostr: update `RelayInformationDocument::get` signature ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/913)
3535
- connect: remove `NostrConnect::get_relays` ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/894)
3636
- database: merge traits into `NostrDatabase` ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/916)
37+
- database: remove `NostrDatabase::has_coordinate_been_deleted` ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/917)
3738

3839
### Changed
3940

crates/nostr-database/src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,6 @@ pub trait NostrDatabase: Debug + Send + Sync {
160160
event_id: &'a EventId,
161161
) -> BoxedFuture<'a, Result<DatabaseEventStatus, DatabaseError>>;
162162

163-
// TODO: rename to `check_coordinate`?
164-
/// Check if [`Coordinate`] has been deleted before a certain [`Timestamp`]
165-
fn has_coordinate_been_deleted<'a>(
166-
&'a self,
167-
coordinate: &'a CoordinateBorrow<'a>,
168-
timestamp: &'a Timestamp,
169-
) -> BoxedFuture<'a, Result<bool, DatabaseError>>;
170-
171163
/// Get [`Event`] by [`EventId`]
172164
fn event_by_id<'a>(
173165
&'a self,

crates/nostr-database/src/memory.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,6 @@ impl NostrDatabase for MemoryDatabase {
157157
})
158158
}
159159

160-
fn has_coordinate_been_deleted<'a>(
161-
&'a self,
162-
coordinate: &'a CoordinateBorrow<'a>,
163-
timestamp: &'a Timestamp,
164-
) -> BoxedFuture<'a, Result<bool, DatabaseError>> {
165-
Box::pin(async move {
166-
match &self.inner {
167-
InnerMemoryDatabase::Tracker(..) => Ok(false),
168-
InnerMemoryDatabase::Full(helper) => Ok(helper
169-
.has_coordinate_been_deleted(coordinate, timestamp)
170-
.await),
171-
}
172-
})
173-
}
174-
175160
fn event_by_id<'a>(
176161
&'a self,
177162
event_id: &'a EventId,

crates/nostr-indexeddb/src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,6 @@ impl NostrDatabase for WebDatabase {
350350
})
351351
}
352352

353-
fn has_coordinate_been_deleted<'a>(
354-
&'a self,
355-
coordinate: &'a CoordinateBorrow<'a>,
356-
timestamp: &'a Timestamp,
357-
) -> BoxedFuture<'a, Result<bool, DatabaseError>> {
358-
Box::pin(async move {
359-
Ok(self
360-
.helper
361-
.has_coordinate_been_deleted(coordinate, timestamp)
362-
.await)
363-
})
364-
}
365-
366353
fn event_by_id<'a>(
367354
&'a self,
368355
event_id: &'a EventId,

crates/nostr-lmdb/src/lib.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,6 @@ impl NostrDatabase for NostrLMDB {
7878
})
7979
}
8080

81-
fn has_coordinate_been_deleted<'a>(
82-
&'a self,
83-
coordinate: &'a CoordinateBorrow<'a>,
84-
timestamp: &'a Timestamp,
85-
) -> BoxedFuture<'a, Result<bool, DatabaseError>> {
86-
Box::pin(async move {
87-
if let Some(t) = self
88-
.db
89-
.when_is_coordinate_deleted(coordinate)
90-
.map_err(DatabaseError::backend)?
91-
{
92-
Ok(&t >= timestamp)
93-
} else {
94-
Ok(false)
95-
}
96-
})
97-
}
98-
9981
fn event_by_id<'a>(
10082
&'a self,
10183
event_id: &'a EventId,

crates/nostr-lmdb/src/store/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,6 @@ impl Store {
9191
Ok(deleted)
9292
}
9393

94-
#[inline]
95-
pub fn when_is_coordinate_deleted<'a>(
96-
&self,
97-
coordinate: &'a CoordinateBorrow<'a>,
98-
) -> Result<Option<Timestamp>, Error> {
99-
let txn = self.db.read_txn()?;
100-
let when = self.db.when_is_coordinate_deleted(&txn, coordinate)?;
101-
txn.commit()?;
102-
Ok(when)
103-
}
104-
10594
pub fn count(&self, filter: Filter) -> Result<usize, Error> {
10695
let txn = self.db.read_txn()?;
10796
let output = self.db.query(&txn, filter)?;

crates/nostr-ndb/src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,6 @@ impl NostrDatabase for NdbDatabase {
104104
})
105105
}
106106

107-
fn has_coordinate_been_deleted<'a>(
108-
&'a self,
109-
_coordinate: &'a CoordinateBorrow<'a>,
110-
_timestamp: &'a Timestamp,
111-
) -> BoxedFuture<'a, Result<bool, DatabaseError>> {
112-
Box::pin(async move { Ok(false) })
113-
}
114-
115107
fn event_by_id<'a>(
116108
&'a self,
117109
event_id: &'a EventId,

crates/nostr-relay-pool/src/relay/inner.rs

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,70 +1092,55 @@ impl InnerRelay {
10921092
}
10931093
}
10941094

1095-
// Check if event status
1096-
let status: DatabaseEventStatus = self.state.database().check_id(&event.id).await?;
1097-
1098-
// Event deleted
1099-
if let DatabaseEventStatus::Deleted = status {
1100-
return Ok(None);
1101-
}
1102-
1103-
// Check if coordinate has been deleted
1104-
// TODO: remove this since it's checked also later?
1105-
if let Some(coordinate) = event.coordinate() {
1106-
if self
1107-
.state
1108-
.database()
1109-
.has_coordinate_been_deleted(&coordinate, &event.created_at)
1110-
.await?
1111-
{
1112-
return Ok(None);
1113-
}
1114-
}
1095+
// Check the event status
1096+
match self.state.database().check_id(&event.id).await? {
1097+
// Already saved, continue with code execution
1098+
DatabaseEventStatus::Saved => {}
1099+
// Deleted, immediately return
1100+
DatabaseEventStatus::Deleted => return Ok(None),
1101+
// Not existent, verify the event and try to save it to the database
1102+
DatabaseEventStatus::NotExistent => {
1103+
// Check if the event was already verified.
1104+
//
1105+
// This is useful if someone continues to send the same invalid event:
1106+
// since invalid events aren't stored in the database,
1107+
// skipping this check would result in the re-verification of the event.
1108+
// This may also be useful to avoid double verification if the event is received at the exact same time by many different Relay instances.
1109+
//
1110+
// This is important since event signature verification is a heavy job!
1111+
if !self.state.verified(&event.id)? {
1112+
event.verify()?;
1113+
}
11151114

1116-
// TODO: check if filter match
1117-
1118-
// Check if the event exists
1119-
if let DatabaseEventStatus::NotExistent = status {
1120-
// Check if the event was already verified.
1121-
//
1122-
// This is useful if someone continues to send the same invalid event:
1123-
// since invalid events aren't stored in the database,
1124-
// skipping this check would result in the re-verification of the event.
1125-
// This may also be useful to avoid double verification if the event is received at the exact same time by many different Relay instances.
1126-
//
1127-
// This is important since event signature verification is a heavy job!
1128-
if !self.state.verified(&event.id)? {
1129-
event.verify()?;
1130-
}
1115+
// Save into the database
1116+
let send_notification: bool = match self.state.database().save_event(&event).await?
1117+
{
1118+
SaveEventStatus::Success => true,
1119+
SaveEventStatus::Rejected(reason) => match reason {
1120+
RejectedReason::Ephemeral => true,
1121+
RejectedReason::Duplicate => true,
1122+
RejectedReason::Deleted => false,
1123+
RejectedReason::Expired => false,
1124+
RejectedReason::Replaced => false,
1125+
RejectedReason::InvalidDelete => false,
1126+
RejectedReason::Other => true,
1127+
},
1128+
};
11311129

1132-
// Save into the database
1133-
let send_notification: bool = match self.state.database().save_event(&event).await? {
1134-
SaveEventStatus::Success => true,
1135-
SaveEventStatus::Rejected(reason) => match reason {
1136-
RejectedReason::Ephemeral => true,
1137-
RejectedReason::Duplicate => true,
1138-
RejectedReason::Deleted => false,
1139-
RejectedReason::Expired => false,
1140-
RejectedReason::Replaced => false,
1141-
RejectedReason::InvalidDelete => false,
1142-
RejectedReason::Other => true,
1143-
},
1144-
};
1130+
// If the notification should NOT be sent, immediately return.
1131+
if !send_notification {
1132+
return Ok(None);
1133+
}
11451134

1146-
// If the notification should NOT be sent, immediately return.
1147-
if !send_notification {
1148-
return Ok(None);
1135+
// Send notification
1136+
self.send_notification(
1137+
RelayNotification::Event {
1138+
subscription_id: subscription_id.clone(),
1139+
event: Box::new(event.clone()),
1140+
},
1141+
true,
1142+
);
11491143
}
1150-
1151-
// Send notification
1152-
self.send_notification(
1153-
RelayNotification::Event {
1154-
subscription_id: subscription_id.clone(),
1155-
event: Box::new(event.clone()),
1156-
},
1157-
true,
1158-
);
11591144
}
11601145

11611146
Ok(Some(RelayMessage::Event {

0 commit comments

Comments
 (0)