Skip to content

Commit

Permalink
storage: Extract out switch_to_rumor()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedilger committed Feb 2, 2024
1 parent 258cdcc commit ff839ad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 54 deletions.
27 changes: 7 additions & 20 deletions gossip-lib/src/storage/event_tag_index1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::error::{Error, ErrorKind};
use crate::globals::GLOBALS;
use crate::error::Error;
use crate::storage::{RawDatabase, Storage};
use heed::{types::UnalignedSlice, DatabaseFlags, RwTxn};
use nostr_types::{Event, EventKind, PublicKeyHex};
use nostr_types::{Event, PublicKeyHex};
use std::sync::Mutex;

// NOTE: "innerp" is a fake tag. We store events that reference a person internally under it.
Expand Down Expand Up @@ -54,23 +53,11 @@ impl Storage {
let f = |txn: &mut RwTxn<'a>| -> Result<(), Error> {
let mut event = event;

let mut rumor_event: Event;
if event.kind == EventKind::GiftWrap {
match GLOBALS.identity.unwrap_giftwrap(event) {
Ok(rumor) => {
rumor_event = rumor.into_event_with_bad_signature();
rumor_event.id = event.id; // lie, so it indexes it under the giftwrap
event = &rumor_event;
}
Err(e) => {
if matches!(e.kind, ErrorKind::NoPrivateKey) {
// Store as unindexed for later indexing
let bytes = vec![];
self.db_unindexed_giftwraps()?
.put(txn, event.id.as_slice(), &bytes)?;
}
}
}
// If giftwrap, index the inner rumor instead
let rumor_event: Event;
if let Some(rumor) = self.switch_to_rumor(event, txn)? {
rumor_event = rumor;
event = &rumor_event;
}

// our user's public key
Expand Down
70 changes: 36 additions & 34 deletions gossip-lib/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,33 @@ impl Storage {
Ok(events)
}

fn switch_to_rumor<'a>(
&'a self,
event: &Event,
txn: &mut RwTxn<'a>,
) -> Result<Option<Event>, Error> {
if event.kind == EventKind::GiftWrap {
match GLOBALS.identity.unwrap_giftwrap(event) {
Ok(rumor) => {
let mut rumor_event = rumor.into_event_with_bad_signature();
rumor_event.id = event.id; // lie, so it indexes it under the giftwrap
Ok(Some(rumor_event))
}
Err(e) => {
if matches!(e.kind, ErrorKind::NoPrivateKey) {
// Store as unindexed for later indexing
let bytes = vec![];
self.db_unindexed_giftwraps()?
.put(txn, event.id.as_slice(), &bytes)?;
}
Err(e)
}
}
} else {
Ok(None)
}
}

// We don't call this externally. Whenever we write an event, we do this.
fn write_event_ek_pk_index<'a>(
&'a self,
Expand All @@ -1662,23 +1689,10 @@ impl Storage {
let mut event = event;

// If giftwrap, index the inner rumor instead
let mut rumor_event: Event;
if event.kind == EventKind::GiftWrap {
match GLOBALS.identity.unwrap_giftwrap(event) {
Ok(rumor) => {
rumor_event = rumor.into_event_with_bad_signature();
rumor_event.id = event.id; // lie, so it indexes it under the giftwrap
event = &rumor_event;
}
Err(e) => {
if matches!(e.kind, ErrorKind::NoPrivateKey) {
// Store as unindexed for later indexing
let bytes = vec![];
self.db_unindexed_giftwraps()?
.put(txn, event.id.as_slice(), &bytes)?;
}
}
}
let rumor_event: Event;
if let Some(rumor) = self.switch_to_rumor(event, txn)? {
rumor_event = rumor;
event = &rumor_event;
}

let ek: u32 = event.kind.into();
Expand Down Expand Up @@ -1712,23 +1726,10 @@ impl Storage {
let mut event = event;

// If giftwrap, index the inner rumor instead
let mut rumor_event: Event;
if event.kind == EventKind::GiftWrap {
match GLOBALS.identity.unwrap_giftwrap(event) {
Ok(rumor) => {
rumor_event = rumor.into_event_with_bad_signature();
rumor_event.id = event.id; // lie, so it indexes it under the giftwrap
event = &rumor_event;
}
Err(e) => {
if matches!(e.kind, ErrorKind::NoPrivateKey) {
// Store as unindexed for later indexing
let bytes = vec![];
self.db_unindexed_giftwraps()?
.put(txn, event.id.as_slice(), &bytes)?;
}
}
}
let rumor_event: Event;
if let Some(rumor) = self.switch_to_rumor(event, txn)? {
rumor_event = rumor;
event = &rumor_event;
}

let ek: u32 = event.kind.into();
Expand All @@ -1752,6 +1753,7 @@ impl Storage {
Ok(())
}

// Switch to rumor before calling this.
fn write_event_tag_index<'a>(
&'a self,
event: &Event,
Expand Down

0 comments on commit ff839ad

Please sign in to comment.