grapevine 1.1.0

A modern, asynchronous peer-to-peer gossip protocol library and application
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Implements `Node` configuration.

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::core::message_codec::MAX_FRAME_SIZE;
use crate::{AntiEntropyConfig, EpidemicConfig, Error, RateLimitConfig, Result, TransportConfig};

const DEFAULT_BIND_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0);

/// Configuration for a Grapevine node.
///
/// A `NodeConfig` cannot be constructed in an invalid state: every path that
/// produces one---[`NodeConfigBuilder::build`] and `serde` deserialization (via
/// [`NodeConfig::validate`])---rejects out-of-range values rather than silently
/// accepting them.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(try_from = "NodeConfigUnchecked")]
pub struct NodeConfig {
    /// Address to bind the node to
    pub bind_addr: SocketAddr,

    /// Initial peers to connect to
    pub bootstrap_peers: Vec<SocketAddr>,

    /// Gossip interval (how often to gossip)
    pub gossip_interval: Duration,

    /// Fan-out factor (how many peers to gossip to)
    pub fanout: usize,

    /// Maximum message size in bytes
    pub max_message_size: usize,

    /// Peer timeout duration
    pub peer_timeout: Duration,

    /// Maximum number of peers to maintain
    pub max_peers: usize,

    /// Connection timeout
    pub connection_timeout: Duration,

    /// Message deduplication TTL (how long to remember seen messages)
    pub message_dedup_ttl: Duration,

    /// Anti-entropy protocol configuration
    pub anti_entropy: AntiEntropyConfig,

    /// Epidemic broadcast configuration
    pub epidemic: EpidemicConfig,

    /// Rate limiting configuration
    pub rate_limit: RateLimitConfig,

    /// Transport protocol
    pub transport: TransportConfig,
}

impl Default for NodeConfig {
    fn default() -> Self {
        Self {
            bind_addr: DEFAULT_BIND_ADDR,
            bootstrap_peers: Vec::new(),
            gossip_interval: Duration::from_secs(5),
            fanout: 3,
            max_message_size: MAX_FRAME_SIZE,
            peer_timeout: Duration::from_secs(30),
            max_peers: 50,
            connection_timeout: Duration::from_secs(10),
            message_dedup_ttl: Duration::from_secs(300), // 5 minutes
            anti_entropy: AntiEntropyConfig::default(),
            epidemic: EpidemicConfig::default(),
            rate_limit: RateLimitConfig::default(),
            transport: TransportConfig::Tcp,
        }
    }
}

impl NodeConfig {
    /// Validate every configuration invariant.
    ///
    /// This is the single source of truth for what a well-formed `NodeConfig`
    /// is. It runs both from [`NodeConfigBuilder::build`] and from `serde`
    /// deserialization, so an invalid configuration cannot reach the rest of
    /// the system through any path.
    ///
    /// # Errors
    /// Returns [`Error::Config`] describing the first invariant that fails.
    pub fn validate(&self) -> Result<()> {
        if self.fanout == 0 {
            return Err(Error::Config("fanout must be > 0".into()));
        }
        if self.max_peers == 0 {
            return Err(Error::Config("max_peers must be > 0".into()));
        }
        if self.max_message_size == 0 {
            return Err(Error::Config("max_message_size must be > 0".into()));
        }
        if self.fanout > self.max_peers {
            return Err(Error::Config("fanout cannot exceed max_peers".into()));
        }
        if self.gossip_interval < Duration::from_secs(1) {
            return Err(Error::Config("gossip_interval must be >= 1 second".into()));
        }
        if self.gossip_interval > Duration::from_secs(3600) {
            return Err(Error::Config("gossip_interval must be <= 1 hour".into()));
        }
        if self.peer_timeout < Duration::from_secs(5) {
            return Err(Error::Config("peer_timeout must be >= 5 seconds".into()));
        }
        if self.connection_timeout < Duration::from_secs(1) {
            return Err(Error::Config(
                "connection_timeout must be >= 1 second".into(),
            ));
        }
        if self.rate_limit.enabled {
            self.rate_limit.validate().map_err(Error::Config)?;
        }
        Ok(())
    }
}

/// Unvalidated wire representation of [`NodeConfig`].
///
/// `serde` deserializes into this first; the [`TryFrom`] conversion then
/// runs [`NodeConfig::validate`], which is what makes an invalid `NodeConfig`
/// impossible to deserialize.
#[derive(Deserialize)]
struct NodeConfigUnchecked {
    bind_addr: SocketAddr,
    bootstrap_peers: Vec<SocketAddr>,
    gossip_interval: Duration,
    fanout: usize,
    max_message_size: usize,
    peer_timeout: Duration,
    max_peers: usize,
    connection_timeout: Duration,
    message_dedup_ttl: Duration,
    anti_entropy: AntiEntropyConfig,
    epidemic: EpidemicConfig,
    rate_limit: RateLimitConfig,
    transport: TransportConfig,
}

