Refactoring related to BFT notary demo (#680)

* Fix: Add missing @StartableByRPC to fix the Raft notary demo
* Make loadConfig take a Config object, for cordformation Node
* Unduplicate User.toMap
* Unduplicate WHITESPACE regex, choose possessive form
* Use slash to make a Path
* Remove Companion where redundant
* Remove unused code
This commit is contained in:
Andrzej Cichocki
2017-05-16 11:30:50 +01:00
committed by GitHub
parent 42d0a3c638
commit d3bb040355
23 changed files with 86 additions and 101 deletions

View File

@ -65,9 +65,7 @@ data class CmdLineOptions(val baseDirectory: Path,
val isVersion: Boolean,
val noLocalShell: Boolean,
val sshdServer: Boolean) {
fun loadConfig(allowMissingConfig: Boolean = false, configOverrides: Map<String, Any?> = emptyMap()): FullNodeConfiguration {
return ConfigHelper
.loadConfig(baseDirectory, configFile, allowMissingConfig, configOverrides)
fun loadConfig() = ConfigHelper
.loadConfig(baseDirectory, configFile)
.parseAs<FullNodeConfiguration>()
}
}

View File

@ -18,9 +18,7 @@ import net.corda.core.node.services.ServiceInfo
import net.corda.core.node.services.ServiceType
import net.corda.core.utilities.*
import net.corda.node.LOGS_DIRECTORY_NAME
import net.corda.node.services.config.ConfigHelper
import net.corda.node.services.config.FullNodeConfiguration
import net.corda.node.services.config.VerifierType
import net.corda.node.services.config.*
import net.corda.node.services.network.NetworkMapService
import net.corda.node.services.transactions.RaftValidatingNotaryService
import net.corda.node.utilities.ServiceIdentityGenerator
@ -491,9 +489,9 @@ class DriverDSL(
val webAddress = portAllocation.nextHostAndPort()
val debugPort = if (isDebug) debugPortAllocation.nextPort() else null
// TODO: Derive name from the full picked name, don't just wrap the common name
val name = providedName ?: X509Utilities.getDevX509Name("${pickA(name).commonName}-${p2pAddress.port}")
val name = providedName ?: X509Utilities.getDevX509Name("${oneOf(names).commonName}-${p2pAddress.port}")
val baseDirectory = driverDirectory / name.commonName
val configOverrides = mapOf(
val configOverrides = configOf(
"myLegalName" to name.toString(),
"p2pAddress" to p2pAddress.toString(),
"rpcAddress" to rpcAddress.toString(),
@ -511,13 +509,7 @@ class DriverDSL(
}
},
"useTestClock" to useTestClock,
"rpcUsers" to rpcUsers.map {
mapOf(
"username" to it.username,
"password" to it.password,
"permissions" to it.permissions
)
},
"rpcUsers" to rpcUsers.map { it.toMap() },
"verifierType" to verifierType.name
) + customOverrides
@ -612,7 +604,7 @@ class DriverDSL(
val config = ConfigHelper.loadConfig(
baseDirectory = baseDirectory,
allowMissingConfig = true,
configOverrides = mapOf(
configOverrides = configOf(
"myLegalName" to dedicatedNetworkMapLegalName.toString(),
// TODO: remove the webAddress as NMS doesn't need to run a web server. This will cause all
// node port numbers to be shifted, so all demos and docs need to be updated accordingly.
@ -635,13 +627,13 @@ class DriverDSL(
}
companion object {
val name = arrayOf(
private val names = arrayOf(
ALICE.name,
BOB.name,
DUMMY_BANK_A.name
)
fun <A> pickA(array: Array<A>): A = array[Math.abs(Random().nextInt()) % array.size]
private fun <A> oneOf(array: Array<A>) = array[Random().nextInt(array.size)]
private fun startNode(
executorService: ListeningScheduledExecutorService,

View File

@ -18,21 +18,23 @@ import net.corda.nodeapi.config.SSLConfiguration
import org.bouncycastle.asn1.x500.X500Name
import java.nio.file.Path
fun configOf(vararg pairs: Pair<String, Any?>) = ConfigFactory.parseMap(mapOf(*pairs))
operator fun Config.plus(overrides: Map<String, Any?>) = ConfigFactory.parseMap(overrides).withFallback(this)
object ConfigHelper {
private val log = loggerFor<ConfigHelper>()
fun loadConfig(baseDirectory: Path,
configFile: Path = baseDirectory / "node.conf",
allowMissingConfig: Boolean = false,
configOverrides: Map<String, Any?> = emptyMap()): Config {
configOverrides: Config = ConfigFactory.empty()): Config {
val parseOptions = ConfigParseOptions.defaults()
val defaultConfig = ConfigFactory.parseResources("reference.conf", parseOptions.setAllowMissing(false))
val appConfig = ConfigFactory.parseFile(configFile.toFile(), parseOptions.setAllowMissing(allowMissingConfig))
val overrideConfig = ConfigFactory.parseMap(configOverrides + mapOf(
val finalConfig = configOf(
// Add substitution values here
"basedir" to baseDirectory.toString())
)
val finalConfig = overrideConfig
.withFallback(configOverrides)
.withFallback(appConfig)
.withFallback(defaultConfig)
.resolve()

View File

@ -0,0 +1,24 @@
package net.corda.node.services.config
import com.typesafe.config.ConfigFactory
import net.corda.nodeapi.config.toProperties
import org.junit.Test
import kotlin.test.assertEquals
class ConfigOperatorTests {
@Test
fun `config plus behaves the same as map plus`() {
val config = arrayOf("x" to "y1", "a" to "b", "z" to "Z")
val overrides = arrayOf("x" to "y2", "c" to "d", "z" to null)
val old = ConfigFactory.parseMap(mapOf(*config) + mapOf(*overrides))
val new = configOf(*config) + mapOf(*overrides)
listOf(old, new).map { it.toProperties() }.forEach { c ->
assertEquals("y2", c["x"])
assertEquals("b", c["a"])
assertEquals("d", c["c"])
assertEquals(null, c["z"])
}
}
}