Deletes encrypted component of the GiftWrap after unwrapping to avoid consuming memory needlessly

This commit is contained in:
Vitor Pamplona
2025-05-16 12:47:35 -04:00
parent 07434eb4b6
commit 1a681e1b53
3 changed files with 30 additions and 7 deletions
@@ -177,6 +177,9 @@ class EventNotificationConsumer(
if (LocalCache.justConsume(event, null)) { if (LocalCache.justConsume(event, null)) {
// new event // new event
event.unwrap(signer) { event.unwrap(signer) {
// clear the encrypted payload to save memory
LocalCache.getOrCreateNote(event.id).event = event.copyNoContent()
unwrapAndConsume(it, signer, onReady) unwrapAndConsume(it, signer, onReady)
} }
} }
@@ -185,6 +188,9 @@ class EventNotificationConsumer(
if (LocalCache.justConsume(event, null)) { if (LocalCache.justConsume(event, null)) {
// new event // new event
event.unseal(signer) { event.unseal(signer) {
// clear the encrypted payload to save memory
LocalCache.getOrCreateNote(event.id).event = event.copyNoContent()
// this is not verifiable // this is not verifiable
if (!LocalCache.hasConsumed(it)) { if (!LocalCache.hasConsumed(it)) {
LocalCache.justConsume(it, null) LocalCache.justConsume(it, null)
@@ -1466,6 +1466,9 @@ class AccountViewModel(
unwrapIfNeeded(existingNote.event, onReady) unwrapIfNeeded(existingNote.event, onReady)
} else { } else {
event.unwrap(account.signer) { event.unwrap(account.signer) {
// clear the encrypted payload to save memory
LocalCache.getOrCreateNote(event.id).event = event.copyNoContent()
if (LocalCache.justConsume(it, null)) { if (LocalCache.justConsume(it, null)) {
unwrapIfNeeded(it, onReady) unwrapIfNeeded(it, onReady)
} }
@@ -1473,6 +1476,9 @@ class AccountViewModel(
} }
} ?: run { } ?: run {
event.unwrap(account.signer) { event.unwrap(account.signer) {
// clear the encrypted payload to save memory
LocalCache.getOrCreateNote(event.id).event = event.copyNoContent()
val existingNote = LocalCache.getNoteIfExists(it.id) val existingNote = LocalCache.getNoteIfExists(it.id)
if (existingNote != null) { if (existingNote != null) {
unwrapIfNeeded(existingNote.event, onReady) unwrapIfNeeded(existingNote.event, onReady)
@@ -1491,6 +1497,9 @@ class AccountViewModel(
unwrapIfNeeded(existingNote.event, onReady) unwrapIfNeeded(existingNote.event, onReady)
} else { } else {
event.unseal(account.signer) { event.unseal(account.signer) {
// clear the encrypted payload to save memory
LocalCache.getOrCreateNote(event.id).event = event.copyNoContent()
// this is not verifiable // this is not verifiable
if (LocalCache.justConsume(it, null)) { if (LocalCache.justConsume(it, null)) {
unwrapIfNeeded(it, onReady) unwrapIfNeeded(it, onReady)
@@ -1499,6 +1508,9 @@ class AccountViewModel(
} }
} ?: run { } ?: run {
event.unseal(account.signer) { event.unseal(account.signer) {
// clear the encrypted payload to save memory
LocalCache.getOrCreateNote(event.id).event = event.copyNoContent()
val existingNote = LocalCache.getNoteIfExists(it.id) val existingNote = LocalCache.getNoteIfExists(it.id)
if (existingNote != null) { if (existingNote != null) {
unwrapIfNeeded(existingNote.event, onReady) unwrapIfNeeded(existingNote.event, onReady)
@@ -52,7 +52,7 @@ class PrecacheNewNotesProcessor(
fun consumeAlreadyVerified( fun consumeAlreadyVerified(
event: Event, event: Event,
note: Note, publicNote: Note,
) { ) {
when (event) { when (event) {
is OtsEvent -> { is OtsEvent -> {
@@ -78,16 +78,18 @@ class PrecacheNewNotesProcessor(
val inner = event.innerEventId val inner = event.innerEventId
if (inner == null) { if (inner == null) {
event.unwrap(account.signer) { event.unwrap(account.signer) {
// clear the encrypted payload to save memory
cache.getOrCreateNote(event.id).event = event.copyNoContent()
if (cache.justConsume(it, null)) { if (cache.justConsume(it, null)) {
cache.copyRelaysFromTo(note, it) cache.copyRelaysFromTo(publicNote, it)
consumeAlreadyVerified(it, note) consumeAlreadyVerified(it, publicNote)
} }
} }
} else { } else {
cache.copyRelaysFromTo(note, inner) cache.copyRelaysFromTo(publicNote, inner)
val event = cache.getOrCreateNote(inner).event val event = cache.getOrCreateNote(inner).event
if (event != null) { if (event != null) {
consumeAlreadyVerified(event, note) consumeAlreadyVerified(event, publicNote)
} }
} }
} }
@@ -97,11 +99,14 @@ class PrecacheNewNotesProcessor(
val inner = event.innerEventId val inner = event.innerEventId
if (inner == null) { if (inner == null) {
event.unseal(account.signer) { event.unseal(account.signer) {
// clear the encrypted payload to save memory
cache.getOrCreateNote(event.id).event = event.copyNoContent()
cache.justConsume(it, null) cache.justConsume(it, null)
cache.copyRelaysFromTo(note, it) cache.copyRelaysFromTo(publicNote, it)
} }
} else { } else {
cache.copyRelaysFromTo(note, inner) cache.copyRelaysFromTo(publicNote, inner)
} }
} }