forked from ZcashFoundation/zebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
125 lines (109 loc) · 4.16 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::{
collections::HashSet,
net::{SocketAddr, ToSocketAddrs},
string::String,
time::Duration,
};
use zebra_chain::parameters::Network;
/// Configuration for networking code.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
/// The address on which this node should listen for connections.
pub listen_addr: SocketAddr,
/// The network to connect to.
pub network: Network,
/// A list of initial peers for the peerset when operating on
/// mainnet.
pub initial_mainnet_peers: HashSet<String>,
/// A list of initial peers for the peerset when operating on
/// testnet.
pub initial_testnet_peers: HashSet<String>,
/// The initial target size for the peer set.
///
/// If you have a slow network connection, and Zebra is having trouble
/// syncing, try reducing the peer set size. You can also reduce the peer
/// set size to reduce Zebra's bandwidth usage.
pub peerset_initial_target_size: usize,
/// How frequently we attempt to connect to a new peer.
pub new_peer_interval: Duration,
}
impl Config {
async fn parse_peers<S: ToSocketAddrs>(peers: HashSet<S>) -> HashSet<SocketAddr> {
// Test dns function
let _test_dns = Config::resolve("willnotresolve.whatev3r:8233".to_string()).await;
peers
.iter()
.flat_map(|s| s.to_socket_addrs())
.flatten()
.collect()
}
/// Get the initial seed peers based on the configured network.
pub async fn initial_peers(&self) -> HashSet<SocketAddr> {
match self.network {
Network::Mainnet => Config::parse_peers(self.initial_mainnet_peers.clone()).await,
Network::Testnet => Config::parse_peers(self.initial_testnet_peers.clone()).await,
}
}
async fn resolve(s: String) -> Option<impl Iterator<Item = SocketAddr>> {
// Resolve using `lookup_host`
let fut = tokio::net::lookup_host(s.clone());
// Add timeout
let fut = tokio::time::timeout(Duration::from_nanos(1), fut);
match fut.await {
Ok(Ok(ips)) => Some(ips),
Ok(Err(e)) => {
tracing::warn!("Can't resolve {:?}. Error: {:?}", s, e);
None
}
Err(e) => {
tracing::warn!("Timeout trying to resolve {:?}: {:?}", s, e);
None
}
}
}
}
impl Default for Config {
fn default() -> Config {
let mainnet_peers = [
"dnsseed.z.cash:8233",
"dnsseed.str4d.xyz:8233",
"mainnet.seeder.zfnd.org:8233",
"mainnet.is.yolo.money:8233",
]
.iter()
.map(|&s| String::from(s))
.collect();
let testnet_peers = [
"dnsseed.testnet.z.cash:18233",
"testnet.seeder.zfnd.org:18233",
"testnet.is.yolo.money:18233",
]
.iter()
.map(|&s| String::from(s))
.collect();
Config {
listen_addr: "0.0.0.0:8233"
.parse()
.expect("Hardcoded address should be parseable"),
network: Network::Mainnet,
initial_mainnet_peers: mainnet_peers,
initial_testnet_peers: testnet_peers,
new_peer_interval: Duration::from_secs(60),
// The default peerset target size should be large enough to ensure
// nodes have a reliable set of peers. But it should also be limited
// to a reasonable size, to avoid queueing too many in-flight block
// downloads. A large queue of in-flight block downloads can choke a
// constrained local network connection.
//
// We assume that Zebra nodes have at least 10 Mbps bandwidth.
// Therefore, a maximum-sized block can take up to 2 seconds to
// download. So a full default peer set adds up to 100 seconds worth
// of blocks to the queue.
//
// But the peer set for slow nodes is typically much smaller, due to
// the handshake RTT timeout.
peerset_initial_target_size: 50,
}
}
}