Skip to content

Commit

Permalink
wip local relay model
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Apr 25, 2024
1 parent d0b449c commit 10bc4f4
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 27 deletions.
7 changes: 6 additions & 1 deletion damus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@
4C75EFB728049D990006080F /* RelayPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB628049D990006080F /* RelayPool.swift */; };
4C75EFB92804A2740006080F /* EventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB82804A2740006080F /* EventView.swift */; };
4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */; };
4C79A6DC2BC07FC6007B2D87 /* QueryResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */; };
4C79A6DD2BC07FC6007B2D87 /* QueryResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */; };
4C7D09592A05BEAD00943473 /* KeyboardVisible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D09582A05BEAD00943473 /* KeyboardVisible.swift */; };
4C7D095F2A098C5D00943473 /* ConnectWalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D095C2A098C5D00943473 /* ConnectWalletView.swift */; };
4C7D09602A098C5D00943473 /* WalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D095D2A098C5D00943473 /* WalletView.swift */; };
Expand Down Expand Up @@ -1072,6 +1074,7 @@
4C78EFD82A707C4D007E8197 /* secp256k1_ecdh.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1_ecdh.h; sourceTree = "<group>"; };
4C78EFD92A707C4D007E8197 /* secp256k1.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1.h; sourceTree = "<group>"; };
4C78EFDA2A707C67007E8197 /* secp256k1_extrakeys.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1_extrakeys.h; sourceTree = "<group>"; };
4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueryResult.swift; sourceTree = "<group>"; };
4C7D09582A05BEAD00943473 /* KeyboardVisible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardVisible.swift; sourceTree = "<group>"; };
4C7D095C2A098C5D00943473 /* ConnectWalletView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectWalletView.swift; sourceTree = "<group>"; };
4C7D095D2A098C5D00943473 /* WalletView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2130,6 +2133,7 @@
D798D2272B085CDA00234419 /* NdbNote+.swift */,
4CF480562B633F2600F2B2C0 /* NdbBlocksIterator.swift */,
4CF480582B633F3800F2B2C0 /* NdbBlock.swift */,
4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */,
);
path = nostrdb;
sourceTree = "<group>";
Expand Down Expand Up @@ -3360,6 +3364,7 @@
4C363A8828236948006E126D /* BlocksView.swift in Sources */,
4C06670628FCB08600038D2A /* ImageCarousel.swift in Sources */,
3A23838E2A297DD200E5AA2E /* ZapButtonModel.swift in Sources */,
4C79A6DC2BC07FC6007B2D87 /* QueryResult.swift in Sources */,
F71694F82A6983AF001F4053 /* GrayGradient.swift in Sources */,
4C1D4FB12A7958E60024F453 /* VersionInfo.swift in Sources */,
D7FF94002AC7AC5300FD969D /* RelayURL.swift in Sources */,
Expand Down Expand Up @@ -3397,7 +3402,6 @@
4C19AE512A5CEF7C00C90DB7 /* NostrScript.swift in Sources */,
4C32B95E2A9AD44700DC3548 /* FlatBufferObject.swift in Sources */,
D783A63F2AD4E53D00658DDA /* SuggestedHashtagsView.swift in Sources */,
4CB88393296F798300DC99E7 /* ReactionsModel.swift in Sources */,
5C42E78C29DB76D90086AAC1 /* EmptyUserSearchView.swift in Sources */,
4CB88396296F7F8B00DC99E7 /* ReactionView.swift in Sources */,
4CF480552B631C4F00F2B2C0 /* wasm.c in Sources */,
Expand Down Expand Up @@ -3732,6 +3736,7 @@
D798D2222B08598A00234419 /* ReferencedId.swift in Sources */,
D7CE1B492B0BE729002EDAD4 /* DisplayName.swift in Sources */,
D7CE1B192B0BE132002EDAD4 /* builder.c in Sources */,
4C79A6DD2BC07FC6007B2D87 /* QueryResult.swift in Sources */,
D7EDED1F2B11797D0018B19C /* LongformEvent.swift in Sources */,
D7CCFC122B05886D00323D86 /* IdType.swift in Sources */,
D7CE1B312B0BE69D002EDAD4 /* Ndb.swift in Sources */,
Expand Down
13 changes: 8 additions & 5 deletions damus/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -661,22 +661,25 @@ struct ContentView: View {
self.selected_timeline = timeline
}

