use std::net::IpAddr;
use proptest::{collection::vec, prelude::*};
use zebra_chain::{parameters::Network::*, serialization::DateTime32};
use crate::protocol::external::arbitrary::canonical_peer_addr_strategy;
use super::{MetaAddr, MetaAddrChange, PeerServices, PeerSocketAddr};
#[allow(dead_code)]
pub const MAX_ADDR_CHANGE: usize = 15;
#[allow(dead_code)]
pub const MAX_META_ADDR: usize = 8;
impl MetaAddr {
pub fn gossiped_strategy() -> BoxedStrategy<Self> {
(
canonical_peer_addr_strategy(),
any::<PeerServices>(),
any::<DateTime32>(),
)
.prop_map(|(addr, untrusted_services, untrusted_last_seen)| {
MetaAddr::new_gossiped_meta_addr(addr, untrusted_services, untrusted_last_seen)
})
.boxed()
}
}
impl MetaAddrChange {
pub fn addr_strategy(addr: PeerSocketAddr) -> BoxedStrategy<Self> {
any::<MetaAddrChange>()
.prop_map(move |mut change| {
change.set_addr(addr);
change
})
.boxed()
}
pub fn addr_changes_strategy(
max_addr_change: usize,
) -> BoxedStrategy<(MetaAddr, Vec<MetaAddrChange>)> {
any::<MetaAddr>()
.prop_flat_map(move |addr| {
(
Just(addr),
vec(MetaAddrChange::addr_strategy(addr.addr), 1..max_addr_change),
)
})
.boxed()
}
pub fn ready_outbound_strategy_ip() -> BoxedStrategy<IpAddr> {
any::<IpAddr>()
.prop_filter("failed MetaAddr::is_valid_for_outbound", |ip| {
!ip.is_unspecified()
})
.boxed()
}
pub fn ready_outbound_strategy_port() -> BoxedStrategy<u16> {
(
canonical_peer_addr_strategy(),
any::<PeerServices>(),
any::<DateTime32>(),
)
.prop_filter_map(
"failed MetaAddr::is_valid_for_outbound",
|(addr, services, local_now)| {
let addr = MetaAddr::new_gossiped_meta_addr(addr, services, local_now);
if addr.last_known_info_is_valid_for_outbound(&Mainnet) {
Some(addr.addr.port())
} else {
None
}
},
)
.boxed()
}
}