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

pub trait ProcessHeadersMessage {

    fn process_headers_basic(self: Arc<Self>, 
        pfrom:             &mut AmoWriteGuard<Box<dyn NodeInterface>>,
        peer:              &Peer,
        headers:           &Vec<BlockHeader>,
        via_compact_block: bool);
}
    
impl ProcessHeadersMessage for PeerManager {

    /**
      | Process a single headers message from
      | a peer.
      |
      */
    fn process_headers_basic(self: Arc<Self>, 
        mut pfrom:         &mut AmoWriteGuard<Box<dyn NodeInterface>>,
        peer:              &Peer,
        headers:           &Vec<BlockHeader>,
        via_compact_block: bool)  {

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

        let n_count: usize = headers.len();

        if n_count == 0 {
            // Nothing interesting. Stop asking
            // this peers for more headers.
            return;
        }

        let mut received_new_header: bool = false;

        let mut pindex_last: Option<Arc<BlockIndex>> = None;

        {
            let mut guard = CS_MAIN.lock();

            let nodestate: Amo<NodeState> = create_state(pfrom.get_id());

            //  If this looks like it could be
            //  a block announcement (nCount
            //  < MAX_BLOCKS_TO_ANNOUNCE), use
            //  special logic for handling headers
            //  that don't connect:
            //
            //  - Send a getheaders message in
            //  response to try to connect the
            //  chain.
            //
            //  - The peer can send up to
            //    MAX_UNCONNECTING_HEADERS in
            //    a row that don't connect before
            //    giving DoS points
            //
            //  - Once a headers message is
            //    received that is valid and does
            //    connect, nUnconnectingHeaders
            //    gets reset back to 0.
            if self.chainman.get()
                .inner
                .blockman
                .lookup_block_index(&headers[0].hash_prev_block).is_none()
            && n_count < MAX_BLOCKS_TO_ANNOUNCE.try_into().unwrap()
            {

                nodestate.get_mut().n_unconnecting_headers.fetch_add(1,atomic::Ordering::Relaxed);

                self.connman.get_mut().push_message(
                    &mut *pfrom, 
                    msg_maker.make(
                        NetMsgType::GETHEADERS, 
                        &[
                            &self.chainman.get().active_chain().get_locator(PINDEX_BEST_HEADER.lock().clone()), 
                            &u256::default()
                        ]
                    )
                );

                log_print!(
                    LogFlags::NET, 
                    "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n", 
                    headers[0].get_hash().to_string(), 
                    headers[0].hash_prev_block().to_string(), 
                    (*PINDEX_BEST_HEADER.lock()).n_height, 
                    pfrom.get_id(), 
                    (*nodestate).n_unconnecting_headers
                );

                // Set hashLastUnknownBlock for
                // this peer, so that if we
                // eventually get the headers
                // - even from a different peer
                // - we can use this peer to
                // download.
                self.clone().update_block_availability(
                    pfrom.get_id(),
                    &headers.last().unwrap().get_hash()
                );

                if nodestate.get().n_unconnecting_headers.load(atomic::Ordering::Relaxed) % MAX_UNCONNECTING_HEADERS == 0 {

                    self.misbehaving(
                        pfrom.get_id(), 
                        20, 
                        format!(
                            "{} non-connecting headers",
                            nodestate.get().n_unconnecting_headers.load(atomic::Ordering::Relaxed)
                        ).as_str()
                    );
                }

                return;
            }

            let mut hash_last_block = u256::default();

            for header in headers.iter() {

                if !hash_last_block.is_null() 
                && header.hash_prev_block != hash_last_block 
                {

                    self.misbehaving(
                        pfrom.get_id(), 
                        20, 
                        "non-continuous headers sequence"
                    );

                    return;
                }

                hash_last_block = header.get_hash();
            }

            // If we don't have the last header,
            // then they'll have given us
            // something new (if these headers are
            // valid).
            if self.chainman.get()
                .inner
                .blockman
                .lookup_block_index(&hash_last_block)
                .is_some()
            {
                received_new_header = true;
            }
        }

        let mut state = BlockValidationState::default();

        if !self.chainman.get_mut()
            .process_new_block_headers(
                headers, 
                &mut state, 
                &self.chainparams, 
                pindex_last.clone()) 
        {
            if state.is_invalid() {

                self.maybe_punish_node_for_block(
                    pfrom.get_id(), 
                    &state, 
                    via_compact_block, 
                    Some("invalid header received")
                );

                return;
            }
        }

        {
            let mut guard = CS_MAIN.lock();

            let nodestate: Amo<NodeState> = create_state(pfrom.get_id());

            if nodestate.get().n_unconnecting_headers.load(atomic::Ordering::Relaxed) > 0 {

                log_print!(
                    LogFlags::NET, 
                    "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", 
                    pfrom.get_id(), 
                    (*nodestate).n_unconnecting_headers
                );
            }

            nodestate.get_mut().n_unconnecting_headers.store(0,atomic::Ordering::Relaxed);

            assert!(pindex_last.is_some());

            self.clone().update_block_availability(
                pfrom.get_id(), 
                &pindex_last.as_ref().unwrap().get_block_hash()
            );

            // From here, pindexBestKnownBlock
            // should be guaranteed to be
            // non-null, because it is set in
            // UpdateBlockAvailability. Some
            // nullptr checks are still present,
            // however, as belt-and-suspenders.
            if received_new_header 
            && pindex_last.as_ref().unwrap().n_chain_work > self.chainman.get().active_chain().tip().as_ref().unwrap().n_chain_work {
                nodestate.get_mut().last_block_announcement = Some(get_datetime());
            }

            if n_count == MAX_HEADERS_RESULTS.try_into().unwrap() {

                // Headers message had its maximum
                // size; the peer may have more
                // headers.
                //
                // TODO: optimize: if pindexLast
                // is an ancestor of
                // m_chainman.ActiveChain().Tip or
                // pindexBestHeader, continue from
                // there instead.
                log_print!(
                    LogFlags::NET,
                    "more getheaders (%d) to end to peer=%d (startheight:%d)\n",
                    pindex_last.get().n_height,
                    pfrom.get_id(),
                    peer.starting_height
                );

                self.connman.get_mut().push_message(
                    &mut *pfrom, 
                    msg_maker.make(
                        NetMsgType::GETHEADERS, 
                        &[
                            &self.chainman.get().active_chain().get_locator(pindex_last.clone()), 
                            &u256::default()
                        ]
                    )
                );
            }

            // If this set of headers is valid and
            // ends in a block with at least as
            // much work as our tip, download as
            // much as possible.
            if self.clone().can_direct_fetch() 
            && pindex_last.as_ref().unwrap().is_valid(Some(BlockStatus::BLOCK_VALID_TREE)) 
            && self.chainman.get().active_chain().tip().as_ref().unwrap().n_chain_work <= pindex_last.as_ref().unwrap().n_chain_work {

                let mut to_fetch: Vec<Option<Arc<BlockIndex>>> = vec![];

                let mut pindex_walk: Option<Arc<BlockIndex>> = pindex_last.clone();

                // Calculate all the blocks we'd
                // need to switch to pindexLast,
                // up to a limit.
                while pindex_walk.is_some() 
                && !self.chainman.get().active_chain().contains(pindex_walk.clone()) 
                && to_fetch.len() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER.try_into().unwrap() 
                {
                    if (pindex_walk.as_ref().unwrap().n_status & BlockStatus::BLOCK_HAVE_DATA.bits()) == 0 
                    && !self.is_block_requested(&pindex_walk.as_ref().unwrap().get_block_hash()) 
                    && (
                        !deployment_active_at_with_buried_deployment(
                            pindex_walk.clone().unwrap(),
                            &self.chainparams.get_consensus(),
                            ConsensusBuriedDeployment::DEPLOYMENT_SEGWIT
                        ) 
                        || 
                        create_state(pfrom.get_id()).get().have_witness.load(atomic::Ordering::Relaxed)
                    ) 
                    {
                        //  We don't have this block, and it's not yet in flight.
                        to_fetch.push(pindex_walk.clone());
                    }

                    pindex_walk = pindex_walk.as_ref().unwrap().pprev.clone();
                }

                // If pindexWalk still isn't on
                // our main chain, we're looking
                // at a very large reorg at a time
                // we think we're close to caught
                // up to the main chain -- this
                // shouldn't really happen.  Bail
                // out on the direct fetch and
                // rely on parallel download
                // instead.
                if !self.chainman.get().active_chain().contains(pindex_walk) {

                    log_print!(
                        LogFlags::NET, 
                        "Large reorg, won't direct fetch to %s (%d)\n", 
                        (*pindex_last).get_block_hash().to_string(), 
                        (*pindex_last).n_height
                    );

                } else {

                    let mut get_data: Vec<Inv> = vec![];

                    //  Download as much as possible, from earliest to latest.
                    for pindex in to_fetch.iter().rev() {

                        if nodestate.get().n_blocks_in_flight.load(atomic::Ordering::Relaxed) >= MAX_BLOCKS_IN_TRANSIT_PER_PEER {

                            // Can't download any
                            // more from this peer
                            break;
                        }

                        let n_fetch_flags: GetDataMsg = get_fetch_flags(&***pfrom);

                        get_data.push(
                            Inv::new(
                                (GetDataMsg::MSG_BLOCK | n_fetch_flags).bits(),
                                &(*pindex.as_ref().unwrap()).get_block_hash()
                            )
                        );

                        self.clone().block_requested(
                            pfrom.get_id(), 
                            pindex.clone(), 
                            Default::default()
                        );

                        log_print!(
                            LogFlags::NET, 
                            "Requesting block %s from  peer=%d\n", 
                            (*pindex).get_block_hash().to_string(), 
                            pfrom.get_id()
                        );
                    }

                    if get_data.len() > 1 {
                        log_print!(
                            LogFlags::NET, 
                            "Downloading blocks toward %s (%d) via headers direct fetch\n", 
                            (*pindex_last).get_block_hash().to_string(), 
                            (*pindex_last).n_height
                        );
                    }

                    if get_data.len() > 0 {

                        let inner = self.inner.lock();
                        let mbif = inner.map_blocks_in_flight.lock();


                        if !self.ignore_incoming_txs 
                        && nodestate.get().supports_desired_cmpct_version.load(atomic::Ordering::Relaxed) 
                        && get_data.len() == 1 
                        && mbif.len() == 1 

                        && pindex_last
                            .as_ref()
                            .unwrap()
                            .pprev
                            .as_ref()
                            .unwrap()
                            .is_valid(Some(BlockStatus::BLOCK_VALID_CHAIN)) 
                        {
                            // In any case, we
                            // want to download
                            // using a compact
                            // block, not
                            // a regular one
                            get_data[0] = Inv::new(
                                GetDataMsg::MSG_CMPCT_BLOCK.bits(),
                                &get_data[0].hash
                            );
                        }

                        self.connman.get_mut().push_message(
                            &mut *pfrom, 
                            msg_maker.make(NetMsgType::GETDATA, &[&get_data])
                        );
                    }
                }
            }

            // If we're in IBD, we want outbound
            // peers that will serve us a useful
            // chain. Disconnect peers that are on
            // chains with insufficient work.
            if self.chainman.get().active_chainstate().is_initial_block_download() 
            && n_count != MAX_HEADERS_RESULTS.try_into().unwrap() {

                // When nCount
                // < MAX_HEADERS_RESULTS, we know
                // we have no more headers to
                // fetch from this peer.
                if nodestate.get().pindex_best_known_block.is_some() 
                && nodestate.get().pindex_best_known_block.as_ref().unwrap().n_chain_work < *N_MINIMUM_CHAIN_WORK 
                {
                    // This peer has too little
                    // work on their headers chain
                    // to help us sync --
                    // disconnect if it is an
                    // outbound disconnection
                    // candidate.
                    //
                    // Note: We compare their tip
                    // to nMinimumChainWork
                    // (rather than
                    // m_chainman.ActiveChain().Tip())
                    // because we won't start
                    // block download until we
                    // have a headers chain that
                    // has at least
                    // nMinimumChainWork, even if
                    // a peer has a chain past our
                    // tip, as an anti-DoS
                    // measure.
                    if pfrom.is_outbound_or_block_relay_conn() {

                        log_printf!(
                            "Disconnecting outbound peer %d -- headers chain has insufficient work\n", 
                            pfrom.get_id()
                        );

                        pfrom.mark_for_disconnect();
                    }
                }
            }

            // If this is an outbound full-relay
            // peer, check to see if we should
            // protect it from the bad/lagging
            // chain logic.
            //
            // Note that outbound block-relay
            // peers are excluded from this
            // protection, and thus always subject
            // to eviction under the bad/lagging
            // chain logic.
            //
            // See ChainSyncTimeoutState.
            if !pfrom.marked_for_disconnect() 
            && pfrom.is_full_outbound_conn() 
            && nodestate.get().pindex_best_known_block.is_some() 
            {

                let inner = self.inner.lock();

                let chainman = self.chainman.get();

                let oppfd = inner.outbound_peers_with_protect_from_disconnect.load(atomic::Ordering::Relaxed);

                if oppfd < MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT 

                && nodestate.get().pindex_best_known_block.as_ref().unwrap().n_chain_work 
                    >= chainman.active_chain().tip().as_ref().unwrap().n_chain_work 

                && !nodestate.get().chain_sync.protect 
                {

                    log_print!(
                        LogFlags::NET, 
                        "Protecting outbound peer=%d from eviction\n", 
                        pfrom.get_id()
                    );

                    nodestate.get_mut().chain_sync.protect = true;

                    inner.outbound_peers_with_protect_from_disconnect.fetch_add(1, atomic::Ordering::Relaxed);
                }
            }
        }
    }
}

