Skip to content

Commit ff563c2

Browse files
committed
add a succeeded_at timestamp to payments
This allows separating between a _completed_ payment and a _successful_ payment. Incoming payments are always successful when they are completed. Outgoing payments may be completed, but unsuccessful. For example a Lightning outgoing payment is completed when all attempts have been exhausted but not route was found.
1 parent ba75224 commit ff563c2

File tree

7 files changed

+34
-21
lines changed

7 files changed

+34
-21
lines changed

modules/core/src/commonMain/kotlin/fr/acinq/lightning/channel/ChannelAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fr.acinq.lightning.MilliSatoshi
66
import fr.acinq.lightning.NodeEvents
77
import fr.acinq.lightning.blockchain.Watch
88
import fr.acinq.lightning.channel.states.PersistedChannelState
9-
import fr.acinq.lightning.db.ChannelClosingType
9+
import fr.acinq.lightning.db.ChannelCloseOutgoingPayment.ChannelClosingType
1010
import fr.acinq.lightning.transactions.Transactions
1111
import fr.acinq.lightning.utils.UUID
1212
import fr.acinq.lightning.wire.*

modules/core/src/commonMain/kotlin/fr/acinq/lightning/channel/states/Channel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import fr.acinq.lightning.channel.Helpers.Closing.claimRemoteCommitTxOutputs
1515
import fr.acinq.lightning.channel.Helpers.Closing.claimRevokedRemoteCommitTxOutputs
1616
import fr.acinq.lightning.channel.Helpers.Closing.getRemotePerCommitmentSecret
1717
import fr.acinq.lightning.crypto.KeyManager
18-
import fr.acinq.lightning.db.ChannelClosingType
18+
import fr.acinq.lightning.db.ChannelCloseOutgoingPayment.ChannelClosingType
1919
import fr.acinq.lightning.logging.*
2020
import fr.acinq.lightning.serialization.channel.Encryption.from
2121
import fr.acinq.lightning.transactions.Transactions.TransactionWithInputInfo.ClosingTx

