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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
use std::{
    sync::Arc,
    time::{Duration, Instant},
};

use ahash::AHashSet;
use anyhow::{ensure, Result};
use cid::Cid;
use iroh_metrics::core::MRecorder;
use iroh_metrics::{bitswap::BitswapMetrics, inc};
use libp2p::PeerId;
use tokio::{sync::mpsc, task::JoinHandle};
use tracing::{debug, error, warn};

use crate::{
    message::{BitswapMessage, Entry, WantType},
    network::{MessageSender, MessageSenderConfig, Network},
};

use self::{dont_have_timeout_manager::DontHaveTimeoutManager, wantlist::Wants};

use super::peer_manager::DontHaveTimeout;

mod dont_have_timeout_manager;
mod wantlist;

#[derive(Debug)]
pub struct MessageQueue {
    peer: PeerId,
    sender_responses: Option<mpsc::Sender<Vec<Cid>>>,
    sender_wants: Option<mpsc::Sender<WantsUpdate>>,
    worker: JoinHandle<()>,
}

#[derive(Debug)]
enum WantsUpdate {
    BroadcastWantHaves(AHashSet<Cid>),
    Wants {
        want_blocks: Vec<Cid>,
        want_haves: Vec<Cid>,
    },
    Cancels(AHashSet<Cid>),
    #[cfg(test)]
    #[allow(dead_code)]
    GetWants(tokio::sync::oneshot::Sender<Wants>),
}

#[derive(Debug, Clone)]
pub struct Config {
    /// Maximum message size in bytes.
    pub max_message_size: usize,
    /// The time to w ait before retrying to connect after an error,
    /// when trying to send a message.
    pub send_error_backof: Duration,
    /// Maximum amount of time in which to accept a response as being valid
    /// for latency calculation (as opposed to discarding it as an outlier).
    pub max_valid_latency: Duration,
    /// Maximum priority allowed in the network.
    pub max_priority: i32,
    pub rebroadcast_interval: Duration,
    pub send_message_max_delay: Duration,
    pub send_message_cutoff: usize,
    pub send_message_debounce: Duration,
    pub send_timeout: Duration,
    pub max_retries: usize,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            max_message_size: 1024 * 1024 * 2,
            send_error_backof: Duration::from_millis(100),
            max_valid_latency: Duration::from_secs(30),
            max_priority: i32::MAX,
            rebroadcast_interval: Duration::from_secs(30),
            send_message_max_delay: Duration::from_millis(20),
            send_message_cutoff: 256,
            send_message_debounce: Duration::from_millis(1),
            send_timeout: Duration::from_secs(30),
            max_retries: 3,
        }
    }
}

impl MessageQueue {
    pub async fn new(
        peer: PeerId,
        network: Network,
        on_dont_have_timeout: Arc<dyn DontHaveTimeout>,
    ) -> Self {
        Self::with_config(peer, network, Config::default(), on_dont_have_timeout).await
    }

    pub async fn with_config(
        peer: PeerId,
        network: Network,
        config: Config,
        on_dont_have_timeout: Arc<dyn DontHaveTimeout>,
    ) -> Self {
        let (sender_responses, receiver_responses) = mpsc::channel(64);
        let (sender_wants, receiver_wants) = mpsc::channel(2048);

        let actor = MessageQueueActor::new(
            config,
            network,
            peer,
            receiver_responses,
            receiver_wants,
            on_dont_have_timeout,
        )
        .await;

        let worker = tokio::task::spawn(async move { run(actor).await });

        MessageQueue {
            peer,
            sender_responses: Some(sender_responses),
            sender_wants: Some(sender_wants),
            worker,
        }
    }

    pub fn is_running(&self) -> bool {
        if let Some(ref sender) = self.sender_wants {
            !sender.is_closed()
        } else {
            false
        }
    }

    #[cfg(test)]
    #[allow(dead_code)]
    pub(crate) async fn wants(&self) -> Result<Wants> {
        let (s, r) = tokio::sync::oneshot::channel();
        self.send_wants_update(WantsUpdate::GetWants(s)).await;
        let wants = r.await?;
        Ok(wants)
    }

    /// Add want-haves that are part of a broadcast to all connected peers.
    pub async fn add_broadcast_want_haves(&self, want_haves: &AHashSet<Cid>) {
        if want_haves.is_empty() || !self.is_running() {
            return;
        }
        self.send_wants_update(WantsUpdate::BroadcastWantHaves(want_haves.to_owned()))
            .await;
    }