impl PeerManager {

    pub fn process_headers_message(
        self:               Arc<Self>, 
        peer:               &Option<Peer>,
        msg_maker:          NetMsgMaker,
        pfrom:              &mut AmoWriteGuard<Box<dyn NodeInterface>>,
        msg_type:           &str,
        recv:               &mut DataStream,
        time_received:      &OffsetDateTime /* micros */,
        interrupt_msg_proc: &AtomicBool)  {

        // Ignore headers received while
        // IMPORTING.load(atomic::Ordering::Relaxed)
        if IMPORTING.load(atomic::Ordering::Relaxed) || REINDEX.load(atomic::Ordering::Relaxed) {

            log_print!(
                LogFlags::NET, 
                "Unexpected headers message received from peer %d\n", 
                pfrom.get_id()
            );

            return;
        }

        let mut headers: Vec<BlockHeader> = vec![];

        // Bypass the normal CBlock
        // deserialization, as we don't want
        // to risk deserializing 2000 full
        // blocks.
        let n_count: usize = read_compact_size(recv, None).try_into().unwrap();

        if n_count > MAX_HEADERS_RESULTS.try_into().unwrap() {

            self.misbehaving(
                pfrom.get_id(), 
                20, 
                format!("headers message size = {}",n_count).as_str()
            );

            return;
        }

        headers.resize(n_count, Default::default());

        for n in 0_usize..n_count {

            recv.stream_into(&mut headers[n]);

            // ignore tx count; assume it is
            // 0.
            read_compact_size(recv, None);
        }

        return self.process_headers_basic(
            pfrom,
            peer.as_ref().unwrap(),
            &headers,
            /*via_compact_block=*/ false
        );
    }
}