Skip to content

Commit c476272

Browse files
committed
wip local relay model
Signed-off-by: William Casarin <[email protected]>
1 parent d0b449c commit c476272

File tree

8 files changed

+75
-28
lines changed

8 files changed

+75
-28
lines changed

damus.xcodeproj/project.pbxproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@
205205
4C75EFB728049D990006080F /* RelayPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB628049D990006080F /* RelayPool.swift */; };
206206
4C75EFB92804A2740006080F /* EventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB82804A2740006080F /* EventView.swift */; };
207207
4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */; };
208+
4C79A6DC2BC07FC6007B2D87 /* QueryResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */; };
209+
4C79A6DD2BC07FC6007B2D87 /* QueryResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */; };
208210
4C7D09592A05BEAD00943473 /* KeyboardVisible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D09582A05BEAD00943473 /* KeyboardVisible.swift */; };
209211
4C7D095F2A098C5D00943473 /* ConnectWalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D095C2A098C5D00943473 /* ConnectWalletView.swift */; };
210212
4C7D09602A098C5D00943473 /* WalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D095D2A098C5D00943473 /* WalletView.swift */; };
@@ -1072,6 +1074,7 @@
10721074
4C78EFD82A707C4D007E8197 /* secp256k1_ecdh.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1_ecdh.h; sourceTree = "<group>"; };
10731075
4C78EFD92A707C4D007E8197 /* secp256k1.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1.h; sourceTree = "<group>"; };
10741076
4C78EFDA2A707C67007E8197 /* secp256k1_extrakeys.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = secp256k1_extrakeys.h; sourceTree = "<group>"; };
1077+
4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueryResult.swift; sourceTree = "<group>"; };
10751078
4C7D09582A05BEAD00943473 /* KeyboardVisible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardVisible.swift; sourceTree = "<group>"; };
10761079
4C7D095C2A098C5D00943473 /* ConnectWalletView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectWalletView.swift; sourceTree = "<group>"; };
10771080
4C7D095D2A098C5D00943473 /* WalletView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletView.swift; sourceTree = "<group>"; };
@@ -2130,6 +2133,7 @@
21302133
D798D2272B085CDA00234419 /* NdbNote+.swift */,
21312134
4CF480562B633F2600F2B2C0 /* NdbBlocksIterator.swift */,
21322135
4CF480582B633F3800F2B2C0 /* NdbBlock.swift */,
2136+
4C79A6DB2BC07FC6007B2D87 /* QueryResult.swift */,
21332137
);
21342138
path = nostrdb;
21352139
sourceTree = "<group>";
@@ -3360,6 +3364,7 @@
33603364
4C363A8828236948006E126D /* BlocksView.swift in Sources */,
33613365
4C06670628FCB08600038D2A /* ImageCarousel.swift in Sources */,
33623366
3A23838E2A297DD200E5AA2E /* ZapButtonModel.swift in Sources */,
3367+
4C79A6DC2BC07FC6007B2D87 /* QueryResult.swift in Sources */,
33633368
F71694F82A6983AF001F4053 /* GrayGradient.swift in Sources */,
33643369
4C1D4FB12A7958E60024F453 /* VersionInfo.swift in Sources */,
33653370
D7FF94002AC7AC5300FD969D /* RelayURL.swift in Sources */,
@@ -3397,7 +3402,6 @@
33973402
4C19AE512A5CEF7C00C90DB7 /* NostrScript.swift in Sources */,
33983403
4C32B95E2A9AD44700DC3548 /* FlatBufferObject.swift in Sources */,
33993404
D783A63F2AD4E53D00658DDA /* SuggestedHashtagsView.swift in Sources */,
3400-
4CB88393296F798300DC99E7 /* ReactionsModel.swift in Sources */,
34013405
5C42E78C29DB76D90086AAC1 /* EmptyUserSearchView.swift in Sources */,
34023406
4CB88396296F7F8B00DC99E7 /* ReactionView.swift in Sources */,
34033407
4CF480552B631C4F00F2B2C0 /* wasm.c in Sources */,
@@ -3732,6 +3736,7 @@
37323736
D798D2222B08598A00234419 /* ReferencedId.swift in Sources */,
37333737
D7CE1B492B0BE729002EDAD4 /* DisplayName.swift in Sources */,
37343738
D7CE1B192B0BE132002EDAD4 /* builder.c in Sources */,
3739+
4C79A6DD2BC07FC6007B2D87 /* QueryResult.swift in Sources */,
37353740
D7EDED1F2B11797D0018B19C /* LongformEvent.swift in Sources */,
37363741
D7CCFC122B05886D00323D86 /* IdType.swift in Sources */,
37373742
D7CE1B312B0BE69D002EDAD4 /* Ndb.swift in Sources */,

