ENT-2145: Configure DemoBench nodes to issue selected currencies. (#3452)

This commit is contained in:
Chris Rankin 2018-06-27 14:01:49 +01:00 committed by GitHub
parent 66cb0e389b
commit 99e314d017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 73 deletions

View File

@ -36,8 +36,8 @@ repositories {
flatDir { flatDir {
dirs 'libs' dirs 'libs'
} }
maven {
jcenter() jcenter()
maven {
url 'http://www.sparetimelabs.com/maven2' url 'http://www.sparetimelabs.com/maven2'
} }
} }

View File

@ -1,9 +1,7 @@
package net.corda.demobench.model package net.corda.demobench.model
import com.typesafe.config.Config import com.typesafe.config.*
import com.typesafe.config.ConfigFactory.empty import com.typesafe.config.ConfigFactory.empty
import com.typesafe.config.ConfigRenderOptions
import com.typesafe.config.ConfigValueFactory
import net.corda.core.identity.CordaX500Name import net.corda.core.identity.CordaX500Name
import net.corda.core.internal.copyToDirectory import net.corda.core.internal.copyToDirectory
import net.corda.core.internal.createDirectories import net.corda.core.internal.createDirectories
@ -45,13 +43,22 @@ data class NodeConfig(
} }
fun nodeConf(): Config { fun nodeConf(): Config {
val rpcSettings: ConfigObject = empty()
val basic = NodeConfigurationData(myLegalName, p2pAddress, rpcAddress, notary, h2port, rpcUsers, useTestClock, detectPublicIp, devMode).toConfig() .withValue("address", valueFor(rpcAddress.toString()))
val rpcSettings = empty() .withValue("adminAddress", valueFor(rpcAdminAddress.toString()))
.withValue("address", ConfigValueFactory.fromAnyRef(rpcAddress.toString()))
.withValue("adminAddress", ConfigValueFactory.fromAnyRef(rpcAdminAddress.toString()))
.root() .root()
return basic.withoutPath("rpcAddress").withoutPath("rpcAdminAddress").withValue("rpcSettings", rpcSettings) val customMap: Map<String, Any> = HashMap<String, Any>().also {
if (issuableCurrencies.isNotEmpty()) {
it["issuableCurrencies"] = issuableCurrencies
}
}
val custom: ConfigObject = ConfigFactory.parseMap(customMap).root()
return NodeConfigurationData(myLegalName, p2pAddress, rpcAddress, notary, h2port, rpcUsers, useTestClock, detectPublicIp, devMode)
.toConfig()
.withoutPath("rpcAddress")
.withoutPath("rpcAdminAddress")
.withValue("rpcSettings", rpcSettings)
.withOptionalValue("custom", custom)
} }
fun webServerConf() = WebServerConfigurationData(myLegalName, rpcAddress, webAddress, rpcUsers).asConfig() fun webServerConf() = WebServerConfigurationData(myLegalName, rpcAddress, webAddress, rpcUsers).asConfig()
@ -60,10 +67,7 @@ data class NodeConfig(
fun toWebServerConfText() = webServerConf().render() fun toWebServerConfText() = webServerConf().render()
fun serialiseAsString(): String { fun serialiseAsString(): String = toConfig().render()
return toConfig().render()
}
private fun Config.render(): String = root().render(renderOptions) private fun Config.render(): String = root().render(renderOptions)
} }
@ -86,7 +90,6 @@ private data class WebServerConfigurationData(
val webAddress: NetworkHostAndPort, val webAddress: NetworkHostAndPort,
val rpcUsers: List<User> val rpcUsers: List<User>
) { ) {
fun asConfig() = toConfig() fun asConfig() = toConfig()
} }
@ -117,3 +120,9 @@ data class NodeConfigWrapper(val baseDir: Path, val nodeConfig: NodeConfig) : Ha
fun user(name: String) = User(name, "letmein", setOf("ALL")) fun user(name: String) = User(name, "letmein", setOf("ALL"))
fun String.toKey() = filter { !it.isWhitespace() }.toLowerCase() fun String.toKey() = filter { !it.isWhitespace() }.toLowerCase()
fun <T> valueFor(any: T): ConfigValue = ConfigValueFactory.fromAnyRef(any)
private fun Config.withOptionalValue(path: String, obj: ConfigObject): Config {
return if (obj.isEmpty()) this else this.withValue(path, obj)
}

View File

