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

pub trait MaybeSendAddr {

    fn maybe_send_addr(self: Arc<Self>, 
        node:         Amo<Box<dyn NodeInterface>>,
        peer:         &mut Peer,
        current_time: OffsetDateTime /* micros */);
}

impl MaybeSendAddr for PeerManager {

    /**
      | Send `addr` messages on a regular schedule.
      |
      */
    fn maybe_send_addr(self: Arc<Self>, 
        node:         Amo<Box<dyn NodeInterface>>,
        peer:         &mut Peer,
        current_time: OffsetDateTime /* micros */)  {

        // Nothing to do for non-address-relay
        // peers
        if !peer.addr_relay_enabled.load(atomic::Ordering::Relaxed) {
            return;
        }

        let mut guard = peer.addr_send_times_mutex.lock();

        // Periodically advertise our local
        // address to the peer.
        if *LISTEN 
        && !self.chainman.get().active_chainstate().is_initial_block_download() 
        && guard.next_local_addr_send < Some(current_time) {

            // If we've sent before, clear the
            // bloom filter for the peer, so that
            // our self-announcement will actually
            // go out.
            //
            // This might be unnecessary if the
            // bloom filter has already rolled
            // over since our last
            // self-announcement, but there is
            // only a small bandwidth cost that we
            // can incur by doing this (which
            // happens once a day on average).
            if guard.next_local_addr_send != None {
                peer.addr_known.as_mut().unwrap().reset();
            }

            let local_addr: Option::<Address> = get_local_addr_for_peer(
                node.clone()
            );

            if let Some(local_addr) = local_addr {

                let mut insecure_rand = FastRandomContext::default();

                peer.push_address(
                    &local_addr, 
                    &mut insecure_rand
                );
            }

            guard.next_local_addr_send = Some(poisson_next_send(current_time,AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL));
        }

        // We sent an `addr` message to this peer
        // recently. Nothing more to do.
        //
        if Some(current_time) <= guard.next_addr_send {
            return;
        }

        guard.next_addr_send = Some(poisson_next_send(current_time,AVG_ADDRESS_BROADCAST_INTERVAL));

        let mut guard = peer.addrs_to_send.lock();

        if !assume!(guard.len() <= MAX_ADDR_TO_SEND) {

            // Should be impossible since we
            // always check len before adding to
            // m_addrs_to_send. Recover by
            // trimming the vector.
            //
            guard.resize(MAX_ADDR_TO_SEND, Default::default());
        }

        // Remove addr records that the peer
        // already knows about, and add new addrs
        // to the m_addr_known filter on the same
        // pass.
        //
        let mut addr_already_known = |addr: &Address| {

            let ret: bool 
                = peer
                .addr_known
                .as_ref()
                .unwrap()
                .contains_key(
                    &addr.get_key()
                );

            if !ret {
                peer.addr_known.as_mut().unwrap().insert_key(&addr.get_key());
            }

            return ret;
        };

        guard.retain(|item| 

            !addr_already_known(item)
        );

        // No addr messages to send
        if guard.is_empty() {
            return;
        }

        let mut msg_type: Option<String> = None;

        let mut make_flags: i32 = 0;

        if peer.wants_addrv2.load(atomic::Ordering::Relaxed) {
            msg_type = Some(NetMsgType::ADDRV2.into());
            make_flags = ADDRV2_FORMAT;
        } else {
            msg_type = Some(NetMsgType::ADDR.into());
            make_flags = 0;
        }

        let msg = {

            let common_version = node.get().get_common_version();

            let msg_maker      = NetMsgMaker::new(common_version);

            msg_maker.make_with_flags(
                make_flags, 
                &msg_type.unwrap(), 
                &[ &guard.clone() ]
            )
        };

        self.connman.get_mut().push_message(
            &mut node.get_mut(), 
            msg
        );

        guard.clear();

        // we only send the big addr message once
        if guard.capacity() > 40 {
            guard.shrink_to_fit();
        }
    }
}