Skip to content

Commit

Permalink
feat: add network name to data path and --network flag to the miners (#…
Browse files Browse the repository at this point in the history
…5291)

Description
---
This PR does two jobs (sorry). Mostly because they're dependent tasks.

- Add `--network` to the miners for simple setting switches
- Add the network into the data directory path

Motivation and Context
---
This can be used to help select the default ports of communication for
the base node and wallet, and also easily keeping network id's and
folders separate.

How Has This Been Tested?
---
Manually

What process can a PR reviewer use to test or verify this change?
---
Run a base node, wallet, and miner. Set a base directory path as
something like `-b ./data/mytest` and the network to igor `--network
igor` and watch in amazement as the data directory `./data/mytest/igor/`
is created to store information.

Closes #5272
Closes #5273

Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->

---------

Co-authored-by: SW van Heerden <swvheerden@gmail.com>
  • Loading branch information
brianp and SWvheerden committed Apr 12, 2023
1 parent 72c19dc commit 1f04beb
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 74 deletions.
35 changes: 21 additions & 14 deletions applications/tari_app_utilities/src/common_cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ pub struct CommonCliArgs {
#[clap()]
pub log_level: Option<Level>,

/// Overrides for properties in the config file, e.g. -p base_node.netwok=esmeralda
/// Supply a network (overrides existing configuration)
#[clap(long, env = "TARI_NETWORK")]
pub network: Option<Network>,

/// Overrides for properties in the config file, e.g. -p base_node.network=esmeralda
#[clap(short = 'p', parse(try_from_str = parse_key_val), multiple_occurrences(true))]
pub config_property_overrides: Vec<(String, String)>,
}
Expand Down Expand Up @@ -74,14 +78,13 @@ impl CommonCliArgs {
if config_path.is_absolute() {
config_path
} else {
let mut base_path = PathBuf::from(&self.base_path);
base_path.push(config_path);
base_path
self.get_base_path().join(config_path)
}
}

pub fn get_base_path(&self) -> PathBuf {
PathBuf::from(&self.base_path)
let network = self.network.unwrap_or_default();
PathBuf::from(&self.base_path).join(network.to_string())
}

pub fn log_config_path(&self, application_name: &str) -> PathBuf {
Expand All @@ -90,24 +93,28 @@ impl CommonCliArgs {
if path.is_absolute() {
log_config.clone()
} else {
let mut base_path = PathBuf::from(&self.base_path);
base_path.push(log_config);
base_path
self.get_base_path().join(log_config)
}
} else {
let mut path = PathBuf::from(&self.base_path);
path.push("config");
path.push(application_name);
path.push("log4rs.yml");
path
self.get_base_path()
.join("config")
.join(application_name)
.join("log4rs.yml")
}
}
}

impl ConfigOverrideProvider for CommonCliArgs {
fn get_config_property_overrides(&self, _default_network: Network) -> Vec<(String, String)> {
let mut overrides = self.config_property_overrides.clone();
overrides.push(("common.base_path".to_string(), self.base_path.clone()));
overrides.push((
"common.base_path".to_string(),
self.get_base_path()
.as_os_str()
.to_str()
.expect("An os string from a path")
.into(),
));
overrides
}
}
Expand Down
6 changes: 2 additions & 4 deletions applications/tari_base_node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ pub struct Cli {
/// Watch a command in the non-interactive mode.
#[clap(long)]
pub watch: Option<String>,
/// Supply a network (overrides existing configuration)
#[clap(long, env = "TARI_NETWORK")]
pub network: Option<Network>,
#[clap(long, alias = "profile")]
pub profile_with_tokio_console: bool,
}

