bip_dht 0.6.0

Implementation of the bittorrent mainline DHT
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
426
427
428
429
430
431
432
433
434
435
436
use bip_util;
use bip_util::convert;

// Transaction IDs are going to be vital for both scalability and performance concerns.
// They allow us to both protect against unsolicited responses as well as dropping those
// messages as soon as possible. We are taking an absurdly large, lazily generate, ringbuffer
// approach to generating transaction ids.

// We are going for a simple, stateless (for the most part) implementation for generating
// the transaction ids. We chose to go this route because 1, we dont want to reuse transaction
// ids used in recent requests that had subsequent responses as well because this would make us
// vulnerable to nodes that we gave that transaction id to, they would know we would be reusing
// it soon. And 2, that makes for an unscalable approach unless we also have a timeout for ids
// that we never received responses for which would lend itself to messy code.

// Instead, we are going to pre-allocate a chunk of ids, shuffle them, and use them until they
// run out, then pre-allocate some more, shuffle them, and use them. When we run out, (which wont
// happen for a VERY long time) we will simply wrap around. Also, we are going to break down the
// transaction id, so our transaction id will be made up of the first 5 bytes which will be the
// action id, this would be something like an individual lookup, a bucket refresh, or a bootstrap.
// Now, each of those actions have a number of messages associated with them, this is where the
// last 3 bytes come in which will be the message id. This allows us to route messages appropriately
// and associate them with some action we are performing right down to a message that the action is
// expecting. The pre-allocation strategy is used both on the action id level as well as the message
// id level.

// To protect against timing attacks, where recently pinged nodes got our transaction id and wish
// to guess other transaction ids in the block that we may have in flight, we will make the pre-allocation
// space fairly large so that our shuffle provides a strong protection from these attacks. In the future,
// we may want to dynamically ban nodes that we feel are guessing our transaction ids.

// IMPORTANT: Allocation markers (not the actual allocated ids) are not shifted so that we can deal with
// overflow by manually checking since I dont want to rely on langauge level overflows and whether they
// cause a panic or not (debug and release should have similar semantics)!

// Together these make up 8 bytes, or, a u64
const TRANSACTION_ID_BYTES: usize = ACTION_ID_BYTES + MESSAGE_ID_BYTES;
const ACTION_ID_BYTES: usize = 5;
const MESSAGE_ID_BYTES: usize = 3;

// Maximum exclusive value for an action id
const ACTION_ID_SHIFT: usize = ACTION_ID_BYTES * 8;
const MAX_ACTION_ID: u64 = 1 << ACTION_ID_SHIFT;

// Maximum exclusive value for a message id
const MESSAGE_ID_SHIFT: usize = MESSAGE_ID_BYTES * 8;
const MAX_MESSAGE_ID: u64 = 1 << MESSAGE_ID_SHIFT;

// Multiple of two so we can wrap around nicely
#[cfg(not(test))]
const ACTION_ID_PREALLOC_LEN: usize = 2048;
#[cfg(not(test))]
const MESSAGE_ID_PREALLOC_LEN: usize = 2048;

// Reduce the pre allocation length in tests to speed them up significantly
#[cfg(test)]
const ACTION_ID_PREALLOC_LEN: usize = 16;
#[cfg(test)]
const MESSAGE_ID_PREALLOC_LEN: usize = 16;

pub struct AIDGenerator {
    // NOT SHIFTED, so that we can wrap around manually!
    next_alloc: u64,
    curr_index: usize,
    action_ids: [u64; ACTION_ID_PREALLOC_LEN],
}

impl AIDGenerator {
    pub fn new() -> AIDGenerator {
        let (next_alloc, mut action_ids) = generate_aids(0);

        // Randomize the order of ids
        bip_util::fisher_shuffle(&mut action_ids);

        AIDGenerator {
            next_alloc: next_alloc,
            curr_index: 0,
            action_ids: action_ids,
        }
    }

    pub fn generate(&mut self) -> MIDGenerator {
        let opt_action_id = self.action_ids.get(self.curr_index).map(|a| *a);

        if let Some(action_id) = opt_action_id {
            self.curr_index += 1;

            // Shift the action id to make room for the message id
            MIDGenerator::new(action_id << MESSAGE_ID_SHIFT)
        } else {
            // Get a new block of action ids
            let (next_alloc, mut action_ids) = generate_aids(self.next_alloc);

            // Randomize the order of ids
            bip_util::fisher_shuffle(&mut action_ids);

            self.next_alloc = next_alloc;
            self.action_ids = action_ids;
            self.curr_index = 0;

            self.generate()
        }
    }
}

