neptune-consensus 0.14.0

Consensus logic and proof abstractions for Neptune Cash.
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use neptune_primitives::block_height::BlockHeight;
use neptune_primitives::block_height::NUM_BLOCKS_SKIPPED_BECAUSE_REBOOT;
use neptune_primitives::network::Network;
use neptune_primitives::timestamp::Timestamp;
use num_traits::Zero;
use tasm_lib::twenty_first::math::b_field_element::BFieldElement;

use crate::block::pow::LustrationStatus;
use crate::block::INITIAL_BLOCK_SUBSIDY;
use crate::block::MAX_NUM_INPUTS_OUTPUTS_ANNOUNCEMENTS;
use crate::block::PREMINE_MAX_SIZE;
use crate::type_scripts::native_currency_amount::NativeCurrencyAmount;

/// Height of 1st block that follows the alpha consensus ruleset, for main net.
pub const BLOCK_HEIGHT_HARDFORK_ALPHA_MAIN_NET: BlockHeight =
    BlockHeight::new(BFieldElement::new(15_000u64));

/// Height of 1st block that follows the alpha consensus ruleset, for test net.
pub const BLOCK_HEIGHT_HARDFORK_ALPHA_TESTNET: BlockHeight =
    BlockHeight::new(BFieldElement::new(120u64));

/// Height of 1st block that follows the alpha consensus ruleset, for test net.
pub const BLOCK_HEIGHT_HARDFORK_TVMV_PROOF_V1_TESTNET: BlockHeight =
    BlockHeight::new(BFieldElement::new(3571u64));

/// Height of 1st block that uses Triton VM with proof version 1.
pub const BLOCK_HEIGHT_HARDFORK_TVMV_PROOF_V1_MAIN_NET: BlockHeight =
    BlockHeight::new(BFieldElement::new(23_401u64));

/// Height of 1st block changing PoW algorithm to drop memory hardness
pub const BLOCK_HEIGHT_HARDFORK_BETA_MAIN_NET: BlockHeight =
    BlockHeight::new(BFieldElement::new(38_000u64));

/// Height of 1st block changing PoW algorithm to drop memory hardness, for test
/// net.
pub const BLOCK_HEIGHT_HARDFORK_BETA_TESTNET: BlockHeight =
    BlockHeight::new(BFieldElement::new(3_669));

/// Height of the first block after hard fork gamma, which fixes the June 2026
/// soundness issues, on main net.
pub const BLOCK_HEIGHT_HARDFORK_GAMMA_MAIN_NET: BlockHeight =
    BlockHeight::new(BFieldElement::new(40_300u64));

/// Height of the first block after hard fork gamma, which fixes the June 2026
/// soundness issue, on test net.
pub const BLOCK_HEIGHT_HARDFORK_GAMMA_TESTNET: BlockHeight =
    BlockHeight::new(BFieldElement::new(4_650));

/// Transactions that are more than three days older than the block are
/// disallowed. Only enforced from hardfork gamma and onwards.
pub const TX_BACKDATING_LIMIT: Timestamp = Timestamp::days(3);

/// Enumerates all possible sets of consensus rules.
///
/// Specifically, this enum captures *differences* between consensus rules,
/// across
///  - networks, and
///  - hard and soft forks triggered by blocks.
#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::EnumIter, Default, strum::Display)]
pub enum ConsensusRuleSet {
    /// First rule set after reboot
    Reboot,

    /// Allow reuse of preprocessing step for new block proposals
    HardforkAlpha,

    /// Upgrade from Triton VM proof version v0 to v1
    TvmProofVersion1,

    /// Remove memory hardness from PoW algorithm, add lustration barrier for
    /// old inputs, compare difficulty to own block header instead of parent's
    /// block header.
    HardforkBeta,

    /// Fix June 2026 soundness issue in recursive proof verifier upstream in
    /// tasm-lib. Fix June 2026 soundness issue in Triton VM's
    /// sponge_mem_absorb instruction, as well as other under-constrained trace
    /// values.
    ///
    /// Also restarts the lustration counting since all past proofs have been
    /// found to be unsound.
    #[default]
    HardforkGamma,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::Display)]
pub enum TritonProofVersion {
    V0,
    V1,
    V5,
}

