Skip to content

Commit

Permalink
Added command line option --static-nodes-file (hyperledger#1414) (hyp…
Browse files Browse the repository at this point in the history
…erledger#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 <[email protected]>
Signed-off-by: Terrence Cooke <[email protected]>
  • Loading branch information
2 people authored and eum602 committed Nov 3, 2023
1 parent 7361ca7 commit b7e0897
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,6 +18,7 @@
### Download link
TBA


## 20.10.3

### Additions and Improvements
Expand Down
22 changes: 19 additions & 3 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,13 @@ void setBannedNodeIds(final List<String> 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;
Expand Down Expand Up @@ -2398,9 +2405,18 @@ public MetricsSystem getMetricsSystem() {
}

private Set<EnodeURL> 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());
}

Expand Down
39 changes: 39 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 @@ -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"
+ "[email protected]: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"
+ "[email protected]:30303\"\n]");
parseCommand("--static-nodes-file", staticNodeTempFile.toString());
assertThat(commandOutput.toString()).isEmpty();
assertThat(commandErrorOutput.toString()).isEmpty();
}
}
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 @@ -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"
Expand Down

0 comments on commit b7e0897

Please sign in to comment.