    /// Add want-haves and want-blocks for the peer for this queue.
    pub async fn add_wants(&self, want_blocks: &[Cid], want_haves: &[Cid]) {
        debug!("add_wants: {} {}", want_blocks.len(), want_haves.len());
        if (want_blocks.is_empty() && want_haves.is_empty()) || !self.is_running() {
            return;
        }

        self.send_wants_update(WantsUpdate::Wants {
            want_blocks: want_blocks.to_vec(),
            want_haves: want_haves.to_vec(),
        })
        .await;
    }

    /// Add cancel messages for the given keys.
    pub async fn add_cancels(&self, cancels: &AHashSet<Cid>) {
        if cancels.is_empty() || !self.is_running() {
            return;
        }

        self.send_wants_update(WantsUpdate::Cancels(cancels.to_owned()))
            .await;
    }

    async fn send_wants_update(&self, update: WantsUpdate) {
        if let Some(ref sender) = self.sender_wants {
            if let Err(err) = sender.send(update).await {
                warn!(
                    "message_queue:{}: failed to send wants update: {:?}",
                    self.peer, err
                );
            }
        } else {
            warn!(
                "message_queue:{}: failed to send message: not running",
                self.peer
            );
        }
    }

    /// Called when a message is received from the network.
    /// `cids` is the set of blocks, HAVEs and DONT_HAVEs in the message.
    /// Note: this is only use to calculate latency currently.
    pub async fn response_received(&self, cids: Vec<Cid>) {
        if cids.is_empty() || !self.is_running() {
            return;
        }

        // Best effort only
        if let Some(ref sender) = self.sender_responses {
            let _ = sender.try_send(cids);
        }
    }

    /// Shuts down this message queue.
    pub async fn stop(mut self) -> Result<()> {
        ensure!(
            self.sender_responses.is_some(),
            "message queue {} already stopped",
            self.peer
        );
        inc!(BitswapMetrics::MessageQueuesStopped);

        let _ = self.sender_wants.take();
        let _ = self.sender_responses.take();
        // just kill it
        self.worker.abort();
        // self.worker.await?;

        Ok(())
    }
}

async fn run(mut actor: MessageQueueActor) {
    let mut work_scheduled: Option<Instant> = None;
    let mut rebroadcast_timer = tokio::time::interval_at(
        tokio::time::Instant::now() + actor.config.rebroadcast_interval,
        actor.config.rebroadcast_interval,
    );

    let schedule_work = tokio::time::sleep(Duration::from_secs(100));
    tokio::pin!(schedule_work);
    let mut schedule_work_enabled = false;

    loop {
        inc!(BitswapMetrics::MessageQueueWorkerLoopTick);
        tokio::select! {
            biased;

            message = actor.receiver_wants.recv() => {
                debug!("{}: {:?}", actor.peer, message);
                match message {
                    Some(wants_update) => {
                        actor.handle_wants_update(wants_update).await;
                    }
                    None => {
                        // Shutdown
                        break;
                    }
                }
            }
            message = actor.receiver_responses.recv() => {
                debug!("{}: {:?}", actor.peer, message);
                match message {
                    Some(responses) => {
                        actor.handle_response(responses).await;
                    }
                    None => {
                        // Shutdown
                        break;
                    }
                }
            }
            _ = rebroadcast_timer.tick() => {
                if actor.rebroadcast_wantlist().await {
                    // fatal error
                    break;
                }
            }
            Some(when) = actor.outgoing_work.1.recv() => {
                if work_scheduled.is_none() {
                    // No work, record when the work was scheduled.
                    work_scheduled = Some(when);
                } else {
                    // If work is scheduled, make sure timer is cancelled.
                    schedule_work_enabled = false;
                }

                // We have so much work, schedule it immeditately
                let pending_work_count = actor.wants.pending_work_count();
                if pending_work_count > actor.config.send_message_cutoff ||
                    work_scheduled.unwrap().elapsed() >= actor.config.send_message_max_delay {
                        debug!("{}: outgoing work sending", actor.peer);
                        if actor.send_if_ready().await {
                            // fatal error
                            break;
                        }
                        work_scheduled = None;
                    } else {
                        debug!("{}: outgoing work extend timer", actor.peer);
                        // Extend the timer
                        schedule_work.as_mut().reset(tokio::time::Instant::now() + actor.config.send_message_debounce);
                        schedule_work_enabled = true;
                    }
            }
            _ = &mut schedule_work, if schedule_work_enabled => {
                debug!("{}: schedule work", actor.peer);
                work_scheduled = None;
                schedule_work_enabled = false;
                if actor.send_if_ready().await {
                    // fatal error
                    break;
                }
            }
        }
    }

    debug!("{}: message loop shutting down", actor.peer);
    if let Err(err) = actor.stop().await {
        error!(
            "message_queue: failed to stop message queue loop: {:?}",
            err
        );
    }
}

