Skip to content

Commit

Permalink
Add --identity flag for client identification in node browsers (hyper…
Browse files Browse the repository at this point in the history
…ledger#150)

Support the "--identity" flag.  This adds a fifth field to the normally 
four part clientId, with the identity in the second position.

For example, if the CLI flag "--identity PegaSysEng" were passed in the
clientID reported by ethernodes would read

`besu/PegaSysEng/v1.3.2/linux-x86_64/oracle_openjdk-java-11`

Whereas without the flag it would just read

`besu/v1.3.2/linux-x86_64/oracle_openjdk-java-11`

Signed-off-by: Danno Ferrin <[email protected]>
Signed-off-by: edwardmack <[email protected]>
  • Loading branch information
shemnon authored and edwardmack committed Nov 4, 2019
1 parent f00f03c commit efe7cbb
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 13 deletions.
23 changes: 13 additions & 10 deletions besu/src/main/java/org/hyperledger/besu/BesuInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@

import org.hyperledger.besu.util.PlatformDetector;

import java.util.Optional;

public final class BesuInfo {
private static final String CLIENT_IDENTITY = "besu";
private static final String VERSION =
CLIENT_IDENTITY
+ "/v"
+ BesuInfo.class.getPackage().getImplementationVersion()
+ "/"
+ PlatformDetector.getOS()
+ "/"
+ PlatformDetector.getVM();
private static final String CLIENT = "besu";
private static final String VERSION = BesuInfo.class.getPackage().getImplementationVersion();
private static final String OS = PlatformDetector.getOS();
private static final String VM = PlatformDetector.getVM();

private BesuInfo() {}

public static String version() {
return VERSION;
return String.format("%s/v%s/%s/%s", CLIENT, VERSION, OS, VM);
}

public static String nodeName(final Optional<String> maybeIdentity) {
return maybeIdentity
.map(identity -> String.format("%s/%s/v%s/%s/%s", CLIENT, identity, VERSION, OS, VM))
.orElse(version());
}
}
10 changes: 8 additions & 2 deletions besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public class RunnerBuilder {
private ObservableMetricsSystem metricsSystem;
private Optional<PermissioningConfiguration> permissioningConfiguration = Optional.empty();
private Collection<EnodeURL> staticNodes = Collections.emptyList();
private Optional<String> identityString = Optional.empty();

public RunnerBuilder vertx(final Vertx vertx) {
this.vertx = vertx;
Expand Down Expand Up @@ -247,6 +248,11 @@ public RunnerBuilder staticNodes(final Collection<EnodeURL> staticNodes) {
return this;
}

public RunnerBuilder identityString(final Optional<String> identityString) {
this.identityString = identityString;
return this;
}

public Runner build() {

Preconditions.checkNotNull(besuController);
Expand Down Expand Up @@ -290,7 +296,7 @@ public Runner build() {
.setBindPort(p2pListenPort)
.setMaxPeers(maxPeers)
.setSupportedProtocols(subProtocols)
.setClientId(BesuInfo.version())
.setClientId(BesuInfo.nodeName(identityString))
.setLimitRemoteWireConnectionsEnabled(limitRemoteWireConnectionsEnabled)
.setFractionRemoteWireConnectionsAllowed(fractionRemoteConnectionsAllowed);
networkingConfiguration.setRlpx(rlpxConfiguration).setDiscovery(discoveryConfiguration);
Expand Down Expand Up @@ -581,7 +587,7 @@ private Map<String, JsonRpcMethod> jsonRpcMethods(
final Map<String, JsonRpcMethod> methods =
new JsonRpcMethodsFactory()
.methods(
BesuInfo.version(),
BesuInfo.nodeName(identityString),
ethNetworkConfig.getNetworkId(),
besuController.getGenesisConfigOptions(),
network,
Expand Down
11 changes: 10 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ protected KeyLoader getKeyLoader() {
// CLI options defined by user at runtime.
// Options parsing is done with CLI library Picocli https://picocli.info/

@Option(
names = "--identity",
paramLabel = "<String>",
description = "Identification for this node in the Client ID",
arity = "1")
private final Optional<String> identityString = Optional.empty();

// Completely disables P2P within Besu.
@Option(
names = {"--p2p-enabled"},
Expand Down Expand Up @@ -786,7 +793,7 @@ public void parse(
public void run() {
try {
prepareLogging();
logger.info("Starting Besu version: {}", BesuInfo.version());
logger.info("Starting Besu version: {}", BesuInfo.nodeName(identityString));
validateOptions().configure().controller().startPlugins().startSynchronization();
} catch (final Exception e) {
throw new ParameterException(this.commandLine, e.getMessage(), e);
Expand Down Expand Up @@ -846,6 +853,7 @@ private BesuCommand registerConverters() {
commandLine.registerConverter(Wei.class, (arg) -> Wei.of(Long.parseUnsignedLong(arg)));
commandLine.registerConverter(PositiveNumber.class, PositiveNumber::fromString);
commandLine.registerConverter(Hash.class, Hash::fromHexString);
commandLine.registerConverter(Optional.class, Optional::of);

metricCategoryConverter.addCategories(BesuMetricCategory.class);
metricCategoryConverter.addCategories(StandardMetricCategory.class);
Expand Down Expand Up @@ -1413,6 +1421,7 @@ private void synchronize(
.metricsSystem(metricsSystem)
.metricsConfiguration(metricsConfiguration)
.staticNodes(staticNodes)
.identityString(identityString)
.build();

addShutdownHook(runner);
Expand Down
27 changes: 27 additions & 0 deletions besu/src/test/java/org/hyperledger/besu/BesuInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Optional;

import org.junit.Test;

public final class BesuInfoTest {
Expand All @@ -30,4 +32,29 @@ public final class BesuInfoTest {
public void versionStringIsEthstatsFriendly() {
assertThat(BesuInfo.version()).matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+");
}

/**
* Ethstats wants a version string like &lt;foo&gt/v&lt;bar&gt/&lt;baz&gt/&lt;bif&gt. Foo is the
* client identity (besu, Geth, Parity, etc). Bar is the version, in semantic version form
* (1.2.3-whatever), baz is OS and chip architecture, and bif is "compiler" - which we use as JVM
* info.
*/
@Test
public void noIdentityNodeNameIsEthstatsFriendly() {
assertThat(BesuInfo.nodeName(Optional.empty()))
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+");
}

/**
* Ethstats also accepts a version string like
* &lt;foo&gt/%lt;qux&gt;/v&lt;bar&gt/&lt;baz&gt/&lt;bif&gt. Foo is the client identity (besu,
* Geth, Parity, etc). Qux is user identity (PegaSysEng, Yes-EIP-1679, etc) Bar is the version, in
* semantic version form (1.2.3-whatever), baz is OS and chip architecture, and bif is "compiler"
* - which we use as JVM info.
*/
@Test
public void userIdentityNodeNameIsEthstatsFriendly() {
assertThat(BesuInfo.nodeName(Optional.of("TestUserIdentity")))
.matches("[^/]+/[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+");
}
}
10 changes: 10 additions & 0 deletions besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,16 @@ public void genesisPathDisabledUnderDocker() {
assertThat(commandOutput.toString()).isEmpty();
}

@Test
public void identityValueTrueMustBeUsed() {
parseCommand("--identity", "test");

verify(mockRunnerBuilder.identityString(eq(Optional.of("test")))).build();

assertThat(commandOutput.toString()).isEmpty();
assertThat(commandErrorOutput.toString()).isEmpty();
}

@Test
public void p2pEnabledOptionValueTrueMustBeUsed() {
parseCommand("--p2p-enabled", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public void initMocks() throws Exception {
when(mockRunnerBuilder.metricsSystem(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.metricsConfiguration(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.staticNodes(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.identityString(any())).thenReturn(mockRunnerBuilder);
when(mockRunnerBuilder.build()).thenReturn(mockRunner);

when(storageService.getByName("rocksdb")).thenReturn(Optional.of(rocksDBStorageFactory));
Expand Down
1 change: 1 addition & 0 deletions besu/src/test/resources/everything_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ logging="INFO"
node-private-key-file="./path/to/privateKey"

# P2P network
identity="PegaSysEng"
p2p-enabled=true
nat-method="NONE"
discovery-enabled=false
Expand Down

0 comments on commit efe7cbb

Please sign in to comment.