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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
//!
//! The peer-to-peer address manager.
//!
#![warn(missing_docs)]
use std::net;

use nakamoto_common::bitcoin::network::address::Address;
use nakamoto_common::bitcoin::network::constants::ServiceFlags;

use nakamoto_common::block::time::Clock;
use nakamoto_common::block::time::{LocalDuration, LocalTime};
use nakamoto_common::block::BlockTime;
use nakamoto_common::collections::{HashMap, HashSet};
use nakamoto_common::p2p::peer::{AddressSource, KnownAddress, Source, Store};
use nakamoto_common::p2p::Domain;
use nakamoto_net::Disconnect;

use super::output::{SetTimer, Wire};
use super::Link;

/// Time to wait until a request times out.
pub const REQUEST_TIMEOUT: LocalDuration = LocalDuration::from_mins(1);

/// Idle timeout. Used to run periodic functions.
pub const IDLE_TIMEOUT: LocalDuration = LocalDuration::from_mins(1);

/// Sample timeout. How long before a sampled address can be returned again.
pub const SAMPLE_TIMEOUT: LocalDuration = LocalDuration::from_mins(3);

/// Maximum number of addresses expected in a `addr` message.
const MAX_ADDR_ADDRESSES: usize = 1000;
/// Maximum number of addresses we store for a given address range.
const MAX_RANGE_SIZE: usize = 256;

/// An event emitted by the address manager.
#[derive(Debug, Clone)]
pub enum Event {
    /// Peer addresses have been received.
    AddressesReceived {
        /// Number of addresses received.
        count: usize,
        /// Source of addresses received.
        source: Source,
    },
    /// Address book exhausted.
    AddressBookExhausted,
    /// An error was encountered.
    Error(String),
}

impl std::fmt::Display for Event {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Event::AddressesReceived { count, source } => {
                write!(
                    fmt,
                    "received {} addresse(s) from source `{}`",
                    count, source
                )
            }
            Event::AddressBookExhausted => {
                write!(
                    fmt,
                    "Address book exhausted.. fetching new addresses from peers"
                )
            }
            Event::Error(msg) => {
                write!(fmt, "error: {}", msg)
            }
        }
    }
}

/// Iterator over addresses.
pub struct Iter<F>(F);

impl<F> Iterator for Iter<F>
where
    F: FnMut() -> Option<(Address, Source)>,
{
    type Item = (Address, Source);

    fn next(&mut self) -> Option<Self::Item> {
        (self.0)()
    }
}

impl<P: Store, U, C> AddressManager<P, U, C> {
    /// Check whether we have unused addresses.
    pub fn is_exhausted(&self) -> bool {
        let time = self
            .last_idle
            .expect("AddressManager::is_exhausted: manager must be initialized");

        for (addr, ka) in self.peers.iter() {
            // Unsuccessful attempt to connect.
            if ka.last_attempt.is_some() && ka.last_success.is_none() {
                continue;
            }
            if time - ka.last_sampled.unwrap_or_default() < SAMPLE_TIMEOUT {
                continue;
            }
            if !self.connected.contains(addr) {
                return false;
            }
        }
        true
    }
}

/// Address manager configuration.
#[derive(Debug)]
pub struct Config {
    /// Services required from peers.
    pub required_services: ServiceFlags,
    /// Communication domains we're interested in.
    pub domains: Vec<Domain>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            required_services: ServiceFlags::NONE,
            domains: Domain::all(),
        }
    }
}

/// Manages peer network addresses.
#[derive(Debug)]
pub struct AddressManager<P, U, C> {
    /// Peer address store.
    peers: P,
    bans: HashSet<net::IpAddr>,
    address_ranges: HashMap<u8, HashSet<net::IpAddr>>,
    connected: HashSet<net::IpAddr>,
    sources: HashSet<net::SocketAddr>,
    local_addrs: HashSet<net::SocketAddr>,
    /// The last time we asked our peers for new addresses.
    last_request: Option<LocalTime>,
    /// The last time we idled.
    last_idle: Option<LocalTime>,
    cfg: Config,
    upstream: U,
    rng: fastrand::Rng,
    clock: C,
}

impl<P: Store, U: Wire<Event> + SetTimer, C: Clock> AddressManager<P, U, C> {
    /// Initialize the address manager.
    pub fn initialize(&mut self) {
        self.idle();
    }

