Skip to content

Commit

Permalink
fbs: add FlatBufferUtils trait
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 24, 2023
1 parent 8d002e0 commit 9594182
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/nostr-sdk-fbs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ license.workspace = true
[dependencies]
flatbuffers = "23.5"
nostr = { version = "0.24", path = "../nostr", default-features = false, features = ["std"] }
thiserror = { workspace = true }
72 changes: 54 additions & 18 deletions crates/nostr-sdk-fbs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,68 @@
use event_generated::event_fbs::{Fixed32Bytes, Fixed64Bytes};
pub use flatbuffers::FlatBufferBuilder;
use nostr::Event;
use flatbuffers::InvalidFlatbuffer;
use nostr::secp256k1::schnorr::Signature;
use nostr::secp256k1::{self, XOnlyPublicKey};
use nostr::{Event, EventId, Kind, Timestamp};
use thiserror::Error;

#[allow(unused_imports, dead_code)]
mod event_generated;

pub use self::event_generated::event_fbs;

pub fn serialize_event<'a>(fbb: &'a mut FlatBufferBuilder, event: &Event) -> &'a [u8] {
fbb.reset();
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
InvalidFlatbuffer(#[from] InvalidFlatbuffer),
#[error(transparent)]
EventId(#[from] nostr::event::id::Error),
#[error(transparent)]
Secp256k1(#[from] secp256k1::Error),
#[error("not found")]
NotFound,
}

pub trait FlatBufferUtils: Sized {
fn encode<'a>(&self, fbb: &'a mut FlatBufferBuilder) -> &'a [u8];
fn decode(buf: &[u8]) -> Result<Self, Error>;
}

impl FlatBufferUtils for Event {
fn encode<'a>(&self, fbb: &'a mut FlatBufferBuilder) -> &'a [u8] {
fbb.reset();

let id = Fixed32Bytes::new(&self.id.to_bytes());
let pubkey = Fixed32Bytes::new(&self.pubkey.serialize());
let sig = Fixed64Bytes::new(self.sig.as_ref());
let args = event_fbs::EventArgs {
id: Some(&id),
pubkey: Some(&pubkey),
created_at: self.created_at.as_u64(),
kind: self.kind.as_u64(),
tags: None, // TODO
content: None, // TODO
sig: Some(&sig),
};

let id = Fixed32Bytes::new(&event.id.to_bytes());
let pubkey = Fixed32Bytes::new(&event.pubkey.serialize());
let sig = Fixed64Bytes::new(event.sig.as_ref());
let args = event_fbs::EventArgs {
id: Some(&id),
pubkey: Some(&pubkey),
created_at: event.created_at.as_u64(),
kind: event.kind.as_u64(),
tags: None, // TODO
content: None, // TODO
sig: Some(&sig),
};
let offset = event_fbs::Event::create(fbb, &args);

let offset = event_fbs::Event::create(fbb, &args);
event_fbs::finish_event_buffer(fbb, offset);

event_fbs::finish_event_buffer(fbb, offset);
fbb.finished_data()
}

fbb.finished_data()
fn decode(buf: &[u8]) -> Result<Self, Error> {
let ev = event_fbs::root_as_event(buf)?;
Ok(Self {
id: EventId::from_slice(&ev.id().ok_or(Error::NotFound)?.0)?,
pubkey: XOnlyPublicKey::from_slice(&ev.pubkey().ok_or(Error::NotFound)?.0)?,
created_at: Timestamp::from(ev.created_at()),
kind: Kind::from(ev.kind()),
tags: Vec::new(), // TODO
content: String::new(), // TODO
sig: Signature::from_slice(&ev.sig().ok_or(Error::NotFound)?.0)?,
})
}
}
4 changes: 2 additions & 2 deletions crates/nostr-sdk-redb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::Path;

use nostr_sdk_db::nostr::{Event, EventId, Filter, Timestamp, Url};
use nostr_sdk_db::{async_trait, Backend, DatabaseError, DatabaseOptions, NostrDatabase};
use nostr_sdk_fbs::FlatBufferBuilder;
use nostr_sdk_fbs::{FlatBufferBuilder, FlatBufferUtils};
use redb::{Database, TableDefinition};
use thiserror::Error;

Expand Down Expand Up @@ -80,7 +80,7 @@ impl NostrDatabase for RedbDatabase {

// Serialize event
let mut fbb = FlatBufferBuilder::new();
let ser = nostr_sdk_fbs::serialize_event(&mut fbb, event);
let ser = event.encode(&mut fbb);

// Insert
table
Expand Down

0 comments on commit 9594182

Please sign in to comment.