// (next_alloc, aids)
fn generate_aids(next_alloc: u64) -> (u64, [u64; ACTION_ID_PREALLOC_LEN]) {
    // Check if we need to wrap
    let (next_alloc_start, next_alloc_end) = if next_alloc == MAX_ACTION_ID {
        (0, ACTION_ID_PREALLOC_LEN as u64)
    } else {
        (next_alloc, next_alloc + ACTION_ID_PREALLOC_LEN as u64)
    };
    let mut action_ids = [0u64; ACTION_ID_PREALLOC_LEN];

    for (index, action_id) in (next_alloc_start..next_alloc_end).enumerate() {
        action_ids[index] = action_id;
    }

    (next_alloc_end, action_ids)
}

// ----------------------------------------------------------------------------//

pub struct MIDGenerator {
    // ALREADY SHIFTED, for your convenience :)
    action_id: u64,
    // NOT SHIFTED, so that we can wrap around manually!
    next_alloc: u64,
    curr_index: usize,
    message_ids: [u64; MESSAGE_ID_PREALLOC_LEN],
}

impl MIDGenerator {
    // Accepts an action id that has ALREADY BEEN SHIFTED!
    fn new(action_id: u64) -> MIDGenerator {
        // In order to speed up tests, we will generate the first block lazily.
        MIDGenerator {
            action_id: action_id,
            next_alloc: 0,
            curr_index: MESSAGE_ID_PREALLOC_LEN,
            message_ids: [0u64; MESSAGE_ID_PREALLOC_LEN],
        }
    }

    pub fn action_id(&self) -> ActionID {
        ActionID::from_transaction_id(self.action_id)
    }

    pub fn generate(&mut self) -> TransactionID {
        let opt_message_id = self.message_ids.get(self.curr_index).map(|m| *m);

        if let Some(message_id) = opt_message_id {
            self.curr_index += 1;

            TransactionID::new(self.action_id | message_id)
        } else {
            // Get a new block of message ids
            let (next_alloc, mut message_ids) = generate_mids(self.next_alloc);

            // Randomize the order of ids
            bip_util::fisher_shuffle(&mut message_ids);

            self.next_alloc = next_alloc;
            self.message_ids = message_ids;
            self.curr_index = 0;

            self.generate()
        }
    }
}

// (next_alloc, mids)
fn generate_mids(next_alloc: u64) -> (u64, [u64; MESSAGE_ID_PREALLOC_LEN]) {
    // Check if we need to wrap
    let (next_alloc_start, next_alloc_end) = if next_alloc == MAX_MESSAGE_ID {
        (0, MESSAGE_ID_PREALLOC_LEN as u64)
    } else {
        (next_alloc, next_alloc + MESSAGE_ID_PREALLOC_LEN as u64)
    };
    let mut message_ids = [0u64; MESSAGE_ID_PREALLOC_LEN];

    for (index, message_id) in (next_alloc_start..next_alloc_end).enumerate() {
        message_ids[index] = message_id;
    }

    (next_alloc_end, message_ids)
}


// ----------------------------------------------------------------------------//

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct TransactionID {
    trans_id: u64,
    trans_id_bytes: [u8; TRANSACTION_ID_BYTES],
}

impl TransactionID {
    fn new(trans_id: u64) -> TransactionID {
        let trans_id_bytes = convert::eight_bytes_to_array(trans_id);

        TransactionID {
            trans_id: trans_id,
            trans_id_bytes: trans_id_bytes,
        }
    }

    /// Construct a transaction id from a series of bytes.
    pub fn from_bytes(bytes: &[u8]) -> Option<TransactionID> {
        if bytes.len() != TRANSACTION_ID_BYTES {
            return None;
        }
        let mut trans_id = 0u64;

        for byte in bytes.iter() {
            trans_id <<= 8;
            trans_id |= *byte as u64;
        }

        Some(TransactionID::new(trans_id))
    }

    pub fn action_id(&self) -> ActionID {
        ActionID::from_transaction_id(self.trans_id)
    }

    #[allow(unused)]
    pub fn message_id(&self) -> MessageID {
        MessageID::from_transaction_id(self.trans_id)
    }
}

impl AsRef<[u8]> for TransactionID {
    fn as_ref(&self) -> &[u8] {
        &self.trans_id_bytes
    }
}

// ----------------------------------------------------------------------------//

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct ActionID {
    action_id: u64,
}

impl ActionID {
    fn from_transaction_id(trans_id: u64) -> ActionID {
        // The ACTUAL action id
        let shifted_action_id = trans_id >> MESSAGE_ID_SHIFT;

        ActionID { action_id: shifted_action_id }
    }
}

// ----------------------------------------------------------------------------//

#[allow(unused)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct MessageID {
    message_id: u64,
}

impl MessageID {
    #[allow(unused)]
    fn from_transaction_id(trans_id: u64) -> MessageID {
        let clear_action_id = MAX_MESSAGE_ID - 1;
        // The ACTUAL message id
        let shifted_message_id = trans_id & clear_action_id;

        MessageID { message_id: shifted_message_id }
    }
}

