Skip to main content

zebra_chain/parameters/
network_upgrade.rs

1//! Network upgrade consensus parameters for Zcash.
2
3use NetworkUpgrade::*;
4
5use crate::block;
6use crate::parameters::{Network, Network::*};
7use crate::serialization::BytesInDisplayOrder;
8
9use std::collections::{BTreeMap, HashMap};
10use std::fmt;
11
12use chrono::{DateTime, Duration, Utc};
13use hex::{FromHex, ToHex};
14
15use strum::{EnumIter, IntoEnumIterator};
16
17#[cfg(any(test, feature = "proptest-impl"))]
18use proptest_derive::Arbitrary;
19
20/// A Zcash network upgrade.
21///
22/// Network upgrades change the Zcash network protocol or consensus rules. Note that they have no
23/// designated codenames from NU5 onwards.
24///
25/// Enum variants must be ordered by activation height.
26#[derive(
27    Copy, Clone, EnumIter, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Ord, PartialOrd,
28)]
29#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
30pub enum NetworkUpgrade {
31    /// The Zcash protocol for a Genesis block.
32    ///
33    /// Zcash genesis blocks use a different set of consensus rules from
34    /// other BeforeOverwinter blocks, so we treat them like a separate network
35    /// upgrade.
36    Genesis,
37    /// The Zcash protocol before the Overwinter upgrade.
38    ///
39    /// We avoid using `Sprout`, because the specification says that Sprout
40    /// is the name of the pre-Sapling protocol, before and after Overwinter.
41    BeforeOverwinter,
42    /// The Zcash protocol after the Overwinter upgrade.
43    Overwinter,
44    /// The Zcash protocol after the Sapling upgrade.
45    Sapling,
46    /// The Zcash protocol after the Blossom upgrade.
47    Blossom,
48    /// The Zcash protocol after the Heartwood upgrade.
49    Heartwood,
50    /// The Zcash protocol after the Canopy upgrade.
51    Canopy,
52    /// The Zcash protocol after the NU5 upgrade.
53    #[serde(rename = "NU5")]
54    Nu5,
55    /// The Zcash protocol after the NU6 upgrade.
56    #[serde(rename = "NU6")]
57    Nu6,
58    /// The Zcash protocol after the NU6.1 upgrade.
59    #[serde(rename = "NU6.1")]
60    Nu6_1,
61    /// The Zcash protocol after the NU6.2 upgrade.
62    #[serde(rename = "NU6.2")]
63    Nu6_2,
64    /// The Zcash protocol after the NU7 upgrade.
65    #[serde(rename = "NU7")]
66    Nu7,
67
68    #[cfg(zcash_unstable = "zfuture")]
69    ZFuture,
70}
71
72impl TryFrom<u32> for NetworkUpgrade {
73    type Error = crate::Error;
74
75    fn try_from(branch_id: u32) -> Result<Self, Self::Error> {
76        CONSENSUS_BRANCH_IDS
77            .iter()
78            .find(|id| id.1 == ConsensusBranchId(branch_id))
79            .map(|nu| nu.0)
80            .ok_or(Self::Error::InvalidConsensusBranchId)
81    }
82}
83
84impl fmt::Display for NetworkUpgrade {
85    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86        // Same as the debug representation for now
87        fmt::Debug::fmt(self, f)
88    }
89}
90
91/// Mainnet network upgrade activation heights.
92///
93/// This is actually a bijective map, but it is const, so we use a vector, and
94/// do the uniqueness check in the unit tests.
95///
96/// # Correctness
97///
98/// Don't use this directly; use NetworkUpgrade::activation_list() so that
99/// we can switch to fake activation heights for some tests.
100#[allow(unused)]
101pub(super) const MAINNET_ACTIVATION_HEIGHTS: &[(block::Height, NetworkUpgrade)] = {
102    use super::constants::activation_heights::mainnet::*;
103    &[
104        (block::Height(0), Genesis),
105        (BEFORE_OVERWINTER, BeforeOverwinter),
106        (OVERWINTER, Overwinter),
107        (SAPLING, Sapling),
108        (BLOSSOM, Blossom),
109        (HEARTWOOD, Heartwood),
110        (CANOPY, Canopy),
111        (NU5, Nu5),
112        (NU6, Nu6),
113        (NU6_1, Nu6_1),
114        (NU6_2, Nu6_2),
115    ]
116};
117/// Testnet network upgrade activation heights.
118///
119/// This is actually a bijective map, but it is const, so we use a vector, and
120/// do the uniqueness check in the unit tests.
121///
122/// # Correctness
123///
124/// Don't use this directly; use NetworkUpgrade::activation_list() so that
125/// we can switch to fake activation heights for some tests.
126#[allow(unused)]
127pub(super) const TESTNET_ACTIVATION_HEIGHTS: &[(block::Height, NetworkUpgrade)] = {
128    use super::constants::activation_heights::testnet::*;
129    &[
130        (block::Height(0), Genesis),
131        (BEFORE_OVERWINTER, BeforeOverwinter),
132        (OVERWINTER, Overwinter),
133        (SAPLING, Sapling),
134        (BLOSSOM, Blossom),
135        (HEARTWOOD, Heartwood),
136        (CANOPY, Canopy),
137        (NU5, Nu5),
138        (NU6, Nu6),
139        (NU6_1, Nu6_1),
140        (NU6_2, Nu6_2),
141    ]
142};
143
144/// The Consensus Branch Id, used to bind transactions and blocks to a
145/// particular network upgrade.
146#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
147pub struct ConsensusBranchId(pub(crate) u32);
148
149impl BytesInDisplayOrder<false, 4> for ConsensusBranchId {
150    fn bytes_in_serialized_order(&self) -> [u8; 4] {
151        self.0.to_be_bytes()
152    }
153
154    fn from_bytes_in_serialized_order(bytes: [u8; 4]) -> Self {
155        ConsensusBranchId(u32::from_be_bytes(bytes))
156    }
157}
158
159impl From<ConsensusBranchId> for u32 {
160    fn from(branch: ConsensusBranchId) -> u32 {
161        branch.0
162    }
163}
164
165impl From<u32> for ConsensusBranchId {
166    fn from(branch: u32) -> Self {
167        ConsensusBranchId(branch)
168    }
169}
170
171impl ToHex for &ConsensusBranchId {
172    fn encode_hex<T: FromIterator<char>>(&self) -> T {
173        self.bytes_in_display_order().encode_hex()
174    }
175
176    fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
177        self.bytes_in_display_order().encode_hex_upper()
178    }
179}
180
181impl ToHex for ConsensusBranchId {
182    fn encode_hex<T: FromIterator<char>>(&self) -> T {
183        self.bytes_in_display_order().encode_hex()
184    }
185
186    fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
187        self.bytes_in_display_order().encode_hex_upper()
188    }
189}
190
191impl FromHex for ConsensusBranchId {
192    type Error = <[u8; 4] as FromHex>::Error;
193
194    fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
195        let branch = <[u8; 4]>::from_hex(hex)?;
196        Ok(ConsensusBranchId(u32::from_be_bytes(branch)))
197    }
198}
199
200impl fmt::Display for ConsensusBranchId {
201    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
202        f.write_str(&self.encode_hex::<String>())
203    }
204}
205
206impl TryFrom<ConsensusBranchId> for zcash_protocol::consensus::BranchId {
207    type Error = crate::Error;
208
209    fn try_from(id: ConsensusBranchId) -> Result<Self, Self::Error> {
210        zcash_protocol::consensus::BranchId::try_from(u32::from(id))
211            .map_err(|_| Self::Error::InvalidConsensusBranchId)
212    }
213}
214
215/// Network Upgrade Consensus Branch Ids.
216///
217/// Branch ids are the same for mainnet and testnet. If there is a testnet
218/// rollback after a bug, the branch id changes.
219///
220/// Branch ids were introduced in the Overwinter upgrade, so there are no
221/// Genesis or BeforeOverwinter branch ids.
222///
223/// This is actually a bijective map, but it is const, so we use a vector, and
224/// do the uniqueness check in the unit tests.
225pub(crate) const CONSENSUS_BRANCH_IDS: &[(NetworkUpgrade, ConsensusBranchId)] = &[
226    (Overwinter, ConsensusBranchId(0x5ba81b19)),
227    (Sapling, ConsensusBranchId(0x76b809bb)),
228    (Blossom, ConsensusBranchId(0x2bb40e60)),
229    (Heartwood, ConsensusBranchId(0xf5b9230b)),
230    (Canopy, ConsensusBranchId(0xe9ff75a6)),
231    (Nu5, ConsensusBranchId(0xc2d6d0b4)),
232    (Nu6, ConsensusBranchId(0xc8e71055)),
233    (Nu6_1, ConsensusBranchId(0x4dec4df0)),
234    (Nu6_2, ConsensusBranchId(0x5437f330)),
235    // TODO: set below to (Nu7, ConsensusBranchId(0x77190ad8)), once the same value is set in librustzcash
236    #[cfg(any(test, feature = "zebra-test"))]
237    (Nu7, ConsensusBranchId(0xffffffff)),
238    #[cfg(zcash_unstable = "zfuture")]
239    (ZFuture, ConsensusBranchId(0xffffffff)),
240];
241
242/// The target block spacing before Blossom.
243const PRE_BLOSSOM_POW_TARGET_SPACING: i64 = 150;
244
245/// The target block spacing after Blossom activation.
246pub const POST_BLOSSOM_POW_TARGET_SPACING: u32 = 75;
247
248/// The averaging window for difficulty threshold arithmetic mean calculations.
249///
250/// `PoWAveragingWindow` in the Zcash specification.
251pub const POW_AVERAGING_WINDOW: usize = 17;
252
253/// The multiplier used to derive the testnet minimum difficulty block time gap
254/// threshold.
255///
256/// Based on <https://zips.z.cash/zip-0208#minimum-difficulty-blocks-on-the-test-network>
257const TESTNET_MINIMUM_DIFFICULTY_GAP_MULTIPLIER: i32 = 6;
258
259/// The start height for the testnet minimum difficulty consensus rule.
260///
261/// Based on <https://zips.z.cash/zip-0208#minimum-difficulty-blocks-on-the-test-network>
262const TESTNET_MINIMUM_DIFFICULTY_START_HEIGHT: block::Height = block::Height(299_188);
263
264/// The activation height for the block maximum time rule on Testnet.
265///
266/// Part of the block header consensus rules in the Zcash specification at
267/// <https://zips.z.cash/protocol/protocol.pdf#blockheader>
268pub const TESTNET_MAX_TIME_START_HEIGHT: block::Height = block::Height(653_606);
269
270impl Network {
271    /// Returns a map between activation heights and network upgrades for `network`,
272    /// in ascending height order.
273    ///
274    /// If the activation height of a future upgrade is not known, that
275    /// network upgrade does not appear in the list.
276    ///
277    /// This is actually a bijective map.
278    ///
279    /// Note: This skips implicit network upgrade activations, use [`Network::full_activation_list`]
280    ///       to get an explicit list of all network upgrade activations.
281    pub fn activation_list(&self) -> BTreeMap<block::Height, NetworkUpgrade> {
282        match self {
283            Mainnet => MAINNET_ACTIVATION_HEIGHTS.iter().cloned().collect(),
284            Testnet(params) => params.activation_heights().clone(),
285        }
286    }
287
288    /// Returns a vector of all implicit and explicit network upgrades for `network`,
289    /// in ascending height order.
290    pub fn full_activation_list(&self) -> Vec<(block::Height, NetworkUpgrade)> {
291        NetworkUpgrade::iter()
292            .filter_map(|nu| Some((NetworkUpgrade::activation_height(&nu, self)?, nu)))
293            .collect()
294    }
295}
296
297impl NetworkUpgrade {
298    /// Returns the current network upgrade and its activation height for `network` and `height`.
299    pub fn current_with_activation_height(
300        network: &Network,
301        height: block::Height,
302    ) -> (NetworkUpgrade, block::Height) {
303        network
304            .activation_list()
305            .range(..=height)
306            .map(|(&h, &nu)| (nu, h))
307            .next_back()
308            .expect("every height has a current network upgrade")
309    }
310
311    /// Returns the current network upgrade for `network` and `height`.
312    pub fn current(network: &Network, height: block::Height) -> NetworkUpgrade {
313        network
314            .activation_list()
315            .range(..=height)
316            .map(|(_, nu)| *nu)
317            .next_back()
318            .expect("every height has a current network upgrade")
319    }
320
321    /// Returns the next expected network upgrade after this network upgrade.
322    pub fn next_upgrade(self) -> Option<Self> {
323        Self::iter().skip_while(|&nu| self != nu).nth(1)
324    }
325
326    /// Returns the previous network upgrade before this network upgrade.
327    pub fn previous_upgrade(self) -> Option<Self> {
328        Self::iter().rev().skip_while(|&nu| self != nu).nth(1)
329    }
330
331    /// Returns the next network upgrade for `network` and `height`.
332    ///
333    /// Returns None if the next upgrade has not been implemented in Zebra
334    /// yet.
335    #[cfg(test)]
336    pub fn next(network: &Network, height: block::Height) -> Option<NetworkUpgrade> {
337        use std::ops::Bound::*;
338
339        network
340            .activation_list()
341            .range((Excluded(height), Unbounded))
342            .map(|(_, nu)| *nu)
343            .next()
344    }
345
346    /// Returns the activation height for this network upgrade on `network`, or
347    ///
348    /// Returns the activation height of the first network upgrade that follows
349    /// this network upgrade if there is no activation height for this network upgrade
350    /// such as on Regtest or a configured Testnet where multiple network upgrades have the
351    /// same activation height, or if one is omitted when others that follow it are included.
352    ///
353    /// Returns None if this network upgrade is a future upgrade, and its
354    /// activation height has not been set yet.
355    ///
356    /// Returns None if this network upgrade has not been configured on a Testnet or Regtest.
357    pub fn activation_height(&self, network: &Network) -> Option<block::Height> {
358        network
359            .activation_list()
360            .iter()
361            .find(|(_, nu)| nu == &self)
362            .map(|(height, _)| *height)
363            .or_else(|| {
364                self.next_upgrade()
365                    .and_then(|next_nu| next_nu.activation_height(network))
366            })
367    }
368
369    /// Returns `true` if `height` is the activation height of any network upgrade
370    /// on `network`.
371    ///
372    /// Use [`NetworkUpgrade::activation_height`] to get the specific network
373    /// upgrade.
374    pub fn is_activation_height(network: &Network, height: block::Height) -> bool {
375        network.activation_list().contains_key(&height)
376    }
377
378    /// Returns an unordered mapping between NetworkUpgrades and their ConsensusBranchIds.
379    ///
380    /// Branch ids are the same for mainnet and testnet.
381    ///
382    /// If network upgrade does not have a branch id, that network upgrade does
383    /// not appear in the list.
384    ///
385    /// This is actually a bijective map.
386    pub(crate) fn branch_id_list() -> HashMap<NetworkUpgrade, ConsensusBranchId> {
387        CONSENSUS_BRANCH_IDS.iter().cloned().collect()
388    }
389
390    /// Returns the consensus branch id for this network upgrade.
391    ///
392    /// Returns None if this network upgrade has no consensus branch id.
393    pub fn branch_id(&self) -> Option<ConsensusBranchId> {
394        NetworkUpgrade::branch_id_list().get(self).cloned()
395    }
396
397    /// Returns the target block spacing for the network upgrade.
398    ///
399    /// Based on [`PRE_BLOSSOM_POW_TARGET_SPACING`] and
400    /// [`POST_BLOSSOM_POW_TARGET_SPACING`] from the Zcash specification.
401    pub fn target_spacing(&self) -> Duration {
402        let spacing_seconds = match self {
403            Genesis | BeforeOverwinter | Overwinter | Sapling => PRE_BLOSSOM_POW_TARGET_SPACING,
404            Blossom | Heartwood | Canopy | Nu5 | Nu6 | Nu6_1 | Nu6_2 | Nu7 => {
405                POST_BLOSSOM_POW_TARGET_SPACING.into()
406            }
407
408            #[cfg(zcash_unstable = "zfuture")]
409            ZFuture => POST_BLOSSOM_POW_TARGET_SPACING.into(),
410        };
411
412        Duration::seconds(spacing_seconds)
413    }
414
415    /// Returns the target block spacing for `network` and `height`.
416    ///
417    /// See [`NetworkUpgrade::target_spacing`] for details.
418    pub fn target_spacing_for_height(network: &Network, height: block::Height) -> Duration {
419        NetworkUpgrade::current(network, height).target_spacing()
420    }
421
422    /// Returns all the target block spacings for `network` and the heights where they start.
423    pub fn target_spacings(
424        network: &Network,
425    ) -> impl Iterator<Item = (block::Height, Duration)> + '_ {
426        [
427            (NetworkUpgrade::Genesis, PRE_BLOSSOM_POW_TARGET_SPACING),
428            (
429                NetworkUpgrade::Blossom,
430                POST_BLOSSOM_POW_TARGET_SPACING.into(),
431            ),
432        ]
433        .into_iter()
434        .filter_map(move |(upgrade, spacing_seconds)| {
435            let activation_height = upgrade.activation_height(network)?;
436            let target_spacing = Duration::seconds(spacing_seconds);
437            Some((activation_height, target_spacing))
438        })
439    }
440
441    /// Returns the minimum difficulty block spacing for `network` and `height`.
442    /// Returns `None` if the testnet minimum difficulty consensus rule is not active.
443    ///
444    /// Based on <https://zips.z.cash/zip-0208#minimum-difficulty-blocks-on-the-test-network>
445    pub fn minimum_difficulty_spacing_for_height(
446        network: &Network,
447        height: block::Height,
448    ) -> Option<Duration> {
449        match (network, height) {
450            // TODO: Move `TESTNET_MINIMUM_DIFFICULTY_START_HEIGHT` to a field on testnet::Parameters (#8364)
451            (Network::Testnet(_params), height)
452                if height < TESTNET_MINIMUM_DIFFICULTY_START_HEIGHT =>
453            {
454                None
455            }
456            (Network::Mainnet, _) => None,
457            (Network::Testnet(_params), _) => {
458                let network_upgrade = NetworkUpgrade::current(network, height);
459                Some(network_upgrade.target_spacing() * TESTNET_MINIMUM_DIFFICULTY_GAP_MULTIPLIER)
460            }
461        }
462    }
463
464    /// Returns true if the gap between `block_time` and `previous_block_time` is
465    /// greater than the Testnet minimum difficulty time gap. This time gap
466    /// depends on the `network` and `block_height`.
467    ///
468    /// Returns false on Mainnet, when `block_height` is less than the minimum
469    /// difficulty start height, and when the time gap is too small.
470    ///
471    /// `block_time` can be less than, equal to, or greater than
472    /// `previous_block_time`, because block times are provided by miners.
473    ///
474    /// Implements the Testnet minimum difficulty adjustment from ZIPs 205 and 208.
475    ///
476    /// Spec Note: Some parts of ZIPs 205 and 208 previously specified an incorrect
477    /// check for the time gap. This function implements the correct "greater than"
478    /// check.
479    pub fn is_testnet_min_difficulty_block(
480        network: &Network,
481        block_height: block::Height,
482        block_time: DateTime<Utc>,
483        previous_block_time: DateTime<Utc>,
484    ) -> bool {
485        let block_time_gap = block_time - previous_block_time;
486        if let Some(min_difficulty_gap) =
487            NetworkUpgrade::minimum_difficulty_spacing_for_height(network, block_height)
488        {
489            block_time_gap > min_difficulty_gap
490        } else {
491            false
492        }
493    }
494
495    /// Returns the averaging window timespan for the network upgrade.
496    ///
497    /// `AveragingWindowTimespan` from the Zcash specification.
498    pub fn averaging_window_timespan(&self) -> Duration {
499        self.target_spacing() * POW_AVERAGING_WINDOW.try_into().expect("fits in i32")
500    }
501
502    /// Returns the averaging window timespan for `network` and `height`.
503    ///
504    /// See [`NetworkUpgrade::averaging_window_timespan`] for details.
505    pub fn averaging_window_timespan_for_height(
506        network: &Network,
507        height: block::Height,
508    ) -> Duration {
509        NetworkUpgrade::current(network, height).averaging_window_timespan()
510    }
511
512    /// Returns an iterator over [`NetworkUpgrade`] variants.
513    pub fn iter() -> impl DoubleEndedIterator<Item = NetworkUpgrade> {
514        <Self as IntoEnumIterator>::iter()
515    }
516}
517
518impl From<zcash_protocol::consensus::NetworkUpgrade> for NetworkUpgrade {
519    fn from(nu: zcash_protocol::consensus::NetworkUpgrade) -> Self {
520        match nu {
521            zcash_protocol::consensus::NetworkUpgrade::Overwinter => Self::Overwinter,
522            zcash_protocol::consensus::NetworkUpgrade::Sapling => Self::Sapling,
523            zcash_protocol::consensus::NetworkUpgrade::Blossom => Self::Blossom,
524            zcash_protocol::consensus::NetworkUpgrade::Heartwood => Self::Heartwood,
525            zcash_protocol::consensus::NetworkUpgrade::Canopy => Self::Canopy,
526            zcash_protocol::consensus::NetworkUpgrade::Nu5 => Self::Nu5,
527            zcash_protocol::consensus::NetworkUpgrade::Nu6 => Self::Nu6,
528            zcash_protocol::consensus::NetworkUpgrade::Nu6_1 => Self::Nu6_1,
529            zcash_protocol::consensus::NetworkUpgrade::Nu6_2 => Self::Nu6_2,
530            #[cfg(zcash_unstable = "nu7")]
531            zcash_protocol::consensus::NetworkUpgrade::Nu7 => Self::Nu7,
532            #[cfg(zcash_unstable = "zfuture")]
533            zcash_protocol::consensus::NetworkUpgrade::ZFuture => Self::ZFuture,
534        }
535    }
536}
537
538impl ConsensusBranchId {
539    /// The value used by `zcashd` RPCs for missing consensus branch IDs.
540    ///
541    /// # Consensus
542    ///
543    /// This value must only be used in RPCs.
544    ///
545    /// The consensus rules handle missing branch IDs by rejecting blocks and transactions,
546    /// so this substitute value must not be used in consensus-critical code.
547    pub const RPC_MISSING_ID: ConsensusBranchId = ConsensusBranchId(0);
548
549    /// Returns the current consensus branch id for `network` and `height`.
550    ///
551    /// Returns None if the network has no branch id at this height.
552    pub fn current(network: &Network, height: block::Height) -> Option<ConsensusBranchId> {
553        NetworkUpgrade::current(network, height).branch_id()
554    }
555}