    /// Return an iterator over randomly sampled addresses.
    pub fn iter(&mut self, services: ServiceFlags) -> impl Iterator<Item = (Address, Source)> + '_ {
        Iter(move || self.sample(services))
    }

    /// Get addresses from peers.
    pub fn get_addresses(&mut self) {
        for peer in &self.sources {
            self.upstream.get_addr(*peer);
        }
    }

    /// Called when we receive a `getaddr` message.
    pub fn received_getaddr(&mut self, from: &net::SocketAddr) {
        // TODO: We should only respond with peers who were last active within
        // the last 3 hours.
        let mut addrs = Vec::new();

        // Include one random address per address range.
        for range in self.address_ranges.values() {
            let ix = self.rng.usize(..range.len());
            let ip = range.iter().nth(ix).expect("index must be present");
            let ka = self.peers.get(ip).expect("address must exist");

            addrs.push((
                ka.last_active.map(|t| t.block_time()).unwrap_or_default(),
                ka.addr.clone(),
            ));
        }
        self.upstream.addr(*from, addrs);
    }

    /// Called when a tick is received.
    pub fn received_wake(&mut self) {
        let local_time = self.clock.local_time();

        // If we're already using all the addresses we have available, we should fetch more.
        if local_time - self.last_request.unwrap_or_default() >= REQUEST_TIMEOUT
            && self.is_exhausted()
        {
            self.upstream.event(Event::AddressBookExhausted);

            self.get_addresses();
            self.last_request = Some(local_time);
            self.upstream.set_timer(REQUEST_TIMEOUT);
        }

        if local_time - self.last_idle.unwrap_or_default() >= IDLE_TIMEOUT {
            self.idle();
        }
    }

    /// Called when a peer signaled activity.
    pub fn peer_active(&mut self, addr: net::SocketAddr) {
        let time = self.clock.local_time();
        if let Some(ka) = self.peers.get_mut(&addr.ip()) {
            ka.last_active = Some(time);
        }
    }

    /// Called when a peer connection is attempted.
    pub fn peer_attempted(&mut self, addr: &net::SocketAddr) {
        let time = self.clock.local_time();
        // We're only interested in connection attempts for addresses we keep track of.
        if let Some(ka) = self.peers.get_mut(&addr.ip()) {
            ka.last_attempt = Some(time);
        }
    }

    /// Called when a peer has connected.
    pub fn peer_connected(&mut self, addr: &net::SocketAddr) {
        if !self::is_routable(&addr.ip()) || self::is_local(&addr.ip()) {
            return;
        }
        self.connected.insert(addr.ip());
    }

    /// Called when a peer has handshaked.
    pub fn peer_negotiated(&mut self, addr: &net::SocketAddr, services: ServiceFlags, link: Link) {
        let time = self.clock.local_time();

        if !self.connected.contains(&addr.ip()) {
            return;
        }
        if link.is_outbound() {
            self.sources.insert(*addr);
        }

        // We're only interested in peers we already know, eg. from DNS or peer
        // exchange. Peers should only be added to our address book if they are DNS seeds
        // or are discovered via a DNS seed.
        if let Some(ka) = self.peers.get_mut(&addr.ip()) {
            // Only ask for addresses when connecting for the first time.
            if ka.last_success.is_none() {
                self.upstream.get_addr(*addr);
            }
            // Keep track of when the last successful handshake was.
            ka.last_success = Some(time);
            ka.last_active = Some(time);
            ka.addr.services = services;
        }
    }

    /// Called when a peer disconnected.
    pub fn peer_disconnected(
        &mut self,
        addr: &net::SocketAddr,
        reason: Disconnect<super::DisconnectReason>,
    ) {
        if self.connected.remove(&addr.ip()) {
            // Disconnected peers cannot be used as a source for new addresses.
            self.sources.remove(addr);

            // If the reason for disconnecting the peer suggests that we shouldn't try to
            // connect to this peer again, then remove the peer from the address book.
            // Otherwise, we leave it in the address buckets so that it can be chosen
            // in the future.
            if let Disconnect::StateMachine(r) = reason {
                if !r.is_transient() {
                    self.ban(&addr.ip());
                }
            } else if reason.is_dial_err() {
                self.ban(&addr.ip());
            }
        }
    }

    ////////////////////////////////////////////////////////////////////////////

    fn idle(&mut self) {
        // If it's been a while, save addresses to store.
        if let Err(err) = self.peers.flush() {
            self.upstream
                .event(Event::Error(format!("flush to disk failed: {}", err)));
        }
        self.last_idle = Some(self.clock.local_time());
        self.upstream.set_timer(IDLE_TIMEOUT);
    }
}

