@@ -17,10 +17,7 @@ import fr.acinq.eclair.tests.utils.EclairTestSuite
1717import fr.acinq.eclair.tests.utils.runSuspendTest
1818import fr.acinq.eclair.utils.*
1919import fr.acinq.eclair.wire.*
20- import kotlin.test.Test
21- import kotlin.test.assertEquals
22- import kotlin.test.assertNull
23- import kotlin.test.assertTrue
20+ import kotlin.test.*
2421
2522class IncomingPaymentHandlerTestsCommon : EclairTestSuite () {
2623
@@ -314,6 +311,54 @@ class IncomingPaymentHandlerTestsCommon : EclairTestSuite() {
314311 assertEquals(setOf (expected), result.actions.toSet())
315312 }
316313
314+ @Test
315+ fun `process incoming amount with unknown origin` () = runSuspendTest {
316+ val channelId = randomBytes32()
317+ val amountOrigin = ChannelAction .Storage .StoreIncomingAmount (amount = 15_000_000 .msat, origin = null )
318+ val handler = IncomingPaymentHandler (TestConstants .Bob .nodeParams, TestConstants .Bob .walletParams, InMemoryPaymentsDb ())
319+ handler.process(channelId, amountOrigin)
320+ val dbPayment = handler.db.getIncomingPayment(channelId.sha256().sha256())
321+ assertNotNull(dbPayment)
322+ assertTrue { dbPayment.origin is IncomingPayment .Origin .SwapIn }
323+ assertEquals(IncomingPayment .ReceivedWith .NewChannel (fees = 0 .msat, channelId = channelId), dbPayment.received?.receivedWith)
324+ assertEquals(15_000_000 .msat, dbPayment.received?.amount)
325+ }
326+
327+ @Test
328+ fun `process incoming amount with pay-to-open origin` () = runSuspendTest {
329+ val preimage = randomBytes32()
330+ val channelId = randomBytes32()
331+ val amountOrigin = ChannelAction .Storage .StoreIncomingAmount (
332+ amount = 15_000_000 .msat,
333+ origin = ChannelOrigin .PayToOpenOrigin (paymentHash = preimage.sha256(), fee = 1000 .sat))
334+ val handler = IncomingPaymentHandler (TestConstants .Bob .nodeParams, TestConstants .Bob .walletParams, InMemoryPaymentsDb ())
335+ // simulate payment processed as a pay-to-open
336+ handler.db.addIncomingPayment(preimage, IncomingPayment .Origin .KeySend )
337+ handler.db.receivePayment(preimage.sha256(), 20_000_000 .msat, receivedWith = IncomingPayment .ReceivedWith .NewChannel (0 .msat, null ))
338+ // process the amount origin which must reconcile with the existing line in the database
339+ handler.process(channelId, amountOrigin)
340+ val dbPayment = handler.db.getIncomingPayment(preimage.sha256())
341+ assertNotNull(dbPayment)
342+ assertTrue { dbPayment.origin is IncomingPayment .Origin .KeySend }
343+ assertEquals(IncomingPayment .ReceivedWith .NewChannel (fees = 1_000_000 .msat, channelId = channelId), dbPayment.received?.receivedWith)
344+ assertEquals(20_000_000 .msat, dbPayment.received?.amount)
345+ }
346+
347+ @Test
348+ fun `process incoming amount with swap-in origin` () = runSuspendTest {
349+ val channelId = randomBytes32()
350+ val amountOrigin = ChannelAction .Storage .StoreIncomingAmount (
351+ amount = 33_000_000 .msat,
352+ origin = ChannelOrigin .SwapInOrigin (" tb1q97tpc0y4rvdnu9wm7nu354lmmzdm8du228u3g4" , 1_200 .sat))
353+ val handler = IncomingPaymentHandler (TestConstants .Bob .nodeParams, TestConstants .Bob .walletParams, InMemoryPaymentsDb ())
354+ handler.process(channelId, amountOrigin)
355+ val dbPayment = handler.db.getIncomingPayment(channelId.sha256().sha256())
356+ assertNotNull(dbPayment)
357+ assertTrue { dbPayment.origin is IncomingPayment .Origin .SwapIn }
358+ assertEquals(IncomingPayment .ReceivedWith .NewChannel (fees = 1_200_000 .msat, channelId = channelId), dbPayment.received?.receivedWith)
359+ assertEquals(33_000_000 .msat, dbPayment.received?.amount)
360+ }
361+
317362 @Test
318363 fun `receive multipart payment with multiple HTLCs via same channel` () = runSuspendTest {
319364 val channelId = randomBytes32()
0 commit comments