nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

commit 9003870b4fae2da0355ff48237abb780d4abac2d
parent 912bffb4f5a0b8eeabd84ed41259ce470ddcb071
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 28 Aug 2023 07:22:04 -0700

fix lmdb alignment issues

Diffstat:
MMakefile | 2+-
Mnostrdb.c | 28++++++++++++++++++++--------
Mnostrdb.h | 2+-
Mschemas/profile.fbs | 2--
Mtest.c | 5++++-
5 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ CFLAGS = -Wall -Wno-unused-function -Werror -O0 -g -Ideps/secp256k1/include -Ideps/lmdb -Ideps/flatcc/include HEADERS = sha256.h nostrdb.h cursor.h hex.h jsmn.h config.h sha256.h random.h memchr.h $(C_BINDINGS) -FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c +FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/verifier.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c SRCS = nostrdb.c sha256.c bech32.c $(FLATCC_SRCS) LDS = $(SRCS) $(ARS) DEPS = $(SRCS) $(HEADERS) $(ARS) diff --git a/nostrdb.c b/nostrdb.c @@ -16,6 +16,7 @@ #include "bindings/c/profile_json_parser.h" #include "bindings/c/profile_builder.h" +#include "bindings/c/profile_verifier.h" #include "secp256k1.h" #include "secp256k1_ecdh.h" #include "secp256k1_schnorrsig.h" @@ -385,10 +386,18 @@ static int ndbprofile_parse_json(flatcc_builder_t *B, flatcc_json_parser_t parser, *ctx = &parser; flatcc_json_parser_init(ctx, B, buf, buf + bufsiz, flags); - NdbProfile_parse_json_table(ctx, buf, buf + bufsiz, profile); + if (flatcc_builder_start_buffer(B, 0, 0, 0)) + return 0; + NdbProfile_parse_json_table(ctx, buf, buf + bufsiz, profile); if (ctx->error) return 0; + + if (!flatcc_builder_end_buffer(B, *profile)) + return 0; + + ctx->end_loc = buf; + return 1; } @@ -439,11 +448,12 @@ static int ndb_process_profile_note(struct ndb_note *note, uint64_t received_at = time(NULL); const char *lnurl = "fixme"; + NdbProfileRecord_profile_add(builder, profile_table); + NdbProfileRecord_received_at_add(builder, received_at); + flatcc_builder_ref_t lnurl_off; lnurl_off = flatcc_builder_create_string_str(builder, lnurl); - NdbProfileRecord_profile_add(builder, profile_table); - NdbProfileRecord_received_at_add(builder, received_at); NdbProfileRecord_lnurl_add(builder, lnurl_off); //*profile = flatcc_builder_finalize_aligned_buffer(builder, profile_len); @@ -585,12 +595,14 @@ static int ndb_write_profile(struct ndb_lmdb *lmdb, MDB_txn *txn, NdbProfileRecord_end_as_root(profile->record.builder); flatbuf = profile->record.flatbuf = - flatcc_builder_finalize_buffer(profile->record.builder, &flatbuf_len); + flatcc_builder_finalize_aligned_buffer(profile->record.builder, &flatbuf_len); assert(((uint64_t)flatbuf % 8) == 0); // TODO: this may not be safe!? - flatbuf_len = (flatbuf_len + 3) & ~3; + flatbuf_len = (flatbuf_len + 7) & ~7; + + //assert(NdbProfileRecord_verify_as_root(flatbuf, flatbuf_len) == 0); // get dbs profile_db = lmdb->dbs[NDB_DB_PROFILE]; @@ -1440,9 +1452,9 @@ int ndb_builder_finalize(struct ndb_builder *builder, struct ndb_note **note, return 0; } - // make sure we're aligned - total_size = (total_size + 3) & ~3; - assert((total_size % 4) == 0); + // make sure we're aligned as a whole + total_size = (total_size + 7) & ~7; + assert((total_size % 8) == 0); return total_size; } diff --git a/nostrdb.h b/nostrdb.h @@ -7,7 +7,7 @@ #define NDB_PACKED_STR 0x1 #define NDB_PACKED_ID 0x2 -//#define DEBUG 1 +#define DEBUG 1 #ifdef DEBUG #define ndb_debug(...) printf(__VA_ARGS__) diff --git a/schemas/profile.fbs b/schemas/profile.fbs @@ -18,8 +18,6 @@ table NdbProfileRecord { profile:NdbProfile; received_at:ulong; note_key:ulong; - - // artifacts lnurl:string; } diff --git a/test.c b/test.c @@ -5,6 +5,7 @@ #include "protected_queue.h" #include "memchr.h" #include "bindings/c/profile_reader.h" +#include "bindings/c/profile_verifier.h" #include <stdio.h> #include <assert.h> @@ -280,7 +281,9 @@ static void test_fetch_last_noteid() void *root = ndb_get_profile_by_pubkey(ndb, pk, &len); assert(root); - assert((((uint64_t)root) % 4) == 0); + int res = NdbProfileRecord_verify_as_root(root, len); + printf("NdbProfileRecord verify result %d\n", res); + assert(res == 0); NdbProfileRecord_table_t profile_record = NdbProfileRecord_as_root(root); NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record);