impl<P: Store, U: Wire<Event>, C: Clock> AddressManager<P, U, C> {
    /// Create a new, empty address manager.
    pub fn new(cfg: Config, rng: fastrand::Rng, peers: P, upstream: U, clock: C) -> Self {
        let ips = peers.iter().map(|(ip, _)| *ip).collect::<Vec<_>>();
        let mut addrmgr = Self {
            cfg,
            peers,
            bans: HashSet::with_hasher(rng.clone().into()),
            address_ranges: HashMap::with_hasher(rng.clone().into()),
            connected: HashSet::with_hasher(rng.clone().into()),
            sources: HashSet::with_hasher(rng.clone().into()),
            local_addrs: HashSet::with_hasher(rng.clone().into()),
            last_request: None,
            last_idle: None,
            upstream,
            rng,
            clock,
        };

        for ip in ips.iter() {
            addrmgr.populate_address_ranges(ip);
        }
        addrmgr
    }

    /// The number of peers known.
    pub fn len(&self) -> usize {
        self.peers.len()
    }

    /// Whether there are any peers known to the address manager.
    pub fn is_empty(&self) -> bool {
        self.peers.is_empty() || self.address_ranges.is_empty()
    }

    #[cfg(test)]
    /// Clear the address manager of all peers.
    pub fn clear(&mut self) {
        self.peers.clear();
        self.address_ranges.clear();
    }

    /// Called when we received an `addr` message from a peer.
    pub fn received_addr(&mut self, peer: net::SocketAddr, addrs: Vec<(BlockTime, Address)>) {
        if addrs.is_empty() || addrs.len() > MAX_ADDR_ADDRESSES {
            // Peer misbehaving, got empty message or too many addresses.
            return;
        }
        let source = Source::Peer(peer);

        self.upstream.event(Event::AddressesReceived {
            count: addrs.len(),
            source,
        });
        self.insert(addrs.into_iter(), source);
    }

    /// Add addresses to the address manager. The input matches that of the `addr` message
    /// sent by peers on the network.
    pub fn insert(
        &mut self,
        addrs: impl IntoIterator<Item = (BlockTime, Address)>,
        source: Source,
    ) {
        let time = self
            .last_idle
            .expect("AddressManager::insert: manager must be initialized before inserting");

        for (last_active, addr) in addrs {
            // Ignore addresses that don't have the required services.
            if !addr.services.has(self.cfg.required_services) {
                continue;
            }
            // Ignore peers with these antiquated services offered. They are very unlikely to
            // support compact filters, or have up-to-date clients.
            if addr.services.has(ServiceFlags::GETUTXO) || addr.services.has(ServiceFlags::BLOOM) {
                continue;
            }
            // Ignore addresses that don't have a "last active" time.
            if last_active == 0 {
                continue;
            }
            // Ignore addresses that are too far into the future.
            if LocalTime::from_block_time(last_active) > time + LocalDuration::from_mins(60) {
                continue;
            }
            // Ignore addresses from unsupported domains.
            let net_addr = match addr.socket_addr() {
                Ok(a) if self.cfg.domains.contains(&Domain::for_address(&a)) => a,
                _ => continue,
            };
            let ip = net_addr.ip();

            // Ensure no self-connections.
            if self.local_addrs.contains(&net_addr) {
                continue;
            }
            // No banned addresses.
            if self.bans.contains(&ip) {
                continue;
            }

            // Ignore non-routable addresses if they come from a peer.
            if !self::is_routable(&ip) {
                continue;
            }

            // Ignore local addresses.
            if self::is_local(&ip) {
                continue;
            }

            let last_active = if last_active == 0 {
                // TODO: Cannot be 'None' due to above condition.
                None
            } else {
                Some(LocalTime::from_block_time(last_active))
            };

            // Record the address, and ignore addresses we already know.
            // Note that this should never overwrite an existing address.
            if !self
                .peers
                .insert(ip, KnownAddress::new(addr.clone(), source, last_active))
            {
                continue;
            }

            self.populate_address_ranges(&net_addr.ip());
        }
    }

