Skip to main content

zebra_network/
config.rs

1//! Configuration for Zebra's network communication.
2
3use std::{
4    collections::HashSet,
5    io::{self, ErrorKind},
6    net::{IpAddr, SocketAddr},
7    sync::Arc,
8    time::Duration,
9};
10
11use indexmap::IndexSet;
12use serde::{de, Deserialize, Deserializer};
13use tokio::fs;
14
15use tracing::Span;
16use zebra_chain::{
17    common::atomic_write,
18    parameters::{
19        testnet::{
20            self, ConfiguredActivationHeights, ConfiguredCheckpoints, ConfiguredFundingStreams,
21            ConfiguredLockboxDisbursement, RegtestParameters,
22        },
23        Magic, Network, NetworkKind,
24    },
25    work::difficulty::U256,
26};
27
28use crate::{
29    constants::{
30        DEFAULT_CRAWL_NEW_PEER_INTERVAL, DEFAULT_MAX_CONNS_PER_IP,
31        DEFAULT_PEERSET_INITIAL_TARGET_SIZE, DNS_LOOKUP_TIMEOUT, INBOUND_PEER_LIMIT_MULTIPLIER,
32        MAX_PEER_DISK_CACHE_SIZE, OUTBOUND_PEER_LIMIT_MULTIPLIER,
33    },
34    protocol::external::{canonical_peer_addr, canonical_socket_addr},
35    BoxError, PeerSocketAddr,
36};
37
38mod cache_dir;
39
40#[cfg(test)]
41mod tests;
42
43pub use cache_dir::CacheDir;
44
45/// The number of times Zebra will retry each initial peer's DNS resolution,
46/// before checking if any other initial peers have returned addresses.
47///
48/// After doing this number of retries of a failed single peer, Zebra will
49/// check if it has enough peer addresses from other seed peers. If it has
50/// enough addresses, it won't retry this peer again.
51///
52/// If the number of retries is `0`, other peers are checked after every successful
53/// or failed DNS attempt.
54const MAX_SINGLE_SEED_PEER_DNS_RETRIES: usize = 0;
55
56/// Configuration for networking code.
57#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
58#[serde(deny_unknown_fields, default, into = "DConfig")]
59pub struct Config {
60    /// The address on which this node should listen for connections.
61    ///
62    /// Can be `address:port` or just `address`. If there is no configured
63    /// port, Zebra will use the default port for the configured `network`.
64    ///
65    /// `address` can be an IP address or a DNS name. DNS names are
66    /// only resolved once, when Zebra starts up.
67    ///
68    /// By default, Zebra listens on `[::]` (all IPv6 and IPv4 addresses).
69    /// This enables dual-stack support, accepting both IPv4 and IPv6 connections.
70    ///
71    /// If a specific listener address is configured, Zebra will advertise
72    /// it to other nodes. But by default, Zebra uses an unspecified address
73    /// ("\[::\]:port"), which is not advertised to other nodes.
74    ///
75    /// Zebra does not currently support:
76    /// - [Advertising a different external IP address #1890](https://github.com/ZcashFoundation/zebra/issues/1890), or
77    /// - [Auto-discovering its own external IP address #1893](https://github.com/ZcashFoundation/zebra/issues/1893).
78    ///
79    /// However, other Zebra instances compensate for unspecified or incorrect
80    /// listener addresses by adding the external IP addresses of peers to
81    /// their address books.
82    pub listen_addr: SocketAddr,
83
84    /// The external address of this node if any.
85    ///
86    /// Zebra bind to `listen_addr` but this can be an internal address if the node
87    /// is behind a firewall, load balancer or NAT. This field can be used to
88    /// advertise a different address to peers making it possible to receive inbound
89    /// connections and contribute to the P2P network from behind a firewall, load balancer, or NAT.
90    pub external_addr: Option<SocketAddr>,
91
92    /// The network to connect to.
93    pub network: Network,
94
95    /// A list of initial peers for the peerset when operating on
96    /// mainnet.
97    pub initial_mainnet_peers: IndexSet<String>,
98
99    /// A list of initial peers for the peerset when operating on
100    /// testnet.
101    pub initial_testnet_peers: IndexSet<String>,
102
103    /// An optional root directory for storing cached peer address data.
104    ///
105    /// # Configuration
106    ///
107    /// Set to:
108    /// - `true` to read and write peer addresses to disk using the default cache path,
109    /// - `false` to disable reading and writing peer addresses to disk,
110    /// - `'/custom/cache/directory'` to read and write peer addresses to a custom directory.
111    ///
112    /// By default, all Zebra instances run by the same user will share a single peer cache.
113    /// If you use a custom cache path, you might also want to change `state.cache_dir`.
114    ///
115    /// # Functionality
116    ///
117    /// The peer cache is a list of the addresses of some recently useful peers.
118    ///
119    /// For privacy reasons, the cache does *not* include any other information about peers,
120    /// such as when they were connected to the node.
121    ///
122    /// Deleting or modifying the peer cache can impact your node's:
123    /// - reliability: if DNS or the Zcash DNS seeders are unavailable or broken
124    /// - security: if DNS is compromised with malicious peers
125    ///
126    /// If you delete it, Zebra will replace it with a fresh set of peers from the DNS seeders.
127    ///
128    /// # Defaults
129    ///
130    /// The default directory is platform dependent, based on
131    /// [`dirs::cache_dir()`](https://docs.rs/dirs/3.0.1/dirs/fn.cache_dir.html):
132    ///
133    /// |Platform | Value                                           | Example                              |
134    /// | ------- | ----------------------------------------------- | ------------------------------------ |
135    /// | Linux   | `$XDG_CACHE_HOME/zebra` or `$HOME/.cache/zebra` | `/home/alice/.cache/zebra`           |
136    /// | macOS   | `$HOME/Library/Caches/zebra`                    | `/Users/Alice/Library/Caches/zebra`  |
137    /// | Windows | `{FOLDERID_LocalAppData}\zebra`                 | `C:\Users\Alice\AppData\Local\zebra` |
138    /// | Other   | `std::env::current_dir()/cache/zebra`           | `/cache/zebra`                       |
139    ///
140    /// # Security
141    ///
142    /// If you are running Zebra with elevated permissions ("root"), create the
143    /// directory for this file before running Zebra, and make sure the Zebra user
144    /// account has exclusive access to that directory, and other users can't modify
145    /// its parent directories.
146    ///
147    /// # Implementation Details
148    ///
149    /// Each network has a separate peer list, which is updated regularly from the current
150    /// address book. These lists are stored in `network/mainnet.peers` and
151    /// `network/testnet.peers` files, underneath the `cache_dir` path.
152    ///
153    /// Previous peer lists are automatically loaded at startup, and used to populate the
154    /// initial peer set and address book.
155    pub cache_dir: CacheDir,
156
157    /// The initial target size for the peer set.
158    ///
159    /// Also used to limit the number of inbound and outbound connections made by Zebra,
160    /// and the size of the cached peer list.
161    ///
162    /// If you have a slow network connection, and Zebra is having trouble
163    /// syncing, try reducing the peer set size. You can also reduce the peer
164    /// set size to reduce Zebra's bandwidth usage.
165    pub peerset_initial_target_size: usize,
166
167    /// How frequently we attempt to crawl the network to discover new peer
168    /// addresses.
169    ///
170    /// Zebra asks its connected peers for more peer addresses:
171    /// - regularly, every time `crawl_new_peer_interval` elapses, and
172    /// - if the peer set is busy, and there aren't any peer addresses for the
173    ///   next connection attempt.
174    #[serde(with = "humantime_serde")]
175    pub crawl_new_peer_interval: Duration,
176
177    /// The maximum number of peer connections Zebra will keep for a given IP address
178    /// before it drops any additional peer connections with that IP.
179    ///
180    /// The default and minimum value are 1.
181    ///
182    /// # Security
183    ///
184    /// Increasing this config above 1 reduces Zebra's network security.
185    ///
186    /// If this config is greater than 1, Zebra can initiate multiple outbound handshakes to the same
187    /// IP address.
188    ///
189    /// This config does not currently limit the number of inbound connections that Zebra will accept
190    /// from the same IP address.
191    ///
192    /// If Zebra makes multiple inbound or outbound connections to the same IP, they will be dropped
193    /// after the handshake, but before adding them to the peer set. The total numbers of inbound and
194    /// outbound connections are also limited to a multiple of `peerset_initial_target_size`.
195    pub max_connections_per_ip: usize,
196}
197
198impl Config {
199    /// The maximum number of outbound connections that Zebra will open at the same time.
200    /// When this limit is reached, Zebra stops opening outbound connections.
201    ///
202    /// # Security
203    ///
204    /// See the note at [`INBOUND_PEER_LIMIT_MULTIPLIER`].
205    ///
206    /// # Performance
207    ///
208    /// Zebra's peer set should be limited to a reasonable size,
209    /// to avoid queueing too many in-flight block downloads.
210    /// A large queue of in-flight block downloads can choke a
211    /// constrained local network connection.
212    ///
213    /// We assume that Zebra nodes have at least 10 Mbps bandwidth.
214    /// Therefore, a maximum-sized block can take up to 2 seconds to
215    /// download. So the initial outbound peer set adds up to 100 seconds worth
216    /// of blocks to the queue. If Zebra has reached its outbound peer limit,
217    /// that adds an extra 200 seconds of queued blocks.
218    ///
219    /// But the peer set for slow nodes is typically much smaller, due to
220    /// the handshake RTT timeout. And Zebra responds to inbound request
221    /// overloads by dropping peer connections.
222    pub fn peerset_outbound_connection_limit(&self) -> usize {
223        self.peerset_initial_target_size * OUTBOUND_PEER_LIMIT_MULTIPLIER
224    }
225
226    /// The maximum number of inbound connections that Zebra will accept at the same time.
227    /// When this limit is reached, Zebra drops new inbound connections,
228    /// without handshaking on them.
229    ///
230    /// # Security
231    ///
232    /// See the note at [`INBOUND_PEER_LIMIT_MULTIPLIER`].
233    pub fn peerset_inbound_connection_limit(&self) -> usize {
234        self.peerset_initial_target_size * INBOUND_PEER_LIMIT_MULTIPLIER
235    }
236
237    /// The maximum number of inbound and outbound connections that Zebra will have
238    /// at the same time.
239    pub fn peerset_total_connection_limit(&self) -> usize {
240        self.peerset_outbound_connection_limit() + self.peerset_inbound_connection_limit()
241    }
242
243    /// Returns the initial seed peer hostnames for the configured network.
244    pub fn initial_peer_hostnames(&self) -> IndexSet<String> {
245        match &self.network {
246            Network::Mainnet => self.initial_mainnet_peers.clone(),
247            Network::Testnet(_params) => self.initial_testnet_peers.clone(),
248        }
249    }
250
251    /// Resolve initial seed peer IP addresses, based on the configured network,
252    /// and load cached peers from disk, if available.
253    ///
254    /// # Panics
255    ///
256    /// If a configured address is an invalid [`SocketAddr`] or DNS name.
257    pub async fn initial_peers(&self) -> HashSet<PeerSocketAddr> {
258        // TODO: do DNS and disk in parallel if startup speed becomes important
259        let dns_peers =
260            Config::resolve_peers(&self.initial_peer_hostnames().iter().cloned().collect()).await;
261
262        if self.network.is_regtest() {
263            // Only return local peer addresses and skip loading the peer cache on Regtest.
264            dns_peers
265                .into_iter()
266                .filter(PeerSocketAddr::is_localhost)
267                .collect()
268        } else {
269            // Ignore disk errors because the cache is optional and the method already logs them.
270            let disk_peers = self.load_peer_cache().await.unwrap_or_default();
271
272            dns_peers.into_iter().chain(disk_peers).collect()
273        }
274    }
275
276    /// Concurrently resolves `peers` into zero or more IP addresses, with a
277    /// timeout of a few seconds on each DNS request.
278    ///
279    /// If DNS resolution fails or times out for all peers, continues retrying
280    /// until at least one peer is found.
281    async fn resolve_peers(peers: &HashSet<String>) -> HashSet<PeerSocketAddr> {
282        use futures::stream::StreamExt;
283
284        if peers.is_empty() {
285            warn!(
286                "no initial peers in the network config. \
287                 Hint: you must configure at least one peer IP or DNS seeder to run Zebra, \
288                 give it some previously cached peer IP addresses on disk, \
289                 or make sure Zebra's listener port gets inbound connections."
290            );
291            return HashSet::new();
292        }
293
294        loop {
295            // We retry each peer individually, as well as retrying if there are
296            // no peers in the combined list. DNS failures are correlated, so all
297            // peers can fail DNS, leaving Zebra with a small list of custom IP
298            // address peers. Individual retries avoid this issue.
299            let peer_addresses = peers
300                .iter()
301                .map(|s| Config::resolve_host(s, MAX_SINGLE_SEED_PEER_DNS_RETRIES))
302                .collect::<futures::stream::FuturesUnordered<_>>()
303                .concat()
304                .await;
305
306            if peer_addresses.is_empty() {
307                tracing::info!(
308                    ?peers,
309                    ?peer_addresses,
310                    "empty peer list after DNS resolution, retrying after {} seconds",
311                    DNS_LOOKUP_TIMEOUT.as_secs(),
312                );
313                tokio::time::sleep(DNS_LOOKUP_TIMEOUT).await;
314            } else {
315                return peer_addresses;
316            }
317        }
318    }
319
320    /// Resolves `host` into zero or more IP addresses, retrying up to
321    /// `max_retries` times.
322    ///
323    /// If DNS continues to fail, returns an empty list of addresses.
324    ///
325    /// # Panics
326    ///
327    /// If a configured address is an invalid [`SocketAddr`] or DNS name.
328    async fn resolve_host(host: &str, max_retries: usize) -> HashSet<PeerSocketAddr> {
329        for retries in 0..=max_retries {
330            if let Ok(addresses) = Config::resolve_host_once(host).await {
331                return addresses;
332            }
333
334            if retries < max_retries {
335                tracing::info!(
336                    ?host,
337                    previous_attempts = ?(retries + 1),
338                    "Waiting {DNS_LOOKUP_TIMEOUT:?} to retry seed peer DNS resolution",
339                );
340                tokio::time::sleep(DNS_LOOKUP_TIMEOUT).await;
341            } else {
342                tracing::info!(
343                    ?host,
344                    attempts = ?(retries + 1),
345                    "Seed peer DNS resolution failed, checking for addresses from other seed peers",
346                );
347            }
348        }
349
350        HashSet::new()
351    }
352
353    /// Resolves `host` into zero or more IP addresses.
354    ///
355    /// If `host` is a DNS name, performs DNS resolution with a timeout of a few seconds.
356    /// If DNS resolution fails or times out, returns an error.
357    ///
358    /// # Panics
359    ///
360    /// If a configured address is an invalid [`SocketAddr`] or DNS name.
361    async fn resolve_host_once(host: &str) -> Result<HashSet<PeerSocketAddr>, BoxError> {
362        let fut = tokio::net::lookup_host(host);
363        let fut = tokio::time::timeout(DNS_LOOKUP_TIMEOUT, fut);
364
365        match fut.await {
366            Ok(Ok(ip_addrs)) => {
367                let ip_addrs: Vec<PeerSocketAddr> = ip_addrs.map(canonical_peer_addr).collect();
368
369                // This log is needed for user debugging, but it's annoying during tests.
370                #[cfg(not(test))]
371                info!(seed = ?host, remote_ip_count = ?ip_addrs.len(), "resolved seed peer IP addresses");
372                #[cfg(test)]
373                debug!(seed = ?host, remote_ip_count = ?ip_addrs.len(), "resolved seed peer IP addresses");
374
375                for ip in &ip_addrs {
376                    // Count each initial peer, recording the seed config and resolved IP address.
377                    //
378                    // If an IP is returned by multiple seeds,
379                    // each duplicate adds 1 to the initial peer count.
380                    // (But we only make one initial connection attempt to each IP.)
381                    metrics::counter!(
382                        "zcash.net.peers.initial",
383                        "seed" => host.to_string(),
384                        "remote_ip" => ip.to_string()
385                    )
386                    .increment(1);
387                }
388
389                Ok(ip_addrs.into_iter().collect())
390            }
391            Ok(Err(e)) if e.kind() == ErrorKind::InvalidInput => {
392                // TODO: add testnet/mainnet ports, like we do with the listener address
393                panic!(
394                    "Invalid peer IP address in Zebra config: addresses must have ports:\n\
395                     resolving {host:?} returned {e:?}"
396                );
397            }
398            Ok(Err(e)) => {
399                tracing::info!(?host, ?e, "DNS error resolving peer IP addresses");
400                Err(e.into())
401            }
402            Err(e) => {
403                tracing::info!(?host, ?e, "DNS timeout resolving peer IP addresses");
404                Err(e.into())
405            }
406        }
407    }
408
409    /// Returns the addresses in the peer list cache file, if available.
410    pub async fn load_peer_cache(&self) -> io::Result<HashSet<PeerSocketAddr>> {
411        let Some(peer_cache_file) = self.cache_dir.peer_cache_file_path(&self.network) else {
412            return Ok(HashSet::new());
413        };
414
415        let peer_list = match fs::read_to_string(&peer_cache_file).await {
416            Ok(peer_list) => peer_list,
417            Err(peer_list_error) => {
418                // We expect that the cache will be missing for new Zebra installs
419                if peer_list_error.kind() == ErrorKind::NotFound {
420                    return Ok(HashSet::new());
421                } else {
422                    info!(
423                        ?peer_list_error,
424                        "could not load cached peer list, using default seed peers"
425                    );
426                    return Err(peer_list_error);
427                }
428            }
429        };
430
431        // Skip and log addresses that don't parse, and automatically deduplicate using the HashSet.
432        // (These issues shouldn't happen unless users modify the file.)
433        let peer_list: HashSet<PeerSocketAddr> = peer_list
434            .lines()
435            .filter_map(|peer| {
436                peer.parse()
437                    .map_err(|peer_parse_error| {
438                        info!(
439                            ?peer_parse_error,
440                            "invalid peer address in cached peer list, skipping"
441                        );
442                        peer_parse_error
443                    })
444                    .ok()
445            })
446            .collect();
447
448        // This log is needed for user debugging, but it's annoying during tests.
449        #[cfg(not(test))]
450        info!(
451            cached_ip_count = ?peer_list.len(),
452            ?peer_cache_file,
453            "loaded cached peer IP addresses"
454        );
455        #[cfg(test)]
456        debug!(
457            cached_ip_count = ?peer_list.len(),
458            ?peer_cache_file,
459            "loaded cached peer IP addresses"
460        );
461
462        for ip in &peer_list {
463            // Count each initial peer, recording the cache file and loaded IP address.
464            //
465            // If an IP is returned by DNS seeders and the cache,
466            // each duplicate adds 1 to the initial peer count.
467            // (But we only make one initial connection attempt to each IP.)
468            metrics::counter!(
469                "zcash.net.peers.initial",
470                "cache" => peer_cache_file.display().to_string(),
471                "remote_ip" => ip.to_string()
472            )
473            .increment(1);
474        }
475
476        Ok(peer_list)
477    }
478
479    /// Atomically writes a new `peer_list` to the peer list cache file, if configured.
480    /// If the list is empty, keeps the previous cache file.
481    ///
482    /// Also creates the peer cache directory, if it doesn't already exist.
483    ///
484    /// Atomic writes avoid corrupting the cache if Zebra panics or crashes, or if multiple Zebra
485    /// instances try to read and write the same cache file.
486    pub async fn update_peer_cache(&self, peer_list: HashSet<PeerSocketAddr>) -> io::Result<()> {
487        let Some(peer_cache_file) = self.cache_dir.peer_cache_file_path(&self.network) else {
488            return Ok(());
489        };
490
491        if peer_list.is_empty() {
492            info!(
493                ?peer_cache_file,
494                "cacheable peer list was empty, keeping previous cache"
495            );
496            return Ok(());
497        }
498
499        // Turn IP addresses into strings
500        let mut peer_list: Vec<String> = peer_list
501            .iter()
502            .take(MAX_PEER_DISK_CACHE_SIZE)
503            .map(|redacted_peer| redacted_peer.remove_socket_addr_privacy().to_string())
504            .collect();
505        // # Privacy
506        //
507        // Sort to destroy any peer order, which could leak peer connection times.
508        // (Currently the HashSet argument does this as well.)
509        peer_list.sort();
510        // Make a newline-separated list
511        let peer_data = peer_list.join("\n");
512
513        // Write the peer cache file atomically so the cache is not corrupted if Zebra shuts down
514        // or crashes.
515        let span = Span::current();
516        let write_result = tokio::task::spawn_blocking(move || {
517            span.in_scope(move || atomic_write(peer_cache_file, peer_data.as_bytes()))
518        })
519        .await
520        .expect("could not write the peer cache file")?;
521
522        match write_result {
523            Ok(peer_cache_file) => {
524                info!(
525                    cached_ip_count = ?peer_list.len(),
526                    ?peer_cache_file,
527                    "updated cached peer IP addresses"
528                );
529
530                for ip in &peer_list {
531                    metrics::counter!(
532                        "zcash.net.peers.cache",
533                        "cache" => peer_cache_file.display().to_string(),
534                        "remote_ip" => ip.to_string()
535                    )
536                    .increment(1);
537                }
538
539                Ok(())
540            }
541            Err(error) => Err(error.error),
542        }
543    }
544}
545
546impl Default for Config {
547    fn default() -> Config {
548        let mainnet_peers = [
549            "dnsseed.str4d.xyz:8233",
550            "dnsseed.z.cash:8233",
551            "mainnet.seeder.shieldedinfra.net:8233",
552            "mainnet.seeder.zfnd.org:8233",
553            "seeder.zec.rocks:8233",
554        ]
555        .iter()
556        .map(|&s| String::from(s))
557        .collect();
558
559        let testnet_peers = [
560            "dnsseed.testnet.z.cash:18233",
561            "seeder.testnet.zec.rocks:18233",
562            "testnet.seeder.zfnd.org:18233",
563        ]
564        .iter()
565        .map(|&s| String::from(s))
566        .collect();
567
568        Config {
569            listen_addr: "[::]:8233"
570                .parse()
571                .expect("Hardcoded address should be parseable"),
572            external_addr: None,
573            network: Network::Mainnet,
574            initial_mainnet_peers: mainnet_peers,
575            initial_testnet_peers: testnet_peers,
576            cache_dir: CacheDir::default(),
577            crawl_new_peer_interval: DEFAULT_CRAWL_NEW_PEER_INTERVAL,
578
579            // # Security
580            //
581            // The default peerset target size should be large enough to ensure
582            // nodes have a reliable set of peers.
583            //
584            // But Zebra should only make a small number of initial outbound connections,
585            // so that idle peers don't use too many connection slots.
586            peerset_initial_target_size: DEFAULT_PEERSET_INITIAL_TARGET_SIZE,
587            max_connections_per_ip: DEFAULT_MAX_CONNS_PER_IP,
588        }
589    }
590}
591
592#[derive(Serialize, Deserialize)]
593#[serde(deny_unknown_fields)]
594struct DTestnetParameters {
595    network_name: Option<String>,
596    network_magic: Option<[u8; 4]>,
597    slow_start_interval: Option<u32>,
598    target_difficulty_limit: Option<String>,
599    disable_pow: Option<bool>,
600    genesis_hash: Option<String>,
601    activation_heights: Option<ConfiguredActivationHeights>,
602    pre_nu6_funding_streams: Option<ConfiguredFundingStreams>,
603    post_nu6_funding_streams: Option<ConfiguredFundingStreams>,
604    funding_streams: Option<Vec<ConfiguredFundingStreams>>,
605    pre_blossom_halving_interval: Option<u32>,
606    lockbox_disbursements: Option<Vec<ConfiguredLockboxDisbursement>>,
607    #[serde(default)]
608    checkpoints: ConfiguredCheckpoints,
609    /// If `true`, automatically repeats configured funding stream addresses to fill
610    /// all required periods.
611    extend_funding_stream_addresses_as_required: Option<bool>,
612    /// Height at which the soft fork that temporarily disables Orchard actions activates.
613    ///
614    /// If unset, the default activation height for the network is used; the soft fork
615    /// cannot be disabled via configuration.
616    temporary_orchard_disabling_soft_fork_height: Option<u32>,
617    /// Regtest only: whether to allow coinbase spends to have transparent outputs.
618    should_allow_unshielded_coinbase_spends: Option<bool>,
619}
620
621/// Network configuration used during deserialization.
622#[derive(Serialize, Deserialize)]
623#[serde(untagged)]
624enum DNetwork {
625    DefaultForKind(NetworkKind),
626    ConfiguredRegtest {
627        params: Box<DTestnetParameters>,
628
629        #[serde(default, skip_serializing)]
630        regtest: Option<bool>,
631    },
632    ConfiguredTestnet(Box<DTestnetParameters>),
633}
634
635impl Default for DNetwork {
636    fn default() -> Self {
637        DNetwork::DefaultForKind(NetworkKind::Mainnet)
638    }
639}
640
641#[derive(Serialize, Deserialize)]
642#[serde(deny_unknown_fields, default)]
643struct DConfig {
644    listen_addr: String,
645    external_addr: Option<String>,
646    network: DNetwork,
647
648    /// Legacy testnet parameters, kept for backwards compatibility.
649    #[serde(default, skip_serializing_if = "Option::is_none")]
650    testnet_parameters: Option<DTestnetParameters>,
651
652    initial_mainnet_peers: IndexSet<String>,
653    initial_testnet_peers: IndexSet<String>,
654    cache_dir: CacheDir,
655    peerset_initial_target_size: usize,
656    #[serde(alias = "new_peer_interval", with = "humantime_serde")]
657    crawl_new_peer_interval: Duration,
658    max_connections_per_ip: Option<usize>,
659}
660
661impl Default for DConfig {
662    fn default() -> Self {
663        let config = Config::default();
664        Self {
665            listen_addr: "[::]".to_string(),
666            external_addr: None,
667            network: Default::default(),
668            testnet_parameters: None,
669            initial_mainnet_peers: config.initial_mainnet_peers,
670            initial_testnet_peers: config.initial_testnet_peers,
671            cache_dir: config.cache_dir,
672            peerset_initial_target_size: config.peerset_initial_target_size,
673            crawl_new_peer_interval: config.crawl_new_peer_interval,
674            max_connections_per_ip: Some(config.max_connections_per_ip),
675        }
676    }
677}
678
679impl From<Arc<testnet::Parameters>> for DTestnetParameters {
680    fn from(params: Arc<testnet::Parameters>) -> Self {
681        Self {
682            network_name: Some(params.network_name().to_string()),
683            network_magic: Some(params.network_magic().0),
684            slow_start_interval: Some(params.slow_start_interval().0),
685            target_difficulty_limit: Some(params.target_difficulty_limit().to_string()),
686            disable_pow: Some(params.disable_pow()),
687            genesis_hash: Some(params.genesis_hash().to_string()),
688            activation_heights: Some(params.activation_heights().into()),
689            pre_nu6_funding_streams: None,
690            post_nu6_funding_streams: None,
691            funding_streams: Some(params.funding_streams().iter().map(Into::into).collect()),
692            pre_blossom_halving_interval: Some(
693                params
694                    .pre_blossom_halving_interval()
695                    .try_into()
696                    .expect("should convert"),
697            ),
698            lockbox_disbursements: Some(
699                params
700                    .lockbox_disbursements()
701                    .into_iter()
702                    .map(Into::into)
703                    .collect(),
704            ),
705            checkpoints: if params.checkpoints() == testnet::Parameters::default().checkpoints() {
706                ConfiguredCheckpoints::Default(true)
707            } else {
708                params.checkpoints().into()
709            },
710            extend_funding_stream_addresses_as_required: None,
711            temporary_orchard_disabling_soft_fork_height: params
712                .temporary_orchard_disabling_soft_fork_height()
713                .map(|height| height.0),
714            should_allow_unshielded_coinbase_spends: params
715                .is_regtest()
716                .then(|| params.should_allow_unshielded_coinbase_spends()),
717        }
718    }
719}
720
721impl From<Config> for DConfig {
722    fn from(
723        Config {
724            listen_addr,
725            external_addr,
726            network,
727            initial_mainnet_peers,
728            initial_testnet_peers,
729            cache_dir,
730            peerset_initial_target_size,
731            crawl_new_peer_interval,
732            max_connections_per_ip,
733        }: Config,
734    ) -> Self {
735        let dnetwork = match network.kind() {
736            NetworkKind::Testnet => match network
737                .parameters()
738                .filter(|params| !params.is_default_testnet())
739                .map(Into::into)
740            {
741                Some(params) => DNetwork::ConfiguredTestnet(Box::new(params)),
742                None => DNetwork::DefaultForKind(NetworkKind::Testnet),
743            },
744
745            NetworkKind::Regtest => match network.parameters().map(Into::into) {
746                Some(params) => DNetwork::ConfiguredRegtest {
747                    params: Box::new(params),
748                    regtest: Some(true),
749                },
750                None => DNetwork::DefaultForKind(NetworkKind::Regtest),
751            },
752
753            other_kind => DNetwork::DefaultForKind(other_kind),
754        };
755
756        DConfig {
757            listen_addr: listen_addr.to_string(),
758            external_addr: external_addr.map(|addr| addr.to_string()),
759            network: dnetwork,
760            testnet_parameters: None,
761            initial_mainnet_peers,
762            initial_testnet_peers,
763            cache_dir,
764            peerset_initial_target_size,
765            crawl_new_peer_interval,
766            max_connections_per_ip: Some(max_connections_per_ip),
767        }
768    }
769}
770
771impl<'de> Deserialize<'de> for Config {
772    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
773    where
774        D: Deserializer<'de>,
775    {
776        let DConfig {
777            listen_addr,
778            external_addr,
779            network: dnetwork,
780            testnet_parameters,
781            initial_mainnet_peers,
782            initial_testnet_peers,
783            cache_dir,
784            peerset_initial_target_size,
785            crawl_new_peer_interval,
786            max_connections_per_ip,
787        } = DConfig::deserialize(deserializer)?;
788
789        let network = match (dnetwork, testnet_parameters) {
790            (DNetwork::ConfiguredTestnet(params), _) => {
791                build_configured_testnet::<D>(*params, &initial_testnet_peers)?
792            }
793            (DNetwork::ConfiguredRegtest { params, .. }, _) => {
794                Network::new_regtest(build_regtest_params(*params))
795            }
796            (DNetwork::DefaultForKind(NetworkKind::Mainnet), _) => Network::Mainnet,
797            (DNetwork::DefaultForKind(NetworkKind::Testnet), Some(params)) => {
798                build_configured_testnet::<D>(params, &initial_testnet_peers)?
799            }
800            (DNetwork::DefaultForKind(NetworkKind::Testnet), None) => {
801                Network::new_default_testnet()
802            }
803            (DNetwork::DefaultForKind(NetworkKind::Regtest), Some(params)) => {
804                Network::new_regtest(build_regtest_params(params))
805            }
806            (DNetwork::DefaultForKind(NetworkKind::Regtest), None) => {
807                Network::new_regtest(Default::default())
808            }
809        };
810
811        let listen_addr = match listen_addr.parse::<SocketAddr>().or_else(|_| format!("{listen_addr}:{}", network.default_port()).parse()) {
812            Ok(socket) => Ok(socket),
813            Err(_) => match listen_addr.parse::<IpAddr>() {
814                Ok(ip) => Ok(SocketAddr::new(ip, network.default_port())),
815                Err(err) => Err(de::Error::custom(format!(
816                    "{err}; Hint: addresses can be a IPv4, IPv6 (with brackets), or a DNS name, the port is optional"
817                ))),
818            },
819        }?;
820
821        let external_socket_addr = if let Some(address) = &external_addr {
822            match address.parse::<SocketAddr>().or_else(|_| format!("{address}:{}", network.default_port()).parse()) {
823                Ok(socket) => Ok(Some(socket)),
824                Err(_) => match address.parse::<IpAddr>() {
825                    Ok(ip) => Ok(Some(SocketAddr::new(ip, network.default_port()))),
826                    Err(err) => Err(de::Error::custom(format!(
827                        "{err}; Hint: addresses can be a IPv4, IPv6 (with brackets), or a DNS name, the port is optional"
828                    ))),
829                },
830            }?
831        } else {
832            None
833        };
834
835        let [max_connections_per_ip, peerset_initial_target_size] = [
836            ("max_connections_per_ip", max_connections_per_ip, DEFAULT_MAX_CONNS_PER_IP),
837            // If we want Zebra to operate with no network,
838            // we should implement a `zebrad` command that doesn't use `zebra-network`.
839            ("peerset_initial_target_size", Some(peerset_initial_target_size), DEFAULT_PEERSET_INITIAL_TARGET_SIZE)
840        ].map(|(field_name, non_zero_config_field, default_config_value)| {
841            if non_zero_config_field == Some(0) {
842                warn!(
843                    ?field_name,
844                    ?non_zero_config_field,
845                    "{field_name} should be greater than 0, using default value of {default_config_value} instead"
846                );
847            }
848
849            non_zero_config_field.filter(|config_value| config_value > &0).unwrap_or(default_config_value)
850        });
851
852        Ok(Config {
853            listen_addr: canonical_socket_addr(listen_addr),
854            external_addr: external_socket_addr,
855            network,
856            initial_mainnet_peers,
857            initial_testnet_peers,
858            cache_dir,
859            peerset_initial_target_size,
860            crawl_new_peer_interval,
861            max_connections_per_ip,
862        })
863    }
864}
865
866/// Accepts an [`IndexSet`] of initial peers,
867///
868/// Returns true if any of them are the default Testnet or Mainnet initial peers.
869fn contains_default_initial_peers(initial_peers: &IndexSet<String>) -> bool {
870    let Config {
871        initial_mainnet_peers: mut default_initial_peers,
872        initial_testnet_peers: default_initial_testnet_peers,
873        ..
874    } = Config::default();
875    default_initial_peers.extend(default_initial_testnet_peers);
876
877    initial_peers
878        .intersection(&default_initial_peers)
879        .next()
880        .is_some()
881}
882
883fn build_configured_testnet<'de, D>(
884    params: DTestnetParameters,
885    initial_testnet_peers: &IndexSet<String>,
886) -> Result<Network, D::Error>
887where
888    D: Deserializer<'de>,
889{
890    let DTestnetParameters {
891        network_name,
892        network_magic,
893        slow_start_interval,
894        target_difficulty_limit,
895        disable_pow,
896        genesis_hash,
897        activation_heights,
898        pre_nu6_funding_streams,
899        post_nu6_funding_streams,
900        funding_streams,
901        pre_blossom_halving_interval,
902        lockbox_disbursements,
903        checkpoints,
904        extend_funding_stream_addresses_as_required,
905        temporary_orchard_disabling_soft_fork_height,
906        should_allow_unshielded_coinbase_spends,
907    } = params;
908
909    // This is a Regtest-only consensus knob, so reject it rather than silently ignoring it.
910    if should_allow_unshielded_coinbase_spends.is_some() {
911        return Err(de::Error::custom(
912            "should_allow_unshielded_coinbase_spends is only supported on Regtest",
913        ));
914    }
915
916    let mut params_builder = testnet::Parameters::build();
917
918    if let Some(network_name) = network_name.clone() {
919        params_builder = params_builder
920            .with_network_name(network_name)
921            .map_err(de::Error::custom)?
922    }
923
924    if let Some(network_magic) = network_magic {
925        params_builder = params_builder
926            .with_network_magic(Magic(network_magic))
927            .map_err(de::Error::custom)?;
928    }
929
930    if let Some(genesis_hash) = genesis_hash {
931        params_builder = params_builder
932            .with_genesis_hash(genesis_hash)
933            .map_err(de::Error::custom)?;
934    }
935
936    if let Some(slow_start_interval) = slow_start_interval {
937        params_builder = params_builder
938            .with_slow_start_interval(slow_start_interval.try_into().map_err(de::Error::custom)?);
939    }
940
941    if let Some(target_difficulty_limit) = target_difficulty_limit.clone() {
942        params_builder = params_builder
943            .with_target_difficulty_limit(
944                target_difficulty_limit
945                    .parse::<U256>()
946                    .map_err(de::Error::custom)?,
947            )
948            .map_err(de::Error::custom)?;
949    }
950
951    if let Some(disable_pow) = disable_pow {
952        params_builder = params_builder.with_disable_pow(disable_pow);
953    }
954
955    // Retain default Testnet activation heights unless there's an empty [testnet_parameters.activation_heights] section.
956    if let Some(activation_heights) = activation_heights {
957        params_builder = params_builder
958            .with_activation_heights(activation_heights)
959            .map_err(de::Error::custom)?
960    }
961
962    if let Some(halving_interval) = pre_blossom_halving_interval {
963        params_builder = params_builder
964            .with_halving_interval(halving_interval.into())
965            .map_err(de::Error::custom)?
966    }
967
968    // Set configured funding streams after setting any parameters that affect the funding stream address period.
969    let mut funding_streams_vec = funding_streams.unwrap_or_default();
970
971    if let Some(funding_streams) = post_nu6_funding_streams {
972        funding_streams_vec.insert(0, funding_streams);
973    }
974
975    if let Some(funding_streams) = pre_nu6_funding_streams {
976        funding_streams_vec.insert(0, funding_streams);
977    }
978
979    if !funding_streams_vec.is_empty() {
980        params_builder = params_builder.with_funding_streams(funding_streams_vec);
981    }
982
983    if let Some(lockbox_disbursements) = lockbox_disbursements {
984        params_builder = params_builder.with_lockbox_disbursements(lockbox_disbursements);
985    }
986
987    params_builder = params_builder
988        .with_checkpoints(checkpoints)
989        .map_err(de::Error::custom)?;
990
991    if let Some(true) = extend_funding_stream_addresses_as_required {
992        params_builder = params_builder.extend_funding_streams();
993    }
994
995    // Retain the default soft-fork activation height unless one is configured.
996    if let Some(height) = temporary_orchard_disabling_soft_fork_height {
997        params_builder = params_builder.with_temporary_orchard_disabling_soft_fork_height(
998            height.try_into().map_err(de::Error::custom)?,
999        );
1000    }
1001
1002    // Return an error if the initial testnet peers includes any of the default initial Mainnet or Testnet
1003    // peers and the configured network parameters are incompatible with the default public Testnet.
1004    if !params_builder.is_compatible_with_default_parameters()
1005        && contains_default_initial_peers(initial_testnet_peers)
1006    {
1007        return Err(de::Error::custom(
1008            "cannot use default initials peers with incompatible testnet",
1009        ));
1010    };
1011
1012    // Return the default Testnet if no network name was configured and all parameters match the default Testnet
1013    if network_name.is_none() && params_builder == testnet::Parameters::build() {
1014        Ok(Network::new_default_testnet())
1015    } else {
1016        Ok(params_builder.to_network().map_err(de::Error::custom)?)
1017    }
1018}
1019
1020fn build_regtest_params(params: DTestnetParameters) -> RegtestParameters {
1021    let DTestnetParameters {
1022        activation_heights,
1023        pre_nu6_funding_streams,
1024        post_nu6_funding_streams,
1025        funding_streams,
1026        lockbox_disbursements,
1027        checkpoints,
1028        extend_funding_stream_addresses_as_required,
1029        should_allow_unshielded_coinbase_spends,
1030        ..
1031    } = params;
1032
1033    let mut funding_streams_vec = funding_streams.unwrap_or_default();
1034
1035    if let Some(funding_streams) = post_nu6_funding_streams {
1036        funding_streams_vec.insert(0, funding_streams);
1037    }
1038
1039    if let Some(funding_streams) = pre_nu6_funding_streams {
1040        funding_streams_vec.insert(0, funding_streams);
1041    }
1042
1043    RegtestParameters {
1044        activation_heights: activation_heights.unwrap_or_default(),
1045        funding_streams: Some(funding_streams_vec),
1046        lockbox_disbursements,
1047        checkpoints: Some(checkpoints),
1048        extend_funding_stream_addresses_as_required,
1049        should_allow_unshielded_coinbase_spends,
1050    }
1051}