nostrdb: add profiles to nostrdb

This adds profiles to nostrdb

- Remove in-memory Profiles caches, nostrdb is as fast as an in-memory cache
- Remove ProfileDatabase and just use nostrdb directly

Changelog-Changed: Use nostrdb for profiles
This commit is contained in:
William Casarin
2023-08-28 07:52:59 -07:00
parent 8586eed635
commit bb4fd75576
42 changed files with 362 additions and 705 deletions

View File

@@ -37,7 +37,7 @@ final class NostrScriptTests: XCTestCase {
func test_bool_set() throws {
let data = try load_bool_set_test_wasm().bytes
let pool = RelayPool()
let pool = RelayPool(ndb: .empty)
let script = NostrScript(pool: pool, data: data)
let pk = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!
UserSettingsStore.pubkey = pk

View File

@@ -1,124 +0,0 @@
//
// ProfileDatabaseTests.swift
// damusTests
//
// Created by Bryan Montz on 5/13/23.
//
import XCTest
@testable import damus
class ProfileDatabaseTests: XCTestCase {
static let cache_url = (FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.appendingPathComponent("test-profiles"))!
let database = ProfileDatabase(cache_url: ProfileDatabaseTests.cache_url)
override func tearDownWithError() throws {
// This method is called after the invocation of each test method in the class.
try database.remove_all_profiles()
}
var test_profile: Profile {
Profile(name: "test-name",
display_name: "test-display-name",
about: "test-about",
picture: "test-picture",
banner: "test-banner",
website: "test-website",
lud06: "test-lud06",
lud16: "test-lud16",
nip05: "test-nip05",
damus_donation: 100)
}
func testStoreAndRetrieveProfile() async throws {
let id = test_pubkey
let profile = test_profile
// make sure it's not there yet
XCTAssertNil(database.get(id: id))
// store the profile
try await database.upsert(id: id, profile: profile, last_update: .now)
// read the profile out of the database
let retrievedProfile = try XCTUnwrap(database.get(id: id))
XCTAssertEqual(profile.name, retrievedProfile.name)
XCTAssertEqual(profile.display_name, retrievedProfile.display_name)
XCTAssertEqual(profile.about, retrievedProfile.about)
XCTAssertEqual(profile.picture, retrievedProfile.picture)
XCTAssertEqual(profile.banner, retrievedProfile.banner)
XCTAssertEqual(profile.website, retrievedProfile.website)
XCTAssertEqual(profile.lud06, retrievedProfile.lud06)
XCTAssertEqual(profile.lud16, retrievedProfile.lud16)
XCTAssertEqual(profile.nip05, retrievedProfile.nip05)
XCTAssertEqual(profile.damus_donation, retrievedProfile.damus_donation)
}
func testRejectOutdatedProfile() async throws {
let id = test_pubkey
// store a profile
let profile = test_profile
let profile_last_updated = Date.now
try await database.upsert(id: id, profile: profile, last_update: profile_last_updated)
// try to store a profile with the same id but the last_update date is older than the previously stored profile
let outdatedProfile = test_profile
let outdated_last_updated = profile_last_updated.addingTimeInterval(-60)
do {
try await database.upsert(id: id, profile: outdatedProfile, last_update: outdated_last_updated)
XCTFail("expected to throw error")
} catch let error as ProfileDatabaseError {
XCTAssertEqual(error, ProfileDatabaseError.outdated_input)
} catch {
XCTFail("not the expected error")
}
}
func testUpdateExistingProfile() async throws {
let id = test_pubkey
// store a profile
let profile = test_profile
let profile_last_update = Date.now
try await database.upsert(id: id, profile: profile, last_update: profile_last_update)
// update the same profile
let updated_profile = test_profile
updated_profile.nip05 = "updated-nip05"
let updated_profile_last_update = profile_last_update.addingTimeInterval(60)
try await database.upsert(id: id, profile: updated_profile, last_update: updated_profile_last_update)
// retrieve the profile and make sure it was updated
let retrieved_profile = database.get(id: id)
XCTAssertEqual(retrieved_profile?.nip05, "updated-nip05")
}
func testStoreMultipleAndRemoveAllProfiles() async throws {
XCTAssertEqual(database.count, 0)
// store a profile
let id = test_pubkey
let profile = test_profile
let profile_last_update = Date.now
try await database.upsert(id: id, profile: profile, last_update: profile_last_update)
XCTAssertEqual(database.count, 1)
// store another profile
let id2 = test_pubkey_2
let profile2 = test_profile
let profile_last_update2 = Date.now
try await database.upsert(id: id2, profile: profile2, last_update: profile_last_update2)
XCTAssertEqual(database.count, 2)
try database.remove_all_profiles()
XCTAssertEqual(database.count, 0)
}
}

View File

@@ -14,6 +14,7 @@ final class UserSearchCacheTests: XCTestCase {
let damusState = DamusState.empty
let nip05 = "_@somedomain.com"
@MainActor
override func setUpWithError() throws {
keypair = try XCTUnwrap(generate_new_keypair())
@@ -24,8 +25,6 @@ final class UserSearchCacheTests: XCTestCase {
damusState.profiles.set_validated(pubkey, nip05: validatedNip05)
let profile = Profile(name: "tyiu", display_name: "Terry Yiu", about: nil, picture: nil, banner: nil, website: nil, lud06: nil, lud16: nil, nip05: nip05, damus_donation: nil)
let timestampedProfile = TimestampedProfile(profile: profile, timestamp: 0, event: test_note)
damusState.profiles.add(id: pubkey, profile: timestampedProfile)
// Lookup to synchronize access on profiles dictionary to avoid race conditions.
let _ = damusState.profiles.lookup(id: pubkey)
@@ -47,6 +46,7 @@ final class UserSearchCacheTests: XCTestCase {
XCTAssertEqual(damusState.user_search_cache.search(key: "i"), [keypair.pubkey])
}
@MainActor
func testUpdateProfile() throws {
let keypair = try XCTUnwrap(keypair)
@@ -56,8 +56,6 @@ final class UserSearchCacheTests: XCTestCase {
damusState.profiles.set_validated(keypair.pubkey, nip05: NIP05.parse(newNip05))
let newProfile = Profile(name: "whoami", display_name: "T-DAWG", about: nil, picture: nil, banner: nil, website: nil, lud06: nil, lud16: nil, nip05: newNip05, damus_donation: nil)
let newTimestampedProfile = TimestampedProfile(profile: newProfile, timestamp: 1000, event: test_note)
damusState.profiles.add(id: keypair.pubkey, profile: newTimestampedProfile)
// Lookup to synchronize access on profiles dictionary to avoid race conditions.
let _ = damusState.profiles.lookup(id: keypair.pubkey)

View File

@@ -84,7 +84,7 @@ final class WalletConnectTests: XCTestCase {
let pk = "89446b900c70d62438dcf66756405eea6225ad94dc61f3856f62f9699111a9a6"
let nwc = WalletConnectURL(str: "nostrwalletconnect://\(pk)?relay=ws://127.0.0.1&secret=\(sec)&lud16=jb55@jb55.com")!
let pool = RelayPool()
let pool = RelayPool(ndb: .empty)
let box = PostBox(pool: pool)
nwc_pay(url: nwc, pool: pool, post: box, invoice: "invoice")

View File

@@ -11,8 +11,6 @@ import XCTest
final class ZapTests: XCTestCase {
override func setUpWithError() throws {
let db = ProfileDatabase()
try db.remove_all_profiles()
}
override func tearDownWithError() throws {
@@ -71,7 +69,7 @@ final class ZapTests: XCTestCase {
XCTAssertEqual(zap.target, ZapTarget.profile(profile))
XCTAssertEqual(zap_notification_title(zap), "Zap")
XCTAssertEqual(zap_notification_body(profiles: Profiles(user_search_cache: UserSearchCache()), zap: zap), "You received 1k sats from 107jk7ht:2quqncxg")
XCTAssertEqual(zap_notification_body(profiles: Profiles(user_search_cache: UserSearchCache(), ndb: .empty), zap: zap), "You received 1k sats from 107jk7ht:2quqncxg")
}
}