snarkvm_console_network/
consensus_heights.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::{FromBytes, ToBytes, io_error};
17
18use enum_iterator::{Sequence, last};
19use std::io;
20
21/// The different consensus versions.
22/// If you need the version active for a specific height, see: `N::CONSENSUS_VERSION`.
23#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Sequence)]
24#[repr(u16)]
25pub enum ConsensusVersion {
26    /// V1: The initial genesis consensus version.
27    V1 = 1,
28    /// V2: Update to the block reward and execution cost algorithms.
29    V2 = 2,
30    /// V3: Update to the number of validators and finalize scope RNG seed.
31    V3 = 3,
32    /// V4: Update to the Varuna version.
33    V4 = 4,
34    /// V5: Update to the number of validators and enable batch proposal spend limits.
35    V5 = 5,
36    /// V6: Update to the number of validators.
37    V6 = 6,
38    /// V7: Update to program rules.
39    V7 = 7,
40    /// V8: Update to inclusion version, record commitment version, and introduces sender ciphertexts.
41    V8 = 8,
42    /// V9: Support for program upgradability.
43    V9 = 9,
44    /// V10: Lower fees, appropriate record output type checking.
45    V10 = 10,
46    /// V11: Expand array size limit to 512 and introduce ECDSA signature verification opcodes.
47    V11 = 11,
48}
49
50impl ToBytes for ConsensusVersion {
51    fn write_le<W: io::Write>(&self, writer: W) -> io::Result<()> {
52        (*self as u16).write_le(writer)
53    }
54}
55
56impl FromBytes for ConsensusVersion {
57    fn read_le<R: io::Read>(reader: R) -> io::Result<Self> {
58        match u16::read_le(reader)? {
59            0 => Err(io_error("Zero is not a valid consensus version")),
60            1 => Ok(Self::V1),
61            2 => Ok(Self::V2),
62            3 => Ok(Self::V3),
63            4 => Ok(Self::V4),
64            5 => Ok(Self::V5),
65            6 => Ok(Self::V6),
66            7 => Ok(Self::V7),
67            8 => Ok(Self::V8),
68            9 => Ok(Self::V9),
69            10 => Ok(Self::V10),
70            11 => Ok(Self::V11),
71            _ => Err(io_error("Invalid consensus version")),
72        }
73    }
74}
75
76impl ConsensusVersion {
77    pub fn latest() -> Self {
78        last::<ConsensusVersion>().expect("At least one ConsensusVersion should be defined.")
79    }
80}
81
82impl std::fmt::Display for ConsensusVersion {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        // Use Debug formatting for Display.
85        write!(f, "{self:?}")
86    }
87}
88
89/// The number of consensus versions.
90pub(crate) const NUM_CONSENSUS_VERSIONS: usize = 11;
91
92/// The consensus version height for `CanaryV0`.
93pub const CANARY_V0_CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] = [
94    (ConsensusVersion::V1, 0),
95    (ConsensusVersion::V2, 2_900_000),
96    (ConsensusVersion::V3, 4_560_000),
97    (ConsensusVersion::V4, 5_730_000),
98    (ConsensusVersion::V5, 5_780_000),
99    (ConsensusVersion::V6, 6_240_000),
100    (ConsensusVersion::V7, 6_880_000),
101    (ConsensusVersion::V8, 7_565_000),
102    (ConsensusVersion::V9, 8_028_000),
103    (ConsensusVersion::V10, 8_600_000),
104    (ConsensusVersion::V11, 9_510_000),
105];
106
107/// The consensus version height for `MainnetV0`.
108pub const MAINNET_V0_CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] = [
109    (ConsensusVersion::V1, 0),
110    (ConsensusVersion::V2, 2_800_000),
111    (ConsensusVersion::V3, 4_900_000),
112    (ConsensusVersion::V4, 6_135_000),
113    (ConsensusVersion::V5, 7_060_000),
114    (ConsensusVersion::V6, 7_560_000),
115    (ConsensusVersion::V7, 7_570_000),
116    (ConsensusVersion::V8, 9_430_000),
117    (ConsensusVersion::V9, 10_272_000),
118    (ConsensusVersion::V10, 11_205_000),
119    (ConsensusVersion::V11, 12_870_000),
120];
121
122/// The consensus version heights for `TestnetV0`.
123pub const TESTNET_V0_CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] = [
124    (ConsensusVersion::V1, 0),
125    (ConsensusVersion::V2, 2_950_000),
126    (ConsensusVersion::V3, 4_800_000),
127    (ConsensusVersion::V4, 6_625_000),
128    (ConsensusVersion::V5, 6_765_000),
129    (ConsensusVersion::V6, 7_600_000),
130    (ConsensusVersion::V7, 8_365_000),
131    (ConsensusVersion::V8, 9_173_000),
132    (ConsensusVersion::V9, 9_800_000),
133    (ConsensusVersion::V10, 10_525_000),
134    (ConsensusVersion::V11, 11_952_000),
135];
136
137/// The consensus version heights when the `test_consensus_heights` feature is enabled.
138pub const TEST_CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] = [
139    (ConsensusVersion::V1, 0),
140    (ConsensusVersion::V2, 5),
141    (ConsensusVersion::V3, 6),
142    (ConsensusVersion::V4, 7),
143    (ConsensusVersion::V5, 8),
144    (ConsensusVersion::V6, 9),
145    (ConsensusVersion::V7, 10),
146    (ConsensusVersion::V8, 11),
147    (ConsensusVersion::V9, 12),
148    (ConsensusVersion::V10, 13),
149    (ConsensusVersion::V11, 14),
150];
151
152#[cfg(any(test, feature = "test", feature = "test_consensus_heights"))]
153pub fn load_test_consensus_heights() -> [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] {
154    // Attempt to read the test consensus heights from the environment variable.
155    load_test_consensus_heights_inner(std::env::var("CONSENSUS_VERSION_HEIGHTS").ok())
156}
157
158#[cfg(any(test, feature = "test", feature = "test_consensus_heights", feature = "wasm"))]
159pub(crate) fn load_test_consensus_heights_inner(
160    consensus_version_heights: Option<String>,
161) -> [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] {
162    // Define a closure to verify the consensus heights.
163    let verify_consensus_heights = |heights: &[(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS]| {
164        // Assert that the genesis height is 0.
165        assert_eq!(heights[0].1, 0, "Genesis height must be 0.");
166        // Assert that the consensus heights are strictly increasing.
167        for window in heights.windows(2) {
168            if window[0] >= window[1] {
169                panic!("Heights must be strictly increasing, but found: {window:?}");
170            }
171        }
172    };
173
174    // Define consensus version heights container used for testing.
175    let mut test_consensus_heights = TEST_CONSENSUS_VERSION_HEIGHTS;
176
177    // If version heights have been specified, verify and return them.
178    match consensus_version_heights {
179        Some(height_string) => {
180            let parsing_error = format!("Expected exactly {NUM_CONSENSUS_VERSIONS} ConsensusVersion heights.");
181            // Parse the heights from the environment variable.
182            let parsed_test_consensus_heights: [u32; NUM_CONSENSUS_VERSIONS] = height_string
183                .replace(" ", "")
184                .split(",")
185                .map(|height| height.parse::<u32>().expect("Heights should be valid u32 values."))
186                .collect::<Vec<u32>>()
187                .try_into()
188                .expect(&parsing_error);
189            // Set the parsed heights in the test consensus heights.
190            for (i, height) in parsed_test_consensus_heights.into_iter().enumerate() {
191                test_consensus_heights[i] = (TEST_CONSENSUS_VERSION_HEIGHTS[i].0, height);
192            }
193            // Verify and return the parsed test consensus heights.
194            verify_consensus_heights(&test_consensus_heights);
195            test_consensus_heights
196        }
197        None => {
198            // Verify and return the default test consensus heights.
199            verify_consensus_heights(&test_consensus_heights);
200            test_consensus_heights
201        }
202    }
203}
204
205/// Returns the consensus configuration value for the specified height.
206///
207/// Arguments:
208/// - `$network`: The network to use the constant of.
209/// - `$constant`: The constant to search a value of.
210/// - `$seek_height`: The block height to search the value for.
211#[macro_export]
212macro_rules! consensus_config_value {
213    ($network:ident, $constant:ident, $seek_height:expr) => {
214        // Search the consensus version enacted at the specified height.
215        $network::CONSENSUS_VERSION($seek_height).map_or(None, |seek_version| {
216            // Search the consensus value for the specified version.
217            // NOTE: calling `consensus_config_value_by_version!` here would require callers to import both macros.
218            match $network::$constant.binary_search_by(|(version, _)| version.cmp(&seek_version)) {
219                // If a value was found for this consensus version, return it.
220                Ok(index) => Some($network::$constant[index].1),
221                // If the specified version was not found exactly, determine whether to return an appropriate value anyway.
222                Err(index) => {
223                    // This constant is not yet in effect at this consensus version.
224                    if index == 0 {
225                        None
226                    // Return the appropriate value belonging to the consensus version *lower* than the sought version.
227                    } else {
228                        Some($network::$constant[index - 1].1)
229                    }
230                }
231            }
232        })
233    };
234}
235
236/// Returns the consensus configuration value for the specified ConsensusVersion.
237///
238/// Arguments:
239/// - `$network`: The network to use the constant of.
240/// - `$constant`: The constant to search a value of.
241/// - `$seek_version`: The ConsensusVersion to search the value for.
242#[macro_export]
243macro_rules! consensus_config_value_by_version {
244    ($network:ident, $constant:ident, $seek_version:expr) => {
245        // Search the consensus value for the specified version.
246        match $network::$constant.binary_search_by(|(version, _)| version.cmp(&$seek_version)) {
247            // If a value was found for this consensus version, return it.
248            Ok(index) => Some($network::$constant[index].1),
249            // If the specified version was not found exactly, determine whether to return an appropriate value anyway.
250            Err(index) => {
251                // This constant is not yet in effect at this consensus version.
252                if index == 0 {
253                    None
254                // Return the appropriate value belonging to the consensus version *lower* than the sought version.
255                } else {
256                    Some($network::$constant[index - 1].1)
257                }
258            }
259        }
260    };
261}
262
263#[cfg(test)]
264mod tests {
265    use super::*;
266    use crate::{CanaryV0, MainnetV0, Network, TestnetV0};
267
268    /// Ensure that the consensus constants are defined and correct at genesis.
269    /// It is possible this invariant no longer holds in the future, e.g. due to pruning or novel types of constants.
270    fn consensus_constants_at_genesis<N: Network>() {
271        let height = N::_CONSENSUS_VERSION_HEIGHTS.first().unwrap().1;
272        assert_eq!(height, 0);
273        let consensus_version = N::_CONSENSUS_VERSION_HEIGHTS.first().unwrap().0;
274        assert_eq!(consensus_version, ConsensusVersion::V1);
275        assert_eq!(consensus_version as usize, 1);
276    }
277
278    /// Ensure that the consensus *versions* are unique, incrementing and start with 1.
279    fn consensus_versions<N: Network>() {
280        let mut previous_version = N::_CONSENSUS_VERSION_HEIGHTS.first().unwrap().0;
281        // Ensure that the consensus versions start with 1.
282        assert_eq!(previous_version as usize, 1);
283        // Ensure that the consensus versions are unique and incrementing by 1.
284        for (version, _) in N::_CONSENSUS_VERSION_HEIGHTS.iter().skip(1) {
285            assert_eq!(*version as usize, previous_version as usize + 1);
286            previous_version = *version;
287        }
288        // Ensure that the consensus versions are unique and incrementing.
289        let mut previous_version = N::MAX_CERTIFICATES.first().unwrap().0;
290        for (version, _) in N::MAX_CERTIFICATES.iter().skip(1) {
291            assert!(*version > previous_version);
292            previous_version = *version;
293        }
294        let mut previous_version = N::TRANSACTION_SPEND_LIMIT.first().unwrap().0;
295        for (version, _) in N::TRANSACTION_SPEND_LIMIT.iter().skip(1) {
296            assert!(*version > previous_version);
297            previous_version = *version;
298        }
299    }
300
301    /// Ensure that consensus *heights* are unique and incrementing.
302    fn consensus_constants_increasing_heights<N: Network>() {
303        let mut previous_height = N::CONSENSUS_VERSION_HEIGHTS().first().unwrap().1;
304        for (version, height) in N::CONSENSUS_VERSION_HEIGHTS().iter().skip(1) {
305            assert!(*height > previous_height);
306            previous_height = *height;
307            // Ensure that N::CONSENSUS_VERSION returns the expected value.
308            assert_eq!(N::CONSENSUS_VERSION(*height).unwrap(), *version);
309            // Ensure that N::CONSENSUS_HEIGHT returns the expected value.
310            assert_eq!(N::CONSENSUS_HEIGHT(*version).unwrap(), *height);
311        }
312    }
313
314    /// Ensure that version of all consensus-relevant constants are present in the consensus version heights.
315    fn consensus_constants_valid_heights<N: Network>() {
316        for (version, value) in N::MAX_CERTIFICATES.iter() {
317            // Ensure that the height at which an update occurs are present in CONSENSUS_VERSION_HEIGHTS.
318            let height = N::CONSENSUS_VERSION_HEIGHTS().iter().find(|(c_version, _)| *c_version == *version).unwrap().1;
319            // Double-check that consensus_config_value returns the correct value.
320            assert_eq!(consensus_config_value!(N, MAX_CERTIFICATES, height).unwrap(), *value);
321        }
322        for (version, value) in N::TRANSACTION_SPEND_LIMIT.iter() {
323            // Ensure that the height at which an update occurs are present in CONSENSUS_VERSION_HEIGHTS.
324            let height = N::CONSENSUS_VERSION_HEIGHTS().iter().find(|(c_version, _)| *c_version == *version).unwrap().1;
325            // Double-check that consensus_config_value returns the correct value.
326            assert_eq!(consensus_config_value!(N, TRANSACTION_SPEND_LIMIT, height).unwrap(), *value);
327        }
328    }
329
330    /// Ensure that consensus_config_value returns a valid value for all consensus versions.
331    fn consensus_config_returns_some<N: Network>() {
332        for (_, height) in N::CONSENSUS_VERSION_HEIGHTS().iter() {
333            assert!(consensus_config_value!(N, MAX_CERTIFICATES, *height).is_some());
334            assert!(consensus_config_value!(N, TRANSACTION_SPEND_LIMIT, *height).is_some());
335        }
336    }
337
338    /// Ensure that `MAX_CERTIFICATES` increases and is correctly defined.
339    /// See the constant declaration for an explanation why.
340    fn max_certificates_increasing<N: Network>() {
341        let mut previous_value = N::MAX_CERTIFICATES.first().unwrap().1;
342        for (_, value) in N::MAX_CERTIFICATES.iter().skip(1) {
343            assert!(*value >= previous_value);
344            previous_value = *value;
345        }
346    }
347
348    /// Ensure that the number of constant definitions is the same across networks.
349    fn constants_equal_length<N1: Network, N2: Network, N3: Network>() {
350        // If we can construct an array, that means the underlying types must be the same.
351        let _ = [N1::CONSENSUS_VERSION_HEIGHTS, N2::CONSENSUS_VERSION_HEIGHTS, N3::CONSENSUS_VERSION_HEIGHTS];
352        let _ = [N1::MAX_CERTIFICATES, N2::MAX_CERTIFICATES, N3::MAX_CERTIFICATES];
353        let _ = [N1::TRANSACTION_SPEND_LIMIT, N2::TRANSACTION_SPEND_LIMIT, N3::TRANSACTION_SPEND_LIMIT];
354    }
355
356    #[test]
357    #[allow(clippy::assertions_on_constants)]
358    fn test_consensus_constants() {
359        consensus_constants_at_genesis::<MainnetV0>();
360        consensus_constants_at_genesis::<TestnetV0>();
361        consensus_constants_at_genesis::<CanaryV0>();
362
363        consensus_versions::<MainnetV0>();
364        consensus_versions::<TestnetV0>();
365        consensus_versions::<CanaryV0>();
366
367        consensus_constants_increasing_heights::<MainnetV0>();
368        consensus_constants_increasing_heights::<TestnetV0>();
369        consensus_constants_increasing_heights::<CanaryV0>();
370
371        consensus_constants_valid_heights::<MainnetV0>();
372        consensus_constants_valid_heights::<TestnetV0>();
373        consensus_constants_valid_heights::<CanaryV0>();
374
375        consensus_config_returns_some::<MainnetV0>();
376        consensus_config_returns_some::<TestnetV0>();
377        consensus_config_returns_some::<CanaryV0>();
378
379        max_certificates_increasing::<MainnetV0>();
380        max_certificates_increasing::<TestnetV0>();
381        max_certificates_increasing::<CanaryV0>();
382
383        constants_equal_length::<MainnetV0, TestnetV0, CanaryV0>();
384    }
385
386    /// Ensure (de-)serialization works correctly.
387    #[test]
388    fn test_to_bytes() {
389        let version = ConsensusVersion::V8;
390        let bytes = version.to_bytes_le().unwrap();
391        let result = ConsensusVersion::from_bytes_le(&bytes).unwrap();
392        assert_eq!(result, version);
393
394        let invalid_bytes = u16::MAX.to_bytes_le().unwrap();
395        let result = ConsensusVersion::from_bytes_le(&invalid_bytes);
396        assert!(result.is_err());
397    }
398}