    /// Pick an address at random from the set of known addresses.
    ///
    /// This function tries to ensure a good geo-diversity of addresses, such that an adversary
    /// controlling a disproportionately large number of addresses in the same address range does
    /// not have an advantage over other peers.
    ///
    /// This works under the assumption that adversaries are *localized*.
    pub fn sample(&mut self, services: ServiceFlags) -> Option<(Address, Source)> {
        self.sample_with(|ka: &KnownAddress| {
            if !ka.addr.services.has(services) {
                match ka.source {
                    Source::Dns => {
                        // If we've negotiated with this peer and it hasn't signaled the
                        // required services, we know not to return it.
                        // DNS-sourced addresses don't include service information,
                        // so we won't be including these until we know the services.
                    }
                    Source::Imported => {
                        // We expect that imported addresses will always include the correct
                        // service information. Hence, if this one doesn't have the necessary
                        // services, it's safe to skip.
                    }
                    Source::Peer(_) => {
                        // Peer-sourced addresses come with service information. It's safe to
                        // skip this address if it doesn't have the required services.
                    }
                }
                return false;
            }
            true
        })
    }

    /// Sample an address using the provided predicate. Only returns addresses which are `true`
    /// according to the predicate.
    pub fn sample_with(
        &mut self,
        predicate: impl Fn(&KnownAddress) -> bool,
    ) -> Option<(Address, Source)> {
        if self.is_empty() {
            return None;
        }
        let time = self
            .last_idle
            .expect("AddressManager::sample: manager must be initialized before sampling");
        let domains = &self.cfg.domains;

        let mut ranges: Vec<_> = self.address_ranges.values().collect();
        self.rng.shuffle(&mut ranges);

        // First select a random address range.
        for range in ranges.drain(..) {
            assert!(!range.is_empty());

            let mut ips: Vec<_> = range.iter().collect();
            self.rng.shuffle(&mut ips);

            // Then select a random address in that range.
            for ip in ips.drain(..) {
                let ka = self.peers.get_mut(ip).expect("address must exist");

                // If the address domain is unsupported, skip it.
                // Nb. this currently skips Tor addresses too.
                if !ka
                    .addr
                    .socket_addr()
                    .map_or(false, |a| domains.contains(&Domain::for_address(&a)))
                {
                    continue;
                }

                // If the address was already attempted unsuccessfully, skip it.
                if ka.last_attempt.is_some() && ka.last_success.is_none() {
                    continue;
                }
                // If we recently sampled this address, don't return it again.
                if time - ka.last_sampled.unwrap_or_default() < SAMPLE_TIMEOUT {
                    continue;
                }
                // If we're already connected to this address, skip it.
                if self.connected.contains(ip) {
                    continue;
                }
                // If the provided filter doesn't pass, keep looking.
                if !predicate(ka) {
                    continue;
                }
                // Ok, we've found a worthy address!
                ka.last_sampled = Some(time);

                return Some((ka.addr.clone(), ka.source));
            }
        }

        None
    }

    ////////////////////////////////////////////////////////////////////////////

    /// Populate address ranges with an IP. This may remove an existing IP if
    /// its range is full. Returns the range key that was used.
    fn populate_address_ranges(&mut self, ip: &net::IpAddr) -> u8 {
        let key = self::addr_key(ip);
        let range = self.address_ranges.entry(key).or_insert_with({
            let rng = self.rng.clone();

            || HashSet::with_hasher(rng.into())
        });

        // If the address range is already full, remove a random address
        // before inserting this new one.
        if range.len() == MAX_RANGE_SIZE {
            let ix = self.rng.usize(..range.len());
            let addr = range
                .iter()
                .cloned()
                .nth(ix)
                .expect("the range is not empty");

            range.remove(&addr);
            self.peers.remove(&addr);
        }
        range.insert(*ip);

        key
    }

    /// Remove an address from the address book and prevent it from being sampled again.
    fn ban(&mut self, addr: &net::IpAddr) -> bool {
        debug_assert!(!self.connected.contains(addr));

        let key = self::addr_key(addr);

        if let Some(range) = self.address_ranges.get_mut(&key) {
            range.remove(addr);

            // TODO: Persist bans.
            self.peers.remove(addr);
            self.bans.insert(*addr);

            if range.is_empty() {
                self.address_ranges.remove(&key);
            }
            return true;
        }
        false
    }
}

impl<P: Store, U: Wire<Event> + SetTimer, C: Clock> AddressSource for AddressManager<P, U, C> {
    fn sample(&mut self, services: ServiceFlags) -> Option<(Address, Source)> {
        AddressManager::sample(self, services)
    }

    fn record_local_address(&mut self, addr: net::SocketAddr) {
        self.local_addrs.insert(addr);
    }

