Skip to content

Commit

Permalink
Adds zap amount cache for the memory space calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Aug 8, 2024
1 parent 8ed78ea commit 282d461
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,20 @@ class LnZapEvent(
// This event is also kept in LocalCache (same object)
@Transient val zapRequest: LnZapRequestEvent?

// Keeps this as a field because it's a heavier function used everywhere.
val amount by lazy {
try {
lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) }
} catch (e: Exception) {
Log.e("LnZapEvent", "Failed to Parse LnInvoice ${lnInvoice()}", e)
null
}
}

override fun countMemory(): Long =
super.countMemory() +
pointerSizeInBytes + (zapRequest?.countMemory() ?: 0) // rough calculation
pointerSizeInBytes + (zapRequest?.countMemory() ?: 0) + // rough calculation
pointerSizeInBytes + 36 // bigdecimal size

override fun containedPost(): LnZapRequestEvent? =
try {
Expand Down Expand Up @@ -75,16 +86,6 @@ class LnZapEvent(

override fun amount() = amount

// Keeps this as a field because it's a heavier function used everywhere.
val amount by lazy {
try {
lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) }
} catch (e: Exception) {
Log.e("LnZapEvent", "Failed to Parse LnInvoice ${lnInvoice()}", e)
null
}
}

override fun content(): String = content

fun lnInvoice() = tags.firstOrNull { it.size > 1 && it[0] == "bolt11" }?.get(1)
Expand Down

0 comments on commit 282d461

Please sign in to comment.