struct MessageQueueActor {
    peer: PeerId,
    config: Config,
    wants: Wants,
    dh_timeout_manager: DontHaveTimeoutManager,
    outgoing_work: (mpsc::Sender<Instant>, mpsc::Receiver<Instant>),
    sender: Option<MessageSender>,
    network: Network,
    msg_sender_config: MessageSenderConfig,
    receiver_responses: mpsc::Receiver<Vec<Cid>>,
    receiver_wants: mpsc::Receiver<WantsUpdate>,
}

impl MessageQueueActor {
    async fn new(
        config: Config,
        network: Network,
        peer: PeerId,
        receiver_responses: mpsc::Receiver<Vec<Cid>>,
        receiver_wants: mpsc::Receiver<WantsUpdate>,
        on_dont_have_timeout: Arc<dyn DontHaveTimeout>,
    ) -> Self {
        let outgoing_work = mpsc::channel(2);
        let wants = Wants {
            bcst_wants: Default::default(),
            peer_wants: Default::default(),
            cancels: Default::default(),
            priority: config.max_priority,
        };

        let dh_timeout_manager = DontHaveTimeoutManager::new(peer, on_dont_have_timeout).await;

        let msg_sender_config = MessageSenderConfig {
            max_retries: config.max_retries,
            send_timeout: config.send_timeout,
            send_error_backoff: config.send_error_backof,
        };
        Self {
            config,
            wants,
            dh_timeout_manager,
            outgoing_work,
            sender: None,
            network,
            msg_sender_config,
            peer,
            receiver_responses,
            receiver_wants,
        }
    }

    async fn stop(self) -> Result<()> {
        self.dh_timeout_manager.stop().await?;
        Ok(())
    }

    async fn handle_wants_update(&mut self, wants_update: WantsUpdate) {
        match wants_update {
            WantsUpdate::BroadcastWantHaves(want_haves) => {
                for cid in want_haves {
                    self.wants
                        .bcst_wants
                        .add(cid, self.wants.priority, WantType::Have);
                    self.wants.priority -= 1;

                    // Adding a want-have for the cid, so clear any pending cancels.
                    self.wants.cancels.remove(&cid);
                }

                self.signal_work();
            }
            WantsUpdate::Wants {
                want_blocks,
                want_haves,
            } => {
                for cid in want_haves {
                    self.wants
                        .peer_wants
                        .add(cid, self.wants.priority, WantType::Have);
                    self.wants.priority -= 1;

                    // Adding a want-have for the cid, so clear any pending cancels.
                    self.wants.cancels.remove(&cid);
                }

                for cid in want_blocks {
                    self.wants
                        .peer_wants
                        .add(cid, self.wants.priority, WantType::Block);
                    self.wants.priority -= 1;

                    // Adding a want-block for the cid, so clear any pending cancels.
                    self.wants.cancels.remove(&cid);
                }
                self.signal_work();
            }
            WantsUpdate::Cancels(cancels) => {
                // Cancel any outstanding DONT_HAVE timers
                self.dh_timeout_manager.cancel_pending(&cancels).await;

                let mut work_ready = false;
                // Remove keys from broadcast and peer wants, and add to cancels.
                for cid in cancels {
                    // Check if a want for the key was sent
                    let was_sent_bcst = self.wants.bcst_wants.sent.contains(&cid);
                    let was_sent_peer = self.wants.peer_wants.sent.contains(&cid);

                    // Remove the want from tracking wantlist
                    self.wants.bcst_wants.remove(&cid);
                    self.wants.peer_wants.remove(&cid);

                    // Only send a cancel if a want was sent
                    if was_sent_bcst || was_sent_peer {
                        self.wants.cancels.insert(cid);
                        work_ready = true;
                    }
                }

                // Schedule a message send
                if work_ready {
                    self.signal_work();
                }
            }
            #[cfg(test)]
            WantsUpdate::GetWants(r) => r.send(self.wants.clone()).unwrap(),
        }
    }

