diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 480bfcb281..a07e28df90 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -120,6 +120,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; @@ -481,7 +482,7 @@ public class RunnerBuilder { miningCoordinator.addEthHashObserver(stratumServer.get()); } - staticNodes.stream() + sanitizePeers(network, staticNodes) .map(DefaultPeer::fromEnodeURL) .forEach(peerNetwork::addMaintainConnectionPeer); @@ -657,6 +658,16 @@ public class RunnerBuilder { context.getBlockchain()); } + private Stream sanitizePeers( + final P2PNetwork network, final Collection 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 buildNodePermissioningController( final List bootnodesAsEnodeURLs, final Synchronizer synchronizer, diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java index 0fdab4681c..ed1f87b3c5 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java @@ -299,6 +299,12 @@ public class DefaultP2PNetwork implements P2PNetwork { @Override 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); peerDiscoveryAgent.bond(peer); rlpxAgent.connect(peer); diff --git a/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java b/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java index 098f719454..d6bb2447d9 100644 --- a/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java +++ b/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java @@ -322,6 +322,15 @@ public final class DefaultP2PNetworkTest { 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() { return (DefaultP2PNetwork) builder().build(); }