impl TryFrom<NodeConfigUnchecked> for NodeConfig {
    type Error = Error;

    fn try_from(raw: NodeConfigUnchecked) -> Result<Self> {
        let config = Self {
            bind_addr: raw.bind_addr,
            bootstrap_peers: raw.bootstrap_peers,
            gossip_interval: raw.gossip_interval,
            fanout: raw.fanout,
            max_message_size: raw.max_message_size,
            peer_timeout: raw.peer_timeout,
            max_peers: raw.max_peers,
            connection_timeout: raw.connection_timeout,
            message_dedup_ttl: raw.message_dedup_ttl,
            anti_entropy: raw.anti_entropy,
            epidemic: raw.epidemic,
            rate_limit: raw.rate_limit,
            transport: raw.transport,
        };
        config.validate()?;
        Ok(config)
    }
}

/// Builder for NodeConfig.
#[derive(Debug, Default)]
pub struct NodeConfigBuilder {
    config: NodeConfig,
}

impl NodeConfigBuilder {
    /// Create a new builder with defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set bind address.
    pub fn bind_addr(mut self, addr: SocketAddr) -> Self {
        self.config.bind_addr = addr;
        self
    }

    /// Add a bootstrap peer.
    pub fn add_bootstrap_peer(mut self, peer: SocketAddr) -> Self {
        self.config.bootstrap_peers.push(peer);
        self
    }

    /// Set bootstrap peers.
    pub fn bootstrap_peers(mut self, peers: Vec<SocketAddr>) -> Self {
        self.config.bootstrap_peers = peers;
        self
    }

    /// Set gossip interval.
    pub fn gossip_interval(mut self, interval: Duration) -> Self {
        self.config.gossip_interval = interval;
        self
    }

    /// Set fan-out factor.
    pub fn fanout(mut self, fanout: usize) -> Self {
        self.config.fanout = fanout;
        self
    }

    /// Set maximum message size.
    pub fn max_message_size(mut self, size: usize) -> Self {
        self.config.max_message_size = size;
        self
    }

    /// Set peer timeout.
    pub fn peer_timeout(mut self, timeout: Duration) -> Self {
        self.config.peer_timeout = timeout;
        self
    }

    /// Set maximum peers.
    pub fn max_peers(mut self, max: usize) -> Self {
        self.config.max_peers = max;
        self
    }

    /// Set connection timeout.
    pub fn connection_timeout(mut self, timeout: Duration) -> Self {
        self.config.connection_timeout = timeout;
        self
    }

    /// Set message deduplication TTL.
    pub fn message_dedup_ttl(mut self, ttl: Duration) -> Self {
        self.config.message_dedup_ttl = ttl;
        self
    }

    /// Set anti-entropy configuration.
    pub fn anti_entropy(mut self, config: AntiEntropyConfig) -> Self {
        self.config.anti_entropy = config;
        self
    }

    /// Set epidemic broadcast configuration.
    pub fn epidemic(mut self, config: EpidemicConfig) -> Self {
        self.config.epidemic = config;
        self
    }

    /// Set rate limiting configuration.
    pub fn rate_limit(mut self, config: RateLimitConfig) -> Self {
        self.config.rate_limit = config;
        self
    }

    /// Set transport configuration.
    pub fn transport(mut self, transport: TransportConfig) -> Self {
        self.config.transport = transport;
        self
    }

