Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Sep 16, 2024
1 parent 9a74a42 commit a977bc0
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 48 deletions.
25 changes: 20 additions & 5 deletions applications/minotari_console_wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,40 @@ pub struct Cli {
}

impl ConfigOverrideProvider for Cli {
/// Get the configuration property overrides for the given network. In case of duplicates, the final override
/// added to the list will have preference.
fn get_config_property_overrides(&self, network: &mut Network) -> Vec<(String, String)> {
let mut overrides = self.common.get_config_property_overrides(network);
let mut overrides = vec![];
*network = self.common.network.unwrap_or(*network);
// Config file overrides
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()));
// Either of these configs enable grpc
// Command-line overrides
let command_line_overrides = self.common.get_config_property_overrides(network);
command_line_overrides.iter().for_each(|(k, v)| {
replace_or_add_override(&mut overrides, k, v);
});
// Logical overrides based on command-line flags - Either of these configs enable grpc
if let Some(ref addr) = self.grpc_address {
overrides.push(("wallet.grpc_enabled".to_string(), "true".to_string()));
overrides.push(("wallet.grpc_address".to_string(), addr.clone()));
replace_or_add_override(&mut overrides, "wallet.grpc_enabled", "true");
replace_or_add_override(&mut overrides, "wallet.grpc_address", addr);
} else if self.grpc_enabled {
overrides.push(("wallet.grpc_enabled".to_string(), "true".to_string()));
replace_or_add_override(&mut overrides, "wallet.grpc_enabled", "true");
} else {
// GRPC is disabled
}
overrides
}
}

