From ff839ad7735e993a4148001818bdf6c0a0fadbf7 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Sat, 3 Feb 2024 03:43:39 +1300 Subject: [PATCH] storage: Extract out switch_to_rumor() --- gossip-lib/src/storage/event_tag_index1.rs | 27 +++------ gossip-lib/src/storage/mod.rs | 70 +++++++++++----------- 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/gossip-lib/src/storage/event_tag_index1.rs b/gossip-lib/src/storage/event_tag_index1.rs index 512295c16..c8ac7413d 100644 --- a/gossip-lib/src/storage/event_tag_index1.rs +++ b/gossip-lib/src/storage/event_tag_index1.rs @@ -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. @@ -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 diff --git a/gossip-lib/src/storage/mod.rs b/gossip-lib/src/storage/mod.rs index bf4b949da..5737fab79 100644 --- a/gossip-lib/src/storage/mod.rs +++ b/gossip-lib/src/storage/mod.rs @@ -1652,6 +1652,33 @@ impl Storage { Ok(events) } + fn switch_to_rumor<'a>( + &'a self, + event: &Event, + txn: &mut RwTxn<'a>, + ) -> Result, 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, @@ -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(); @@ -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(); @@ -1752,6 +1753,7 @@ impl Storage { Ok(()) } + // Switch to rumor before calling this. fn write_event_tag_index<'a>( &'a self, event: &Event,