|
| 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(".").toAbsolutePath().normalize() |
| 23 | + val persistenceFilePath = Files.createTempDirectory(currentPath, "tempDirPrefix_") |
| 24 | + |
| 25 | + val wallet = getNewWallet(ActiveWalletScriptType.P2WPKH, Network.REGTEST) |
| 26 | + val address = wallet.revealNextAddress(KeychainKind.EXTERNAL) |
| 27 | + |
| 28 | + // Fund this address. Send coins from your regtest to this address |
| 29 | + println("Receiving address. Send funds to this address: ${address.address}") |
| 30 | + |
| 31 | + // Wait 70 seconds for funds to arrive before syncing |
| 32 | + println("Waiting 70 seconds for funds to arrive here ${address.address} before kyoto (compact block filter syncing) ...") |
| 33 | + Thread.sleep(10000) |
| 34 | + |
| 35 | + // Create CBF node and client |
| 36 | + runBlocking { |
| 37 | + val lightClient = CbfBuilder() |
| 38 | + .peers(peers) |
| 39 | + .connections(1u) |
| 40 | + .scanType(ScanType.New) |
| 41 | + .dataDir(persistenceFilePath.toString()) |
| 42 | + .build(wallet) |
| 43 | + val client = lightClient.client |
| 44 | + val node = lightClient.node |
| 45 | + |
| 46 | + val logJob = launchLogCollector(this, client::nextLog, "LOG") |
| 47 | + val logWarningJob = launchLogCollector(this, client::nextWarning, "WARNING") |
| 48 | + val logInfoJob = launchLogCollector(this, client::nextInfo, "INFO") |
| 49 | + |
| 50 | + //Start CBF node |
| 51 | + node.run() |
| 52 | + println("Node running") |
| 53 | + |
| 54 | + //Update wallet |
| 55 | + val update: Update = client.update() |
| 56 | + wallet.applyUpdate(update) |
| 57 | + println("Wallet balance: ${wallet.balance().total.toSat()} sats") |
| 58 | + |
| 59 | + if(wallet.balance().total.toSat() > 0uL) { |
| 60 | + println("Wallet is synced and ready for use!") |
| 61 | + println("Test completed successfully") |
| 62 | + }else{ |
| 63 | + println("Wallet balance is 0. Try sending funds to ${address.address} and try again.") |
| 64 | + } |
| 65 | + logJob.cancelAndJoin() |
| 66 | + logWarningJob.cancelAndJoin() |
| 67 | + logInfoJob.cancelAndJoin() |
| 68 | + client.shutdown() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fun <T> launchLogCollector(scope: CoroutineScope, collector: suspend () -> T, logType: String) = scope.launch { |
| 73 | + val logger: Logger = LoggerFactory.getLogger("LogCollector") |
| 74 | + |
| 75 | + while (true) { |
| 76 | + val log = collector() |
| 77 | + when (logType) { |
| 78 | + "LOG" -> logger.info("$log") |
| 79 | + "WARNING" -> logger.warn("$log") |
| 80 | + "INFO" -> logger.info("$log") |
| 81 | + else -> logger.debug("[$logType]: $log") |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
0 commit comments