This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Maintain a staticnodes.json The staticnodes.json file resides in the pantheon data directory, is parsed at startup to determine which peers can be connected to (aside from bootnodes), and is updated whenever addPeer/removePeer RPC is invoked. This file is not updated when finding neighbour peers (ONLY on RPC call).
- Loading branch information
Showing
9 changed files
with
236 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/peers/StaticNodesParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.p2p.peers; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.Collections.emptySet; | ||
|
||
import tech.pegasys.pantheon.util.enode.EnodeURL; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.NoSuchFileException; | ||
import java.nio.file.Path; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import io.vertx.core.json.DecodeException; | ||
import io.vertx.core.json.JsonArray; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class StaticNodesParser { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
public static Set<EnodeURL> fromPath(final Path path) | ||
throws IOException, IllegalArgumentException { | ||
|
||
try { | ||
return readEnodesFromPath(path); | ||
} catch (FileNotFoundException | NoSuchFileException ex) { | ||
LOG.info("No StaticNodes file ({}) exists, creating empty cache.", path); | ||
return emptySet(); | ||
} catch (IOException ex) { | ||
LOG.info("Unable to parse static nodes file ({})", path); | ||
throw ex; | ||
} catch (DecodeException ex) { | ||
LOG.info("Content of ({}} was invalid json, and could not be decoded.", path); | ||
throw ex; | ||
} catch (IllegalArgumentException ex) { | ||
LOG.info("Parsing ({}) has failed due incorrectly formatted enode element.", path); | ||
throw ex; | ||
} | ||
} | ||
|
||
private static Set<EnodeURL> readEnodesFromPath(final Path path) throws IOException { | ||
final byte[] staticNodesContent = Files.readAllBytes(path); | ||
if (staticNodesContent.length == 0) { | ||
return emptySet(); | ||
} | ||
|
||
final JsonArray enodeJsonArray = new JsonArray(new String(staticNodesContent, UTF_8)); | ||
return enodeJsonArray.stream() | ||
.map(obj -> decodeString((String) obj)) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private static EnodeURL decodeString(final String input) { | ||
try { | ||
return new EnodeURL(input); | ||
} catch (IllegalArgumentException ex) { | ||
LOG.info("Illegally constructed enode supplied ({})", input); | ||
throw ex; | ||
} | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...eum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/peers/StaticNodesParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.p2p.peers; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import tech.pegasys.pantheon.util.enode.EnodeURL; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import com.google.common.collect.Lists; | ||
import io.vertx.core.json.DecodeException; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public class StaticNodesParserTest { | ||
|
||
// NOTE: The invalid_static_nodes file is identical to the valid, however one node's port is set | ||
// to "A". | ||
|
||
// First peer ion the valid_static_nodes file. | ||
private final List<EnodeURL> validFileItems = | ||
Lists.newArrayList( | ||
new EnodeURL( | ||
"50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa", | ||
"127.0.0.1", | ||
30303), | ||
new EnodeURL( | ||
"02beb46bc17227616be44234071dfa18516684e45eed88049190b6cb56b0bae218f045fd0450f123b8f55c60b96b78c45e8e478004293a8de6818aa4e02eff97", | ||
"127.0.0.1", | ||
30304), | ||
new EnodeURL( | ||
"819e5cbd81f123516b10f04bf620daa2b385efef06d77253148b814bf1bb6197ff58ebd1fd7bf5dc765b49a4440c733bf941e479c800173f2bfeb887e4fbcbc2", | ||
"127.0.0.1", | ||
30305), | ||
new EnodeURL( | ||
"6cf53e25d2a98a22e7e205a86bda7077e3c8a7bc99e5ff88ddfd2037a550969ab566f069ffa455df0cfae0c21f7aec3447e414eccc473a3e8b20984b90f164ac", | ||
"127.0.0.1", | ||
30306)); | ||
|
||
@Rule public TemporaryFolder testFolder = new TemporaryFolder(); | ||
|
||
@Test | ||
public void validFileLoadsWithExpectedEnodes() throws IOException { | ||
final URL resource = StaticNodesParserTest.class.getResource("valid_static_nodes.json"); | ||
final Path path = Paths.get(resource.getPath()); | ||
|
||
final Set<EnodeURL> enodes = StaticNodesParser.fromPath(path); | ||
|
||
assertThat(enodes).containsExactly(validFileItems.toArray(new EnodeURL[validFileItems.size()])); | ||
} | ||
|
||
@Test | ||
public void invalidFileThrowsAnException() { | ||
final URL resource = StaticNodesParserTest.class.getResource("invalid_static_nodes.json"); | ||
final Path path = Paths.get(resource.getPath()); | ||
|
||
assertThatThrownBy(() -> StaticNodesParser.fromPath(path)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
public void nonJsonFileThrowsAnException() throws IOException { | ||
final File tempFile = testFolder.newFile("file.txt"); | ||
tempFile.deleteOnExit(); | ||
Files.write(tempFile.toPath(), "This Is Not Json".getBytes(Charset.forName("UTF-8"))); | ||
|
||
assertThatThrownBy(() -> StaticNodesParser.fromPath(tempFile.toPath())) | ||
.isInstanceOf(DecodeException.class); | ||
} | ||
|
||
@Test | ||
public void anEmptyCacheIsCreatedIfTheFileDoesNotExist() throws IOException { | ||
final Path path = Paths.get("./arbirtraryFilename.txt"); | ||
|
||
final Set<EnodeURL> enodes = StaticNodesParser.fromPath(path); | ||
assertThat(enodes.size()).isZero(); | ||
} | ||
|
||
@Test | ||
public void cacheIsCreatedIfFileExistsButIsEmpty() throws IOException { | ||
final File tempFile = testFolder.newFile("file.txt"); | ||
tempFile.deleteOnExit(); | ||
|
||
final Set<EnodeURL> enodes = StaticNodesParser.fromPath(tempFile.toPath()); | ||
assertThat(enodes.size()).isZero(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...p2p/src/test/resources/tech/pegasys/pantheon/ethereum/p2p/peers/invalid_static_nodes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["enode://50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa@127.0.0.1:A","enode://02beb46bc17227616be44234071dfa18516684e45eed88049190b6cb56b0bae218f045fd0450f123b8f55c60b96b78c45e8e478004293a8de6818aa4e02eff97@127.0.0.1:30304","enode://819e5cbd81f123516b10f04bf620daa2b385efef06d77253148b814bf1bb6197ff58ebd1fd7bf5dc765b49a4440c733bf941e479c800173f2bfeb887e4fbcbc2@127.0.0.1:30305","enode://6cf53e25d2a98a22e7e205a86bda7077e3c8a7bc99e5ff88ddfd2037a550969ab566f069ffa455df0cfae0c21f7aec3447e414eccc473a3e8b20984b90f164ac@127.0.0.1:30306"] |
4 changes: 4 additions & 0 deletions
4
...m/p2p/src/test/resources/tech/pegasys/pantheon/ethereum/p2p/peers/valid_static_nodes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
["enode://50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa@127.0.0.1:30303", | ||
"enode://02beb46bc17227616be44234071dfa18516684e45eed88049190b6cb56b0bae218f045fd0450f123b8f55c60b96b78c45e8e478004293a8de6818aa4e02eff97@127.0.0.1:30304", | ||
"enode://819e5cbd81f123516b10f04bf620daa2b385efef06d77253148b814bf1bb6197ff58ebd1fd7bf5dc765b49a4440c733bf941e479c800173f2bfeb887e4fbcbc2@127.0.0.1:30305", | ||
"enode://6cf53e25d2a98a22e7e205a86bda7077e3c8a7bc99e5ff88ddfd2037a550969ab566f069ffa455df0cfae0c21f7aec3447e414eccc473a3e8b20984b90f164ac@127.0.0.1:30306"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters