snarkvm-console-network 4.6.1

Network console library for a decentralized virtual machine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright (c) 2019-2026 Provable Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{FromBytes, ToBytes, io_error};

use enum_iterator::{Sequence, last};
use std::io;

/// The different consensus versions.
/// If you need the version active for a specific height, see: `N::CONSENSUS_VERSION`.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Sequence)]
#[repr(u16)]
pub enum ConsensusVersion {
    /// V1: The initial genesis consensus version.
    V1 = 1,
    /// V2: Update to the block reward and execution cost algorithms.
    V2 = 2,
    /// V3: Update to the number of validators and finalize scope RNG seed.
    V3 = 3,
    /// V4: Update to the Varuna version.
    V4 = 4,
    /// V5: Update to the number of validators and enable batch proposal spend limits.
    V5 = 5,
    /// V6: Update to the number of validators.
    V6 = 6,
    /// V7: Update to program rules.
    V7 = 7,
    /// V8: Update to inclusion version, record commitment version, and introduces sender ciphertexts.
    V8 = 8,
    /// V9: Support for program upgradability.
    V9 = 9,
    /// V10: Lower fees, appropriate record output type checking.
    V10 = 10,
    /// V11: Expand array size limit to 512 and introduce ECDSA signature verification opcodes.
    V11 = 11,
    /// V12: Prevent connection to forked nodes, disable StringType, enable block timestamp.
    V12 = 12,
    /// V13: Introduces external structs.
    V13 = 13,
    /// V14: Increase the program size limit to 512 kB, the transaction size limit to 540 kB,
    ///      the array size limit to 2048, and the `Future` argument bit size to 32 bits.
    ///      Introduces `aleo::GENERATOR`, `aleo::GENERATOR_POWERS`, `snark.verify` opcodes,
    ///      and dynamic dispatch, and identifier literal types.
    V14 = 14,
}

impl ToBytes for ConsensusVersion {
    fn write_le<W: io::Write>(&self, writer: W) -> io::Result<()> {
        (*self as u16).write_le(writer)
    }
}

impl FromBytes for ConsensusVersion {
    fn read_le<R: io::Read>(reader: R) -> io::Result<Self> {
        match u16::read_le(reader)? {
            0 => Err(io_error("Zero is not a valid consensus version")),
            1 => Ok(Self::V1),
            2 => Ok(Self::V2),
            3 => Ok(Self::V3),
            4 => Ok(Self::V4),
            5 => Ok(Self::V5),
            6 => Ok(Self::V6),
            7 => Ok(Self::V7),
            8 => Ok(Self::V8),
            9 => Ok(Self::V9),
            10 => Ok(Self::V10),
            11 => Ok(Self::V11),
            12 => Ok(Self::V12),
            13 => Ok(Self::V13),
            14 => Ok(Self::V14),
            _ => Err(io_error("Invalid consensus version")),
        }
    }
}

impl ConsensusVersion {
    pub fn latest() -> Self {
        last::<ConsensusVersion>().expect("At least one ConsensusVersion should be defined.")
    }
}

impl std::fmt::Display for ConsensusVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Use Debug formatting for Display.
        write!(f, "{self:?}")
    }
}

/// The number of consensus versions.
pub(crate) const NUM_CONSENSUS_VERSIONS: usize = enum_iterator::cardinality::<ConsensusVersion>();

/// The consensus version height for `CanaryV0`.
pub const CANARY_V0_CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] = [
    (ConsensusVersion::V1, 0),
    (ConsensusVersion::V2, 2_900_000),
    (ConsensusVersion::V3, 4_560_000),
    (ConsensusVersion::V4, 5_730_000),
    (ConsensusVersion::V5, 5_780_000),
    (ConsensusVersion::V6, 6_240_000),
    (ConsensusVersion::V7, 6_880_000),
    (ConsensusVersion::V8, 7_565_000),
    (ConsensusVersion::V9, 8_028_000),
    (ConsensusVersion::V10, 8_600_000),
    (ConsensusVersion::V11, 9_510_000),
    (ConsensusVersion::V12, 10_030_000),
    (ConsensusVersion::V13, 10_881_000),
    (ConsensusVersion::V14, 11_960_000),
];