    fn iter(&mut self, services: ServiceFlags) -> Box<dyn Iterator<Item = (Address, Source)> + '_> {
        Box::new(AddressManager::iter(self, services))
    }
}

/// Check whether an IP address is globally routable.
pub fn is_routable(addr: &net::IpAddr) -> bool {
    match addr {
        net::IpAddr::V4(addr) => ipv4_is_routable(addr),
        net::IpAddr::V6(addr) => ipv6_is_routable(addr),
    }
}

/// Check whether an IP address is locally routable.
pub fn is_local(addr: &net::IpAddr) -> bool {
    match addr {
        net::IpAddr::V4(addr) => {
            addr.is_private() || addr.is_loopback() || addr.is_link_local() || addr.is_unspecified()
        }
        net::IpAddr::V6(_) => false,
    }
}

/// Get the 8-bit key of an IP address. This key is based on the IP address's
/// range, and is used as a key to group IP addresses by range.
fn addr_key(ip: &net::IpAddr) -> u8 {
    match ip {
        net::IpAddr::V4(ip) => {
            // Use the /16 range (first two components) of the IP address to key into the
            // range buckets.
            //
            // Eg. 124.99.123.1 and 124.54.123.1 would be placed in
            // different buckets, but 100.99.43.12 and 100.99.12.8
            // would be placed in the same bucket.
            let octets: [u8; 4] = ip.octets();
            let bits: u16 = (octets[0] as u16) << 8 | octets[1] as u16;

            (bits % u8::MAX as u16) as u8
        }
        net::IpAddr::V6(ip) => {
            // Use the first 32 bits of an IPv6 address to as a key.
            let segments: [u16; 8] = ip.segments();
            let bits: u32 = (segments[0] as u32) << 16 | segments[1] as u32;

            (bits % u8::MAX as u32) as u8
        }
    }
}

/// Check whether an IPv4 address is globally routable.
///
/// This code is adapted from the Rust standard library's `net::Ipv4Addr::is_global`. It can be
/// replaced once that function is stabilized.
fn ipv4_is_routable(addr: &net::Ipv4Addr) -> bool {
    // Check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
    // globally routable addresses in the 192.0.0.0/24 range.
    if u32::from(*addr) == 0xc0000009 || u32::from(*addr) == 0xc000000a {
        return true;
    }
    !addr.is_private()
        && !addr.is_loopback()
        && !addr.is_link_local()
        && !addr.is_broadcast()
        && !addr.is_documentation()
        // Make sure the address is not in 0.0.0.0/8.
        && addr.octets()[0] != 0
}

