Skip to main content

zebra_network/meta_addr/
arbitrary.rs

1//! Randomised test data generation for MetaAddr.
2
3use std::net::IpAddr;
4
5use proptest::{collection::vec, prelude::*};
6
7use zebra_chain::{parameters::Network::*, serialization::DateTime32};
8
9use crate::protocol::external::arbitrary::canonical_peer_addr_strategy;
10
11use super::{MetaAddr, MetaAddrChange, PeerServices, PeerSocketAddr};
12
13/// The largest number of random changes we want to apply to a [`MetaAddr`].
14///
15/// This should be at least twice the number of [`PeerAddrState`][1]s, so the
16/// tests can cover multiple transitions through every state.
17///
18/// [1]: super::PeerAddrState
19#[allow(dead_code)]
20pub const MAX_ADDR_CHANGE: usize = 15;
21
22/// The largest number of random addresses we want to add to an [`AddressBook`][2].
23///
24/// This should be at least the number of [`PeerAddrState`][1]s, so the tests
25/// can cover interactions between addresses in different states.
26///
27/// [1]: super::PeerAddrState
28/// [2]: crate::AddressBook
29#[allow(dead_code)]
30pub const MAX_META_ADDR: usize = 8;
31
32impl MetaAddr {
33    /// Create a strategy that generates [`MetaAddr`]s in the
34    /// [`NeverAttemptedGossiped`][1] state.
35    ///
36    /// [1]: super::PeerAddrState::NeverAttemptedGossiped
37    pub fn gossiped_strategy() -> BoxedStrategy<Self> {
38        (
39            canonical_peer_addr_strategy(),
40            any::<PeerServices>(),
41            any::<DateTime32>(),
42        )
43            .prop_map(|(addr, untrusted_services, untrusted_last_seen)| {
44                MetaAddr::new_gossiped_meta_addr(addr, untrusted_services, untrusted_last_seen)
45            })
46            .boxed()
47    }
48
49    /// Create a strategy that generates arbitrary [`MetaAddr`]s with known services.
50    ///
51    /// Addresses with unknown services are never sanitized for gossip, so this fills them in with
52    /// arbitrary services that include `NODE_NETWORK`, which gossip requires.
53    pub fn known_services_strategy() -> BoxedStrategy<Self> {
54        (any::<MetaAddr>(), any::<PeerServices>())
55            .prop_map(|(mut addr, services)| {
56                addr.services
57                    .get_or_insert(services | PeerServices::NODE_NETWORK);
58                addr
59            })
60            .boxed()
61    }
62}
63
64impl MetaAddrChange {
65    /// Returns a strategy which generates changes for `addr`.
66    ///
67    /// `addr` is typically generated by the `canonical_peer_addr` strategy.
68    pub fn addr_strategy(addr: PeerSocketAddr) -> BoxedStrategy<Self> {
69        any::<MetaAddrChange>()
70            .prop_map(move |mut change| {
71                change.set_addr(addr);
72                change
73            })
74            .boxed()
75    }
76
77    /// Returns a strategy which generates a `MetaAddr`, and a vector of up to
78    /// `max_addr_change` changes.
79    ///
80    /// The address and the changes all have matching `PeerSocketAddr`s.
81    pub fn addr_changes_strategy(
82        max_addr_change: usize,
83    ) -> BoxedStrategy<(MetaAddr, Vec<MetaAddrChange>)> {
84        any::<MetaAddr>()
85            .prop_flat_map(move |addr| {
86                let peer_addr = addr.addr;
87                (
88                    Just(addr),
89                    vec(MetaAddrChange::addr_strategy(peer_addr), 1..max_addr_change),
90                )
91            })
92            .boxed()
93    }
94
95    /// Create a strategy that generates [`IpAddr`]s for [`MetaAddrChange`]s which are ready for
96    /// outbound connections.
97    pub fn ready_outbound_strategy_ip() -> BoxedStrategy<IpAddr> {
98        any::<IpAddr>()
99            .prop_filter("failed MetaAddr::is_valid_for_outbound", |ip| {
100                !ip.is_unspecified()
101            })
102            .boxed()
103    }
104
105    /// Create a strategy that generates port numbers for [`MetaAddr`]s which are ready for
106    /// outbound connections.
107    ///
108    /// Currently, all generated [`MetaAddr`]s are the [`NeverAttemptedGossiped`][1] variant.
109    ///
110    /// TODO: Generate all [`MetaAddr`] variants, and give them ready fields.
111    ///
112    /// [1]: super::NeverAttemptedGossiped
113    pub fn ready_outbound_strategy_port() -> BoxedStrategy<u16> {
114        (
115            canonical_peer_addr_strategy(),
116            any::<PeerServices>(),
117            any::<DateTime32>(),
118        )
119            .prop_filter_map(
120                "failed MetaAddr::is_valid_for_outbound",
121                |(addr, services, local_now)| {
122                    let addr = MetaAddr::new_gossiped_meta_addr(addr, services, local_now);
123
124                    if addr.last_known_info_is_valid_for_outbound(&Mainnet) {
125                        Some(addr.addr.port())
126                    } else {
127                        None
128                    }
129                },
130            )
131            .boxed()
132    }
133}