diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/DiscoveryConfiguration.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/DiscoveryConfiguration.java index 79e9010672..4e7763c6f8 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/DiscoveryConfiguration.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/DiscoveryConfiguration.java @@ -296,8 +296,9 @@ public class DiscoveryConfiguration { return dnsDiscoveryURL; } - public void setDnsDiscoveryURL(final String dnsDiscoveryURL) { + public DiscoveryConfiguration setDnsDiscoveryURL(final String dnsDiscoveryURL) { this.dnsDiscoveryURL = dnsDiscoveryURL; + return this; } @Override 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 cbb51af5b2..d528372db4 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 @@ -225,6 +225,7 @@ public class DefaultP2PNetwork implements P2PNetwork { dnsPeers.set(peers); }); } + getDnsDaemon().ifPresent(DNSDaemon::start); final int listeningPort = rlpxAgent.start().join(); final int discoveryPort = @@ -267,9 +268,7 @@ public class DefaultP2PNetwork implements P2PNetwork { return; } - if (dnsDaemon != null) { - dnsDaemon.close(); - } + getDnsDaemon().ifPresent(DNSDaemon::close); peerConnectionScheduler.shutdownNow(); peerDiscoveryAgent.stop().whenComplete((res, err) -> shutdownLatch.countDown()); @@ -321,6 +320,11 @@ public class DefaultP2PNetwork implements P2PNetwork { return wasRemoved; } + @VisibleForTesting + Optional getDnsDaemon() { + return Optional.ofNullable(dnsDaemon); + } + @VisibleForTesting void checkMaintainedConnectionPeers() { if (!localNode.isReady()) { 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 eb7594f6f0..f1f0133511 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 @@ -20,6 +20,7 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -67,7 +68,6 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.StrictStubs.class) @@ -231,7 +231,7 @@ public final class DefaultP2PNetworkTest { when(upnpNatManager.queryExternalIPAddress()) .thenReturn(CompletableFuture.completedFuture(externalIp)); - final NatService natService = Mockito.spy(new NatService(Optional.of(upnpNatManager))); + final NatService natService = spy(new NatService(Optional.of(upnpNatManager))); final P2PNetwork network = builder().natService(natService).build(); network.start(); @@ -333,6 +333,36 @@ public final class DefaultP2PNetworkTest { assertThat(network.addMaintainConnectionPeer(peer)).isFalse(); } + @Test + public void shouldNotStartDnsDiscoveryWhenDNSURLIsNotConfigured() { + // spy on DefaultP2PNetwork + DefaultP2PNetwork testClass = spy(network()); + + testClass.start(); + // ensure we called getDnsDaemon during start, and that it is NOT present: + verify(testClass, times(1)).getDnsDaemon(); + assertThat(testClass.getDnsDaemon()).isNotPresent(); + } + + @Test + public void shouldStartDnsDiscoveryWhenDNSURLIsNotConfigured() { + // create a discovery config with a dns config + DiscoveryConfiguration disco = + DiscoveryConfiguration.create().setDnsDiscoveryURL("enrtree://mock@localhost"); + + // spy on config to return dns discovery config: + NetworkingConfiguration dnsConfig = + when(spy(config).getDiscovery()).thenReturn(disco).getMock(); + + // spy on DefaultP2PNetwork + DefaultP2PNetwork testClass = spy((DefaultP2PNetwork) builder().config(dnsConfig).build()); + + testClass.start(); + // ensure we called getDnsDaemon during start, and that it is present: + verify(testClass, times(1)).getDnsDaemon(); + assertThat(testClass.getDnsDaemon()).isPresent(); + } + private DefaultP2PNetwork network() { return (DefaultP2PNetwork) builder().build(); }