// ----------------------------------------------------------------------------//

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::{AIDGenerator, TransactionID};

    #[test]
    fn positive_tid_from_bytes() {
        let mut aid_generator = AIDGenerator::new();
        let mut mid_generator = aid_generator.generate();

        let tid = mid_generator.generate();
        let tid_from_bytes = TransactionID::from_bytes(tid.as_ref()).unwrap();

        assert_eq!(tid, tid_from_bytes);
    }

    #[test]
    fn positive_unique_aid_blocks() {
        // Go through ten blocks worth of action ids, make sure they are unique
        let mut action_ids = HashSet::new();
        let mut aid_generator = AIDGenerator::new();

        for _ in 0..(super::ACTION_ID_PREALLOC_LEN * 10) {
            let action_id = aid_generator.generate().action_id();

            assert!(!action_ids.contains(&action_id));

            action_ids.insert(action_id);
        }
    }

    #[test]
    fn positive_unique_mid_blocks() {
        // Go through ten blocks worth of message ids, make sure they are unique
        let mut message_ids = HashSet::new();
        let mut aid_generator = AIDGenerator::new();
        let mut mid_generator = aid_generator.generate();

        for _ in 0..(super::MESSAGE_ID_PREALLOC_LEN * 10) {
            let message_id = mid_generator.generate().message_id();

            assert!(!message_ids.contains(&message_id));

            message_ids.insert(message_id);
        }
    }

    #[test]
    fn positive_unique_tid_blocks() {
        // Go through two blocks of compound ids (transaction ids), make sure they are unique
        let mut transaction_ids = HashSet::new();
        let mut aid_generator = AIDGenerator::new();

        for _ in 0..(super::ACTION_ID_PREALLOC_LEN) {
            let mut mid_generator = aid_generator.generate();

            for _ in 0..(super::MESSAGE_ID_PREALLOC_LEN) {
                let transaction_id = mid_generator.generate();

                assert!(!transaction_ids.contains(&transaction_id));

                transaction_ids.insert(transaction_id);
            }
        }
    }

    #[test]
    fn positive_overflow_aid_generate() {
        let mut action_ids = HashSet::new();
        let mut aid_generator = AIDGenerator::new();

        // Track all action ids in the first block
        for _ in 0..(super::ACTION_ID_PREALLOC_LEN) {
            let action_id = aid_generator.generate().action_id();

            assert!(!action_ids.contains(&action_id));

            action_ids.insert(action_id);
        }

        // Modify private variables to overflow back to first block
        aid_generator.next_alloc = super::MAX_ACTION_ID;
        aid_generator.curr_index = super::ACTION_ID_PREALLOC_LEN;

        // Check all action ids in the block (should be first block)
        for _ in 0..(super::ACTION_ID_PREALLOC_LEN) {
            let action_id = aid_generator.generate().action_id();

            assert!(action_ids.remove(&action_id));
        }

        assert!(action_ids.is_empty());
    }

    #[test]
    fn positive_overflow_mid_generate() {
        let mut message_ids = HashSet::new();
        let mut aid_generator = AIDGenerator::new();
        let mut mid_generator = aid_generator.generate();

        // Track all message ids in the first block
        for _ in 0..(super::MESSAGE_ID_PREALLOC_LEN) {
            let message_id = mid_generator.generate().message_id();

            assert!(!message_ids.contains(&message_id));

            message_ids.insert(message_id);
        }

        // Modify private variables to overflow back to first block
        mid_generator.next_alloc = super::MAX_MESSAGE_ID;
        mid_generator.curr_index = super::MESSAGE_ID_PREALLOC_LEN;

        // Check all message ids in the block (should be first block)
        for _ in 0..(super::MESSAGE_ID_PREALLOC_LEN) {
            let message_id = mid_generator.generate().message_id();

            assert!(message_ids.remove(&message_id));
        }

        assert!(message_ids.is_empty());
    }

    #[test]
    fn positive_overflow_tid_generate() {
        let mut transaction_ids = HashSet::new();
        let mut aid_generator = AIDGenerator::new();

        // Track all transaction ids in the first block
        for _ in 0..(super::ACTION_ID_PREALLOC_LEN) {
            let mut mid_generator = aid_generator.generate();

            for _ in 0..(super::MESSAGE_ID_PREALLOC_LEN) {
                let transaction_id = mid_generator.generate();

                assert!(!transaction_ids.contains(&transaction_id));

                transaction_ids.insert(transaction_id);
            }
        }

        // Modify private variables to overflow back to first block
        aid_generator.next_alloc = super::MAX_ACTION_ID;
        aid_generator.curr_index = super::ACTION_ID_PREALLOC_LEN;

        // Check all transaction ids in the block (should be first block)
        for _ in 0..(super::ACTION_ID_PREALLOC_LEN) {
            let mut mid_generator = aid_generator.generate();

            for _ in 0..(super::MESSAGE_ID_PREALLOC_LEN) {
                let transaction_id = mid_generator.generate();

                assert!(transaction_ids.remove(&transaction_id));
            }
        }

        assert!(transaction_ids.is_empty());
    }
}