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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use crate::protocol::{GossipsubMessage, MessageId};
use std::borrow::Cow;
use std::time::Duration;
pub const IDENTITY_SOURCE: [u8; 3] = [0, 1, 0];
#[derive(Clone)]
pub struct GossipsubConfig {
pub protocol_id: Cow<'static, [u8]>,
pub history_length: usize,
pub history_gossip: usize,
pub mesh_n: usize,
pub mesh_n_low: usize,
pub mesh_n_high: usize,
pub gossip_lazy: usize,
pub heartbeat_initial_delay: Duration,
pub heartbeat_interval: Duration,
pub fanout_ttl: Duration,
pub max_transmit_size: usize,
pub hash_topics: bool,
pub no_source_id: bool,
pub manual_propagation: bool,
pub message_id_fn: fn(&GossipsubMessage) -> MessageId,
}
impl Default for GossipsubConfig {
fn default() -> GossipsubConfig {
GossipsubConfig {
protocol_id: Cow::Borrowed(b"/meshsub/1.0.0"),
history_length: 5,
history_gossip: 3,
mesh_n: 6,
mesh_n_low: 4,
mesh_n_high: 12,
gossip_lazy: 6,
heartbeat_initial_delay: Duration::from_secs(5),
heartbeat_interval: Duration::from_secs(1),
fanout_ttl: Duration::from_secs(60),
max_transmit_size: 2048,
hash_topics: false,
no_source_id: false,
manual_propagation: false,
message_id_fn: |message| {
let mut source_string = message.source.to_base58();
source_string.push_str(&message.sequence_number.to_string());
MessageId(source_string)
},
}
}
}
pub struct GossipsubConfigBuilder {
config: GossipsubConfig,
}
impl Default for GossipsubConfigBuilder {
fn default() -> GossipsubConfigBuilder {
GossipsubConfigBuilder {
config: GossipsubConfig::default(),
}
}
}
impl GossipsubConfigBuilder {
pub fn new() -> GossipsubConfigBuilder {
GossipsubConfigBuilder::default()
}
pub fn protocol_id(&mut self, protocol_id: impl Into<Cow<'static, [u8]>>) -> &mut Self {
self.config.protocol_id = protocol_id.into();
self
}
pub fn history_length(&mut self, history_length: usize) -> &mut Self {
assert!(
history_length >= self.config.history_gossip,
"The history_length must be greater than or equal to the history_gossip length"
);
self.config.history_length = history_length;
self
}
pub fn history_gossip(&mut self, history_gossip: usize) -> &mut Self {
assert!(
self.config.history_length >= history_gossip,
"The history_length must be greater than or equal to the history_gossip length"
);
self.config.history_gossip = history_gossip;
self
}
pub fn mesh_n(&mut self, mesh_n: usize) -> &mut Self {
assert!(
self.config.mesh_n_low <= mesh_n && mesh_n <= self.config.mesh_n_high,
"The following equality doesn't hold mesh_n_low <= mesh_n <= mesh_n_high"
);
self.config.mesh_n = mesh_n;
self
}
pub fn mesh_n_low(&mut self, mesh_n_low: usize) -> &mut Self {
assert!(
mesh_n_low <= self.config.mesh_n && self.config.mesh_n <= self.config.mesh_n_high,
"The following equality doesn't hold mesh_n_low <= mesh_n <= mesh_n_high"
);
self.config.mesh_n_low = mesh_n_low;
self
}
pub fn mesh_n_high(&mut self, mesh_n_high: usize) -> &mut Self {
assert!(
self.config.mesh_n_low <= self.config.mesh_n && self.config.mesh_n <= mesh_n_high,
"The following equality doesn't hold mesh_n_low <= mesh_n <= mesh_n_high"
);
self.config.mesh_n_high = mesh_n_high;
self
}
pub fn gossip_lazy(&mut self, gossip_lazy: usize) -> &mut Self {
self.config.gossip_lazy = gossip_lazy;
self
}
pub fn heartbeat_initial_delay(&mut self, heartbeat_initial_delay: Duration) -> &mut Self {
self.config.heartbeat_initial_delay = heartbeat_initial_delay;
self
}
pub fn heartbeat_interval(&mut self, heartbeat_interval: Duration) -> &mut Self {
self.config.heartbeat_interval = heartbeat_interval;
self
}
pub fn fanout_ttl(&mut self, fanout_ttl: Duration) -> &mut Self {
self.config.fanout_ttl = fanout_ttl;
self
}
pub fn max_transmit_size(&mut self, max_transmit_size: usize) -> &mut Self {
self.config.max_transmit_size = max_transmit_size;
self
}
pub fn hash_topics(&mut self) -> &mut Self {
self.config.hash_topics = true;
self
}
pub fn no_source_id(&mut self) -> &mut Self {
self.config.no_source_id = true;
self
}
pub fn manual_propagation(&mut self) -> &mut Self {
self.config.manual_propagation = true;
self
}
pub fn message_id_fn(&mut self, id_fn: fn(&GossipsubMessage) -> MessageId) -> &mut Self {
self.config.message_id_fn = id_fn;
self
}
pub fn build(&self) -> GossipsubConfig {
self.config.clone()
}
}
impl std::fmt::Debug for GossipsubConfig {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut builder = f.debug_struct("GossipsubConfig");
let _ = builder.field("protocol_id", &self.protocol_id);
let _ = builder.field("history_length", &self.history_length);
let _ = builder.field("history_gossip", &self.history_gossip);
let _ = builder.field("mesh_n", &self.mesh_n);
let _ = builder.field("mesh_n_low", &self.mesh_n_low);
let _ = builder.field("mesh_n_high", &self.mesh_n_high);
let _ = builder.field("gossip_lazy", &self.gossip_lazy);
let _ = builder.field("heartbeat_initial_delay", &self.heartbeat_initial_delay);
let _ = builder.field("heartbeat_interval", &self.heartbeat_interval);
let _ = builder.field("fanout_ttl", &self.fanout_ttl);
let _ = builder.field("max_transmit_size", &self.max_transmit_size);
let _ = builder.field("hash_topics", &self.hash_topics);
let _ = builder.field("no_source_id", &self.no_source_id);
let _ = builder.field("manual_propagation", &self.manual_propagation);
builder.finish()
}
}