    async fn rebroadcast_wantlist(&mut self) -> bool {
        if self.transfer_rebroadcast_wants().await {
            return self.send_message().await;
        }
        false
    }

    /// Transfer wants from the rebroadcast lists into the pending lists.
    async fn transfer_rebroadcast_wants(&mut self) -> bool {
        // Check if there are any wants to rebroadcast.
        if self.wants.bcst_wants.sent.is_empty() && self.wants.peer_wants.sent.is_empty() {
            return false;
        }

        // Copy sent wants into pending wants lists
        self.wants
            .bcst_wants
            .pending
            .extend(self.wants.bcst_wants.sent.clone());
        self.wants
            .peer_wants
            .pending
            .extend(self.wants.peer_wants.sent.clone());

        true
    }

    async fn send_message(&mut self) -> bool {
        self.dh_timeout_manager.start(self.network.clone()).await;
        // Convert want lists to a bitswap message
        let (msg, sender, peer_entries, bcst_entries) = match self.extract_outgoing_message().await
        {
            Ok(res) => res,
            Err(err) => {
                debug!(
                    "message_queue:{}: failed to prepare message: {:?}",
                    self.peer, err
                );
                return true;
            }
        };
        if msg.is_empty() {
            return false;
        }

        let wantlist: Vec<_> = msg.wantlist().cloned().collect();
        if let Err(err) = sender.send_message(msg).await {
            debug!(
                "message_queue:{}: failed to send message {:?}",
                self.peer, err
            );
            return true;
        }

        // Record sent time so as to calculate message latency.
        // Update state after the message has been sent.
        {
            let now = Instant::now();
            for e in peer_entries {
                self.wants.peer_wants.sent_at(e.cid, now);
            }
            for e in bcst_entries {
                self.wants.bcst_wants.sent_at(e.cid, now);
            }
        };

        // Set a timer to wait for responses.
        self.simulate_dont_have_with_timeout(wantlist).await;

        // If the message was too big and only a subset of wants could be sent
        // schedule sending the rest of the wants in the next iteration of the event loop.
        if self.wants.has_pending_work() {
            self.signal_work();
        }
        false
    }

    async fn simulate_dont_have_with_timeout(&mut self, wantlist: Vec<Entry>) {
        // Get the Cid of each want-block that expects a DONT_HAVE reponse.
        let pending_wants: Vec<Cid> = wantlist
            .iter()
            .filter_map(|entry| {
                if entry.want_type == WantType::Block && entry.send_dont_have {
                    // check if the block was already sent
                    if self.wants.peer_wants.sent.contains(&entry.cid) {
                        return Some(entry.cid);
                    }
                }
                None
            })
            .collect();

        // Add wants to DONT_HAVE timeout manger
        self.dh_timeout_manager.add_pending(&pending_wants).await;
    }

    async fn send_if_ready(&mut self) -> bool {
        if self.wants.has_pending_work() {
            return self.send_message().await;
        }
        false
    }

    async fn handle_response(&mut self, response: Vec<Cid>) {
        let now = Instant::now();
        // Check if the keys in the response correspond to any request that was sent to the peer.
        //
        // - Finde the earliest request so as to calculate the longest latency as we want
        //   to be conservative when setting the timeout.
        // - Ignore latencies that are very long, as these are likely to be outliers caused when
        //   - we send a want to pere A
        //   - peer A does not have the block
        //   - peer A later receives the block from peer B
        //   - peer A sends us HAVE/block

        let mut earliest = None;
        for cid in response {
            if let Some(at) = self.wants.bcst_wants.sent_at.get(&cid) {
                if (earliest.is_none() || at < earliest.as_ref().unwrap())
                    && now - *at < self.config.max_valid_latency
                {
                    earliest = Some(*at);
                }
                self.wants.bcst_wants.clear_sent_at(&cid);
            }
            if let Some(at) = self.wants.peer_wants.sent_at.get(&cid) {
                if (earliest.is_none() || at < earliest.as_ref().unwrap())
                    && now - *at < self.config.max_valid_latency
                {
                    earliest = Some(*at);
                    // Clear out the sent time, as we want to only record the latency
                    // between the request and the first response.
                    self.wants.peer_wants.clear_sent_at(&cid);
                }
            }
        }

        if let Some(earliest) = earliest {
            self.dh_timeout_manager
                .update_message_latency(now - earliest)
                .await;
        }
    }

