Skip to content

ETCM-365: Version info in Hello.clientId #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ val root = {
val root = project
.in(file("."))
.configs(Integration, Benchmark, Evm, Ets, Snappy, Rpc)
.enablePlugins(BuildInfoPlugin)
.settings(
buildInfoKeys := Seq[BuildInfoKey](name, version, git.gitHeadCommit),
buildInfoPackage := "io.iohk.ethereum.utils"
)
.settings(commonSettings: _*)
.settings(
libraryDependencies ++= dep
Expand Down Expand Up @@ -116,8 +121,9 @@ Test / parallelExecution := true
testOptions in Test += Tests.Argument("-oDG")

// protobuf compilation
// Into a subdirectory of src_managed to avoid it deleting other generated files; see https://github.com/sbt/sbt-buildinfo/issues/149
PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
scalapb.gen() -> (sourceManaged in Compile).value / "protobuf"
)

// have the protobuf API version file as a resource
Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.7.5")
addSbtPlugin("com.thoughtworks.sbt-api-mappings" % "sbt-api-mappings" % "3.0.0")
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.25")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.5.1")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0")

libraryDependencies += "com.trueaccord.scalapb" %% "compilerplugin" % "0.6.6"
200 changes: 200 additions & 0 deletions project/repo.nix

Large diffs are not rendered by default.

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 augment the client ID sent in Hello messages.
client-identity = null

# Version string (reported by an RPC method)
client-version = "mantis/v2.0"
Expand Down
3 changes: 2 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,8 @@ object Config {

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

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

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

Expand Down
48 changes: 48 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,48 @@
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
*
* Apparently ethstats expects either 4 parts or 5:
* - client/version/os/compiler
* - client/identity/version/os/compiler
*/
def nodeName(maybeIdentity: Option[String] = None): String = {
val app = {
val name = BuildInfo.name
val id = maybeIdentity.map("/" + _).getOrElse("")
s"$name$id"
}
val version = {
val version = BuildInfo.version
val commit = BuildInfo.gitHeadCommit.map("-" + _.take(7)).getOrElse("")
s"v$version$commit"
}
val os = {
val name = norm(prop("os.name"))
val arch = norm(prop("os.arch"))
s"$name-$arch"
}
val vm = {
val vendor = norm(prop("java.vendor"))
val vmName = norm(prop("java.vm.name"))
val version = prop("java.version")
s"$vendor-$vmName-java-$version"
}
s"$app/$version/$os/$vm"
}

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.nodeName()
}
}
18 changes: 18 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,18 @@
package io.iohk.ethereum.utils

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

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

it should "match ethstats expected structure and preserve major and minor Java version" in {
VersionInfo.nodeName() should fullyMatch regex """mantis/v\d(\.\d+)*-[a-z0-9]{7}/[^/]+-[^/]+/[^/]+-.[^/]+-java-\d+\.\d+[._0-9]*"""
}

it should "augment the name with an identity" in {
val name = VersionInfo.nodeName(Some("iohk"))
name should startWith("mantis/iohk/v")
name.count(_ == '/') shouldBe 4
}
}