Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nip10: fix one edgecase, add owned variants #9

Merged
merged 6 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "nostrdb"
authors = ["William Casarin <[email protected]>"]
description = "An unfairly fast embedded nostr database backed by lmdb"
readme = "README.md"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
build = "build.rs"
license = "GPL-3.0-or-later"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ pub use result::Result;
pub use subscription::Subscription;
pub use tags::{Tag, TagIter, Tags, TagsIter};
pub use transaction::Transaction;
pub use util::nip10::{Marker, NoteIdRef, NoteReply};
pub use util::nip10::{Marker, NoteIdRef, NoteIdRefBuf, NoteReply, NoteReplyBuf};

mod test_util;
240 changes: 223 additions & 17 deletions src/util/nip10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,115 @@ pub enum Marker {
Mention,
}

/// Parsed `e` tags
#[derive(Clone, Copy, Debug)]
pub struct NoteIdRef<'a> {
pub index: u16,
pub id: &'a [u8; 32],
pub relay: Option<&'a str>,
pub marker: Option<Marker>,
}

impl<'a> NoteIdRef<'a> {
pub fn to_owned(&self) -> NoteIdRefBuf {
NoteIdRefBuf {
index: self.index,
marker: self.marker,
}
}
}

pub struct NoteIdRefBuf {
pub index: u16,
pub marker: Option<Marker>,
}

fn tag_to_note_id_ref(tag: Tag<'_>, marker: Option<Marker>, index: i32) -> NoteIdRef<'_> {
let id = tag
.get_unchecked(1)
.variant()
.id()
.expect("expected id at index, do you have the correct note?");
let relay = tag.get(2).and_then(|t| t.variant().str());
NoteIdRef {
index: index as u16,
id,
relay,
marker,
}
}

impl NoteReplyBuf {
// TODO(jb55): optimize this function. It is not the nicest code.
// We could simplify the index lookup by offsets into the Note's
// string table
pub fn borrow<'a>(&self, tags: Tags<'a>) -> NoteReply<'a> {
let mut root: Option<NoteIdRef<'a>> = None;
let mut reply: Option<NoteIdRef<'a>> = None;
let mut mention: Option<NoteIdRef<'a>> = None;

let mut index: i32 = -1;
for tag in tags {
index += 1;
if tag.count() < 2 && tag.get_unchecked(0).variant().str() != Some("e") {
continue;
}

if self
.root
.as_ref()
.map_or(false, |x| x.index == index as u16)
{
root = Some(tag_to_note_id_ref(
tag,
self.root.as_ref().unwrap().marker,
index,
))
} else if self
.reply
.as_ref()
.map_or(false, |x| x.index == index as u16)
{
reply = Some(tag_to_note_id_ref(
tag,
self.reply.as_ref().unwrap().marker,
index,
))
} else if self
.mention
.as_ref()
.map_or(false, |x| x.index == index as u16)
{
mention = Some(tag_to_note_id_ref(
tag,
self.mention.as_ref().unwrap().marker,
index,
))
}
}

NoteReply {
root,
reply,
mention,
}
}
}

#[derive(Clone, Copy, Debug)]
pub struct NoteReply<'a> {
root: Option<NoteIdRef<'a>>,
reply: Option<NoteIdRef<'a>>,
mention: Option<NoteIdRef<'a>>,
}

/// Owned version of NoteReply, stores tag indices
pub struct NoteReplyBuf {
root: Option<NoteIdRefBuf>,
reply: Option<NoteIdRefBuf>,
mention: Option<NoteIdRefBuf>,
}

impl<'a> NoteReply<'a> {
pub fn reply_to_root(self) -> Option<NoteIdRef<'a>> {
if self.is_reply_to_root() {
Expand All @@ -30,6 +125,14 @@ impl<'a> NoteReply<'a> {
}
}

pub fn to_owned(&self) -> NoteReplyBuf {
NoteReplyBuf {
root: self.root.map(|x| x.to_owned()),
reply: self.reply.map(|x| x.to_owned()),
mention: self.mention.map(|x| x.to_owned()),
}
}

pub fn new(tags: Tags<'a>) -> NoteReply<'a> {
tags_to_note_reply(tags)
}
Expand All @@ -38,6 +141,10 @@ impl<'a> NoteReply<'a> {
self.root.is_some() && self.reply.is_none()
}

pub fn root(self) -> Option<NoteIdRef<'a>> {
self.root
}

pub fn is_reply(&self) -> bool {
self.reply().is_some()
}
Expand Down Expand Up @@ -76,36 +183,33 @@ fn tags_to_note_reply<'a>(tags: Tags<'a>) -> NoteReply<'a> {
let mut reply: Option<NoteIdRef<'a>> = None;
let mut mention: Option<NoteIdRef<'a>> = None;
let mut first: bool = true;
let mut index: i32 = -1;
let mut any_marker: bool = false;

for tag in tags {
index += 1;

if root.is_some() && reply.is_some() && mention.is_some() {
break;
}

let note_ref = if let Ok(note_ref) = tag_to_noteid_ref(tag) {
let note_ref = if let Ok(note_ref) = tag_to_noteid_ref(tag, index as u16) {
note_ref
} else {
continue;
};

if let Some(marker) = note_ref.marker {
any_marker = true;
match marker {
Marker::Root => root = Some(note_ref),
Marker::Reply => {
if reply.is_none() {
reply = Some(note_ref)
}
}
Marker::Mention => {
if mention.is_none() {
mention = Some(note_ref)
}
}
Marker::Reply => reply = Some(note_ref),
Marker::Mention => mention = Some(note_ref),
}
} else if first {
} else if !any_marker && first {
root = Some(note_ref);
first = false;
} else if reply.is_none() {
} else if !any_marker && reply.is_none() {
reply = Some(note_ref)
}
}
Expand All @@ -117,7 +221,7 @@ fn tags_to_note_reply<'a>(tags: Tags<'a>) -> NoteReply<'a> {
}
}