    /// Convert the lists of wants into a bitswap message
    async fn extract_outgoing_message(
        &mut self,
    ) -> Result<(
        BitswapMessage,
        &MessageSender,
        Vec<super::wantlist::Entry>,
        Vec<super::wantlist::Entry>,
    )> {
        if self.sender.is_none() {
            let sender = self
                .network
                .new_message_sender(self.peer, self.msg_sender_config.clone())
                .await?;
            self.sender = Some(sender);
        }
        let sender = self.sender.as_ref().unwrap();

        let supports_have = sender.supports_have();

        let (mut peer_entries, mut bcst_entries, mut cancels) = {
            let mut peer_entries: Vec<_> = self.wants.peer_wants.pending.entries().collect();
            if !supports_have {
                // Remove want haves
                peer_entries.retain(|entry| {
                    if entry.want_type == WantType::Have {
                        self.wants
                            .peer_wants
                            .remove_type(&entry.cid, WantType::Have);
                        false
                    } else {
                        true
                    }
                });
            }
            let bcst_entries: Vec<_> = self.wants.bcst_wants.pending.entries().collect();
            let cancels: Vec<_> = self.wants.cancels.iter().cloned().collect();
            (peer_entries, bcst_entries, cancels)
        };

        debug!("pending bcst: {:?}", bcst_entries);
        debug!("pending peer: {:?}", peer_entries);
        debug!(
            "pending cancels: {:?}",
            cancels.iter().map(|s| s.to_string()).collect::<Vec<_>>()
        );
        // We prioritize cancels, then regular wants, then broadcast wants.

        let mut msg_size = 0;
        let mut sent_cancels = 0;
        let mut sent_peer_entries = 0;
        let mut sent_bcst_entries = 0;
        let mut done = false;

        let mut msg = BitswapMessage::default();

        // add cancels
        for c in &cancels {
            msg_size += msg.cancel(*c);
            sent_cancels += 1;

            if msg_size >= self.config.max_message_size {
                done = true;
                break;
            }
        }

        if !done {
            // add wants, if there are too many entires for a single message, sort by
            // by priority.
            for entry in &peer_entries {
                msg_size += msg.add_entry(entry.cid, entry.priority, entry.want_type, true);
                sent_peer_entries += 1;

                if msg_size >= self.config.max_message_size {
                    done = true;
                    break;
                }
            }
        }

        if !done {
            // add each broadcast want-have to the message

            for entry in &bcst_entries {
                // Broadcast wants are sent as want-have
                let want_type = if supports_have {
                    WantType::Have
                } else {
                    WantType::Block
                };
                msg_size += msg.add_entry(entry.cid, entry.priority, want_type, false);
                sent_bcst_entries += 1;

                if msg_size >= self.config.max_message_size {
                    break;
                }
            }
        }

        // Finally mark sent and remove any entries from our message that we've decided to cancel at the last minute.
        {
            // shorten to actually sent
            peer_entries.truncate(sent_peer_entries);
            peer_entries.retain(|entry| {
                if !self.wants.peer_wants.mark_sent(entry) {
                    // It changed
                    msg.remove(&entry.cid);
                    false
                } else {
                    true
                }
            });

            // shorten to actually sent
            bcst_entries.truncate(sent_bcst_entries);
            bcst_entries.retain(|entry| {
                if !self.wants.bcst_wants.mark_sent(entry) {
                    // It changed
                    msg.remove(&entry.cid);
                    false
                } else {
                    true
                }
            });

            // shorten to actually sent
            cancels.truncate(sent_cancels);
            for cancel in &cancels {
                if !self.wants.cancels.contains(cancel) {
                    msg.remove(cancel);
                } else {
                    self.wants.cancels.remove(cancel);
                }
            }
        }
        debug!("got done {}", done);

        Ok((msg, sender, peer_entries, bcst_entries))
    }

    /// Signal the event loop that there is new work.
    fn signal_work(&self) {
        // Ignore error, we only want to make sure the loop is aware that there is work to be done.
        let _ = self.outgoing_work.0.try_send(Instant::now());
    }
}