impl TritonProofVersion {
    /// The version value used in Triton VM's claim
    pub(crate) fn version(&self) -> u32 {
        match self {
            TritonProofVersion::V0 => 0,
            TritonProofVersion::V1 => 1,
            TritonProofVersion::V5 => 5,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LustrationRule {
    Initial(LustrationStatus),
    Updated {
        // This data is actually redundant but allows for a security-in-depth
        // extra sanity check.
        initial_counter: NativeCurrencyAmount,
    },
}

impl ConsensusRuleSet {
    /// Infer the [`ConsensusRuleSet`] from the [`Network`] and the
    /// [`BlockHeight`]. The second argument is necessary to take into account
    /// planned hard or soft forks that activate at a given height. The first
    /// argument is necessary because the forks can activate at different
    /// heights based on the network.
    pub fn infer_from(network: Network, block_height: BlockHeight) -> Self {
        let first_lustration_block = Self::first_lustration_block(network);
        match network {
            Network::Main => {
                if block_height < BLOCK_HEIGHT_HARDFORK_ALPHA_MAIN_NET {
                    ConsensusRuleSet::Reboot
                } else if block_height < BLOCK_HEIGHT_HARDFORK_TVMV_PROOF_V1_MAIN_NET {
                    ConsensusRuleSet::HardforkAlpha
                } else if block_height < first_lustration_block {
                    ConsensusRuleSet::TvmProofVersion1
                } else if block_height < BLOCK_HEIGHT_HARDFORK_GAMMA_MAIN_NET {
                    ConsensusRuleSet::HardforkBeta
                } else {
                    ConsensusRuleSet::HardforkGamma
                }
            }
            Network::Testnet(0) => {
                if block_height < BLOCK_HEIGHT_HARDFORK_ALPHA_TESTNET {
                    ConsensusRuleSet::Reboot
                } else if block_height < BLOCK_HEIGHT_HARDFORK_TVMV_PROOF_V1_TESTNET {
                    ConsensusRuleSet::HardforkAlpha
                } else if block_height < first_lustration_block {
                    ConsensusRuleSet::TvmProofVersion1
                } else if block_height < BLOCK_HEIGHT_HARDFORK_GAMMA_TESTNET {
                    ConsensusRuleSet::HardforkBeta
                } else {
                    ConsensusRuleSet::HardforkGamma
                }
            }
            _ => ConsensusRuleSet::HardforkGamma,
        }
    }

    pub fn memory_hard_pow(&self) -> bool {
        match self {
            ConsensusRuleSet::Reboot => true,
            ConsensusRuleSet::HardforkAlpha => true,
            ConsensusRuleSet::TvmProofVersion1 => true,
            ConsensusRuleSet::HardforkBeta => false,
            ConsensusRuleSet::HardforkGamma => false,
        }
    }

    /// Returns true if PoW threshold is defined relative to parent difficulty,
    /// and false if it is defined relative to own difficulty value.
    pub fn use_parent_difficulty(&self) -> bool {
        match self {
            ConsensusRuleSet::Reboot => true,
            ConsensusRuleSet::HardforkAlpha => true,
            ConsensusRuleSet::TvmProofVersion1 => true,
            ConsensusRuleSet::HardforkBeta => false,
            ConsensusRuleSet::HardforkGamma => false,
        }
    }

    pub fn requires_lustration_status_in_block_header(&self) -> bool {
        match self {
            ConsensusRuleSet::Reboot
            | ConsensusRuleSet::HardforkAlpha
            | ConsensusRuleSet::TvmProofVersion1 => false,
            ConsensusRuleSet::HardforkBeta => true,
            ConsensusRuleSet::HardforkGamma => true,
        }
    }

    pub(crate) fn requires_version_in_pow(&self) -> bool {
        match self {
            ConsensusRuleSet::Reboot
            | ConsensusRuleSet::HardforkAlpha
            | ConsensusRuleSet::TvmProofVersion1 => false,
            ConsensusRuleSet::HardforkBeta => true,
            ConsensusRuleSet::HardforkGamma => true,
        }
    }

    pub fn transaction_backdating_threshold(&self) -> Option<Timestamp> {
        match self {
            ConsensusRuleSet::Reboot
            | ConsensusRuleSet::HardforkAlpha
            | ConsensusRuleSet::TvmProofVersion1
            | ConsensusRuleSet::HardforkBeta => None,
            ConsensusRuleSet::HardforkGamma => Some(TX_BACKDATING_LIMIT),
        }
    }

    /// Return a boolean whether or not the double-counting issue in the
    /// update of the lustration counter should be fixed.
    ///
    /// Must be false for some block heights due to consensus-preservation
    /// logic of historical blocks. Otherwise, a huge rollback would be needed.
    /// Cf.:
    /// <https://talk.neptune.cash/t/small-bug-in-lustration-counter-update-logic/286>
    ///
    /// # Panics
    /// - If called on a consensus rule set that does not require lustration.
    pub fn fix_lustration_double_counting(&self) -> bool {
        // TODO: Move this boolean to LustrationRule
        match self {
            ConsensusRuleSet::Reboot => unreachable!("Lustration not active"),
            ConsensusRuleSet::HardforkAlpha => unreachable!("Lustration not active"),
            ConsensusRuleSet::TvmProofVersion1 => unreachable!("Lustration not active"),
            ConsensusRuleSet::HardforkBeta => false,
            ConsensusRuleSet::HardforkGamma => true,
        }
    }

    /// The proof version used by this consensus rule set.
    pub fn triton_proof_version(&self) -> TritonProofVersion {
        match self {
            ConsensusRuleSet::Reboot => TritonProofVersion::V0,
            ConsensusRuleSet::HardforkAlpha => TritonProofVersion::V0,
            ConsensusRuleSet::TvmProofVersion1 => TritonProofVersion::V1,
            ConsensusRuleSet::HardforkBeta => TritonProofVersion::V1,
            ConsensusRuleSet::HardforkGamma => TritonProofVersion::V5,
        }
    }

    /// Maximum block size in number of BFieldElements
    pub const fn max_block_size(&self) -> usize {
        match self {
            ConsensusRuleSet::Reboot
            | ConsensusRuleSet::HardforkAlpha
            | ConsensusRuleSet::TvmProofVersion1
            | ConsensusRuleSet::HardforkBeta
            | ConsensusRuleSet::HardforkGamma => {
                // This size is 8MB which should keep it feasible to run archival nodes for
                // many years without requiring excessive disk space.
                1_000_000
            }
        }
    }

    pub fn max_num_inputs(&self) -> usize {
        match self {
            ConsensusRuleSet::Reboot
            | ConsensusRuleSet::HardforkAlpha
            | ConsensusRuleSet::TvmProofVersion1
            | ConsensusRuleSet::HardforkBeta
            | ConsensusRuleSet::HardforkGamma => MAX_NUM_INPUTS_OUTPUTS_ANNOUNCEMENTS,
        }
    }
    pub fn max_num_outputs(&self) -> usize {
        match self {
            ConsensusRuleSet::Reboot
            | ConsensusRuleSet::HardforkAlpha
            | ConsensusRuleSet::TvmProofVersion1
            | ConsensusRuleSet::HardforkBeta
            | ConsensusRuleSet::HardforkGamma => MAX_NUM_INPUTS_OUTPUTS_ANNOUNCEMENTS,
        }
    }
    pub fn max_num_announcements(&self) -> usize {
        match self {
            ConsensusRuleSet::Reboot
            | ConsensusRuleSet::HardforkAlpha
            | ConsensusRuleSet::TvmProofVersion1
            | ConsensusRuleSet::HardforkBeta
            | ConsensusRuleSet::HardforkGamma => MAX_NUM_INPUTS_OUTPUTS_ANNOUNCEMENTS,
        }
    }

    pub fn first_lustration_block(network: Network) -> BlockHeight {
        match network {
            Network::Main => BLOCK_HEIGHT_HARDFORK_BETA_MAIN_NET,
            Network::Testnet(0) => BLOCK_HEIGHT_HARDFORK_BETA_TESTNET,
            // Using block height one means that all blocks must lustrate,
            // except for the genesis block and block 1, and that only AOCL
            // leafs indistinguishable from premine AOCLs and those produced in
            // block 1 must lustrate.
            _ => BlockHeight::genesis().next(),
        }
    }

    pub fn latest_checkpoint(network: Network) -> BlockHeight {
        match network {
            Network::Main => BLOCK_HEIGHT_HARDFORK_GAMMA_MAIN_NET.previous().unwrap(),
            Network::Testnet(0) => BLOCK_HEIGHT_HARDFORK_GAMMA_TESTNET.previous().unwrap(),
            _ => BlockHeight::genesis(),
        }
    }

    pub(crate) fn lustration_rule(
        network: Network,
        block_height: BlockHeight,
        last_aocl_leaf_index: u64,
    ) -> Option<LustrationRule> {
        let premine = PREMINE_MAX_SIZE;
        let claims_pool = INITIAL_BLOCK_SUBSIDY
            .scalar_mul(u32::try_from(NUM_BLOCKS_SKIPPED_BECAUSE_REBOOT).unwrap());

        let first_hf_beta_block = Self::first_lustration_block(network);
        let first_hf_gamma_block = Self::hardfork_gamma_activation_block_height(network);

        assert!(
            first_hf_beta_block.get_generation().is_zero()
                && first_hf_gamma_block.get_generation().is_zero(),
            "This calculation assumes all transparency gateways start at generation zero."
        );

        let mined_at_hf_beta_activation =
            INITIAL_BLOCK_SUBSIDY.scalar_mul(u32::try_from(first_hf_beta_block.value()).unwrap());
        let mined_at_hf_gamma_activation =
            INITIAL_BLOCK_SUBSIDY.scalar_mul(u32::try_from(first_hf_gamma_block.value()).unwrap());
        let initial_counter_beta = premine + claims_pool + mined_at_hf_beta_activation;
        let initial_counter_gamma = premine + claims_pool + mined_at_hf_gamma_activation;

        if block_height < first_hf_beta_block {
            None
        } else if block_height == first_hf_beta_block {
            Some(LustrationRule::Initial(LustrationStatus {
                counter: initial_counter_beta,
                max_lustrating_aocl_leaf_index: last_aocl_leaf_index,
            }))
        } else if block_height < first_hf_gamma_block {
            Some(LustrationRule::Updated {
                initial_counter: initial_counter_beta,
            })
        } else if block_height == first_hf_gamma_block {
            Some(LustrationRule::Initial(LustrationStatus {
                counter: initial_counter_gamma,
                max_lustrating_aocl_leaf_index: last_aocl_leaf_index,
            }))
        } else {
            Some(LustrationRule::Updated {
                initial_counter: initial_counter_gamma,
            })
        }
    }

    fn hardfork_gamma_activation_block_height(network: Network) -> BlockHeight {
        let one = BlockHeight::genesis().next();
        match network {
            Network::Main => BLOCK_HEIGHT_HARDFORK_GAMMA_MAIN_NET,
            Network::Testnet(0) => BLOCK_HEIGHT_HARDFORK_GAMMA_TESTNET,
            Network::Testnet(_) => one,
            Network::TestnetMock => one,
            Network::RegTest => one,
            _ => one,
        }
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use neptune_primitives::difficulty_control::Difficulty;
    use tasm_lib::twenty_first::bfe;
    use tracing_test::traced_test;

    use super::*;
    use crate::block::validity::block_primitive_witness::BlockPrimitiveWitness;
    use crate::block::Block;
    use crate::proof_abstractions::test_runtime::tokio_runtime;
    use crate::type_scripts::native_currency_amount::NativeCurrencyAmount;

    #[test]
    fn lustration_counter_has_expected_initial_value() {
        let network = Network::Main;
        let first_lustration = ConsensusRuleSet::lustration_rule(
            network,
            BLOCK_HEIGHT_HARDFORK_BETA_MAIN_NET,
            100_000,
        )
        .unwrap();
        let LustrationRule::Initial(first_lustration) = first_lustration else {
            panic!("First lustration rule must be of type 'initial'");
        };

        // premine + redemption pool + miner rewards up to hard fork activation
        assert_eq!(
            NativeCurrencyAmount::coins(8_423_168),
            first_lustration.counter
        );
        assert_eq!(100_000, first_lustration.max_lustrating_aocl_leaf_index);

        let second_lustration = ConsensusRuleSet::lustration_rule(
            network,
            BLOCK_HEIGHT_HARDFORK_GAMMA_MAIN_NET,
            200_000,
        )
        .unwrap();
        let LustrationRule::Initial(second_lustration) = second_lustration else {
            panic!("Restarted lustration rule must be of type 'initial'");
        };
        assert_eq!(
            NativeCurrencyAmount::coins(8_717_568),
            second_lustration.counter
        );
        assert_eq!(200_000, second_lustration.max_lustrating_aocl_leaf_index);

        assert!(
            matches!(
                ConsensusRuleSet::lustration_rule(
                    network,
                    BLOCK_HEIGHT_HARDFORK_GAMMA_MAIN_NET.next(),
                    200_000,
                )
                .unwrap(),
                LustrationRule::Updated { .. },
            ),
            "Updated lustration rule must follow initial"
        );
    }

    #[test]
    fn future_and_past_memory_hardness() {
        assert!(ConsensusRuleSet::infer_from(Network::Main, 1_000u64.into()).memory_hard_pow());
        assert!(!ConsensusRuleSet::infer_from(Network::Main, 100_000u64.into()).memory_hard_pow());
    }

    #[test]
    fn future_and_past_lustration_rule() {
        let dummy_count = 55647;
        let network = Network::Main;
        assert!(
            ConsensusRuleSet::lustration_rule(network, 10_000u64.into(), dummy_count).is_none()
        );
        assert!(matches!(
            ConsensusRuleSet::lustration_rule(network, 100_000u64.into(), dummy_count).unwrap(),
            LustrationRule::Updated { .. }
        ));
    }

    #[test]
    fn expected_use_parent_difficulty() {
        assert!(ConsensusRuleSet::Reboot.use_parent_difficulty());
        assert!(ConsensusRuleSet::HardforkAlpha.use_parent_difficulty());
        assert!(ConsensusRuleSet::TvmProofVersion1.use_parent_difficulty());
        assert!(!ConsensusRuleSet::HardforkBeta.use_parent_difficulty());
        assert!(!ConsensusRuleSet::HardforkGamma.use_parent_difficulty());
    }

    #[test]
    fn expected_tvm_proof_versions() {
        assert_eq!(0, ConsensusRuleSet::Reboot.triton_proof_version().version());
        assert_eq!(
            0,
            ConsensusRuleSet::HardforkAlpha
                .triton_proof_version()
                .version()
        );
        assert_eq!(
            1,
            ConsensusRuleSet::TvmProofVersion1
                .triton_proof_version()
                .version()
        );
        assert_eq!(
            1,
            ConsensusRuleSet::HardforkBeta
                .triton_proof_version()
                .version()
        );
        assert_eq!(
            5,
            ConsensusRuleSet::HardforkGamma
                .triton_proof_version()
                .version(),
        );
    }

    #[test]
    fn tvm_v1_preceeds_hf_beta() {
        let network = Network::Main;
        let first_lustration_block = ConsensusRuleSet::first_lustration_block(network);
        assert_eq!(
            ConsensusRuleSet::TvmProofVersion1,
            ConsensusRuleSet::infer_from(network, first_lustration_block.previous().unwrap())
        );
        assert_eq!(
            ConsensusRuleSet::HardforkBeta,
            ConsensusRuleSet::infer_from(network, first_lustration_block)
        );

        let dummy_count = 55647;
        assert!(ConsensusRuleSet::lustration_rule(
            network,
            first_lustration_block.previous().unwrap(),
            dummy_count
        )
        .is_none(),);
        assert!(matches!(
            ConsensusRuleSet::lustration_rule(network, first_lustration_block, dummy_count),
            Some(LustrationRule::Initial(_)),
        ));
        assert!(matches!(
            ConsensusRuleSet::lustration_rule(network, first_lustration_block.next(), dummy_count),
            Some(LustrationRule::Updated { .. }),
        ));
    }

    #[traced_test]
    #[test]
    fn allow_non_zero_version() {
        // Start well into hardfork gamma
        let init_block_heigth = BlockHeight::from(59998u64);
        let network = Network::Main;
        let bpw = BlockPrimitiveWitness::deterministic_with_block_height_and_difficulty(
            init_block_heigth,
            Difficulty::MINIMUM,
            network,
        );

        tokio_runtime().block_on(new_block_allow_non_zero_version(bpw, network));

        async fn new_block_allow_non_zero_version(
            block_primitive_witness: BlockPrimitiveWitness,
            network: Network,
        ) {
            let (invalid_block, mut valid_successor) =
                Block::fake_block_pair_genesis_and_child_from_witness(block_primitive_witness)
                    .await;

            assert!(
                valid_successor
                    .is_valid(&invalid_block, valid_successor.header().timestamp, network)
                    .await
            );

            valid_successor.set_version_consistently(bfe!(5550001));
            assert!(
                valid_successor
                    .is_valid(&invalid_block, valid_successor.header().timestamp, network)
                    .await
            );

            let consensus_rule_set = ConsensusRuleSet::HardforkGamma;
            assert_eq!(
                consensus_rule_set,
                ConsensusRuleSet::infer_from(network, valid_successor.header().height)
            );

            valid_successor.satisfy_pow(invalid_block.header().difficulty, consensus_rule_set);
            assert!(
                valid_successor
                    .is_valid(&invalid_block, valid_successor.header().timestamp, network)
                    .await
            );
            assert!(valid_successor.has_proof_of_work(network, invalid_block.header()));
        }
    }
}