@@ -23,10 +23,19 @@ fun runCommandAndWait(cmd: String): String {
23
23
return stdout + stderr
24
24
}
25
25
26
- fun mine (blocks : UInt ) {
26
+ fun mine (blocks : UInt ): String {
27
27
val address = runCommandAndWait(" bitcoin-cli -regtest getnewaddress" )
28
28
val output = runCommandAndWait(" bitcoin-cli -regtest generatetoaddress $blocks $address " )
29
29
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)
30
39
}
31
40
32
41
fun sendToAddress (address : String , amountSats : UInt ): String {
@@ -39,6 +48,7 @@ fun setup() {
39
48
runCommandAndWait(" bitcoin-cli -regtest createwallet ldk_node_test" )
40
49
runCommandAndWait(" bitcoin-cli -regtest loadwallet ldk_node_test true" )
41
50
mine(101u )
51
+ Thread .sleep(5_000 )
42
52
}
43
53
44
54
fun waitForTx (esploraEndpoint : String , txid : String ) {
@@ -53,12 +63,30 @@ fun waitForTx(esploraEndpoint: String, txid: String) {
53
63
val response = client.send(request, HttpResponse .BodyHandlers .ofString());
54
64
55
65
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 )
57
84
}
58
85
}
59
86
60
87
class LibraryTest {
61
88
@Test fun fullCycle () {
89
+ val esploraEndpoint = " http://127.0.0.1:3002"
62
90
setup()
63
91
64
92
val network = Network .REGTEST
@@ -71,13 +99,14 @@ class LibraryTest {
71
99
val listenAddress1 = " 127.0.0.1:2323"
72
100
val listenAddress2 = " 127.0.0.1:2324"
73
101
74
- val esploraEndpoint = " http://127.0.0.1:3002"
75
102
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 )
78
105
79
106
val builder1 = Builder .fromConfig(config1)
107
+ builder1.setEsploraServer(esploraEndpoint)
80
108
val builder2 = Builder .fromConfig(config2)
109
+ builder2.setEsploraServer(esploraEndpoint)
81
110
82
111
val node1 = builder1.build()
83
112
val node2 = builder2.build()
@@ -99,7 +128,7 @@ class LibraryTest {
99
128
100
129
val txid1 = sendToAddress(address1, 100000u )
101
130
val txid2 = sendToAddress(address2, 100000u )
102
- mine( 6u )
131
+ mineAndWait(esploraEndpoint, 6u )
103
132
104
133
waitForTx(esploraEndpoint, txid1)
105
134
waitForTx(esploraEndpoint, txid2)
@@ -139,7 +168,7 @@ class LibraryTest {
139
168
140
169
waitForTx(esploraEndpoint, fundingTxid)
141
170
142
- mine( 6u )
171
+ mineAndWait(esploraEndpoint, 6u )
143
172
144
173
node1.syncWallets()
145
174
node2.syncWallets()
@@ -196,10 +225,7 @@ class LibraryTest {
196
225
assert (channelClosedEvent2 is Event .ChannelClosed )
197
226
node2.eventHandled()
198
227
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 )
203
229
204
230
node1.syncWallets()
205
231
node2.syncWallets()
0 commit comments