impl ConfigOverrideProvider for Cli {
fn get_config_property_overrides(&self, default_network: Network) -> Vec<(String, String)> {
let mut overrides = self.common.get_config_property_overrides(default_network);
let network = self.network.unwrap_or(default_network);
let network = self.common.network.unwrap_or(default_network);
overrides.push(("base_node.network".to_string(), network.to_string()));
overrides.push(("base_node.override_from".to_string(), network.to_string()));
overrides.push(("p2p.seeds.override_from".to_string(), network.to_string()));
overrides.push(("auto_update.override_from".to_string(), network.to_string()));
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_base_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ pub async fn run_base_node(
config: config_path.into_os_string().into_string().unwrap(),
log_config: None,
log_level: None,
network: None,
config_property_overrides: vec![],
},
init: true,
rebuild_db: false,
non_interactive_mode: true,
watch: None,
network: None,
profile_with_tokio_console: false,
};

Expand Down
3 changes: 0 additions & 3 deletions applications/tari_base_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ fn main_inner() -> Result<(), ExitError> {
);

let mut config = ApplicationConfig::load_from(&cfg)?;
if let Some(network) = cli.network {
config.base_node.network = network;
}
debug!(target: LOG_TARGET, "Using base node configuration: {:?}", config);

// Load or create the Node identity
Expand Down
5 changes: 1 addition & 4 deletions applications/tari_console_wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ pub struct Cli {
/// Automatically exit wallet command/script mode when done
#[clap(long, alias = "auto-exit")]
pub command_mode_auto_exit: bool,
/// Supply a network (overrides existing configuration)
#[clap(long, env = "TARI_NETWORK")]
pub network: Option<Network>,
#[clap(long, env = "TARI_WALLET_ENABLE_GRPC", alias = "enable-grpc")]
pub grpc_enabled: bool,
#[clap(long, env = "TARI_WALLET_GRPC_ADDRESS")]
Expand All @@ -93,7 +90,7 @@ pub struct Cli {
impl ConfigOverrideProvider for Cli {
fn get_config_property_overrides(&self, default_network: Network) -> Vec<(String, String)> {
let mut overrides = self.common.get_config_property_overrides(default_network);
let network = self.network.unwrap_or(default_network);
let network = self.common.network.unwrap_or(default_network);
overrides.push(("wallet.network".to_string(), network.to_string()));
overrides.push(("wallet.override_from".to_string(), network.to_string()));
overrides.push(("p2p.seeds.override_from".to_string(), network.to_string()));
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub fn run_wallet(shutdown: &mut Shutdown, runtime: Runtime, config: &mut Applic
config: config_path.into_os_string().into_string().unwrap(),
log_config: None,
log_level: None,
network: None,
config_property_overrides: vec![],
},
password: None,
Expand All @@ -92,7 +93,6 @@ pub fn run_wallet(shutdown: &mut Shutdown, runtime: Runtime, config: &mut Applic
command: None,
wallet_notify: None,
command_mode_auto_exit: false,
network: None,
grpc_enabled: true,
grpc_address: None,
command2: None,
Expand Down
6 changes: 2 additions & 4 deletions applications/tari_merge_mining_proxy/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ use tari_common::configuration::{ConfigOverrideProvider, Network};
pub struct Cli {
#[clap(flatten)]
pub common: CommonCliArgs,
/// Supply a network (overrides existing configuration)
#[clap(long, env = "TARI_NETWORK")]
pub network: Option<Network>,
}

impl ConfigOverrideProvider for Cli {
fn get_config_property_overrides(&self, default_network: Network) -> Vec<(String, String)> {
let mut overrides = self.common.get_config_property_overrides(default_network);
let network = self.network.unwrap_or(default_network);
let network = self.common.network.unwrap_or(default_network);
overrides.push(("merge_mining_proxy.override_from".to_string(), network.to_string()));
overrides.push(("merge_mining_proxy.network".to_string(), network.to_string()));
overrides
}
}
37 changes: 23 additions & 14 deletions applications/tari_merge_mining_proxy/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use serde::{Deserialize, Serialize};
use tari_common::{configuration::StringList, SubConfigPath};
use tari_common::{
configuration::{Network, StringList},
SubConfigPath,
};
use tari_comms::multiaddr::Multiaddr;
use tari_wallet_grpc_client::GrpcAuthentication;

Expand All @@ -39,9 +42,9 @@ pub struct MergeMiningProxyConfig {
/// If authentication is being used for curl
pub monerod_use_auth: bool,
/// The Tari base node's GRPC address
pub base_node_grpc_address: Multiaddr,
pub base_node_grpc_address: Option<Multiaddr>,
/// The Tari console wallet's GRPC address
pub console_wallet_grpc_address: Multiaddr,
pub console_wallet_grpc_address: Option<Multiaddr>,
/// GRPC authentication for console wallet
pub console_wallet_grpc_authentication: GrpcAuthentication,
/// Address of the tari_merge_mining_proxy application
Expand All @@ -64,6 +67,8 @@ pub struct MergeMiningProxyConfig {
/// Note that this data is publicly readable, but it is suggested you populate it so that
/// pool dominance can be seen before any one party has more than 51%.
pub coinbase_extra: String,
/// Selected network
pub network: Network,
}

impl Default for MergeMiningProxyConfig {
Expand All @@ -74,15 +79,16 @@ impl Default for MergeMiningProxyConfig {
monerod_username: String::new(),
monerod_password: String::new(),
monerod_use_auth: false,
base_node_grpc_address: "/ip4/127.0.0.1/tcp/18142".parse().unwrap(),
console_wallet_grpc_address: "/ip4/127.0.0.1/tcp/18143".parse().unwrap(),
base_node_grpc_address: None,
console_wallet_grpc_address: None,
console_wallet_grpc_authentication: GrpcAuthentication::default(),
listener_address: "/ip4/127.0.0.1/tcp/18081".parse().unwrap(),
submit_to_origin: true,
wait_for_initial_sync_at_startup: true,
check_tari_difficulty_before_submit: true,
max_randomx_vms: 5,
coinbase_extra: "tari_merge_mining_proxy".to_string(),
network: Default::default(),
}
}
}
Expand All @@ -95,7 +101,10 @@ impl SubConfigPath for MergeMiningProxyConfig {

#[cfg(test)]
mod test {
use std::str::FromStr;

use tari_common::DefaultConfigLoader;
use tari_comms::multiaddr::Multiaddr;

use crate::config::MergeMiningProxyConfig;

Expand Down Expand Up @@ -135,12 +144,12 @@ mod test {
assert_eq!(config.monerod_username.as_str(), "cmot");
assert_eq!(config.monerod_password.as_str(), "password_stagenet");
assert_eq!(
config.base_node_grpc_address.to_string().as_str(),
"/dns4/base_node_b/tcp/8080"
config.base_node_grpc_address,
Some(Multiaddr::from_str("/dns4/base_node_b/tcp/8080").unwrap())
);
assert_eq!(
config.console_wallet_grpc_address.to_string().as_str(),
"/dns4/wallet/tcp/9000"
config.console_wallet_grpc_address,
Some(Multiaddr::from_str("/dns4/wallet/tcp/9000").unwrap())
);

let cfg = get_config("config_a");
Expand All @@ -150,19 +159,19 @@ mod test {
assert_eq!(config.monerod_username.as_str(), "cmot");
assert_eq!(config.monerod_password.as_str(), "password_igor");
assert_eq!(
config.base_node_grpc_address.to_string().as_str(),
"/dns4/base_node_a/tcp/8080"
config.base_node_grpc_address,
Some(Multiaddr::from_str("/dns4/base_node_a/tcp/8080").unwrap())
);
assert_eq!(
config.console_wallet_grpc_address.to_string().as_str(),
"/dns4/wallet_a/tcp/9000"
config.console_wallet_grpc_address,
Some(Multiaddr::from_str("/dns4/wallet_a/tcp/9000").unwrap())
);
}

#[test]
fn default_config() {
let config = MergeMiningProxyConfig::default();
assert_eq!(&config.base_node_grpc_address.to_string(), "/ip4/127.0.0.1/tcp/18142");
assert_eq!(config.base_node_grpc_address, None);
assert!(!config.monerod_use_auth);
assert!(config.submit_to_origin);
}
Expand Down
47 changes: 43 additions & 4 deletions applications/tari_merge_mining_proxy/src/run_merge_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ use futures::future;
use hyper::{service::make_service_fn, Server};
use log::*;
use tari_base_node_grpc_client::BaseNodeGrpcClient;
use tari_common::{load_configuration, DefaultConfigLoader};
use tari_common::{
configuration::bootstrap::{grpc_default_port, ApplicationType},
load_configuration,
DefaultConfigLoader,
};
use tari_comms::utils::multiaddr::multiaddr_to_socketaddr;
use tari_core::proof_of_work::randomx_factory::RandomXFactory;
use tari_wallet_grpc_client::WalletGrpcClient;
Expand All @@ -44,7 +48,8 @@ const LOG_TARGET: &str = "tari_mm_proxy::proxy";
pub async fn start_merge_miner(cli: Cli) -> Result<(), anyhow::Error> {
let config_path = cli.common.config_path();
let cfg = load_configuration(&config_path, true, &cli)?;
let config = MergeMiningProxyConfig::load_from(&cfg)?;
let mut config = MergeMiningProxyConfig::load_from(&cfg)?;
setup_grpc_config(&mut config);

info!(target: LOG_TARGET, "Configuration: {:?}", config);
let client = reqwest::Client::builder()
Expand All @@ -54,11 +59,21 @@ pub async fn start_merge_miner(cli: Cli) -> Result<(), anyhow::Error> {
.build()
.map_err(MmProxyError::ReqwestError)?;

let base_node = multiaddr_to_socketaddr(&config.base_node_grpc_address)?;
let base_node = multiaddr_to_socketaddr(
config
.base_node_grpc_address
.as_ref()
.expect("No base node address provided"),
)?;
info!(target: LOG_TARGET, "Connecting to base node at {}", base_node);
println!("Connecting to base node at {}", base_node);
let base_node_client = BaseNodeGrpcClient::connect(format!("http://{}", base_node)).await?;
let wallet_addr = multiaddr_to_socketaddr(&config.console_wallet_grpc_address)?;
let wallet_addr = multiaddr_to_socketaddr(
config
.console_wallet_grpc_address
.as_ref()
.expect("No waller address provided"),
)?;
info!(target: LOG_TARGET, "Connecting to wallet at {}", wallet_addr);
let wallet_addr = format!("http://{}", wallet_addr);
let wallet_client =
Expand Down Expand Up @@ -94,3 +109,27 @@ pub async fn start_merge_miner(cli: Cli) -> Result<(), anyhow::Error> {
},
}
}

fn setup_grpc_config(config: &mut MergeMiningProxyConfig) {
if config.base_node_grpc_address.is_none() {
config.base_node_grpc_address = Some(
format!(
"/ip4/127.0.0.1/tcp/{}",
grpc_default_port(ApplicationType::BaseNode, config.network)
)
.parse()
.unwrap(),
);
}

if config.console_wallet_grpc_address.is_none() {
config.console_wallet_grpc_address = Some(
format!(
"/ip4/127.0.0.1/tcp/{}",
grpc_default_port(ApplicationType::ConsoleWallet, config.network)
)
.parse()
.unwrap(),
);
}
}
10 changes: 10 additions & 0 deletions applications/tari_miner/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use clap::Parser;
use tari_app_utilities::common_cli_args::CommonCliArgs;
use tari_common::configuration::{ConfigOverrideProvider, Network};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand All @@ -37,3 +38,12 @@ pub struct Cli {
#[clap(long, alias = "max-difficulty")]
pub miner_max_diff: Option<u64>,
}

impl ConfigOverrideProvider for Cli {
fn get_config_property_overrides(&self, default_network: Network) -> Vec<(String, String)> {
let mut overrides = self.common.get_config_property_overrides(default_network);
let network = self.common.network.unwrap_or(default_network);
overrides.push(("miner.network".to_string(), network.to_string()));
overrides
}
}
Loading

0 comments on commit 1f04beb

Please sign in to comment.