Skip to content

Commit

Permalink
ETCM-365: Put the version info into the client ID. Allow override fro…
Browse files Browse the repository at this point in the history
…m properties.
  • Loading branch information
aakoshh committed Nov 13, 2020
1 parent d792c84 commit 5210423
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mantis {
# Identifier used when connecting to other clients
client-id = "mantis"
# Optionally override the client ID sent in Hello messages.
client-id = null

# Version string (reported by an RPC method)
client-version = "mantis/v2.0"
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/io/iohk/ethereum/utils/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ object Config {

val testmode: Boolean = config.getBoolean("testmode")

val clientId: String = config.getString("client-id")
val clientId =
ConfigUtils
.getOptionalValue(config, "client-id", _.getString("client-id"))
.getOrElse(VersionInfo.clientId)

val clientVersion: String = config.getString("client-version")

Expand Down
34 changes: 34 additions & 0 deletions src/main/scala/io/iohk/ethereum/utils/VersionInfo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.iohk.ethereum.utils

object VersionInfo {

/** Produce a node name that get sent in `WireProtocol.Hello.clientId` according to Ethereum conventions.
*
* Check out examples on https://etcnodes.org
*
* e.g.
* - mantis/v3.0-cd5ae33/linux-amd64/ubuntu-openjdk64bitservervm-java-11.0.9
* - besu/v20.10.0/linux-x86_64/oracle_openjdk-java-11
* - coregeth/v1.11.8-stable-305b5089/linux-amd64/go1.14.4
*/
val clientId = {
val appName = BuildInfo.name
val appVersion = BuildInfo.version
val appCommit = BuildInfo.gitHeadCommit.map("-" + _.take(7)).getOrElse("")

val osName = norm(prop("os.name"))
val osArch = norm(prop("os.arch"))

val javaVendor = norm(prop("java.vendor"))
val javaVmName = norm(prop("java.vm.name"))
val javaVersion = prop("java.version")

s"$appName/v$appVersion$appCommit/$osName-$osArch/$javaVendor-$javaVmName-java-$javaVersion"
}

private def prop(name: String) =
System.getProperty(name)

private def norm(value: String) =
value.toLowerCase.replaceAll("[^a-z0-9]+", "")
}
2 changes: 0 additions & 2 deletions src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mantis {

client-id = "mantis"

datadir = "/tmp/mantis-test/"

secure-random-algo = "NativePRNGNonBlocking"
Expand Down
10 changes: 10 additions & 0 deletions src/test/scala/io/iohk/ethereum/utils/ConfigSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.iohk.ethereum.utils

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ConfigSpec extends AnyFlatSpec with Matchers {
"clientId" should "by default come from VersionInfo" in {
Config.clientId shouldBe VersionInfo.clientId
}
}
12 changes: 12 additions & 0 deletions src/test/scala/io/iohk/ethereum/utils/VersionInfoSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.iohk.ethereum.utils

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class VersionInfoSpec extends AnyFlatSpec with Matchers {
behavior of "clientId"

it should "preserve major and minor Java version" in {
VersionInfo.clientId should fullyMatch regex """.+/v\d.*-[a-z0-9]{7}/.+-.+/.+-.+-java-\d+\.\d+.*"""
}
}

0 comments on commit 5210423

Please sign in to comment.