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

pub trait MaybeSetPeerAsAnnouncingHeaderAndIds {

    fn maybe_set_peer_as_announcing_header_and_ids(self: Arc<Self>, nodeid: NodeId);
}

impl MaybeSetPeerAsAnnouncingHeaderAndIds for PeerManager {

    /**
      | When a peer sends us a valid block, instruct
      | it to announce blocks to us using CMPCTBLOCK
      | if possible by adding its nodeid to the
      | end of lNodesAnnouncingHeaderAndIDs,
      | and keeping that list under a certain
      | size by removing the first element if
      | necessary.
      |
      */
    #[EXCLUSIVE_LOCKS_REQUIRED(CS_MAIN)]
    fn maybe_set_peer_as_announcing_header_and_ids(self: Arc<Self>, nodeid: NodeId)  {

        assert_lock_held!(CS_MAIN);

        // Never request high-bandwidth mode from
        // peers if we're blocks-only. Our mempool
        // will not contain the transactions
        // necessary to reconstruct the compact
        // block.
        if self.ignore_incoming_txs {
            return;
        }

        let nodestate: Amo<NodeState> = create_state(nodeid);

        if nodestate.is_none() || !nodestate.get().supports_desired_cmpct_version.load(atomic::Ordering::Relaxed) {
            //  Never ask from peers who can't provide witnesses.
            return;
        }
        
        if nodestate.get().provides_header_and_ids.load(atomic::Ordering::Relaxed) {

            let mut num_outbound_hb_peers: i32 = 0;

            let inner = self.inner.lock();

            let mut it = inner.l_nodes_announcing_header_and_ids.iter().peekable();

            while it.peek().is_some() {

                if **it.peek().unwrap() == nodeid {

                    let idx: usize = (**it.peek().unwrap()).try_into().unwrap();

                    self.inner.lock().l_nodes_announcing_header_and_ids.remove(idx);

                    self.inner.lock().l_nodes_announcing_header_and_ids.push_back(nodeid);

                    return;
                }

                let state: Amo<NodeState> = create_state(**it.peek().unwrap());

                if state.is_some() && !state.get().is_inbound.load(atomic::Ordering::Relaxed) {
                    {
                        num_outbound_hb_peers += 1;
                        num_outbound_hb_peers
                    };
                }

                it.next();
            }

            if nodestate.get().is_inbound.load(atomic::Ordering::Relaxed) {

                // If we're adding an inbound HB
                // peer, make sure we're not
                // removing our last outbound HB
                // peer in the process.
                if self.inner.lock().l_nodes_announcing_header_and_ids.len() >= 3 
                && num_outbound_hb_peers == 1 {

                    let remove_node: Amo<NodeState> 
                    = create_state(
                        *self.inner.lock().l_nodes_announcing_header_and_ids.front().unwrap()
                    );

                    if remove_node.is_some() && !remove_node.get().is_inbound.load(atomic::Ordering::Relaxed) {

                        // Put the HB outbound
                        // peer in the second
                        // slot, so that it
                        // doesn't get removed.
                        self.inner.lock().l_nodes_announcing_header_and_ids.swap(0,1);
                    }
                }
            }

            let cself = self.clone();

            let mut maybe_push_message = move |pfrom: Amo<Box<dyn NodeInterface>>| {

                let cself = cself.clone();

                // EXCLUSIVE_LOCKS_REQUIRED(::CS_MAIN)
                assert_lock_held!(CS_MAIN);

                let mut n_cmpctblock_version: u64 = 2;

                if cself.inner.lock().l_nodes_announcing_header_and_ids.len() >= 3 {

                    let ccself = cself.clone();

                    let push_message = move |pnode_stop: Amo<Box<dyn NodeInterface>>| {

                        let ccself = ccself.clone();


                        let msg = {

                            let common_version = pnode_stop.get().get_common_version();
                            let msg_maker      = NetMsgMaker::new(common_version);

                            msg_maker.make(
                                NetMsgType::SENDCMPCT, 
                                &[
                                /*fAnnounceUsingCMPCTBLOCK=*/ &false, 
                                &n_cmpctblock_version
                                ]
                            )
                        };

                        ccself.connman.get_mut().push_message(
                            &mut pnode_stop.get_mut(), 
                            msg 
                        );

                        let mut node_stop = pnode_stop.get_mut();

                        // save BIP152
                        // bandwidth state: we
                        // select peer to be
                        // low-bandwidth
                        node_stop.set_bip152_highbandwidth_to(false);

                        return true;
                    };

                    // As per BIP152, we only
                    // get 3 of our peers to
                    // announce blocks using
                    // compact encodings.
                    cself.connman.get_mut().for_node( 
                        *cself.inner.lock().l_nodes_announcing_header_and_ids.front().unwrap(),
                        &push_message
                    );

                    cself.inner.lock().l_nodes_announcing_header_and_ids.pop_front();
                }

                let msg = {

                    let common_version = pfrom.get().get_common_version();
                    let msg_maker      = NetMsgMaker::new(common_version);

                    msg_maker 
                        .make(
                            NetMsgType::SENDCMPCT, 
                            &[
                            /*fAnnounceUsingCMPCTBLOCK=*/ &true, 
                            &n_cmpctblock_version
                            ]
                        )
                };

                cself.connman.get_mut().push_message(
                    &mut pfrom.get_mut(), 
                    msg
                );

                // save BIP152 bandwidth
                // state: we select peer to be
                // high-bandwidth
                pfrom.get_mut().set_bip152_highbandwidth_to(true);

                cself.inner.lock().l_nodes_announcing_header_and_ids.push_back(
                    pfrom.get().get_id()
                );

                return true;
            };

            self.connman
                .get_mut()
                .for_node_mut(
                    nodeid, 
                    &mut maybe_push_message
                );
        }
    }
}