/// The consensus version height for `MainnetV0`.
pub const MAINNET_V0_CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] = [
    (ConsensusVersion::V1, 0),
    (ConsensusVersion::V2, 2_800_000),
    (ConsensusVersion::V3, 4_900_000),
    (ConsensusVersion::V4, 6_135_000),
    (ConsensusVersion::V5, 7_060_000),
    (ConsensusVersion::V6, 7_560_000),
    (ConsensusVersion::V7, 7_570_000),
    (ConsensusVersion::V8, 9_430_000),
    (ConsensusVersion::V9, 10_272_000),
    (ConsensusVersion::V10, 11_205_000),
    (ConsensusVersion::V11, 12_870_000),
    (ConsensusVersion::V12, 13_815_000),
    (ConsensusVersion::V13, 16_850_000),
    (ConsensusVersion::V14, 17_700_000),
];

/// The consensus version heights for `TestnetV0`.
pub const TESTNET_V0_CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] = [
    (ConsensusVersion::V1, 0),
    (ConsensusVersion::V2, 2_950_000),
    (ConsensusVersion::V3, 4_800_000),
    (ConsensusVersion::V4, 6_625_000),
    (ConsensusVersion::V5, 6_765_000),
    (ConsensusVersion::V6, 7_600_000),
    (ConsensusVersion::V7, 8_365_000),
    (ConsensusVersion::V8, 9_173_000),
    (ConsensusVersion::V9, 9_800_000),
    (ConsensusVersion::V10, 10_525_000),
    (ConsensusVersion::V11, 11_952_000),
    (ConsensusVersion::V12, 12_669_000),
    (ConsensusVersion::V13, 14_906_000),
    (ConsensusVersion::V14, 15_370_000),
];

/// The consensus version heights when the `test_consensus_heights` feature is enabled.
pub const TEST_CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] = [
    (ConsensusVersion::V1, 0),
    (ConsensusVersion::V2, 5),
    (ConsensusVersion::V3, 6),
    (ConsensusVersion::V4, 7),
    (ConsensusVersion::V5, 8),
    (ConsensusVersion::V6, 9),
    (ConsensusVersion::V7, 10),
    (ConsensusVersion::V8, 11),
    (ConsensusVersion::V9, 12),
    (ConsensusVersion::V10, 13),
    (ConsensusVersion::V11, 14),
    (ConsensusVersion::V12, 15),
    (ConsensusVersion::V13, 16),
    (ConsensusVersion::V14, 17),
];

#[cfg(any(test, feature = "test", feature = "test_consensus_heights"))]
pub fn load_test_consensus_heights() -> [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] {
    // Attempt to read the test consensus heights from the environment variable.
    load_test_consensus_heights_inner(std::env::var("CONSENSUS_VERSION_HEIGHTS").ok())
}

