From b7e08977bd157331365a2ea77145b2ba776d1f68 Mon Sep 17 00:00:00 2001 From: terrencecooke Date: Mon, 4 Jan 2021 15:36:51 -0500 Subject: [PATCH] Added command line option --static-nodes-file (#1414) (#1644) Now able to inject static nodes by explicitly specifying a static nodes JSON file (.json) on the command line Co-authored-by: Ratan (Rai) Sur Signed-off-by: Terrence Cooke --- CHANGELOG.md | 2 + .../org/hyperledger/besu/cli/BesuCommand.java | 22 +++++++++-- .../hyperledger/besu/cli/BesuCommandTest.java | 39 +++++++++++++++++++ .../src/test/resources/everything_config.toml | 1 + 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19748c8f1ce..7c7c76ac48c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Implemented [EIP-778](https://eips.ethereum.org/EIPS/eip-778): Ethereum Node Records (ENR) [\#1680](https://github.com/hyperledger/besu/pull/1680) * Implemented [EIP-868](https://eips.ethereum.org/EIPS/eip-868): Simple Subroutines for the EVM [\#1721](https://github.com/hyperledger/besu/pull/1721) * Added revert reason to eth_estimateGas RPC call. [\#1730](https://github.com/hyperledger/besu/pull/1730) +* Added command line option --static-nodes-file. [#1644](https://github.com/hyperledger/besu/pull/1644) ### Bug Fixes @@ -17,6 +18,7 @@ ### Download link TBA + ## 20.10.3 ### Additions and Improvements diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 2ecb303c580..4baff94dfe0 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -1055,6 +1055,13 @@ void setBannedNodeIds(final List values) { description = "Start Besu in GoQuorum compatibility mode (default: ${DEFAULT-VALUE})") private final Boolean isGoQuorumCompatibilityMode = false; + @CommandLine.Option( + names = {"--static-nodes-file"}, + paramLabel = MANDATORY_FILE_FORMAT_HELP, + description = + "Specifies the static node file containing the static nodes for this node to connect to") + private final Path staticNodesFile = null; + private EthNetworkConfig ethNetworkConfig; private JsonRpcConfiguration jsonRpcConfiguration; private GraphQLConfiguration graphQLConfiguration; @@ -2398,9 +2405,18 @@ public MetricsSystem getMetricsSystem() { } private Set loadStaticNodes() throws IOException { - final String staticNodesFilename = "static-nodes.json"; - final Path staticNodesPath = dataDir().resolve(staticNodesFilename); - + final Path staticNodesPath; + if (staticNodesFile != null) { + staticNodesPath = staticNodesFile.toAbsolutePath(); + if (!staticNodesPath.toFile().exists()) { + throw new ParameterException( + commandLine, String.format("Static nodes file %s does not exist", staticNodesPath)); + } + } else { + final String staticNodesFilename = "static-nodes.json"; + staticNodesPath = dataDir().resolve(staticNodesFilename); + } + logger.info("Static Nodes file = {}", staticNodesPath); return StaticNodesParser.fromPath(staticNodesPath, getEnodeDnsConfiguration()); } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 84e03b87baf..90209310334 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -4074,4 +4074,43 @@ public void assertThatCheckPortClashRejectsAsExpected() throws Exception { .contains( "Port number '8546' has been specified multiple times. Please review the supplied configuration."); } + + @Test + public void staticNodesFileOptionValueAbsentMessage() { + parseCommand("--static-nodes-file"); + assertThat(commandErrorOutput.toString()).startsWith("Missing required parameter for option"); + } + + @Test + public void staticNodesFilesOptionInvalidJSONFormatError() throws IOException { + final Path tempfile = + createTempFile( + "static-nodes-badformat.json", + "\"enode://c0b0e1151971f8a22dc2493c622317c8706c731f6fcf46d93104ef" + + "3a08f21f7750b5d5e17f311091f732c9f917b02e1ae6d39f076903779fd1e7" + + "e7e6cd2fcef6@192.168.1.25:30303\"\n]"); + parseCommand("--static-nodes-file", tempfile.toString()); + assertThat(commandErrorOutput.toString()) + .startsWith("Failed to decode:Cannot deserialize instance of"); + } + + @Test + public void staticNodesFileOptionFileDoesNotExistMessage() { + parseCommand("--static-nodes-file", "this-file-does-not-exist-at-all.json"); + assertThat(commandErrorOutput.toString()).contains("Static nodes file", "does not exist"); + } + + @Test + public void staticNodesFileOptionValidParamenter() throws IOException { + final Path staticNodeTempFile = + createTempFile( + "static-nodes-goodformat.json", + "[\n" + + "\"enode://c0b0e1151971f8a22dc2493c622317c8706c731f6fcf46d93104ef" + + "3a08f21f7750b5d5e17f311091f732c9f917b02e1ae6d39f076903779fd1e7" + + "e7e6cd2fcef6@192.168.1.25:30303\"\n]"); + parseCommand("--static-nodes-file", staticNodeTempFile.toString()); + assertThat(commandOutput.toString()).isEmpty(); + assertThat(commandErrorOutput.toString()).isEmpty(); + } } diff --git a/besu/src/test/resources/everything_config.toml b/besu/src/test/resources/everything_config.toml index 51fc22b90cf..bec77159da7 100644 --- a/besu/src/test/resources/everything_config.toml +++ b/besu/src/test/resources/everything_config.toml @@ -15,6 +15,7 @@ color-enabled=false node-private-key-file="./path/to/privateKey" pid-path="~/.pid" reorg-logging-threshold=0 +static-nodes-file="~/besudata/static-nodes.json" # Security Module plugin to use security-module="localfile"