diff --git a/Cargo.lock b/Cargo.lock index 1aeb3e97a48..58cdc5d9d61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5149,6 +5149,7 @@ dependencies = [ "tari_common", "tari_common_types", "tari_comms", + "tari_features", "tari_utilities", "thiserror", "tokio", diff --git a/applications/tari_app_utilities/Cargo.toml b/applications/tari_app_utilities/Cargo.toml index 7adde4daa5b..bfffc9a7363 100644 --- a/applications/tari_app_utilities/Cargo.toml +++ b/applications/tari_app_utilities/Cargo.toml @@ -6,9 +6,10 @@ edition = "2018" license = "BSD-3-Clause" [dependencies] -tari_comms = { path = "../../comms/core" } tari_common = { path = "../../common" } tari_common_types = { path = "../../base_layer/common_types" } +tari_comms = { path = "../../comms/core" } +tari_features = { version = "0.0.1", path = "../../common/tari_features"} tari_utilities = { version = "0.4.10"} clap = { version = "3.2.0", features = ["derive", "env"] } @@ -22,3 +23,4 @@ thiserror = "^1.0.26" [build-dependencies] tari_common = { path = "../../common", features = ["build", "static-application-info"] } +tari_features = { version = "0.0.1", path = "../../common/tari_features"} diff --git a/applications/tari_app_utilities/build.rs b/applications/tari_app_utilities/build.rs index 29deabd66c6..8df78dd7367 100644 --- a/applications/tari_app_utilities/build.rs +++ b/applications/tari_app_utilities/build.rs @@ -21,8 +21,10 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use tari_common::build::StaticApplicationInfo; +use tari_features::resolver::build_features; fn main() -> Result<(), Box> { + build_features(); let gen = StaticApplicationInfo::initialize()?; gen.write_consts_to_outdir("consts.rs")?; Ok(()) diff --git a/applications/tari_app_utilities/src/lib.rs b/applications/tari_app_utilities/src/lib.rs index cdf9a3ebe7d..62243bc3b2b 100644 --- a/applications/tari_app_utilities/src/lib.rs +++ b/applications/tari_app_utilities/src/lib.rs @@ -22,6 +22,7 @@ pub mod common_cli_args; pub mod identity_management; +pub mod network_check; pub mod utilities; pub mod consts { diff --git a/applications/tari_app_utilities/src/network_check.rs b/applications/tari_app_utilities/src/network_check.rs new file mode 100644 index 00000000000..b59183a56a4 --- /dev/null +++ b/applications/tari_app_utilities/src/network_check.rs @@ -0,0 +1,70 @@ +// Copyright 2023. The Tari Project +// +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the +// following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following +// disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the +// following disclaimer in the documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote +// products derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +use tari_common::{ + configuration::Network, + exit_codes::{ExitCode, ExitError}, +}; +use tari_features::resolver::Target; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum NetworkCheckError { + #[error("The network {0} is invalid for this binary built for MainNet")] + MainNetBinary(Network), + #[error("The network {0} is invalid for this binary built for NextNet")] + NextNetBinary(Network), + #[error("The network {0} is invalid for this binary built for TestNet")] + TestNetBinary(Network), +} + +impl From for ExitError { + fn from(err: NetworkCheckError) -> Self { + Self::new(ExitCode::NetworkError, err) + } +} + +#[cfg(tari_network_mainnet)] +pub const TARGET_NETWORK: Target = Target::MainNet; + +#[cfg(tari_network_nextnet)] +pub const TARGET_NETWORK: Target = Target::NextNet; + +#[cfg(all(not(tari_network_mainnet), not(tari_network_nextnet)))] +pub const TARGET_NETWORK: Target = Target::TestNet; + +pub fn is_network_choice_valid(network: Network) -> Result<(), NetworkCheckError> { + match TARGET_NETWORK { + Target::MainNet => match network { + Network::MainNet | Network::StageNet => Ok(()), + _ => Err(NetworkCheckError::MainNetBinary(network)), + }, + Target::NextNet => match network { + Network::NextNet => Ok(()), + _ => Err(NetworkCheckError::NextNetBinary(network)), + }, + Target::TestNet => match network { + Network::LocalNet | Network::Igor | Network::Esmeralda => Ok(()), + _ => Err(NetworkCheckError::TestNetBinary(network)), + }, + } +} diff --git a/applications/tari_base_node/src/lib.rs b/applications/tari_base_node/src/lib.rs index cc4dd91bcbc..f3e2553a13c 100644 --- a/applications/tari_base_node/src/lib.rs +++ b/applications/tari_base_node/src/lib.rs @@ -42,7 +42,7 @@ use std::{process, sync::Arc}; use commands::{cli_loop::CliLoop, command::CommandContext}; use futures::FutureExt; use log::*; -use tari_app_utilities::common_cli_args::CommonCliArgs; +use tari_app_utilities::{common_cli_args::CommonCliArgs, network_check::is_network_choice_valid}; use tari_common::{ configuration::bootstrap::{grpc_default_port, ApplicationType}, exit_codes::{ExitCode, ExitError}, @@ -97,6 +97,8 @@ pub async fn run_base_node_with_cli( cli: Cli, shutdown: Shutdown, ) -> Result<(), ExitError> { + is_network_choice_valid(config.network())?; + #[cfg(feature = "metrics")] { metrics::install( diff --git a/applications/tari_console_wallet/src/lib.rs b/applications/tari_console_wallet/src/lib.rs index 84371360206..708cac000db 100644 --- a/applications/tari_console_wallet/src/lib.rs +++ b/applications/tari_console_wallet/src/lib.rs @@ -49,7 +49,7 @@ pub use cli::{ use init::{change_password, get_base_node_peer_config, init_wallet, start_wallet, tari_splash_screen, WalletBoot}; use log::*; use recovery::{get_seed_from_seed_words, prompt_private_key_from_seed_words}; -use tari_app_utilities::{common_cli_args::CommonCliArgs, consts}; +use tari_app_utilities::{common_cli_args::CommonCliArgs, consts, network_check::is_network_choice_valid}; use tari_common::{ configuration::bootstrap::ApplicationType, exit_codes::{ExitCode, ExitError}, @@ -114,6 +114,8 @@ pub fn run_wallet_with_cli( consts::APP_VERSION ); + is_network_choice_valid(config.wallet.network)?; + let password = get_password(config, &cli); if password.is_none() { diff --git a/common/tari_features/src/resolver.rs b/common/tari_features/src/resolver.rs index 869c14ef596..8dde5f5fa6d 100644 --- a/common/tari_features/src/resolver.rs +++ b/common/tari_features/src/resolver.rs @@ -32,6 +32,16 @@ pub enum Target { MainNet, } +impl Target { + pub const fn as_key_str(&self) -> &'static str { + match self { + Target::MainNet => "mainnet", + Target::NextNet => "nextnet", + Target::TestNet => "testnet", + } + } +} + impl Display for Target { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -109,6 +119,7 @@ pub fn build_features() { println!("cargo:rerun-if-env-changed=TARI_NETWORK"); let target = identify_target(); + println!("cargo:rustc-cfg=tari_network_{}", target.as_key_str()); println!("Building for {}", target); list_active_features(); list_removed_features();