fn replace_or_add_override(overrides: &mut Vec<(String, String)>, key: &str, value: &str) {
if let Some(index) = overrides.iter().position(|(k, _)| k == key) {
overrides.remove(index);
}
overrides.push((key.to_string(), value.to_string()));
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Subcommand, Clone)]
pub enum CliCommands {
Expand Down
10 changes: 8 additions & 2 deletions applications/minotari_console_wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::process;

use clap::Parser;
use log::*;
use minotari_app_utilities::consts;
use minotari_console_wallet::{run_wallet_with_cli, ApplicationConfig, Cli};
use tari_common::{
configuration::bootstrap::{grpc_default_port, ApplicationType},
Expand Down Expand Up @@ -69,14 +70,19 @@ fn main() {

fn main_inner() -> Result<(), ExitError> {
let cli = Cli::parse();

let cfg = load_configuration(cli.common.config_path(), true, cli.non_interactive_mode, &cli)?;
let base_path = cli.common.get_base_path();
initialize_logging(
&cli.common.log_config_path("wallet"),
cli.common.log_path.as_ref().unwrap_or(&base_path),
include_str!("../log4rs_sample.yml"),
)?;
info!(
target: LOG_TARGET,
"Starting Minotari Console Wallet version: {}",
consts::APP_VERSION
);

let cfg = load_configuration(cli.common.config_path(), true, cli.non_interactive_mode, &cli)?;

if cli.profile_with_tokio_console {
// Uncomment to enable tokio tracing via tokio-console
Expand Down
17 changes: 16 additions & 1 deletion applications/minotari_merge_mining_proxy/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,26 @@ pub struct Cli {
}

impl ConfigOverrideProvider for Cli {
/// Get the configuration property overrides for the given network. In case of duplicates, the final override
/// added to the list will have preference.
fn get_config_property_overrides(&self, network: &mut Network) -> Vec<(String, String)> {
let mut overrides = self.common.get_config_property_overrides(network);
let mut overrides = vec![];
*network = self.common.network.unwrap_or(*network);
// Config file overrides
overrides.push(("merge_mining_proxy.override_from".to_string(), network.to_string()));
overrides.push(("merge_mining_proxy.network".to_string(), network.to_string()));
// Command-line overrides
let command_line_overrides = self.common.get_config_property_overrides(network);
command_line_overrides.iter().for_each(|(k, v)| {
replace_or_add_override(&mut overrides, k, v);
});
overrides
}
}

fn replace_or_add_override(overrides: &mut Vec<(String, String)>, key: &str, value: &str) {
if let Some(index) = overrides.iter().position(|(k, _)| k == key) {
overrides.remove(index);
}
overrides.push((key.to_string(), value.to_string()));
}
5 changes: 5 additions & 0 deletions applications/minotari_merge_mining_proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ async fn main() -> Result<(), anyhow::Error> {
cli.common.log_path.as_ref().unwrap_or(&base_path),
include_str!("../log4rs_sample.yml"),
)?;
info!(
target: LOG_TARGET,
"Starting Minotari Merge Mining Proxy version: {}",
consts::APP_VERSION
);
match run_merge_miner::start_merge_miner(cli).await {
Ok(_) => Ok(()),
Err(err) => {
Expand Down
17 changes: 16 additions & 1 deletion applications/minotari_miner/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,25 @@ pub struct Cli {
}

impl ConfigOverrideProvider for Cli {
/// Get the configuration property overrides for the given network. In case of duplicates, the final override
/// added to the list will have preference.
fn get_config_property_overrides(&self, network: &mut Network) -> Vec<(String, String)> {
let mut overrides = self.common.get_config_property_overrides(network);
let mut overrides = vec![];
// Config file overrides
*network = self.common.network.unwrap_or(*network);
overrides.push(("miner.network".to_string(), network.to_string()));
// Command-line overrides
let command_line_overrides = self.common.get_config_property_overrides(network);
command_line_overrides.iter().for_each(|(k, v)| {
replace_or_add_override(&mut overrides, k, v);
});
overrides
}
}

fn replace_or_add_override(overrides: &mut Vec<(String, String)>, key: &str, value: &str) {
if let Some(index) = overrides.iter().position(|(k, _)| k == key) {
overrides.remove(index);
}
overrides.push((key.to_string(), value.to_string()));
}
5 changes: 5 additions & 0 deletions applications/minotari_miner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,10 @@ async fn main_inner() -> Result<(), ExitError> {
&cli.common.get_base_path(),
include_str!("../log4rs_sample.yml"),
)?;
info!(
target: LOG_TARGET,
"Starting Minotari Miner version: {}",
consts::APP_VERSION
);
start_miner(cli).await
}
28 changes: 22 additions & 6 deletions applications/minotari_node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,41 @@ pub struct Cli {
}

impl ConfigOverrideProvider for Cli {
/// Get the configuration property overrides for the given network. In case of duplicates, the final override
/// added to the list will have preference.
fn get_config_property_overrides(&self, network: &mut Network) -> Vec<(String, String)> {
let mut overrides = self.common.get_config_property_overrides(network);
let mut overrides = vec![];
*network = self.common.network.unwrap_or(*network);
// Config file overrides
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()));
overrides.push(("metrics.override_from".to_string(), network.to_string()));
// Command-line overrides
let command_line_overrides = self.common.get_config_property_overrides(network);
command_line_overrides.iter().for_each(|(k, v)| {
replace_or_add_override(&mut overrides, k, v);
});
// Logical overrides based on command-line flags
if self.grpc_enabled {
overrides.push(("base_node.grpc_enabled".to_string(), "true".to_string()));
replace_or_add_override(&mut overrides, "base_node.grpc_enabled", "true");
}
if self.mining_enabled {
overrides.push(("base_node.grpc_enabled".to_string(), "true".to_string()));
overrides.push(("base_node.mining_enabled".to_string(), "true".to_string()));
replace_or_add_override(&mut overrides, "base_node.grpc_enabled", "true");
replace_or_add_override(&mut overrides, "base_node.mining_enabled", "true");
}
if self.second_layer_grpc_enabled {
overrides.push(("base_node.grpc_enabled".to_string(), "true".to_string()));
overrides.push(("base_node.second_layer_grpc_enabled".to_string(), "true".to_string()));
replace_or_add_override(&mut overrides, "base_node.grpc_enabled", "true");
replace_or_add_override(&mut overrides, "base_node.second_layer_grpc_enabled", "true");
}
overrides
}
}

fn replace_or_add_override(overrides: &mut Vec<(String, String)>, key: &str, value: &str) {
if let Some(index) = overrides.iter().position(|(k, _)| k == key) {
overrides.remove(index);
}
overrides.push((key.to_string(), value.to_string()));
}
15 changes: 8 additions & 7 deletions applications/minotari_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,26 @@ fn main() {

fn main_inner() -> Result<(), ExitError> {
let cli = Cli::parse();
let config_path = cli.common.config_path();
let cfg = load_configuration(config_path, true, cli.non_interactive_mode, &cli)?;

if cli.profile_with_tokio_console {
console_subscriber::init();
}

let base_path = cli.common.get_base_path();
initialize_logging(
&cli.common.log_config_path("base_node"),
cli.common.log_path.as_ref().unwrap_or(&base_path),
include_str!("../log4rs_sample.yml"),
)?;

info!(
target: LOG_TARGET,
"Starting Minotari Base Node version: {}",
consts::APP_VERSION
);

let config_path = cli.common.config_path();
let cfg = load_configuration(config_path, true, cli.non_interactive_mode, &cli)?;

if cli.profile_with_tokio_console {
console_subscriber::init();
}

#[cfg(all(unix, feature = "libtor"))]
let mut config = ApplicationConfig::load_from(&cfg)?;
#[cfg(not(all(unix, feature = "libtor")))]
Expand Down
4 changes: 2 additions & 2 deletions base_layer/chat_ffi/src/application_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use tari_chat_client::{
config::{ApplicationConfig, ChatClientConfig},
networking::Multiaddr,
};
use tari_common::configuration::{MultiaddrList, Network, StringList};
use tari_p2p::{PeerSeedsConfig, TransportConfig, TransportType, DEFAULT_DNS_NAME_SERVER};
use tari_common::configuration::{name_server::DEFAULT_DNS_NAME_SERVER, MultiaddrList, Network, StringList};
use tari_p2p::{PeerSeedsConfig, TransportConfig, TransportType};

use crate::error::{InterfaceError, LibChatError};

Expand Down
3 changes: 2 additions & 1 deletion base_layer/p2p/src/auto_update/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ mod test {
mod dns_software_update {
use std::time::Duration;

use tari_common::configuration::name_server::DEFAULT_DNS_NAME_SERVER;

use super::*;
use crate::DEFAULT_DNS_NAME_SERVER;

impl AutoUpdateConfig {
fn get_test_defaults() -> Self {
Expand Down
16 changes: 2 additions & 14 deletions base_layer/p2p/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ use tari_common::{
use tari_comms::multiaddr::Multiaddr;
use tari_comms_dht::{DbConnectionUrl, DhtConfig};

use crate::{transport::TransportConfig, DEFAULT_DNS_NAME_SERVER};
use crate::transport::TransportConfig;

/// Peer seed configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PeerSeedsConfig {
pub override_from: Option<String>,
Expand All @@ -65,18 +65,6 @@ pub struct PeerSeedsConfig {
pub dns_seeds_use_dnssec: bool,
}

impl Default for PeerSeedsConfig {
fn default() -> Self {
Self {
override_from: None,
peer_seeds: StringList::default(),
dns_seeds: StringList::default(),
dns_seeds_name_server: DEFAULT_DNS_NAME_SERVER.parse().unwrap(),
dns_seeds_use_dnssec: false,
}
}
}

impl SubConfigPath for PeerSeedsConfig {
fn main_key_prefix() -> &'static str {
"p2p.seeds"
Expand Down
3 changes: 0 additions & 3 deletions base_layer/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ pub use transport::{Socks5TransportConfig, TcpTransportConfig, TorTransportConfi

pub use self::config::{P2pConfig, PeerSeedsConfig};

/// Default DNS resolver set to cloudflare's private 1.1.1.1 resolver
pub const DEFAULT_DNS_NAME_SERVER: &str = "1.1.1.1:853/cloudflare-dns.com";

/// Major network version. Peers will refuse connections if this value differs
pub const MAJOR_NETWORK_VERSION: u8 = 0;
/// Minor network version. This should change with each time the network protocol has changed in a backward-compatible
Expand Down
3 changes: 2 additions & 1 deletion base_layer/p2p/src/peer_seeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ mod test {
}

mod peer_seed_resolver {
use tari_common::configuration::name_server::DEFAULT_DNS_NAME_SERVER;
use trust_dns_client::{
proto::{
op::Query,
Expand All @@ -235,7 +236,7 @@ mod test {
};

use super::*;
use crate::{dns::mock, DEFAULT_DNS_NAME_SERVER};
use crate::dns::mock;

#[tokio::test]
#[ignore = "Useful for developer testing but will fail unless the DNS has TXT records setup correctly."]
Expand Down
3 changes: 1 addition & 2 deletions base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ use minotari_wallet::{
use num_traits::FromPrimitive;
use rand::rngs::OsRng;
use tari_common::{
configuration::{MultiaddrList, StringList},
configuration::{name_server::DEFAULT_DNS_NAME_SERVER, MultiaddrList, StringList},
network_check::set_network_if_choice_valid,
};
use tari_common_types::{
Expand Down Expand Up @@ -171,7 +171,6 @@ use tari_p2p::{
TorTransportConfig,
TransportConfig,
TransportType,
DEFAULT_DNS_NAME_SERVER,
};
use tari_script::TariScript;
use tari_shutdown::Shutdown;
Expand Down
7 changes: 5 additions & 2 deletions common/src/configuration/name_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ use std::{
use anyhow::anyhow;
use serde::Deserialize;

/// Default DNS resolver set to cloudflare's private 1.1.1.1 resolver
pub const DEFAULT_DNS_NAME_SERVER: &str = "1.1.1.1:853/cloudflare-dns.com";

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct DnsNameServer {
pub addr: SocketAddr,
Expand All @@ -43,7 +46,7 @@ impl DnsNameServer {

impl Default for DnsNameServer {
fn default() -> Self {
Self::from_str("1.1.1.1:853/cloudflare-dns.com").expect("is valid")
Self::from_str(DEFAULT_DNS_NAME_SERVER).expect("is valid")
}
}

Expand Down Expand Up @@ -95,7 +98,7 @@ mod test {
// default
assert_eq!(
DnsNameServer::default(),
DnsNameServer::from_str("1.1.1.1:853/cloudflare-dns.com").unwrap()
DnsNameServer::from_str(DEFAULT_DNS_NAME_SERVER).unwrap()
);
}
}
7 changes: 6 additions & 1 deletion common/src/configuration/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::{fmt, fmt::Display, fs, fs::File, io::Write, marker::PhantomData, path::Path, str::FromStr};

use config::Config;
use log::{debug, info};
use log::{debug, info, trace};
use serde::{
de::{self, MapAccess, Visitor},
Deserialize,
Expand Down Expand Up @@ -84,6 +84,8 @@ pub fn load_configuration_with_overrides<P: AsRef<Path>, TOverride: ConfigOverri

info!(target: LOG_TARGET, "Configuration file loaded.");
let overrides = overrides.get_config_property_overrides(&mut network);
debug!(target: LOG_TARGET, "Config property overrides: {:?}", overrides);

// Set the static network variable according to the user chosen network (for use with
// `get_current_or_user_setting_or_default()`) -
set_network_if_choice_valid(network)?;
Expand All @@ -93,14 +95,17 @@ pub fn load_configuration_with_overrides<P: AsRef<Path>, TOverride: ConfigOverri
}

let mut cfg = Config::builder().add_source(cfg);
trace!(target: LOG_TARGET, "Config before overrides: {:?}", cfg);
for (key, value) in overrides {
trace!(target: LOG_TARGET, "Set override: ({}, {})", key, value);
cfg = cfg
.set_override(key.as_str(), value.as_str())
.map_err(|ce| ConfigError::new("Could not override config property", Some(ce.to_string())))?;
}
let cfg = cfg
.build()
.map_err(|ce| ConfigError::new("Could not build config", Some(ce.to_string())))?;
trace!(target: LOG_TARGET, "Config with overrides: {:?}", cfg);

Ok(cfg)
}
Expand Down

0 comments on commit a977bc0

Please sign in to comment.