zebra_network/protocol/external/
types.rs1use std::{cmp::max, fmt};
2
3use zebra_chain::{
4 block,
5 parameters::{
6 Network::{self, *},
7 NetworkUpgrade::{self, *},
8 },
9};
10
11use crate::constants::{self, CURRENT_NETWORK_PROTOCOL_VERSION};
12
13#[cfg(any(test, feature = "proptest-impl"))]
14use proptest_derive::Arbitrary;
15
16#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
18pub struct Version(pub u32);
19
20impl fmt::Display for Version {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 f.write_str(&self.0.to_string())
23 }
24}
25
26impl Version {
27 pub(crate) fn metric_label(&self) -> &'static str {
29 match self.0 {
31 170_002 => "170002",
32 170_003 => "170003",
33 170_005 => "170005",
34 170_006 => "170006",
35 170_007 => "170007",
36 170_008 => "170008",
37 170_009 => "170009",
38 170_010 => "170010",
39 170_011 => "170011",
40 170_012 => "170012",
41 170_013 => "170013",
42 170_050 => "170050",
43 170_100 => "170100",
44 170_110 => "170110",
45 170_120 => "170120",
46 170_130 => "170130",
47 170_140 => "170140",
48 170_150 => "170150",
49 170_160 => "170160",
50 170_180 => "170180",
51 170_190 => "170190",
52 _ => "other",
53 }
54 }
55
56 pub fn min_remote_for_height(
63 network: &Network,
64 height: impl Into<Option<block::Height>>,
65 ) -> Version {
66 let height = height.into().unwrap_or(block::Height(0));
67 let min_spec = Version::min_specified_for_height(network, height);
68
69 assert!(
71 constants::CURRENT_NETWORK_PROTOCOL_VERSION >= min_spec,
72 "Zebra does not implement the minimum specified {:?} protocol version for {:?} at {:?}",
73 NetworkUpgrade::current(network, height),
74 network,
75 height,
76 );
77
78 max(min_spec, Version::initial_min_for_network(network))
79 }
80
81 fn initial_min_for_network(network: &Network) -> Version {
88 *constants::INITIAL_MIN_NETWORK_PROTOCOL_VERSION
89 .get(&network.kind())
90 .expect("We always have a value for testnet or mainnet")
91 }
92
93 fn min_specified_for_height(network: &Network, height: block::Height) -> Version {
98 let network_upgrade = NetworkUpgrade::current(network, height);
99 Version::min_specified_for_upgrade(network, network_upgrade)
100 }
101
102 pub(crate) fn min_specified_for_upgrade(
118 network: &Network,
119 network_upgrade: NetworkUpgrade,
120 ) -> Version {
121 Version(match (network, network_upgrade) {
122 (_, Genesis) | (_, BeforeOverwinter) => 170_002,
123 (Testnet(params), Overwinter) if params.is_default_testnet() => 170_003,
124 (Mainnet, Overwinter) => 170_005,
125 (Testnet(params), Sapling) if params.is_default_testnet() => 170_007,
126 (Testnet(params), Sapling) if params.is_regtest() => 170_006,
127 (Mainnet, Sapling) => 170_007,
128 (Testnet(params), Blossom) if params.is_default_testnet() || params.is_regtest() => {
129 170_008
130 }
131 (Mainnet, Blossom) => 170_009,
132 (Testnet(params), Heartwood) if params.is_default_testnet() || params.is_regtest() => {
133 170_010
134 }
135 (Mainnet, Heartwood) => 170_011,
136 (Testnet(params), Canopy) if params.is_default_testnet() || params.is_regtest() => {
137 170_012
138 }
139 (Mainnet, Canopy) => 170_013,
140 (Testnet(params), Nu5) if params.is_default_testnet() || params.is_regtest() => 170_050,
141 (Mainnet, Nu5) => 170_100,
142 (Testnet(params), Nu6) if params.is_default_testnet() || params.is_regtest() => 170_110,
143 (Mainnet, Nu6) => 170_120,
144 (Testnet(params), Nu6_1) if params.is_default_testnet() || params.is_regtest() => {
145 170_130
146 }
147 (Mainnet, Nu6_1) => 170_140,
148 (Testnet(params), Nu6_2) if params.is_default_testnet() || params.is_regtest() => {
149 170_150
150 }
151 (Mainnet, Nu6_2) => 170_150,
152 (Testnet(params), Nu6_3) if params.is_default_testnet() || params.is_regtest() => {
153 170_160
154 }
155 (Mainnet, Nu6_3) => 170_160,
156 (Testnet(params), Nu7) if params.is_default_testnet() || params.is_regtest() => 170_180,
158 (Mainnet, Nu7) => 170_190,
159
160 (Testnet(_), _) => CURRENT_NETWORK_PROTOCOL_VERSION.0,
162
163 #[cfg(zcash_unstable = "zfuture")]
164 (Mainnet, ZFuture) => {
165 panic!("ZFuture network upgrade should not be active on Mainnet")
166 }
167 })
168 }
169}
170
171bitflags! {
172 #[derive(Copy, Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
177 pub struct PeerServices: u64 {
178 const NODE_NETWORK = 1;
182
183 const NODE_PRUNED = 1 << 25;
188
189 const NODE_COMPACT_BLOCKS = 1 << 16;
195 }
196}
197
198impl PeerServices {
199 pub fn serves_full_history(&self) -> bool {
201 self.contains(PeerServices::NODE_NETWORK) && !self.contains(PeerServices::NODE_PRUNED)
202 }
203}
204
205#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
207#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
208pub struct Nonce(pub u64);
209
210impl Default for Nonce {
211 fn default() -> Self {
212 Self(rand::random())
213 }
214}
215
216#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
218#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
219pub struct Tweak(pub u32);
220
221impl Default for Tweak {
222 fn default() -> Self {
223 Self(rand::random())
224 }
225}
226
227#[derive(Clone, Debug, Eq, PartialEq)]
230#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
231pub struct Filter(pub Vec<u8>);
232
233#[cfg(test)]
234mod test;