#[cfg(any(test, feature = "test", feature = "test_consensus_heights", feature = "wasm"))]
pub(crate) fn load_test_consensus_heights_inner(
    consensus_version_heights: Option<String>,
) -> [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] {
    // Define a closure to verify the consensus heights.
    let verify_consensus_heights = |heights: &[(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS]| {
        // Assert that the genesis height is 0.
        assert_eq!(heights[0].1, 0, "Genesis height must be 0.");
        // Assert that the consensus heights are strictly increasing.
        for window in heights.windows(2) {
            if window[0] >= window[1] {
                panic!("Heights must be strictly increasing, but found: {window:?}");
            }
        }
    };

    // Define consensus version heights container used for testing.
    let mut test_consensus_heights = TEST_CONSENSUS_VERSION_HEIGHTS;

    // If version heights have been specified, verify and return them.
    match consensus_version_heights {
        Some(height_string) => {
            let parsing_error = format!("Expected exactly {NUM_CONSENSUS_VERSIONS} ConsensusVersion heights.");
            // Parse the heights from the environment variable.
            let parsed_test_consensus_heights: [u32; NUM_CONSENSUS_VERSIONS] = height_string
                .replace(" ", "")
                .split(",")
                .map(|height| height.parse::<u32>().expect("Heights should be valid u32 values."))
                .collect::<Vec<u32>>()
                .try_into()
                .expect(&parsing_error);
            // Set the parsed heights in the test consensus heights.
            for (i, height) in parsed_test_consensus_heights.into_iter().enumerate() {
                test_consensus_heights[i] = (TEST_CONSENSUS_VERSION_HEIGHTS[i].0, height);
            }
            // Verify and return the parsed test consensus heights.
            verify_consensus_heights(&test_consensus_heights);
            test_consensus_heights
        }
        None => {
            // Verify and return the default test consensus heights.
            verify_consensus_heights(&test_consensus_heights);
            test_consensus_heights
        }
    }
}

/// Returns the consensus configuration value for the specified height.
///
/// Arguments:
/// - `$network`: The network to use the constant of.
/// - `$constant`: The constant to search a value of.
/// - `$seek_height`: The block height to search the value for.
#[macro_export]
macro_rules! consensus_config_value {
    ($network:ident, $constant:ident, $seek_height:expr) => {
        // Search the consensus version enacted at the specified height.
        $network::CONSENSUS_VERSION($seek_height).map_or(None, |seek_version| {
            // Search the consensus value for the specified version.
            // NOTE: calling `consensus_config_value_by_version!` here would require callers to import both macros.
            match $network::$constant.binary_search_by(|(version, _)| version.cmp(&seek_version)) {
                // If a value was found for this consensus version, return it.
                Ok(index) => Some($network::$constant[index].1),
                // If the specified version was not found exactly, determine whether to return an appropriate value anyway.
                Err(index) => {
                    // This constant is not yet in effect at this consensus version.
                    if index == 0 {
                        None
                    // Return the appropriate value belonging to the consensus version *lower* than the sought version.
                    } else {
                        Some($network::$constant[index - 1].1)
                    }
                }
            }
        })
    };
}

/// Returns the consensus configuration value for the specified ConsensusVersion.
///
/// Arguments:
/// - `$network`: The network to use the constant of.
/// - `$constant`: The constant to search a value of.
/// - `$seek_version`: The ConsensusVersion to search the value for.
#[macro_export]
macro_rules! consensus_config_value_by_version {
    ($network:ident, $constant:ident, $seek_version:expr) => {
        // Search the consensus value for the specified version.
        match $network::$constant.binary_search_by(|(version, _)| version.cmp(&$seek_version)) {
            // If a value was found for this consensus version, return it.
            Ok(index) => Some($network::$constant[index].1),
            // If the specified version was not found exactly, determine whether to return an appropriate value anyway.
            Err(index) => {
                // This constant is not yet in effect at this consensus version.
                if index == 0 {
                    None
                // Return the appropriate value belonging to the consensus version *lower* than the sought version.
                } else {
                    Some($network::$constant[index - 1].1)
                }
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{CanaryV0, MainnetV0, Network, TestnetV0};

    /// Ensure that the consensus constants are defined and correct at genesis.
    /// It is possible this invariant no longer holds in the future, e.g. due to pruning or novel types of constants.
    fn consensus_constants_at_genesis<N: Network>() {
        let height = N::_CONSENSUS_VERSION_HEIGHTS.first().unwrap().1;
        assert_eq!(height, 0);
        let consensus_version = N::_CONSENSUS_VERSION_HEIGHTS.first().unwrap().0;
        assert_eq!(consensus_version, ConsensusVersion::V1);
        assert_eq!(consensus_version as usize, 1);
    }

    /// Ensure that the consensus *versions* are unique, incrementing and start with 1.
    fn consensus_versions<N: Network>() {
        let mut previous_version = N::_CONSENSUS_VERSION_HEIGHTS.first().unwrap().0;
        // Ensure that the consensus versions start with 1.
        assert_eq!(previous_version as usize, 1);
        // Ensure that the consensus versions are unique and incrementing by 1.
        for (version, _) in N::_CONSENSUS_VERSION_HEIGHTS.iter().skip(1) {
            assert_eq!(*version as usize, previous_version as usize + 1);
            previous_version = *version;
        }
        // Ensure that the consensus versions are unique and incrementing.
        let mut previous_version = N::MAX_CERTIFICATES.first().unwrap().0;
        for (version, _) in N::MAX_CERTIFICATES.iter().skip(1) {
            assert!(*version > previous_version);
            previous_version = *version;
        }
        let mut previous_version = N::TRANSACTION_SPEND_LIMIT.first().unwrap().0;
        for (version, _) in N::TRANSACTION_SPEND_LIMIT.iter().skip(1) {
            assert!(*version > previous_version);
            previous_version = *version;
        }
        let mut previous_version = N::MAX_ARRAY_ELEMENTS.first().unwrap().0;
        for (version, _) in N::MAX_ARRAY_ELEMENTS.iter().skip(1) {
            assert!(*version > previous_version);
            previous_version = *version;
        }
        let mut previous_version = N::MAX_PROGRAM_SIZE.first().unwrap().0;
        for (version, _) in N::MAX_PROGRAM_SIZE.iter().skip(1) {
            assert!(*version > previous_version);
            previous_version = *version;
        }
        let mut previous_version = N::MAX_TRANSACTION_SIZE.first().unwrap().0;
        for (version, _) in N::MAX_TRANSACTION_SIZE.iter().skip(1) {
            assert!(*version > previous_version);
            previous_version = *version;
        }
        let mut previous_version = N::MAX_WRITES.first().unwrap().0;
        for (version, _) in N::MAX_WRITES.iter().skip(1) {
            assert!(*version > previous_version);
            previous_version = *version;
        }
    }

    /// Ensure that consensus *heights* are unique and incrementing.
    fn consensus_constants_increasing_heights<N: Network>() {
        let mut previous_height = N::CONSENSUS_VERSION_HEIGHTS().first().unwrap().1;
        for (version, height) in N::CONSENSUS_VERSION_HEIGHTS().iter().skip(1) {
            assert!(*height > previous_height);
            previous_height = *height;
            // Ensure that N::CONSENSUS_VERSION returns the expected value.
            assert_eq!(N::CONSENSUS_VERSION(*height).unwrap(), *version);
            // Ensure that N::CONSENSUS_HEIGHT returns the expected value.
            assert_eq!(N::CONSENSUS_HEIGHT(*version).unwrap(), *height);
        }
    }

    /// Ensure that version of all consensus-relevant constants are present in the consensus version heights.
    fn consensus_constants_valid_heights<N: Network>() {
        for (version, value) in N::MAX_CERTIFICATES.iter() {
            // Ensure that the height at which an update occurs are present in CONSENSUS_VERSION_HEIGHTS.
            let height = N::CONSENSUS_VERSION_HEIGHTS().iter().find(|(c_version, _)| *c_version == *version).unwrap().1;
            // Double-check that consensus_config_value returns the correct value.
            assert_eq!(consensus_config_value!(N, MAX_CERTIFICATES, height).unwrap(), *value);
        }
        for (version, value) in N::TRANSACTION_SPEND_LIMIT.iter() {
            // Ensure that the height at which an update occurs are present in CONSENSUS_VERSION_HEIGHTS.
            let height = N::CONSENSUS_VERSION_HEIGHTS().iter().find(|(c_version, _)| *c_version == *version).unwrap().1;
            // Double-check that consensus_config_value returns the correct value.
            assert_eq!(consensus_config_value!(N, TRANSACTION_SPEND_LIMIT, height).unwrap(), *value);
        }
        for (version, value) in N::MAX_ARRAY_ELEMENTS.iter() {
            // Ensure that the height at which an update occurs are present in CONSENSUS_VERSION_HEIGHTS.
            let height = N::CONSENSUS_VERSION_HEIGHTS().iter().find(|(c_version, _)| *c_version == *version).unwrap().1;
            // Double-check that consensus_config_value returns the correct value.
            assert_eq!(consensus_config_value!(N, MAX_ARRAY_ELEMENTS, height).unwrap(), *value);
        }
        for (version, value) in N::MAX_PROGRAM_SIZE.iter() {
            // Ensure that the height at which an update occurs are present in CONSENSUS_VERSION_HEIGHTS.
            let height = N::CONSENSUS_VERSION_HEIGHTS().iter().find(|(c_version, _)| *c_version == *version).unwrap().1;
            // Double-check that consensus_config_value returns the correct value.
            assert_eq!(consensus_config_value!(N, MAX_PROGRAM_SIZE, height).unwrap(), *value);
        }
        for (version, value) in N::MAX_TRANSACTION_SIZE.iter() {
            // Ensure that the height at which an update occurs are present in CONSENSUS_VERSION_HEIGHTS.
            let height = N::CONSENSUS_VERSION_HEIGHTS().iter().find(|(c_version, _)| *c_version == *version).unwrap().1;
            // Double-check that consensus_config_value returns the correct value.
            assert_eq!(consensus_config_value!(N, MAX_TRANSACTION_SIZE, height).unwrap(), *value);
        }
        for (version, value) in N::MAX_WRITES.iter() {
            // Ensure that the height at which an update occurs are present in CONSENSUS_VERSION_HEIGHTS.
            let height = N::CONSENSUS_VERSION_HEIGHTS().iter().find(|(c_version, _)| *c_version == *version).unwrap().1;
            // Double-check that consensus_config_value returns the correct value.
            assert_eq!(consensus_config_value!(N, MAX_WRITES, height).unwrap(), *value);
        }
    }

    /// Ensure that consensus_config_value returns a valid value for all consensus versions.
    fn consensus_config_returns_some<N: Network>() {
        for (_, height) in N::CONSENSUS_VERSION_HEIGHTS().iter() {
            assert!(consensus_config_value!(N, MAX_CERTIFICATES, *height).is_some());
            assert!(consensus_config_value!(N, TRANSACTION_SPEND_LIMIT, *height).is_some());
            assert!(consensus_config_value!(N, MAX_ARRAY_ELEMENTS, *height).is_some());
            assert!(consensus_config_value!(N, MAX_PROGRAM_SIZE, *height).is_some());
            assert!(consensus_config_value!(N, MAX_TRANSACTION_SIZE, *height).is_some());
            assert!(consensus_config_value!(N, MAX_WRITES, *height).is_some());
        }
    }

    /// Ensure that `MAX_CERTIFICATES` increases and is correctly defined.
    /// See the constant declaration for an explanation why.
    fn max_certificates_increasing<N: Network>() {
        let mut previous_value = N::MAX_CERTIFICATES.first().unwrap().1;
        for (_, value) in N::MAX_CERTIFICATES.iter().skip(1) {
            assert!(*value >= previous_value);
            previous_value = *value;
        }
    }

    /// Ensure that `MAX_ARRAY_ELEMENTS` increases and is correctly defined.
    /// See the constant declaration for an explanation why.
    fn max_array_elements_increasing<N: Network>() {
        let mut previous_value = N::MAX_ARRAY_ELEMENTS.first().unwrap().1;
        for (_, value) in N::MAX_ARRAY_ELEMENTS.iter().skip(1) {
            assert!(*value >= previous_value);
            previous_value = *value;
        }
    }

    /// Ensure that `MAX_TRANSACTION_SIZE` is at least 28KB greater than `MAX_PROGRAM_SIZE` for all consensus versions.
    /// This overhead accounts for proofs, signatures, and other transaction metadata.
    fn transaction_size_exceeds_program_size<N: Network>() {
        const MIN_OVERHEAD: usize = 28_000; // 28 kB minimum overhead

        for (_, height) in N::CONSENSUS_VERSION_HEIGHTS().iter() {
            let max_program_size = consensus_config_value!(N, MAX_PROGRAM_SIZE, *height).unwrap();
            let max_transaction_size = consensus_config_value!(N, MAX_TRANSACTION_SIZE, *height).unwrap();

            assert!(
                max_transaction_size >= max_program_size + MIN_OVERHEAD,
                "At height {height}: MAX_TRANSACTION_SIZE ({max_transaction_size}) must be at least {MIN_OVERHEAD} bytes greater than MAX_PROGRAM_SIZE ({max_program_size})"
            );
        }
    }

    /// Ensure that the number of constant definitions is the same across networks.
    fn constants_equal_length<N1: Network, N2: Network, N3: Network>() {
        // If we can construct an array, that means the underlying types must be the same.
        let _ = [N1::CONSENSUS_VERSION_HEIGHTS, N2::CONSENSUS_VERSION_HEIGHTS, N3::CONSENSUS_VERSION_HEIGHTS];
        let _ = [N1::MAX_CERTIFICATES, N2::MAX_CERTIFICATES, N3::MAX_CERTIFICATES];
        let _ = [N1::TRANSACTION_SPEND_LIMIT, N2::TRANSACTION_SPEND_LIMIT, N3::TRANSACTION_SPEND_LIMIT];
        let _ = [N1::MAX_ARRAY_ELEMENTS, N2::MAX_ARRAY_ELEMENTS, N3::MAX_ARRAY_ELEMENTS];
        let _ = [N1::MAX_PROGRAM_SIZE, N2::MAX_PROGRAM_SIZE, N3::MAX_PROGRAM_SIZE];
        let _ = [N1::MAX_TRANSACTION_SIZE, N2::MAX_TRANSACTION_SIZE, N3::MAX_TRANSACTION_SIZE];
        let _ = [N1::MAX_WRITES, N2::MAX_WRITES, N3::MAX_WRITES];
    }

    /// Ensure that `LATEST_MAX_*` functions return valid values without panicking.
    /// These functions use `.expect()` internally, so this test verifies the arrays are non-empty.
    fn latest_max_functions_are_safe<N: Network>() {
        // Verify LATEST_MAX_CERTIFICATES returns a positive value.
        assert!(N::LATEST_MAX_CERTIFICATES() > 0, "LATEST_MAX_CERTIFICATES must be positive");
        // Verify LATEST_MAX_PROGRAM_SIZE returns a positive value.
        assert!(N::LATEST_MAX_PROGRAM_SIZE() > 0, "LATEST_MAX_PROGRAM_SIZE must be positive");
        // Verify LATEST_MAX_TRANSACTION_SIZE returns a positive value.
        assert!(N::LATEST_MAX_TRANSACTION_SIZE() > 0, "LATEST_MAX_TRANSACTION_SIZE must be positive");
        // Verify LATEST_MAX_WRITES returns a positive value.
        assert!(N::LATEST_MAX_WRITES() > 0, "LATEST_MAX_WRITES must be positive");
    }

    #[test]
    #[allow(clippy::assertions_on_constants)]
    fn test_consensus_constants() {
        consensus_constants_at_genesis::<MainnetV0>();
        consensus_constants_at_genesis::<TestnetV0>();
        consensus_constants_at_genesis::<CanaryV0>();

        consensus_versions::<MainnetV0>();
        consensus_versions::<TestnetV0>();
        consensus_versions::<CanaryV0>();

        consensus_constants_increasing_heights::<MainnetV0>();
        consensus_constants_increasing_heights::<TestnetV0>();
        consensus_constants_increasing_heights::<CanaryV0>();

        consensus_constants_valid_heights::<MainnetV0>();
        consensus_constants_valid_heights::<TestnetV0>();
        consensus_constants_valid_heights::<CanaryV0>();

        consensus_config_returns_some::<MainnetV0>();
        consensus_config_returns_some::<TestnetV0>();
        consensus_config_returns_some::<CanaryV0>();

        max_certificates_increasing::<MainnetV0>();
        max_certificates_increasing::<TestnetV0>();
        max_certificates_increasing::<CanaryV0>();

        max_array_elements_increasing::<MainnetV0>();
        max_array_elements_increasing::<TestnetV0>();
        max_array_elements_increasing::<CanaryV0>();

        transaction_size_exceeds_program_size::<MainnetV0>();
        transaction_size_exceeds_program_size::<TestnetV0>();
        transaction_size_exceeds_program_size::<CanaryV0>();

        latest_max_functions_are_safe::<MainnetV0>();
        latest_max_functions_are_safe::<TestnetV0>();
        latest_max_functions_are_safe::<CanaryV0>();

        constants_equal_length::<MainnetV0, TestnetV0, CanaryV0>();
    }

    /// Ensure (de-)serialization works correctly.
    #[test]
    fn test_to_bytes() {
        let version = ConsensusVersion::V8;
        let bytes = version.to_bytes_le().unwrap();
        let result = ConsensusVersion::from_bytes_le(&bytes).unwrap();
        assert_eq!(result, version);

        let version = ConsensusVersion::latest();
        let bytes = version.to_bytes_le().unwrap();
        let result = ConsensusVersion::from_bytes_le(&bytes).unwrap();
        assert_eq!(result, version);

        let invalid_bytes = u16::MAX.to_bytes_le().unwrap();
        let result = ConsensusVersion::from_bytes_le(&invalid_bytes);
        assert!(result.is_err());
    }
}