@@ -7,6 +7,11 @@ import kotlin.UInt
7
7
import kotlin.test.Test
8
8
import kotlin.test.assertEquals
9
9
import kotlin.io.path.createTempDirectory
10
+ import java.net.URI
11
+ import java.net.http.HttpClient
12
+ import java.net.http.HttpRequest
13
+ import java.net.http.HttpResponse
14
+
10
15
import org.lightningdevkit.ldknode.*;
11
16
12
17
fun runCommandAndWait (cmd : String ): String {
@@ -24,10 +29,10 @@ fun mine(blocks: UInt) {
24
29
println (" Mining output: $output " )
25
30
}
26
31
27
- fun sendToAddress (address : String , amountSats : UInt ) {
32
+ fun sendToAddress (address : String , amountSats : UInt ): String {
28
33
val amountBtc = amountSats.toDouble() / 100000000.0
29
34
val output = runCommandAndWait(" bitcoin-cli -regtest sendtoaddress $address $amountBtc " )
30
- println ( " Sending output: $output " )
35
+ return output
31
36
}
32
37
33
38
fun setup () {
@@ -36,6 +41,22 @@ fun setup() {
36
41
mine(101u )
37
42
}
38
43
44
+ fun waitForTx (esploraEndpoint : String , txid : String ) {
45
+ var esploraPickedUpTx = false
46
+ val re = Regex (" \" txid\" :\" $txid \" " );
47
+ while (! esploraPickedUpTx) {
48
+ val client = HttpClient .newBuilder().build()
49
+ val request = HttpRequest .newBuilder()
50
+ .uri(URI .create(esploraEndpoint + " /tx/" + txid))
51
+ .build();
52
+
53
+ val response = client.send(request, HttpResponse .BodyHandlers .ofString());
54
+
55
+ esploraPickedUpTx = re.containsMatchIn(response.body());
56
+ Thread .sleep(1_000 )
57
+ }
58
+ }
59
+
39
60
class LibraryTest {
40
61
@Test fun fullCycle () {
41
62
setup()
@@ -51,8 +72,10 @@ class LibraryTest {
51
72
val listenAddress1 = " 127.0.0.1:2323"
52
73
val listenAddress2 = " 127.0.0.1:2324"
53
74
54
- val config1 = Config (tmpDir1, " http://127.0.0.1:3002" , network, listenAddress1, 2048u )
55
- val config2 = Config (tmpDir2, " http://127.0.0.1:3002" , network, listenAddress2, 2048u )
75
+ val esploraEndpoint = " http://127.0.0.1:3002"
76
+
77
+ val config1 = Config (tmpDir1, esploraEndpoint, network, listenAddress1, 2048u )
78
+ val config2 = Config (tmpDir2, esploraEndpoint, network, listenAddress2, 2048u )
56
79
57
80
val builder1 = Builder .fromConfig(config1)
58
81
val builder2 = Builder .fromConfig(config2)
@@ -75,10 +98,12 @@ class LibraryTest {
75
98
val address2 = node2.newFundingAddress()
76
99
println (" Funding address 2: $address2 " )
77
100
78
- sendToAddress(address1, 100000u )
79
- sendToAddress(address2, 100000u )
101
+ val txid1 = sendToAddress(address1, 100000u )
102
+ val txid2 = sendToAddress(address2, 100000u )
80
103
mine(6u )
81
- Thread .sleep(5_000 )
104
+
105
+ waitForTx(esploraEndpoint, txid1)
106
+ waitForTx(esploraEndpoint, txid2)
82
107
83
108
node1.syncWallets()
84
109
node2.syncWallets()
@@ -96,11 +121,24 @@ class LibraryTest {
96
121
assertEquals(100000u , totalBalance1)
97
122
assertEquals(100000u , totalBalance2)
98
123
99
- val idAndAddress2 = nodeId2 + " @" + listenAddress2
100
- node1.connectOpenChannel(idAndAddress2, 50000u , true )
124
+ node1.connectOpenChannel(nodeId2, listenAddress2, 50000u , null , true )
101
125
102
- // Sleep a bit to allow for propagation
103
- Thread .sleep(3_000 )
126
+ val channelPendingEvent1 = node1.nextEvent()
127
+ println (" Got event: $channelPendingEvent1 " )
128
+ assert (channelPendingEvent1 is Event .ChannelPending )
129
+ node1.eventHandled()
130
+
131
+ val channelPendingEvent2 = node2.nextEvent()
132
+ println (" Got event: $channelPendingEvent2 " )
133
+ assert (channelPendingEvent2 is Event .ChannelPending )
134
+ node2.eventHandled()
135
+
136
+ val fundingTxid = when (channelPendingEvent1) {
137
+ is Event .ChannelPending -> channelPendingEvent1.fundingTxo.txid
138
+ else -> return
139
+ }
140
+
141
+ waitForTx(esploraEndpoint, fundingTxid)
104
142
105
143
mine(6u )
106
144
@@ -146,9 +184,6 @@ class LibraryTest {
146
184
147
185
node2.closeChannel(channelId, nodeId1)
148
186
149
- // Sleep a bit to allow for propagation
150
- Thread .sleep(3_000 )
151
-
152
187
val channelClosedEvent1 = node1.nextEvent()
153
188
println (" Got event: $channelClosedEvent1 " )
154
189
assert (channelClosedEvent1 is Event .ChannelClosed )
@@ -161,7 +196,7 @@ class LibraryTest {
161
196
162
197
mine(1u )
163
198
164
- // Sleep a bit to allow for propagation
199
+ // Sleep a bit to allow for the block to propagate to esplora
165
200
Thread .sleep(3_000 )
166
201
167
202
node1.syncWallets()
0 commit comments