Don't connect to peer if it has some enode ID (#1703)

* don't connect to peer if it has some enode ID

Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net>
pull/1711/head
Abdelhamid Bakhta 4 years ago committed by GitHub
parent cdf907bae1
commit 55291657c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
  2. 6
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java
  3. 9
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java

@ -120,6 +120,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@ -481,7 +482,7 @@ public class RunnerBuilder {
miningCoordinator.addEthHashObserver(stratumServer.get()); miningCoordinator.addEthHashObserver(stratumServer.get());
} }
staticNodes.stream() sanitizePeers(network, staticNodes)
.map(DefaultPeer::fromEnodeURL) .map(DefaultPeer::fromEnodeURL)
.forEach(peerNetwork::addMaintainConnectionPeer); .forEach(peerNetwork::addMaintainConnectionPeer);
@ -657,6 +658,16 @@ public class RunnerBuilder {
context.getBlockchain()); context.getBlockchain());
} }
private Stream<EnodeURL> sanitizePeers(
final P2PNetwork network, final Collection<EnodeURL> enodeURLS) {
if (network.getLocalEnode().isEmpty()) {
return enodeURLS.stream();
}
final EnodeURL localEnodeURL = network.getLocalEnode().get();
return enodeURLS.stream()
.filter(enodeURL -> !enodeURL.getNodeId().equals(localEnodeURL.getNodeId()));
}
private Optional<NodePermissioningController> buildNodePermissioningController( private Optional<NodePermissioningController> buildNodePermissioningController(
final List<EnodeURL> bootnodesAsEnodeURLs, final List<EnodeURL> bootnodesAsEnodeURLs,
final Synchronizer synchronizer, final Synchronizer synchronizer,

@ -299,6 +299,12 @@ public class DefaultP2PNetwork implements P2PNetwork {
@Override @Override
public boolean addMaintainConnectionPeer(final Peer peer) { public boolean addMaintainConnectionPeer(final Peer peer) {
if (localNode.isReady()
&& localNode.getPeer() != null
&& localNode.getPeer().getEnodeURL() != null
&& peer.getEnodeURL().getNodeId().equals(localNode.getPeer().getEnodeURL().getNodeId())) {
return false;
}
final boolean wasAdded = maintainedPeers.add(peer); final boolean wasAdded = maintainedPeers.add(peer);
peerDiscoveryAgent.bond(peer); peerDiscoveryAgent.bond(peer);
rlpxAgent.connect(peer); rlpxAgent.connect(peer);

@ -322,6 +322,15 @@ public final class DefaultP2PNetworkTest {
assertThat(capturedPeers.get(2)).isEqualTo(discoPeers.get(2)); assertThat(capturedPeers.get(2)).isEqualTo(discoPeers.get(2));
} }
@Test
public void cannotAddNodeWithSameEnodeID() {
final DefaultP2PNetwork network = network();
network.start();
assertThat(network.getLocalEnode()).isPresent();
final Peer peer = PeerTestHelper.createPeer(network.getLocalEnode().get().getNodeId());
assertThat(network.addMaintainConnectionPeer(peer)).isFalse();
}
private DefaultP2PNetwork network() { private DefaultP2PNetwork network() {
return (DefaultP2PNetwork) builder().build(); return (DefaultP2PNetwork) builder().build();
} }

Loading…
Cancel
Save