/// Check whether an IPv6 address is globally routable.
///
/// For now, this always returns `true`, as IPv6 addresses
/// are not fully supported.
fn ipv6_is_routable(_addr: &net::Ipv6Addr) -> bool {
    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fsm;
    use std::collections::HashMap;
    use std::iter;

    use nakamoto_common::block::time::RefClock;
    use nakamoto_common::network::Network;
    use quickcheck::TestResult;
    use quickcheck_macros::quickcheck;

    #[test]
    fn test_sample_empty() {
        let mut addrmgr = AddressManager::new(
            Config::default(),
            fastrand::Rng::new(),
            HashMap::new(),
            (),
            LocalTime::now(),
        );

        assert!(addrmgr.sample(ServiceFlags::NONE).is_none());
    }

    #[test]
    fn test_known_addresses() {
        let time = LocalTime::now();
        let mut addrmgr = AddressManager::new(
            Config::default(),
            fastrand::Rng::new(),
            HashMap::new(),
            (),
            time,
        );
        let source = Source::Dns;
        let services = ServiceFlags::NETWORK;

        addrmgr.initialize();
        addrmgr.insert(
            [
                (
                    time.block_time(),
                    Address::new(&([33, 33, 33, 33], 8333).into(), services),
                ),
                (
                    time.block_time(),
                    Address::new(&([44, 44, 44, 44], 8333).into(), services),
                ),
            ],
            source,
        );

        let addr: &net::SocketAddr = &([33, 33, 33, 33], 8333).into();

        let ka = addrmgr.peers.get(&addr.ip()).unwrap();
        assert!(ka.last_success.is_none());
        assert!(ka.last_attempt.is_none());
        assert!(ka.last_active.is_some());
        assert!(ka.last_sampled.is_none());
        assert_eq!(
            ka.last_active,
            Some(LocalTime::from_block_time(time.block_time()))
        );

        addrmgr.peer_attempted(addr);

        let ka = addrmgr.peers.get(&addr.ip()).unwrap();
        assert!(ka.last_success.is_none());
        assert!(ka.last_attempt.is_some());
        assert!(ka.last_active.is_some());
        assert!(ka.last_sampled.is_none());

        // When a peer is connected, it is not yet considered a "success".
        addrmgr.peer_connected(addr);

        let ka = addrmgr.peers.get(&addr.ip()).unwrap();
        assert!(ka.last_success.is_none());
        assert!(ka.last_attempt.is_some());
        assert!(ka.last_active.is_some());
        assert!(ka.last_sampled.is_none());

        // Only when it is negotiated is it a "success".
        addrmgr.peer_negotiated(addr, services, Link::Outbound);

        let ka = addrmgr.peers.get(&addr.ip()).unwrap();
        assert!(ka.last_success.is_some());
        assert!(ka.last_attempt.is_some());
        assert!(ka.last_active.is_some());
        assert!(ka.last_sampled.is_none());
        assert_eq!(ka.last_active, ka.last_success);

        let (sampled, _) = addrmgr.sample(services).unwrap();
        assert_eq!(
            sampled.socket_addr().ok(),
            Some(([44, 44, 44, 44], 8333).into())
        );
        let ka = addrmgr.peers.get(&[44, 44, 44, 44].into()).unwrap();
        assert!(ka.last_success.is_none());
        assert!(ka.last_attempt.is_none());
        assert!(ka.last_active.is_some());
        assert!(ka.last_sampled.is_some());
    }

    #[test]
    fn test_is_exhausted() {
        let time = LocalTime::now();
        let mut addrmgr = AddressManager::new(
            Config::default(),
            fastrand::Rng::new(),
            HashMap::new(),
            (),
            time,
        );
        let source = Source::Dns;
        let services = ServiceFlags::NETWORK;

        addrmgr.initialize();
        addrmgr.insert(
            [(
                time.block_time(),
                Address::new(&net::SocketAddr::from(([33, 33, 33, 33], 8333)), services),
            )],
            source,
        );
        assert!(!addrmgr.is_exhausted());

        // Once a peer connects, it's no longer available as an address.
        addrmgr.peer_connected(&([33, 33, 33, 33], 8333).into());
        assert!(addrmgr.is_exhausted());

        addrmgr.insert(
            [(
                time.block_time(),
                Address::new(&net::SocketAddr::from(([44, 44, 44, 44], 8333)), services),
            )],
            source,
        );
        assert!(!addrmgr.is_exhausted());

        // If a peer has been attempted with no success, it should not count towards the available
        // addresses.
        addrmgr.peer_attempted(&([44, 44, 44, 44], 8333).into());
        assert!(addrmgr.is_exhausted());

        // If a peer has been connected to successfully, and then disconnected for a transient
        // reason, its address should be once again available.
        addrmgr.peer_connected(&([44, 44, 44, 44], 8333).into());
        addrmgr.peer_negotiated(&([44, 44, 44, 44], 8333).into(), services, Link::Outbound);
        addrmgr.peer_disconnected(
            &([44, 44, 44, 44], 8333).into(),
            fsm::DisconnectReason::PeerTimeout("timeout").into(),
        );
        assert!(!addrmgr.is_exhausted());
        assert!(addrmgr.sample(services).is_some());
        assert!(addrmgr.sample(services).is_none());
        assert!(addrmgr.is_exhausted());

        // If a peer has been attempted and then disconnected, it is not available
        // for sampling, even when the disconnect reason is transient.
        addrmgr.insert(
            [(
                time.block_time(),
                Address::new(&net::SocketAddr::from(([55, 55, 55, 55], 8333)), services),
            )],
            source,
        );
        addrmgr.sample(services);
        addrmgr.peer_attempted(&([55, 55, 55, 55], 8333).into());
        addrmgr.peer_connected(&([55, 55, 55, 55], 8333).into());
        addrmgr.peer_disconnected(
            &([55, 55, 55, 55], 8333).into(),
            fsm::DisconnectReason::PeerTimeout("timeout").into(),
        );
        assert!(addrmgr.sample(services).is_none());
    }

    #[test]
    fn test_disconnect_rediscover() {
        // Check that if we re-discover an address after permanent disconnection, we still know
        // not to connect to it.
        let time = LocalTime::now();
        let mut addrmgr = AddressManager::new(
            Config::default(),
            fastrand::Rng::new(),
            HashMap::new(),
            (),
            time,
        );
        let source = Source::Dns;
        let services = ServiceFlags::NETWORK;
        let addr: &net::SocketAddr = &([33, 33, 33, 33], 8333).into();

        addrmgr.initialize();
        addrmgr.insert([(time.block_time(), Address::new(addr, services))], source);

        let (sampled, _) = addrmgr.sample(services).unwrap();
        assert_eq!(sampled.socket_addr().ok(), Some(*addr));
        assert!(addrmgr.sample(services).is_none());

        addrmgr.peer_attempted(addr);
        addrmgr.peer_connected(addr);
        addrmgr.peer_negotiated(addr, services, Link::Outbound);
        addrmgr.peer_disconnected(
            addr,
            fsm::DisconnectReason::PeerMisbehaving("misbehaving").into(),
        );

        // Peer is now disconnected for non-transient reasons.
        // Receive from a new peer the same address we just disconnected from.
        addrmgr.received_addr(
            ([99, 99, 99, 99], 8333).into(),
            vec![(time.block_time(), Address::new(addr, services))],
        );
        // It should not be returned from `sample`.
        assert!(addrmgr.sample(services).is_none());
    }

    #[quickcheck]
    fn prop_sample_no_duplicates(size: usize, seed: u64) -> TestResult {
        let clock = LocalTime::now();

        if size > 24 {
            return TestResult::discard();
        }

        let mut addrmgr = {
            let upstream = crate::fsm::output::Outbox::new(Network::Mainnet, 0);

            AddressManager::new(
                Config::default(),
                fastrand::Rng::with_seed(seed),
                HashMap::new(),
                upstream,
                clock,
            )
        };
        let time = LocalTime::now();
        let services = ServiceFlags::NETWORK;
        let mut addrs = vec![];

        for i in 0..size {
            addrs.push([96 + i as u8, 96 + i as u8, 96, 96]);
        }

        addrmgr.initialize();
        addrmgr.insert(
            addrs.iter().map(|a| {
                (
                    time.block_time(),
                    Address::new(&net::SocketAddr::from((*a, 8333)), services),
                )
            }),
            Source::Dns,
        );

        let mut sampled = HashSet::with_hasher(fastrand::Rng::with_seed(seed).into());
        for _ in 0..addrs.len() {
            let (addr, _) = addrmgr
                .sample(services)
                .expect("an address should be returned");
            sampled.insert(addr.socket_addr().unwrap().ip());
        }

        assert_eq!(
            sampled,
            addrs.iter().map(|a| (*a).into()).collect::<HashSet<_>>()
        );
        TestResult::passed()
    }

    #[test]
    fn test_max_range_size() {
        let services = ServiceFlags::NONE;
        let time = LocalTime::now();

        let mut addrmgr = AddressManager::new(
            Config::default(),
            fastrand::Rng::new(),
            HashMap::new(),
            (),
            time,
        );
        addrmgr.initialize();

        for i in 0..MAX_RANGE_SIZE + 1 {
            addrmgr.insert(
                iter::once((
                    time.block_time(),
                    Address::new(
                        &([111, 111, (i / u8::MAX as usize) as u8, i as u8], 8333).into(),
                        services,
                    ),
                )),
                Source::Dns,
            );
        }
        assert_eq!(
            addrmgr.len(),
            MAX_RANGE_SIZE,
            "we can't insert more than a certain amount of addresses in the same range"
        );

        addrmgr.insert(
            iter::once((
                time.block_time(),
                Address::new(&([129, 44, 12, 2], 8333).into(), services),
            )),
            Source::Dns,
        );
        assert_eq!(
            addrmgr.len(),
            MAX_RANGE_SIZE + 1,
            "inserting in another range is perfectly fine"
        );
    }

    #[test]
    fn test_addr_key() {
        assert_eq!(
            addr_key(&net::IpAddr::V4(net::Ipv4Addr::new(255, 0, 3, 4))),
            0
        );
        assert_eq!(
            addr_key(&net::IpAddr::V4(net::Ipv4Addr::new(255, 1, 3, 4))),
            1
        );
        assert_eq!(
            addr_key(&net::IpAddr::V4(net::Ipv4Addr::new(1, 255, 3, 4))),
            1
        );
    }

    #[test]
    fn test_insert() {
        use std::collections::HashMap;

        use nakamoto_common::bitcoin::network::address::Address;
        use nakamoto_common::bitcoin::network::constants::ServiceFlags;
        use nakamoto_common::block::time::LocalTime;
        use nakamoto_common::p2p::peer::Source;

        let cfg = Config::default();
        let time = LocalTime::now();
        let mut addrmgr = AddressManager::new(cfg, fastrand::Rng::new(), HashMap::new(), (), time);

        addrmgr.initialize();
        addrmgr.insert(
            vec![
                Address::new(&([183, 8, 55, 2], 8333).into(), ServiceFlags::NONE),
                Address::new(&([211, 48, 99, 4], 8333).into(), ServiceFlags::NONE),
                Address::new(&([241, 44, 12, 5], 8333).into(), ServiceFlags::NONE),
            ]
            .into_iter()
            .map(|a| (time.block_time(), a)),
            Source::Dns,
        );

        assert_eq!(addrmgr.len(), 3);

        addrmgr.insert(
            std::iter::once((
                time.block_time(),
                Address::new(&([183, 8, 55, 2], 8333).into(), ServiceFlags::NONE),
            )),
            Source::Dns,
        );

        assert_eq!(addrmgr.len(), 3, "already known addresses are ignored");

        addrmgr.clear();
        addrmgr.insert(
            vec![Address::new(
                &([255, 255, 255, 255], 8333).into(),
                ServiceFlags::NONE,
            )]
            .into_iter()
            .map(|a| (time.block_time(), a)),
            Source::Dns,
        );

        assert!(
            addrmgr.is_empty(),
            "non-routable/non-local addresses are ignored"
        );
    }

    #[test]
    fn test_sample() {
        use std::collections::HashMap;

        use nakamoto_common::bitcoin::network::address::Address;
        use nakamoto_common::bitcoin::network::constants::ServiceFlags;
        use nakamoto_common::block::time::{LocalDuration, LocalTime};
        use nakamoto_common::p2p::peer::Source;

        let cfg = Config::default();
        let clock = RefClock::from(LocalTime::now());
        let mut addrmgr =
            AddressManager::new(cfg, fastrand::Rng::new(), HashMap::new(), (), clock.clone());

        addrmgr.initialize();

        // Addresses controlled by an adversary.
        let adversary_addrs = vec![
            Address::new(&([111, 8, 55, 2], 8333).into(), ServiceFlags::NONE),
            Address::new(&([111, 8, 43, 11], 8333).into(), ServiceFlags::NONE),
            Address::new(&([111, 8, 89, 6], 8333).into(), ServiceFlags::NONE),
            Address::new(&([111, 8, 124, 41], 8333).into(), ServiceFlags::NONE),
            Address::new(&([111, 8, 65, 4], 8333).into(), ServiceFlags::NONE),
            Address::new(&([111, 8, 161, 73], 8333).into(), ServiceFlags::NONE),
        ];
        addrmgr.insert(
            adversary_addrs
                .iter()
                .cloned()
                .map(|a| (clock.block_time(), a)),
            Source::Dns,
        );

        // Safe addresses, controlled by non-adversarial peers.
        let safe_addrs = vec![
            Address::new(&([183, 8, 55, 2], 8333).into(), ServiceFlags::NONE),
            Address::new(&([211, 48, 99, 4], 8333).into(), ServiceFlags::NONE),
            Address::new(&([241, 44, 12, 5], 8333).into(), ServiceFlags::NONE),
            Address::new(&([99, 129, 2, 15], 8333).into(), ServiceFlags::NONE),
        ];
        addrmgr.insert(
            safe_addrs.iter().cloned().map(|a| (clock.block_time(), a)),
            Source::Dns,
        );

        // Keep track of how many times we pick a safe vs. an adversary-controlled address.
        let mut adversary = 0;
        let mut safe = 0;

        for _ in 0..99 {
            let (addr, _) = addrmgr.sample(ServiceFlags::NONE).unwrap();

            // Make sure we can re-sample the same addresses for the purpose of this example.
            clock.elapse(LocalDuration::from_mins(60));
            addrmgr.received_wake();

            if adversary_addrs.contains(&addr) {
                adversary += 1;
            } else if safe_addrs.contains(&addr) {
                safe += 1;
            }
        }

        // Despite there being more adversary-controlled addresses, our safe addresses
        // are picked much more often.
        assert!(
            safe > adversary * 2,
            "safe addresses are picked twice more often"
        );
    }
}