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

Prevent connecting to self #1150

Merged
merged 4 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.p2p.ConnectingToLocalNodeException;
import tech.pegasys.pantheon.ethereum.p2p.PeerNotPermittedException;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer;
Expand Down Expand Up @@ -50,6 +51,8 @@ protected JsonRpcResponse performOperation(final Object id, final String enode)
} catch (final PeerNotPermittedException e) {
return new JsonRpcErrorResponse(
id, JsonRpcError.NON_PERMITTED_NODE_CANNOT_BE_ADDED_AS_A_PEER);
} catch (final ConnectingToLocalNodeException e) {
return new JsonRpcErrorResponse(id, JsonRpcError.INVALID_PARAMS);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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;

public class ConnectingToLocalNodeException extends RuntimeException {
public ConnectingToLocalNodeException(final String message) {
super(message);
}

public ConnectingToLocalNodeException() {
super("Cannot add the local node as a peer connection");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.chain.BlockAddedEvent;
import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.p2p.ConnectingToLocalNodeException;
import tech.pegasys.pantheon.ethereum.p2p.PeerNotPermittedException;
import tech.pegasys.pantheon.ethereum.p2p.api.DisconnectCallback;
import tech.pegasys.pantheon.ethereum.p2p.api.Message;
Expand Down Expand Up @@ -369,6 +370,11 @@ public boolean addMaintainConnectionPeer(final Peer peer) {
if (!isPeerAllowed(peer)) {
throw new PeerNotPermittedException();
}

if (peer.getId().equals(this.getLocalPeerInfo().getNodeId())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the this looks unnecessary

throw new ConnectingToLocalNodeException();
}

final boolean added = peerMaintainConnectionList.add(peer);
if (added) {
connect(peer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static Set<EnodeURL> fromPath(final Path path)
try {
return readEnodesFromPath(path);
} catch (FileNotFoundException | NoSuchFileException ex) {
LOG.info("No StaticNodes file ({}) exists, creating empty cache.", path);
LOG.info("StaticNodes file {} does not exist, no static connections will be created.", path);
return emptySet();
} catch (IOException ex) {
LOG.info("Unable to parse static nodes file ({})", path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.pending.PendingTransactionSubscriptionService;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.syncing.SyncingSubscriptionService;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.ethereum.p2p.ConnectingToLocalNodeException;
import tech.pegasys.pantheon.ethereum.p2p.NetworkRunner;
import tech.pegasys.pantheon.ethereum.p2p.NoopP2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
Expand Down Expand Up @@ -303,7 +304,10 @@ public Runner build() {
staticNodes.forEach(
enodeURL -> {
final Peer peer = DefaultPeer.fromEnodeURL(enodeURL);
peerNetwork.addMaintainConnectionPeer(peer);
try {
peerNetwork.addMaintainConnectionPeer(peer);
} catch (ConnectingToLocalNodeException ex) {
}
});

Optional<JsonRpcHttpService> jsonRpcHttpService = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ public void run() {
permissioningConfiguration();

final Collection<EnodeURL> staticNodes = loadStaticNodes();
logger.info("Connecting to {} static nodes.", staticNodes.size());
logger.trace("Static Nodes = {}", staticNodes);

permissioningConfiguration
.flatMap(PermissioningConfiguration::getLocalConfig)
Expand Down