commit 35b67dc08d4b61bf98922cc94cffcb3037c77710
parent 1f5f1e28a4fff3fa8dd69593b17149b5ef4c7911
Author: William Casarin <jb55@jb55.com>
Date: Fri, 25 Aug 2023 18:13:42 -0700
nostrdb: initial Ndb class
Diffstat:
4 files changed, 78 insertions(+), 1 deletion(-)
diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj
@@ -103,6 +103,7 @@
4C30AC7629A5770900E2BD5A /* NotificationItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C30AC7529A5770900E2BD5A /* NotificationItemView.swift */; };
4C30AC7829A577AB00E2BD5A /* EventCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C30AC7729A577AB00E2BD5A /* EventCache.swift */; };
4C30AC8029A6A53F00E2BD5A /* ProfilePicturesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C30AC7F29A6A53F00E2BD5A /* ProfilePicturesView.swift */; };
+ 4C32B9332A99845B00DC3548 /* Ndb.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C478E242A9932C100489948 /* Ndb.swift */; };
4C363A8428233689006E126D /* Parser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C363A8328233689006E126D /* Parser.swift */; };
4C363A8828236948006E126D /* BlocksView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C363A8728236948006E126D /* BlocksView.swift */; };
4C363A8C28236B92006E126D /* PubkeyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C363A8B28236B92006E126D /* PubkeyView.swift */; };
@@ -1732,6 +1733,7 @@
4C90548A2A6AEDEE00811EEC /* NdbNote.swift */,
4C5D5C9C2A6B2CB40024563C /* AsciiCharacter.swift */,
4CDD1ADF2A6B305F001CD4DF /* NdbTagElem.swift */,
+ 4C478E242A9932C100489948 /* Ndb.swift */,
4CDD1AE12A6B3074001CD4DF /* NdbTagsIterator.swift */,
4CE9FBB82A6B3B26007E485C /* nostrdb.c */,
4C4793032A993DB900489948 /* midl.c */,
@@ -1749,7 +1751,6 @@
4C78EFD72A707C4D007E8197 /* secp256k1_schnorrsig.h */,
4C78EFDA2A707C67007E8197 /* secp256k1_extrakeys.h */,
4C78EFD92A707C4D007E8197 /* secp256k1.h */,
- 4C478E242A9932C100489948 /* Ndb.swift */,
);
path = nostrdb;
sourceTree = "<group>";
@@ -2403,6 +2404,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ 4C32B9332A99845B00DC3548 /* Ndb.swift in Sources */,
4C4793082A993E8900489948 /* refmap.c in Sources */,
4C4793072A993E6200489948 /* emitter.c in Sources */,
4C4793062A993E5300489948 /* json_parser.c in Sources */,
diff --git a/damus/TestData.swift b/damus/TestData.swift
@@ -58,6 +58,11 @@ func test_damus_state() -> DamusState {
return damus
}
+let test_wire_events = """
+["EVENT","s",{"id":"d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349","pubkey":"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245","created_at":1650049978,"kind":0,"tags":[],"content":"{\\"name\\\":\\"jb55\\",\\"picture\\":\\\"http://cdn.jb55.com/img/red-me.jpg\\",\\"about\\":\\"bitcoin, lightning and nostr dev\\",\\"nip05\\":\\"jb55.com\\"}","sig":"1315045da793c4825de1517149172bf35a6da39d91b7787afb3263721e07bc816cb898996ed8d69af05d6efcd1c926a089bd66cad870bcc361405c11ba302c51"}]
+["EVENT","s",{"id":"b2e03951843b191b5d9d1969f48db0156b83cc7dbd841f543f109362e24c4a9c","pubkey":"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245","created_at":1650050002,"kind":1,"tags":[],"content":"hello, this is my new key","sig":"4342eff1d78a82b42522cd26ec66a5293eca997f81d4b80efd02230d3d27317fb63d42656e8f32383562f075a2b6d999b60dcf70e2df18cf5e8b3801faeb0bd6"}]
+"""
+
let test_failing_nostr_report = """
{
"id": "99198ecb6a34372b7e39b88280bab3394654a00f5f8504466fac2d6acb569663",
diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift
@@ -0,0 +1,47 @@
+//
+// Ndb.swift
+// damus
+//
+// Created by William Casarin on 2023-08-25.
+//
+
+import Foundation
+
+class Ndb {
+ let ndb: ndb_t
+
+ init?() {
+ var ndb_p: OpaquePointer? = nil
+
+ let dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.absoluteString.replacingOccurrences(of: "file://", with: "")
+
+ let ok = dir!.withCString { testdir in
+ return ndb_init(&ndb_p, testdir, 1024 * 1024 * 700, 4) != 0
+ }
+
+ if !ok {
+ return nil
+ }
+
+ self.ndb = ndb_t(ndb: ndb_p)
+ }
+
+ func lookup_note(_ id: NoteId) -> NdbNote? {
+ id.id.withUnsafeBytes { bs in
+ guard let note_p = ndb_get_note_by_id(ndb.ndb, bs) else {
+ return nil
+ }
+ return NdbNote(note: note_p, owned_size: nil)
+ }
+ }
+
+ func process_events(_ str: String) -> Bool {
+ return str.withCString { cstr in
+ return ndb_process_events(ndb.ndb, cstr, str.utf8.count) != 0
+ }
+ }
+
+ deinit {
+ ndb_destroy(ndb.ndb)
+ }
+}
diff --git a/nostrdb/Test/NdbTests.swift b/nostrdb/Test/NdbTests.swift
@@ -31,6 +31,29 @@ final class NdbTests: XCTestCase {
}
+ func test_ndb_init() {
+
+ do {
+ let ndb = Ndb()!
+ let ok = ndb.process_events(test_wire_events)
+ XCTAssertTrue(ok)
+ }
+
+ do {
+ let ndb = Ndb()!
+ let id1 = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")!
+ let note1 = ndb.lookup_note(id1)
+ XCTAssertNotNil(note1)
+ let id = NoteId(hex: "b2e03951843b191b5d9d1969f48db0156b83cc7dbd841f543f109362e24c4a9c")!
+ let note = ndb.lookup_note(id)
+ XCTAssertNotNil(note)
+ guard let note else { return }
+ XCTAssertEqual(note.pubkey, Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!)
+ }
+
+
+ }
+
func test_ndb_note() throws {
let note = NdbNote.owned_from_json(json: test_contact_list_json)
XCTAssertNotNil(note)