@@ -7,6 +7,11 @@ import kotlin.UInt
77import kotlin.test.Test
88import kotlin.test.assertEquals
99import 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+
1015import org.lightningdevkit.ldknode.*;
1116
1217fun runCommandAndWait (cmd : String ): String {
@@ -24,10 +29,10 @@ fun mine(blocks: UInt) {
2429 println (" Mining output: $output " )
2530}
2631
27- fun sendToAddress (address : String , amountSats : UInt ) {
32+ fun sendToAddress (address : String , amountSats : UInt ): String {
2833 val amountBtc = amountSats.toDouble() / 100000000.0
2934 val output = runCommandAndWait(" bitcoin-cli -regtest sendtoaddress $address $amountBtc " )
30- println ( " Sending output: $output " )
35+ return output
3136}
3237
3338fun setup () {
@@ -36,6 +41,22 @@ fun setup() {
3641 mine(101u )
3742}
3843
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+
3960class LibraryTest {
4061 @Test fun fullCycle () {
4162 setup()
@@ -51,8 +72,10 @@ class LibraryTest {
5172 val listenAddress1 = " 127.0.0.1:2323"
5273 val listenAddress2 = " 127.0.0.1:2324"
5374
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 )
5679
5780 val builder1 = Builder .fromConfig(config1)
5881 val builder2 = Builder .fromConfig(config2)
@@ -75,10 +98,12 @@ class LibraryTest {
7598 val address2 = node2.newFundingAddress()
7699 println (" Funding address 2: $address2 " )
77100
78- sendToAddress(address1, 100000u )
79- sendToAddress(address2, 100000u )
101+ val txid1 = sendToAddress(address1, 100000u )
102+ val txid2 = sendToAddress(address2, 100000u )
80103 mine(6u )
81- Thread .sleep(5_000 )
104+
105+ waitForTx(esploraEndpoint, txid1)
106+ waitForTx(esploraEndpoint, txid2)
82107
83108 node1.syncWallets()
84109 node2.syncWallets()
@@ -96,11 +121,24 @@ class LibraryTest {
96121 assertEquals(100000u , totalBalance1)
97122 assertEquals(100000u , totalBalance2)
98123
99- val idAndAddress2 = nodeId2 + " @" + listenAddress2
100- node1.connectOpenChannel(idAndAddress2, 50000u , true )
124+ node1.connectOpenChannel(nodeId2, listenAddress2, 50000u , null , true )
101125
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)
104142
105143 mine(6u )
106144
@@ -146,9 +184,6 @@ class LibraryTest {
146184
147185 node2.closeChannel(channelId, nodeId1)
148186
149- // Sleep a bit to allow for propagation
150- Thread .sleep(3_000 )
151-
152187 val channelClosedEvent1 = node1.nextEvent()
153188 println (" Got event: $channelClosedEvent1 " )
154189 assert (channelClosedEvent1 is Event .ChannelClosed )
@@ -161,7 +196,7 @@ class LibraryTest {
161196
162197 mine(1u )
163198
164- // Sleep a bit to allow for propagation
199+ // Sleep a bit to allow for the block to propagate to esplora
165200 Thread .sleep(3_000 )
166201
167202 node1.syncWallets()
0 commit comments