Skip to content

Commit

Permalink
Rework liquidity-ads purchase database storage
Browse files Browse the repository at this point in the history
Payment details are now stored inside the purchase blob.
The payment details type in the db table is just for
information purpose and is not typed. It is a free text
column.

The column storing the purchase/lease type is now also a
free text column. If it happens to contains legacy lease
data, then we apply a special logic to the blob.

The classes used for storing/versioning liquidity ads data
are moved in a liquidity ads package.

The serialiser for LiquidityAds.FundingFee has been removed
(instead we use the same pattern as the other liquidity data).

The methods from moving to the canonical data to the versioned
db data have been rename: asDb(), asCanonical().
  • Loading branch information
dpad85 committed Sep 20, 2024
1 parent 3668a3e commit 231ea8f
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 304 deletions.
4 changes: 0 additions & 4 deletions src/commonMain/kotlin/fr/acinq/lightning/bin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,6 @@ class Phoenixd : CliktCommand() {
channel_close_outgoing_paymentsAdapter = Channel_close_outgoing_payments.Adapter(
closing_info_typeAdapter = EnumColumnAdapter()
),
inbound_liquidity_outgoing_paymentsAdapter = Inbound_liquidity_outgoing_payments.Adapter(
lease_typeAdapter = EnumColumnAdapter(),
payment_details_typeAdapter = EnumColumnAdapter()
),
)
val channelsDb = SqliteChannelsDb(driver, database)
val paymentsDb = SqlitePaymentsDb(database)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ object DbTypesHelper {
subclass(IncomingReceivedWithData.Part.SpliceIn.V0::class)
subclass(IncomingReceivedWithData.Part.FeeCredit.V0::class)
}
polymorphic(InboundLiquidityPaymentDetailsData::class) {
subclass(InboundLiquidityPaymentDetailsData.ChannelBalance.V0::class)
subclass(InboundLiquidityPaymentDetailsData.FutureHtlc.V0::class)
subclass(InboundLiquidityPaymentDetailsData.FutureHtlcWithPreimage.V0::class)
subclass(InboundLiquidityPaymentDetailsData.ChannelBalanceForFutureHtlc.V0::class)
}
polymorphic(InboundLiquidityPurchaseData::class) {
subclass(InboundLiquidityPurchaseData.Standard.V0::class)
subclass(InboundLiquidityPurchaseData.WithFeeCredit.V0::class)
}
}

val polymorphicFormat = Json { serializersModule = module }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,36 @@
package fr.acinq.lightning.bin.db.payments

import fr.acinq.bitcoin.TxId
import fr.acinq.lightning.bin.db.payments.liquidityads.PurchaseData
import fr.acinq.lightning.bin.db.payments.liquidityads.PurchaseData.Companion.encodeForDb
import fr.acinq.lightning.db.InboundLiquidityOutgoingPayment
import fr.acinq.lightning.utils.UUID
import fr.acinq.lightning.utils.sat
import fr.acinq.lightning.utils.toByteVector32
import fr.acinq.lightning.wire.LiquidityAds
import fr.acinq.phoenix.db.PhoenixDatabase