func on_local_sub(subid: UInt64) {
}

func connect() {
// nostrdb
var mndb = Ndb()
if mndb == nil {
var ndb: Ndb? = Ndb()
if ndb?.open() == nil {
// try recovery
print("DB ISSUE! RECOVERING")
mndb = Ndb.safemode()
ndb = Ndb.safemode()

// out of space or something?? maybe we need a in-memory fallback
if mndb == nil {
if ndb == nil {
logout(nil)
return
}
}

guard let ndb = mndb else { return }
guard let ndb else { return }

let pool = RelayPool(ndb: ndb, keypair: keypair)
let model_cache = RelayModelCache()
Expand Down
3 changes: 2 additions & 1 deletion damus/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ var test_damus_state: DamusState = ({
}

print("opening \(tempDir!)")
let ndb = Ndb(path: tempDir)!
let ndb = Ndb(path: tempDir)
let _ = ndb.open()!
let our_pubkey = test_pubkey
let pool = RelayPool(ndb: ndb)
let settings = UserSettingsStore()
Expand Down
2 changes: 1 addition & 1 deletion damus/Util/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ enum Route: Hashable {
case .CreateAccount:
CreateAccountView(nav: navigationCoordinator)
case .SaveKeys(let account):
SaveKeysView(account: account)
SaveKeysView(account: account, pool: damusState.pool)
case .Wallet(let walletModel):
WalletView(damus_state: damusState, model: walletModel)
case .WalletScanner(let walletScanResult):
Expand Down
4 changes: 2 additions & 2 deletions damus/Views/SaveKeysView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Security

struct SaveKeysView: View {
let account: CreateAccountModel
let pool: RelayPool = RelayPool(ndb: Ndb()!)
let pool: RelayPool
@State var pub_copied: Bool = false
@State var priv_copied: Bool = false
@State var loading: Bool = false
Expand Down Expand Up @@ -252,7 +252,7 @@ struct SaveKeyView: View {
struct SaveKeysView_Previews: PreviewProvider {
static var previews: some View {
let model = CreateAccountModel(real: "William", nick: "jb55", about: "I'm me")
SaveKeysView(account: model)
SaveKeysView(account: model, pool: RelayPool(ndb: test_damus_state.ndb))
}
}

Expand Down
54 changes: 38 additions & 16 deletions nostrdb/Ndb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,24 @@ enum DatabaseError: Error {
}
}


func subscription_cb(ctx: UnsafeMutableRawPointer?, subid: UInt64) -> Void {
guard let ctx else { return }
let ndb = Unmanaged<Ndb>.fromOpaque(ctx).takeUnretainedValue()
ndb.sub_cb?(subid)
}

class Ndb {
var ndb: ndb_t
let path: String?
let owns_db: Bool
var generation: Int
let sub_cb: ((UInt64) -> ())?
private var closed: Bool

var is_closed: Bool {
self.closed || self.ndb.ndb == nil
}

static func safemode() -> Ndb? {
guard let path = db_path ?? old_db_path else { return nil }

Expand All @@ -49,7 +56,8 @@ class Ndb {
}
}

guard let ndb = Ndb(path: path) else {
let ndb = Ndb(path: path)
guard let _ = ndb.open() else {
return nil
}

Expand Down Expand Up @@ -79,14 +87,14 @@ class Ndb {
print("txn: NOSTRDB EMPTY")
return Ndb(ndb: ndb_t(ndb: nil))
}
static func open(path: String? = nil, owns_db_file: Bool = true) -> ndb_t? {

func open() -> ndb_t? {
var ndb_p: OpaquePointer? = nil

let ingest_threads: Int32 = 4
var mapsize: Int = 1024 * 1024 * 1024 * 32

if path == nil && owns_db_file {
if path == nil && owns_db {
// `nil` path indicates the default path will be used.
// The default path changed over time, so migrate the database to the new location if needed
do {
Expand All @@ -99,7 +107,7 @@ class Ndb {
}

guard let db_path = Self.db_path,
owns_db_file || Self.db_files_exist(path: db_path) else {
owns_db || Self.db_files_exist(path: db_path) else {
return nil // If the caller claims to not own the DB file, and the DB files do not exist, then we should not initialize Ndb
}

Expand All @@ -109,8 +117,9 @@ class Ndb {

let ok = path.withCString { testdir in
var ok = false
let ctx = Unmanaged.passUnretained(self).toOpaque()
while !ok && mapsize > 1024 * 1024 * 700 {
var cfg = ndb_config(flags: 0, ingester_threads: ingest_threads, mapsize: mapsize, filter_context: nil, ingest_filter: nil, sub_cb_ctx: nil, sub_cb: nil)
var cfg = ndb_config(flags: 0, ingester_threads: ingest_threads, mapsize: mapsize, filter_context: nil, ingest_filter: nil, sub_cb_ctx: ctx, sub_cb: subscription_cb)
ok = ndb_init(&ndb_p, testdir, &cfg) != 0
if !ok {
mapsize /= 2
Expand All @@ -123,19 +132,17 @@ class Ndb {
return nil
}

return ndb_t(ndb: ndb_p)
self.ndb = ndb_t(ndb: ndb_p)
return self.ndb
}

init?(path: String? = nil, owns_db_file: Bool = true) {
guard let db = Self.open(path: path, owns_db_file: owns_db_file) else {
return nil
}

init(path: String? = nil, owns_db_file: Bool = true, sub_cb: ((UInt64) -> ())? = nil) {
self.generation = 0
self.path = path
self.owns_db = owns_db_file
self.ndb = db
self.closed = false
self.sub_cb = sub_cb
self.ndb = ndb_t()
}

private static func migrate_db_location_if_needed() throws {
Expand Down Expand Up @@ -181,6 +188,7 @@ class Ndb {
self.path = nil
self.owns_db = true
self.closed = false
self.sub_cb = nil
}

func close() {
Expand All @@ -193,8 +201,8 @@ class Ndb {
}

func reopen() -> Bool {
guard self.is_closed,
let db = Self.open(path: self.path, owns_db_file: self.owns_db) else {
let ctx = Unmanaged.passUnretained(self).toOpaque()
guard self.is_closed, let db = self.open() else {
return false
}

Expand All @@ -205,6 +213,20 @@ class Ndb {
return true
}

func poll_for_notes(subid: Int64, capacity: Int) -> [NoteKey] {
var buf = Array<UInt64>.init(repeating: 0, count: capacity)

let r = buf.withUnsafeMutableBufferPointer { bytes in
return ndb_poll_for_notes(self.ndb.ndb, UInt64(subid), bytes.baseAddress, Int32(capacity))
}

guard r != 0 else {
return []
}

return Array(buf.prefix(Int(r)))
}

func lookup_blocks_by_key_with_txn<Y>(_ key: NoteKey, txn: NdbTxn<Y>) -> NdbBlocks? {
guard let blocks = ndb_get_blocks_by_key(self.ndb.ndb, &txn.txn, key) else {
return nil
Expand Down
14 changes: 14 additions & 0 deletions nostrdb/QueryResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// QueryResult.swift
// damus
//
// Created by William Casarin on 2024-04-05.
//

import Foundation

// A timeline note holds the key and
struct TimelineNote {
let note_key: NoteKey
let created_at: UInt64
}
2 changes: 1 addition & 1 deletion nostrdb/src/nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -5959,7 +5959,7 @@ uint64_t ndb_subscribe(struct ndb *ndb, struct ndb_filter *filters, int num_filt
ndb_filter_group_init(&sub->group);
if (!ndb_filter_group_add_filters(&sub->group, filters, num_filters))
return 0;

// 500k ought to be enough for anyone
buflen = sizeof(uint64_t) * 65536;
buf = malloc(buflen);
Expand Down

0 comments on commit 10bc4f4

Please sign in to comment.