-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Description --- This PR is for reflective purposes regarding feature gating at compile time. See #5135 for more info. This PR as it stands is to be used as an example of a network _type_ based feature gate at compile time. It allows setting an ENVVAR for the network with conditional compilation for features. The feature sets can be imported across any crate in the project easily and then used with the `#[cfg(tari_feature_...)]` attribute macro. I've also introduced a secondary commit for binaries to perform a quick network check against itself to ensure the intended network is supported by the binary. This check is made non-invasive (not conditionally compiling possible network) as that method of check ends up being much more tedious. Motivation and Context --- We want some features on some networks, but maybe not all. How Has This Been Tested? --- Manually What process can a PR reviewer use to test or verify this change? --- Compile a binary with a set network `TARI_NETWORK=nextnet cargo build --release --bin tari_base_node` then run the binary with a test network as a parameter: `./tari_base_node --network esme` and receive an error: ``` The application exited because of an internal network error. The network esmeralda is invalid for this binary built for NextNet ``` *Please note* That running the binary will cause the build to default to the test network _always_. Meaning this will not work: ``` $ TARI_NETWORK=nextnet cargo build --release --bin tari_base_node $ cargo run --release --bin tari_base_node --network nextnet ``` The second command `cargo run` will re-build the binary, and without the `TARI_NETWORK` env set the binary will default to a test binary. In development if you want to call run and connect to a non test network you need to also pass the TARI_NETWORK envvar instead of the `--network` flag. `TARI_NETWORK=nextnet cargo run --bin tari_base_node --release -- --network nextnet` Breaking Changes --- - [x] None --------- Co-authored-by: Cayle Sharrock <CjS77@users.noreply.github.com> Co-authored-by: SW van Heerden <swvheerden@gmail.com>
- Loading branch information
1 parent
344040a
commit 72c19dc
Showing
19 changed files
with
418 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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, network) { | ||
(Target::MainNet, Network::MainNet | Network::StageNet) => Ok(()), | ||
(Target::MainNet, _) => Err(NetworkCheckError::MainNetBinary(network)), | ||
|
||
(Target::NextNet, Network::NextNet) => Ok(()), | ||
(Target::NextNet, _) => Err(NetworkCheckError::NextNetBinary(network)), | ||
|
||
(Target::TestNet, Network::LocalNet | Network::Igor | Network::Esmeralda) => Ok(()), | ||
(Target::TestNet, _) => Err(NetworkCheckError::TestNetBinary(network)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "tari_features" | ||
description = "Compilable features for Tari applications" | ||
authors = ["The Tari Development Community"] | ||
repository = "https://github.com/tari-project/tari" | ||
homepage = "https://tari.com" | ||
readme = "README.md" | ||
license = "BSD-3-Clause" | ||
version = "0.49.0-pre.6" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
tari_common = { path = "../../common" } |
Oops, something went wrong.