@@ -637,6 +637,57 @@ pub fn create_unit_test_config() -> ChainConfig {
637
637
. build ( )
638
638
}
639
639
640
+ /// This function ensure that IgnoreConsensus will never be used in anything other than regtest
641
+ pub fn assert_no_ignore_consensus_in_chain_config ( chain_config : & ChainConfig ) {
642
+ match chain_config. chain_type ( ) {
643
+ ChainType :: Regtest => {
644
+ return ;
645
+ }
646
+ ChainType :: Mainnet | ChainType :: Testnet | ChainType :: Signet => { }
647
+ }
648
+
649
+ let upgrades = chain_config. net_upgrade ( ) ;
650
+
651
+ let all_upgrades = upgrades. all_upgrades ( ) ;
652
+
653
+ assert ! (
654
+ !all_upgrades. is_empty( ) ,
655
+ "Invalid chain config. There are no net-upgrades defined, not even for genesis."
656
+ ) ;
657
+
658
+ assert ! ( all_upgrades. len( ) >= 2 , "Invalid chain config. There must be at least 2 net-upgrades defined, one for genesis and one for the first block after genesis." ) ;
659
+
660
+ assert ! (
661
+ all_upgrades[ 0 ] . 0 == 0 . into( ) ,
662
+ "Invalid chain config. The first net-upgrade must be at height 0"
663
+ ) ;
664
+
665
+ assert ! (
666
+ upgrades. consensus_status( 0 . into( ) ) == RequiredConsensus :: IgnoreConsensus ,
667
+ "Invalid chain config. The genesis net-upgrade must be IgnoreConsensus"
668
+ ) ;
669
+
670
+ assert ! (
671
+ upgrades. consensus_status( 1 . into( ) ) != RequiredConsensus :: IgnoreConsensus ,
672
+ "Invalid chain config. The net-upgrade at height 1 must not be IgnoreConsensus"
673
+ ) ;
674
+
675
+ for upgrade in all_upgrades. iter ( ) . skip ( 1 ) {
676
+ let upgrade_height = & upgrade. 0 ;
677
+ let upgrade_data = & upgrade. 1 ;
678
+
679
+ let consensus = upgrades. consensus_status ( * upgrade_height) ;
680
+ assert_ne ! (
681
+ RequiredConsensus :: IgnoreConsensus ,
682
+ consensus,
683
+ "Upgrade {:?} at height {} is ignoring consensus in net type {}. This is only allowed in regtest" ,
684
+ upgrade_data,
685
+ upgrade_height,
686
+ chain_config. chain_type( ) . name( )
687
+ )
688
+ }
689
+ }
690
+
640
691
#[ cfg( test) ]
641
692
mod tests {
642
693
use super :: * ;
@@ -816,4 +867,32 @@ mod tests {
816
867
. build ( ) ;
817
868
assert_eq ! ( expected_epoch, config. sealed_epoch_index( & block_height) ) ;
818
869
}
870
+
871
+ #[ test]
872
+ fn test_ignore_consensus_in_mainnet ( ) {
873
+ let config = create_mainnet ( ) ;
874
+
875
+ assert_no_ignore_consensus_in_chain_config ( & config) ;
876
+ }
877
+
878
+ #[ test]
879
+ #[ should_panic(
880
+ expected = "Invalid chain config. There must be at least 2 net-upgrades defined, one for genesis and one for the first block after genesis."
881
+ ) ]
882
+ fn test_ignore_consensus_outside_regtest_in_no_upgrades ( ) {
883
+ let config =
884
+ Builder :: new ( ChainType :: Mainnet ) . net_upgrades ( NetUpgrades :: unit_tests ( ) ) . build ( ) ;
885
+
886
+ assert_no_ignore_consensus_in_chain_config ( & config) ;
887
+ }
888
+
889
+ #[ test]
890
+ #[ should_panic( expected = "The net-upgrade at height 1 must not be IgnoreConsensus" ) ]
891
+ fn test_ignore_consensus_outside_regtest_with_deliberate_bad_upgrades ( ) {
892
+ let config = Builder :: new ( ChainType :: Mainnet )
893
+ . net_upgrades ( NetUpgrades :: deliberate_ignore_consensus_twice ( ) )
894
+ . build ( ) ;
895
+
896
+ assert_no_ignore_consensus_in_chain_config ( & config) ;
897
+ }
819
898
}
0 commit comments