Skip to content

Commit

Permalink
Add check to ensure the network matched the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Apr 4, 2023
1 parent 1311a0d commit 0b843e7
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion applications/tari_app_utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"}
2 changes: 2 additions & 0 deletions applications/tari_app_utilities/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
build_features();
let gen = StaticApplicationInfo::initialize()?;
gen.write_consts_to_outdir("consts.rs")?;
Ok(())
Expand Down
1 change: 1 addition & 0 deletions applications/tari_app_utilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

pub mod common_cli_args;
pub mod identity_management;
pub mod network_check;
pub mod utilities;

pub mod consts {
Expand Down
70 changes: 70 additions & 0 deletions applications/tari_app_utilities/src/network_check.rs
Original file line number Diff line number Diff line change
@@ -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<NetworkCheckError> 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)),
},
}
}
4 changes: 3 additions & 1 deletion applications/tari_base_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion applications/tari_console_wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions common/tari_features/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0b843e7

Please sign in to comment.