|
| 1 | +/* |
| 2 | + * Copyright contributors to Hyperledger Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.ethereum.p2p.discovery.dns; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +import io.vertx.core.AbstractVerticle; |
| 21 | +import org.apache.tuweni.devp2p.EthereumNodeRecord; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +// Adapted from https://github.com/tmio/tuweni and licensed under Apache 2.0 |
| 26 | +/** |
| 27 | + * Resolves DNS records over time, refreshing records. This is written as a Vertx Verticle which |
| 28 | + * allows to run outside the Vertx event loop |
| 29 | + */ |
| 30 | +public class DNSDaemon extends AbstractVerticle { |
| 31 | + private static final Logger LOG = LoggerFactory.getLogger(DNSDaemon.class); |
| 32 | + private final String enrLink; |
| 33 | + private final long seq; |
| 34 | + private final long initialDelay; |
| 35 | + private final long delay; |
| 36 | + private final Optional<DNSDaemonListener> listener; |
| 37 | + private final Optional<String> dnsServer; |
| 38 | + private Optional<Long> periodicTaskId = Optional.empty(); |
| 39 | + private DNSResolver dnsResolver; |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a new DNSDaemon. |
| 43 | + * |
| 44 | + * @param enrLink the ENR link to start with, of the form enrtree://PUBKEY@domain |
| 45 | + * @param listener Listener notified when records are read and whenever they are updated. |
| 46 | + * @param seq the sequence number of the root record. If the root record seq is higher, proceed |
| 47 | + * with visit. |
| 48 | + * @param initialDelay the delay in milliseconds before the first poll of DNS records. |
| 49 | + * @param delay the delay in milliseconds at which to poll DNS records. If negative or zero, it |
| 50 | + * runs only once. |
| 51 | + * @param dnsServer the DNS server to use for DNS query. If null, the default DNS server will be |
| 52 | + * used. |
| 53 | + */ |
| 54 | + public DNSDaemon( |
| 55 | + final String enrLink, |
| 56 | + final DNSDaemonListener listener, |
| 57 | + final long seq, |
| 58 | + final long initialDelay, |
| 59 | + final long delay, |
| 60 | + final String dnsServer) { |
| 61 | + this.enrLink = enrLink; |
| 62 | + this.listener = Optional.ofNullable(listener); |
| 63 | + this.seq = seq; |
| 64 | + this.initialDelay = initialDelay; |
| 65 | + this.delay = delay; |
| 66 | + this.dnsServer = Optional.ofNullable(dnsServer); |
| 67 | + } |
| 68 | + |
| 69 | + /** Starts the DNSDaemon. */ |
| 70 | + @Override |
| 71 | + public void start() { |
| 72 | + if (vertx == null) { |
| 73 | + throw new IllegalStateException("DNSDaemon must be deployed as a vertx verticle."); |
| 74 | + } |
| 75 | + |
| 76 | + LOG.info("Starting DNSDaemon for {}, using {} DNS host.", enrLink, dnsServer.orElse("default")); |
| 77 | + dnsResolver = new DNSResolver(vertx, enrLink, seq, dnsServer); |
| 78 | + if (delay > 0) { |
| 79 | + periodicTaskId = Optional.of(vertx.setPeriodic(initialDelay, delay, this::refreshENRRecords)); |
| 80 | + } else { |
| 81 | + // do one-shot resolution |
| 82 | + refreshENRRecords(0L); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** Stops the DNSDaemon. */ |
| 87 | + @Override |
| 88 | + public void stop() { |
| 89 | + LOG.info("Stopping DNSDaemon for {}", enrLink); |
| 90 | + periodicTaskId.ifPresent(vertx::cancelTimer); |
| 91 | + dnsResolver.close(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Refresh enr records by calling dnsResolver and updating the listeners. |
| 96 | + * |
| 97 | + * @param taskId the task id of the periodic task |
| 98 | + */ |
| 99 | + void refreshENRRecords(final Long taskId) { |
| 100 | + LOG.debug("Refreshing DNS records"); |
| 101 | + final long startTime = System.nanoTime(); |
| 102 | + final List<EthereumNodeRecord> ethereumNodeRecords = dnsResolver.collectAll(); |
| 103 | + final long endTime = System.nanoTime(); |
| 104 | + LOG.debug("Time taken to DNSResolver.collectAll: {} ms", (endTime - startTime) / 1_000_000); |
| 105 | + listener.ifPresent(it -> it.newRecords(dnsResolver.sequence(), ethereumNodeRecords)); |
| 106 | + } |
| 107 | +} |
0 commit comments