Skip to content
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

Add --identity flag for client identification in node browsers #150

Merged
merged 4 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
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
10 changes: 9 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,12 @@ 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")
private final Optional<String> identityString = Optional.empty();
AbdelStark marked this conversation as resolved.
Show resolved Hide resolved

// Completely disables P2P within Besu.
@Option(
names = {"--p2p-enabled"},
Expand Down Expand Up @@ -786,7 +792,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 +852,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 +1420,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 @@ -995,6 +995,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