Skip to content

Commit

Permalink
Regtest: Bypass recursive Gradle invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Sep 12, 2023
1 parent e0164df commit f872d41
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {
class RegtestPlugin @Inject constructor(private val javaToolchainService: JavaToolchainService) : Plugin<Project> {

companion object {
const val RPC_USER = "bisqdao"
Expand All @@ -28,15 +35,17 @@ class RegtestPlugin : Plugin<Project> {
val startFirstSeedNodeTask = project.tasks.register<StartBisqTask>("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<KillTask>("stopRegtestFirstSeednode") {
Expand All @@ -47,15 +56,17 @@ class RegtestPlugin : Plugin<Project> {
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<KillTask>("stopRegtestSecondSeednode") {
Expand All @@ -66,15 +77,17 @@ class RegtestPlugin : Plugin<Project> {
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<KillTask>("stopRegtestMediator") {
Expand All @@ -85,23 +98,26 @@ class RegtestPlugin : Plugin<Project> {
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<KillTask>("stopRegtestAlice") {
Expand All @@ -112,15 +128,17 @@ class RegtestPlugin : Plugin<Project> {
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<KillTask>("stopRegtest") {
Expand All @@ -136,34 +154,43 @@ class RegtestPlugin : Plugin<Project> {
}
}

private fun createBisqUserArgs(nodePort: Int,
dataDir: String,
appName: String,
additionalArgs: List<String> = emptyList()): List<String> =
createBisqCommonArgs(nodePort) +
listOf(
"--appDataDir=$dataDir",
"--appName=$appName"
) + additionalArgs
private fun getJavaExecutable(): Provider<RegularFile> =
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<String> = emptyList()
): List<String> =
createBisqCommonArgs(nodePort) +
listOf(
"--appDataDir=$dataDir",
"--appName=$appName"
) + additionalArgs

private fun createSeedNodeArgs(blockNotificationPort: Int, nodePort: Int, appName: String): List<String> =
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<String> =
listOf(
"--baseCurrencyNetwork=BTC_REGTEST",
"--useLocalhostForP2P=true",
"--useDevPrivilegeKeys=true",
"--nodePort=$nodePort"
)
listOf(
"--baseCurrencyNetwork=BTC_REGTEST",
"--useLocalhostForP2P=true",
"--useDevPrivilegeKeys=true",
"--nodePort=$nodePort"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
abstract val mainClass: Property<String>

@get:InputDirectory
abstract val workingDirectory: DirectoryProperty
@get:Input
abstract val arguments: ListProperty<String>

@get:Internal
abstract val logFile: RegularFileProperty
Expand All @@ -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())
Expand All @@ -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 }
}
}

0 comments on commit f872d41

Please sign in to comment.