    /// Build the configuration.
    ///
    /// # Errors
    /// Returns [`Error::Config`] if any invariant in [`NodeConfig::validate`]
    /// fails.
    pub fn build(self) -> Result<NodeConfig> {
        self.config.validate()?;
        Ok(self.config)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_config() {
        let config = NodeConfig::default();
        assert_eq!(config.fanout, 3);
        assert_eq!(config.max_peers, 50);
        assert_eq!(config.max_message_size, MAX_FRAME_SIZE);
        assert_eq!(config.gossip_interval, Duration::from_secs(5));
        assert_eq!(config.peer_timeout, Duration::from_secs(30));
        assert_eq!(config.connection_timeout, Duration::from_secs(10));
        assert!(config.bootstrap_peers.is_empty());
        assert!(matches!(config.transport, TransportConfig::Tcp));
    }

    #[test]
    fn config_builder_basic() {
        let config = NodeConfigBuilder::new()
            .fanout(5)
            .max_peers(100)
            .build()
            .unwrap();
        assert_eq!(config.fanout, 5);
        assert_eq!(config.max_peers, 100);
    }

    #[test]
    fn config_builder_all_fields() {
        let bind_addr = "192.168.1.1:9000".parse().unwrap();
        let peer1 = "192.168.1.2:9000".parse().unwrap();
        let peer2 = "192.168.1.3:9000".parse().unwrap();

        let config = NodeConfigBuilder::new()
            .bind_addr(bind_addr)
            .add_bootstrap_peer(peer1)
            .add_bootstrap_peer(peer2)
            .fanout(7)
            .max_peers(200)
            .max_message_size(2048)
            .gossip_interval(Duration::from_secs(10))
            .peer_timeout(Duration::from_secs(60))
            .connection_timeout(Duration::from_secs(20))
            .build()
            .unwrap();

        assert_eq!(config.bind_addr, bind_addr);
        assert_eq!(config.bootstrap_peers.len(), 2);
        assert_eq!(config.bootstrap_peers[0], peer1);
        assert_eq!(config.bootstrap_peers[1], peer2);
        assert_eq!(config.fanout, 7);
        assert_eq!(config.max_peers, 200);
        assert_eq!(config.max_message_size, 2048);
        assert_eq!(config.gossip_interval, Duration::from_secs(10));
        assert_eq!(config.peer_timeout, Duration::from_secs(60));
        assert_eq!(config.connection_timeout, Duration::from_secs(20));
    }

    #[test]
    fn config_builder_bootstrap_peers() {
        let peer1 = "192.168.1.2:9000".parse().unwrap();
        let peer2 = "192.168.1.3:9000".parse().unwrap();
        let peers = vec![peer1, peer2];

        let config = NodeConfigBuilder::new()
            .bootstrap_peers(peers.clone())
            .build()
            .unwrap();

        assert_eq!(config.bootstrap_peers, peers);
    }

    #[test]
    fn validate_fanout_zero() {
        let result = NodeConfigBuilder::new().fanout(0).build();
        assert!(result.is_err());
        match result {
            Err(Error::Config(msg)) => assert!(msg.contains("fanout")),
            _ => panic!("Expected Config error"),
        }
    }

    #[test]
    fn validate_max_peers_zero() {
        let result = NodeConfigBuilder::new().max_peers(0).build();
        assert!(result.is_err());
        match result {
            Err(Error::Config(msg)) => assert!(msg.contains("max_peers")),
            _ => panic!("Expected Config error"),
        }
    }

    #[test]
    fn validate_max_message_size_zero() {
        let result = NodeConfigBuilder::new().max_message_size(0).build();
        assert!(result.is_err());
        match result {
            Err(Error::Config(msg)) => assert!(msg.contains("max_message_size")),
            _ => panic!("Expected Config error"),
        }
    }

    #[test]
    fn validate_all_valid() {
        let config = NodeConfigBuilder::new()
            .fanout(1)
            .max_peers(1)
            .max_message_size(1)
            .build();
        assert!(config.is_ok());
    }

    #[test]
    fn config_serialization() {
        let config = NodeConfig::default();
        let serialized = serde_json::to_string(&config).unwrap();
        let deserialized: NodeConfig = serde_json::from_str(&serialized).unwrap();

        assert_eq!(config.fanout, deserialized.fanout);
        assert_eq!(config.max_peers, deserialized.max_peers);
        assert_eq!(config.max_message_size, deserialized.max_message_size);
    }

    #[test]
    fn transport_config_tcp() {
        let config = NodeConfigBuilder::new()
            .transport(TransportConfig::Tcp)
            .build()
            .unwrap();
        assert!(matches!(config.transport, TransportConfig::Tcp));
    }

    #[test]
    fn invalid_config_rejected_on_deserialize() {
        let valid = serde_json::to_value(NodeConfig::default()).unwrap();
        assert!(serde_json::from_value::<NodeConfig>(valid.clone()).is_ok());

        let mut bad_fanout = valid.clone();
        bad_fanout["fanout"] = serde_json::json!(0);
        assert!(serde_json::from_value::<NodeConfig>(bad_fanout).is_err());

        let mut bad_rate_limit = valid;
        bad_rate_limit["rate_limit"]["capacity"] = serde_json::json!(0);
        assert!(serde_json::from_value::<NodeConfig>(bad_rate_limit).is_err());
    }
}