class InboundLiquidityQueries(val database: PhoenixDatabase) {
private val queries = database.inboundLiquidityOutgoingPaymentsQueries

fun add(payment: InboundLiquidityOutgoingPayment) {
database.transaction {
val (purchase, details) = payment.mapPurchaseToDb()
queries.insert(
id = payment.id.toString(),
mining_fees_sat = payment.miningFees.sat,
channel_id = payment.channelId.toByteArray(),
tx_id = payment.txId.value.toByteArray(),
lease_type = purchase.first,
lease_blob = purchase.second,
payment_details_type = details.first,
payment_details_blob = details.second,
lease_type = when (payment.purchase) {
is LiquidityAds.Purchase.Standard -> "STANDARD"
is LiquidityAds.Purchase.WithFeeCredit -> "WITH_FEE_CREDIT"
},
lease_blob = payment.purchase.encodeForDb(),
payment_details_type = when (payment.purchase.paymentDetails) {
is LiquidityAds.PaymentDetails.FromChannelBalance -> "FROM_CHANNEL_BALANCE"
is LiquidityAds.PaymentDetails.FromFutureHtlc -> "FROM_FUTURE_HTLC"
is LiquidityAds.PaymentDetails.FromFutureHtlcWithPreimage -> "FROM_FUTURE_HTLC_WITH_PREIMAGE"
is LiquidityAds.PaymentDetails.FromChannelBalanceForFutureHtlc -> "FROM_CHANNEL_BALANCE_FOR_FUTURE_HTLC"
},
created_at = payment.createdAt,
confirmed_at = payment.confirmedAt,
locked_at = payment.lockedAt,
Expand Down Expand Up @@ -73,9 +82,9 @@ class InboundLiquidityQueries(val database: PhoenixDatabase) {
mining_fees_sat: Long,
channel_id: ByteArray,
tx_id: ByteArray,
lease_type: InboundLiquidityPurchaseType,
lease_type: String,
lease_blob: ByteArray,
payment_details_blob: ByteArray,
payment_details_type: String?,
created_at: Long,
confirmed_at: Long?,
locked_at: Long?
Expand All @@ -85,7 +94,7 @@ class InboundLiquidityQueries(val database: PhoenixDatabase) {
miningFees = mining_fees_sat.sat,
channelId = channel_id.toByteVector32(),
txId = TxId(tx_id),
purchase = InboundLiquidityPurchaseData.deserialize(lease_type, lease_blob, payment_details_blob),
purchase = PurchaseData.decodeDataToCanonical(lease_type, lease_blob),
createdAt = created_at,
confirmedAt = confirmed_at,
lockedAt = locked_at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.Satoshi
import fr.acinq.bitcoin.TxId
import fr.acinq.lightning.MilliSatoshi
import fr.acinq.lightning.bin.db.payments.liquidityads.FundingFeeData
import fr.acinq.lightning.bin.db.payments.liquidityads.FundingFeeData.Companion.asCanonical
import fr.acinq.lightning.bin.db.payments.liquidityads.FundingFeeData.Companion.asDb
import fr.acinq.lightning.bin.db.serializers.v1.*
import fr.acinq.lightning.db.IncomingPayment
import fr.acinq.lightning.wire.LiquidityAds
import io.ktor.utils.io.charsets.*
import io.ktor.utils.io.core.*
import kotlinx.serialization.*
Expand Down Expand Up @@ -58,7 +60,7 @@ sealed class IncomingReceivedWithData {
val amountReceived: MilliSatoshi,
val channelId: ByteVector32,
val htlcId: Long,
@Serializable(with = FundingFeeSerializer::class) val fundingFee: LiquidityAds.FundingFee?,
val fundingFee: FundingFeeData?,
) : Htlc()
}

Expand Down Expand Up @@ -116,7 +118,7 @@ sealed class IncomingReceivedWithData {
amountReceived = it.amountReceived,
channelId = it.channelId,
htlcId = it.htlcId,
fundingFee = it.fundingFee
fundingFee = it.fundingFee?.asCanonical()
)
is Part.NewChannel.V2 -> IncomingPayment.ReceivedWith.NewChannel(
amountReceived = it.amount,
Expand Down Expand Up @@ -153,7 +155,7 @@ fun List<IncomingPayment.ReceivedWith>.mapToDb(): Pair<IncomingReceivedWithTypeV
amountReceived = it.amountReceived,
channelId = it.channelId,
htlcId = it.htlcId,
fundingFee = it.fundingFee
fundingFee = it.fundingFee?.asDb()
)
is IncomingPayment.ReceivedWith.NewChannel -> IncomingReceivedWithData.Part.NewChannel.V2(
amount = it.amountReceived,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@file:UseSerializers(
MilliSatoshiSerializer::class,
TxIdSerializer::class,
)

package fr.acinq.lightning.bin.db.payments.liquidityads

import fr.acinq.bitcoin.TxId
import fr.acinq.lightning.MilliSatoshi
import fr.acinq.lightning.bin.db.serializers.v1.MilliSatoshiSerializer
import fr.acinq.lightning.bin.db.serializers.v1.TxIdSerializer
import fr.acinq.lightning.wire.LiquidityAds
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers

@Serializable
sealed class FundingFeeData {

@Serializable
data class V0(val amount: MilliSatoshi, val fundingTxId: TxId) : FundingFeeData()

companion object {
fun FundingFeeData.asCanonical(): LiquidityAds.FundingFee = when (this) {
is V0 -> LiquidityAds.FundingFee(amount = amount, fundingTxId = fundingTxId)
}
fun LiquidityAds.FundingFee.asDb(): FundingFeeData = V0(amount = amount, fundingTxId = fundingTxId)
}
}
Loading

0 comments on commit 231ea8f

Please sign in to comment.