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

Commit

Permalink
Change EthNetworkConfig bootnodes to always be URIs instead of accept…
Browse files Browse the repository at this point in the history
…ing either URI or Peer (#1027)
  • Loading branch information
ajsutton authored Mar 3, 2019
1 parent 4ce86c3 commit b3eee20
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import tech.pegasys.pantheon.tests.acceptance.dsl.node.RunnableNode;
import tech.pegasys.pantheon.tests.acceptance.dsl.waitcondition.WaitCondition;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -79,7 +80,12 @@ public void start(final List<? extends RunnableNode> nodes) {
node.getConfiguration()
.genesisConfigProvider()
.createGenesisConfig(nodes)
.map(config -> new EthNetworkConfig(config, NETWORK_ID, bootNodes));
.map(
config ->
new EthNetworkConfig(
config,
NETWORK_ID,
bootNodes.stream().map(URI::create).collect(Collectors.toList())));
node.getConfiguration().ethNetworkConfig(ethNetworkConfig);
node.start(pantheonNodeRunner);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.stream.Stream;

public class DiscoveryConfiguration {
public static List<Peer> MAINNET_BOOTSTRAP_NODES =
public static List<URI> MAINNET_BOOTSTRAP_NODES =
Collections.unmodifiableList(
Stream.of(
"enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303",
Expand All @@ -35,33 +35,33 @@ public class DiscoveryConfiguration {
"enode://158f8aab45f6d19c6cbf4a089c2670541a8da11978a2f90dbf6a502a4a3bab80d288afdbeb7ec0ef6d92de563767f3b1ea9e8e334ca711e9f8e2df5a0385e8e6@13.75.154.138:30303",
"enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303",
"enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303")
.map(DefaultPeer::fromURI)
.map(URI::create)
.collect(toList()));
public static List<Peer> RINKEBY_BOOTSTRAP_NODES =
public static List<URI> RINKEBY_BOOTSTRAP_NODES =
Collections.unmodifiableList(
Stream.of(
"enode://a24ac7c5484ef4ed0c5eb2d36620ba4e4aa13b8c84684e1b4aab0cebea2ae45cb4d375b77eab56516d34bfbd3c1a833fc51296ff084b770b94fb9028c4d25ccf@52.169.42.101:30303",
"enode://343149e4feefa15d882d9fe4ac7d88f885bd05ebb735e547f12e12080a9fa07c8014ca6fd7f373123488102fe5e34111f8509cf0b7de3f5b44339c9f25e87cb8@52.3.158.184:30303",
"enode://b6b28890b006743680c52e64e0d16db57f28124885595fa03a562be1d2bf0f3a1da297d56b13da25fb992888fd556d4c1a27b1f39d531bde7de1921c90061cc6@159.89.28.211:30303")
.map(DefaultPeer::fromURI)
.map(URI::create)
.collect(toList()));
public static List<Peer> ROPSTEN_BOOTSTRAP_NODES =
public static List<URI> ROPSTEN_BOOTSTRAP_NODES =
Collections.unmodifiableList(
Stream.of(
"enode://6332792c4a00e3e4ee0926ed89e0d27ef985424d97b6a45bf0f23e51f0dcb5e66b875777506458aea7af6f9e4ffb69f43f3778ee73c81ed9d34c51c4b16b0b0f@52.232.243.152:30303",
"enode://94c15d1b9e2fe7ce56e458b9a3b672ef11894ddedd0c6f247e0f1d3487f52b66208fb4aeb8179fce6e3a749ea93ed147c37976d67af557508d199d9594c35f09@192.81.208.223:30303")
.map(DefaultPeer::fromURI)
.map(URI::create)
.collect(toList()));

public static List<Peer> GOERLI_BOOTSTRAP_NODES =
public static List<URI> GOERLI_BOOTSTRAP_NODES =
Collections.unmodifiableList(
Stream.of(
"enode://011f758e6552d105183b1761c5e2dea0111bc20fd5f6422bc7f91e0fabbec9a6595caf6239b37feb773dddd3f87240d99d859431891e4a642cf2a0a9e6cbb98a@51.141.78.53:30303",
"enode://176b9417f511d05b6b2cf3e34b756cf0a7096b3094572a8f6ef4cdcb9d1f9d00683bf0f83347eebdf3b81c3521c2332086d9592802230bf528eaf606a1d9677b@13.93.54.137:30303",
"enode://46add44b9f13965f7b9875ac6b85f016f341012d84f975377573800a863526f4da19ae2c620ec73d11591fa9510e992ecc03ad0751f53cc02f7c7ed6d55c7291@94.237.54.114:30313",
"enode://c1f8b7c2ac4453271fa07d8e9ecf9a2e8285aa0bd0c07df0131f47153306b0736fd3db8924e7a9bf0bed6b1d8d4f87362a71b033dc7c64547728d953e43e59b2@52.64.155.147:30303",
"enode://f4a9c6ee28586009fb5a96c8af13a58ed6d8315a9eee4772212c1d4d9cebe5a8b8a78ea4434f318726317d04a3f531a1ef0420cf9752605a562cfe858c46e263@213.186.16.82:30303")
.map(DefaultPeer::fromURI)
.map(URI::create)
.collect(toList()));

private boolean active = true;
Expand Down Expand Up @@ -106,25 +106,11 @@ public List<Peer> getBootstrapPeers() {
return bootstrapPeers;
}

public DiscoveryConfiguration setBootstrapPeers(final Collection<?> bootstrapPeers) {
this.bootstrapPeers = getBootstrapPeersFromGenericCollection(bootstrapPeers);
public DiscoveryConfiguration setBootstrapPeers(final Collection<URI> bootstrapPeers) {
this.bootstrapPeers = bootstrapPeers.stream().map(DefaultPeer::fromURI).collect(toList());
return this;
}

public static List<Peer> getBootstrapPeersFromGenericCollection(
final Collection<?> bootstrapPeers) {
List<Peer> bootnodes;
if (bootstrapPeers.stream().allMatch(URI.class::isInstance)) {
bootnodes =
bootstrapPeers.stream().map(URI.class::cast).map(DefaultPeer::fromURI).collect(toList());
} else if (bootstrapPeers.stream().allMatch(Peer.class::isInstance)) {
bootnodes = bootstrapPeers.stream().map(Peer.class::cast).collect(toList());
} else {
throw new IllegalArgumentException("Expected a list of Peers or a list of enode URIs");
}
return bootnodes;
}

public String getAdvertisedHost() {
return advertisedHost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@
*/
package tech.pegasys.pantheon.ethereum.p2p.discovery;

import static java.util.Arrays.asList;

import tech.pegasys.pantheon.crypto.SECP256K1;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.p2p.config.DiscoveryConfiguration;
import tech.pegasys.pantheon.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent;
import tech.pegasys.pantheon.ethereum.p2p.discovery.internal.Packet;
import tech.pegasys.pantheon.ethereum.p2p.discovery.internal.PacketType;
import tech.pegasys.pantheon.ethereum.p2p.discovery.internal.PingPacketData;
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer;
import tech.pegasys.pantheon.ethereum.p2p.peers.PeerBlacklist;
import tech.pegasys.pantheon.ethereum.permissioning.NodeWhitelistController;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.Arrays;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -114,13 +117,13 @@ public List<MockPeerDiscoveryAgent> startDiscoveryAgents(final int count) {
* @return a list of discovery agents.
*/
public MockPeerDiscoveryAgent startDiscoveryAgent(final List<DiscoveryPeer> bootstrapPeers) {
AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers);
final AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers);

return startDiscoveryAgent(agentBuilder);
}

public MockPeerDiscoveryAgent startDiscoveryAgent(final DiscoveryPeer... bootstrapPeers) {
AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers);
final AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers);

return startDiscoveryAgent(agentBuilder);
}
Expand All @@ -134,7 +137,8 @@ public MockPeerDiscoveryAgent startDiscoveryAgent(final DiscoveryPeer... bootstr
*/
public MockPeerDiscoveryAgent startDiscoveryAgent(
final List<DiscoveryPeer> bootstrapPeers, final PeerBlacklist blacklist) {
AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers).blacklist(blacklist);
final AgentBuilder agentBuilder =
agentBuilder().bootstrapPeers(bootstrapPeers).blacklist(blacklist);

return startDiscoveryAgent(agentBuilder);
}
Expand All @@ -146,13 +150,13 @@ public MockPeerDiscoveryAgent startDiscoveryAgent(final AgentBuilder agentBuilde
}

public MockPeerDiscoveryAgent createDiscoveryAgent(final List<DiscoveryPeer> bootstrapPeers) {
AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers);
final AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers);

return createDiscoveryAgent(agentBuilder);
}

public MockPeerDiscoveryAgent createDiscoveryAgent(final DiscoveryPeer... bootstrapPeers) {
AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers);
final AgentBuilder agentBuilder = agentBuilder().bootstrapPeers(bootstrapPeers);

return createDiscoveryAgent(agentBuilder);
}
Expand All @@ -169,7 +173,7 @@ public static class AgentBuilder {

private PeerBlacklist blacklist = new PeerBlacklist();
private Optional<NodeWhitelistController> whitelist = Optional.empty();
private List<DiscoveryPeer> bootstrapPeers = Collections.emptyList();
private List<URI> bootstrapPeers = Collections.emptyList();
private boolean active = true;

public AgentBuilder(
Expand All @@ -180,13 +184,16 @@ public AgentBuilder(
}

public AgentBuilder bootstrapPeers(final List<DiscoveryPeer> peers) {
this.bootstrapPeers = peers;
this.bootstrapPeers = asEnodes(peers);
return this;
}

public AgentBuilder bootstrapPeers(final DiscoveryPeer... peers) {
this.bootstrapPeers = Arrays.asList(peers);
return this;
return bootstrapPeers(asList(peers));
}

private List<URI> asEnodes(final List<DiscoveryPeer> peers) {
return peers.stream().map(Peer::getEnodeURI).map(URI::create).collect(Collectors.toList());
}

public AgentBuilder whiteList(final Optional<NodeWhitelistController> whitelist) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.net.URI;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -173,7 +174,7 @@ public Runner build() {

final DiscoveryConfiguration discoveryConfiguration;
if (discovery) {
final Collection<?> bootstrap;
final Collection<URI> bootstrap;
if (ethNetworkConfig.getBootNodes() == null) {
bootstrap = DiscoveryConfiguration.MAINNET_BOOTSTRAP_NODES;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public class EthNetworkConfig {
private static final String DEV_GENESIS = "dev.json";
private final String genesisConfig;
private final int networkId;
private final Collection<?> bootNodes;
private final Collection<URI> bootNodes;

public EthNetworkConfig(
final String genesisConfig, final int networkId, final Collection<?> bootNodes) {
final String genesisConfig, final int networkId, final Collection<URI> bootNodes) {
Preconditions.checkNotNull(genesisConfig);
Preconditions.checkNotNull(bootNodes);
this.genesisConfig = genesisConfig;
Expand All @@ -61,7 +61,7 @@ public int getNetworkId() {
return networkId;
}

public Collection<?> getBootNodes() {
public Collection<URI> getBootNodes() {
return bootNodes;
}

Expand Down Expand Up @@ -146,7 +146,7 @@ public static class Builder {

private String genesisConfig;
private int networkId;
private Collection<?> bootNodes;
private Collection<URI> bootNodes;

public Builder(final EthNetworkConfig ethNetworkConfig) {
this.genesisConfig = ethNetworkConfig.genesisConfig;
Expand All @@ -164,7 +164,7 @@ public Builder setNetworkId(final int networkId) {
return this;
}

public Builder setBootNodes(final Collection<?> bootNodes) {
public Builder setBootNodes(final Collection<URI> bootNodes) {
this.bootNodes = bootNodes;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
package tech.pegasys.pantheon.util;

import tech.pegasys.pantheon.cli.EthNetworkConfig;
import tech.pegasys.pantheon.ethereum.p2p.config.DiscoveryConfiguration;
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer;
import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration;

import java.net.URI;
Expand All @@ -29,30 +27,24 @@ public static void areAllBootnodesAreInWhitelist(
final EthNetworkConfig ethNetworkConfig,
final PermissioningConfiguration permissioningConfiguration)
throws Exception {
List<Peer> bootnodesNotInWhitelist = new ArrayList<>();
final List<Peer> bootnodes =
DiscoveryConfiguration.getBootstrapPeersFromGenericCollection(
ethNetworkConfig.getBootNodes());
List<URI> bootnodesNotInWhitelist = new ArrayList<>();
final Collection<URI> bootnodes = ethNetworkConfig.getBootNodes();
if (permissioningConfiguration.isNodeWhitelistEnabled() && bootnodes != null) {
bootnodesNotInWhitelist =
bootnodes.stream()
.filter(
node ->
!permissioningConfiguration
.getNodeWhitelist()
.contains(URI.create(node.getEnodeURI())))
.filter(enode -> !permissioningConfiguration.getNodeWhitelist().contains(enode))
.collect(Collectors.toList());
}
if (!bootnodesNotInWhitelist.isEmpty()) {
throw new Exception(
"Bootnode(s) not in nodes-whitelist " + nodeToURI(bootnodesNotInWhitelist));
"Bootnode(s) not in nodes-whitelist " + enodesAsStrings(bootnodesNotInWhitelist));
}
}

private static Collection<String> nodeToURI(final List<Peer> bootnodesNotInWhitelist) {
private static Collection<String> enodesAsStrings(final List<URI> bootnodesNotInWhitelist) {
return bootnodesNotInWhitelist
.parallelStream()
.map(Peer::getEnodeURI)
.map(URI::toASCIIString)
.collect(Collectors.toList());
}
}
13 changes: 8 additions & 5 deletions pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Collections;
Expand Down Expand Up @@ -197,11 +198,13 @@ private void syncFromGenesis(final SyncMode mode) throws Exception {
EthNetworkConfig.jsonConfig(DEV),
DEV_NETWORK_ID,
Collections.singletonList(
new DefaultPeer(
aheadDbNodeKeys.getPublicKey().getEncodedBytes(),
listenHost,
runnerAhead.getP2pUdpPort(),
runnerAhead.getP2pTcpPort())));
URI.create(
new DefaultPeer(
aheadDbNodeKeys.getPublicKey().getEncodedBytes(),
listenHost,
runnerAhead.getP2pUdpPort(),
runnerAhead.getP2pTcpPort())
.getEnodeURI())));
final Runner runnerBehind =
runnerBuilder
.pantheonController(controllerBehind)
Expand Down

0 comments on commit b3eee20

Please sign in to comment.