@ -1,19 +1,16 @@
package net.corda.demobench.model package net.corda.demobench.model
import com.typesafe.config.ConfigException
import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigValueFactory
import net.corda.core.identity.CordaX500Name import net.corda.core.identity.CordaX500Name
import net.corda.core.utilities.NetworkHostAndPort import net.corda.core.utilities.NetworkHostAndPort
import net.corda.node.services.config.parseAsNodeConfiguration import net.corda.node.services.config.parseAsNodeConfiguration
import net.corda.nodeapi.internal.config.User import net.corda.nodeapi.internal.config.User
import net.corda.webserver.WebServerConfig import net.corda.webserver.WebServerConfig
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test import org.junit.Test
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.Paths import java.nio.file.Paths
import kotlin.test.assertEquals import kotlin.test.*
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class NodeConfigTest { class NodeConfigTest {
companion object { companion object {
@ -35,12 +32,15 @@ class NodeConfigTest {
) )
val nodeConfig = config.nodeConf() val nodeConfig = config.nodeConf()
.withValue("baseDirectory", ConfigValueFactory.fromAnyRef(baseDir.toString())) .withValue("baseDirectory", valueFor(baseDir.toString()))
.withFallback(ConfigFactory.parseResources("reference.conf")) .withFallback(ConfigFactory.parseResources("reference.conf"))
.withFallback(ConfigFactory.parseMap(mapOf("devMode" to true))) .withFallback(ConfigFactory.parseMap(mapOf("devMode" to true)))
.resolve() .resolve()
val fullConfig = nodeConfig.parseAsNodeConfiguration() val fullConfig = nodeConfig.parseAsNodeConfiguration()
// No custom configuration is created by default.
assertFailsWith<ConfigException.Missing> { nodeConfig.getConfig("custom") }
assertEquals(myLegalName, fullConfig.myLegalName) assertEquals(myLegalName, fullConfig.myLegalName)
assertEquals(localPort(40002), fullConfig.rpcOptions.address) assertEquals(localPort(40002), fullConfig.rpcOptions.address)
assertEquals(localPort(10001), fullConfig.p2pAddress) assertEquals(localPort(10001), fullConfig.p2pAddress)
@ -49,6 +49,27 @@ class NodeConfigTest {
assertFalse(fullConfig.detectPublicIp) assertFalse(fullConfig.detectPublicIp)
} }
@Test
fun `reading node configuration with currencies`() {
val config = createConfig(
legalName = myLegalName,
p2pPort = 10001,
rpcPort = 10002,
rpcAdminPort = 10003,
webPort = 10004,
h2port = 10005,
notary = NotaryService(validating = false),
issuableCurrencies = listOf("GBP")
)
val nodeConfig = config.nodeConf()
.withValue("baseDirectory", valueFor(baseDir.toString()))
.withFallback(ConfigFactory.parseResources("reference.conf"))
.resolve()
val custom = nodeConfig.getConfig("custom")
assertEquals(listOf("GBP"), custom.getAnyRefList("issuableCurrencies"))
}
@Test @Test
fun `reading webserver configuration`() { fun `reading webserver configuration`() {
val config = createConfig( val config = createConfig(
@ -63,11 +84,14 @@ class NodeConfigTest {
) )
val nodeConfig = config.webServerConf() val nodeConfig = config.webServerConf()
.withValue("baseDirectory", ConfigValueFactory.fromAnyRef(baseDir.toString())) .withValue("baseDirectory", valueFor(baseDir.toString()))
.withFallback(ConfigFactory.parseResources("web-reference.conf")) .withFallback(ConfigFactory.parseResources("web-reference.conf"))
.resolve() .resolve()
val webConfig = WebServerConfig(baseDir, nodeConfig) val webConfig = WebServerConfig(baseDir, nodeConfig)
// No custom configuration is created by default.
assertFailsWith<ConfigException.Missing> { nodeConfig.getConfig("custom") }
assertEquals(localPort(20001), webConfig.webAddress) assertEquals(localPort(20001), webConfig.webAddress)
assertEquals(localPort(40002), webConfig.rpcAddress) assertEquals(localPort(40002), webConfig.rpcAddress)
assertEquals("trustpass", webConfig.trustStorePassword) assertEquals("trustpass", webConfig.trustStorePassword)
@ -82,7 +106,8 @@ class NodeConfigTest {
webPort: Int = -1, webPort: Int = -1,
h2port: Int = -1, h2port: Int = -1,
notary: NotaryService?, notary: NotaryService?,
users: List<User> = listOf(user("guest")) users: List<User> = listOf(user("guest")),
issuableCurrencies: List<String> = emptyList()
): NodeConfig { ): NodeConfig {
return NodeConfig( return NodeConfig(
myLegalName = legalName, myLegalName = legalName,
@ -92,7 +117,8 @@ class NodeConfigTest {
webAddress = localPort(webPort), webAddress = localPort(webPort),
h2port = h2port, h2port = h2port,
notary = notary, notary = notary,
rpcUsers = users rpcUsers = users,
issuableCurrencies = issuableCurrencies
) )
} }