@@ -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,16 +63,32 @@ 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
- val network = Network .REGTEST
65
-
66
92
val tmpDir1 = createTempDirectory(" ldk_node" ).toString()
67
93
println (" Random dir 1: $tmpDir1 " )
68
94
val tmpDir2 = createTempDirectory(" ldk_node" ).toString()
@@ -71,13 +97,27 @@ class LibraryTest {
71
97
val listenAddress1 = " 127.0.0.1:2323"
72
98
val listenAddress2 = " 127.0.0.1:2324"
73
99
74
- val esploraEndpoint = " http://127.0.0.1:3002"
100
+ val logLevel = LogLevel .TRACE ;
101
+
102
+ val config1 = Config ()
103
+ config1.storageDirPath = tmpDir1
104
+ config1.listeningAddress = listenAddress1
105
+ config1.network = Network .REGTEST
106
+ config1.logLevel = LogLevel .TRACE
75
107
76
- val config1 = Config (tmpDir1, esploraEndpoint, network, listenAddress1, 2048u )
77
- val config2 = Config (tmpDir2, esploraEndpoint, network, listenAddress2, 2048u )
108
+ println (" Config 1: $config1 " )
109
+
110
+ val config2 = Config ()
111
+ config2.storageDirPath = tmpDir2
112
+ config2.listeningAddress = listenAddress2
113
+ config2.network = Network .REGTEST
114
+ config2.logLevel = LogLevel .TRACE
115
+ println (" Config 2: $config2 " )
78
116
79
117
val builder1 = Builder .fromConfig(config1)
118
+ builder1.setEsploraServer(esploraEndpoint)
80
119
val builder2 = Builder .fromConfig(config2)
120
+ builder2.setEsploraServer(esploraEndpoint)
81
121
82
122
val node1 = builder1.build()
83
123
val node2 = builder2.build()
@@ -99,7 +139,7 @@ class LibraryTest {
99
139
100
140
val txid1 = sendToAddress(address1, 100000u )
101
141
val txid2 = sendToAddress(address2, 100000u )
102
- mine( 6u )
142
+ mineAndWait(esploraEndpoint, 6u )
103
143
104
144
waitForTx(esploraEndpoint, txid1)
105
145
waitForTx(esploraEndpoint, txid2)
@@ -139,7 +179,7 @@ class LibraryTest {
139
179
140
180
waitForTx(esploraEndpoint, fundingTxid)
141
181
142
- mine( 6u )
182
+ mineAndWait(esploraEndpoint, 6u )
143
183
144
184
node1.syncWallets()
145
185
node2.syncWallets()
@@ -196,10 +236,7 @@ class LibraryTest {
196
236
assert (channelClosedEvent2 is Event .ChannelClosed )
197
237
node2.eventHandled()
198
238
199
- mine(1u )
200
-
201
- // Sleep a bit to allow for the block to propagate to esplora
202
- Thread .sleep(5_000 )
239
+ mineAndWait(esploraEndpoint, 1u )
203
240
204
241
node1.syncWallets()
205
242
node2.syncWallets()
0 commit comments