Skip to content

Commit 4d9ebe2

Browse files
committed
feat: Add kyoto example and logging
1 parent 7507fe7 commit 4d9ebe2

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

examples/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ repositories {
1515
dependencies {
1616
implementation(project(":lib"))
1717
testImplementation(kotlin("test"))
18+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
19+
implementation("org.slf4j:slf4j-api:2.0.16")
20+
implementation("ch.qos.logback:logback-classic:1.5.13")
1821
}
1922

2023
tasks.test {
@@ -39,11 +42,21 @@ tasks.register<JavaExec>("WalletSetupBip32") {
3942
description = "Runs the main function in the WalletSetupBip32 example"
4043
mainClass.set("org.bitcoindevkit.WalletSetupBip32Kt")
4144
classpath = sourceSets["main"].runtimeClasspath
45+
workingDir = project.rootDir
4246
}
4347

4448
tasks.register<JavaExec>("MultisigTransaction") {
4549
group = "application"
4650
description = "Runs the main function in the MultisigTransaction example"
4751
mainClass.set("org.bitcoindevkit.MultisigTransactionKt")
4852
classpath = sourceSets["main"].runtimeClasspath
53+
workingDir = project.rootDir
54+
}
55+
56+
tasks.register<JavaExec>("Kyoto") {
57+
group = "application"
58+
description = "Runs the main function in the Kyoto example"
59+
mainClass.set("org.bitcoindevkit.KyotoKt")
60+
classpath = sourceSets["main"].runtimeClasspath
61+
workingDir = project.rootDir
4962
}

examples/src/main/kotlin/Kyoto.kt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.bitcoindevkit
2+
3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.cancelAndJoin
5+
import kotlinx.coroutines.launch
6+
import kotlinx.coroutines.runBlocking
7+
import java.nio.file.Files
8+
import java.nio.file.Paths
9+
import org.slf4j.Logger
10+
import org.slf4j.LoggerFactory
11+
12+
13+
14+
fun main() {
15+
// Regtest environment must have Compact block filter enabled to run this
16+
// Setup your environment according to the https://github.com/thunderbiscuit/podman-regtest-infinity-pro documentation.
17+
18+
// Add your regtest IP address
19+
val ip: IpAddress = IpAddress.fromIpv4(127u, 0u, 0u, 1u)
20+
val peer: Peer = Peer(ip, 18444u, false)
21+
val peers: List<Peer> = listOf(peer)
22+
val currentPath = Paths.get("examples/data").toAbsolutePath().normalize()
23+
println("Current path: $currentPath")
24+
val persistenceFilePath = Files.createTempDirectory(currentPath, "kyoto-data_")
25+
26+
val wallet = getNewWallet(ActiveWalletScriptType.P2WPKH, Network.REGTEST)
27+
val address = wallet.revealNextAddress(KeychainKind.EXTERNAL)
28+
29+
// Fund this address. Send coins from your regtest to this address
30+
println("Receiving address. Send funds to this address: ${address.address}")
31+
32+
// Wait 70 seconds for funds to arrive before syncing
33+
println("Waiting 70 seconds for funds to arrive here ${address.address} before kyoto (compact block filter syncing) ...")
34+
Thread.sleep(10000)
35+
36+
// Create CBF node and client
37+
runBlocking {
38+
val lightClient = CbfBuilder()
39+
.peers(peers)
40+
.connections(1u)
41+
.scanType(ScanType.New)
42+
.dataDir(persistenceFilePath.toString())
43+
.build(wallet)
44+
val client = lightClient.client
45+
val node = lightClient.node
46+
47+
val logJob = launchLogCollector(this, client::nextLog, "LOG")
48+
val logWarningJob = launchLogCollector(this, client::nextWarning, "WARNING")
49+
val logInfoJob = launchLogCollector(this, client::nextInfo, "INFO")
50+
51+
//Start CBF node
52+
node.run()
53+
println("Node running")
54+
55+
//Update wallet
56+
val update: Update = client.update()
57+
wallet.applyUpdate(update)
58+
println("Wallet balance: ${wallet.balance().total.toSat()} sats")
59+
60+
if(wallet.balance().total.toSat() > 0uL) {
61+
println("Wallet is synced and ready for use!")
62+
println("Test completed successfully")
63+
}else{
64+
println("Wallet balance is 0. Try sending funds to ${address.address} and try again.")
65+
}
66+
logJob.cancelAndJoin()
67+
logWarningJob.cancelAndJoin()
68+
logInfoJob.cancelAndJoin()
69+
client.shutdown()
70+
}
71+
}
72+
73+
fun <T> launchLogCollector(scope: CoroutineScope, collector: suspend () -> T, logType: String) = scope.launch {
74+
val logger: Logger = LoggerFactory.getLogger("LogCollector")
75+
76+
while (true) {
77+
val log = collector()
78+
when (logType) {
79+
"LOG" -> logger.info("$log")
80+
"WARNING" -> logger.warn("$log")
81+
"INFO" -> logger.info("$log")
82+
else -> logger.debug("[$logType]: $log")
83+
}
84+
}
85+
}
86+

0 commit comments

Comments
 (0)