@@ -23,10 +23,19 @@ fun runCommandAndWait(cmd: String): String {
2323 return stdout + stderr
2424}
2525
26- fun mine (blocks : UInt ) {
26+ fun mine (blocks : UInt ): String {
2727 val address = runCommandAndWait(" bitcoin-cli -regtest getnewaddress" )
2828 val output = runCommandAndWait(" bitcoin-cli -regtest generatetoaddress $blocks $address " )
2929 println (" Mining output: $output " )
30+ val re = Regex (" \n .+\n\\ ]$" )
31+ val lastBlock = re.find(output)!! .value.replace(" ]" ," " ).replace(" \" " , " " ).replace(" \n " ," " ).trim()
32+ println (" Last block: $lastBlock " )
33+ return lastBlock
34+ }
35+
36+ fun mineAndWait (esploraEndpoint : String , blocks : UInt ) {
37+ val lastBlockHash = mine(blocks)
38+ waitForBlock(esploraEndpoint, lastBlockHash)
3039}
3140
3241fun sendToAddress (address : String , amountSats : UInt ): String {
@@ -39,6 +48,7 @@ fun setup() {
3948 runCommandAndWait(" bitcoin-cli -regtest createwallet ldk_node_test" )
4049 runCommandAndWait(" bitcoin-cli -regtest loadwallet ldk_node_test true" )
4150 mine(101u )
51+ Thread .sleep(5_000 )
4252}
4353
4454fun waitForTx (esploraEndpoint : String , txid : String ) {
@@ -53,12 +63,30 @@ fun waitForTx(esploraEndpoint: String, txid: String) {
5363 val response = client.send(request, HttpResponse .BodyHandlers .ofString());
5464
5565 esploraPickedUpTx = re.containsMatchIn(response.body());
56- Thread .sleep(1_000 )
66+ Thread .sleep(500 )
67+ }
68+ }
69+
70+ fun waitForBlock (esploraEndpoint : String , blockHash : String ) {
71+ var esploraPickedUpBlock = false
72+ val re = Regex (" \" in_best_chain\" :true" );
73+ while (! esploraPickedUpBlock) {
74+ val client = HttpClient .newBuilder().build()
75+ val request = HttpRequest .newBuilder()
76+ .uri(URI .create(esploraEndpoint + " /block/" + blockHash + " /status" ))
77+ .build();
78+
79+ val response = client.send(request, HttpResponse .BodyHandlers .ofString());
80+ val body = response.body()
81+
82+ esploraPickedUpBlock = re.containsMatchIn(response.body());
83+ Thread .sleep(500 )
5784 }
5885}
5986
6087class LibraryTest {
6188 @Test fun fullCycle () {
89+ val esploraEndpoint = " http://127.0.0.1:3002"
6290 setup()
6391
6492 val network = Network .REGTEST
@@ -71,13 +99,14 @@ class LibraryTest {
7199 val listenAddress1 = " 127.0.0.1:2323"
72100 val listenAddress2 = " 127.0.0.1:2324"
73101
74- val esploraEndpoint = " http://127.0.0.1:3002"
75102
76- val config1 = Config (tmpDir1, esploraEndpoint, network, listenAddress1, 2048u )
77- val config2 = Config (tmpDir2, esploraEndpoint, network, listenAddress2, 2048u )
103+ val config1 = Config (tmpDir1, network, listenAddress1, 2048u )
104+ val config2 = Config (tmpDir2, network, listenAddress2, 2048u )
78105
79106 val builder1 = Builder .fromConfig(config1)
107+ builder1.setEsploraServer(esploraEndpoint)
80108 val builder2 = Builder .fromConfig(config2)
109+ builder2.setEsploraServer(esploraEndpoint)
81110
82111 val node1 = builder1.build()
83112 val node2 = builder2.build()
@@ -99,7 +128,7 @@ class LibraryTest {
99128
100129 val txid1 = sendToAddress(address1, 100000u )
101130 val txid2 = sendToAddress(address2, 100000u )
102- mine( 6u )
131+ mineAndWait(esploraEndpoint, 6u )
103132
104133 waitForTx(esploraEndpoint, txid1)
105134 waitForTx(esploraEndpoint, txid2)
@@ -139,7 +168,7 @@ class LibraryTest {
139168
140169 waitForTx(esploraEndpoint, fundingTxid)
141170
142- mine( 6u )
171+ mineAndWait(esploraEndpoint, 6u )
143172
144173 node1.syncWallets()
145174 node2.syncWallets()
@@ -196,10 +225,7 @@ class LibraryTest {
196225 assert (channelClosedEvent2 is Event .ChannelClosed )
197226 node2.eventHandled()
198227
199- mine(1u )
200-
201- // Sleep a bit to allow for the block to propagate to esplora
202- Thread .sleep(5_000 )
228+ mineAndWait(esploraEndpoint, 1u )
203229
204230 node1.syncWallets()
205231 node2.syncWallets()
0 commit comments