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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::{dht::DhtInitializationError, outbound::DhtOutboundRequest, DbConnectionUrl, Dht, DhtConfig};
use futures::channel::mpsc;
use std::{sync::Arc, time::Duration};
use tari_comms::{
connectivity::ConnectivityRequester,
peer_manager::{NodeIdentity, PeerManager},
};
use tari_shutdown::ShutdownSignal;
pub struct DhtBuilder {
node_identity: Arc<NodeIdentity>,
peer_manager: Arc<PeerManager>,
config: DhtConfig,
outbound_tx: mpsc::Sender<DhtOutboundRequest>,
connectivity: ConnectivityRequester,
shutdown_signal: ShutdownSignal,
}
impl DhtBuilder {
pub fn new(
node_identity: Arc<NodeIdentity>,
peer_manager: Arc<PeerManager>,
outbound_tx: mpsc::Sender<DhtOutboundRequest>,
connectivity: ConnectivityRequester,
shutdown_signal: ShutdownSignal,
) -> Self
{
Self {
#[cfg(test)]
config: DhtConfig::default_local_test(),
#[cfg(not(test))]
config: Default::default(),
node_identity,
peer_manager,
outbound_tx,
connectivity,
shutdown_signal,
}
}
pub fn with_config(mut self, config: DhtConfig) -> Self {
self.config = config;
self
}
pub fn local_test(mut self) -> Self {
self.config = DhtConfig::default_local_test();
self
}
pub fn set_auto_store_and_forward_requests(mut self, enabled: bool) -> Self {
self.config.saf_auto_request = enabled;
self
}
pub fn testnet(mut self) -> Self {
self.config = DhtConfig::default_testnet();
self
}
pub fn mainnet(mut self) -> Self {
self.config = DhtConfig::default_mainnet();
self
}
pub fn with_database_url(mut self, database_url: DbConnectionUrl) -> Self {
self.config.database_url = database_url;
self
}
pub fn with_signature_cache_ttl(mut self, ttl: Duration) -> Self {
self.config.msg_hash_cache_ttl = ttl;
self
}
pub fn with_signature_cache_capacity(mut self, capacity: usize) -> Self {
self.config.msg_hash_cache_capacity = capacity;
self
}
pub fn with_num_random_nodes(mut self, n: usize) -> Self {
self.config.num_random_nodes = n;
self
}
pub fn with_num_neighbouring_nodes(mut self, n: usize) -> Self {
self.config.num_neighbouring_nodes = n;
self
}
pub fn with_propagation_factor(mut self, propagation_factor: usize) -> Self {
self.config.propagation_factor = propagation_factor;
self
}
pub fn with_broadcast_factor(mut self, broadcast_factor: usize) -> Self {
self.config.broadcast_factor = broadcast_factor;
self
}
pub fn with_discovery_timeout(mut self, timeout: Duration) -> Self {
self.config.discovery_request_timeout = timeout;
self
}
pub fn enable_auto_join(mut self) -> Self {
self.config.auto_join = true;
self
}
pub async fn build(self) -> Result<Dht, DhtInitializationError> {
Dht::initialize(
self.config,
self.node_identity,
self.peer_manager,
self.outbound_tx,
self.connectivity,
self.shutdown_signal,
)
.await
}
}