Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Added static nodes acceptance test (#1745)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassaldanha authored Jul 24, 2019
1 parent 12d19a1 commit 3d1d766
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.tests.acceptance;

import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase;
import tech.pegasys.pantheon.tests.acceptance.dsl.WaitUtils;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;

public class StaticNodesAcceptanceTest extends AcceptanceTestBase {

private Node otherNode;
private Node node;

@Before
public void setUp() throws Exception {
otherNode = pantheon.createNodeWithNoDiscovery("other-node");
cluster.start(otherNode);
}

@Test
public void shouldConnectToNodeAddedAsStaticNode() throws Exception {
node = pantheon.createNodeWithStaticNodes("node", Arrays.asList(otherNode));
cluster.addNode(node);

node.verify(net.awaitPeerCount(1));

WaitUtils.waitFor(1000000, () -> {});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.tests.acceptance.dsl;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

public class StaticNodesUtils {

public static Path createStaticNodesFile(final Path directory, final List<String> staticNodes) {
try {
final Path tempFile = Files.createTempFile(directory, "", "");
tempFile.toFile().deleteOnExit();

final Path staticNodesFile = tempFile.getParent().resolve("static-nodes.json");
Files.move(tempFile, staticNodesFile);
staticNodesFile.toFile().deleteOnExit();

final String json =
staticNodes.stream()
.map(s -> String.format("\"%s\"", s))
.collect(Collectors.joining(",", "[", "]"));
Files.write(staticNodesFile, json.getBytes(UTF_8));

return staticNodesFile;
} catch (final IOException e) {
throw new IllegalStateException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public class PantheonNode implements NodeConfiguration, RunnableNode, AutoClosea
private final boolean discoveryEnabled;
private final List<URI> bootnodes = new ArrayList<>();
private final boolean bootnodeEligible;

private Optional<String> genesisConfig = Optional.empty();
private NodeRequests nodeRequests;
private LoginRequestFactory loginRequestFactory;
private boolean useWsForJsonRpc = false;
private String token = null;
private final List<String> plugins = new ArrayList<>();
private final List<String> extraCLIOptions;
private final List<String> staticNodes;

public PantheonNode(
final String name,
Expand All @@ -120,7 +120,8 @@ public PantheonNode(
final boolean bootnodeEligible,
final boolean revertReasonEnabled,
final List<String> plugins,
final List<String> extraCLIOptions)
final List<String> extraCLIOptions,
final List<String> staticNodes)
throws IOException {
this.bootnodeEligible = bootnodeEligible;
this.revertReasonEnabled = revertReasonEnabled;
Expand Down Expand Up @@ -159,6 +160,7 @@ public PantheonNode(
}
});
this.extraCLIOptions = extraCLIOptions;
this.staticNodes = staticNodes;
LOG.info("Created PantheonNode {}", this.toString());
}

Expand Down Expand Up @@ -529,6 +531,15 @@ public boolean isRevertReasonEnabled() {
return revertReasonEnabled;
}

@Override
public List<String> getStaticNodes() {
return staticNodes;
}

public boolean hasStaticNodes() {
return staticNodes != null && !staticNodes.isEmpty();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApis;
import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration;
import tech.pegasys.pantheon.tests.acceptance.dsl.StaticNodesUtils;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -99,6 +100,10 @@ public void startNode(final PantheonNode node) {
params.add(node.getBootnodes().stream().map(URI::toString).collect(Collectors.joining(",")));
}

if (node.hasStaticNodes()) {
createStaticNodes(node);
}

if (node.isJsonRpcEnabled()) {
params.add("--rpc-http-enabled");
params.add("--rpc-http-host");
Expand Down Expand Up @@ -244,6 +249,10 @@ private Path createGenesisFile(final PantheonNode node, final String genesisConf
}
}

private void createStaticNodes(final PantheonNode node) {
StaticNodesUtils.createStaticNodesFile(node.homeDirectory(), node.getStaticNodes());
}

private String apiList(final Collection<RpcApi> rpcApis) {
return rpcApis.stream().map(RpcApis::getValue).collect(Collectors.joining(","));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public void startNode(final PantheonNode node) {
.metricsConfiguration(node.metricsConfiguration())
.p2pEnabled(node.isP2pEnabled())
.graphQLConfiguration(GraphQLConfiguration.createDefault())
.staticNodes(
node.getStaticNodes().stream()
.map(EnodeURL::fromString)
.collect(Collectors.toList()))
.build();

runner.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ public interface NodeConfiguration {
List<String> getExtraCLIOptions();

boolean isRevertReasonEnabled();

List<String> getStaticNodes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class PantheonFactoryConfiguration {
private final boolean revertReasonEnabled;
private final List<String> plugins;
private final List<String> extraCLIOptions;
private final List<String> staticNodes;

public PantheonFactoryConfiguration(
final String name,
Expand All @@ -61,7 +62,8 @@ public PantheonFactoryConfiguration(
final boolean bootnodeEligible,
final boolean revertReasonEnabled,
final List<String> plugins,
final List<String> extraCLIOptions) {
final List<String> extraCLIOptions,
final List<String> staticNodes) {
this.name = name;
this.miningParameters = miningParameters;
this.privacyParameters = privacyParameters;
Expand All @@ -79,6 +81,7 @@ public PantheonFactoryConfiguration(
this.revertReasonEnabled = revertReasonEnabled;
this.plugins = plugins;
this.extraCLIOptions = extraCLIOptions;
this.staticNodes = staticNodes;
}

public String getName() {
Expand Down Expand Up @@ -148,4 +151,8 @@ public List<String> getExtraCLIOptions() {
public boolean isRevertReasonEnabled() {
return revertReasonEnabled;
}

public List<String> getStaticNodes() {
return staticNodes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class PantheonFactoryConfigurationBuilder {
private boolean revertReasonEnabled = false;
private List<String> plugins = new ArrayList<>();
private List<String> extraCLIOptions = new ArrayList<>();
private List<String> staticNodes = new ArrayList<>();

public PantheonFactoryConfigurationBuilder() {
// Check connections more frequently during acceptance tests to cut down on
Expand Down Expand Up @@ -193,6 +194,11 @@ public PantheonFactoryConfigurationBuilder revertReasonEnabled() {
return this;
}

public PantheonFactoryConfigurationBuilder staticNodes(final List<String> staticNodes) {
this.staticNodes = staticNodes;
return this;
}

public PantheonFactoryConfiguration build() {
return new PantheonFactoryConfiguration(
name,
Expand All @@ -211,6 +217,7 @@ public PantheonFactoryConfiguration build() {
bootnodeEligible,
revertReasonEnabled,
plugins,
extraCLIOptions);
extraCLIOptions,
staticNodes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
package tech.pegasys.pantheon.tests.acceptance.dsl.node.configuration;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;

import tech.pegasys.pantheon.ethereum.jsonrpc.JsonRpcConfiguration;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.WebSocketConfiguration;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.RunnableNode;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.configuration.genesis.GenesisConfigurationFactory;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -49,7 +52,8 @@ public PantheonNode create(final PantheonFactoryConfiguration config) throws IOE
config.isBootnodeEligible(),
config.isRevertReasonEnabled(),
config.getPlugins(),
config.getExtraCLIOptions());
config.getExtraCLIOptions(),
config.getStaticNodes());
}

public PantheonNode createMinerNode(final String name) throws IOException {
Expand Down Expand Up @@ -263,4 +267,25 @@ public PantheonNode createIbft2NodeWithValidators(final String name, final Strin
asList(validators), nodes, genesis::createIbft2GenesisConfig))
.build());
}

public PantheonNode createNodeWithStaticNodes(final String name, final List<Node> staticNodes)
throws IOException {

final List<String> staticNodesUrls =
staticNodes.stream()
.map(node -> (RunnableNode) node)
.map(RunnableNode::enodeUrl)
.map(URI::toASCIIString)
.collect(toList());

return create(
new PantheonFactoryConfigurationBuilder()
.name(name)
.jsonRpcEnabled()
.webSocketEnabled()
.discoveryEnabled(false)
.staticNodes(staticNodesUrls)
.bootnodeEligible(false)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import tech.pegasys.pantheon.tests.acceptance.dsl.node.configuration.PantheonFactoryConfiguration;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.configuration.genesis.GenesisConfigurationProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -66,7 +67,8 @@ public class PrivacyPantheonFactoryConfiguration extends PantheonFactoryConfigur
bootnodeEligible,
revertReasonEnabled,
plugins,
extraCLIOptions);
extraCLIOptions,
new ArrayList<>());
this.orion = orion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import tech.pegasys.pantheon.util.bytes.BytesValues;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -83,7 +84,8 @@ public PrivacyNode(
bootnodeEligible,
revertReasonEnabled,
plugins,
extraCLIOptions);
extraCLIOptions,
new ArrayList<>());
this.orion = orion;
}

Expand Down

0 comments on commit 3d1d766

Please sign in to comment.