1- /*
2- * This Kotlin source file was generated by the Gradle 'init' task.
3- */
41package org.lightningdevkit.ldknode
52
6- import kotlin.UInt
7- import kotlin.test.Test
8- import kotlin.test.assertEquals
9- import kotlin.io.path.createTempDirectory
3+ import org.junit.jupiter.api.BeforeAll
4+ import org.junit.jupiter.api.Test
5+ import org.junit.jupiter.api.TestInstance
106import java.net.URI
117import java.net.http.HttpClient
128import java.net.http.HttpRequest
139import java.net.http.HttpResponse
10+ import kotlin.io.path.createTempDirectory
11+ import kotlin.test.assertEquals
1412
1513fun runCommandAndWait (vararg cmd : String ): String {
1614 println (" Running command \" ${cmd.joinToString(" " )} \" " )
@@ -24,12 +22,29 @@ fun runCommandAndWait(vararg cmd: String): String {
2422 return stdout + stderr
2523}
2624
25+ fun bitcoinCli (vararg cmd : String ): String {
26+ val bitcoinCliBin = System .getenv(" BITCOIN_CLI_BIN" )?.split(" " ) ? : listOf (" bitcoin-cli" )
27+ val bitcoinDRpcUser = System .getenv(" BITCOIND_RPC_USER" ) ? : " "
28+ val bitcoinDRpcPassword = System .getenv(" BITCOIND_RPC_PASSWORD" ) ? : " "
29+
30+ val baseCommand = bitcoinCliBin + " -regtest"
31+
32+ val rpcAuth = if (bitcoinDRpcUser.isNotBlank() && bitcoinDRpcPassword.isNotBlank()) {
33+ listOf (" -rpcuser=$bitcoinDRpcUser " , " -rpcpassword=$bitcoinDRpcPassword " )
34+ } else {
35+ emptyList()
36+ }
37+
38+ val fullCommand = baseCommand + rpcAuth + cmd.toList()
39+ return runCommandAndWait(* fullCommand.toTypedArray())
40+ }
41+
2742fun mine (blocks : UInt ): String {
28- val address = runCommandAndWait( " bitcoin-cli " , " -regtest " , " getnewaddress" )
29- val output = runCommandAndWait( " bitcoin-cli " , " -regtest " , " generatetoaddress" , blocks.toString(), address)
43+ val address = bitcoinCli( " getnewaddress" )
44+ val output = bitcoinCli( " generatetoaddress" , blocks.toString(), address)
3045 println (" Mining output: $output " )
3146 val re = Regex (" \n .+\n\\ ]$" )
32- val lastBlock = re.find(output)!! .value.replace(" ]" ," " ).replace(" \" " , " " ).replace(" \n " ," " ).trim()
47+ val lastBlock = re.find(output)!! .value.replace(" ]" , " " ).replace(" \" " , " " ).replace(" \n " , " " ).trim()
3348 println (" Last block: $lastBlock " )
3449 return lastBlock
3550}
@@ -41,54 +56,56 @@ fun mineAndWait(esploraEndpoint: String, blocks: UInt) {
4156
4257fun sendToAddress (address : String , amountSats : UInt ): String {
4358 val amountBtc = amountSats.toDouble() / 100000000.0
44- val output = runCommandAndWait( " bitcoin-cli " , " -regtest " , " sendtoaddress" , address, amountBtc.toString())
59+ val output = bitcoinCli( " sendtoaddress" , address, amountBtc.toString())
4560 return output
4661}
4762
48- fun setup () {
49- runCommandAndWait(" bitcoin-cli" , " -regtest" , " createwallet" , " ldk_node_test" )
50- runCommandAndWait(" bitcoin-cli" , " -regtest" , " loadwallet" , " ldk_node_test" , " true" )
51- mine(101u )
52- Thread .sleep(5_000 )
53- }
54-
5563fun waitForTx (esploraEndpoint : String , txid : String ) {
5664 var esploraPickedUpTx = false
57- val re = Regex (" \" txid\" :\" $txid \" " );
65+ val re = Regex (" \" txid\" :\" $txid \" " )
5866 while (! esploraPickedUpTx) {
5967 val client = HttpClient .newBuilder().build()
6068 val request = HttpRequest .newBuilder()
6169 .uri(URI .create(esploraEndpoint + " /tx/" + txid))
62- .build();
70+ .build()
6371
64- val response = client.send(request, HttpResponse .BodyHandlers .ofString());
72+ val response = client.send(request, HttpResponse .BodyHandlers .ofString())
6573
66- esploraPickedUpTx = re.containsMatchIn(response.body());
74+ esploraPickedUpTx = re.containsMatchIn(response.body())
6775 Thread .sleep(500 )
6876 }
6977}
7078
7179fun waitForBlock (esploraEndpoint : String , blockHash : String ) {
7280 var esploraPickedUpBlock = false
73- val re = Regex (" \" in_best_chain\" :true" );
81+ val re = Regex (" \" in_best_chain\" :true" )
7482 while (! esploraPickedUpBlock) {
7583 val client = HttpClient .newBuilder().build()
7684 val request = HttpRequest .newBuilder()
7785 .uri(URI .create(esploraEndpoint + " /block/" + blockHash + " /status" ))
78- .build();
86+ .build()
7987
80- val response = client.send(request, HttpResponse .BodyHandlers .ofString());
88+ val response = client.send(request, HttpResponse .BodyHandlers .ofString())
8189
82- esploraPickedUpBlock = re.containsMatchIn(response.body());
90+ esploraPickedUpBlock = re.containsMatchIn(response.body())
8391 Thread .sleep(500 )
8492 }
8593}
8694
95+ @TestInstance(TestInstance .Lifecycle .PER_CLASS )
8796class LibraryTest {
88- @Test fun fullCycle () {
89- val esploraEndpoint = " http://127.0.0.1:3002"
90- setup()
9197
98+ val esploraEndpoint = System .getenv(" ESPLORA_ENDPOINT" )
99+
100+ @BeforeAll
101+ fun setup () {
102+ bitcoinCli(" createwallet" , " ldk_node_test" )
103+ bitcoinCli(" loadwallet" , " ldk_node_test" , " true" )
104+ mine(101u )
105+ Thread .sleep(5_000 )
106+ }
107+
108+ @Test fun fullCycle () {
92109 val tmpDir1 = createTempDirectory(" ldk_node" ).toString()
93110 println (" Random dir 1: $tmpDir1 " )
94111 val tmpDir2 = createTempDirectory(" ldk_node" ).toString()
@@ -172,7 +189,7 @@ class LibraryTest {
172189
173190 val fundingTxid = when (channelPendingEvent1) {
174191 is Event .ChannelPending -> channelPendingEvent1.fundingTxo.txid
175- else -> return
192+ else -> return
176193 }
177194
178195 waitForTx(esploraEndpoint, fundingTxid)
@@ -202,7 +219,7 @@ class LibraryTest {
202219
203220 val channelId = when (channelReadyEvent2) {
204221 is Event .ChannelReady -> channelReadyEvent2.channelId
205- else -> return
222+ else -> return
206223 }
207224
208225 val invoice = node2.receivePayment(2500000u , " asdf" , 9217u )
0 commit comments