modules/core/src/commonMain/kotlin/fr/acinq/lightning/db/PaymentsDb.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,17 @@ sealed class WalletPayment {
9191
/** Absolute time in milliseconds since UNIX epoch when the payment was created. */
9292
abstract val createdAt: Long
9393

94-
/** Absolute time in milliseconds since UNIX epoch when the payment was completed. May be null. */
94+
/**
95+
* Absolute time in milliseconds since UNIX epoch when the payment was completed.
96+
* A completed payment is not necessarily successful. For example a Lightning
97+
* outgoing payment will be considered completed after all payment attempts
98+
* have been exhausted.
99+
*/
95100
abstract val completedAt: Long?
96101

102+
/** Absolute time in milliseconds since UNIX epoch when the payment succeeded. */
103+
abstract val succeededAt: Long?
104+
97105
/** Fees applied to complete this payment. */
98106
abstract val fees: MilliSatoshi
99107

@@ -109,6 +117,8 @@ sealed class IncomingPayment : WalletPayment() {
109117
/** Amount received for this part after applying the fees. This is the final amount we can use. */
110118
abstract val amountReceived: MilliSatoshi
111119
override val amount: MilliSatoshi get() = amountReceived
120+
/** A completed incoming payment is always successful. */
121+
override val succeededAt: Long? get() = completedAt
112122
}
113123

114124
/**
@@ -321,6 +331,8 @@ data class LightningOutgoingPayment(
321331

322332
override val completedAt: Long? = (status as? Status.Completed)?.completedAt
323333

334+
override val succeededAt: Long? = (status as? Status.Succeeded)?.completedAt
335+
324336
/** This is the total fees that have been paid to make the payment work. It includes the LN routing fees, the fee for the swap-out service, the mining fees for closing a channel. */
325337
override val fees: MilliSatoshi = when (status) {
326338
is Status.Pending -> 0.msat
@@ -460,6 +472,7 @@ sealed class OnChainOutgoingPayment : OutgoingPayment() {
460472
abstract val confirmedAt: Long?
461473
abstract val lockedAt: Long?
462474
override val completedAt: Long? get() = lockedAt
475+
override val succeededAt: Long? get() = lockedAt
463476

464477
/** Helper method to facilitate updating child classes */
465478
fun setLocked(lockedAt: Long): OnChainOutgoingPayment =
@@ -551,10 +564,6 @@ data class InboundLiquidityOutgoingPayment(
551564
}
552565
}
553566

554-
enum class ChannelClosingType {
555-
Mutual, Local, Remote, Revoked, Other;
556-
}
557-
558567
data class ChannelCloseOutgoingPayment(
559568
override val id: UUID,
560569
val recipientAmount: Satoshi,
@@ -572,6 +581,9 @@ data class ChannelCloseOutgoingPayment(
572581
override val lockedAt: Long?,
573582
val closingType: ChannelClosingType
574583
) : OnChainOutgoingPayment() {
584+
enum class ChannelClosingType {
585+
Mutual, Local, Remote, Revoked, Other;
586+
}
575587
override val amount: MilliSatoshi = (recipientAmount + miningFees).toMilliSatoshi()
576588
override val fees: MilliSatoshi = miningFees.toMilliSatoshi()
577589
}

modules/core/src/commonMain/kotlin/fr/acinq/lightning/serialization/payment/v1/Deserialization.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ object Deserialization {
303303
confirmedAt = readNullable { readNumber() },
304304
lockedAt = readNullable { readNumber() },
305305
closingType = when (val discriminator = read()) {
306-
0x00 -> ChannelClosingType.Mutual
307-
0x01 -> ChannelClosingType.Local
308-
0x02 -> ChannelClosingType.Remote
309-
0x03 -> ChannelClosingType.Revoked
310-
0x04 -> ChannelClosingType.Other
311-
else -> error("unknown discriminator $discriminator for class ${ChannelClosingType::class}")
306+
0x00 -> ChannelCloseOutgoingPayment.ChannelClosingType.Mutual
307+
0x01 -> ChannelCloseOutgoingPayment.ChannelClosingType.Local
308+
0x02 -> ChannelCloseOutgoingPayment.ChannelClosingType.Remote
309+
0x03 -> ChannelCloseOutgoingPayment.ChannelClosingType.Revoked
310+
0x04 -> ChannelCloseOutgoingPayment.ChannelClosingType.Other
311+
else -> error("unknown discriminator $discriminator for class ${ChannelCloseOutgoingPayment.ChannelClosingType::class}")
312312
}
313313
)
314314
}

modules/core/src/commonMain/kotlin/fr/acinq/lightning/serialization/payment/v1/Serialization.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,11 @@ object Serialization {
354354
writeNullable(confirmedAt) { writeNumber(it) }
355355
writeNullable(lockedAt) { writeNumber(it) }
356356
when (closingType) {
357-
ChannelClosingType.Mutual -> write(0x00)
358-
ChannelClosingType.Local -> write(0x01)
359-
ChannelClosingType.Remote -> write(0x02)
360-
ChannelClosingType.Revoked -> write(0x03)
361-
ChannelClosingType.Other -> write(0x04)
357+
ChannelCloseOutgoingPayment.ChannelClosingType.Mutual -> write(0x00)
358+
ChannelCloseOutgoingPayment.ChannelClosingType.Local -> write(0x01)
359+
ChannelCloseOutgoingPayment.ChannelClosingType.Remote -> write(0x02)
360+
ChannelCloseOutgoingPayment.ChannelClosingType.Revoked -> write(0x03)
361+
ChannelCloseOutgoingPayment.ChannelClosingType.Other -> write(0x04)
362362
}
363363
}
364364
}

modules/core/src/commonTest/kotlin/fr/acinq/lightning/channel/TestsHelper.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import fr.acinq.lightning.blockchain.fee.FeeratePerKw
99
import fr.acinq.lightning.blockchain.fee.OnChainFeerates
1010
import fr.acinq.lightning.channel.states.*
1111
import fr.acinq.lightning.crypto.KeyManager
12-
import fr.acinq.lightning.db.ChannelClosingType
12+
import fr.acinq.lightning.db.ChannelCloseOutgoingPayment
13+
import fr.acinq.lightning.db.ChannelCloseOutgoingPayment.ChannelClosingType
1314
import fr.acinq.lightning.json.JsonSerializers
1415
import fr.acinq.lightning.logging.MDCLogger
1516
import fr.acinq.lightning.logging.mdc
@@ -303,7 +304,7 @@ object TestsHelper {
303304
actions1.has<ChannelAction.Storage.StoreState>()
304305
actions1.find<ChannelAction.Storage.StoreOutgoingPayment.ViaClose>().also {
305306
assertEquals(commitTx.txid, it.txId)
306-
assertEquals(ChannelClosingType.Local, it.closingType)
307+
assertEquals(ChannelCloseOutgoingPayment.ChannelClosingType.Local, it.closingType)
307308
}
308309

309310
val localCommitPublished = s1.state.localCommitPublished

modules/core/src/commonTest/kotlin/fr/acinq/lightning/channel/states/ClosingTestsCommon.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import fr.acinq.lightning.channel.TestsHelper.mutualCloseBob
2323
import fr.acinq.lightning.channel.TestsHelper.reachNormal
2424
import fr.acinq.lightning.channel.TestsHelper.remoteClose
2525
import fr.acinq.lightning.channel.TestsHelper.useAlternativeCommitSig
26-
import fr.acinq.lightning.db.ChannelClosingType
26+
import fr.acinq.lightning.db.ChannelCloseOutgoingPayment.ChannelClosingType
2727
import fr.acinq.lightning.tests.TestConstants
2828
import fr.acinq.lightning.tests.utils.LightningTestSuite
2929
import fr.acinq.lightning.transactions.Scripts

0 commit comments

Comments
 (0)