Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to ensure that consensus will never be IgnoreConsensus in production #1252

Merged
merged 4 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions common/src/chain/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,57 @@ pub fn create_unit_test_config() -> ChainConfig {
.build()
}

/// This function ensure that IgnoreConsensus will never be used in anything other than regtest
pub fn assert_no_ignore_consensus_in_chain_config(chain_config: &ChainConfig) {
match chain_config.chain_type() {
ChainType::Regtest => {
return;
}
ChainType::Mainnet | ChainType::Testnet | ChainType::Signet => {}
}

let upgrades = chain_config.net_upgrade();

let all_upgrades = upgrades.all_upgrades();

assert!(
!all_upgrades.is_empty(),
"Invalid chain config. There are no net-upgrades defined, not even for genesis."
);

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.");

assert!(
all_upgrades[0].0 == 0.into(),
"Invalid chain config. The first net-upgrade must be at height 0"
);

assert!(
upgrades.consensus_status(0.into()) == RequiredConsensus::IgnoreConsensus,
"Invalid chain config. The genesis net-upgrade must be IgnoreConsensus"
);

assert!(
upgrades.consensus_status(1.into()) != RequiredConsensus::IgnoreConsensus,
"Invalid chain config. The net-upgrade at height 1 must not be IgnoreConsensus"
);

for upgrade in all_upgrades.iter().skip(1) {
let upgrade_height = &upgrade.0;
let upgrade_data = &upgrade.1;

let consensus = upgrades.consensus_status(*upgrade_height);
assert_ne!(
RequiredConsensus::IgnoreConsensus,
consensus,
"Upgrade {:?} at height {} is ignoring consensus in net type {}. This is only allowed in regtest",
upgrade_data,
upgrade_height,
chain_config.chain_type().name()
)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -816,4 +867,32 @@ mod tests {
.build();
assert_eq!(expected_epoch, config.sealed_epoch_index(&block_height));
}

#[test]
fn test_ignore_consensus_in_mainnet() {
let config = create_mainnet();

assert_no_ignore_consensus_in_chain_config(&config);
}

#[test]
#[should_panic(
expected = "Invalid chain config. There must be at least 2 net-upgrades defined, one for genesis and one for the first block after genesis."
)]
fn test_ignore_consensus_outside_regtest_in_no_upgrades() {
let config =
Builder::new(ChainType::Mainnet).net_upgrades(NetUpgrades::unit_tests()).build();

assert_no_ignore_consensus_in_chain_config(&config);
}

#[test]
#[should_panic(expected = "The net-upgrade at height 1 must not be IgnoreConsensus")]
fn test_ignore_consensus_outside_regtest_with_deliberate_bad_upgrades() {
let config = Builder::new(ChainType::Mainnet)
.net_upgrades(NetUpgrades::deliberate_ignore_consensus_twice())
.build();

assert_no_ignore_consensus_in_chain_config(&config);
}
}
18 changes: 18 additions & 0 deletions common/src/chain/upgrades/netupgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ impl NetUpgrades<UpgradeVersion> {
)])
}

#[cfg(test)]
pub fn deliberate_ignore_consensus_twice() -> Self {
Self(vec![
TheQuantumPhysicist marked this conversation as resolved.
Show resolved Hide resolved
(
BlockHeight::zero(),
UpgradeVersion::ConsensusUpgrade(ConsensusUpgrade::IgnoreConsensus),
),
(
BlockHeight::new(1),
UpgradeVersion::ConsensusUpgrade(ConsensusUpgrade::IgnoreConsensus),
),
])
}

pub fn regtest_with_pos() -> Self {
Self(vec![
(
Expand All @@ -57,6 +71,10 @@ impl NetUpgrades<UpgradeVersion> {
),
])
}

pub fn all_upgrades(&self) -> &[(BlockHeight, UpgradeVersion)] {
&self.0
}
}

pub trait Activate {
Expand Down
3 changes: 3 additions & 0 deletions node-lib/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use chainstate::{rpc::ChainstateRpcServer, ChainstateError, InitializationError}
use common::{
chain::{
config::{
assert_no_ignore_consensus_in_chain_config,
regtest::{create_regtest_pos_genesis, create_regtest_pow_genesis},
Builder as ChainConfigBuilder, ChainConfig, ChainType, EmissionScheduleTabular,
},
Expand Down Expand Up @@ -86,6 +87,8 @@ async fn initialize(
) -> Result<(subsystem::Manager, NodeController)> {
let chain_config = Arc::new(chain_config);

assert_no_ignore_consensus_in_chain_config(&chain_config);

// INITIALIZE SUBSYSTEMS

let mut manager = subsystem::Manager::new("mintlayer");
Expand Down
Loading