From f872d41d30435ec316ebd8766b39e95ec6576184 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Tue, 12 Sep 2023 15:38:12 +0200 Subject: [PATCH] Regtest: Bypass recursive Gradle invocations --- .../gradle/regtest_plugin/RegtestPlugin.kt | 129 +++++++++++------- .../gradle/regtest_plugin/StartBisqTask.kt | 48 +++++-- 2 files changed, 113 insertions(+), 64 deletions(-) diff --git a/build-logic/regtest/src/main/kotlin/bisq/gradle/regtest_plugin/RegtestPlugin.kt b/build-logic/regtest/src/main/kotlin/bisq/gradle/regtest_plugin/RegtestPlugin.kt index ca5cf7a6fac..8430065a7e0 100644 --- a/build-logic/regtest/src/main/kotlin/bisq/gradle/regtest_plugin/RegtestPlugin.kt +++ b/build-logic/regtest/src/main/kotlin/bisq/gradle/regtest_plugin/RegtestPlugin.kt @@ -2,9 +2,16 @@ package bisq.gradle.regtest_plugin import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.file.RegularFile +import org.gradle.api.provider.Provider +import org.gradle.jvm.toolchain.JavaLanguageVersion +import org.gradle.jvm.toolchain.JavaToolchainService +import org.gradle.jvm.toolchain.JvmImplementation +import org.gradle.jvm.toolchain.JvmVendorSpec import org.gradle.kotlin.dsl.register +import javax.inject.Inject -class RegtestPlugin : Plugin { +class RegtestPlugin @Inject constructor(private val javaToolchainService: JavaToolchainService) : Plugin { companion object { const val RPC_USER = "bisqdao" @@ -28,15 +35,17 @@ class RegtestPlugin : Plugin { val startFirstSeedNodeTask = project.tasks.register("startRegtestFirstSeednode") { dependsOn(startBitcoindTask) - pidFile.set(project.layout.projectDirectory.file(".localnet/seednode_1.pid")) - startScriptFile.set(project.layout.projectDirectory.file("bisq-seednode")) + workingDirectory.set(project.layout.projectDirectory) + javaExecutable.set(getJavaExecutable()) + libsDir.set(project.layout.projectDirectory.dir("seednode/build/app/lib")) + mainClass.set("bisq.seednode.SeedNodeMain") arguments.set( - createSeedNodeArgs(5120, 2002, "seednode") + createSeedNodeArgs(5120, 2002, "seednode") ) - workingDirectory.set(project.layout.projectDirectory) logFile.set(project.layout.projectDirectory.file(".localnet/seednode_1_shell.log")) + pidFile.set(project.layout.projectDirectory.file(".localnet/seednode_1.pid")) } val stopFirstSeedNodeTask = project.tasks.register("stopRegtestFirstSeednode") { @@ -47,15 +56,17 @@ class RegtestPlugin : Plugin { dependsOn(startBitcoindTask) dependsOn(startFirstSeedNodeTask) - pidFile.set(project.layout.projectDirectory.file(".localnet/seednode_2.pid")) - startScriptFile.set(project.layout.projectDirectory.file("bisq-seednode")) + workingDirectory.set(project.layout.projectDirectory) + javaExecutable.set(getJavaExecutable()) + libsDir.set(project.layout.projectDirectory.dir("seednode/build/app/lib")) + mainClass.set("bisq.seednode.SeedNodeMain") arguments.set( - createSeedNodeArgs(5121, 3002, "seednode2") + createSeedNodeArgs(5121, 3002, "seednode2") ) - workingDirectory.set(project.layout.projectDirectory) logFile.set(project.layout.projectDirectory.file(".localnet/seednode_2_shell.log")) + pidFile.set(project.layout.projectDirectory.file(".localnet/seednode_2.pid")) } val stopSeedNodeTask = project.tasks.register("stopRegtestSecondSeednode") { @@ -66,15 +77,17 @@ class RegtestPlugin : Plugin { dependsOn(startFirstSeedNodeTask) dependsOn(startSecondSeedNodeTask) - pidFile.set(project.layout.projectDirectory.file(".localnet/mediator.pid")) - startScriptFile.set(project.layout.projectDirectory.file("bisq-desktop")) + workingDirectory.set(project.layout.projectDirectory) + javaExecutable.set(getJavaExecutable()) + libsDir.set(project.layout.projectDirectory.dir("desktop/build/app/lib")) + mainClass.set("bisq.desktop.app.BisqAppMain") arguments.set( - createBisqUserArgs(4444, ".localnet/mediator", "Mediator") + createBisqUserArgs(4444, ".localnet/mediator", "Mediator") ) - workingDirectory.set(project.layout.projectDirectory) logFile.set(project.layout.projectDirectory.file(".localnet/mediator_shell.log")) + pidFile.set(project.layout.projectDirectory.file(".localnet/mediator.pid")) } val stopMediatorTask = project.tasks.register("stopRegtestMediator") { @@ -85,23 +98,26 @@ class RegtestPlugin : Plugin { dependsOn(startFirstSeedNodeTask) dependsOn(startSecondSeedNodeTask) - pidFile.set(project.layout.projectDirectory.file(".localnet/alice.pid")) - startScriptFile.set(project.layout.projectDirectory.file("bisq-desktop")) + workingDirectory.set(project.layout.projectDirectory) + javaExecutable.set(getJavaExecutable()) + libsDir.set(project.layout.projectDirectory.dir("desktop/build/app/lib")) + + mainClass.set("bisq.desktop.app.BisqAppMain") val additionalArgs = listOf( - "--fullDaoNode=true", - "--rpcUser=bisqdao", - "--rpcPassword=bsq", - "--rpcBlockNotificationPort=5122", - "--genesisBlockHeight=111", - "--genesisTxId=30af0050040befd8af25068cc697e418e09c2d8ebd8d411d2240591b9ec203cf" + "--fullDaoNode=true", + "--rpcUser=bisqdao", + "--rpcPassword=bsq", + "--rpcBlockNotificationPort=5122", + "--genesisBlockHeight=111", + "--genesisTxId=30af0050040befd8af25068cc697e418e09c2d8ebd8d411d2240591b9ec203cf" ) arguments.set( - createBisqUserArgs(5555, ".localnet/alice", "Alice", additionalArgs) + createBisqUserArgs(5555, ".localnet/alice", "Alice", additionalArgs) ) - workingDirectory.set(project.layout.projectDirectory) logFile.set(project.layout.projectDirectory.file(".localnet/alice_shell.log")) + pidFile.set(project.layout.projectDirectory.file(".localnet/alice.pid")) } val stopAliceTask = project.tasks.register("stopRegtestAlice") { @@ -112,15 +128,17 @@ class RegtestPlugin : Plugin { dependsOn(startMediatorTask) dependsOn(startAliceTask) - pidFile.set(project.layout.projectDirectory.file(".localnet/bob.pid")) - startScriptFile.set(project.layout.projectDirectory.file("bisq-desktop")) + workingDirectory.set(project.layout.projectDirectory) + javaExecutable.set(getJavaExecutable()) + libsDir.set(project.layout.projectDirectory.dir("desktop/build/app/lib")) + mainClass.set("bisq.desktop.app.BisqAppMain") arguments.set( - createBisqUserArgs(6666, ".localnet/bob", "Bob") + createBisqUserArgs(6666, ".localnet/bob", "Bob") ) - workingDirectory.set(project.layout.projectDirectory) logFile.set(project.layout.projectDirectory.file(".localnet/bob_shell.log")) + pidFile.set(project.layout.projectDirectory.file(".localnet/bob.pid")) } project.tasks.register("stopRegtest") { @@ -136,34 +154,43 @@ class RegtestPlugin : Plugin { } } - private fun createBisqUserArgs(nodePort: Int, - dataDir: String, - appName: String, - additionalArgs: List = emptyList()): List = - createBisqCommonArgs(nodePort) + - listOf( - "--appDataDir=$dataDir", - "--appName=$appName" - ) + additionalArgs + private fun getJavaExecutable(): Provider = + javaToolchainService.launcherFor { + languageVersion.set(JavaLanguageVersion.of(11)) + vendor.set(JvmVendorSpec.AZUL) + implementation.set(JvmImplementation.VENDOR_SPECIFIC) + }.map { it.executablePath } + + private fun createBisqUserArgs( + nodePort: Int, + dataDir: String, + appName: String, + additionalArgs: List = emptyList() + ): List = + createBisqCommonArgs(nodePort) + + listOf( + "--appDataDir=$dataDir", + "--appName=$appName" + ) + additionalArgs private fun createSeedNodeArgs(blockNotificationPort: Int, nodePort: Int, appName: String): List = - createBisqCommonArgs(nodePort) + - listOf( - "--fullDaoNode=true", + createBisqCommonArgs(nodePort) + + listOf( + "--fullDaoNode=true", - "--rpcUser=${RPC_USER}", - "--rpcPassword=${RPC_PASSWORD}", - "--rpcBlockNotificationPort=$blockNotificationPort", + "--rpcUser=${RPC_USER}", + "--rpcPassword=${RPC_PASSWORD}", + "--rpcBlockNotificationPort=$blockNotificationPort", - "--userDataDir=.localnet", - "--appName=$appName" - ) + "--userDataDir=.localnet", + "--appName=$appName" + ) private fun createBisqCommonArgs(nodePort: Int): List = - listOf( - "--baseCurrencyNetwork=BTC_REGTEST", - "--useLocalhostForP2P=true", - "--useDevPrivilegeKeys=true", - "--nodePort=$nodePort" - ) + listOf( + "--baseCurrencyNetwork=BTC_REGTEST", + "--useLocalhostForP2P=true", + "--useDevPrivilegeKeys=true", + "--nodePort=$nodePort" + ) } diff --git a/build-logic/regtest/src/main/kotlin/bisq/gradle/regtest_plugin/StartBisqTask.kt b/build-logic/regtest/src/main/kotlin/bisq/gradle/regtest_plugin/StartBisqTask.kt index f2940c8408f..c02ae4f0d4d 100644 --- a/build-logic/regtest/src/main/kotlin/bisq/gradle/regtest_plugin/StartBisqTask.kt +++ b/build-logic/regtest/src/main/kotlin/bisq/gradle/regtest_plugin/StartBisqTask.kt @@ -4,22 +4,25 @@ import org.gradle.api.DefaultTask import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.ListProperty -import org.gradle.api.tasks.Input -import org.gradle.api.tasks.InputDirectory -import org.gradle.api.tasks.InputFile -import org.gradle.api.tasks.Internal -import org.gradle.api.tasks.TaskAction +import org.gradle.api.provider.Property +import org.gradle.api.tasks.* abstract class StartBisqTask : DefaultTask() { + @get:InputDirectory + abstract val workingDirectory: DirectoryProperty + @get:InputFile - abstract val startScriptFile: RegularFileProperty + abstract val javaExecutable: RegularFileProperty + + @get:InputDirectory + abstract val libsDir: DirectoryProperty @get:Input - abstract val arguments: ListProperty + abstract val mainClass: Property - @get:InputDirectory - abstract val workingDirectory: DirectoryProperty + @get:Input + abstract val arguments: ListProperty @get:Internal abstract val logFile: RegularFileProperty @@ -30,15 +33,29 @@ abstract class StartBisqTask : DefaultTask() { @TaskAction fun run() { ProcessKiller(pidFile.asFile.get()) - .kill() + .kill() // Wait until process stopped Thread.sleep(5000) val processBuilder = ProcessBuilder( - "bash", startScriptFile.asFile.get().absolutePath, arguments.get().joinToString(" ") + javaExecutable.asFile.get().absolutePath, + + "-XX:MaxRAM=8g", + "-Xss1280k", + "-XX:+UseG1GC", + "-XX:MaxHeapFreeRatio=10", + "-XX:MinHeapFreeRatio=5", + "-XX:+UseStringDeduplication", + "-Djava.net.preferIPv4Stack=true", + + "-classpath", createClassPath(), + mainClass.get(), ) + processBuilder.command() + .addAll(arguments.get()) + processBuilder.directory(workingDirectory.asFile.get()) processBuilder.redirectErrorStream(true) processBuilder.redirectOutput(logFile.asFile.get()) @@ -47,7 +64,12 @@ abstract class StartBisqTask : DefaultTask() { val pid = process.pid() pidFile.asFile - .get() - .writeText(pid.toString()) + .get() + .writeText(pid.toString()) + } + + private fun createClassPath(): String { + val libsDirFile = libsDir.asFile.get() + return libsDirFile.listFiles()!!.joinToString(separator = ":") { it.absolutePath } } }