From 624ac88eee70c3f83fac75cc3c0b54fb56ad9d21 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 21 Sep 2026 15:22:19 +0000 Subject: [PATCH] refactor(commons): spill NIP-95 blobs through a KMP store, not java.io.File MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LocalCacheHost's nip95BlobDir was the last java.* type in the port, and a directory is not what the cache needs — it needs somewhere to put bytes and a way to ask whether they are already there. That is now Nip95BlobStore, with an okio-backed FileSystemNip95BlobStore in commonMain; okio was already a commons dependency for DeferredDeleteFileSystem, so nothing new is linked in. Its constructor takes the platform's own path string, so the Android host passes modules.nip95cache.absolutePath and needs no okio of its own. okio declares FileSystem.SYSTEM per platform rather than in common, hence the small platformFileSystem expect/actual beside it. Two things the rewrite had to face that the old block did not: decode() returns ByteArray?, which the old code passed straight into FileOutputStream.write — a platform type, so the compiler allowed it and a null would have thrown NPE past the IOException catch. It is now checked, and undecodable content simply is not spilled. The note's copy dropped its content whenever a blob directory existed, even when the write had just failed with an IOException — losing the only copy of those bytes. Content is now dropped only when the blob really is in the store, whether this call wrote it or found it already there. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01KkULS5SVq4GHDdoCzajKi8 --- .../amethyst/AmethystLocalCacheHost.kt | 7 +- .../commons/model/cache/Nip95BlobStore.kt | 75 +++++++++++++++++++ .../amethyst/commons/util/SystemFileSystem.kt | 31 ++++++++ .../commons/util/SystemFileSystem.ios.kt | 25 +++++++ .../commons/model/cache/EventCache.kt | 52 +++++++------ .../commons/model/cache/LocalCacheHost.kt | 11 ++- .../util/SystemFileSystem.jvmAndroid.kt | 25 +++++++ 7 files changed, 191 insertions(+), 35 deletions(-) create mode 100644 commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/cache/Nip95BlobStore.kt create mode 100644 commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.kt create mode 100644 commons/src/iosMain/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.ios.kt create mode 100644 commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.jvmAndroid.kt diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/AmethystLocalCacheHost.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/AmethystLocalCacheHost.kt index 0d7fd603b1..b935b7a401 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/AmethystLocalCacheHost.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/AmethystLocalCacheHost.kt @@ -20,14 +20,15 @@ */ package com.vitorpamplona.amethyst +import com.vitorpamplona.amethyst.commons.model.cache.FileSystemNip95BlobStore import com.vitorpamplona.amethyst.commons.model.cache.LocalCache import com.vitorpamplona.amethyst.commons.model.cache.LocalCacheHost +import com.vitorpamplona.amethyst.commons.model.cache.Nip95BlobStore import com.vitorpamplona.amethyst.service.checkNotInMainThread import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.relay.client.stats.RelayStats import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import kotlinx.coroutines.CoroutineScope -import java.io.File /** * Binds the shared [LocalCache] in `commons` to this app's shell. @@ -45,7 +46,9 @@ class AmethystLocalCacheHost( ) : LocalCacheHost { override val scope: CoroutineScope get() = modules.applicationIOScope - override val nip95BlobDir: File get() = modules.nip95cache + // by lazy, not a getter: modules.nip95cache is itself lazy and creating the directory + // is the point of touching it, so this must not happen once per NIP-95 event. + override val nip95Blobs: Nip95BlobStore by lazy { FileSystemNip95BlobStore(modules.nip95cache.absolutePath) } override val relayStats: RelayStats get() = modules.relayStats diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/cache/Nip95BlobStore.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/cache/Nip95BlobStore.kt new file mode 100644 index 0000000000..e35c28513f --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/cache/Nip95BlobStore.kt @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.model.cache + +import com.vitorpamplona.amethyst.commons.util.platformFileSystem +import com.vitorpamplona.quartz.utils.Log +import okio.FileSystem +import okio.IOException +import okio.Path +import okio.Path.Companion.toPath + +/** + * Where a NIP-95 blob's bytes go so they can leave memory. + * + * The cache does not need a directory, it needs somewhere to put bytes and a way to ask whether + * they are already there — which is all this is. A front end that has nowhere to spill to simply + * supplies none, and the event keeps its content in memory. + */ +interface Nip95BlobStore { + /** Whether a blob is already stored under [id]. */ + fun exists(id: String): Boolean + + /** Stores [bytes] under [id]. Returns false when the write failed. */ + fun store( + id: String, + bytes: ByteArray, + ): Boolean +} + +/** + * The ordinary [Nip95BlobStore]: one file per event id under [directory]. + * + * okio rather than `java.io.File` because this sits behind `LocalCacheHost`, which the event + * cache reads on every NIP-95 event, and the cache is shared code. + */ +class FileSystemNip95BlobStore( + private val directory: Path, + private val fileSystem: FileSystem = platformFileSystem, +) : Nip95BlobStore { + /** Takes the platform's own path string, so callers need no okio of their own. */ + constructor(directoryPath: String) : this(directoryPath.toPath()) + + override fun exists(id: String): Boolean = fileSystem.exists(directory / id) + + override fun store( + id: String, + bytes: ByteArray, + ): Boolean = + try { + fileSystem.createDirectories(directory) + fileSystem.write(directory / id) { write(bytes) } + true + } catch (e: IOException) { + Log.e("Nip95BlobStore", "Could not store the NIP-95 blob $id", e) + false + } +} diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.kt new file mode 100644 index 0000000000..e45d6e58ee --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.util + +import okio.FileSystem + +/** + * The real filesystem for this target. + * + * okio declares `FileSystem.SYSTEM` per platform rather than in common — there is no such thing + * on JS — so shared code that wants the ordinary filesystem has to come through here. + */ +expect val platformFileSystem: FileSystem diff --git a/commons/src/iosMain/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.ios.kt b/commons/src/iosMain/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.ios.kt new file mode 100644 index 0000000000..2ca484b1cd --- /dev/null +++ b/commons/src/iosMain/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.ios.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.util + +import okio.FileSystem + +actual val platformFileSystem: FileSystem = FileSystem.SYSTEM diff --git a/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/model/cache/EventCache.kt b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/model/cache/EventCache.kt index b1a59820f6..4edd119a23 100644 --- a/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/model/cache/EventCache.kt +++ b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/model/cache/EventCache.kt @@ -422,9 +422,6 @@ import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch -import java.io.File -import java.io.FileOutputStream -import java.io.IOException import java.util.SortedSet /** @@ -3084,42 +3081,43 @@ open class EventCache : note.addRelay(relay) } - // A host with no blob directory keeps the bytes in memory below instead of spilling them. - val cachePath = appHost.nip95BlobDir + // A host with nowhere to spill keeps the bytes in memory below instead. + val blobs = appHost.nip95Blobs + + // Whether the bytes are in the store now. The note's copy may only drop its content + // once this is true — a store that refused the write is not somewhere to drop it. + var spilled = blobs != null && blobs.exists(event.id) val isVerified = - if (cachePath == null) { + if (blobs == null || spilled) { wasVerified - } else { - try { - cachePath.mkdirs() - val file = File(cachePath, event.id) - if (!file.exists() && (wasVerified || justVerify(event))) { - FileOutputStream(file).use { stream -> - stream.write(event.decode()) - } - Log.i( - "FileStorageEvent", - "NIP95 File received from $relay and saved to disk as $file", - ) - true - } else { - wasVerified - } - } catch (e: IOException) { - Log.e("FileStorageEvent", "FileStorageEvent save to disk error: " + event.id, e) + } else if (wasVerified || justVerify(event)) { + // decode() is null when the content is not the base64 the event claims, so + // there is nothing to spill and nothing this call can vouch for. + val bytes = event.decode() + if (bytes != null && blobs.store(event.id, bytes)) { + spilled = true + Log.i( + "FileStorageEvent", + "NIP95 File received from $relay and stored as ${event.id}", + ) + true + } else { wasVerified } + } else { + wasVerified } // Already processed this event. if (note.event != null) return false if (isVerified || justVerify(event)) { - // The blob is on disk now, so the copy in the note drops its content. Without a - // directory to spill to there is nowhere else for it to live, so keep it whole. + // The blob is in the store, so the copy in the note drops its content. If it is + // not — no store, a refused write, undecodable content — the note is the only + // thing holding those bytes, so the event stays whole. val stored = - if (cachePath != null) { + if (spilled) { FileStorageEvent(event.id, event.pubKey, event.createdAt, event.tags, "", event.sig) } else { event diff --git a/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/model/cache/LocalCacheHost.kt b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/model/cache/LocalCacheHost.kt index 27cf2884f1..5e60076323 100644 --- a/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/model/cache/LocalCacheHost.kt +++ b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/model/cache/LocalCacheHost.kt @@ -26,14 +26,13 @@ import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob -import java.io.File /** * The handful of things the event cache needs from the application shell around it. * * The cache itself is a plain in-memory store and can run without any of them, so every * member here has a default that is the honest answer for a host that does not provide it - * — an own scope, no NIP-95 spill directory, no relay identity, no stats sink. A front end + * — an own scope, nowhere to spill NIP-95 blobs, no relay identity, no stats sink. A front end * installs its own via [EventCache.appHost]; Android's `Amethyst` does so at startup, * Desktop and unit tests keep the defaults. * @@ -57,10 +56,10 @@ interface LocalCacheHost { val isDebug: Boolean /** - * Directory the NIP-95 `FileStorageEvent` blobs are spilled to, so their bytes can leave - * memory once they are on disk. `null` keeps the event in memory and skips the spill. + * Where NIP-95 `FileStorageEvent` blobs are spilled, so their bytes can leave memory once + * they are stored. `null` keeps the event in memory and skips the spill. */ - val nip95BlobDir: File? + val nip95Blobs: Nip95BlobStore? /** Per-relay counters the anti-spam filter reports duplicate events to. */ val relayStats: RelayStats? @@ -81,7 +80,7 @@ interface LocalCacheHost { companion object Default : LocalCacheHost { override val scope: CoroutineScope by lazy { CoroutineScope(Dispatchers.IO + SupervisorJob()) } override val isDebug = false - override val nip95BlobDir: File? = null + override val nip95Blobs: Nip95BlobStore? = null override val relayStats: RelayStats? = null } } diff --git a/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.jvmAndroid.kt b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.jvmAndroid.kt new file mode 100644 index 0000000000..2ca484b1cd --- /dev/null +++ b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/util/SystemFileSystem.jvmAndroid.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.util + +import okio.FileSystem + +actual val platformFileSystem: FileSystem = FileSystem.SYSTEM