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
use std::{
    collections::HashSet,
    fmt::{Display, Error as FmtError, Formatter},
    time::Duration,
};

use rand::{thread_rng, Rng};

use crate::{
    sync::{data::PreRequest, forest::Interest, handler::InterestProvider, Justification, PeerId},
    BlockId,
};

const MIN_DELAY: Duration = Duration::from_millis(300);
const ADDITIONAL_DELAY: Duration = Duration::from_millis(200);

// The delay is the minimum delay, plus uniformly randomly chosen multiple of additional delay,
// linear with the ettempt number.
fn delay_for_attempt(attempt: u32) -> Duration {
    MIN_DELAY
        + ADDITIONAL_DELAY
            .mul_f32(thread_rng().gen())
            .saturating_mul(attempt)
}

/// A task for requesting blocks. Keeps track of how many times it was executed.
pub struct RequestTask {
    id: BlockId,
    tries: u32,
}

impl Display for RequestTask {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
        write!(f, "block request for {:?}, attempt {}", self.id, self.tries)
    }
}

type DelayedTask = (RequestTask, Duration);

/// What do to with the task, either ignore or perform a request and add a delayed task.
pub enum Action<I: PeerId> {
    Ignore,
    Request(PreRequest<I>, DelayedTask),
}

impl RequestTask {
    /// A new task for requesting block with the provided ID.
    pub fn new(id: BlockId) -> Self {
        RequestTask { id, tries: 0 }
    }

    /// Process the task.
    pub fn process<I, J>(self, interest_provider: InterestProvider<I, J>) -> Action<I>
    where
        I: PeerId,
        J: Justification,
    {
        let RequestTask { id, tries } = self;
        match interest_provider.get(&id) {
            Interest::Required {
                branch_knowledge,
                know_most,
            } => {
                // Every second time we request from a random peer rather than the one we expect to
                // have it.
                let know_most = match tries % 2 == 0 {
                    true => know_most,
                    false => HashSet::new(),
                };
                let tries = tries + 1;
                Action::Request(
                    PreRequest::new(id.clone(), branch_knowledge, know_most),
                    (
                        RequestTask {
                            id: id.clone(),
                            tries,
                        },
                        delay_for_attempt(tries),
                    ),
                )
            }
            Interest::Uninterested => Action::Ignore,
        }
    }
}