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
crate::ix!();

pub trait ReattemptInitialBroadcast {

    fn reattempt_initial_broadcast(
        self:      Arc<Self>, 
        scheduler: Arc<Mutex<Scheduler>>
    );
}

impl ReattemptInitialBroadcast for PeerManager {

    /**
      | Retrieve unbroadcast transactions
      | from the mempool and reattempt sending
      | to peers
      |
      */
    fn reattempt_initial_broadcast(
        self:      Arc<Self>, 
        scheduler: Arc<Mutex<Scheduler>>)
    {
        let mut mempool = self.mempool.get_mut();

        let unbroadcast_txids: HashSet::<u256> 
        = mempool.get_unbroadcast_txs();

        for txid in unbroadcast_txids.iter() {

            let tx: TransactionRef = mempool.get(txid);

            if tx.is_some() {

                let mut guard = CS_MAIN.lock();

                self.clone().relay_transaction(
                    txid, 
                    tx.get().get_witness_hash()
                );

            } else {

                mempool.remove_unbroadcast_tx(txid, Some(true));
            }
        }

        /**
          | Schedule next run for 10-15 minutes in
          | the future.
          |
          | We add randomness on every cycle to
          | avoid the possibility of P2P
          | fingerprinting.
          */
        let delta: Duration = Duration::minutes(10) + get_random_duration(Duration::minutes(5));

        let cself = self.clone();
        let cscheduler = scheduler.clone();

        let closure = Box::new(
            move || { 

                let cself      = cself.clone();
                let cscheduler = cscheduler.clone();

                cself.reattempt_initial_broadcast(cscheduler); 
            }
        );

        scheduler.lock().schedule_from_now(closure, delta);
    }
}