damus/ContentView.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -661,22 +661,25 @@ struct ContentView: View {
661661
self.selected_timeline = timeline
662662
}
663663

664+
func on_local_sub(subid: UInt64) {
665+
}
666+
664667
func connect() {
665668
// nostrdb
666-
var mndb = Ndb()
667-
if mndb == nil {
669+
var ndb: Ndb? = Ndb()
670+
if ndb?.open() == nil {
668671
// try recovery
669672
print("DB ISSUE! RECOVERING")
670-
mndb = Ndb.safemode()
673+
ndb = Ndb.safemode()
671674

672675
// out of space or something?? maybe we need a in-memory fallback
673-
if mndb == nil {
676+
if ndb == nil {
674677
logout(nil)
675678
return
676679
}
677680
}
678681

679-
guard let ndb = mndb else { return }
682+
guard let ndb else { return }
680683

681684
let pool = RelayPool(ndb: ndb, keypair: keypair)
682685
let model_cache = RelayModelCache()

damus/TestData.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ var test_damus_state: DamusState = ({
6464
}
6565

6666
print("opening \(tempDir!)")
67-
let ndb = Ndb(path: tempDir)!
67+
let ndb = Ndb(path: tempDir)
68+
let _ = ndb.open()!
6869
let our_pubkey = test_pubkey
6970
let pool = RelayPool(ndb: ndb)
7071
let settings = UserSettingsStore()

damus/Util/Router.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ enum Route: Hashable {
111111
case .CreateAccount:
112112
CreateAccountView(nav: navigationCoordinator)
113113
case .SaveKeys(let account):
114-
SaveKeysView(account: account)
114+
SaveKeysView(account: account, pool: damusState.pool)
115115
case .Wallet(let walletModel):
116116
WalletView(damus_state: damusState, model: walletModel)
117117
case .WalletScanner(let walletScanResult):

damus/Views/SaveKeysView.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import Security
1010

1111
struct SaveKeysView: View {
1212
let account: CreateAccountModel
13-
let pool: RelayPool = RelayPool(ndb: Ndb()!)
13+
let pool: RelayPool
14+
1415
@State var pub_copied: Bool = false
1516
@State var priv_copied: Bool = false
1617
@State var loading: Bool = false
@@ -23,7 +24,8 @@ struct SaveKeysView: View {
2324

2425
let first_contact_event: NdbNote?
2526

26-
init(account: CreateAccountModel) {
27+
init(account: CreateAccountModel, pool: RelayPool) {
28+
self.pool = pool
2729
self.account = account
2830
self.first_contact_event = make_first_contact_event(keypair: account.keypair)
2931
}
@@ -252,7 +254,7 @@ struct SaveKeyView: View {
252254
struct SaveKeysView_Previews: PreviewProvider {
253255
static var previews: some View {
254256
let model = CreateAccountModel(real: "William", nick: "jb55", about: "I'm me")
255-
SaveKeysView(account: model)
257+
SaveKeysView(account: model, pool: RelayPool(ndb: test_damus_state.ndb))
256258
}
257259
}
258260

nostrdb/Ndb.swift

+38-16
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,24 @@ enum DatabaseError: Error {
2727
}
2828
}
2929

30+
31+
func subscription_cb(ctx: UnsafeMutableRawPointer?, subid: UInt64) -> Void {
32+
guard let ctx else { return }
33+
let ndb = Unmanaged<Ndb>.fromOpaque(ctx).takeUnretainedValue()
34+
ndb.sub_cb?(subid)
35+
}
36+
3037
class Ndb {
3138
var ndb: ndb_t
3239
let path: String?
3340
let owns_db: Bool
3441
var generation: Int
42+
let sub_cb: ((UInt64) -> ())?
3543
private var closed: Bool
3644

3745
var is_closed: Bool {
3846
self.closed || self.ndb.ndb == nil
3947
}
40-
4148
static func safemode() -> Ndb? {
4249
guard let path = db_path ?? old_db_path else { return nil }
4350

@@ -49,7 +56,8 @@ class Ndb {
4956
}
5057
}
5158

52-
guard let ndb = Ndb(path: path) else {
59+
let ndb = Ndb(path: path)
60+
guard let _ = ndb.open() else {
5361
return nil
5462
}
5563

@@ -79,14 +87,14 @@ class Ndb {
7987
print("txn: NOSTRDB EMPTY")
8088
return Ndb(ndb: ndb_t(ndb: nil))
8189
}
82-
83-
static func open(path: String? = nil, owns_db_file: Bool = true) -> ndb_t? {
90+
91+
func open() -> ndb_t? {
8492
var ndb_p: OpaquePointer? = nil
8593

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

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

101109
guard let db_path = Self.db_path,
102-
owns_db_file || Self.db_files_exist(path: db_path) else {
110+
owns_db || Self.db_files_exist(path: db_path) else {
103111
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
104112
}
105113

@@ -109,8 +117,9 @@ class Ndb {
109117

110118
let ok = path.withCString { testdir in
111119
var ok = false
120+
let ctx = Unmanaged.passUnretained(self).toOpaque()
112121
while !ok && mapsize > 1024 * 1024 * 700 {
113-
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)
122+
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)
114123
ok = ndb_init(&ndb_p, testdir, &cfg) != 0
115124
if !ok {
116125
mapsize /= 2
@@ -123,19 +132,17 @@ class Ndb {
123132
return nil
124133
}
125134

126-
return ndb_t(ndb: ndb_p)
135+
self.ndb = ndb_t(ndb: ndb_p)
136+
return self.ndb
127137
}
128138

129-
init?(path: String? = nil, owns_db_file: Bool = true) {
130-
guard let db = Self.open(path: path, owns_db_file: owns_db_file) else {
131-
return nil
132-
}
133-
139+
init(path: String? = nil, owns_db_file: Bool = true, sub_cb: ((UInt64) -> ())? = nil) {
134140
self.generation = 0
135141
self.path = path
136142
self.owns_db = owns_db_file
137-
self.ndb = db
138143
self.closed = false
144+
self.sub_cb = sub_cb
145+
self.ndb = ndb_t()
139146
}
140147

141148
private static func migrate_db_location_if_needed() throws {
@@ -181,6 +188,7 @@ class Ndb {
181188
self.path = nil
182189
self.owns_db = true
183190
self.closed = false
191+
self.sub_cb = nil
184192
}
185193

186194
func close() {
@@ -193,8 +201,8 @@ class Ndb {
193201
}
194202

195203
func reopen() -> Bool {
196-
guard self.is_closed,
197-
let db = Self.open(path: self.path, owns_db_file: self.owns_db) else {
204+
let ctx = Unmanaged.passUnretained(self).toOpaque()
205+
guard self.is_closed, let db = self.open() else {
198206
return false
199207
}
200208

@@ -205,6 +213,20 @@ class Ndb {
205213
return true
206214
}
207215

216+
func poll_for_notes(subid: Int64, capacity: Int) -> [NoteKey] {
217+
var buf = Array<UInt64>.init(repeating: 0, count: capacity)
218+
219+
let r = buf.withUnsafeMutableBufferPointer { bytes in
220+
return ndb_poll_for_notes(self.ndb.ndb, UInt64(subid), bytes.baseAddress, Int32(capacity))
221+
}
222+
223+
guard r != 0 else {
224+
return []
225+
}
226+
227+
return Array(buf.prefix(Int(r)))
228+
}
229+
208230
func lookup_blocks_by_key_with_txn<Y>(_ key: NoteKey, txn: NdbTxn<Y>) -> NdbBlocks? {
209231
guard let blocks = ndb_get_blocks_by_key(self.ndb.ndb, &txn.txn, key) else {
210232
return nil

nostrdb/QueryResult.swift

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// QueryResult.swift
3+
// damus
4+
//
5+
// Created by William Casarin on 2024-04-05.
6+
//
7+
8+
import Foundation
9+
10+
// A timeline note holds the key and
11+
struct TimelineNote {
12+
let note_key: NoteKey
13+
let created_at: UInt64
14+
}

nostrdb/src/nostrdb.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5959,7 +5959,7 @@ uint64_t ndb_subscribe(struct ndb *ndb, struct ndb_filter *filters, int num_filt
59595959
ndb_filter_group_init(&sub->group);
59605960
if (!ndb_filter_group_add_filters(&sub->group, filters, num_filters))
59615961
return 0;
5962-
5962+
59635963
// 500k ought to be enough for anyone
59645964
buflen = sizeof(uint64_t) * 65536;
59655965
buf = malloc(buflen);

0 commit comments

Comments
 (0)