pub fn tag_to_noteid_ref(tag: Tag<'_>) -> Result<NoteIdRef<'_>, Error> {
pub fn tag_to_noteid_ref(tag: Tag<'_>, index: u16) -> Result<NoteIdRef<'_>, Error> {
if tag.count() < 2 {
return Err(Error::DecodeError);
}
Expand All @@ -132,13 +236,21 @@ pub fn tag_to_noteid_ref(tag: Tag<'_>) -> Result<NoteIdRef<'_>, Error> {
.id()
.ok_or(Error::DecodeError)?;

let relay = tag.get(2).and_then(|t| t.variant().str());
let relay = tag
.get(2)
.and_then(|t| t.variant().str())
.filter(|x| !x.is_empty());
let marker = tag
.get(3)
.and_then(|t| t.variant().str())
.and_then(Marker::new);

Ok(NoteIdRef { id, relay, marker })
Ok(NoteIdRef {
index,
id,
relay,
marker,
})
}

#[cfg(test)]
Expand Down Expand Up @@ -320,6 +432,93 @@ mod test {
}
}

#[tokio::test]
async fn nip10_marker_mixed() {
let db = "target/testdbs/nip10_marker_mixed";
test_util::cleanup_db(&db);

{
let ndb = Ndb::new(db, &Config::new()).expect("ndb");
let filter = Filter::new().kinds(vec![1]).build();
let root_id: [u8; 32] =
hex::decode("27e71cf53299dafb5dc7bcc0a078357418a4375cb1097bf5184662493f79a627")
.unwrap()
.try_into()
.unwrap();
let reply_id: [u8; 32] =
hex::decode("1a616998552cf76e9786f76ac68f6104cdae46377330735c68bfe0b9426d2fa8")
.unwrap()
.try_into()
.unwrap();
let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);

ndb.process_event(r#"
[
"EVENT",
"nostril-query",
{
"content": "Go to pleblab plz",
"created_at": 1714157088,
"id": "19ae8cd276185f6f48fd7e25736c260ea0ac25d9b591ec3194631e3196e19622",
"kind": 1,
"pubkey": "ae1008d23930b776c18092f6eab41e4b09fcf3f03f3641b1b4e6ee3aa166d760",
"sig": "fdafc7192a0f3b5fef5ae794ef61eb2b3c7cc70bace53f3aa6d4263347581d36add7e9468a4e329d9c986e3a5c46e4689a6b79f60c5cf7778a403316ac5b2629",
"tags": [
[
"e",
"27e71cf53299dafb5dc7bcc0a078357418a4375cb1097bf5184662493f79a627",
"",
"root"
],
[
"e",
"f99046bd87be7508d55e139de48517c06ef90830d77a5d3213df858d77bb2f8f"
],
[
"e",
"1a616998552cf76e9786f76ac68f6104cdae46377330735c68bfe0b9426d2fa8",
"",
"reply"
],
[
"p",
"3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"
],
[
"p",
"8ea485266b2285463b13bf835907161c22bb3da1e652b443db14f9cee6720a43"
],
[
"p",
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"
]
]
}
]
"#).expect("process ok");

let res = waiter.await.expect("await ok");
assert_eq!(res, vec![NoteKey::new(1)]);
let txn = Transaction::new(&ndb).unwrap();
let res = ndb.query(&txn, vec![filter], 1).expect("note");
let note = &res[0].note;
let note_reply = NoteReply::new(note.tags());

assert_eq!(note_reply.reply_to_root().is_none(), true);
assert_eq!(*note_reply.reply().unwrap().id, reply_id);
assert_eq!(*note_reply.root().unwrap().id, root_id);
assert_eq!(note_reply.mention().is_none(), true);

// test the to_owned version
let back_again = note_reply.to_owned().borrow(note.tags());
assert_eq!(back_again.reply_to_root().is_none(), true);
assert_eq!(*back_again.reply().unwrap().id, reply_id);
assert_eq!(*back_again.root().unwrap().id, root_id);
assert_eq!(back_again.mention().is_none(), true);
}
}

#[tokio::test]
async fn nip10_deprecated_reply_to_root() {
let db = "target/testdbs/nip10_deprecated_reply_to_root";
Expand Down Expand Up @@ -360,11 +559,18 @@ mod test {
assert_eq!(res, vec![NoteKey::new(1)]);
let txn = Transaction::new(&ndb).unwrap();
let res = ndb.query(&txn, vec![filter], 1).expect("note");
let note_reply = NoteReply::new(res[0].note.tags());
let note = &res[0].note;
let note_reply = NoteReply::new(note.tags());

assert_eq!(*note_reply.reply_to_root().unwrap().id, root_id);
assert_eq!(*note_reply.reply().unwrap().id, root_id);
assert_eq!(note_reply.mention().is_none(), true);

// test the to_owned version
let back_again = note_reply.to_owned().borrow(note.tags());
assert_eq!(*back_again.reply_to_root().unwrap().id, root_id);
assert_eq!(*back_again.reply().unwrap().id, root_id);
assert_eq!(back_again.mention().is_none(), true);
}
}
}
Loading