Fix dns discovery issue (#3033)

We are creating but not starting the dns discovery daemon. After this modification the node find peers instantly

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>

Co-authored-by: garyschulte <garyschulte@gmail.com>
pull/3039/head
matkt 3 years ago committed by GitHub
parent bd68a1647f
commit 2d630cffd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/DiscoveryConfiguration.java
  2. 10
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java
  3. 34
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.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

@ -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<DNSDaemon> getDnsDaemon() {
return Optional.ofNullable(dnsDaemon);
}
@VisibleForTesting
void checkMaintainedConnectionPeers() {
if (!localNode.isReady()) {

@ -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();
}

Loading…
Cancel
Save