1
+ package fr.acinq.lightning.io
2
+
3
+ import fr.acinq.bitcoin.ByteVector
4
+ import fr.acinq.bitcoin.Chain
5
+ import fr.acinq.bitcoin.PublicKey
6
+ import fr.acinq.lightning.*
7
+ import fr.acinq.lightning.blockchain.mempool.MempoolSpaceClient
8
+ import fr.acinq.lightning.blockchain.mempool.MempoolSpaceWatcher
9
+ import fr.acinq.lightning.crypto.LocalKeyManager
10
+ import fr.acinq.lightning.db.InMemoryDatabases
11
+ import fr.acinq.lightning.tests.utils.testLoggerFactory
12
+ import fr.acinq.lightning.utils.Connection
13
+ import fr.acinq.lightning.utils.msat
14
+ import fr.acinq.lightning.utils.sat
15
+ import kotlinx.coroutines.delay
16
+ import kotlinx.coroutines.flow.dropWhile
17
+ import kotlinx.coroutines.launch
18
+ import kotlinx.coroutines.runBlocking
19
+ import kotlin.test.Test
20
+ import kotlin.time.Duration.Companion.minutes
21
+ import kotlin.time.Duration.Companion.seconds
22
+
23
+ class LeakTests {
24
+
25
+ @Test
26
+ fun `connection loop` (): Unit = runBlocking {
27
+ val scope = this
28
+ val loggerFactory = testLoggerFactory
29
+
30
+ val trampolineFees = listOf (
31
+ TrampolineFees (
32
+ feeBase = 4 .sat,
33
+ feeProportional = 4_000 ,
34
+ cltvExpiryDelta = CltvExpiryDelta (576 )
35
+ )
36
+ )
37
+
38
+ val invoiceDefaultRoutingFees = InvoiceDefaultRoutingFees (
39
+ feeBase = 1_000 .msat,
40
+ feeProportional = 100 ,
41
+ cltvExpiryDelta = CltvExpiryDelta (144 )
42
+ )
43
+
44
+ val swapInParams = SwapInParams (
45
+ minConfirmations = DefaultSwapInParams .MinConfirmations ,
46
+ maxConfirmations = DefaultSwapInParams .MaxConfirmations ,
47
+ refundDelay = DefaultSwapInParams .RefundDelay ,
48
+ )
49
+
50
+ val mempoolSpace = MempoolSpaceClient (MempoolSpaceClient .OfficialMempoolTestnet3 , loggerFactory)
51
+ val watcher = MempoolSpaceWatcher (mempoolSpace, scope, loggerFactory, pollingInterval = 10 .minutes)
52
+
53
+ val peer = Peer (
54
+ nodeParams = NodeParams (
55
+ chain = Chain .Testnet3 ,
56
+ loggerFactory = loggerFactory,
57
+ keyManager = LocalKeyManager (
58
+ seed = ByteVector (" 78f2b7cd614bb474e9196a189e45f98efbde20eb73e12d8ece16bdf8f0afefca66c64a9e6fb6c09ca3f94679cb6360022739e74d19bfc0fa338f9809cd1247d1" ),
59
+ chain = Chain .Testnet3 ,
60
+ remoteSwapInExtendedPublicKey = " tpubDAmCFB21J9ExKBRPDcVxSvGs9jtcf8U1wWWbS1xTYmnUsuUHPCoFdCnEGxLE3THSWcQE48GHJnyz8XPbYUivBMbLSMBifFd3G9KmafkM9og"
61
+ )
62
+ ),
63
+ walletParams = WalletParams (
64
+ trampolineNode = NodeUri (PublicKey .fromHex(" 03933884aaf1d6b108397e5efe5c86bcf2d8ca8d2f700eda99db9214fc2712b134" ), " 13.248.222.197" , 9735 ),
65
+ trampolineFees,
66
+ invoiceDefaultRoutingFees,
67
+ swapInParams
68
+ ),
69
+ client = mempoolSpace,
70
+ watcher = watcher,
71
+ db = InMemoryDatabases (),
72
+ socketBuilder = TcpSocket .Builder (),
73
+ scope = scope
74
+ )
75
+ scope.launch {
76
+ // drop initial CLOSED event
77
+ peer.connectionState.dropWhile { it is Connection .CLOSED }.collect {
78
+ when (it) {
79
+ Connection .ESTABLISHING -> println (" connecting to lightning peer..." )
80
+ Connection .ESTABLISHED -> println (" connected to lightning peer" )
81
+ is Connection .CLOSED -> println (" disconnected from lightning peer" )
82
+ }
83
+ }
84
+ }
85
+ scope.launch {
86
+ while (true ) {
87
+ peer.connect(connectTimeout = 10 .seconds, handshakeTimeout = 10 .seconds)
88
+ delay(1 .seconds)
89
+ peer.disconnect()
90
+ delay(1 .seconds)
91
+ }
92
+ }
93
+ }
94
+ }
0 commit comments